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