diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 14ac63fc9..4dcc7a293 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -158,6 +158,7 @@ set(SOURCES globalsearch/suggestionwidget.cpp globalsearch/urlsearchprovider.cpp + internet/cloudfileservice.cpp internet/digitallyimportedclient.cpp internet/digitallyimportedservicebase.cpp internet/digitallyimportedsettingspage.cpp @@ -440,6 +441,7 @@ set(HEADERS globalsearch/soundcloudsearchprovider.h globalsearch/suggestionwidget.h + internet/cloudfileservice.h internet/digitallyimportedclient.h internet/digitallyimportedservicebase.h internet/digitallyimportedsettingspage.h @@ -450,7 +452,7 @@ set(HEADERS internet/icecastbackend.h internet/icecastfilterwidget.h internet/icecastmodel.h - internet/icecastservice.h + internet/icecastservice.h internet/internetmimedata.h internet/internetmodel.h internet/internetservice.h diff --git a/src/internet/cloudfileservice.cpp b/src/internet/cloudfileservice.cpp new file mode 100644 index 000000000..01799c8a7 --- /dev/null +++ b/src/internet/cloudfileservice.cpp @@ -0,0 +1,109 @@ +#include "cloudfileservice.h" + +#include +#include + +#include "core/application.h" +#include "core/database.h" +#include "core/mergedproxymodel.h" +#include "core/network.h" +#include "core/player.h" +#include "globalsearch/globalsearch.h" +#include "globalsearch/librarysearchprovider.h" +#include "internet/internetmodel.h" +#include "library/librarybackend.h" +#include "library/librarymodel.h" +#include "playlist/playlist.h" +#include "ui/iconloader.h" + +CloudFileService::CloudFileService( + Application* app, + InternetModel* parent, + const QString& service_name, + const QString& service_id, + const QIcon& icon, + SettingsDialog::Page settings_page) + : InternetService(service_name, app, parent, parent), + root_(nullptr), + network_(new NetworkAccessManager(this)), + library_sort_model_(new QSortFilterProxyModel(this)), + playlist_manager_(app->playlist_manager()), + icon_(icon), + settings_page_(settings_page) { + library_backend_ = new LibraryBackend; + library_backend_->moveToThread(app_->database()->thread()); + + QString songs_table = service_id + "_songs"; + QString songs_fts_table = service_id + "_songs_fts"; + + library_backend_->Init( + app->database(), songs_table, QString::null, QString::null, songs_fts_table); + library_model_ = new LibraryModel(library_backend_, app_, this); + + library_sort_model_->setSourceModel(library_model_); + library_sort_model_->setSortRole(LibraryModel::Role_SortText); + library_sort_model_->setDynamicSortFilter(true); + library_sort_model_->sort(0); + + app->global_search()->AddProvider(new LibrarySearchProvider( + library_backend_, + service_name, + service_id, + icon_, + true, app_, this)); +} + +QStandardItem* CloudFileService::CreateRootItem() { + root_ = new QStandardItem(icon_, name()); + root_->setData(true, InternetModel::Role_CanLazyLoad); + return root_; +} + +void CloudFileService::LazyPopulate(QStandardItem* item) { + switch (item->data(InternetModel::Role_Type).toInt()) { + case InternetModel::Type_Service: + if (!has_credentials()) { + ShowSettingsDialog(); + } else { + Connect(); + } + library_model_->Init(); + model()->merged_model()->AddSubModel(item->index(), library_sort_model_); + break; + + default: + break; + } +} + +void CloudFileService::ShowContextMenu(const QPoint& global_pos) { + if (!context_menu_) { + context_menu_.reset(new QMenu); + context_menu_->addActions(GetPlaylistActions()); + context_menu_->addAction( + IconLoader::Load("download"), + tr("Cover Manager"), + this, + SLOT(ShowCoverManager())); + } + context_menu_->popup(global_pos); +} + +void CloudFileService::ShowCoverManager() { + if (!cover_manager_) { + cover_manager_.reset(new AlbumCoverManager(app_, library_backend_)); + cover_manager_->Init(); + connect(cover_manager_.get(), SIGNAL(AddToPlaylist(QMimeData*)), + SLOT(AddToPlaylist(QMimeData*))); + } + cover_manager_->show(); +} + +void CloudFileService::AddToPlaylist(QMimeData* mime) { + playlist_manager_->current()->dropMimeData( + mime, Qt::CopyAction, -1, 0, QModelIndex()); +} + +void CloudFileService::ShowSettingsDialog() { + app_->OpenSettingsDialogAtPage(settings_page_); +} diff --git a/src/internet/cloudfileservice.h b/src/internet/cloudfileservice.h new file mode 100644 index 000000000..060f84829 --- /dev/null +++ b/src/internet/cloudfileservice.h @@ -0,0 +1,59 @@ +#ifndef CLOUDFILESERVICE_H +#define CLOUDFILESERVICE_H + +#include "internetservice.h" + +#include + +#include "ui/albumcovermanager.h" + +class UrlHandler; +class QSortFilterProxyModel; +class LibraryBackend; +class LibraryModel; +class NetworkAccessManager; +class PlaylistManager; + +class CloudFileService : public InternetService { + Q_OBJECT + public: + CloudFileService( + Application* app, + InternetModel* parent, + const QString& service_name, + const QString& service_id, + const QIcon& icon, + SettingsDialog::Page settings_page); + + // InternetService + virtual QStandardItem* CreateRootItem(); + virtual void LazyPopulate(QStandardItem* item); + virtual void ShowContextMenu(const QPoint& point); + + protected: + virtual bool has_credentials() const = 0; + virtual void Connect() = 0; + + private slots: + void ShowCoverManager(); + void AddToPlaylist(QMimeData* mime); + void ShowSettingsDialog(); + + protected: + QStandardItem* root_; + NetworkAccessManager* network_; + + LibraryBackend* library_backend_; + LibraryModel* library_model_; + QSortFilterProxyModel* library_sort_model_; + + boost::scoped_ptr context_menu_; + boost::scoped_ptr cover_manager_; + PlaylistManager* playlist_manager_; + + private: + QIcon icon_; + SettingsDialog::Page settings_page_; +}; + +#endif // CLOUDFILESERVICE_H diff --git a/src/internet/ubuntuoneservice.cpp b/src/internet/ubuntuoneservice.cpp index cd582f62d..8a29b34e2 100644 --- a/src/internet/ubuntuoneservice.cpp +++ b/src/internet/ubuntuoneservice.cpp @@ -31,56 +31,17 @@ namespace { static const char* kFileStorageEndpoint = "https://one.ubuntu.com/api/file_storage/v1"; static const char* kContentRoot = "https://files.one.ubuntu.com"; -static const char* kSongsTable = "ubuntu_one_songs"; -static const char* kFtsTable = "ubuntu_one_songs_fts"; +static const char* kServiceId = "ubuntu_one"; } UbuntuOneService::UbuntuOneService(Application* app, InternetModel* parent) - : InternetService(kServiceName, app, parent, parent), - root_(nullptr), - network_(new NetworkAccessManager(this)), - library_sort_model_(new QSortFilterProxyModel(this)), - playlist_manager_(app->playlist_manager()) { - library_backend_ = new LibraryBackend; - library_backend_->moveToThread(app_->database()->thread()); - library_backend_->Init( - app->database(), kSongsTable, QString::null, QString::null, kFtsTable); - library_model_ = new LibraryModel(library_backend_, app_, this); + : CloudFileService( + app, parent, + kServiceName, kServiceId, + QIcon(":/providers/ubuntuone.png"), + SettingsDialog::Page_UbuntuOne) { + app_->player()->RegisterUrlHandler(new UbuntuOneUrlHandler(this, this)); - library_sort_model_->setSourceModel(library_model_); - library_sort_model_->setSortRole(LibraryModel::Role_SortText); - library_sort_model_->setDynamicSortFilter(true); - library_sort_model_->sort(0); - - app->player()->RegisterUrlHandler(new UbuntuOneUrlHandler(this, this)); - app->global_search()->AddProvider(new LibrarySearchProvider( - library_backend_, - kServiceName, - "ubuntu_one", - QIcon(":/providers/ubuntuone.png"), - true, app_, this)); -} - -QStandardItem* UbuntuOneService::CreateRootItem() { - root_ = new QStandardItem(QIcon(":providers/ubuntuone.png"), "Ubuntu One"); - root_->setData(true, InternetModel::Role_CanLazyLoad); - return root_; -} - -void UbuntuOneService::LazyPopulate(QStandardItem* item) { - switch (item->data(InternetModel::Role_Type).toInt()) { - case InternetModel::Type_Service: - Connect(); - library_model_->Init(); - model()->merged_model()->AddSubModel(item->index(), library_sort_model_); - break; - - default: - break; - } -} - -void UbuntuOneService::Connect() { QSettings s; s.beginGroup(kSettingsGroup); if (s.contains("consumer_key")) { @@ -88,7 +49,15 @@ void UbuntuOneService::Connect() { consumer_secret_ = s.value("consumer_secret").toString(); token_ = s.value("token").toString(); token_secret_ = s.value("token_secret").toString(); + } +} +bool UbuntuOneService::has_credentials() const { + return !consumer_key_.isEmpty(); +} + +void UbuntuOneService::Connect() { + if (has_credentials()) { RequestFileList("/~/Ubuntu One"); } else { ShowSettingsDialog(); @@ -211,22 +180,6 @@ void UbuntuOneService::ShowSettingsDialog() { app_->OpenSettingsDialogAtPage(SettingsDialog::Page_UbuntuOne); } -void UbuntuOneService::ShowContextMenu(const QPoint& global_pos) { - if (!context_menu_) { - context_menu_.reset(new QMenu); - context_menu_->addActions(GetPlaylistActions()); - context_menu_->addAction( - IconLoader::Load("download"), - tr("Cover Manager"), - this, - SLOT(ShowCoverManager())); - context_menu_->addAction(IconLoader::Load("configure"), - tr("Configure..."), - this, SLOT(ShowSettingsDialog())); - } - context_menu_->popup(global_pos); -} - void UbuntuOneService::ShowCoverManager() { if (!cover_manager_) { cover_manager_.reset(new AlbumCoverManager(app_, library_backend_)); diff --git a/src/internet/ubuntuoneservice.h b/src/internet/ubuntuoneservice.h index f0ad6335d..5f46208ab 100644 --- a/src/internet/ubuntuoneservice.h +++ b/src/internet/ubuntuoneservice.h @@ -1,7 +1,7 @@ #ifndef UBUNTUONESERVICE_H #define UBUNTUONESERVICE_H -#include "internet/internetservice.h" +#include "internet/cloudfileservice.h" #include @@ -16,7 +16,7 @@ class QNetworkReply; class QSortFilterProxyModel; class UbuntuOneAuthenticator; -class UbuntuOneService : public InternetService { +class UbuntuOneService : public CloudFileService { Q_OBJECT public: UbuntuOneService(Application* app, InternetModel* parent); @@ -24,11 +24,6 @@ class UbuntuOneService : public InternetService { static const char* kServiceName; static const char* kSettingsGroup; - // InternetService - virtual QStandardItem* CreateRootItem(); - virtual void LazyPopulate(QStandardItem* parent); - virtual void ShowContextMenu(const QPoint& global_pos); - QUrl GetStreamingUrlFromSongId(const QString& song_id); private slots: @@ -44,25 +39,15 @@ class UbuntuOneService : public InternetService { void Connect(); void RequestFileList(const QString& path); void MaybeAddFileToDatabase(const QVariantMap& file); + bool has_credentials() const; private: QByteArray GenerateAuthorisationHeader(); - QStandardItem* root_; - NetworkAccessManager* network_; - QString consumer_key_; QString consumer_secret_; QString token_; QString token_secret_; - - LibraryBackend* library_backend_; - LibraryModel* library_model_; - QSortFilterProxyModel* library_sort_model_; - - boost::scoped_ptr context_menu_; - boost::scoped_ptr cover_manager_; - PlaylistManager* playlist_manager_; }; #endif // UBUNTUONESERVICE_H