SVN_SILENT made messages (.desktop file)
[kdepim.git] / kalarm / akonadiresourcecreator.cpp
blob3d480f26750f64b56e7a040b7bd612767db556c9
1 /*
2 * akonadiresourcecreator.cpp - interactively create an Akonadi resource
3 * Program: kalarm
4 * Copyright © 2011 by David Jarvie <djarvie@kde.org>
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.
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.
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.
21 #include "akonadiresourcecreator.h"
22 #include "autoqpointer.h"
23 #include "kalarmsettings.h"
24 #include "kalarmdirsettings.h"
25 #include "controlinterface.h"
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>
33 #include <kmessagebox.h>
34 #include <klocale.h>
35 #include <kdebug.h>
37 #include <QTimer>
39 using namespace Akonadi;
40 using namespace KAlarmCal;
43 AkonadiResourceCreator::AkonadiResourceCreator(CalEvent::Type defaultType, QWidget* parent)
44 : QObject(),
45 mParent(parent),
46 mDefaultType(defaultType)
50 /******************************************************************************
51 * Create a new resource. The user will be prompted to enter its configuration.
53 void AkonadiResourceCreator::createResource()
55 QTimer::singleShot(0, this, SLOT(getAgentType()));
58 void AkonadiResourceCreator::getAgentType()
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)
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;
81 dlg->agentFilterProxyModel()->addMimeTypeFilter(mimeType);
82 dlg->agentFilterProxyModel()->addCapabilityFilter(QLatin1String("Resource"));
83 if (dlg->exec() != QDialog::Accepted)
85 emit finished(this, false);
86 return;
88 mAgentType = dlg->agentType();
89 if (!mAgentType.isValid())
91 emit finished(this, false);
92 return;
94 AgentInstanceCreateJob* job = new AgentInstanceCreateJob(mAgentType, mParent);
95 connect(job, SIGNAL(result(KJob*)), SLOT(agentInstanceCreated(KJob*)));
96 job->start();
99 /******************************************************************************
100 * Called when an agent creation job has completed.
101 * Checks for any error.
103 void AkonadiResourceCreator::agentInstanceCreated(KJob* j)
105 AgentInstanceCreateJob* job = static_cast<AgentInstanceCreateJob*>(j);
106 if (j->error())
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();
112 else
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>();
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)
130 delete agentControlIface;
131 kWarning() << "Unable to access D-Bus interface of created agent.";
133 else
135 connect(agentControlIface, SIGNAL(configurationDialogAccepted()), SLOT(configurationDialogAccepted()));
136 connect(agentControlIface, SIGNAL(configurationDialogRejected()), SLOT(exitWithError()));
138 mAgentInstance.configure(mParent);
140 if (!controlOk)
141 emit finished(this, true); // don't actually know the result in this case
145 /******************************************************************************
146 * Set the alarm type for an Akonadi resource.
148 template <class Settings>
149 void AkonadiResourceCreator::setResourceAlarmType()
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
157 iface.setAlarmTypes(CalEvent::mimeTypes(mDefaultType));
158 iface.writeConfig();
159 mAgentInstance.reconfigure(); // notify the agent that its configuration has changed
163 /******************************************************************************
164 * Called when the user has clicked OK in the resource configuration dialog.
166 void AkonadiResourceCreator::configurationDialogAccepted()
168 emit finished(this, true);
171 /******************************************************************************
172 * Called when the user has clicked cancel in the resource configuration dialog.
173 * Remove the newly created agent instance.
175 void AkonadiResourceCreator::exitWithError()
177 AgentManager::self()->removeInstance(mAgentInstance);
178 emit finished(this, false);
181 // vim: et sw=4: