diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 91c871f9b..b4d3ad22a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1120,10 +1120,14 @@ optional_source(HAVE_DROPBOX optional_source(HAVE_SKYDRIVE SOURCES internet/skydriveservice.cpp + internet/skydrivesettingspage.cpp internet/skydriveurlhandler.cpp HEADERS internet/skydriveservice.h + internet/skydrivesettingspage.h internet/skydriveurlhandler.h + UI + internet/skydrivesettingspage.ui ) # Box support diff --git a/src/internet/skydriveservice.cpp b/src/internet/skydriveservice.cpp index 435051a65..06a5e32d3 100644 --- a/src/internet/skydriveservice.cpp +++ b/src/internet/skydriveservice.cpp @@ -12,9 +12,7 @@ namespace { -static const char* kServiceName = "OneDrive"; static const char* kServiceId = "skydrive"; -static const char* kSettingsGroup = "Skydrive"; static const char* kClientId = "0000000040111F16"; static const char* kClientSecret = "w2ClguSX0jG56cBl1CeUniypTBRjXt2Z"; @@ -30,6 +28,9 @@ static const char* kSkydriveBase = "https://apis.live.net/v5.0/"; } // namespace +const char* SkydriveService::kServiceName = "OneDrive"; +const char* SkydriveService::kSettingsGroup = "Skydrive"; + SkydriveService::SkydriveService(Application* app, InternetModel* parent) : CloudFileService(app, parent, kServiceName, kServiceId, QIcon(":providers/skydrive.png"), @@ -37,16 +38,22 @@ SkydriveService::SkydriveService(Application* app, InternetModel* parent) app->player()->RegisterUrlHandler(new SkydriveUrlHandler(this, this)); } -bool SkydriveService::has_credentials() const { return true; } +bool SkydriveService::has_credentials() const { + return !refresh_token().isEmpty(); +} + +QString SkydriveService::refresh_token() const { + QSettings s; + s.beginGroup(kSettingsGroup); + + return s.value("refresh_token").toString(); +} void SkydriveService::Connect() { OAuthenticator* oauth = new OAuthenticator( kClientId, kClientSecret, OAuthenticator::RedirectStyle::REMOTE, this); - QSettings s; - s.beginGroup(kSettingsGroup); - if (s.contains("refresh_token")) { - oauth->RefreshAuthorisation(kOAuthTokenEndpoint, - s.value("refresh_token").toString()); + if (!refresh_token().isEmpty()) { + oauth->RefreshAuthorisation(kOAuthTokenEndpoint, refresh_token()); } else { oauth->StartAuthorisation(kOAuthEndpoint, kOAuthTokenEndpoint, kOAuthScope); } @@ -162,3 +169,11 @@ void SkydriveService::EnsureConnected() { Connect(); WaitForSignal(this, SIGNAL(Connected())); } + +void SkydriveService::ForgetCredentials() { + QSettings s; + s.beginGroup(kSettingsGroup); + + s.remove("refresh_token"); + s.remove("name"); +} diff --git a/src/internet/skydriveservice.h b/src/internet/skydriveservice.h index 330abdba6..5599ee60c 100644 --- a/src/internet/skydriveservice.h +++ b/src/internet/skydriveservice.h @@ -14,22 +14,27 @@ class SkydriveService : public CloudFileService { public: SkydriveService(Application* app, InternetModel* parent); + + static const char* kServiceName; + static const char* kSettingsGroup; + + virtual bool has_credentials() const; QUrl GetStreamingUrlFromSongId(const QString& song_id); - protected: - // CloudFileService - virtual bool has_credentials() const; + public slots: virtual void Connect(); + void ForgetCredentials(); private slots: void ConnectFinished(OAuthenticator* oauth); void FetchUserInfoFinished(QNetworkReply* reply); void ListFilesFinished(QNetworkReply* reply); -signals: + signals: void Connected(); private: + QString refresh_token() const; void AddAuthorizationHeader(QNetworkRequest* request); void ListFiles(const QString& folder); void EnsureConnected(); diff --git a/src/internet/skydrivesettingspage.cpp b/src/internet/skydrivesettingspage.cpp new file mode 100644 index 000000000..e7a6b33c4 --- /dev/null +++ b/src/internet/skydrivesettingspage.cpp @@ -0,0 +1,86 @@ +/* This file is part of Clementine. + Copyright 2013, John Maguire + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#include "skydrivesettingspage.h" + +#include + +#include "ui_skydrivesettingspage.h" +#include "core/application.h" +#include "internet/skydriveservice.h" +#include "internet/internetmodel.h" +#include "ui/settingsdialog.h" + +SkydriveSettingsPage::SkydriveSettingsPage(SettingsDialog* parent) + : SettingsPage(parent), + ui_(new Ui::SkydriveSettingsPage), + service_(dialog()->app()->internet_model()->Service()) { + ui_->setupUi(this); + ui_->login_state->AddCredentialGroup(ui_->login_container); + + connect(ui_->login_button, SIGNAL(clicked()), SLOT(LoginClicked())); + connect(ui_->login_state, SIGNAL(LogoutClicked()), SLOT(LogoutClicked())); + connect(service_, SIGNAL(Connected()), SLOT(Connected())); + + dialog()->installEventFilter(this); +} + +SkydriveSettingsPage::~SkydriveSettingsPage() { delete ui_; } + +void SkydriveSettingsPage::Load() { + QSettings s; + s.beginGroup(SkydriveService::kSettingsGroup); + + const QString name = s.value("name").toString(); + + if (!name.isEmpty()) { + ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, name); + } +} + +void SkydriveSettingsPage::Save() { + QSettings s; + s.beginGroup(SkydriveService::kSettingsGroup); +} + +void SkydriveSettingsPage::LoginClicked() { + service_->Connect(); + ui_->login_button->setEnabled(false); +} + +bool SkydriveSettingsPage::eventFilter(QObject* object, QEvent* event) { + if (object == dialog() && event->type() == QEvent::Enter) { + ui_->login_button->setEnabled(true); + return false; + } + + return SettingsPage::eventFilter(object, event); +} + +void SkydriveSettingsPage::LogoutClicked() { + service_->ForgetCredentials(); + ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedOut); +} + +void SkydriveSettingsPage::Connected() { + QSettings s; + s.beginGroup(SkydriveService::kSettingsGroup); + + const QString name = s.value("name").toString(); + + ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, name); +} diff --git a/src/internet/skydrivesettingspage.h b/src/internet/skydrivesettingspage.h new file mode 100644 index 000000000..4b27cc487 --- /dev/null +++ b/src/internet/skydrivesettingspage.h @@ -0,0 +1,53 @@ +/* This file is part of Clementine. + Copyright 2013, John Maguire + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#ifndef SKYDRIVESETTINGSPAGE_H +#define SKYDRIVESETTINGSPAGE_H + +#include "ui/settingspage.h" + +#include +#include + +class SkydriveService; +class Ui_SkydriveSettingsPage; + +class SkydriveSettingsPage : public SettingsPage { + Q_OBJECT + + public: + SkydriveSettingsPage(SettingsDialog* parent = nullptr); + ~SkydriveSettingsPage(); + + void Load(); + void Save(); + + // QObject + bool eventFilter(QObject* object, QEvent* event); + + private slots: + void LoginClicked(); + void LogoutClicked(); + void Connected(); + + private: + Ui_SkydriveSettingsPage* ui_; + + SkydriveService* service_; +}; + +#endif // SKYDRIVESETTINGSPAGE_H diff --git a/src/internet/skydrivesettingspage.ui b/src/internet/skydrivesettingspage.ui new file mode 100644 index 000000000..6fd3bb7e0 --- /dev/null +++ b/src/internet/skydrivesettingspage.ui @@ -0,0 +1,110 @@ + + + SkydriveSettingsPage + + + + 0 + 0 + 569 + 491 + + + + OneDrive + + + + :/providers/skydrive.png:/providers/skydrive.png + + + + + + Clementine can play music that you have uploaded to OneDrive + + + true + + + + + + + + + + + 28 + + + 0 + + + 0 + + + + + + + Login + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Clicking the Login button will open a web browser. You should return to Clementine after you have logged in. + + + true + + + + + + + + + + Qt::Vertical + + + + 20 + 357 + + + + + + + + + LoginStateWidget + QWidget +
widgets/loginstatewidget.h
+ 1 +
+
+ + + + +
diff --git a/src/ui/settingsdialog.cpp b/src/ui/settingsdialog.cpp index d923b0aa9..40db17c2f 100644 --- a/src/ui/settingsdialog.cpp +++ b/src/ui/settingsdialog.cpp @@ -77,7 +77,11 @@ #endif #ifdef HAVE_VK -# include "internet/vksettingspage.h" +#include "internet/vksettingspage.h" +#endif + +#ifdef HAVE_SKYDRIVE +#include "internet/skydrivesettingspage.h" #endif #include @@ -177,6 +181,10 @@ SettingsDialog::SettingsDialog(Application* app, BackgroundStreams* streams, AddPage(Page_Box, new BoxSettingsPage(this), providers); #endif +#ifdef HAVE_SKYDRIVE + AddPage(Page_Skydrive, new SkydriveSettingsPage(this), providers); +#endif + AddPage(Page_SoundCloud, new SoundCloudSettingsPage(this), providers); AddPage(Page_Spotify, new SpotifySettingsPage(this), providers);