lib

KoGlobal.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3 Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "config.h"
22#include <KoGlobal.h>
23#include <kdebug.h>
24#include <tqfont.h>
25#include <tqfontinfo.h>
26#include <tdeglobalsettings.h>
27#include <tdeglobal.h>
28#include <tdelocale.h>
29#include <ksimpleconfig.h>
30#include <kstandarddirs.h>
31#include <kstaticdeleter.h>
32#include <kimageio.h>
33#include <kiconloader.h>
34#include <kstandarddirs.h>
35#include <locale.h>
36
37KoGlobal* KoGlobal::s_global = 0L;
38static KStaticDeleter<KoGlobal> sdg;
39
40KoGlobal* KoGlobal::self()
41{
42 if ( !s_global )
43 sdg.setObject( s_global, new KoGlobal );
44 return s_global;
45}
46
47KoGlobal::KoGlobal()
48 : m_pointSize( -1 ), m_kofficeConfig( 0L )
49{
50 // Make sure that TQCString::setNum doesn't give us "," as decimal point, e.g. when saving to OpenDocument.
51 setlocale( LC_NUMERIC, "C" );
52
53 // Install the libkoffice* translations
54 TDEGlobal::locale()->insertCatalogue("koffice");
55
56 KImageIO::registerFormats();
57
58 // Tell TDEStandardDirs about the koffice prefix
59 TDEGlobal::dirs()->addPrefix(PREFIX);
60
61 // Tell the iconloader about share/apps/koffice/icons
62 TDEGlobal::iconLoader()->addAppDir("koffice");
63
64 // Another way to get the DPI of the display would be TQPaintDeviceMetrics,
65 // but we have no widget here (and moving this to KoView wouldn't allow
66 // using this from the document easily).
67#ifdef TQ_WS_X11
68 m_dpiX = TQPaintDevice::x11AppDpiX();
69 m_dpiY = TQPaintDevice::x11AppDpiY();
70#else
71 m_dpiX = 75;
72 m_dpiY = 75;
73#endif
74}
75
76KoGlobal::~KoGlobal()
77{
78 delete m_kofficeConfig;
79}
80
81TQFont KoGlobal::_defaultFont()
82{
83 TQFont font = TDEGlobalSettings::generalFont();
84 // we have to use TQFontInfo, in case the font was specified with a pixel size
85 if ( font.pointSize() == -1 )
86 {
87 // cache size into m_pointSize, since TQFontInfo loads the font -> slow
88 if ( m_pointSize == -1 )
89 m_pointSize = TQFontInfo(font).pointSize();
90 Q_ASSERT( m_pointSize != -1 );
91 font.setPointSize( m_pointSize );
92 }
93 //kdDebug()<<k_funcinfo<<"TQFontInfo(font).pointSize() :"<<TQFontInfo(font).pointSize()<<endl;
94 //kdDebug()<<k_funcinfo<<"font.name() :"<<font.family ()<<endl;
95 return font;
96}
97
98TQStringList KoGlobal::_listOfLanguageTags()
99{
100 if ( m_langMap.isEmpty() )
101 createListOfLanguages();
102 return m_langMap.values();
103}
104
105TQStringList KoGlobal::_listOfLanguages()
106{
107 if ( m_langMap.empty() )
108 createListOfLanguages();
109 return m_langMap.keys();
110}
111
112void KoGlobal::createListOfLanguages()
113{
114 TDEConfig config( "all_languages", true, false, "locale" );
115 // Note that we could also use TDELocale::allLanguagesTwoAlpha
116
117 TQMap<TQString, bool> seenLanguages;
118 const TQStringList langlist = config.groupList();
119 for ( TQStringList::ConstIterator itall = langlist.begin();
120 itall != langlist.end(); ++itall )
121 {
122 const TQString tag = *itall;
123 config.setGroup( tag );
124 const TQString name = config.readEntry("Name", tag);
125 // e.g. name is "French" and tag is "fr"
126
127 // The TQMap does the sorting on the display-name, so that
128 // comboboxes are sorted.
129 m_langMap.insert( name, tag );
130
131 seenLanguages.insert( tag, true );
132 }
133
134 // Also take a look at the installed translations.
135 // Many of them are already in all_languages but all_languages doesn't
136 // currently have en_GB or en_US etc.
137
138 const TQStringList translationList = TDEGlobal::dirs()->findAllResources("locale",
139 TQString::fromLatin1("*/entry.desktop"));
140 for ( TQStringList::ConstIterator it = translationList.begin();
141 it != translationList.end(); ++it )
142 {
143 // Extract the language tag from the directory name
144 TQString tag = *it;
145 int index = tag.findRev('/');
146 tag = tag.left(index);
147 index = tag.findRev('/');
148 tag = tag.mid(index+1);
149
150 if ( seenLanguages.find( tag ) == seenLanguages.end() ) {
151 KSimpleConfig entry(*it);
152 entry.setGroup("KCM Locale");
153
154 const TQString name = entry.readEntry("Name", tag);
155 // e.g. name is "US English" and tag is "en_US"
156 m_langMap.insert( name, tag );
157
158 // enable this if writing a third way of finding languages below
159 //seenLanguages.insert( tag, true );
160 }
161
162 }
163
164 // #### We also might not have an entry for a language where spellchecking is supported,
165 // but no KDE translation is available, like fr_CA.
166 // How to add them?
167}
168
169TQString KoGlobal::tagOfLanguage( const TQString & _lang)
170{
171 const LanguageMap& map = self()->m_langMap;
172 TQMap<TQString,TQString>::ConstIterator it = map.find( _lang );
173 if ( it != map.end() )
174 return *it;
175 return TQString();
176}
177
178TQString KoGlobal::languageFromTag( const TQString &langTag )
179{
180 const LanguageMap& map = self()->m_langMap;
181 TQMap<TQString,TQString>::ConstIterator it = map.begin();
182 const TQMap<TQString,TQString>::ConstIterator end = map.end();
183 for ( ; it != end; ++it )
184 if ( it.data() == langTag )
185 return it.key();
186
187 // Language code not found. Better return the code (tag) than nothing.
188 return langTag;
189}
190
191TDEConfig* KoGlobal::_kofficeConfig()
192{
193 if ( !m_kofficeConfig ) {
194 m_kofficeConfig = new TDEConfig( "kofficerc" );
195 }
196 return m_kofficeConfig;
197}
198
199void KoGlobal::setDPI( int x, int y )
200{
201 //kdDebug() << k_funcinfo << x << "," << y << endl;
202 KoGlobal* s = self();
203 s->m_dpiX = x;
204 s->m_dpiY = y;
205}
const char * name(StdAction id)