KoGlobal.cpp 4.96 KB
Newer Older
Casper Boemann's avatar
Casper Boemann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* This file is part of the KDE project
   Copyright (C) 2001 David Faure <faure@kde.org>
   Copyright 2003 Nicolas GOUTTE <goutte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

Thomas Zander's avatar
Thomas Zander committed
21 22
#include "KoGlobal.h"

23
#include <KoConfig.h>
24
#include <KoResourcePaths.h>
Casper Boemann's avatar
Casper Boemann committed
25

26 27 28
#include <QPaintDevice>
#include <QFont>
#include <QFontInfo>
29
#include <QFontDatabase>
30
#include <QGlobalStatic>
Casper Boemann's avatar
Casper Boemann committed
31

32
#include <WidgetsDebug.h>
33
#include <kconfiggroup.h>
34
#include <klocalizedstring.h>
35
#include <ksharedconfig.h>
Casper Boemann's avatar
Casper Boemann committed
36 37
#include <kconfig.h>

38
Q_GLOBAL_STATIC(KoGlobal, s_instance)
Casper Boemann's avatar
Casper Boemann committed
39 40 41

KoGlobal* KoGlobal::self()
{
42
    return s_instance;
Casper Boemann's avatar
Casper Boemann committed
43 44 45
}

KoGlobal::KoGlobal()
46 47
    : m_pointSize(-1)
    , m_calligraConfig(0)
Casper Boemann's avatar
Casper Boemann committed
48
{
49
    // Fixes a bug where values from some config files are not picked up
50
    // due to  KSharedConfig::openConfig() being initialized before paths have been set up above.
51
    // NOTE: Values set without a sync() call before KoGlobal has been initialized will not stick
52
     KSharedConfig::openConfig()->reparseConfiguration();
Casper Boemann's avatar
Casper Boemann committed
53 54 55 56
}

KoGlobal::~KoGlobal()
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
57
    delete m_calligraConfig;
Casper Boemann's avatar
Casper Boemann committed
58 59 60 61
}

QStringList KoGlobal::_listOfLanguageTags()
{
62
    if (m_langMap.isEmpty())
Casper Boemann's avatar
Casper Boemann committed
63 64 65 66 67 68
        createListOfLanguages();
    return m_langMap.values();
}

QStringList KoGlobal::_listOfLanguages()
{
69
    if (m_langMap.empty())
Casper Boemann's avatar
Casper Boemann committed
70 71 72 73 74 75
        createListOfLanguages();
    return m_langMap.keys();
}

void KoGlobal::createListOfLanguages()
{
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    KConfig config("all_languages", KConfig::NoGlobals);
    // Note that we could also use KLocale::allLanguagesTwoAlpha

    QMap<QString, bool> seenLanguages;
    const QStringList langlist = config.groupList();
    for (QStringList::ConstIterator itall = langlist.begin();
            itall != langlist.end(); ++itall) {
        const QString tag = *itall;
        const QString name = config.group(tag).readEntry("Name", tag);
        // e.g. name is "French" and tag is "fr"

        // The QMap does the sorting on the display-name, so that
        // comboboxes are sorted.
        m_langMap.insert(name, tag);

        seenLanguages.insert(tag, true);
    }

    // Also take a look at the installed translations.
    // Many of them are already in all_languages but all_languages doesn't
    // currently have en_GB or en_US etc.

98 99 100
    QStringList translationList = KoResourcePaths::findAllResources("locale",
                                        QString::fromLatin1("*/entry.desktop"))
            + KoResourcePaths::findAllResources("locale",
101
                                                QString::fromLatin1("*/kf5_entry.desktop"));
102
    translationList.removeDuplicates();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
103
    for (QStringList::Iterator it = translationList.begin();
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
            it != translationList.end(); ++it) {
        // Extract the language tag from the directory name
        QString tag = *it;
        int index = tag.lastIndexOf('/');
        tag = tag.left(index);
        index = tag.lastIndexOf('/');
        tag = tag.mid(index + 1);

        if (seenLanguages.find(tag) == seenLanguages.end()) {
            KConfig entry(*it, KConfig::SimpleConfig);

            const QString name = entry.group("KCM Locale").readEntry("Name", tag);
            // e.g. name is "US English" and tag is "en_US"
            m_langMap.insert(name, tag);

            // enable this if writing a third way of finding languages below
            //seenLanguages.insert( tag, true );
        }

    }

    // #### We also might not have an entry for a language where spellchecking is supported,
    //      but no KDE translation is available, like fr_CA.
    // How to add them?
Casper Boemann's avatar
Casper Boemann committed
128 129
}

130
QString KoGlobal::tagOfLanguage(const QString & _lang)
Casper Boemann's avatar
Casper Boemann committed
131 132
{
    const LanguageMap& map = self()->m_langMap;
133 134
    QMap<QString, QString>::ConstIterator it = map.find(_lang);
    if (it != map.end())
Casper Boemann's avatar
Casper Boemann committed
135 136 137 138
        return *it;
    return QString();
}

139
QString KoGlobal::languageFromTag(const QString &langTag)
Casper Boemann's avatar
Casper Boemann committed
140 141
{
    const LanguageMap& map = self()->m_langMap;
142 143 144 145
    QMap<QString, QString>::ConstIterator it = map.begin();
    const QMap<QString, QString>::ConstIterator end = map.end();
    for (; it != end; ++it)
        if (it.value() == langTag)
Casper Boemann's avatar
Casper Boemann committed
146 147 148 149 150 151
            return it.key();

    // Language code not found. Better return the code (tag) than nothing.
    return langTag;
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
152
KConfig* KoGlobal::_calligraConfig()
Casper Boemann's avatar
Casper Boemann committed
153
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
154
    if (!m_calligraConfig) {
155
        m_calligraConfig = new KConfig("kritarc");
Casper Boemann's avatar
Casper Boemann committed
156
    }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
157
    return m_calligraConfig;
Casper Boemann's avatar
Casper Boemann committed
158
}