provider-plugin-process.cpp

00001 /*
00002  * This file is part of accounts-ui
00003  *
00004  * Copyright (C) 2011 Nokia Corporation.
00005  *
00006  * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * version 2.1 as published by the Free Software Foundation.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00020  * 02110-1301 USA
00021  */
00022 
00023 #include "provider-plugin-process-priv.h"
00024 
00025 #include <Accounts/Account>
00026 #include <Accounts/Manager>
00027 
00028 #include <QCoreApplication>
00029 #include <QDebug>
00030 #include <QFile>
00031 #include <QLocalSocket>
00032 #include <QVariant>
00033 
00034 using namespace AccountSetup;
00035 
00036 static ProviderPluginProcess *plugin_instance = 0;
00037 const int cancelId = -1;
00038 
00039 ProviderPluginProcessPrivate::ProviderPluginProcessPrivate(ProviderPluginProcess *parent):
00040     q_ptr(parent),
00041     setupType(Unset),
00042     windowId(0),
00043     goToAccountsPage(false),
00044     exitData()
00045 {
00046     account = 0;
00047     manager = new Accounts::Manager(this);
00048 
00049     /* parse command line options */
00050     QStringList args = QCoreApplication::arguments();
00051     for (int i = 0; i < args.length(); ++i)
00052     {
00053         Q_ASSERT(args[i] != NULL);
00054 
00055         if (args[i] == QLatin1String("--create"))
00056         {
00057             setupType = CreateNew;
00058 
00059             i++;
00060             if (i < args.length()) {
00061                 account = manager->createAccount(args[i]);
00062             }
00063         }
00064         else if (args[i] == QLatin1String("--edit"))
00065         {
00066             setupType = EditExisting;
00067 
00068             i++;
00069             if (i < args.length())
00070                 account = manager->account(args[i].toInt());
00071         }
00072         else if (args[i] == QLatin1String("--windowId"))
00073         {
00074             i++;
00075             if (i < args.length())
00076                 windowId = (WId)args[i].toUInt();
00077             Q_ASSERT(windowId != 0);
00078         }
00079         else if (args[i] == QLatin1String("--socketName"))
00080         {
00081             i++;
00082             if (i < args.length())
00083                 socketName = args[i];
00084             Q_ASSERT(socketName != 0);
00085         }
00086         else if (args[i] == QLatin1String("--serviceType"))
00087         {
00088             i++;
00089             if (i < args.length())
00090                 serviceType = args[i];
00091             Q_ASSERT(serviceType != 0);
00092         }
00093     }
00094 }
00095 
00096 ProviderPluginProcessPrivate::~ProviderPluginProcessPrivate()
00097 {
00098 }
00099 
00100 void ProviderPluginProcessPrivate::sendResultToCaller()
00101 {
00102     if (!socketName.isEmpty()) {
00103         QLocalSocket *socket = new QLocalSocket();
00104         connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
00105                 this, SLOT(onSocketError(QLocalSocket::LocalSocketError)));
00106         socket->connectToServer(socketName);
00107 
00108         QByteArray ba;
00109         QDataStream stream(&ba, QIODevice::WriteOnly);
00110         if (!goToAccountsPage)
00111             stream << account->id();
00112         else
00113             stream << cancelId;
00114         stream << exitData;
00115         socket->write(ba);
00116         socket->flush();
00117         socket->close();
00118     } else {
00119         QByteArray ba;
00120         if (!goToAccountsPage)
00121             ba = QString::number(account->id()).toAscii();
00122         else
00123             ba = QString::number(cancelId).toAscii();
00124         QFile output;
00125         output.open(STDOUT_FILENO, QIODevice::WriteOnly);
00126         output.write(ba.constData());
00127         output.close();
00128     }
00129 }
00130 
00131 void ProviderPluginProcessPrivate::onSocketError(QLocalSocket::LocalSocketError status)
00132 {
00133     qDebug() << Q_FUNC_INFO << status;
00134 }
00135 
00136 ProviderPluginProcess::ProviderPluginProcess(QObject *object):
00137     QObject(object),
00138     d_ptr(new ProviderPluginProcessPrivate(this))
00139 {
00140     if (plugin_instance != 0)
00141         qWarning() << "ProviderPluginProcess already instantiated";
00142     plugin_instance = this;
00143 }
00144 
00145 ProviderPluginProcess::~ProviderPluginProcess()
00146 {
00147     Q_D(ProviderPluginProcess);
00148     delete d;
00149 }
00150 
00151 ProviderPluginProcess *ProviderPluginProcess::instance()
00152 {
00153     return plugin_instance;
00154 }
00155 
00156 SetupType ProviderPluginProcess::setupType() const
00157 {
00158     Q_D(const ProviderPluginProcess);
00159     return d->setupType;
00160 }
00161 
00162 Accounts::Account *ProviderPluginProcess::account() const
00163 {
00164     Q_D(const ProviderPluginProcess);
00165     return d->account;
00166 }
00167 
00168 QString ProviderPluginProcess::serviceType() const
00169 {
00170     Q_D(const ProviderPluginProcess);
00171     return d->serviceType;
00172 }
00173 
00174 WId ProviderPluginProcess::parentWindowId() const
00175 {
00176     Q_D(const ProviderPluginProcess);
00177     return d->windowId;
00178 }
00179 
00180 void ProviderPluginProcess::setReturnToAccountsList(bool value)
00181 {
00182     Q_D(ProviderPluginProcess);
00183     /* goToAccountsPage is only true when plugin is stopped in between without
00184       creating the account */
00185     d->goToAccountsPage = value;
00186     quit();
00187 
00188 }
00189 
00190 void ProviderPluginProcess::setExitData(const QVariant &data)
00191 {
00192     Q_D(ProviderPluginProcess);
00193     d->exitData = data;
00194 }
00195 
00196 void ProviderPluginProcess::quit()
00197 {
00198     Q_D(ProviderPluginProcess);
00199     d->sendResultToCaller();
00200     QCoreApplication::exit(0);
00201 }
00202