kio Library API Documentation

kdcopservicestarter.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2003 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "kdcopservicestarter.h" 00020 #include "ktrader.h" 00021 #include <kapplication.h> 00022 #include "kservice.h" 00023 #include <kstaticdeleter.h> 00024 #include <kdebug.h> 00025 #include <klocale.h> 00026 #include <dcopclient.h> 00027 00028 static KStaticDeleter<KDCOPServiceStarter> dss_sd; 00029 KDCOPServiceStarter* KDCOPServiceStarter::s_self; 00030 00031 KDCOPServiceStarter* KDCOPServiceStarter::self() 00032 { 00033 if ( !s_self ) 00034 dss_sd.setObject( s_self, new KDCOPServiceStarter ); 00035 return s_self; 00036 } 00037 00038 KDCOPServiceStarter::KDCOPServiceStarter() 00039 { 00040 // Set the singleton instance - useful when a derived KDCOPServiceStarter 00041 // was created (before self() was called) 00042 s_self = this; 00043 } 00044 00045 KDCOPServiceStarter::~KDCOPServiceStarter() 00046 { 00047 } 00048 00049 int KDCOPServiceStarter::findServiceFor( const QString& serviceType, 00050 const QString& _constraint, 00051 const QString& preferences, 00052 QString *error, QCString* pDcopService, 00053 int flags ) 00054 { 00055 // Ask the trader which service is preferred for this servicetype 00056 // We want one that provides a DCOP interface 00057 QString constraint = _constraint; 00058 if ( !constraint.isEmpty() ) 00059 constraint += " and "; 00060 constraint += "exist [X-DCOP-ServiceName]"; 00061 KTrader::OfferList offers = KTrader::self()->query(serviceType, "Application", constraint, preferences); 00062 if ( offers.isEmpty() ) { 00063 if ( error ) 00064 *error = i18n("No service implementing %1").arg( serviceType ); 00065 kdWarning() << "KDCOPServiceStarter: No service implementing " << serviceType << endl; 00066 return -1; 00067 } 00068 KService::Ptr ptr = offers.first(); 00069 QCString dcopService = ptr->property("X-DCOP-ServiceName").toString().latin1(); 00070 00071 if ( !kapp->dcopClient()->isApplicationRegistered( dcopService ) ) 00072 { 00073 QString error; 00074 if ( startServiceFor( serviceType, constraint, preferences, &error, &dcopService, flags ) != 0 ) 00075 { 00076 kdDebug() << "KDCOPServiceStarter: Couldn't start service: " << error << endl; 00077 return -2; 00078 } 00079 } 00080 kdDebug() << "KDCOPServiceStarter: DCOP service is available now, as " << dcopService << endl; 00081 if ( pDcopService ) 00082 *pDcopService = dcopService; 00083 return 0; 00084 } 00085 00086 int KDCOPServiceStarter::startServiceFor( const QString& serviceType, 00087 const QString& constraint, 00088 const QString& preferences, 00089 QString *error, QCString* dcopService, int /*flags*/ ) 00090 { 00091 KTrader::OfferList offers = KTrader::self()->query(serviceType, "Application", constraint, preferences); 00092 if ( offers.isEmpty() ) 00093 return -1; 00094 KService::Ptr ptr = offers.first(); 00095 kdDebug() << "KDCOPServiceStarter: starting " << ptr->desktopEntryPath() << endl; 00096 return kapp->startServiceByDesktopPath( ptr->desktopEntryPath(), QStringList(), error, dcopService ); 00097 }
KDE Logo
This file is part of the documentation for kio Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:55:26 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003