Commit 87452658 authored by Paweł Wegner's avatar Paweł Wegner

ICloudStorage: return initialized ICloudProviders.

parent 16aad40a
......@@ -31,8 +31,6 @@ namespace cloudstorage {
MockProvider::MockProvider() {}
void MockProvider::initialize(InitData&&) {}
std::string MockProvider::token() const { return ""; }
ICloudProvider::Hints MockProvider::hints() const { return Hints(); }
......
......@@ -136,7 +136,6 @@ class MockProvider : public ICloudProvider {
MockProvider();
void initialize(InitData&&) override;
std::string token() const override;
Hints hints() const override;
std::string name() const override;
......
......@@ -55,14 +55,11 @@ std::string read_file(const std::string& path) {
} // namespace
Window::Window(MediaPlayer* media_player)
: cloud_storage_(ICloudStorage::create()),
last_played_(-1),
media_player_(media_player) {
: last_played_(-1), media_player_(media_player) {
qRegisterMetaType<ItemPointer>();
QStringList clouds;
for (auto p : ICloudStorage::create()->providers())
clouds.append(p->name().c_str());
for (auto p : ICloudStorage::create()->providers()) clouds.append(p.c_str());
clouds.append("mock");
#ifdef WITH_QTWEBENGINE
rootContext()->setContextProperty("qtwebengine", QVariant(true));
......@@ -145,11 +142,6 @@ void Window::initializeCloud(QString name) {
if (cloud_provider_ && cloud_provider_->name() == name.toStdString()) return;
saveCloudAccessToken();
cancelRequests();
if (name != "mock")
cloud_provider_ = cloud_storage_->provider(name.toStdString());
else
cloud_provider_ = util::make_unique<cloudstorage::MockProvider>();
current_directory_ = cloud_provider_->rootDirectory();
if (initialized_clouds_.find(name.toStdString()) ==
std::end(initialized_clouds_)) {
QSettings settings;
......@@ -172,14 +164,21 @@ void Window::initializeCloud(QString name) {
hints["success_page"] = read_file(":/resources/default_success.html");
}
hints["error_page"] = read_file(":/resources/default_error.html");
cloud_provider_->initialize({settings.value(name).toString().toStdString(),
util::make_unique<CloudProviderCallback>(this),
nullptr, nullptr, nullptr, hints});
initialized_clouds_.insert(name.toStdString());
if (name != "mock")
cloud_provider_ = ICloudStorage::create()->provider(
name.toStdString(),
{settings.value(name).toString().toStdString(),
util::make_unique<CloudProviderCallback>(this), nullptr, nullptr,
nullptr, hints});
else
cloud_provider_ = util::make_unique<cloudstorage::MockProvider>();
initialized_clouds_[name.toStdString()] = cloud_provider_;
} else if (unauthorized_clouds_.find(name.toStdString()) !=
std::end(unauthorized_clouds_)) {
emit openBrowser(cloud_provider_->authorizeLibraryUrl().c_str());
}
if (cloud_provider_) current_directory_ = cloud_provider_->rootDirectory();
emit runListDirectory();
}
......
......@@ -171,7 +171,6 @@ class Window : public QQuickView {
ICloudProvider::Hints fromJson(const QJsonObject&) const;
QJsonObject toJson(const ICloudProvider::Hints&) const;
ICloudStorage::Pointer cloud_storage_;
ICloudProvider::Pointer cloud_provider_;
IItem::Pointer current_directory_;
std::vector<cloudstorage::IItem::Pointer> directory_stack_;
......@@ -190,7 +189,7 @@ class Window : public QQuickView {
int last_played_;
MediaPlayer* media_player_;
QString current_media_;
std::unordered_set<std::string> initialized_clouds_;
std::unordered_map<std::string, ICloudProvider::Pointer> initialized_clouds_;
std::unordered_set<std::string> unauthorized_clouds_;
Q_OBJECT
......
......@@ -66,7 +66,7 @@ int main(int, char**) {
if (current_directory == nullptr) {
std::cout << "Select a provider: \n";
for (auto p : ICloudStorage::create()->providers())
std::cout << p->name() << "\n";
std::cout << p << "\n";
} else {
auto lst = current_provider->listDirectoryAsync(current_directory)
->result()
......@@ -80,24 +80,24 @@ int main(int, char**) {
if (current_provider == nullptr) {
std::string provider_name;
line >> provider_name;
auto provider = ICloudStorage::create()->provider(provider_name);
if (!provider)
std::cout << "No provider with name " << provider_name << "\n";
else {
std::string filename = provider_name + ".txt";
std::string token;
std::fstream(filename, std::fstream::in) >> token;
provider->initialize(
{token,
std::unique_ptr<Callback>(new Callback(filename)),
nullptr,
nullptr,
nullptr,
{}});
std::string filename = provider_name + ".txt";
std::string token;
std::fstream(filename, std::fstream::in) >> token;
auto provider = ICloudStorage::create()->provider(
provider_name,
{token,
std::unique_ptr<Callback>(new Callback(filename)),
nullptr,
nullptr,
nullptr,
{}});
if (provider) {
prompt += provider_name + "/";
current_provider = provider;
current_directory = provider->rootDirectory();
directory_stack.push_back(current_directory);
} else {
std::cout << "Provider " << provider_name << " unavailable.\n";
}
} else {
std::string destination;
......
......@@ -41,7 +41,7 @@ class CloudProvider : public ICloudProvider,
CloudProvider(IAuth::Pointer);
void initialize(InitData&&) override;
virtual void initialize(InitData&&);
Hints hints() const override;
std::string access_token() const;
......
......@@ -135,14 +135,6 @@ class ICloudProvider {
virtual ~ICloudProvider() = default;
/**
* Initializes the cloud provider, doesn't do any authorization just yet. The
* actual authorization will be done the first time it's actually needed.
*
* @param init_data initialization data
*/
virtual void initialize(InitData&& init_data) = 0;
/**
* Serializes token and hints in a json compact that are useful to be
* restored in the following session. They might include refresh token, access
......
......@@ -44,7 +44,7 @@ class ICloudStorage {
*
* @return cloud providers
*/
virtual std::vector<ICloudProvider::Pointer> providers() const = 0;
virtual std::vector<std::string> providers() const = 0;
/**
* Gets provider by name.
......@@ -52,7 +52,8 @@ class ICloudStorage {
* @param name
* @return cloud provider
*/
virtual ICloudProvider::Pointer provider(const std::string& name) const = 0;
virtual ICloudProvider::Pointer provider(
const std::string& name, ICloudProvider::InitData&&) const = 0;
static ICloudStorage::Pointer create();
};
......
......@@ -41,29 +41,40 @@
namespace cloudstorage {
CloudStorage::CloudStorage()
: providers_({std::make_shared<GoogleDrive>(), std::make_shared<OneDrive>(),
std::make_shared<Dropbox>(), std::make_shared<AmazonDrive>(),
std::make_shared<Box>(), std::make_shared<YouTube>(),
std::make_shared<YandexDisk>(), std::make_shared<OwnCloud>(),
std::make_shared<AmazonS3>()}) {
CloudStorage::CloudStorage() {
add([]() { return std::make_shared<GoogleDrive>(); });
add([]() { return std::make_shared<OneDrive>(); });
add([]() { return std::make_shared<Dropbox>(); });
add([]() { return std::make_shared<AmazonDrive>(); });
add([]() { return std::make_shared<Box>(); });
add([]() { return std::make_shared<YouTube>(); });
add([]() { return std::make_shared<YandexDisk>(); });
add([]() { return std::make_shared<OwnCloud>(); });
add([]() { return std::make_shared<AmazonS3>(); });
#ifdef WITH_MEGA
providers_.emplace_back(std::make_shared<MegaNz>());
add([]() { return std::make_shared<MegaNz>(); });
#endif
}
std::vector<ICloudProvider::Pointer> CloudStorage::providers() const {
return providers_;
std::vector<std::string> CloudStorage::providers() const {
std::vector<std::string> result;
for (auto r : providers_) result.push_back(r.first);
return result;
}
ICloudProvider::Pointer CloudStorage::provider(const std::string& name) const {
for (ICloudProvider::Pointer p : providers_)
if (p->name() == name) return p;
return nullptr;
ICloudProvider::Pointer CloudStorage::provider(
const std::string& name, ICloudProvider::InitData&& init_data) const {
auto it = providers_.find(name);
if (it == std::end(providers_)) return nullptr;
auto ret = it->second();
ret->initialize(std::move(init_data));
return ret;
}
ICloudStorage::Pointer ICloudStorage::create() {
return util::make_unique<CloudStorage>();
}
void CloudStorage::add(CloudProviderFactory f) { providers_[f()->name()] = f; }
} // namespace cloudstorage
......@@ -24,19 +24,27 @@
#ifndef CLOUDSTORAGE_H
#define CLOUDSTORAGE_H
#include "CloudProvider/CloudProvider.h"
#include "ICloudStorage.h"
#include <map>
namespace cloudstorage {
class CloudStorage : public ICloudStorage {
public:
using CloudProviderFactory = std::function<CloudProvider::Pointer()>;
CloudStorage();
std::vector<ICloudProvider::Pointer> providers() const override;
ICloudProvider::Pointer provider(const std::string& name) const override;
std::vector<std::string> providers() const override;
ICloudProvider::Pointer provider(const std::string& name,
ICloudProvider::InitData&&) const override;
private:
std::vector<ICloudProvider::Pointer> providers_;
void add(CloudProviderFactory);
std::map<std::string, CloudProviderFactory> providers_;
};
} // namespace cloudstorage
......
......@@ -73,24 +73,24 @@ int main(int argc, char** argv) {
std::string drive_backend = "google";
if (argc >= 2) drive_backend = argv[1];
cloudstorage::ICloudProvider::Pointer drive =
cloudstorage::ICloudStorage::create()->provider(drive_backend);
if (drive == nullptr) {
std::cout << "Invalid drive backend.\n";
return 1;
}
std::string drive_file = drive_backend + ".txt";
std::string token;
{
std::fstream file(drive_file, std::fstream::in);
file >> token;
}
drive->initialize({token,
std::unique_ptr<Callback>(new Callback(drive_file)),
nullptr,
nullptr,
nullptr,
{}});
auto drive = cloudstorage::ICloudStorage::create()->provider(
drive_backend,
{token,
std::unique_ptr<Callback>(new Callback(drive_file)),
nullptr,
nullptr,
nullptr,
{}});
if (drive == nullptr) {
std::cout << "Invalid drive backend.\n";
return 1;
}
traverse_drive(*drive, drive->rootDirectory(), "/");
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment