summaryrefslogtreecommitdiffstats
path: root/tdeutils/kplugininfo.cpp
blob: cb93b26005fabed5cbd534b0454828b2dc196949 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*  This file is part of the KDE project
    Copyright (C) 2003 Matthias Kretz <kretz@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.

*/

#include "kplugininfo.h"
#include <ksimpleconfig.h>
#include <ktrader.h>
#include <kdebug.h>
#include <tdeconfigbase.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kdesktopfile.h>
#include <kservice.h>

class KPluginInfo::KPluginInfoPrivate
{
    public:
        KPluginInfoPrivate()
            : hidden( false )
            , enabledbydefault( false )
            , pluginenabled( false )
            , config( 0 )
            , kcmservicesCached( false )
            {}

        ~KPluginInfoPrivate()
        {
            delete config;
        }

        TQString specfile; // the filename of the file containing all the info
        TQString name;
        TQString comment;
        TQString icon;
        TQString author;
        TQString email;
        TQString pluginName; // the name attribute in the .rc file
        TQString version;
        TQString website; // URL to the website of the plugin/author
        TQString category;
        TQString license;
        TQStringList dependencies;

        bool hidden;
        bool enabledbydefault;
        bool pluginenabled;

        TDEConfig * config;
        TQString configgroup;
        KService::Ptr service;
        TQValueList<KService::Ptr> kcmservices;
        bool kcmservicesCached;
};

KPluginInfo::KPluginInfo( const TQString & filename, const char* resource )
: d( new KPluginInfoPrivate )
{
    KDesktopFile file( filename, true, resource );

    d->specfile = filename;

    if( filename.endsWith( TQString::fromAscii( ".desktop" ) ) )
    {
        file.setDesktopGroup();
        d->hidden = file.readBoolEntry( "Hidden", false );
        if( d->hidden )
            return;

        d->name = file.readName();
        d->comment = file.readComment();
        d->icon = file.readEntry( "Icon" );
        d->author = file.readEntry( "X-TDE-PluginInfo-Author" );
        d->email = file.readEntry( "X-TDE-PluginInfo-Email" );
        d->pluginName = file.readEntry( "X-TDE-PluginInfo-Name" );
        d->version = file.readEntry( "X-TDE-PluginInfo-Version" );
        d->website = file.readEntry( "X-TDE-PluginInfo-Website" );
        d->category = file.readEntry( "X-TDE-PluginInfo-Category" );
        d->license = file.readEntry( "X-TDE-PluginInfo-License" );
        d->dependencies = file.readListEntry( "X-TDE-PluginInfo-Depends" );
        d->enabledbydefault = file.readBoolEntry(
                "X-TDE-PluginInfo-EnabledByDefault", false );
    }
    else if( filename.endsWith( TQString::fromAscii( ".plugin" ) ) )
    { // provided for noatun style .plugin files compatibility

        d->name = file.readName();
        d->comment = file.readComment();
        d->icon = file.readEntry( "Icon" );
        d->author = file.readEntry( "Author" );
        d->email = file.readEntry( "Email" );
        d->pluginName = file.readPathEntry( "Filename" );
        // no version
        d->website = file.readEntry( "Site" );
        d->category = file.readEntry( "Type" );
        d->license = file.readEntry( "License" );
        d->dependencies = file.readListEntry( "Require" );
    }
}

KPluginInfo::KPluginInfo( const KService::Ptr service )
: d( new KPluginInfoPrivate )
{
    d->service = service;
    d->specfile = service->desktopEntryPath();

    if ( service->isDeleted() )
    {
        d->hidden = true;
        return;
    }

    d->name = service->name();
    d->comment = service->comment();
    d->icon = service->icon();
    d->author = service->property( "X-TDE-PluginInfo-Author" ).toString();
    d->email = service->property( "X-TDE-PluginInfo-Email" ).toString();
    d->pluginName = service->property( "X-TDE-PluginInfo-Name" ).toString();
    d->version = service->property( "X-TDE-PluginInfo-Version" ).toString();
    d->website = service->property( "X-TDE-PluginInfo-Website" ).toString();
    d->category = service->property( "X-TDE-PluginInfo-Category" ).toString();
    d->license = service->property( "X-TDE-PluginInfo-License" ).toString();
    d->dependencies =
        service->property( "X-TDE-PluginInfo-Depends" ).toStringList();
    TQVariant tmp = service->property( "X-TDE-PluginInfo-EnabledByDefault" );
    d->enabledbydefault = tmp.isValid() ? tmp.toBool() : false;
}

//X KPluginInfo::KPluginInfo()
//X : d( new KPluginInfoPrivate )
//X {
//X     d->hidden = true;
//X }

KPluginInfo::~KPluginInfo()
{
    delete d;
}

TQValueList<KPluginInfo*> KPluginInfo::fromServices( const KService::List & services, TDEConfig * config, const TQString & group )
{
    TQValueList<KPluginInfo*> infolist;
    KPluginInfo * info;
    for( KService::List::ConstIterator it = services.begin();
            it != services.end(); ++it )
    {
        info = new KPluginInfo( *it );
        info->setConfig( config, group );
        infolist += info;
    }
    return infolist;
}

TQValueList<KPluginInfo*> KPluginInfo::fromFiles( const TQStringList & files, TDEConfig * config, const TQString & group )
{
    TQValueList<KPluginInfo*> infolist;
    for( TQStringList::ConstIterator it = files.begin(); it != files.end(); ++it )
    {
        KPluginInfo * info = new KPluginInfo( *it );
        info->setConfig( config, group );
        infolist += info;
    }
    return infolist;
}

TQValueList<KPluginInfo*> KPluginInfo::fromKPartsInstanceName( const TQString & name, TDEConfig * config, const TQString & group )
{
    TQStringList files = TDEGlobal::dirs()->findAllResources( "data", name +
            "/kpartplugins/*.desktop", true, false );
    return fromFiles( files, config, group );
}

bool KPluginInfo::isHidden() const
{
    return d->hidden;
}

void KPluginInfo::setPluginEnabled( bool enabled )
{
    kdDebug( 703 ) << k_funcinfo << endl;
    d->pluginenabled = enabled;
}

bool KPluginInfo::isPluginEnabled() const
{
    kdDebug( 703 ) << k_funcinfo << endl;
    return d->pluginenabled;
}

bool KPluginInfo::isPluginEnabledByDefault() const
{
    kdDebug( 703 ) << k_funcinfo << endl;
    return d->enabledbydefault;
}

const TQString & KPluginInfo::name() const
{
    return d->name;
}

const TQString & KPluginInfo::comment() const
{
    return d->comment;
}

const TQString & KPluginInfo::icon() const
{
    return d->icon;
}

const TQString & KPluginInfo::specfile() const
{
    return d->specfile;
}

const TQString & KPluginInfo::author() const
{
    return d->author;
}

const TQString & KPluginInfo::email() const
{
    return d->email;
}

const TQString & KPluginInfo::category() const
{
    return d->category;
}

const TQString & KPluginInfo::pluginName() const
{
    return d->pluginName;
}

const TQString & KPluginInfo::version() const
{
    return d->version;
}

const TQString & KPluginInfo::website() const
{
    return d->website;
}

const TQString & KPluginInfo::license() const
{
    return d->license;
}

const TQStringList & KPluginInfo::dependencies() const
{
    return d->dependencies;
}

KService::Ptr KPluginInfo::service() const
{
    return d->service;
}

const TQValueList<KService::Ptr> & KPluginInfo::kcmServices() const
{
    if ( !d->kcmservicesCached )
    {
        d->kcmservices = TDETrader::self()->query( "TDECModule", "'" + d->pluginName +
            "' in [X-TDE-ParentComponents]" );
        kdDebug( 703 ) << "found " << d->kcmservices.count() << " offers for " <<
            d->pluginName << endl;

        d->kcmservicesCached = true;
    }

    return d->kcmservices;
}

void KPluginInfo::setConfig( TDEConfig * config, const TQString & group )
{
    d->config = config;
    d->configgroup = group;
}

TDEConfig * KPluginInfo::config() const
{
    return d->config;
}

const TQString & KPluginInfo::configgroup() const
{
    return d->configgroup;
}

TQVariant KPluginInfo::property( const TQString & key ) const
{
    if( d->service )
        return d->service->property( key );
    else
        return TQVariant();
}

TQVariant KPluginInfo::operator[]( const TQString & key ) const
{
    return property( key );
}

void KPluginInfo::save( TDEConfigGroup * config )
{
    kdDebug( 703 ) << k_funcinfo << endl;
    if( 0 == config )
    {
        if( 0 == d->config )
        {
            kdWarning( 703 ) << "no TDEConfigGroup, cannot save" << endl;
            return;
        }
        d->config->setGroup( d->configgroup );
        d->config->writeEntry( d->pluginName + "Enabled", isPluginEnabled() );
    }
    else
        config->writeEntry( d->pluginName + "Enabled", isPluginEnabled() );
}

void KPluginInfo::load( TDEConfigGroup * config )
{
    kdDebug( 703 ) << k_funcinfo << endl;
    if( 0 == config )
    {
        if( 0 == d->config )
        {
            kdWarning( 703 ) << "no TDEConfigGroup, cannot load" << endl;
            return;
        }
        d->config->setGroup( d->configgroup );
        setPluginEnabled( d->config->readBoolEntry( d->pluginName + "Enabled", isPluginEnabledByDefault() ) );
    }
    else
        setPluginEnabled( config->readBoolEntry( d->pluginName + "Enabled", isPluginEnabledByDefault() ) );
}

void KPluginInfo::defaults()
{
    kdDebug( 703 ) << k_funcinfo << endl;
    setPluginEnabled( isPluginEnabledByDefault() );
}

// vim: sw=4 sts=4 et