Add a context menu to google drive items

This commit is contained in:
David Sansome 2012-08-24 19:33:05 +01:00
parent 4719a347fa
commit 252f1e38d8
3 changed files with 65 additions and 0 deletions

View File

@ -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);

View File

@ -1,6 +1,8 @@
#include "googledriveservice.h"
#include <QDesktopServices>
#include <QEventLoop>
#include <QMenu>
#include <QScopedPointer>
#include <QSortFilterProxyModel>
@ -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);
}

View File

@ -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<QMenu> context_menu_;
QAction* open_in_drive_action_;
};
#endif