kglobal.cpp
/* This file is part of the KDE libraries
Copyright (C) 1999 Sirtaj Singh Kanq <taj@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 version 2 as published by the Free Software Foundation.
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.
*/
/*
* kglobal.cpp -- Implementation of class KGlobal.
* Author: Sirtaj Singh Kang
* Version: $Id: kglobal.cpp 700792 2007-08-16 12:57:23Z mueller $
* Generated: Sat May 1 02:08:43 EST 1999
*/
#include <qglobal.h>
#include <qdict.h>
#include <qptrlist.h>
#include "kglobal.h"
#include <kapplication.h>
#include <kaboutdata.h>
#include <kdebug.h>
#include <kconfig.h>
#include <klocale.h>
#include <kcharsets.h>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include "kstaticdeleter.h"
#include <qfont.h>
#ifndef NDEBUG
#define MYASSERT(x) if (!x) \
qFatal("Fatal error: you need to have a KInstance object before\n" \
"you do anything that requires it! Examples of this are config\n" \
"objects, standard directories or translations.");
#else
#define MYASSERT(x) /* nope */
#endif
static void kglobal_init();
KStandardDirs *KGlobal::dirs()
{
MYASSERT(_instance);
return _instance->dirs();
}
KConfig *KGlobal::config()
{
MYASSERT(_instance);
return _instance->config();
}
KSharedConfig *KGlobal::sharedConfig()
{
MYASSERT(_instance);
return _instance->sharedConfig();
}
KIconLoader *KGlobal::iconLoader()
{
MYASSERT(_instance);
return _instance->iconLoader();
}
KInstance *KGlobal::instance()
{
MYASSERT(_instance);
return _instance;
}
KLocale *KGlobal::locale()
{
if( _locale == 0 ) {
if (!_instance)
return 0;
kglobal_init();
// will set _locale if it works - otherwise 0 is returned
KLocale::initInstance();
if( _instance->aboutData())
_instance->aboutData()->translateInternalProgramName();
}
return _locale;
}
KCharsets *KGlobal::charsets()
{
if( _charsets == 0 ) {
_charsets =new KCharsets();
kglobal_init();
}
return _charsets;
}
void KGlobal::setActiveInstance(KInstance *i)
{
_activeInstance = i;
if (i && _locale)
_locale->setActiveCatalogue(QString::fromUtf8(i->instanceName()));
}
const QString &
KGlobal::staticQString(const char *str)
{
return staticQString(QString::fromLatin1(str));
}
class KStringDict : public QDict<QString>
{
public:
KStringDict() : QDict<QString>(139) { }
};
const QString &
KGlobal::staticQString(const QString &str)
{
if (!_stringDict) {
_stringDict = new KStringDict;
_stringDict->setAutoDelete( true );
kglobal_init();
}
QString *result = _stringDict->find(str);
if (!result)
{
result = new QString(str);
_stringDict->insert(str, result);
}
return *result;
}
class KStaticDeleterList: public QPtrList<KStaticDeleterBase>
{
public:
KStaticDeleterList() { }
};
void
KGlobal::registerStaticDeleter(KStaticDeleterBase *obj)
{
if (!_staticDeleters)
kglobal_init();
if (_staticDeleters->find(obj) == -1)
_staticDeleters->append(obj);
}
void
KGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj)
{
if (_staticDeleters)
_staticDeleters->removeRef(obj);
}
void
KGlobal::deleteStaticDeleters()
{
if (!KGlobal::_staticDeleters)
return;
for(;_staticDeleters->count();)
{
_staticDeleters->take(0)->destructObject();
}
delete KGlobal::_staticDeleters;
KGlobal::_staticDeleters = 0;
}
// The Variables
KStringDict *KGlobal::_stringDict = 0;
KInstance *KGlobal::_instance = 0;
KInstance *KGlobal::_activeInstance = 0;
KLocale *KGlobal::_locale = 0;
KCharsets *KGlobal::_charsets = 0;
KStaticDeleterList *KGlobal::_staticDeleters = 0;
#ifdef WIN32
#include <windows.h>
static void kglobal_freeAll();
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad )
{
if (reason == DLL_PROCESS_DETACH)
kglobal_freeAll();
return TRUE;
}
#else
__attribute__((destructor))
#endif
static void kglobal_freeAll()
{
delete KGlobal::_locale;
KGlobal::_locale = 0;
delete KGlobal::_charsets;
KGlobal::_charsets = 0;
delete KGlobal::_stringDict;
KGlobal::_stringDict = 0;
KGlobal::deleteStaticDeleters();
// so that we don't hold a reference and see memory leaks :/
KGlobal::setActiveInstance(0);
}
static void kglobal_init()
{
if (KGlobal::_staticDeleters)
return;
KGlobal::_staticDeleters = new KStaticDeleterList;
}
int kasciistricmp( const char *str1, const char *str2 )
{
const unsigned char *s1 = (const unsigned char *)str1;
const unsigned char *s2 = (const unsigned char *)str2;
int res;
unsigned char c1, c2;
if ( !s1 || !s2 )
return s1 ? 1 : (s2 ? -1 : 0);
if ( !*s1 || !*s2 )
return *s1 ? 1 : (*s2 ? -1 : 0);
for (;*s1; ++s1, ++s2) {
c1 = *s1; c2 = *s2;
if (c1 >= 'A' && c1 <= 'Z')
c1 += 'a' - 'A';
if (c2 >= 'A' && c2 <= 'Z')
c2 += 'a' - 'A';
if ((res = c1 - c2))
break;
}
return *s1 ? res : (*s2 ? -1 : 0);
}
KInstance::instanceName
QCString instanceName() const
Returns the name of the instance.
Definition: kinstance.cpp:263
KSharedConfig
KConfig variant using shared memory.
Definition: kconfig.h:273
KGlobal::locale
static KLocale * locale()
Returns the global locale object.
Definition: kglobal.cpp:88
KCharsets
Charset font and encoder/decoder handling.
Definition: kcharsets.h:43
KGlobal::staticQString
static const QString & staticQString(const char *str)
Creates a static QString.
Definition: kglobal.cpp:128
KGlobal::charsets
static KCharsets * charsets()
The global charset manager.
Definition: kglobal.cpp:104
KGlobal::iconLoader
static KIconLoader * iconLoader()
Returns an iconloader object.
Definition: kglobal.cpp:75
KGlobal::setActiveInstance
static void setActiveInstance(KInstance *d)
The instance currently active (useful in a multi-instance application, such as a KParts application)...
Definition: kglobal.cpp:114
KInstance::config
KConfig * config() const
Returns the general config object ("appnamerc").
Definition: kinstance.cpp:177
KInstance::aboutData
const KAboutData * aboutData() const
Returns the about data of this instance Warning, can be 0L.
Definition: kinstance.cpp:257
klocale.h
KInstance::iconLoader
KIconLoader * iconLoader() const
Returns an iconloader object.
Definition: kinstance.cpp:239
KGlobal::dirs
static KStandardDirs * dirs()
Returns the application standard dirs object.
Definition: kglobal.cpp:54
KGlobal::registerStaticDeleter
static void registerStaticDeleter(KStaticDeleterBase *d)
Registers a static deleter.
Definition: kglobal.cpp:169
KStandardDirs
Site-independent access to standard KDE directories.
Definition: kstandarddirs.h:125
KLocale::setActiveCatalogue
void setActiveCatalogue(const QString &catalog)
Sets the active catalog for translation lookup.
Definition: klocale.cpp:687
KInstance
Access to KDE global objects for use in shared libraries.
Definition: kinstance.h:43
KGlobal::instance
static KInstance * instance()
Returns the global instance.
Definition: kglobal.cpp:82
KConfig
Access KDE Configuration entries.
Definition: kconfig.h:43
KLocale
KLocale provides support for country specific stuff like the national language.
Definition: klocale.h:124
KGlobal::unregisterStaticDeleter
static void unregisterStaticDeleter(KStaticDeleterBase *d)
Unregisters a static deleter.
Definition: kglobal.cpp:178
KGlobal::deleteStaticDeleters
static void deleteStaticDeleters()
Calls KStaticDeleterBase::destructObject() on all registered static deleters and unregisters them all...
Definition: kglobal.cpp:185
KGlobal::sharedConfig
static KSharedConfig * sharedConfig()
Returns the general config object.
Definition: kglobal.cpp:68
KInstance::dirs
KStandardDirs * dirs() const
Returns the application standard dirs object.
Definition: kinstance.cpp:159
KInstance::sharedConfig
KSharedConfig * sharedConfig() const
Returns the general config object ("appnamerc").
Definition: kinstance.cpp:224
KStaticDeleterBase
Static deleters are used to manage static resources.
Definition: kstaticdeleter.h:39
KGlobal::config
static KConfig * config()
Returns the general config object.
Definition: kglobal.cpp:61
KIconLoader
Iconloader for KDE.
Definition: kiconloader.h:77

KDE Home | KDE Accessibility Home | Description of Access Keys