1 /*****************************************************************
2 
3 This file is a copy of the KWin internal API. It allows the KDE4
4 window decorator the loading of KWin decoration plugins.
5 
6 Copyright © 2008 Dennis Kasprzyk <onestone@opencompositing.org>
7 
8 ******************************************************************
9 
10 This file is part of the KDE project.
11 
12 Copyright (C) 1999, 2000    Daniel M. Duley <mosfet@kde.org>
13 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
14 
15 Permission is hereby granted, free of charge, to any person obtaining a
16 copy of this software and associated documentation files (the "Software"),
17 to deal in the Software without restriction, including without limitation
18 the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 and/or sell copies of the Software, and to permit persons to whom the
20 Software is furnished to do so, subject to the following conditions:
21 
22 The above copyright notice and this permission notice shall be included in
23 all copies or substantial portions of the Software.
24 
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 DEALINGS IN THE SOFTWARE.
32 ******************************************************************/
33 
34 #include "kdecoration_plugins.h"
35 
36 #include <kconfig.h>
37 #include <kdebug.h>
38 #include <klocale.h>
39 #include <klibloader.h>
40 #include <kconfiggroup.h>
41 #include <assert.h>
42 
43 #include <QDir>
44 #include <QFile>
45 
46 #include <kdecorationfactory.h>
47 
KDecorationPlugins(const KSharedConfigPtr & cfg)48 KWD::KDecorationPlugins::KDecorationPlugins(const KSharedConfigPtr &cfg)
49     :   create_ptr( NULL ),
50         library( NULL ),
51         fact( NULL ),
52         old_library( NULL ),
53         old_fact( NULL ),
54         pluginStr( "kwin3_undefined " ),
55         config( cfg )
56     {
57     }
58 
~KDecorationPlugins()59 KWD::KDecorationPlugins::~KDecorationPlugins()
60     {
61     if(library)
62         {
63         assert( fact != NULL );
64         delete fact;
65 	library->unload();
66         }
67     if(old_library)
68         {
69         assert( old_fact != NULL );
70         delete old_fact;
71 	old_library->unload();
72         }
73     }
74 
currentPlugin()75 QString KWD::KDecorationPlugins::currentPlugin()
76     {
77     return pluginStr;
78     }
79 
reset(unsigned long changed)80 bool KWD::KDecorationPlugins::reset( unsigned long changed )
81     {
82     QString oldPlugin = pluginStr;
83     config->reparseConfiguration();
84     bool ret = false;
85     if(( !loadPlugin( "" ) && library ) // "" = read the one in cfg file
86         || oldPlugin == pluginStr )
87         { // no new plugin loaded, reset the old one
88         assert( fact != NULL );
89         ret = fact->reset( changed );
90         }
91     return ret || oldPlugin != pluginStr;
92     }
93 
factory()94 KDecorationFactory* KWD::KDecorationPlugins::factory()
95     {
96     return fact;
97     }
98 
99 // convenience
createDecoration(KDecorationBridge * bridge)100 KDecoration* KWD::KDecorationPlugins::createDecoration( KDecorationBridge* bridge )
101     {
102     if( fact != NULL )
103         return fact->createDecoration( bridge );
104     return NULL;
105     }
106 
107 // returns true if plugin was loaded successfully
loadPlugin(QString nameStr)108 bool KWD::KDecorationPlugins::loadPlugin( QString nameStr )
109     {
110     if( nameStr.isEmpty())
111         {
112         KConfigGroup group( config, QString("Style") );
113         nameStr = group.readEntry("PluginLib", defaultPlugin );
114         }
115     // make sure people can switch between HEAD and kwin_iii branch
116     if( nameStr.startsWith( "kwin_" ))
117 	nameStr = "kwin3_" + nameStr.mid( 5 );
118 
119     KLibrary *oldLibrary = library;
120     KDecorationFactory* oldFactory = fact;
121 
122     QString path = KLibLoader::findLibrary(nameStr);
123 	kDebug() << "kwin : path " << path << " for " << nameStr;
124 
125     // If the plugin was not found, try to find the default
126     if (path.isEmpty())
127         {
128         nameStr = defaultPlugin;
129         path = KLibLoader::findLibrary(nameStr);
130         }
131 
132     // If no library was found, exit kwin with an error message
133     if (path.isEmpty())
134         {
135         error( i18n("No window decoration plugin library was found." ));
136         return false;
137         }
138 
139     // Check if this library is not already loaded.
140     if(pluginStr == nameStr)
141 	return true;
142 
143     // Try loading the requested plugin
144     library = KLibLoader::self()->library(path);
145 
146     // If that fails, fall back to the default plugin
147     if (!library)
148         {
149 	kDebug() << " could not load library, try default plugin again";
150         nameStr = defaultPlugin;
151 	if ( pluginStr == nameStr )
152 	    return true;
153         path = KLibLoader::findLibrary(nameStr);
154 	if (!path.isEmpty())
155             library = KLibLoader::self()->library(path);
156         }
157 
158     if (!library)
159         {
160         error( i18n("The default decoration plugin is corrupt "
161                           "and could not be loaded." ));
162         return false;
163         }
164 
165     create_ptr = NULL;
166     KLibrary::void_function_ptr create_func = library->resolveFunction("create_factory");
167     if(create_func)
168         create_ptr = (KDecorationFactory* (*)())create_func;
169 
170     if(!create_ptr)
171         {
172         error( i18n( "The library %1 is not a KWin plugin.", path ));
173         library->unload();
174         return false;
175         }
176     fact = create_ptr();
177     fact->checkRequirements( this ); // let it check what is supported
178 
179     pluginStr = nameStr;
180 
181     // For clients in kdeartwork
182     QString catalog = nameStr;
183     catalog.replace( "kwin3_", "kwin_" );
184     KGlobal::locale()->insertCatalog( catalog );
185     // For KCommonDecoration based clients
186     KGlobal::locale()->insertCatalog( "kwin_lib" );
187     // For clients in kdebase
188     KGlobal::locale()->insertCatalog( "kwin_clients" );
189     // For clients in kdeartwork
190     KGlobal::locale()->insertCatalog( "kwin_art_clients" );
191 
192     old_library = oldLibrary; // save for delayed destroying
193     old_fact = oldFactory;
194 
195     return true;
196 }
197 
destroyPreviousPlugin()198 void KWD::KDecorationPlugins::destroyPreviousPlugin()
199 {
200     // Destroy the old plugin
201     if(old_library)
202         {
203         delete old_fact;
204         old_fact = NULL;
205 	old_library->unload();
206         old_library = NULL;
207         }
208 }
209 
error(const QString &)210 void KWD::KDecorationPlugins::error( const QString& )
211     {
212     }
213