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

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
akonadiresourcecreator.cpp
Go to the documentation of this file.
1 /*
2  * akonadiresourcecreator.cpp - interactively create an Akonadi resource
3  * Program: kalarm
4  * Copyright © 2011 by David Jarvie <djarvie@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "akonadiresourcecreator.h"
22 #include "autoqpointer.h"
23 #include "kalarmsettings.h"
24 #include "kalarmdirsettings.h"
25 #include "controlinterface.h"
26 
27 #include <akonadi/agentfilterproxymodel.h>
28 #include <akonadi/agentinstancecreatejob.h>
29 #include <akonadi/agentmanager.h>
30 #include <akonadi/agenttypedialog.h>
31 #include <akonadi/dbusconnectionpool.h>
32 
33 #include <kmessagebox.h>
34 #include <klocale.h>
35 #include <kdebug.h>
36 
37 #include <QTimer>
38 
39 using namespace Akonadi;
40 using namespace KAlarmCal;
41 
42 
43 AkonadiResourceCreator::AkonadiResourceCreator(CalEvent::Type defaultType, QWidget* parent)
44  : QObject(),
45  mParent(parent),
46  mDefaultType(defaultType)
47 {
48 }
49 
50 /******************************************************************************
51 * Create a new resource. The user will be prompted to enter its configuration.
52 */
53 void AkonadiResourceCreator::createResource()
54 {
55  QTimer::singleShot(0, this, SLOT(getAgentType()));
56 }
57 
58 void AkonadiResourceCreator::getAgentType()
59 {
60  kDebug() << "Type:" << mDefaultType;
61  // Use AutoQPointer to guard against crash on application exit while
62  // the dialogue is still open. It prevents double deletion (both on
63  // deletion of parent, and on return from this function).
64  AutoQPointer<AgentTypeDialog> dlg = new AgentTypeDialog(mParent);
65  QString mimeType;
66  switch (mDefaultType)
67  {
68  case CalEvent::ACTIVE:
69  mimeType = KAlarmCal::MIME_ACTIVE;
70  break;
71  case CalEvent::ARCHIVED:
72  mimeType = KAlarmCal::MIME_ARCHIVED;
73  break;
74  case CalEvent::TEMPLATE:
75  mimeType = KAlarmCal::MIME_TEMPLATE;
76  break;
77  default:
78  emit finished(this, false);
79  return;
80  }
81  dlg->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
82  dlg->agentFilterProxyModel()->addCapabilityFilter(QLatin1String("Resource"));
83  if (dlg->exec() != QDialog::Accepted)
84  {
85  emit finished(this, false);
86  return;
87  }
88  mAgentType = dlg->agentType();
89  if (!mAgentType.isValid())
90  {
91  emit finished(this, false);
92  return;
93  }
94  AgentInstanceCreateJob* job = new AgentInstanceCreateJob(mAgentType, mParent);
95  connect(job, SIGNAL(result(KJob*)), SLOT(agentInstanceCreated(KJob*)));
96  job->start();
97 }
98 
99 /******************************************************************************
100 * Called when an agent creation job has completed.
101 * Checks for any error.
102 */
103 void AkonadiResourceCreator::agentInstanceCreated(KJob* j)
104 {
105  AgentInstanceCreateJob* job = static_cast<AgentInstanceCreateJob*>(j);
106  if (j->error())
107  {
108  kError() << "Failed to create new calendar resource:" << j->errorString();
109  KMessageBox::error(0, i18nc("@info", "%1<nl/>(%2)", i18nc("@info/plain", "Failed to create new calendar resource"), j->errorString()));
110  exitWithError();
111  }
112  else
113  {
114  // Set the default alarm type for a directory resource config dialog
115  mAgentInstance = job->instance();
116  QString type = mAgentInstance.type().identifier();
117  if (type == QLatin1String("akonadi_kalarm_dir_resource"))
118  setResourceAlarmType<OrgKdeAkonadiKAlarmDirSettingsInterface>();
119  else if (type == QLatin1String("akonadi_kalarm_resource"))
120  setResourceAlarmType<OrgKdeAkonadiKAlarmSettingsInterface>();
121 
122  // Display the resource config dialog, but first ensure we get
123  // notified of the user cancelling the operation.
124  org::freedesktop::Akonadi::Agent::Control* agentControlIface =
125  new org::freedesktop::Akonadi::Agent::Control(QLatin1String("org.freedesktop.Akonadi.Agent.") + mAgentInstance.identifier(),
126  QLatin1String("/"), DBusConnectionPool::threadConnection(), this);
127  bool controlOk = agentControlIface && agentControlIface->isValid();
128  if (!controlOk)
129  {
130  delete agentControlIface;
131  kWarning() << "Unable to access D-Bus interface of created agent.";
132  }
133  else
134  {
135  connect(agentControlIface, SIGNAL(configurationDialogAccepted()), SLOT(configurationDialogAccepted()));
136  connect(agentControlIface, SIGNAL(configurationDialogRejected()), SLOT(exitWithError()));
137  }
138  mAgentInstance.configure(mParent);
139 
140  if (!controlOk)
141  emit finished(this, true); // don't actually know the result in this case
142  }
143 }
144 
145 /******************************************************************************
146 * Set the alarm type for an Akonadi resource.
147 */
148 template <class Settings>
149 void AkonadiResourceCreator::setResourceAlarmType()
150 {
151  Settings iface(QLatin1String("org.freedesktop.Akonadi.Resource.") + mAgentInstance.identifier(),
152  QLatin1String("/Settings"), QDBusConnection::sessionBus(), this);
153  if (!iface.isValid())
154  kError() << "Error creating D-Bus interface for" << mAgentInstance.identifier() << "resource configuration.";
155  else
156  {
157  iface.setAlarmTypes(CalEvent::mimeTypes(mDefaultType));
158  iface.writeConfig();
159  mAgentInstance.reconfigure(); // notify the agent that its configuration has changed
160  }
161 }
162 
163 /******************************************************************************
164 * Called when the user has clicked OK in the resource configuration dialog.
165 */
166 void AkonadiResourceCreator::configurationDialogAccepted()
167 {
168  emit finished(this, true);
169 }
170 
171 /******************************************************************************
172 * Called when the user has clicked cancel in the resource configuration dialog.
173 * Remove the newly created agent instance.
174 */
175 void AkonadiResourceCreator::exitWithError()
176 {
177  AgentManager::self()->removeInstance(mAgentInstance);
178  emit finished(this, false);
179 }
180 
181 // vim: et sw=4:
QWidget
QDBusConnection::sessionBus
QDBusConnection sessionBus()
AkonadiResourceCreator::AkonadiResourceCreator
AkonadiResourceCreator(CalEvent::Type defaultType, QWidget *parent)
Definition: akonadiresourcecreator.cpp:43
autoqpointer.h
QObject
QString
AutoQPointer
QLatin1String
akonadiresourcecreator.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
AkonadiResourceCreator::finished
void finished(AkonadiResourceCreator *, bool success)
KJob
AkonadiResourceCreator::createResource
void createResource()
Definition: akonadiresourcecreator.cpp:53
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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