Kontact Plugin Interface Library
uniqueapphandler.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "uniqueapphandler.h"
00023 #include <qdbusabstractadaptor.h>
00024 #include "core.h"
00025
00026 #include <kpimutils/processes.h>
00027
00028 #include <kapplication.h>
00029 #include <kcmdlineargs.h>
00030 #include <kdebug.h>
00031 #include <kstartupinfo.h>
00032 #include <kuniqueapplication.h>
00033 #include <kwindowsystem.h>
00034
00035 #include <QDBusConnection>
00036 #include <QDBusConnectionInterface>
00037
00038 #ifdef Q_WS_WIN
00039 # include <process.h>
00040 #endif
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 using namespace KontactInterface;
00088
00089
00090 class UniqueAppHandler::Private
00091 {
00092 public:
00093 Plugin *mPlugin;
00094 };
00095
00096
00097 UniqueAppHandler::UniqueAppHandler( Plugin *plugin )
00098 : d( new Private )
00099 {
00100
00101
00102 d->mPlugin = plugin;
00103 QDBusConnection session = QDBusConnection::sessionBus();
00104 const QString appName = plugin->objectName();
00105 session.registerService( "org.kde." + appName );
00106 const QString objectName = QString( '/' ) + appName + "_PimApplication";
00107 session.registerObject( objectName, this, QDBusConnection::ExportAllSlots );
00108 }
00109
00110 UniqueAppHandler::~UniqueAppHandler()
00111 {
00112 delete d;
00113 }
00114
00115
00116 int UniqueAppHandler::newInstance( const QByteArray &asn_id, const QByteArray &args )
00117 {
00118 if ( !asn_id.isEmpty() ) {
00119 kapp->setStartupId( asn_id );
00120 }
00121
00122 KCmdLineArgs::reset();
00123 loadCommandLineOptions();
00124
00125
00126 QDataStream ds( args );
00127 KCmdLineArgs::loadAppArgs( ds );
00128
00129 return newInstance();
00130 }
00131
00132 static QWidget *s_mainWidget = 0;
00133
00134
00135 int KontactInterface::UniqueAppHandler::newInstance()
00136 {
00137 if ( s_mainWidget ) {
00138 s_mainWidget->show();
00139 KWindowSystem::forceActiveWindow( s_mainWidget->winId() );
00140 KStartupInfo::appStarted();
00141 }
00142
00143
00144 d->mPlugin->core()->selectPlugin( d->mPlugin );
00145 return 0;
00146 }
00147
00148 Plugin *UniqueAppHandler::plugin() const
00149 {
00150 return d->mPlugin;
00151 }
00152
00153 bool KontactInterface::UniqueAppHandler::load()
00154 {
00155 (void)d->mPlugin->part();
00156 return true;
00157 }
00158
00159
00160 class UniqueAppWatcher::Private
00161 {
00162 public:
00163 UniqueAppHandlerFactoryBase *mFactory;
00164 Plugin *mPlugin;
00165 bool mRunningStandalone;
00166 };
00167
00168
00169 UniqueAppWatcher::UniqueAppWatcher( UniqueAppHandlerFactoryBase *factory, Plugin *plugin )
00170 : QObject( plugin ), d( new Private )
00171 {
00172 d->mFactory = factory;
00173 d->mPlugin = plugin;
00174
00175
00176 const QString serviceName = "org.kde." + plugin->objectName();
00177 d->mRunningStandalone =
00178 QDBusConnection::sessionBus().interface()->isServiceRegistered( serviceName );
00179 #ifdef Q_WS_WIN
00180 if ( d->mRunningStandalone ) {
00181 QList<int> pids;
00182 KPIMUtils::getProcessesIdForName( plugin->objectName(), pids );
00183 const int mypid = getpid();
00184 bool processExits = false;
00185 foreach ( int pid, pids ) {
00186 if ( mypid != pid ) {
00187 processExits = true;
00188 break;
00189 }
00190 }
00191 if ( !processExits ) {
00192 d->mRunningStandalone = false;
00193 }
00194 }
00195 #endif
00196
00197 QString owner = QDBusConnection::sessionBus().interface()->serviceOwner( serviceName );
00198 if ( d->mRunningStandalone && ( owner == QDBusConnection::sessionBus().baseService() ) ) {
00199 d->mRunningStandalone = false;
00200 }
00201
00202
00203
00204 if ( d->mRunningStandalone ) {
00205 QObject::connect( QDBusConnection::sessionBus().interface(),
00206 SIGNAL(serviceOwnerChanged(QString,QString,QString)),
00207 this, SLOT(slotApplicationRemoved(QString,QString,QString)) );
00208 } else {
00209 d->mFactory->createHandler( d->mPlugin );
00210 }
00211 }
00212
00213 UniqueAppWatcher::~UniqueAppWatcher()
00214 {
00215 delete d->mFactory;
00216 delete d;
00217 }
00218
00219 bool UniqueAppWatcher::isRunningStandalone() const
00220 {
00221 return d->mRunningStandalone;
00222 }
00223
00224 void KontactInterface::UniqueAppWatcher::slotApplicationRemoved( const QString &name,
00225 const QString &oldOwner,
00226 const QString &newOwner )
00227 {
00228 if ( oldOwner.isEmpty() || !newOwner.isEmpty() ) {
00229 return;
00230 }
00231
00232 const QString serviceName = "org.kde." + d->mPlugin->objectName();
00233 if ( name == serviceName && d->mRunningStandalone ) {
00234 d->mFactory->createHandler( d->mPlugin );
00235 d->mRunningStandalone = false;
00236 }
00237 }
00238
00239 void KontactInterface::UniqueAppHandler::setMainWidget( QWidget *widget )
00240 {
00241 s_mainWidget = widget;
00242 }
00243
00244 #include "uniqueapphandler.moc"