• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi

agentinstancecreatejob.cpp
00001 /*
00002     Copyright (c) 2008 Volker Krause <vkrause@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or modify it
00005     under the terms of the GNU Library General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or (at your
00007     option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful, but WITHOUT
00010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00012     License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to the
00016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301, USA.
00018 */
00019 
00020 #include "agentinstancecreatejob.h"
00021 
00022 #include "agentinstance.h"
00023 #include "agentmanager.h"
00024 #include "agentmanager_p.h"
00025 #include "controlinterface.h"
00026 #include "dbusconnectionpool.h"
00027 #include "kjobprivatebase_p.h"
00028 
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 
00032 #include <QtCore/QTimer>
00033 
00034 #ifdef Q_OS_UNIX
00035 #include <sys/types.h>
00036 #include <signal.h>
00037 #endif
00038 
00039 using namespace Akonadi;
00040 
00041 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_WINCE)
00042 static const int safetyTimeout = 60000; // ms
00043 #else
00044 static const int safetyTimeout = 10000; // ms
00045 #endif
00046 
00047 
00048 namespace Akonadi {
00052 class AgentInstanceCreateJobPrivate : public KJobPrivateBase
00053 {
00054   public:
00055     AgentInstanceCreateJobPrivate( AgentInstanceCreateJob* parent ) : q( parent ),
00056       parentWidget( 0 ),
00057       safetyTimer( new QTimer( parent ) ),
00058       doConfig( false ),
00059       tooLate( false )
00060     {
00061       QObject::connect( AgentManager::self(), SIGNAL( instanceAdded( const Akonadi::AgentInstance& ) ),
00062                         q, SLOT( agentInstanceAdded( const Akonadi::AgentInstance& ) ) );
00063       QObject::connect( safetyTimer, SIGNAL( timeout() ), q, SLOT( timeout() ) );
00064     }
00065 
00066     void agentInstanceAdded( const AgentInstance &instance )
00067     {
00068       if ( agentInstance == instance && !tooLate ) {
00069         safetyTimer->stop();
00070         if ( doConfig ) {
00071           // return from dbus call first before doing the next one
00072           QTimer::singleShot( 0, q, SLOT( doConfigure() ) );
00073         } else {
00074           q->emitResult();
00075         }
00076       }
00077     }
00078 
00079     void doConfigure()
00080     {
00081       org::freedesktop::Akonadi::Agent::Control *agentControlIface =
00082         new org::freedesktop::Akonadi::Agent::Control( QLatin1String( "org.freedesktop.Akonadi.Agent." ) + agentInstance.identifier(),
00083                                                        QLatin1String( "/" ), DBusConnectionPool::threadConnection(), q );
00084       if ( !agentControlIface || !agentControlIface->isValid() ) {
00085         delete agentControlIface;
00086 
00087         q->setError( KJob::UserDefinedError );
00088         q->setErrorText( i18n( "Unable to access D-Bus interface of created agent." ) );
00089         q->emitResult();
00090         return;
00091       }
00092 
00093       q->connect( agentControlIface, SIGNAL( configurationDialogAccepted() ),
00094                   q, SLOT( configurationDialogAccepted() ) );
00095       q->connect( agentControlIface, SIGNAL( configurationDialogRejected() ),
00096                   q, SLOT( configurationDialogRejected() ) );
00097 
00098       agentInstance.configure( parentWidget );
00099     }
00100 
00101     void configurationDialogAccepted()
00102     {
00103       // The user clicked 'Ok' in the initial configuration dialog, so we assume
00104       // he wants to keep the resource and the job is done.
00105       q->emitResult();
00106     }
00107 
00108     void configurationDialogRejected()
00109     {
00110       // The user clicked 'Cancel' in the initial configuration dialog, so we assume
00111       // he wants to abort the 'create new resource' job and the new resource will be
00112       // removed again.
00113       AgentManager::self()->removeInstance( agentInstance );
00114 
00115       q->emitResult();
00116     }
00117 
00118     void timeout()
00119     {
00120       tooLate = true;
00121       q->setError( KJob::UserDefinedError );
00122       q->setErrorText( i18n( "Agent instance creation timed out." ) );
00123       q->emitResult();
00124     }
00125 
00126     void emitResult()
00127     {
00128       q->emitResult();
00129     }
00130 
00131     void doStart();
00132 
00133     AgentInstanceCreateJob* q;
00134     AgentType agentType;
00135     QString agentTypeId;
00136     AgentInstance agentInstance;
00137     QWidget* parentWidget;
00138     QTimer *safetyTimer;
00139     bool doConfig;
00140     bool tooLate;
00141 };
00142 
00143 }
00144 
00145 AgentInstanceCreateJob::AgentInstanceCreateJob( const AgentType &agentType, QObject *parent )
00146   : KJob( parent ),
00147     d( new AgentInstanceCreateJobPrivate( this ) )
00148 {
00149   d->agentType = agentType;
00150 }
00151 
00152 AgentInstanceCreateJob::AgentInstanceCreateJob( const QString &typeId, QObject *parent )
00153   : KJob( parent ),
00154     d( new AgentInstanceCreateJobPrivate( this ) )
00155 {
00156   d->agentTypeId = typeId;
00157 }
00158 
00159 AgentInstanceCreateJob::~ AgentInstanceCreateJob()
00160 {
00161   delete d;
00162 }
00163 
00164 void AgentInstanceCreateJob::configure( QWidget *parent )
00165 {
00166   d->parentWidget = parent;
00167   d->doConfig = true;
00168 }
00169 
00170 AgentInstance AgentInstanceCreateJob::instance() const
00171 {
00172   return d->agentInstance;
00173 }
00174 
00175 void AgentInstanceCreateJob::start()
00176 {
00177   d->start();
00178 }
00179 
00180 void AgentInstanceCreateJobPrivate::doStart()
00181 {
00182   if ( !agentType.isValid() && !agentTypeId.isEmpty() )
00183     agentType = AgentManager::self()->type( agentTypeId );
00184 
00185   if ( !agentType.isValid() ) {
00186     q->setError( KJob::UserDefinedError );
00187     q->setErrorText( i18n( "Unable to obtain agent type '%1'.", agentTypeId) );
00188     QTimer::singleShot( 0, q, SLOT( emitResult() ) );
00189     return;
00190   }
00191 
00192   agentInstance = AgentManager::self()->d->createInstance( agentType );
00193   if ( !agentInstance.isValid() ) {
00194     q->setError( KJob::UserDefinedError );
00195     q->setErrorText( i18n( "Unable to create agent instance." ) );
00196     QTimer::singleShot( 0, q, SLOT( emitResult() ) );
00197   } else {
00198     int timeout = safetyTimeout;
00199 #ifdef Q_OS_UNIX
00200     // Increate the timeout when valgrinding the agent, because that slows down things a log.
00201     QString agentValgrind = QString::fromLocal8Bit( qgetenv( "AKONADI_VALGRIND" ) );
00202     if ( !agentValgrind.isEmpty() && agentType.identifier().contains( agentValgrind ) )
00203       timeout *= 15;
00204 
00205     // change the timeout when debugging the agent, because we need time to start the debugger
00206     const QString agentDebugging = QString::fromLocal8Bit( qgetenv( "AKONADI_DEBUG_WAIT" ) );
00207     if ( !agentDebugging.isEmpty() ) {
00208       // we are debugging
00209       const QString agentDebuggingTimeout = QString::fromLocal8Bit( qgetenv( "AKONADI_DEBUG_TIMEOUT" ) );
00210       if ( agentDebuggingTimeout.isEmpty() ) {
00211         // use default value of 150 seconds (the same as "valgrinding", this has to be checked)
00212         timeout = 15* safetyTimeout;
00213       } else {
00214         // use own value
00215         timeout = agentDebuggingTimeout.toInt();
00216       }
00217     }
00218 #endif
00219     safetyTimer->start( timeout );
00220   }
00221 }
00222 
00223 #include "agentinstancecreatejob.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.5
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