libAccountSetup 1.0
provider-plugin-process.cpp
1#include <unistd.h>
2/*
3 * This file is part of accounts-ui
4 *
5 * Copyright (C) 2011 Nokia Corporation.
6 *
7 * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1 as published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24#include "provider-plugin-process-priv.h"
25
26#include <Accounts/Account>
27#include <Accounts/Manager>
28
29#include <QCoreApplication>
30#include <QDebug>
31#include <QFile>
32#include <QLocalSocket>
33#include <QVariant>
34
35using namespace AccountSetup;
36
37static ProviderPluginProcess *plugin_instance = 0;
38const int cancelId = -1;
39
40ProviderPluginProcessPrivate::ProviderPluginProcessPrivate(ProviderPluginProcess *parent):
41 q_ptr(parent),
42 setupType(Unset),
43 windowId(0),
44 goToAccountsPage(false),
45 exitData(),
46 editExistingAccount(false),
47 existingAccountId(0)
48{
49 account = 0;
50 manager = new Accounts::Manager(this);
51
52 /* parse command line options */
53 QStringList args = QCoreApplication::arguments();
54 for (int i = 0; i < args.length(); ++i)
55 {
56 Q_ASSERT(args[i] != NULL);
57
58 if (args[i] == QLatin1String("--create"))
59 {
60 setupType = CreateNew;
61
62 i++;
63 if (i < args.length()) {
64 account = manager->createAccount(args[i]);
65 }
66 }
67 else if (args[i] == QLatin1String("--edit"))
68 {
69 setupType = EditExisting;
70
71 i++;
72 if (i < args.length())
73 account = manager->account(args[i].toInt());
74 }
75 else if (args[i] == QLatin1String("--windowId"))
76 {
77 i++;
78 if (i < args.length())
79 windowId = (WId)args[i].toUInt();
80 Q_ASSERT(windowId != 0);
81 }
82 else if (args[i] == QLatin1String("--socketName"))
83 {
84 i++;
85 if (i < args.length())
86 socketName = args[i];
87 Q_ASSERT(socketName != 0);
88 }
89 else if (args[i] == QLatin1String("--serviceType"))
90 {
91 i++;
92 if (i < args.length())
93 serviceType = args[i];
94 Q_ASSERT(serviceType != 0);
95 }
96 }
97}
98
99ProviderPluginProcessPrivate::~ProviderPluginProcessPrivate()
100{
101}
102
103void ProviderPluginProcessPrivate::sendResultToCaller()
104{
105 if (!socketName.isEmpty()) {
106 QLocalSocket *socket = new QLocalSocket();
107 connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
108 this, SLOT(onSocketError(QLocalSocket::LocalSocketError)));
109 socket->connectToServer(socketName);
110
111 QByteArray ba;
112 QDataStream stream(&ba, QIODevice::WriteOnly);
113
114 if (editExistingAccount)
115 stream << existingAccountId;
116 else if (!goToAccountsPage)
117 stream << account->id();
118 else
119 stream << cancelId;
120
121 stream << exitData;
122 socket->write(ba);
123 socket->flush();
124 socket->close();
125 } else {
126 QByteArray ba;
127 if (editExistingAccount)
128 ba = QString::number(existingAccountId).toAscii();
129 else if (!goToAccountsPage)
130 ba = QString::number(account->id()).toAscii();
131 else
132 ba = QString::number(cancelId).toAscii();
133
134 QFile output;
135 output.open(STDOUT_FILENO, QIODevice::WriteOnly);
136 output.write(ba.constData());
137 output.close();
138 }
139}
140
141void ProviderPluginProcessPrivate::onSocketError(QLocalSocket::LocalSocketError status)
142{
143 qDebug() << Q_FUNC_INFO << status;
144}
145
147 QObject(object),
148 d_ptr(new ProviderPluginProcessPrivate(this))
149{
150 if (plugin_instance != 0)
151 qWarning() << "ProviderPluginProcess already instantiated";
152 plugin_instance = this;
153}
154
155ProviderPluginProcess::~ProviderPluginProcess()
156{
158 delete d;
159}
160
162{
163 return plugin_instance;
164}
165
167{
168 Q_D(const ProviderPluginProcess);
169 return d->setupType;
170}
171
172Accounts::Account *ProviderPluginProcess::account() const
173{
174 Q_D(const ProviderPluginProcess);
175 return d->account;
176}
177
179{
180 Q_D(const ProviderPluginProcess);
181 return d->serviceType;
182}
183
185{
186 Q_D(const ProviderPluginProcess);
187 return d->windowId;
188}
189
191{
193 /* goToAccountsPage is only true when plugin is stopped in between without
194 creating the account */
195 d->goToAccountsPage = value;
196 quit();
197
198}
199
200void ProviderPluginProcess::setExitData(const QVariant &data)
201{
203 d->exitData = data;
204}
205
206void ProviderPluginProcess::setEditExistingAccount(Accounts::AccountId accountId)
207{
209 d->editExistingAccount = true;
210 d->existingAccountId = accountId;
211}
212
214{
216 d->sendResultToCaller();
217 QCoreApplication::exit(0);
218}
219
Class to manage the account provider plugins as separate processes.
void quit()
Clean termination of the plugin process.
WId parentWindowId() const
Gets the platform specific identifier of the window to which this plugin UI should be modal.
Accounts::Account * account() const
Gets the account being setup by this plugin.
void setReturnToAccountsList(bool value)
Sets the variable to go to accountsPage, when plugin exits.
static ProviderPluginProcess * instance()
Get the instance of the object.
void setExitData(const QVariant &data)
sets the exit data.
void setEditExistingAccount(Accounts::AccountId accountId)
Informs accounts-ui that the already existing account which the plugin instance attempted to create,...
ProviderPluginProcess(QObject *object=0)
Constructs the account provider plugin process.
SetupType
Operation type.
Definition types.h:38