• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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   const QString appName = KCmdLineArgs::aboutData()->appName();
00076   // Try talking to /appName_PimApplication in org.kde.appName,
00077   // (which could be kontact or the standalone application),
00078   // otherwise fall back to standard KUniqueApplication behavior (start appName).
00079 
00080   const QString serviceName = "org.kde." + appName;
00081   if ( tryToInitDBusConnection().interface()->isServiceRegistered( serviceName ) ) {
00082     QByteArray saved_args;
00083     QDataStream ds( &saved_args, QIODevice::WriteOnly );
00084     KCmdLineArgs::saveAppArgs( ds );
00085 
00086     QByteArray new_asn_id;
00087 #if defined Q_WS_X11
00088     KStartupInfoId id;
00089     if( kapp ) { // KApplication constructor unsets the env. variable
00090       id.initId( kapp->startupId() );
00091     } else {
00092       id = KStartupInfo::currentStartupIdEnv();
00093     }
00094     if( !id.none() ) {
00095       new_asn_id = id.id();
00096     }
00097 #endif
00098 
00099     KWindowSystem::allowExternalProcessWindowActivation();
00100 
00101     const QString objectName = QString( '/' ) + appName + "_PimApplication";
00102     //kDebug() << objectName;
00103     QDBusInterface iface(
00104       serviceName, objectName, "org.kde.KUniqueApplication", QDBusConnection::sessionBus() );
00105     QDBusReply<int> reply;
00106     if ( iface.isValid() &&
00107          ( reply = iface.call( "newInstance", new_asn_id, saved_args ) ).isValid() ) {
00108       return false; // success means that main() can exit now.
00109     }
00110   }
00111 
00112   QDBusConnection::disconnectFromBus( _k_sessionBusName );
00113 
00114   //kDebug() << "kontact not running -- start standalone application";
00115   // kontact not running -- start standalone application.
00116   return KUniqueApplication::start();
00117 }

Kontact Plugin Interface Library

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal