diff --git a/src/internet/googledriveclient.h b/src/internet/googledriveclient.h index 20bc8f612..991aee6d2 100644 --- a/src/internet/googledriveclient.h +++ b/src/internet/googledriveclient.h @@ -49,6 +49,7 @@ public: QString description() const { return data_["description"].toString(); } long size() const { return data_["fileSize"].toUInt(); } QUrl download_url() const { return data_["downloadUrl"].toUrl(); } + QUrl alternate_link() const { return data_["alternateLink"].toUrl(); } QDateTime modified_date() const { return QDateTime::fromString(data_["modifiedDate"].toString(), Qt::ISODate); diff --git a/src/internet/googledriveservice.cpp b/src/internet/googledriveservice.cpp index a09afec9e..740e35a14 100644 --- a/src/internet/googledriveservice.cpp +++ b/src/internet/googledriveservice.cpp @@ -1,6 +1,8 @@ #include "googledriveservice.h" +#include #include +#include #include #include @@ -15,6 +17,7 @@ #include "globalsearch/librarysearchprovider.h" #include "library/librarybackend.h" #include "library/librarymodel.h" +#include "ui/iconloader.h" #include "googledriveclient.h" #include "googledriveurlhandler.h" #include "internetmodel.h" @@ -26,6 +29,7 @@ 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"; } @@ -56,6 +60,9 @@ GoogleDriveService::GoogleDriveService(Application* app, InternetModel* parent) true, app_, this)); } +GoogleDriveService::~GoogleDriveService() { +} + QStandardItem* GoogleDriveService::CreateRootItem() { root_ = new QStandardItem(QIcon(":providers/googledrive.png"), "Google Drive"); root_->setData(true, InternetModel::Role_CanLazyLoad); @@ -220,3 +227,52 @@ QUrl GoogleDriveService::GetStreamingUrlFromSongId(const QString& id) { url.setFragment(client_->access_token()); return url; } + +void GoogleDriveService::ShowContextMenu(const QPoint& global_pos) { + if (!context_menu_) { + context_menu_.reset(new QMenu); + context_menu_->addActions(GetPlaylistActions()); + open_in_drive_action_ = context_menu_->addAction( + QIcon(":/providers/googledrive.png"), tr("Open in Google Drive"), + this, SLOT(OpenWithDrive())); + context_menu_->addSeparator(); + context_menu_->addAction(IconLoader::Load("configure"), + tr("Configure..."), + this, SLOT(ShowSettingsDialog())); + } + + // Only show some actions if there are real songs selected + bool songs_selected = false; + foreach (const QModelIndex& index, model()->selected_indexes()) { + const int type = index.data(LibraryModel::Role_Type).toInt(); + if (type == LibraryItem::Type_Song || + type == LibraryItem::Type_Container) { + songs_selected = true; + break; + } + } + + open_in_drive_action_->setEnabled(songs_selected); + + context_menu_->popup(global_pos); +} + +void GoogleDriveService::OpenWithDrive() { + // Map indexes to the actual library model. + QModelIndexList library_indexes; + foreach (const QModelIndex& index, model()->selected_indexes()) { + if (index.model() == library_sort_model_) { + library_indexes << library_sort_model_->mapToSource(index); + } + } + + // Ask the library for the songs for these indexes. + foreach (const Song& song, library_model_->GetChildSongs(library_indexes)) { + QDesktopServices::openUrl( + QUrl(QString(kDriveEditFileUrl).arg(song.url().path()))); + } +} + +void GoogleDriveService::ShowSettingsDialog() { + app_->OpenSettingsDialogAtPage(SettingsDialog::Page_GoogleDrive); +} diff --git a/src/internet/googledriveservice.h b/src/internet/googledriveservice.h index 505b825ea..59327a23a 100644 --- a/src/internet/googledriveservice.h +++ b/src/internet/googledriveservice.h @@ -24,6 +24,7 @@ class GoogleDriveService : public InternetService { Q_OBJECT public: GoogleDriveService(Application* app, InternetModel* parent); + ~GoogleDriveService(); static const char* kServiceName; static const char* kSettingsGroup; @@ -32,6 +33,7 @@ class GoogleDriveService : public InternetService { QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item); + void ShowContextMenu(const QPoint& global_pos); QUrl GetStreamingUrlFromSongId(const QString& file_id); @@ -51,6 +53,9 @@ class GoogleDriveService : public InternetService { const QString& url, const int task_id); + void OpenWithDrive(); + void ShowSettingsDialog(); + private: void EnsureConnected(); void RefreshAuthorisation(const QString& refresh_token); @@ -69,6 +74,9 @@ class GoogleDriveService : public InternetService { QSortFilterProxyModel* library_sort_model_; int indexing_task_id_; + + boost::scoped_ptr context_menu_; + QAction* open_in_drive_action_; }; #endif