lib

KoGlobal.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <faure@kde.org>
00003    Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "config.h"
00022 #include <KoGlobal.h>
00023 #include <kdebug.h>
00024 #include <qfont.h>
00025 #include <qfontinfo.h>
00026 #include <kglobalsettings.h>
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 #include <ksimpleconfig.h>
00030 #include <kstandarddirs.h>
00031 #include <kstaticdeleter.h>
00032 #include <kimageio.h>
00033 #include <kiconloader.h>
00034 #include <kstandarddirs.h>
00035 #include <locale.h>
00036 
00037 KoGlobal* KoGlobal::s_global = 0L;
00038 static KStaticDeleter<KoGlobal> sdg;
00039 
00040 KoGlobal* KoGlobal::self()
00041 {
00042     if ( !s_global )
00043         sdg.setObject( s_global, new KoGlobal );
00044     return s_global;
00045 }
00046 
00047 KoGlobal::KoGlobal()
00048     : m_pointSize( -1 ), m_kofficeConfig( 0L )
00049 {
00050     // Make sure that QCString::setNum doesn't give us "," as decimal point, e.g. when saving to OpenDocument.
00051     setlocale( LC_NUMERIC, "C" );
00052 
00053     // Install the libkoffice* translations
00054     KGlobal::locale()->insertCatalogue("koffice");
00055 
00056     KImageIO::registerFormats();
00057 
00058     // Tell KStandardDirs about the koffice prefix
00059     KGlobal::dirs()->addPrefix(PREFIX);
00060 
00061     // Tell the iconloader about share/apps/koffice/icons
00062     KGlobal::iconLoader()->addAppDir("koffice");
00063 
00064     // Another way to get the DPI of the display would be QPaintDeviceMetrics,
00065     // but we have no widget here (and moving this to KoView wouldn't allow
00066     // using this from the document easily).
00067 #ifdef Q_WS_X11
00068     m_dpiX = QPaintDevice::x11AppDpiX();
00069     m_dpiY = QPaintDevice::x11AppDpiY();
00070 #else
00071     m_dpiX = 75;
00072     m_dpiY = 75;
00073 #endif
00074 }
00075 
00076 KoGlobal::~KoGlobal()
00077 {
00078     delete m_kofficeConfig;
00079 }
00080 
00081 QFont KoGlobal::_defaultFont()
00082 {
00083     QFont font = KGlobalSettings::generalFont();
00084     // we have to use QFontInfo, in case the font was specified with a pixel size
00085     if ( font.pointSize() == -1 )
00086     {
00087         // cache size into m_pointSize, since QFontInfo loads the font -> slow
00088         if ( m_pointSize == -1 )
00089             m_pointSize = QFontInfo(font).pointSize();
00090         Q_ASSERT( m_pointSize != -1 );
00091         font.setPointSize( m_pointSize );
00092     }
00093     //kdDebug()<<k_funcinfo<<"QFontInfo(font).pointSize() :"<<QFontInfo(font).pointSize()<<endl;
00094     //kdDebug()<<k_funcinfo<<"font.name() :"<<font.family ()<<endl;
00095     return font;
00096 }
00097 
00098 QStringList KoGlobal::_listOfLanguageTags()
00099 {
00100     if ( m_langMap.isEmpty() )
00101         createListOfLanguages();
00102     return m_langMap.values();
00103 }
00104 
00105 QStringList KoGlobal::_listOfLanguages()
00106 {
00107     if ( m_langMap.empty() )
00108         createListOfLanguages();
00109     return m_langMap.keys();
00110 }
00111 
00112 void KoGlobal::createListOfLanguages()
00113 {
00114     KConfig config( "all_languages", true, false, "locale" );
00115     // Note that we could also use KLocale::allLanguagesTwoAlpha
00116 
00117     QMap<QString, bool> seenLanguages;
00118     const QStringList langlist = config.groupList();
00119     for ( QStringList::ConstIterator itall = langlist.begin();
00120           itall != langlist.end(); ++itall )
00121     {
00122         const QString tag = *itall;
00123         config.setGroup( tag );
00124         const QString name = config.readEntry("Name", tag);
00125         // e.g. name is "French" and tag is "fr"
00126 
00127         // The QMap does the sorting on the display-name, so that
00128         // comboboxes are sorted.
00129         m_langMap.insert( name, tag );
00130 
00131         seenLanguages.insert( tag, true );
00132     }
00133 
00134     // Also take a look at the installed translations.
00135     // Many of them are already in all_languages but all_languages doesn't
00136     // currently have en_GB or en_US etc.
00137 
00138     const QStringList translationList = KGlobal::dirs()->findAllResources("locale",
00139                                                             QString::fromLatin1("*/entry.desktop"));
00140     for ( QStringList::ConstIterator it = translationList.begin();
00141           it != translationList.end(); ++it )
00142     {
00143         // Extract the language tag from the directory name
00144         QString tag = *it;
00145         int index = tag.findRev('/');
00146         tag = tag.left(index);
00147         index = tag.findRev('/');
00148         tag = tag.mid(index+1);
00149 
00150         if ( seenLanguages.find( tag ) == seenLanguages.end() ) {
00151             KSimpleConfig entry(*it);
00152             entry.setGroup("KCM Locale");
00153 
00154             const QString name = entry.readEntry("Name", tag);
00155             // e.g. name is "US English" and tag is "en_US"
00156             m_langMap.insert( name, tag );
00157 
00158             // enable this if writing a third way of finding languages below
00159             //seenLanguages.insert( tag, true );
00160         }
00161 
00162     }
00163 
00164     // #### We also might not have an entry for a language where spellchecking is supported,
00165     //      but no KDE translation is available, like fr_CA.
00166     // How to add them?
00167 }
00168 
00169 QString KoGlobal::tagOfLanguage( const QString & _lang)
00170 {
00171     const LanguageMap& map = self()->m_langMap;
00172     QMap<QString,QString>::ConstIterator it = map.find( _lang );
00173     if ( it != map.end() )
00174         return *it;
00175     return QString::null;
00176 }
00177 
00178 QString KoGlobal::languageFromTag( const QString &langTag )
00179 {
00180     const LanguageMap& map = self()->m_langMap;
00181     QMap<QString,QString>::ConstIterator it = map.begin();
00182     const QMap<QString,QString>::ConstIterator end = map.end();
00183     for ( ; it != end; ++it )
00184         if ( it.data() == langTag )
00185             return it.key();
00186 
00187     // Language code not found. Better return the code (tag) than nothing.
00188     return langTag;
00189 }
00190 
00191 KConfig* KoGlobal::_kofficeConfig()
00192 {
00193     if ( !m_kofficeConfig ) {
00194         m_kofficeConfig = new KConfig( "kofficerc" );
00195     }
00196     return m_kofficeConfig;
00197 }
00198 
00199 void KoGlobal::setDPI( int x, int y )
00200 {
00201     //kdDebug() << k_funcinfo << x << "," << y << endl;
00202     KoGlobal* s = self();
00203     s->m_dpiX = x;
00204     s->m_dpiY = y;
00205 }
KDE Home | KDE Accessibility Home | Description of Access Keys