• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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 "kjobprivatebase_p.h"
00027 
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 
00031 #include <QtCore/QTimer>
00032 
00033 #ifdef Q_OS_UNIX
00034 #include <sys/types.h>
00035 #include <signal.h>
00036 #endif
00037 
00038 using namespace Akonadi;
00039 
00040 static const int safetyTimeout = 10000; // ms
00041 
00042 namespace Akonadi {
00046 class AgentInstanceCreateJobPrivate : public KJobPrivateBase
00047 {
00048   public:
00049     AgentInstanceCreateJobPrivate( AgentInstanceCreateJob* parent ) : q( parent ),
00050       parentWidget( 0 ),
00051       safetyTimer( new QTimer( parent ) ),
00052       doConfig( false ),
00053       tooLate( false )
00054     {
00055       QObject::connect( AgentManager::self(), SIGNAL( instanceAdded( const Akonadi::AgentInstance& ) ),
00056                         q, SLOT( agentInstanceAdded( const Akonadi::AgentInstance& ) ) );
00057       QObject::connect( safetyTimer, SIGNAL( timeout() ), q, SLOT( timeout() ) );
00058     }
00059 
00060     void agentInstanceAdded( const AgentInstance &instance )
00061     {
00062       if ( agentInstance == instance && !tooLate ) {
00063         safetyTimer->stop();
00064         if ( doConfig ) {
00065           // return from dbus call first before doing the next one
00066           QTimer::singleShot( 0, q, SLOT( doConfigure() ) );
00067         } else {
00068           q->emitResult();
00069         }
00070       }
00071     }
00072 
00073     void doConfigure()
00074     {
00075       org::freedesktop::Akonadi::Agent::Control *agentControlIface =
00076         new org::freedesktop::Akonadi::Agent::Control( QLatin1String( "org.freedesktop.Akonadi.Agent." ) + agentInstance.identifier(),
00077                                                        QLatin1String( "/" ), QDBusConnection::sessionBus(), q );
00078       if ( !agentControlIface || !agentControlIface->isValid() ) {
00079         if ( agentControlIface )
00080           delete agentControlIface;
00081 
00082         q->setError( KJob::UserDefinedError );
00083         q->setErrorText( i18n( "Unable to access D-Bus interface of created agent." ) );
00084         q->emitResult();
00085         return;
00086       }
00087 
00088       q->connect( agentControlIface, SIGNAL( configurationDialogAccepted() ),
00089                   q, SLOT( configurationDialogAccepted() ) );
00090       q->connect( agentControlIface, SIGNAL( configurationDialogRejected() ),
00091                   q, SLOT( configurationDialogRejected() ) );
00092 
00093       agentInstance.configure( parentWidget );
00094     }
00095 
00096     void configurationDialogAccepted()
00097     {
00098       // The user clicked 'Ok' in the initial configuration dialog, so we assume
00099       // he wants to keep the resource and the job is done.
00100       q->emitResult();
00101     }
00102 
00103     void configurationDialogRejected()
00104     {
00105       // The user clicked 'Cancel' in the initial configuration dialog, so we assume
00106       // he wants to abort the 'create new resource' job and the new resource will be
00107       // removed again.
00108       AgentManager::self()->removeInstance( agentInstance );
00109 
00110       q->emitResult();
00111     }
00112 
00113     void timeout()
00114     {
00115       tooLate = true;
00116       q->setError( KJob::UserDefinedError );
00117       q->setErrorText( i18n( "Agent instance creation timed out." ) );
00118       q->emitResult();
00119     }
00120 
00121     void emitResult()
00122     {
00123       q->emitResult();
00124     }
00125 
00126     void doStart();
00127 
00128     AgentInstanceCreateJob* q;
00129     AgentType agentType;
00130     QString agentTypeId;
00131     AgentInstance agentInstance;
00132     QWidget* parentWidget;
00133     QTimer *safetyTimer;
00134     bool doConfig;
00135     bool tooLate;
00136 };
00137 
00138 }
00139 
00140 AgentInstanceCreateJob::AgentInstanceCreateJob( const AgentType &agentType, QObject *parent )
00141   : KJob( parent ),
00142     d( new AgentInstanceCreateJobPrivate( this ) )
00143 {
00144   d->agentType = agentType;
00145 }
00146 
00147 AgentInstanceCreateJob::AgentInstanceCreateJob( const QString &typeId, QObject *parent )
00148   : KJob( parent ),
00149     d( new AgentInstanceCreateJobPrivate( this ) )
00150 {
00151   d->agentTypeId = typeId;
00152 }
00153 
00154 AgentInstanceCreateJob::~ AgentInstanceCreateJob()
00155 {
00156   delete d;
00157 }
00158 
00159 void AgentInstanceCreateJob::configure( QWidget *parent )
00160 {
00161   d->parentWidget = parent;
00162   d->doConfig = true;
00163 }
00164 
00165 AgentInstance AgentInstanceCreateJob::instance() const
00166 {
00167   return d->agentInstance;
00168 }
00169 
00170 void AgentInstanceCreateJob::start()
00171 {
00172   d->start();
00173 }
00174 
00175 void AgentInstanceCreateJobPrivate::doStart()
00176 {
00177   if ( !agentType.isValid() && !agentTypeId.isEmpty() )
00178     agentType = AgentManager::self()->type( agentTypeId );
00179 
00180   if ( !agentType.isValid() ) {
00181     q->setError( KJob::UserDefinedError );
00182     q->setErrorText( i18n( "Unable to obtain agent type '%1'.", agentTypeId) );
00183     QTimer::singleShot( 0, q, SLOT( emitResult() ) );
00184     return;
00185   }
00186 
00187   agentInstance = AgentManager::self()->d->createInstance( agentType );
00188   if ( !agentInstance.isValid() ) {
00189     q->setError( KJob::UserDefinedError );
00190     q->setErrorText( i18n( "Unable to create agent instance." ) );
00191     QTimer::singleShot( 0, q, SLOT( emitResult() ) );
00192   } else {
00193     int timeout = safetyTimeout;
00194 #ifdef Q_OS_UNIX
00195     // Increate the timeout when valgrinding the agent, because that slows down things a log.
00196     QString agentValgrind = QString::fromLocal8Bit( qgetenv( "AKONADI_VALGRIND" ) );
00197     if ( !agentValgrind.isEmpty() && agentType.identifier().contains( agentValgrind ) )
00198       timeout *= 15;
00199 #endif
00200     safetyTimer->start( timeout );
00201   }
00202 }
00203 
00204 #include "agentinstancecreatejob.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • 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.7.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