• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
kurifilter.cpp
1/* This file is part of the KDE libraries
2 * Copyright (C) 2000 Yves Arrouye <yves@realnames.com>
3 * Copyright (C) 2000 Dawit Alemayehu <adawit at kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 **/
20
21#include <config.h>
22
23#include <kdebug.h>
24#include <kiconloader.h>
25#include <ktrader.h>
26#include <kmimetype.h>
27#include <klibloader.h>
28#include <kstaticdeleter.h>
29#include <tdeparts/componentfactory.h>
30
31#ifdef HAVE_ELFICON
32#include <tqimage.h>
33#include "tdelficon.h"
34#endif // HAVE_ELFICON
35
36#include "kurifilter.h"
37
38template class TQPtrList<KURIFilterPlugin>;
39
40KURIFilterPlugin::KURIFilterPlugin( TQObject *parent, const char *name, double pri )
41 :TQObject( parent, name )
42{
43 m_strName = TQString::fromLatin1( name );
44 m_dblPriority = pri;
45}
46
47void KURIFilterPlugin::setFilteredURI( KURIFilterData& data, const KURL& uri ) const
48{
49 if ( data.uri() != uri )
50 {
51 data.m_pURI = uri;
52 data.m_bChanged = true;
53 }
54}
55
56class KURIFilterDataPrivate
57{
58public:
59 KURIFilterDataPrivate() {};
60 TQString abs_path;
61 TQString args;
62 TQString typedString;
63};
64
65KURIFilterData::KURIFilterData( const KURIFilterData& data )
66{
67 m_iType = data.m_iType;
68 m_pURI = data.m_pURI;
69 m_strErrMsg = data.m_strErrMsg;
70 m_strIconName = data.m_strIconName;
71 m_bChanged = data.m_bChanged;
72 m_bCheckForExecutables = data.m_bCheckForExecutables;
73 d = new KURIFilterDataPrivate;
74 d->abs_path = data.absolutePath();
75 d->typedString = data.typedString();
76 d->args = data.argsAndOptions();
77}
78
79KURIFilterData::~KURIFilterData()
80{
81 delete d;
82 d = 0;
83}
84
85void KURIFilterData::init( const KURL& url )
86{
87 m_iType = KURIFilterData::UNKNOWN;
88 m_pURI = url;
89 m_strErrMsg = TQString::null;
90 m_strIconName = TQString::null;
91 m_bCheckForExecutables = true;
92 m_bChanged = true;
93 d = new KURIFilterDataPrivate;
94 d->typedString = url.url();
95}
96
97void KURIFilterData::init( const TQString& url )
98{
99 m_iType = KURIFilterData::UNKNOWN;
100 m_pURI = url;
101 m_strErrMsg = TQString::null;
102 m_strIconName = TQString::null;
103 m_bCheckForExecutables = true;
104 m_bChanged = true;
105 d = new KURIFilterDataPrivate;
106 d->typedString = url;
107}
108
109void KURIFilterData::reinit(const KURL &url)
110{
111 delete d;
112 init(url);
113}
114
115void KURIFilterData::reinit(const TQString &url)
116{
117 delete d;
118 init(url);
119}
120
121TQString KURIFilterData::typedString() const
122{
123 return d->typedString;
124}
125
126void KURIFilterData::setCheckForExecutables( bool check )
127{
128 m_bCheckForExecutables = check;
129}
130
131bool KURIFilterData::hasArgsAndOptions() const
132{
133 return !d->args.isEmpty();
134}
135
136bool KURIFilterData::hasAbsolutePath() const
137{
138 return !d->abs_path.isEmpty();
139}
140
141bool KURIFilterData::setAbsolutePath( const TQString& absPath )
142{
143 // Since a malformed URL could possibly be a relative
144 // URL we tag it as a possible local resource...
145 if( (!m_pURI.isValid() || m_pURI.isLocalFile()) )
146 {
147 d->abs_path = absPath;
148 return true;
149 }
150 return false;
151}
152
153TQString KURIFilterData::absolutePath() const
154{
155 return d->abs_path;
156}
157
158TQString KURIFilterData::argsAndOptions() const
159{
160 return d->args;
161}
162
163TQString KURIFilterData::iconName()
164{
165 if( m_bChanged )
166 {
167 m_customIconPixmap = TQPixmap();
168 switch ( m_iType )
169 {
170 case KURIFilterData::LOCAL_FILE:
171 case KURIFilterData::LOCAL_DIR:
172 case KURIFilterData::NET_PROTOCOL:
173 {
174 m_strIconName = KMimeType::iconForURL( m_pURI );
175 break;
176 }
177 case KURIFilterData::EXECUTABLE:
178 {
179 TQString exeName = m_pURI.url();
180 exeName = exeName.mid( exeName.findRev( '/' ) + 1 ); // strip path if given
181 KService::Ptr service = KService::serviceByDesktopName( exeName );
182#ifndef HAVE_ELFICON
183 // Try to find an icon with the same name as the binary (useful for non-tde apps)
184 // FIXME: We should only do this if the binary is in the system path somewhere,
185 // otherwise TDE could end up showing system icons for user binaries
186 if (service && service->icon() != TQString::fromLatin1( "unknown" )) {
187 m_strIconName = service->icon();
188 }
189 else if ( !TDEGlobal::iconLoader()->loadIcon( exeName, TDEIcon::NoGroup, 16, TDEIcon::DefaultState, 0, true ).isNull() ) {
190 m_strIconName = exeName;
191 }
192 else {
193 // use default
194 m_strIconName = TQString::fromLatin1("application-x-executable");
195 }
196#else // HAVE_ELFICON
197 // Try to find an icon with the same name as the binary (useful for non-tde apps)
198 // FIXME: We should only do this if the binary is in the system path somewhere,
199 // otherwise TDE could end up showing system icons for user binaries
200 if (service && service->icon() != TQString::fromLatin1( "unknown" )) {
201 m_strIconName = service->icon();
202 }
203 else if ( !TDEGlobal::iconLoader()->loadIcon( exeName, TDEIcon::NoGroup, 16, TDEIcon::DefaultState, 0, true ).isNull() ) {
204 m_strIconName = exeName;
205 }
206 else {
207 // use default
208 m_strIconName = TQString::fromLatin1("application-x-executable");
209 }
210 // Try to load from elf file (if supported)
211 // Check for an embedded icon
212 unsigned int icon_size;
213 libr_icon *icon = NULL;
214 libr_file *handle = NULL;
215 libr_access_t access = LIBR_READ;
216 char libr_can_continue = 1;
217
218 if((handle = libr_open(const_cast<char*>(m_pURI.path().ascii()), access)) == NULL)
219 {
220 kdWarning() << "failed to open file " << m_pURI.path() << endl;
221 libr_can_continue = 0;
222 }
223
224 if (libr_can_continue == 1) {
225 icon_size = 32; // FIXME: Is this a reasonable size request for all possible usages of kurifilter?
226 icon = libr_icon_geticon_bysize(handle, icon_size);
227
228 if (libr_can_continue == 1) {
229 // See if the embedded icon name matches any icon file names already on the system
230 // If it does, use the system icon instead of the embedded one
231 int iconresnamefound = 0;
232 iconentry *entry = NULL;
233 iconlist icons;
234 if(!get_iconlist(handle, &icons))
235 {
236 // Failed to obtain a list of ELF icons
237 kdDebug() << "failed to obtain ELF icon from " << m_pURI.path() << ": " << libr_errmsg() << endl;
238
239 // See if there is a system icon we can use
240 TQString sysIconName = elf_get_resource(handle, ".metadata_sysicon");
241 if (!sysIconName.isEmpty()) {
242 if (TDEGlobal::iconLoader()->iconPath(sysIconName.ascii(), 0, true) != "") {
243 m_strIconName = sysIconName;
244 }
245 }
246
247 libr_close(handle);
248 libr_can_continue = 0;
249 }
250 else {
251 while((entry = get_nexticon(&icons, entry)) != NULL)
252 {
253 if(icon == NULL)
254 {
255 // Try loading this icon as fallback
256 icon = libr_icon_geticon_byname(handle, entry->name);
257 }
258 if (TDEGlobal::iconLoader()->iconPath(entry->name, 0, true) != "") {
259 iconresnamefound = 1;
260 m_strIconName = entry->name;
261 break;
262 }
263 }
264 }
265
266 if (libr_can_continue == 1) {
267 if ((iconresnamefound == 0) && (icon)) {
268 // Extract the embedded icon
269 size_t icon_data_length;
270 char* icondata = libr_icon_malloc(icon, &icon_data_length);
271 m_customIconPixmap.loadFromData(static_cast<uchar*>(static_cast<void*>(icondata)), icon_data_length); // EVIL CAST
272 if (icon_size != 0) {
273 TQImage ip = m_customIconPixmap.convertToImage();
274 ip = ip.smoothScale(icon_size, icon_size);
275 m_customIconPixmap.convertFromImage(ip);
276 }
277 free(icondata);
278 libr_icon_close(icon);
279 }
280
281 libr_close(handle);
282 }
283 }
284 }
285#endif // HAVE_ELFICON
286 break;
287 }
288 case KURIFilterData::HELP:
289 {
290 m_strIconName = TQString::fromLatin1("khelpcenter");
291 break;
292 }
293 case KURIFilterData::SHELL:
294 {
295 m_strIconName = TQString::fromLatin1("konsole");
296 break;
297 }
298 case KURIFilterData::ERROR:
299 case KURIFilterData::BLOCKED:
300 {
301 m_strIconName = TQString::fromLatin1("error");
302 break;
303 }
304 default:
305 m_strIconName = TQString::null;
306 break;
307 }
308 m_bChanged = false;
309 }
310 return m_strIconName;
311}
312
313TQPixmap KURIFilterData::customIconPixmap()
314{
315 return m_customIconPixmap;
316}
317
318//******************************************** KURIFilterPlugin **********************************************
319void KURIFilterPlugin::setArguments( KURIFilterData& data, const TQString& args ) const
320{
321 data.d->args = args;
322}
323
324//******************************************** KURIFilter **********************************************
325KURIFilter *KURIFilter::s_self;
326static KStaticDeleter<KURIFilter> kurifiltersd;
327
328KURIFilter *KURIFilter::self()
329{
330 if (!s_self)
331 s_self = kurifiltersd.setObject(s_self, new KURIFilter);
332 return s_self;
333}
334
335KURIFilter::KURIFilter()
336{
337 m_lstPlugins.setAutoDelete(true);
338 loadPlugins();
339}
340
341KURIFilter::~KURIFilter()
342{
343}
344
345bool KURIFilter::filterURI( KURIFilterData& data, const TQStringList& filters )
346{
347 bool filtered = false;
348 KURIFilterPluginList use_plugins;
349
350 // If we have a filter list, only include the once
351 // explicitly specified by it. Otherwise, use all available filters...
352 if( filters.isEmpty() )
353 use_plugins = m_lstPlugins; // Use everything that is loaded...
354 else
355 {
356 //kdDebug() << "Named plugins requested..." << endl;
357 for( TQStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst )
358 {
359 TQPtrListIterator<KURIFilterPlugin> it( m_lstPlugins );
360 for( ; it.current() ; ++it )
361 {
362 if( (*lst) == it.current()->name() )
363 {
364 //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl;
365 use_plugins.append( it.current() );
366 break; // We already found it ; so lets test the next named filter...
367 }
368 }
369 }
370 }
371
372 TQPtrListIterator<KURIFilterPlugin> it( use_plugins );
373 //kdDebug() << "Using " << use_plugins.count() << " out of the "
374 // << m_lstPlugins.count() << " available plugins" << endl;
375 for (; it.current() && !filtered; ++it)
376 {
377 //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl;
378 filtered |= it.current()->filterURI( data );
379 }
380 return filtered;
381}
382
383bool KURIFilter::filterURI( KURL& uri, const TQStringList& filters )
384{
385 KURIFilterData data = uri;
386 bool filtered = filterURI( data, filters );
387 if( filtered ) uri = data.uri();
388 return filtered;
389}
390
391bool KURIFilter::filterURI( TQString& uri, const TQStringList& filters )
392{
393 KURIFilterData data = uri;
394 bool filtered = filterURI( data, filters );
395 if( filtered ) uri = data.uri().url();
396 return filtered;
397
398}
399
400KURL KURIFilter::filteredURI( const KURL &uri, const TQStringList& filters )
401{
402 KURIFilterData data = uri;
403 filterURI( data, filters );
404 return data.uri();
405}
406
407TQString KURIFilter::filteredURI( const TQString &uri, const TQStringList& filters )
408{
409 KURIFilterData data = uri;
410 filterURI( data, filters );
411 return data.uri().url();
412}
413
414TQPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const
415{
416 return TQPtrListIterator<KURIFilterPlugin>(m_lstPlugins);
417}
418
419TQStringList KURIFilter::pluginNames() const
420{
421 TQStringList list;
422 for(TQPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i)
423 list.append((*i)->name());
424 return list;
425}
426
427void KURIFilter::loadPlugins()
428{
429 TDETrader::OfferList offers = TDETrader::self()->query( "KURIFilter/Plugin" );
430
431 TDETrader::OfferList::ConstIterator it = offers.begin();
432 TDETrader::OfferList::ConstIterator end = offers.end();
433
434 for (; it != end; ++it )
435 {
436 KURIFilterPlugin *plugin = KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it, 0, (*it)->desktopEntryName().latin1() );
437 if ( plugin )
438 m_lstPlugins.append( plugin );
439 }
440
441 // NOTE: Plugin priority is now determined by
442 // the entry in the .desktop files...
443 // TODO: Config dialog to differentiate "system"
444 // plugins from "user-defined" ones...
445 // m_lstPlugins.sort();
446}
447
448void KURIFilterPlugin::virtual_hook( int, void* )
449{ /*BASE::virtual_hook( id, data );*/ }
450
451#include "kurifilter.moc"
KMimeType::iconForURL
static TQString iconForURL(const KURL &_url, mode_t _mode=0)
The same functionality as pixmapForURL(), but this method returns the name of the icon to load.
Definition kmimetype.cpp:507
KService::serviceByDesktopName
static Ptr serviceByDesktopName(const TQString &_name)
Find a service by the name of its desktop file, not depending on its actual location (as long as it's...
Definition kservice.cpp:680
KURIFilterData
A basic message object used for exchanging filtering information between the filter plugins and the a...
Definition kurifilter.h:80
KURIFilterData::KURIFilterData
KURIFilterData()
Default constructor.
Definition kurifilter.h:107
KURIFilterData::init
void init(const KURL &url)
Initializes the KURIFilterData on construction.
Definition kurifilter.cpp:85
KURIFilterData::uri
KURL uri() const
Returns the filtered or the original URL.
Definition kurifilter.h:157
KURIFilterData::customIconPixmap
TQPixmap customIconPixmap()
Returns the current custom icon The results are valid iff iconName() has returned TQString::null.
Definition kurifilter.cpp:313
KURIFilterData::argsAndOptions
TQString argsAndOptions() const
Returns the command line options and arguments for a local resource when present.
Definition kurifilter.cpp:158
KURIFilterData::~KURIFilterData
~KURIFilterData()
Destructor.
Definition kurifilter.cpp:79
KURIFilterData::setAbsolutePath
bool setAbsolutePath(const TQString &abs_path)
Sets the absolute path to be used whenever the supplied data is a relative local URL.
Definition kurifilter.cpp:141
KURIFilterData::setCheckForExecutables
void setCheckForExecutables(bool check)
Check whether the provided uri is executable or not.
Definition kurifilter.cpp:126
KURIFilterData::iconName
TQString iconName()
Returns the name of the icon that matches the current filtered URL.
Definition kurifilter.cpp:163
KURIFilterData::hasArgsAndOptions
bool hasArgsAndOptions() const
Checks whether the current data is a local resource with command line options and arguments.
Definition kurifilter.cpp:131
KURIFilterData::absolutePath
TQString absolutePath() const
Returns the absolute path if one has already been set.
Definition kurifilter.cpp:153
KURIFilterData::hasAbsolutePath
bool hasAbsolutePath() const
Checks whether the supplied data had an absolute path.
Definition kurifilter.cpp:136
KURIFilterData::typedString
TQString typedString() const
Definition kurifilter.cpp:121
KURIFilterPluginList
A list of filter plugins.
Definition kurifilter.h:464
KURIFilterPlugin
Base class for URI filter plugins.
Definition kurifilter.h:364
KURIFilterPlugin::setFilteredURI
void setFilteredURI(KURIFilterData &data, const KURL &uri) const
Sets the the URL in data to uri.
Definition kurifilter.cpp:47
KURIFilterPlugin::KURIFilterPlugin
KURIFilterPlugin(TQObject *parent=0, const char *name=0, double pri=1.0)
Constructs a filter plugin with a given name and priority.
Definition kurifilter.cpp:40
KURIFilterPlugin::name
virtual TQString name() const
Returns the filter's name.
Definition kurifilter.h:385
KURIFilterPlugin::setArguments
void setArguments(KURIFilterData &data, const TQString &args) const
Sets the arguments and options string in data to args if any were found during filterting.
Definition kurifilter.cpp:319
KURIFilter
Manages the filtering of URIs.
Definition kurifilter.h:549
KURIFilter::filteredURI
KURL filteredURI(const KURL &uri, const TQStringList &filters=TQStringList())
Returns the filtered URI.
Definition kurifilter.cpp:400
KURIFilter::pluginNames
TQStringList pluginNames() const
Return a list of the names of all loaded plugins.
Definition kurifilter.cpp:419
KURIFilter::filterURI
bool filterURI(KURIFilterData &data, const TQStringList &filters=TQStringList())
Filters the URI given by the object URIFilterData.
Definition kurifilter.cpp:345
KURIFilter::self
static KURIFilter * self()
Returns an instance of KURIFilter.
Definition kurifilter.cpp:328
KURIFilter::KURIFilter
KURIFilter()
A protected constructor.
Definition kurifilter.cpp:335
KURIFilter::pluginsIterator
TQPtrListIterator< KURIFilterPlugin > pluginsIterator() const
Return an iterator to iterate over all loaded plugins.
Definition kurifilter.cpp:414
KURIFilter::~KURIFilter
~KURIFilter()
Destructor.
Definition kurifilter.cpp:341
KURIFilter::loadPlugins
void loadPlugins()
Loads all allowed plugins.
Definition kurifilter.cpp:427
TDETrader::query
virtual OfferList query(const TQString &servicetype, const TQString &constraint=TQString::null, const TQString &preferences=TQString::null) const
The main function in the TDETrader class.
Definition ktrader.cpp:106
TDETrader::self
static TDETrader * self()
This is a static pointer to a TDETrader instance.
Definition ktrader.cpp:90
TDETrader::OfferList
TQValueList< KService::Ptr > OfferList
A list of services.
Definition ktrader.h:92

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.9.8
This website is maintained by Timothy Pearson.