lib Library API Documentation

koGlobal.cc

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