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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
agentinstancecreatejob.cpp
1 /*
2  Copyright (c) 2008 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "agentinstancecreatejob.h"
21 
22 #include "agentinstance.h"
23 #include "agentmanager.h"
24 #include "agentmanager_p.h"
25 #include "controlinterface.h"
26 #include "dbusconnectionpool.h"
27 #include "kjobprivatebase_p.h"
28 #include "servermanager.h"
29 
30 #include <kdebug.h>
31 #include <klocalizedstring.h>
32 
33 #include <QtCore/QTimer>
34 
35 #ifdef Q_OS_UNIX
36 #include <sys/types.h>
37 #include <signal.h>
38 #endif
39 
40 using namespace Akonadi;
41 
42 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_WINCE)
43 static const int safetyTimeout = 60000; // ms
44 #else
45 static const int safetyTimeout = 10000; // ms
46 #endif
47 
48 namespace Akonadi {
52 class AgentInstanceCreateJobPrivate : public KJobPrivateBase
53 {
54  public:
55  AgentInstanceCreateJobPrivate( AgentInstanceCreateJob* parent ) : q( parent ),
56  parentWidget( 0 ),
57  safetyTimer( new QTimer( parent ) ),
58  doConfig( false ),
59  tooLate( false )
60  {
61  QObject::connect( AgentManager::self(), SIGNAL(instanceAdded(Akonadi::AgentInstance)),
62  q, SLOT(agentInstanceAdded(Akonadi::AgentInstance)) );
63  QObject::connect( safetyTimer, SIGNAL(timeout()), q, SLOT(timeout()) );
64  }
65 
66  void agentInstanceAdded( const AgentInstance &instance )
67  {
68  if ( agentInstance == instance && !tooLate ) {
69  safetyTimer->stop();
70  if ( doConfig ) {
71  // return from dbus call first before doing the next one
72  QTimer::singleShot( 0, q, SLOT(doConfigure()) );
73  } else {
74  q->emitResult();
75  }
76  }
77  }
78 
79  void doConfigure()
80  {
81  org::freedesktop::Akonadi::Agent::Control *agentControlIface =
82  new org::freedesktop::Akonadi::Agent::Control( ServerManager::agentServiceName( ServerManager::Agent, agentInstance.identifier() ),
83  QLatin1String( "/" ), DBusConnectionPool::threadConnection(), q );
84  if ( !agentControlIface || !agentControlIface->isValid() ) {
85  delete agentControlIface;
86 
87  q->setError( KJob::UserDefinedError );
88  q->setErrorText( i18n( "Unable to access D-Bus interface of created agent." ) );
89  q->emitResult();
90  return;
91  }
92 
93  q->connect( agentControlIface, SIGNAL(configurationDialogAccepted()),
94  q, SLOT(configurationDialogAccepted()) );
95  q->connect( agentControlIface, SIGNAL(configurationDialogRejected()),
96  q, SLOT(configurationDialogRejected()) );
97 
98  agentInstance.configure( parentWidget );
99  }
100 
101  void configurationDialogAccepted()
102  {
103  // The user clicked 'Ok' in the initial configuration dialog, so we assume
104  // he wants to keep the resource and the job is done.
105  q->emitResult();
106  }
107 
108  void configurationDialogRejected()
109  {
110  // The user clicked 'Cancel' in the initial configuration dialog, so we assume
111  // he wants to abort the 'create new resource' job and the new resource will be
112  // removed again.
113  AgentManager::self()->removeInstance( agentInstance );
114 
115  q->emitResult();
116  }
117 
118  void timeout()
119  {
120  tooLate = true;
121  q->setError( KJob::UserDefinedError );
122  q->setErrorText( i18n( "Agent instance creation timed out." ) );
123  q->emitResult();
124  }
125 
126  void emitResult()
127  {
128  q->emitResult();
129  }
130 
131  void doStart();
132 
133  AgentInstanceCreateJob* q;
134  AgentType agentType;
135  QString agentTypeId;
136  AgentInstance agentInstance;
137  QWidget* parentWidget;
138  QTimer *safetyTimer;
139  bool doConfig;
140  bool tooLate;
141 };
142 
143 }
144 
145 AgentInstanceCreateJob::AgentInstanceCreateJob( const AgentType &agentType, QObject *parent )
146  : KJob( parent ),
147  d( new AgentInstanceCreateJobPrivate( this ) )
148 {
149  d->agentType = agentType;
150 }
151 
152 AgentInstanceCreateJob::AgentInstanceCreateJob( const QString &typeId, QObject *parent )
153  : KJob( parent ),
154  d( new AgentInstanceCreateJobPrivate( this ) )
155 {
156  d->agentTypeId = typeId;
157 }
158 
159 AgentInstanceCreateJob::~ AgentInstanceCreateJob()
160 {
161  delete d;
162 }
163 
164 void AgentInstanceCreateJob::configure( QWidget *parent )
165 {
166  d->parentWidget = parent;
167  d->doConfig = true;
168 }
169 
170 AgentInstance AgentInstanceCreateJob::instance() const
171 {
172  return d->agentInstance;
173 }
174 
175 void AgentInstanceCreateJob::start()
176 {
177  d->start();
178 }
179 
180 void AgentInstanceCreateJobPrivate::doStart()
181 {
182  if ( !agentType.isValid() && !agentTypeId.isEmpty() ) {
183  agentType = AgentManager::self()->type( agentTypeId );
184  }
185 
186  if ( !agentType.isValid() ) {
187  q->setError( KJob::UserDefinedError );
188  q->setErrorText( i18n( "Unable to obtain agent type '%1'.", agentTypeId ) );
189  QTimer::singleShot( 0, q, SLOT(emitResult()) );
190  return;
191  }
192 
193  agentInstance = AgentManager::self()->d->createInstance( agentType );
194  if ( !agentInstance.isValid() ) {
195  q->setError( KJob::UserDefinedError );
196  q->setErrorText( i18n( "Unable to create agent instance." ) );
197  QTimer::singleShot( 0, q, SLOT(emitResult()) );
198  } else {
199  int timeout = safetyTimeout;
200 #ifdef Q_OS_UNIX
201  // Increate the timeout when valgrinding the agent, because that slows down things a log.
202  QString agentValgrind = QString::fromLocal8Bit( qgetenv( "AKONADI_VALGRIND" ) );
203  if ( !agentValgrind.isEmpty() && agentType.identifier().contains( agentValgrind ) ) {
204  timeout *= 15;
205  }
206 
207  // change the timeout when debugging the agent, because we need time to start the debugger
208  const QString agentDebugging = QString::fromLocal8Bit( qgetenv( "AKONADI_DEBUG_WAIT" ) );
209  if ( !agentDebugging.isEmpty() ) {
210  // we are debugging
211  const QString agentDebuggingTimeout = QString::fromLocal8Bit( qgetenv( "AKONADI_DEBUG_TIMEOUT" ) );
212  if ( agentDebuggingTimeout.isEmpty() ) {
213  // use default value of 150 seconds (the same as "valgrinding", this has to be checked)
214  timeout = 15* safetyTimeout;
215  } else {
216  // use own value
217  timeout = agentDebuggingTimeout.toInt();
218  }
219  }
220 #endif
221  safetyTimer->start( timeout );
222  }
223 }
224 
225 #include "moc_agentinstancecreatejob.cpp"
Akonadi::AgentInstanceCreateJob::configure
void configure(QWidget *parent=0)
Setup the job to show agent configuration dialog once the agent instance has been successfully starte...
Definition: agentinstancecreatejob.cpp:164
Akonadi::AgentManager::removeInstance
void removeInstance(const AgentInstance &instance)
Removes the given agent instance.
Definition: agentmanager.cpp:409
Akonadi::AgentType
A representation of an agent type.
Definition: agenttype.h:58
Akonadi::ServerManager::agentServiceName
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
Returns the namespaced D-Bus service name for an agent of type agentType with agent identifier identi...
Definition: servermanager.cpp:310
Akonadi::AgentManager::type
AgentType type(const QString &identifier) const
Returns the agent type with the given identifier or an invalid agent type if the identifier does not ...
Definition: agentmanager.cpp:394
Akonadi::AgentInstanceCreateJob::~AgentInstanceCreateJob
~AgentInstanceCreateJob()
Destroys the agent instance create job.
Definition: agentinstancecreatejob.cpp:159
Akonadi::AgentInstanceCreateJob
Job for creating new agent instances.
Definition: agentinstancecreatejob.h:71
Akonadi::AgentInstanceCreateJob::AgentInstanceCreateJob
AgentInstanceCreateJob(const AgentType &type, QObject *parent=0)
Creates a new agent instance create job.
Definition: agentinstancecreatejob.cpp:145
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:380
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
Akonadi::KJobPrivateBase
Base class for the private class of KJob but not Akonadi::Job based jobs that require the Akonadi ser...
Definition: kjobprivatebase_p.h:35
Akonadi::AgentInstanceCreateJob::start
void start()
Starts the instance creation.
Definition: agentinstancecreatejob.cpp:175
Akonadi::AgentInstanceCreateJob::instance
AgentInstance instance() const
Returns the AgentInstance object of the newly created agent instance.
Definition: agentinstancecreatejob.cpp:170
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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