• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

Kontact Plugin Interface Library

pimuniqueapplication.cpp
00001 /* This file is part of the KDE project
00002 
00003    Copyright 2008 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU Library General Public License as published
00007    by the Free Software Foundation; either version 2 of the License or
00008    ( at your option ) version 3 or, at the discretion of KDE e.V.
00009    ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "pimuniqueapplication.h"
00023 
00024 #include <kaboutdata.h>
00025 #include <kcmdlineargs.h>
00026 #include <kdebug.h>
00027 #include <kstartupinfo.h>
00028 #include <kwindowsystem.h>
00029 
00030 #include <qdbusconnectioninterface.h>
00031 #include <qdbusinterface.h>
00032 
00033 using namespace KontactInterface;
00034 
00035 //@cond PRIVATE
00036 class KontactInterface::PimUniqueApplication::Private
00037 {
00038 };
00039 //@endcond
00040 
00041 PimUniqueApplication::PimUniqueApplication()
00042   : KUniqueApplication(), d( new Private() )
00043 {
00044   // This object name is used in start(), and also in kontact's UniqueAppHandler.
00045   const QString objectName = QString( '/' ) + applicationName() + "_PimApplication";
00046   QDBusConnection::sessionBus().registerObject(
00047     objectName, this,
00048     QDBusConnection::ExportScriptableSlots |
00049     QDBusConnection::ExportScriptableProperties |
00050     QDBusConnection::ExportAdaptors );
00051 }
00052 
00053 static const char _k_sessionBusName[] = "kdepimapplication_session_bus";
00054 
00055 PimUniqueApplication::~PimUniqueApplication()
00056 {
00057     delete d;
00058 }
00059 
00060 static QDBusConnection tryToInitDBusConnection()
00061 {
00062   // Check the D-Bus connection health, and connect - but not using QDBusConnection::sessionBus()
00063   // since we can't close that one before forking.
00064   QDBusConnection connection = QDBusConnection::connectToBus(
00065     QDBusConnection::SessionBus, _k_sessionBusName );
00066   if ( !connection.isConnected() ) {
00067     kError() << "Cannot find the D-Bus session server" << endl; //krazy:exclude=kdebug
00068     ::exit(255);
00069   }
00070   return connection;
00071 }
00072 
00073 bool PimUniqueApplication::start()
00074 {
00075   return start( KUniqueApplication::StartFlags() );
00076 }
00077 
00078 bool PimUniqueApplication::start( KUniqueApplication::StartFlags flags )
00079 {
00080   const QString appName = KCmdLineArgs::aboutData()->appName();
00081   // Try talking to /appName_PimApplication in org.kde.appName,
00082   // (which could be kontact or the standalone application),
00083   // otherwise fall back to standard KUniqueApplication behavior (start appName).
00084 
00085   const QString serviceName = "org.kde." + appName;
00086   if ( tryToInitDBusConnection().interface()->isServiceRegistered( serviceName ) ) {
00087     QByteArray saved_args;
00088     QDataStream ds( &saved_args, QIODevice::WriteOnly );
00089     KCmdLineArgs::saveAppArgs( ds );
00090 
00091     QByteArray new_asn_id;
00092 #if defined Q_WS_X11
00093     KStartupInfoId id;
00094     if( kapp ) { // KApplication constructor unsets the env. variable
00095       id.initId( kapp->startupId() );
00096     } else {
00097       id = KStartupInfo::currentStartupIdEnv();
00098     }
00099     if( !id.none() ) {
00100       new_asn_id = id.id();
00101     }
00102 #endif
00103 
00104     KWindowSystem::allowExternalProcessWindowActivation();
00105 
00106     const QString objectName = QString( '/' ) + appName + "_PimApplication";
00107     //kDebug() << objectName;
00108     QDBusInterface iface(
00109       serviceName, objectName, "org.kde.KUniqueApplication", QDBusConnection::sessionBus() );
00110     QDBusReply<int> reply;
00111     if ( iface.isValid() &&
00112          ( reply = iface.call( "newInstance", new_asn_id, saved_args ) ).isValid() ) {
00113       return false; // success means that main() can exit now.
00114     }
00115   }
00116 
00117   QDBusConnection::disconnectFromBus( _k_sessionBusName );
00118 
00119   //kDebug() << "kontact not running -- start standalone application";
00120   // kontact not running -- start standalone application.
00121   return KUniqueApplication::start(flags);
00122 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:11 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kontact Plugin Interface Library

Skip menu "Kontact Plugin Interface Library"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.8.5 API Reference

Skip menu "kdepimlibs-4.8.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal