kwin/libkdecorations/kdecoration_plugins_p.cpp

241 lines
7.6 KiB
C++
Raw Normal View History

/*****************************************************************
// vim: sw=4 sts=4 et tw=100
This file is part of the KDE project.
Copyright (C) 1999, 2000 Daniel M. Duley <mosfet@kde.org>
Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
******************************************************************/
#include "kdecoration_plugins_p.h"
#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <klibrary.h>
#include <kconfiggroup.h>
#include <assert.h>
#include <QDir>
#include <QFile>
#include "kdecorationfactory.h"
KDecorationPlugins::KDecorationPlugins(const KSharedConfigPtr &cfg)
2011-01-30 17:34:42 +03:00
: create_ptr(NULL),
library(NULL),
fact(NULL),
old_library(NULL),
old_fact(NULL),
pluginStr("kwin3_undefined "),
config(cfg)
{
}
KDecorationPlugins::~KDecorationPlugins()
2011-01-30 17:34:42 +03:00
{
if (library) {
assert(fact != NULL);
delete fact;
2011-01-30 17:34:42 +03:00
library->unload();
}
if (old_library) {
assert(old_fact != NULL);
delete old_fact;
2011-01-30 17:34:42 +03:00
old_library->unload();
}
2011-01-30 17:34:42 +03:00
}
QString KDecorationPlugins::currentPlugin()
2011-01-30 17:34:42 +03:00
{
return pluginStr;
2011-01-30 17:34:42 +03:00
}
2011-01-30 17:34:42 +03:00
bool KDecorationPlugins::reset(unsigned long changed)
{
QString oldPlugin = pluginStr;
config->reparseConfiguration();
bool ret = false;
2011-01-30 17:34:42 +03:00
if ((!loadPlugin("") && library) // "" = read the one in cfg file
|| oldPlugin == pluginStr) {
// no new plugin loaded, reset the old one
// assert( fact != NULL );
if (fact != NULL) {
ret = fact->reset(changed);
}
2011-01-30 17:34:42 +03:00
}
2011-01-30 17:34:42 +03:00
return ret || oldPlugin != pluginStr;
}
KDecorationFactory* KDecorationPlugins::factory()
2011-01-30 17:34:42 +03:00
{
return fact;
2011-01-30 17:34:42 +03:00
}
// convenience
2011-01-30 17:34:42 +03:00
KDecoration* KDecorationPlugins::createDecoration(KDecorationBridge* bridge)
{
if (fact != NULL)
return fact->createDecoration(bridge);
return NULL;
2011-01-30 17:34:42 +03:00
}
// returns true if plugin was loaded successfully
2011-01-30 17:34:42 +03:00
bool KDecorationPlugins::loadPlugin(QString nameStr)
{
KConfigGroup group(config, QString("Style"));
2011-01-30 17:34:42 +03:00
if (nameStr.isEmpty()) {
nameStr = group.readEntry("PluginLib", defaultPlugin);
}
if (group.readEntry<bool>("NoPlugin", false)) {
error(i18n("Loading of window decoration plugin library disabled in configuration."));
return false;
}
KLibrary *oldLibrary = library;
KDecorationFactory* oldFactory = fact;
KLibrary libToFind(nameStr);
QString path = libToFind.fileName();
2011-01-30 17:34:42 +03:00
kDebug(1212) << "kwin : path " << path << " for " << nameStr;
// If the plugin was not found, try to find the default
2011-01-30 17:34:42 +03:00
if (path.isEmpty()) {
nameStr = defaultPlugin;
KLibrary libToFind(nameStr);
path = libToFind.fileName();
2011-01-30 17:34:42 +03:00
}
// If no library was found, exit kwin with an error message
2011-01-30 17:34:42 +03:00
if (path.isEmpty()) {
error(i18n("No window decoration plugin library was found."));
return false;
2011-01-30 17:34:42 +03:00
}
// Check if this library is not already loaded.
2011-01-30 17:34:42 +03:00
if (pluginStr == nameStr)
return true;
// Try loading the requested plugin
library = new KLibrary(path);
// If that fails, fall back to the default plugin
trydefaultlib:
2011-01-30 17:34:42 +03:00
if (!library) {
kDebug(1212) << " could not load library, try default plugin again";
nameStr = defaultPlugin;
2011-01-30 17:34:42 +03:00
if (pluginStr == nameStr)
return true;
KLibrary libToFind(nameStr);
path = libToFind.fileName();
2011-01-30 17:34:42 +03:00
if (!path.isEmpty())
library = new KLibrary(path);
2011-01-30 17:34:42 +03:00
}
2011-01-30 17:34:42 +03:00
if (!library) {
error(i18n("The default decoration plugin is corrupt "
"and could not be loaded."));
return false;
2011-01-30 17:34:42 +03:00
}
create_ptr = NULL;
version_ptr = 0;
int deco_version = 0;
KLibrary::void_function_ptr version_func = library->resolveFunction("decoration_version");
if (version_func) {
version_ptr = (int(*)())version_func;
deco_version = version_ptr();
} else {
// block some decos known to link the unstable API but (for now) let through other legacy stuff
const bool isLegacyStableABI = !(nameStr.contains("qtcurve", Qt::CaseInsensitive) ||
nameStr.contains("crystal", Qt::CaseInsensitive) ||
nameStr.contains("oxygen", Qt::CaseInsensitive));
if (isLegacyStableABI) {
// it's an old build of a legacy decoration that very likely uses the stable API
// so we just set the API version to the current one
// TODO: remove for 4.9.x or 4.10 - this is just to cover recompiles
deco_version = KWIN_DECORATION_API_VERSION;
}
kWarning(1212) << QString("****** The library %1 has no API version ******").arg(path);
kWarning(1212) << "****** Please use the KWIN_DECORATION macro in extern \"C\" to get this decoration loaded in future versions of kwin";
}
if (deco_version != KWIN_DECORATION_API_VERSION) {
if (nameStr != defaultPlugin) {
if (version_func)
kWarning(1212) << i18n("The library %1 has wrong API version %2", path, deco_version);
library->unload();
library = NULL;
goto trydefaultlib;
}
}
KLibrary::void_function_ptr create_func = library->resolveFunction("create_factory");
2011-01-30 17:34:42 +03:00
if (create_func)
create_ptr = (KDecorationFactory * (*)())create_func;
if (!create_ptr) {
if (nameStr != defaultPlugin) {
kDebug(1212) << i18n("The library %1 is not a KWin plugin.", path);
library->unload();
library = NULL;
goto trydefaultlib;
2011-01-30 17:34:42 +03:00
}
error(i18n("The library %1 is not a KWin plugin.", path));
library->unload();
return false;
2011-01-30 17:34:42 +03:00
}
fact = create_ptr();
2011-01-30 17:34:42 +03:00
fact->checkRequirements(this); // let it check what is supported
pluginStr = nameStr;
// For clients in kdeartwork
QString catalog = nameStr;
2011-01-30 17:34:42 +03:00
catalog.replace("kwin3_", "kwin_");
KGlobal::locale()->insertCatalog(catalog);
// For KCommonDecoration based clients
KGlobal::locale()->insertCatalog("libkdecorations");
// For clients in kdebase
2011-01-30 17:34:42 +03:00
KGlobal::locale()->insertCatalog("kwin_clients");
// For clients in kdeartwork
2011-01-30 17:34:42 +03:00
KGlobal::locale()->insertCatalog("kwin_art_clients");
old_library = oldLibrary; // save for delayed destroying
old_fact = oldFactory;
return true;
}
void KDecorationPlugins::destroyPreviousPlugin()
{
// Destroy the old plugin
2011-01-30 17:34:42 +03:00
if (old_library) {
delete old_fact;
old_fact = NULL;
2011-01-30 17:34:42 +03:00
old_library->unload();
old_library = NULL;
2011-01-30 17:34:42 +03:00
}
}
2011-01-30 17:34:42 +03:00
void KDecorationPlugins::error(const QString&)
{
}