summaryrefslogtreecommitdiffstats
path: root/twin/lib/kdecoration_plugins_p.cpp
blob: a7b12ab85a2fd556d9586472a963191efd0bc41a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*****************************************************************
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 <klibloader.h>
#include <assert.h>

#include <tqdir.h>
#include <tqfile.h>

#include "kdecorationfactory.h"

KDecorationPlugins::KDecorationPlugins( KConfig* cfg )
    :   create_ptr( NULL ),
        library( NULL ),
        fact( NULL ),
        old_library( NULL ),
        old_fact( NULL ),
        pluginStr( "twin3_undefined " ),
        config( cfg )
    {
    }

KDecorationPlugins::~KDecorationPlugins()
    {
    if(library)
        {
        assert( fact != NULL );
        delete fact;
	library->unload();
        }
    if(old_library)
        {
        assert( old_fact != NULL );
        delete old_fact;
	old_library->unload();
        }
    }

bool KDecorationPlugins::reset( unsigned long changed )
    {
    TQString oldPlugin = pluginStr;
    config->reparseConfiguration();
    bool ret = false;
    if(( !loadPlugin( "" ) && library ) // "" = read the one in cfg file
        || oldPlugin == pluginStr )
        { // no new plugin loaded, reset the old one
        assert( fact != NULL );
        ret = fact->reset( changed );
        }
    return ret || oldPlugin != pluginStr;
    }

KDecorationFactory* KDecorationPlugins::factory()
    {
    return fact;
    }

// convenience
KDecoration* KDecorationPlugins::createDecoration( KDecorationBridge* bridge )
    {
    if( fact != NULL )
        return fact->createDecoration( bridge );
    return NULL;
    }
    
// returns true if plugin was loaded successfully
bool KDecorationPlugins::loadPlugin( TQString nameStr )
    {
    if( nameStr.isEmpty())
        {
        KConfigGroupSaver saver( config, "Style" );
        nameStr = config->readEntry("PluginLib", defaultPlugin );
        }
    // make sure people can switch between HEAD and twin_iii branch
    if( nameStr.startsWith( "twin_" ))
	nameStr = "twin3_" + nameStr.mid( 5 );

    KLibrary *oldLibrary = library;
    KDecorationFactory* oldFactory = fact;

    TQString path = KLibLoader::findLibrary(TQFile::encodeName(nameStr));

    // If the plugin was not found, try to find the default
    if (path.isEmpty())
        {
        nameStr = defaultPlugin;
        path = KLibLoader::findLibrary(TQFile::encodeName(nameStr));
        }

    // If no library was found, exit twin with an error message
    if (path.isEmpty())
        {
        error( i18n("No window decoration plugin library was found." ));
        return false;
        }

    // Check if this library is not already loaded.
    if(pluginStr == nameStr)
	return true;

    // Try loading the requested plugin
    library = KLibLoader::self()->library(TQFile::encodeName(path));

    // If that fails, fall back to the default plugin
    if (!library)
        {
	kdDebug() << " could not load library, try default plugin again" << endl;
        nameStr = defaultPlugin;
	if ( pluginStr == nameStr )
	    return true;
        path = KLibLoader::findLibrary(TQFile::encodeName(nameStr));
	if (!path.isEmpty())
            library = KLibLoader::self()->library(TQFile::encodeName(path));
        }

    if (!library)
        {
        error( i18n("The default decoration plugin is corrupt "
                          "and could not be loaded." ));
        return false;
        }

    create_ptr = NULL;
    if( library->hasSymbol("create_factory"))
        {
        void* create_func = library->symbol("create_factory");
        if(create_func)
            create_ptr = (KDecorationFactory* (*)())create_func;
        }
    if(!create_ptr)
        {
        error( i18n( "The library %1 is not a KWin plugin." ).arg( path ));
        library->unload();
        return false;
        }
    fact = create_ptr();
    fact->checkRequirements( this ); // let it check what is supported

    pluginStr = nameStr;

    // For clients in kdeartwork    
    TQString catalogue = nameStr;
    catalogue.replace( "twin3_", "twin_" );
    KGlobal::locale()->insertCatalogue( catalogue );
    // For KCommonDecoration based clients
    KGlobal::locale()->insertCatalogue( "twin_lib" );
    // For clients in kdebase
    KGlobal::locale()->insertCatalogue( "twin_clients" );
    // For clients in kdeartwork
    KGlobal::locale()->insertCatalogue( "twin_art_clients" );

    old_library = oldLibrary; // save for delayed destroying
    old_fact = oldFactory;
    
    return true;
}

void KDecorationPlugins::destroyPreviousPlugin()
{
    // Destroy the old plugin
    if(old_library)
        {
        delete old_fact;
        old_fact = NULL;
	old_library->unload();
        old_library = NULL;
        }
}

void KDecorationPlugins::error( const TQString& )
    {
    }