not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kwin / lib / kdecoration_plugins_p.cpp
blob8c1333bc34bea494527afc1f42a527494268fa5e
1 /*****************************************************************
2 // vim: sw=4 sts=4 et tw=100
3 This file is part of the KDE project.
5 Copyright (C) 1999, 2000 Daniel M. Duley <mosfet@kde.org>
6 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 and/or sell copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ******************************************************************/
27 #include "kdecoration_plugins_p.h"
29 #include <kconfig.h>
30 #include <kdebug.h>
31 #include <klocale.h>
32 #include <klibloader.h>
33 #include <kconfiggroup.h>
34 #include <assert.h>
36 #include <QDir>
37 #include <QFile>
39 #include "kdecorationfactory.h"
41 KDecorationPlugins::KDecorationPlugins(const KSharedConfigPtr &cfg)
42 : create_ptr( NULL ),
43 library( NULL ),
44 fact( NULL ),
45 old_library( NULL ),
46 old_fact( NULL ),
47 pluginStr( "kwin3_undefined " ),
48 config( cfg )
52 KDecorationPlugins::~KDecorationPlugins()
54 if(library)
56 assert( fact != NULL );
57 delete fact;
58 library->unload();
60 if(old_library)
62 assert( old_fact != NULL );
63 delete old_fact;
64 old_library->unload();
68 QString KDecorationPlugins::currentPlugin()
70 return pluginStr;
73 bool KDecorationPlugins::reset( unsigned long changed )
75 QString oldPlugin = pluginStr;
76 config->reparseConfiguration();
77 bool ret = false;
78 if(( !loadPlugin( "" ) && library ) // "" = read the one in cfg file
79 || oldPlugin == pluginStr )
80 { // no new plugin loaded, reset the old one
81 assert( fact != NULL );
82 ret = fact->reset( changed );
84 return ret || oldPlugin != pluginStr;
87 KDecorationFactory* KDecorationPlugins::factory()
89 return fact;
92 // convenience
93 KDecoration* KDecorationPlugins::createDecoration( KDecorationBridge* bridge )
95 if( fact != NULL )
96 return fact->createDecoration( bridge );
97 return NULL;
100 // returns true if plugin was loaded successfully
101 bool KDecorationPlugins::loadPlugin( QString nameStr )
103 if( nameStr.isEmpty())
105 KConfigGroup group( config, QString("Style") );
106 nameStr = group.readEntry("PluginLib", defaultPlugin );
108 // make sure people can switch between HEAD and kwin_iii branch
109 if( nameStr.startsWith( "kwin_" ))
110 nameStr = "kwin3_" + nameStr.mid( 5 );
112 KLibrary *oldLibrary = library;
113 KDecorationFactory* oldFactory = fact;
115 QString path = KLibLoader::findLibrary(nameStr);
116 kDebug(1212) << "kwin : path " << path << " for " << nameStr;
118 // If the plugin was not found, try to find the default
119 if (path.isEmpty())
121 nameStr = defaultPlugin;
122 path = KLibLoader::findLibrary(nameStr);
125 // If no library was found, exit kwin with an error message
126 if (path.isEmpty())
128 error( i18n("No window decoration plugin library was found." ));
129 return false;
132 // Check if this library is not already loaded.
133 if(pluginStr == nameStr)
134 return true;
136 // Try loading the requested plugin
137 library = KLibLoader::self()->library(path);
139 // If that fails, fall back to the default plugin
140 if (!library)
142 kDebug(1212) << " could not load library, try default plugin again";
143 nameStr = defaultPlugin;
144 if ( pluginStr == nameStr )
145 return true;
146 path = KLibLoader::findLibrary(nameStr);
147 if (!path.isEmpty())
148 library = KLibLoader::self()->library(path);
151 if (!library)
153 error( i18n("The default decoration plugin is corrupt "
154 "and could not be loaded." ));
155 return false;
158 create_ptr = NULL;
159 KLibrary::void_function_ptr create_func = library->resolveFunction("create_factory");
160 if(create_func)
161 create_ptr = (KDecorationFactory* (*)())create_func;
163 if(!create_ptr)
165 error( i18n( "The library %1 is not a KWin plugin.", path ));
166 library->unload();
167 return false;
169 fact = create_ptr();
170 fact->checkRequirements( this ); // let it check what is supported
172 pluginStr = nameStr;
174 // For clients in kdeartwork
175 QString catalog = nameStr;
176 catalog.replace( "kwin3_", "kwin_" );
177 KGlobal::locale()->insertCatalog( catalog );
178 // For KCommonDecoration based clients
179 KGlobal::locale()->insertCatalog( "kwin_lib" );
180 // For clients in kdebase
181 KGlobal::locale()->insertCatalog( "kwin_clients" );
182 // For clients in kdeartwork
183 KGlobal::locale()->insertCatalog( "kwin_art_clients" );
185 old_library = oldLibrary; // save for delayed destroying
186 old_fact = oldFactory;
188 return true;
191 void KDecorationPlugins::destroyPreviousPlugin()
193 // Destroy the old plugin
194 if(old_library)
196 delete old_fact;
197 old_fact = NULL;
198 old_library->unload();
199 old_library = NULL;
203 void KDecorationPlugins::error( const QString& )