From db7679abe63f6dd6302c529c3a889cbb8e1e9687 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Wed, 28 Nov 2012 16:21:14 +0100 Subject: [PATCH] Refactor GoogleDriveService into a CloudFileService. --- src/internet/googledriveservice.cpp | 77 ++++------------------------- src/internet/googledriveservice.h | 36 ++------------ src/internet/ubuntuoneservice.h | 8 --- 3 files changed, 14 insertions(+), 107 deletions(-) diff --git a/src/internet/googledriveservice.cpp b/src/internet/googledriveservice.cpp index 1a6d8dc6a..c6f388bb2 100644 --- a/src/internet/googledriveservice.cpp +++ b/src/internet/googledriveservice.cpp @@ -29,64 +29,24 @@ const char* GoogleDriveService::kSettingsGroup = "GoogleDrive"; namespace { -static const char* kSongsTable = "google_drive_songs"; -static const char* kFtsTable = "google_drive_songs_fts"; static const char* kDriveEditFileUrl = "https://docs.google.com/file/d/%1/edit"; +static const char* kServiceId = "google_drive"; } GoogleDriveService::GoogleDriveService(Application* app, InternetModel* parent) - : InternetService(kServiceName, app, parent, parent), - root_(NULL), - client_(new google_drive::Client(this)), - task_manager_(app->task_manager()), - 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); - - library_sort_model_->setSourceModel(library_model_); - library_sort_model_->setSortRole(LibraryModel::Role_SortText); - library_sort_model_->setDynamicSortFilter(true); - library_sort_model_->sort(0); - + : CloudFileService( + app, parent, + kServiceName, kServiceId, + QIcon(":/providers/googledrive.png"), + SettingsDialog::Page_GoogleDrive), + client_(new google_drive::Client(this)) { app->player()->RegisterUrlHandler(new GoogleDriveUrlHandler(this, this)); - app->global_search()->AddProvider(new LibrarySearchProvider( - library_backend_, - tr("Google Drive"), - "google_drive", - QIcon(":/providers/googledrive.png"), - true, app_, this)); } -GoogleDriveService::~GoogleDriveService() { -} - -QStandardItem* GoogleDriveService::CreateRootItem() { - root_ = new QStandardItem(QIcon(":providers/googledrive.png"), "Google Drive"); - root_->setData(true, InternetModel::Role_CanLazyLoad); - return root_; -} - -void GoogleDriveService::LazyPopulate(QStandardItem* item) { - switch (item->data(InternetModel::Role_Type).toInt()) { - case InternetModel::Type_Service: - if (refresh_token().isEmpty()) { - ShowSettingsDialog(); - } else if (!client_->is_authenticated()) { - Connect(); - } - library_model_->Init(); - model()->merged_model()->AddSubModel(item->index(), library_sort_model_); - break; - - default: - break; - } +bool GoogleDriveService::has_credentials() const { + return !refresh_token().isEmpty(); } QString GoogleDriveService::refresh_token() const { @@ -290,22 +250,3 @@ void GoogleDriveService::OpenWithDrive() { QUrl(QString(kDriveEditFileUrl).arg(song.url().path()))); } } - -void GoogleDriveService::ShowSettingsDialog() { - app_->OpenSettingsDialogAtPage(SettingsDialog::Page_GoogleDrive); -} - -void GoogleDriveService::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 GoogleDriveService::AddToPlaylist(QMimeData* mime) { - playlist_manager_->current()->dropMimeData( - mime, Qt::CopyAction, -1, 0, QModelIndex()); -} diff --git a/src/internet/googledriveservice.h b/src/internet/googledriveservice.h index c3c9331aa..7d2b5c60f 100644 --- a/src/internet/googledriveservice.h +++ b/src/internet/googledriveservice.h @@ -1,20 +1,10 @@ #ifndef GOOGLEDRIVESERVICE_H #define GOOGLEDRIVESERVICE_H -#include "internetservice.h" +#include "cloudfileservice.h" -#include "core/network.h" #include "core/tagreaderclient.h" -class QStandardItem; - -class AlbumCoverManager; -class LibraryBackend; -class LibraryModel; -class PlaylistManager; -class TaskManager; -class QSortFilterProxyModel; - namespace google_drive { class Client; class ConnectResponse; @@ -22,22 +12,20 @@ namespace google_drive { class ListFilesResponse; } -class GoogleDriveService : public InternetService { +class GoogleDriveService : public CloudFileService { Q_OBJECT public: GoogleDriveService(Application* app, InternetModel* parent); - ~GoogleDriveService(); static const char* kServiceName; static const char* kSettingsGroup; + virtual bool has_credentials() const; + virtual void ShowContextMenu(const QPoint& global_pos); + google_drive::Client* client() const { return client_; } QString refresh_token() const; - QStandardItem* CreateRootItem(); - void LazyPopulate(QStandardItem* item); - void ShowContextMenu(const QPoint& global_pos); - QUrl GetStreamingUrlFromSongId(const QString& file_id); public slots: @@ -57,9 +45,6 @@ class GoogleDriveService : public InternetService { const int task_id); void OpenWithDrive(); - void ShowSettingsDialog(); - void ShowCoverManager(); - void AddToPlaylist(QMimeData* mime); private: void EnsureConnected(); @@ -67,24 +52,13 @@ class GoogleDriveService : public InternetService { void MaybeAddFileToDatabase(const google_drive::File& file); void ListFilesForMimeType(const QString& mime_type); - QStandardItem* root_; - google_drive::Client* client_; - NetworkAccessManager network_; TaskManager* task_manager_; - LibraryBackend* library_backend_; - LibraryModel* library_model_; - QSortFilterProxyModel* library_sort_model_; - PlaylistManager* playlist_manager_; - int indexing_task_id_; - boost::scoped_ptr context_menu_; QAction* open_in_drive_action_; - - boost::scoped_ptr cover_manager_; }; #endif diff --git a/src/internet/ubuntuoneservice.h b/src/internet/ubuntuoneservice.h index 5f46208ab..f252f7db8 100644 --- a/src/internet/ubuntuoneservice.h +++ b/src/internet/ubuntuoneservice.h @@ -3,17 +3,9 @@ #include "internet/cloudfileservice.h" -#include - #include "core/tagreaderclient.h" -#include "ui/albumcovermanager.h" -class LibraryBackend; -class LibraryModel; -class NetworkAccessManager; -class PlaylistManager; class QNetworkReply; -class QSortFilterProxyModel; class UbuntuOneAuthenticator; class UbuntuOneService : public CloudFileService {