~unity-api-team/storage-framework/vivid

« back to all changes in this revision

Viewing changes to src/qt/client/internal/remote_client/RuntimeImpl.cpp

  • Committer: Bileto Bot
  • Author(s): James Henstridge
  • Date: 2016-08-04 07:19:51 UTC
  • mfrom: (8.25.12 merge-devel)
  • Revision ID: ci-train-bot@canonical.com-20160804071951-3mfrcheyjvif6o99
Initial release of storage framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors: Michi Henning <michi.henning@canonical.com>
 
17
 */
 
18
 
 
19
#include <unity/storage/qt/client/internal/remote_client/RuntimeImpl.h>
 
20
 
 
21
#include <unity/storage/qt/client/Account.h>
 
22
#include <unity/storage/qt/client/Exceptions.h>
 
23
#include <unity/storage/qt/client/internal/make_future.h>
 
24
#include <unity/storage/qt/client/internal/remote_client/AccountImpl.h>
 
25
#include <unity/storage/qt/client/internal/remote_client/dbusmarshal.h>
 
26
 
 
27
#include <QDBusMetaType>
 
28
 
 
29
// TODO: Hack until we can use the registry instead
 
30
#include <OnlineAccounts/Account>
 
31
 
 
32
#pragma GCC diagnostic push
 
33
#pragma GCC diagnostic ignored "-Wold-style-cast"
 
34
#include <glib.h>
 
35
#pragma GCC diagnostic pop
 
36
 
 
37
#include <cassert>
 
38
#include <cstdlib>
 
39
 
 
40
#include <unistd.h>
 
41
 
 
42
using namespace std;
 
43
 
 
44
namespace unity
 
45
{
 
46
namespace storage
 
47
{
 
48
namespace qt
 
49
{
 
50
namespace client
 
51
{
 
52
namespace internal
 
53
{
 
54
namespace remote_client
 
55
{
 
56
 
 
57
RuntimeImpl::RuntimeImpl(QDBusConnection const& bus)
 
58
    : conn_(bus)
 
59
{
 
60
    if (!conn_.isConnected())
 
61
    {
 
62
        throw LocalCommsException("Runtime: cannot connect to session bus");  // LCOV_EXCL_LINE
 
63
    }
 
64
    qDBusRegisterMetaType<unity::storage::internal::ItemMetadata>();
 
65
    qDBusRegisterMetaType<QList<unity::storage::internal::ItemMetadata>>();
 
66
}
 
67
 
 
68
RuntimeImpl::~RuntimeImpl()
 
69
{
 
70
    try
 
71
    {
 
72
        shutdown();
 
73
    }
 
74
    // LCOV_EXCL_START
 
75
    catch (std::exception const&)
 
76
    {
 
77
        qCritical() << "shutdown error";  // TODO, log the error properly
 
78
    }
 
79
    // LCOV_EXCL_STOP
 
80
}
 
81
 
 
82
void RuntimeImpl::shutdown()
 
83
{
 
84
    if (destroyed_.exchange(true))
 
85
    {
 
86
        return;
 
87
    }
 
88
    conn_.disconnectFromBus(conn_.name());
 
89
}
 
90
 
 
91
QFuture<QVector<Account::SPtr>> RuntimeImpl::accounts()
 
92
{
 
93
    if (!manager_)
 
94
    {
 
95
        manager_.reset(new OnlineAccounts::Manager("", conn_));
 
96
        connect(manager_.get(), &OnlineAccounts::Manager::ready, this, &RuntimeImpl::manager_ready);
 
97
        connect(&timer_, &QTimer::timeout, this, &RuntimeImpl::timeout);
 
98
        timer_.setSingleShot(true);
 
99
        timer_.start(5000);
 
100
    }
 
101
 
 
102
    qf_.reportStarted();
 
103
    return qf_.future();
 
104
}
 
105
 
 
106
QDBusConnection& RuntimeImpl::connection()
 
107
{
 
108
    return conn_;
 
109
}
 
110
 
 
111
void RuntimeImpl::manager_ready()
 
112
{
 
113
    timer_.stop();
 
114
    try
 
115
    {
 
116
        QVector<Account::SPtr> accounts;
 
117
        for (auto const& a : manager_->availableAccounts("google-drive-scope"))
 
118
        {
 
119
            qDebug() << "got account:" << a->displayName() << a->serviceId() << a->id();
 
120
            auto impl = new AccountImpl(public_instance_, a->id(), "", a->serviceId(), a->displayName());
 
121
            Account::SPtr acc(new Account(impl));
 
122
            impl->set_public_instance(acc);
 
123
            accounts.append(acc);
 
124
        }
 
125
        accounts_ = accounts;
 
126
        make_ready_future(qf_, accounts);
 
127
    }
 
128
    catch (StorageException const& e)
 
129
    {
 
130
        make_exceptional_future(qf_, e);
 
131
    }
 
132
}
 
133
 
 
134
void RuntimeImpl::timeout()
 
135
{
 
136
    make_exceptional_future(qf_, ResourceException("timeout error"));  // TODO
 
137
}
 
138
 
 
139
}  // namespace local_client
 
140
}  // namespace internal
 
141
}  // namespace client
 
142
}  // namespace qt
 
143
}  // namespace storage
 
144
}  // namespace unity