KDECore
kservice.h
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org> 00003 Copyright 1999-2006 David Faure <faure@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #ifndef KSERVICE_H 00022 #define KSERVICE_H 00023 00024 #include "kserviceaction.h" 00025 #include <QtCore/QStringList> 00026 #include <QtCore/QVariant> 00027 #ifndef KDE_NO_DEPRECATED 00028 #include <klibloader.h> 00029 #endif 00030 #include <kpluginfactory.h> 00031 #include <kpluginloader.h> 00032 #include <ksycocaentry.h> 00033 #include <klocale.h> 00034 00035 class KServiceType; 00036 class QDataStream; 00037 class KDesktopFile; 00038 class QWidget; 00039 00040 class KServicePrivate; 00041 00058 class KDECORE_EXPORT KService : public KSycocaEntry // TODO KDE5: inherit kshared, but move KSycocaEntry to Private 00059 { 00060 public: 00061 typedef KSharedPtr<KService> Ptr; 00062 typedef QList<Ptr> List; 00063 00070 KService(const QString & name, const QString &exec, const QString &icon); 00071 00077 explicit KService( const QString & fullpath ); 00078 00083 explicit KService( const KDesktopFile *config ); 00084 00090 KService( QDataStream& str, int offset ); 00091 00092 virtual ~KService(); 00093 00099 bool isApplication() const; 00100 00106 #ifndef KDE_NO_DEPRECATED 00107 KDE_DEPRECATED QString type() const; 00108 #endif 00109 00115 QString exec() const; 00121 QString library() const; 00122 00128 QString icon() const; 00133 bool terminal() const; 00134 00143 QString terminalOptions() const; 00149 bool substituteUid() const; 00157 QString username() const; 00158 00173 #ifndef KDE_NO_DEPRECATED 00174 KDE_DEPRECATED QString desktopEntryPath() const; 00175 #endif 00176 00183 QString desktopEntryName() const; 00184 00190 QString menuId() const; 00191 00198 QString storageId() const; 00199 00212 enum DBusStartupType { DBusNone = 0, DBusUnique, DBusMulti, DBusWait }; 00213 00218 DBusStartupType dbusStartupType() const; 00219 00225 QString path() const; 00226 00232 QString comment() const; 00233 00240 QString genericName() const; 00241 00249 QString untranslatedGenericName() const; 00250 00255 QStringList keywords() const; 00256 00261 QStringList categories() const; 00262 00269 QStringList serviceTypes() const; 00270 00278 bool hasServiceType( const QString& serviceTypePtr ) const; 00279 00298 #ifndef KDE_NO_DEPRECATED 00299 KDE_DEPRECATED bool hasMimeType( const KServiceType* mimeTypePtr ) const; 00300 #endif 00301 00308 bool hasMimeType( const QString& mimeType ) const; 00309 00320 bool allowAsDefault() const; 00321 00325 QList<KServiceAction> actions() const; 00326 00333 bool allowMultipleFiles() const; 00334 00341 int initialPreference() const; 00342 00350 bool noDisplay() const; 00351 00362 bool showInKDE() const; 00363 00369 QString parentApp() const; 00370 00376 QString pluginKeyword() const; 00377 00383 QString docPath() const; 00384 00393 QVariant property( const QString& _name, QVariant::Type t ) const; 00394 00395 using KSycocaEntry::property; 00396 00402 QString locateLocal() const; 00403 00408 void setMenuId(const QString &menuId); 00413 void setTerminal(bool b); 00418 void setTerminalOptions(const QString &options); 00419 00430 #ifndef KDE_NO_DEPRECATED 00431 static Ptr serviceByName( const QString& _name ); // KDE5: remove 00432 #endif 00433 00443 static Ptr serviceByDesktopPath( const QString& _path ); 00444 00459 static Ptr serviceByDesktopName( const QString& _name ); 00460 00469 static Ptr serviceByMenuId( const QString& _menuId ); 00470 00480 static Ptr serviceByStorageId( const QString& _storageId ); 00481 00491 static List allServices(); 00492 00508 static QString newServicePath(bool showInMenu, const QString &suggestedName, 00509 QString *menuId = 0, 00510 const QStringList *reservedMenuIds = 0); 00511 00524 template <class T> 00525 T *createInstance(QObject *parent = 0, 00526 const QVariantList &args = QVariantList(), QString *error = 0) const 00527 { 00528 return createInstance<T>(0, parent, args, error); 00529 } 00530 00544 template <class T> 00545 T *createInstance(QWidget *parentWidget, QObject *parent, 00546 const QVariantList &args = QVariantList(), QString *error = 0) const 00547 { 00548 KPluginLoader pluginLoader(*this); 00549 KPluginFactory *factory = pluginLoader.factory(); 00550 if (factory) { 00551 T *o = factory->template create<T>(parentWidget, parent, pluginKeyword(), args); 00552 if (!o && error) 00553 *error = i18n("The service '%1' does not provide an interface '%2' with keyword '%3'", 00554 name(), QString::fromLatin1(T::staticMetaObject.className()), pluginKeyword()); 00555 return o; 00556 } 00557 else if (error) { 00558 *error = pluginLoader.errorString(); 00559 pluginLoader.unload(); 00560 } 00561 return 0; 00562 } 00563 00567 #ifndef KDE_NO_DEPRECATED 00568 template <class T> 00569 static KDE_DEPRECATED T *createInstance(const KService::Ptr &service, QObject *parent = 0, 00570 const QVariantList &args = QVariantList(), QString *error = 0) 00571 { 00572 return service->createInstance<T>(parent, args, error); 00573 } 00574 #endif 00575 00580 #ifndef KDE_NO_DEPRECATED 00581 template <class T> 00582 static KDE_DEPRECATED T *createInstance( const KService::Ptr &service, 00583 QObject *parent, 00584 const QStringList &args, 00585 int *error = 0 ) 00586 { 00587 const QString library = service->library(); 00588 if ( library.isEmpty() ) { 00589 if ( error ) 00590 *error = KLibLoader::ErrServiceProvidesNoLibrary; 00591 return 0; 00592 } 00593 00594 return KLibLoader::createInstance<T>( library, parent, args, error ); 00595 } 00596 #endif 00597 00613 #ifndef KDE_NO_DEPRECATED 00614 template <class T, class ServiceIterator> 00615 static KDE_DEPRECATED T *createInstance(ServiceIterator begin, ServiceIterator end, QObject *parent = 0, 00616 const QVariantList &args = QVariantList(), QString *error = 0) 00617 { 00618 for (; begin != end; ++begin) { 00619 KService::Ptr service = *begin; 00620 if (error) { 00621 error->clear(); 00622 } 00623 00624 T *component = createInstance<T>(service, parent, args, error); 00625 if (component) { 00626 return component; 00627 } 00628 } 00629 if (error) { 00630 *error = KLibLoader::errorString(KLibLoader::ErrNoServiceFound); 00631 } 00632 return 0; 00633 } 00634 #endif 00635 00636 #ifndef KDE_NO_DEPRECATED 00637 template <class T, class ServiceIterator> 00638 static KDE_DEPRECATED T *createInstance( ServiceIterator begin, ServiceIterator end, 00639 QObject *parent, 00640 const QStringList &args, 00641 int *error = 0 ) 00642 { 00643 for (; begin != end; ++begin ) { 00644 KService::Ptr service = *begin; 00645 if ( error ) 00646 *error = 0; 00647 00648 T *component = createInstance<T>( service, parent, args, error ); 00649 if ( component ) 00650 return component; 00651 } 00652 if ( error ) 00653 *error = KLibLoader::ErrNoServiceFound; 00654 return 0; 00655 } 00656 #endif 00657 00658 protected: 00659 friend class KMimeAssociations; 00660 friend class KBuildServiceFactory; 00661 00663 struct ServiceTypeAndPreference 00664 { 00665 ServiceTypeAndPreference() 00666 : preference(-1), serviceType() {} 00667 ServiceTypeAndPreference(int pref, const QString& servType) 00668 : preference(pref), serviceType(servType) {} 00669 int preference; 00670 QString serviceType; // or mimetype 00671 }; 00673 QVector<ServiceTypeAndPreference>& _k_accessServiceTypes(); 00674 00675 friend QDataStream& operator>>( QDataStream&, ServiceTypeAndPreference& ); 00676 friend QDataStream& operator<<( QDataStream&, const ServiceTypeAndPreference& ); 00677 private: 00678 Q_DECLARE_PRIVATE(KService) 00679 }; 00680 #endif
KDE 4.6 API Reference