~phablet-team/sync-monitor/trunk

« back to all changes in this revision

Viewing changes to authenticator/main.cpp

  • Committer: Renato Araujo Oliveira Filho
  • Date: 2015-02-10 21:09:59 UTC
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: renato.filho@canonical.com-20150210210959-eakta4pkpo3azj2s
Created sync-monitor-helper to re-authenticate accounts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include <QUrlQuery>
 
18
#include <QGuiApplication>
 
19
#include <QQuickView>
 
20
#include <QQmlEngine>
 
21
#include <QQmlContext>
 
22
#include <QDebug>
 
23
 
 
24
QVariantMap parseAccountArg(QStringList args)
 
25
{
 
26
    //syncmonitor:///authenticate?id=%1&service=%2
 
27
    Q_FOREACH(const QString &arg, args) {
 
28
        if (arg.startsWith("syncmonitor:///")) {
 
29
            QUrl url = QUrl::fromPercentEncoding(arg.toUtf8());
 
30
            QString methodName = url.path().right(url.path().length() -1);
 
31
            if (methodName != "authenticate") {
 
32
                return QVariantMap();
 
33
            }
 
34
 
 
35
            //convert items to map
 
36
            QUrlQuery query(url);
 
37
            QList<QPair<QString, QString> > queryItemsPair = query.queryItems();
 
38
            QMap<QString, QString> queryItems;
 
39
            for(int i=0; i < queryItemsPair.count(); i++) {
 
40
                QPair<QString, QString> item = queryItemsPair[i];
 
41
                queryItems.insert(item.first, item.second);
 
42
            }
 
43
 
 
44
            if (queryItems.contains("id") && queryItems.contains("service")) {
 
45
                QVariantMap info;
 
46
                info.insert("accountId", queryItems.value("id"));
 
47
                info.insert("serviceName", queryItems.value("service"));
 
48
                return info;
 
49
            } else {
 
50
                return QVariantMap();
 
51
            }
 
52
        }
 
53
    }
 
54
    return QVariantMap();
 
55
}
 
56
 
 
57
int main(int argc, char **argv)
 
58
{
 
59
    QCoreApplication::setOrganizationName("Canonical");
 
60
    QCoreApplication::setOrganizationDomain("canonical.com");
 
61
    QCoreApplication::setApplicationName("Sync Monitor Helper");
 
62
 
 
63
    QGuiApplication *app = new QGuiApplication(argc, argv);
 
64
    QVariantMap accountInfo = parseAccountArg(app->arguments());
 
65
    if (accountInfo.isEmpty()) {
 
66
        qWarning() << "Usage: sync-monitor-helper syncmonitor:///authenticate?id=<accountId>&service=<serviceName";
 
67
        delete app;
 
68
        return -1;
 
69
    }
 
70
 
 
71
    QQuickView *view = new QQuickView;
 
72
    app->connect(view->engine(), SIGNAL(quit()), SLOT(quit()));
 
73
 
 
74
    view->setResizeMode(QQuickView::SizeRootObjectToView);
 
75
    view->setTitle("Sync Monitor");
 
76
    view->rootContext()->setContextProperty("ONLINE_ACCOUNT", accountInfo);
 
77
    view->setSource(QUrl("qrc:/main.qml"));
 
78
 
 
79
    qDebug() << accountInfo;
 
80
    view->showMaximized();
 
81
    app->exec();
 
82
    delete view;
 
83
    delete app;
 
84
    return 0;
 
85
}