2
* Copyright (C) 2016 Canonical Ltd
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.
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.
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/>.
16
* Authors: Michi Henning <michi.henning@canonical.com>
19
#include <unity/storage/qt/internal/AccountsJobImpl.h>
21
#include <unity/storage/qt/internal/AccountImpl.h>
22
#include <unity/storage/qt/internal/RuntimeImpl.h>
23
#include <unity/storage/qt/internal/StorageErrorImpl.h>
25
#include <OnlineAccounts/Account>
43
// TODO: We retrieve the accounts directly from online accounts until we have a working registry.
45
static map<QString, QString> const BUS_NAMES =
47
{ "google-drive-scope", "com.canonical.StorageFramework.Provider.ProviderTest" },
48
{ "com.canonical.scopes.mcloud_mcloud_mcloud", "com.canonical.StorageFramework.Provider.McloudProvider" },
49
{ "storage-provider-owncloud", "com.canonical.StorageFramework.Provider.OwnCloud" },
50
{ "storage-provider-onedrive", "com.canonical.StorageFramework.Provider.OnedriveProvider" },
55
AccountsJobImpl::AccountsJobImpl(AccountsJob* public_instance, shared_ptr<RuntimeImpl> const& runtime_impl)
56
: public_instance_(public_instance)
57
, status_(AccountsJob::Status::Loading)
58
, runtime_impl_(runtime_impl)
60
assert(public_instance);
63
initialize_accounts();
66
AccountsJobImpl::AccountsJobImpl(AccountsJob* public_instance, StorageError const& error)
67
: public_instance_(public_instance)
68
, status_(AccountsJob::Status::Loading)
71
assert(public_instance);
72
assert(error.type() != StorageError::Type::NoError);
74
status_ = emit_status_changed(AccountsJob::Status::Error);
77
bool AccountsJobImpl::isValid() const
79
return status_ != AccountsJob::Status::Error;
82
AccountsJob::Status AccountsJobImpl::status() const
87
StorageError AccountsJobImpl::error() const
92
QList<Account> AccountsJobImpl::accounts() const
94
auto runtime = get_runtime_impl("AccountsJob::accounts()");
97
return QList<Account>();
99
if (status_ != AccountsJob::Status::Finished)
101
return QList<Account>();
106
QVariantList AccountsJobImpl::accountsAsVariantList() const
108
QVariantList account_list;
109
for (auto const& a : accounts())
111
account_list.append(QVariant::fromValue(a));
116
void AccountsJobImpl::manager_ready()
120
initialize_accounts();
124
void AccountsJobImpl::timeout()
127
error_ = StorageErrorImpl::local_comms_error("AccountsJob(): timeout retrieving Online accounts");
128
status_ = emit_status_changed(AccountsJob::Status::Error);
132
AccountsJob::Status AccountsJobImpl::emit_status_changed(AccountsJob::Status new_status) const
134
if (status_ == AccountsJob::Status::Loading) // Once in a final state, we don't emit the signal again.
136
// We defer emission of the signal so the client gets a chance to connect to the signal
137
// in case we emit the signal from the constructor.
138
QMetaObject::invokeMethod(public_instance_,
140
Qt::QueuedConnection,
141
Q_ARG(unity::storage::qt::AccountsJob::Status, new_status));
146
shared_ptr<RuntimeImpl> AccountsJobImpl::get_runtime_impl(QString const& method) const
148
auto runtime = runtime_impl_.lock();
149
if (!runtime || !runtime->isValid())
151
QString msg = method + ": Runtime was destroyed previously";
152
auto This = const_cast<AccountsJobImpl*>(this);
153
This->error_ = StorageErrorImpl::runtime_destroyed_error(msg);
154
This->status_ = emit_status_changed(AccountsJob::Status::Error);
159
void AccountsJobImpl::initialize_accounts()
161
auto runtime = get_runtime_impl("AccountsJob()");
164
auto manager = runtime->accounts_manager();
165
if (!manager->isReady())
167
connect(manager.get(), &OnlineAccounts::Manager::ready, this, &AccountsJobImpl::manager_ready);
168
connect(&timer_, &QTimer::timeout, this, &AccountsJobImpl::timeout);
169
timer_.setSingleShot(true);
170
timer_.start(30000); // TODO: Need config for this eventually.
174
for (auto const map_entry : BUS_NAMES)
176
auto service_id = map_entry.first;
177
for (auto const& a : manager->availableAccounts(service_id))
179
auto object_path = QStringLiteral("/provider/%1").arg(a->id());
180
auto bus_name = map_entry.second;
181
accounts_.append(AccountImpl::make_account(runtime,
184
QString::number(a->id()),
189
status_ = emit_status_changed(AccountsJob::Status::Finished);
192
} // namespace internal
194
} // namespace storage