summaryrefslogtreecommitdiffstats
path: root/tdeio/tdeio/kurifilter.cpp
blob: 6ae0017be61367c05087f76029bdbc09970ba8f6 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* This file is part of the KDE libraries
 *  Copyright (C) 2000 Yves Arrouye <yves@realnames.com>
 *  Copyright (C) 2000 Dawit Alemayehu <adawit at 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 as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  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 <config.h>

#include <kdebug.h>
#include <kiconloader.h>
#include <ktrader.h>
#include <kmimetype.h>
#include <klibloader.h>
#include <kstaticdeleter.h>
#include <tdeparts/componentfactory.h>

#ifdef HAVE_ELFICON
#include <tqimage.h>
#include "tdelficon.h"
#endif // HAVE_ELFICON

#include "kurifilter.h"

template class TQPtrList<KURIFilterPlugin>;

KURIFilterPlugin::KURIFilterPlugin( TQObject *parent, const char *name, double pri )
                 :TQObject( parent, name )
{
    m_strName = TQString::fromLatin1( name );
    m_dblPriority = pri;
}

void KURIFilterPlugin::setFilteredURI( KURIFilterData& data, const KURL& uri ) const
{
    if ( data.uri() != uri )
    {
        data.m_pURI = uri;
        data.m_bChanged = true;
    }
}

class KURIFilterDataPrivate
{
public:
    KURIFilterDataPrivate() {};
    TQString abs_path;
    TQString args;
    TQString typedString;
};

KURIFilterData::KURIFilterData( const KURIFilterData& data )
{
    m_iType = data.m_iType;
    m_pURI = data.m_pURI;
    m_strErrMsg = data.m_strErrMsg;
    m_strIconName = data.m_strIconName;
    m_bChanged = data.m_bChanged;
    m_bCheckForExecutables = data.m_bCheckForExecutables;
    d = new KURIFilterDataPrivate;
    d->abs_path = data.absolutePath();
    d->typedString = data.typedString();
    d->args = data.argsAndOptions();
}

KURIFilterData::~KURIFilterData()
{
    delete d;
    d = 0;
}

void KURIFilterData::init( const KURL& url )
{
    m_iType = KURIFilterData::UNKNOWN;
    m_pURI = url;
    m_strErrMsg = TQString::null;
    m_strIconName = TQString::null;
    m_bCheckForExecutables = true;
    m_bChanged = true;
    d = new KURIFilterDataPrivate;
    d->typedString = url.url();
}

void KURIFilterData::init( const TQString& url )
{
    m_iType = KURIFilterData::UNKNOWN;
    m_pURI = url;
    m_strErrMsg = TQString::null;
    m_strIconName = TQString::null;
    m_bCheckForExecutables = true;
    m_bChanged = true;
    d = new KURIFilterDataPrivate;
    d->typedString = url;
}

void KURIFilterData::reinit(const KURL &url)
{
    delete d;
    init(url);
}

void KURIFilterData::reinit(const TQString &url)
{
    delete d;
    init(url);
}

TQString KURIFilterData::typedString() const
{
    return d->typedString;
}

void KURIFilterData::setCheckForExecutables( bool check )
{
    m_bCheckForExecutables = check;
}

bool KURIFilterData::hasArgsAndOptions() const
{
    return !d->args.isEmpty();
}

bool KURIFilterData::hasAbsolutePath() const
{
    return !d->abs_path.isEmpty();
}

bool KURIFilterData::setAbsolutePath( const TQString& absPath )
{
    // Since a malformed URL could possibly be a relative
    // URL we tag it as a possible local resource...
    if( (!m_pURI.isValid() || m_pURI.isLocalFile()) )
    {
        d->abs_path = absPath;
        return true;
    }
    return false;
}

TQString KURIFilterData::absolutePath() const
{
    return d->abs_path;
}

TQString KURIFilterData::argsAndOptions() const
{
    return d->args;
}

TQString KURIFilterData::iconName()
{
    if( m_bChanged )
    {
        m_customIconPixmap = TQPixmap();
        switch ( m_iType )
        {
            case KURIFilterData::LOCAL_FILE:
            case KURIFilterData::LOCAL_DIR:
            case KURIFilterData::NET_PROTOCOL:
            {
                m_strIconName = KMimeType::iconForURL( m_pURI );
                break;
            }
            case KURIFilterData::EXECUTABLE:
            {
                TQString exeName = m_pURI.url();
                exeName = exeName.mid( exeName.findRev( '/' ) + 1 ); // strip path if given
                KService::Ptr service = KService::serviceByDesktopName( exeName );
#ifndef HAVE_ELFICON
                // Try to find an icon with the same name as the binary (useful for non-tde apps)
                // FIXME: We should only do this if the binary is in the system path somewhere,
                // otherwise TDE could end up showing system icons for user binaries
                if (service && service->icon() != TQString::fromLatin1( "unknown" )) {
                    m_strIconName = service->icon();
                }
                else if ( !TDEGlobal::iconLoader()->loadIcon( exeName, TDEIcon::NoGroup, 16, TDEIcon::DefaultState, 0, true ).isNull() ) {
                    m_strIconName = exeName;
                }
                else {
                    // use default
                    m_strIconName = TQString::fromLatin1("application-x-executable");
                }
#else // HAVE_ELFICON
		// Try to find an icon with the same name as the binary (useful for non-tde apps)
		// FIXME: We should only do this if the binary is in the system path somewhere,
		// otherwise TDE could end up showing system icons for user binaries
		if (service && service->icon() != TQString::fromLatin1( "unknown" )) {
			m_strIconName = service->icon();
		}
		else if ( !TDEGlobal::iconLoader()->loadIcon( exeName, TDEIcon::NoGroup, 16, TDEIcon::DefaultState, 0, true ).isNull() ) {
			m_strIconName = exeName;
		}
		else {
			// use default
			m_strIconName = TQString::fromLatin1("application-x-executable");
		}
		// Try to load from elf file (if supported)
		// Check for an embedded icon
		unsigned int icon_size;
		libr_icon *icon = NULL;
		libr_file *handle = NULL;
		libr_access_t access = LIBR_READ;
		char libr_can_continue = 1;

		if((handle = libr_open(const_cast<char*>(m_pURI.path().ascii()), access)) == NULL)
		{
			kdWarning() << "failed to open file " << m_pURI.path() << endl;
			libr_can_continue = 0;
		}

		if (libr_can_continue == 1) {
			icon_size = 32;	// FIXME: Is this a reasonable size request for all possible usages of kurifilter?
			icon = libr_icon_geticon_bysize(handle, icon_size);

			if (libr_can_continue == 1) {
				// See if the embedded icon name matches any icon file names already on the system
				// If it does, use the system icon instead of the embedded one
				int iconresnamefound = 0;
				iconentry *entry = NULL;
				iconlist icons;
				if(!get_iconlist(handle, &icons))
				{
					// Failed to obtain a list of ELF icons
					kdDebug() << "failed to obtain ELF icon from " << m_pURI.path() << ": " << libr_errmsg() << endl;

					// See if there is a system icon we can use
					TQString sysIconName = elf_get_resource(handle, ".metadata_sysicon");
					if (!sysIconName.isEmpty()) {
						if (TDEGlobal::iconLoader()->iconPath(sysIconName.ascii(), 0, true) != "") {
							m_strIconName = sysIconName;
						}
					}

					libr_close(handle);
					libr_can_continue = 0;
				}
				else {
					while((entry = get_nexticon(&icons, entry)) != NULL)
					{
						if(icon == NULL)
						{
							// Try loading this icon as fallback
							icon = libr_icon_geticon_byname(handle, entry->name);
						}
						if (TDEGlobal::iconLoader()->iconPath(entry->name, 0, true) != "") {
							iconresnamefound = 1;
							m_strIconName = entry->name;
							break;
						}
					}
				}

				if (libr_can_continue == 1) {
					if ((iconresnamefound == 0) && (icon)) {
						// Extract the embedded icon
						size_t icon_data_length;
						char* icondata = libr_icon_malloc(icon, &icon_data_length);
						m_customIconPixmap.loadFromData(static_cast<uchar*>(static_cast<void*>(icondata)), icon_data_length);	// EVIL CAST
						if (icon_size != 0) {
							TQImage ip = m_customIconPixmap.convertToImage();
							ip = ip.smoothScale(icon_size, icon_size);
							m_customIconPixmap.convertFromImage(ip);
						}
						free(icondata);
						libr_icon_close(icon);
					}

					libr_close(handle);
				}
			}
		}
#endif // HAVE_ELFICON
                break;
            }
            case KURIFilterData::HELP:
            {
                m_strIconName = TQString::fromLatin1("khelpcenter");
                break;
            }
            case KURIFilterData::SHELL:
            {
                m_strIconName = TQString::fromLatin1("konsole");
                break;
            }
            case KURIFilterData::ERROR:
            case KURIFilterData::BLOCKED:
            {
                m_strIconName = TQString::fromLatin1("error");
                break;
            }
            default:
                m_strIconName = TQString::null;
                break;
        }
        m_bChanged = false;
    }
    return m_strIconName;
}

TQPixmap KURIFilterData::customIconPixmap()
{
    return m_customIconPixmap;
}

//********************************************  KURIFilterPlugin **********************************************
void KURIFilterPlugin::setArguments( KURIFilterData& data, const TQString& args ) const
{
    data.d->args = args;
}

//********************************************  KURIFilter **********************************************
KURIFilter *KURIFilter::s_self;
static KStaticDeleter<KURIFilter> kurifiltersd;

KURIFilter *KURIFilter::self()
{
    if (!s_self)
        s_self = kurifiltersd.setObject(s_self, new KURIFilter);
    return s_self;
}

KURIFilter::KURIFilter()
{
    m_lstPlugins.setAutoDelete(true);
    loadPlugins();
}

KURIFilter::~KURIFilter()
{
}

bool KURIFilter::filterURI( KURIFilterData& data, const TQStringList& filters )
{
    bool filtered = false;
    KURIFilterPluginList use_plugins;

    // If we have a filter list, only include the once
    // explicitly specified by it. Otherwise, use all available filters...
    if( filters.isEmpty() )
        use_plugins = m_lstPlugins;  // Use everything that is loaded...
    else
    {
        //kdDebug() << "Named plugins requested..."  << endl;
        for( TQStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst )
        {
            TQPtrListIterator<KURIFilterPlugin> it( m_lstPlugins );
            for( ; it.current() ; ++it )
            {
                if( (*lst) == it.current()->name() )
                {
                    //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl;
                    use_plugins.append( it.current() );
                    break;  // We already found it ; so lets test the next named filter...
                }
            }
        }
    }

    TQPtrListIterator<KURIFilterPlugin> it( use_plugins );
    //kdDebug() << "Using " << use_plugins.count() << " out of the "
    //          << m_lstPlugins.count() << " available plugins" << endl;
    for (; it.current() && !filtered; ++it)
    {
        //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl;
        filtered |= it.current()->filterURI( data );
    }
    return filtered;
}

bool KURIFilter::filterURI( KURL& uri, const TQStringList& filters )
{
    KURIFilterData data = uri;
    bool filtered = filterURI( data, filters );
    if( filtered ) uri = data.uri();
    return filtered;
}

bool KURIFilter::filterURI( TQString& uri, const TQStringList& filters )
{
    KURIFilterData data = uri;
    bool filtered = filterURI( data, filters );
    if( filtered )  uri = data.uri().url();
    return filtered;

}

KURL KURIFilter::filteredURI( const KURL &uri, const TQStringList& filters )
{
    KURIFilterData data = uri;
    filterURI( data, filters );
    return data.uri();
}

TQString KURIFilter::filteredURI( const TQString &uri, const TQStringList& filters )
{
    KURIFilterData data = uri;
    filterURI( data, filters );
    return data.uri().url();
}

TQPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const
{
    return TQPtrListIterator<KURIFilterPlugin>(m_lstPlugins);
}

TQStringList KURIFilter::pluginNames() const
{
    TQStringList list;
    for(TQPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i)
        list.append((*i)->name());
    return list;
}

void KURIFilter::loadPlugins()
{
    TDETrader::OfferList offers = TDETrader::self()->query( "KURIFilter/Plugin" );

    TDETrader::OfferList::ConstIterator it = offers.begin();
    TDETrader::OfferList::ConstIterator end = offers.end();

    for (; it != end; ++it )
    {
      KURIFilterPlugin *plugin = KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it, 0, (*it)->desktopEntryName().latin1() );
      if ( plugin )
        m_lstPlugins.append( plugin );
    }

    // NOTE: Plugin priority is now determined by
    // the entry in the .desktop files...
    // TODO: Config dialog to differentiate "system"
    // plugins from "user-defined" ones...
    // m_lstPlugins.sort();
}

void KURIFilterPlugin::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "kurifilter.moc"