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 
00038 KoGlobal* KoGlobal::s_global = 0L;
00039 static KStaticDeleter<KoGlobal> sdg;
00040 
00041 KoGlobal* KoGlobal::self()
00042 {
00043     if ( !s_global )
00044         sdg.setObject( s_global, new KoGlobal );
00045     return s_global;
00046 }
00047 
00048 KoGlobal::KoGlobal()
00049     : m_pointSize( -1 ), m_kofficeConfig( 0L )
00050 {
00051     // Make sure that QCString::setNum doesn't give us "," as decimal point, e.g. when saving to OpenDocument.
00052     setlocale( LC_NUMERIC, "C" );
00053 
00054     // Install the libkoffice* translations
00055     KGlobal::locale()->insertCatalogue("koffice");
00056 
00057     KImageIO::registerFormats();
00058 
00059     // Tell KStandardDirs about the koffice prefix
00060     KGlobal::dirs()->addPrefix(PREFIX);
00061 
00062     // Tell the iconloader about share/apps/koffice/icons
00063     KGlobal::iconLoader()->addAppDir("koffice");
00064 
00065     // Another way to get the DPI of the display would be QPaintDeviceMetrics,
00066     // but we have no widget here (and moving this to KoView wouldn't allow
00067     // using this from the document easily).
00068 #ifdef Q_WS_X11
00069     m_dpiX = QPaintDevice::x11AppDpiX();
00070     m_dpiY = QPaintDevice::x11AppDpiY();
00071 #else
00072     m_dpiX = 75;
00073     m_dpiY = 75;
00074 #endif
00075 }
00076 
00077 KoGlobal::~KoGlobal()
00078 {
00079     delete m_kofficeConfig;
00080 }
00081 
00082 QFont KoGlobal::_defaultFont()
00083 {
00084     QFont font = KGlobalSettings::generalFont();
00085     // we have to use QFontInfo, in case the font was specified with a pixel size
00086     if ( font.pointSize() == -1 )
00087     {
00088         // cache size into m_pointSize, since QFontInfo loads the font -> slow
00089         if ( m_pointSize == -1 )
00090             m_pointSize = QFontInfo(font).pointSize();
00091         Q_ASSERT( m_pointSize != -1 );
00092         font.setPointSize( m_pointSize );
00093     }
00094     //kdDebug()<<k_funcinfo<<"QFontInfo(font).pointSize() :"<<QFontInfo(font).pointSize()<<endl;
00095     //kdDebug()<<k_funcinfo<<"font.name() :"<<font.family ()<<endl;
00096     return font;
00097 }
00098 
00099 QStringList KoGlobal::_listOfLanguageTags()
00100 {
00101     if ( m_langMap.isEmpty() )
00102         createListOfLanguages();
00103     return m_langMap.values();
00104 }
00105 
00106 QStringList KoGlobal::_listOfLanguages()
00107 {
00108     if ( m_langMap.empty() )
00109         createListOfLanguages();
00110     return m_langMap.keys();
00111 }
00112 
00113 void KoGlobal::createListOfLanguages()
00114 {
00115     KConfig config( "all_languages", true, false, "locale" );
00116     // Note that we could also use KLocale::allLanguagesTwoAlpha
00117 
00118     QMap<QString, bool> seenLanguages;
00119     const QStringList langlist = config.groupList();
00120     for ( QStringList::ConstIterator itall = langlist.begin();
00121           itall != langlist.end(); ++itall )
00122     {
00123         const QString tag = *itall;
00124         config.setGroup( tag );
00125         const QString name = config.readEntry("Name", tag);
00126         // e.g. name is "French" and tag is "fr"
00127 
00128         // The QMap does the sorting on the display-name, so that
00129         // comboboxes are sorted.
00130         m_langMap.insert( name, tag );
00131 
00132         seenLanguages.insert( tag, true );
00133     }
00134 
00135     // Also take a look at the installed translations.
00136     // Many of them are already in all_languages but all_languages doesn't
00137     // currently have en_GB or en_US etc.
00138 
00139     const QStringList translationList = KGlobal::dirs()->findAllResources("locale",
00140                                                             QString::fromLatin1("*/entry.desktop"));
00141     for ( QStringList::ConstIterator it = translationList.begin();
00142           it != translationList.end(); ++it )
00143     {
00144         // Extract the language tag from the directory name
00145         QString tag = *it;
00146         int index = tag.findRev('/');
00147         tag = tag.left(index);
00148         index = tag.findRev('/');
00149         tag = tag.mid(index+1);
00150 
00151         if ( seenLanguages.find( tag ) == seenLanguages.end() ) {
00152             KSimpleConfig entry(*it);
00153             entry.setGroup("KCM Locale");
00154 
00155             const QString name = entry.readEntry("Name", tag);
00156             // e.g. name is "US English" and tag is "en_US"
00157             m_langMap.insert( name, tag );
00158 
00159             // enable this if writing a third way of finding languages below
00160             //seenLanguages.insert( tag, true );
00161         }
00162 
00163     }
00164 
00165     // #### We also might not have an entry for a language where spellchecking is supported,
00166     //      but no KDE translation is available, like fr_CA.
00167     // How to add them?
00168 }
00169 
00170 QString KoGlobal::tagOfLanguage( const QString & _lang)
00171 {
00172     const LanguageMap& map = self()->m_langMap;
00173     QMap<QString,QString>::ConstIterator it = map.find( _lang );
00174     if ( it != map.end() )
00175         return *it;
00176     return QString::null;
00177 }
00178 
00179 QString KoGlobal::languageFromTag( const QString &langTag )
00180 {
00181     const LanguageMap& map = self()->m_langMap;
00182     QMap<QString,QString>::ConstIterator it = map.begin();
00183     const QMap<QString,QString>::ConstIterator end = map.end();
00184     for ( ; it != end; ++it )
00185         if ( it.data() == langTag )
00186             return it.key();
00187 
00188     // Language code not found. Better return the code (tag) than nothing.
00189     return langTag;
00190 }
00191 
00192 KConfig* KoGlobal::_kofficeConfig()
00193 {
00194     if ( !m_kofficeConfig ) {
00195         m_kofficeConfig = new KConfig( "kofficerc" );
00196     }
00197     return m_kofficeConfig;
00198 }
00199 
00200 void KoGlobal::setDPI( int x, int y )
00201 {
00202     //kdDebug() << k_funcinfo << x << "," << y << endl;
00203     KoGlobal* s = self();
00204     s->m_dpiX = x;
00205     s->m_dpiY = y;
00206 }
KDE Home | KDE Accessibility Home | Description of Access Keys