2012-07-12 14:09:20 +02:00
|
|
|
#include "googledriveservice.h"
|
|
|
|
|
2012-08-24 20:33:05 +02:00
|
|
|
#include <QDesktopServices>
|
2012-07-25 17:57:50 +02:00
|
|
|
#include <QEventLoop>
|
2012-08-24 20:33:05 +02:00
|
|
|
#include <QMenu>
|
2012-07-28 18:18:03 +02:00
|
|
|
#include <QScopedPointer>
|
2012-07-27 16:04:12 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
2012-07-25 17:57:50 +02:00
|
|
|
|
2012-07-26 16:35:57 +02:00
|
|
|
#include "core/application.h"
|
2012-07-12 14:09:20 +02:00
|
|
|
#include "core/closure.h"
|
2012-07-27 16:04:12 +02:00
|
|
|
#include "core/database.h"
|
|
|
|
#include "core/mergedproxymodel.h"
|
2012-07-26 16:35:57 +02:00
|
|
|
#include "core/player.h"
|
2012-07-27 16:04:12 +02:00
|
|
|
#include "core/timeconstants.h"
|
2012-10-01 16:39:58 +02:00
|
|
|
#include "ui/albumcovermanager.h"
|
2012-07-27 16:04:12 +02:00
|
|
|
#include "globalsearch/globalsearch.h"
|
|
|
|
#include "globalsearch/librarysearchprovider.h"
|
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/librarymodel.h"
|
2012-10-01 16:39:58 +02:00
|
|
|
#include "playlist/playlist.h"
|
2012-08-24 20:33:05 +02:00
|
|
|
#include "ui/iconloader.h"
|
2012-07-28 18:18:03 +02:00
|
|
|
#include "googledriveclient.h"
|
2012-07-26 16:35:57 +02:00
|
|
|
#include "googledriveurlhandler.h"
|
2012-07-12 14:09:20 +02:00
|
|
|
#include "internetmodel.h"
|
|
|
|
|
2012-07-29 16:06:23 +02:00
|
|
|
const char* GoogleDriveService::kServiceName = "Google Drive";
|
|
|
|
const char* GoogleDriveService::kSettingsGroup = "GoogleDrive";
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-07-29 16:06:23 +02:00
|
|
|
namespace {
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-08-24 20:33:05 +02:00
|
|
|
static const char* kDriveEditFileUrl = "https://docs.google.com/file/d/%1/edit";
|
2012-11-28 16:21:14 +01:00
|
|
|
static const char* kServiceId = "google_drive";
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GoogleDriveService::GoogleDriveService(Application* app, InternetModel* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: CloudFileService(app, parent, kServiceName, kServiceId,
|
|
|
|
QIcon(":/providers/googledrive.png"),
|
|
|
|
SettingsDialog::Page_GoogleDrive),
|
2012-12-06 14:23:27 +01:00
|
|
|
client_(new google_drive::Client(this)) {
|
2012-07-26 16:35:57 +02:00
|
|
|
app->player()->RegisterUrlHandler(new GoogleDriveUrlHandler(this, this));
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 16:21:14 +01:00
|
|
|
bool GoogleDriveService::has_credentials() const {
|
|
|
|
return !refresh_token().isEmpty();
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
2012-08-24 22:26:10 +02:00
|
|
|
QString GoogleDriveService::refresh_token() const {
|
2012-07-26 16:55:59 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
2012-08-24 22:26:10 +02:00
|
|
|
return s.value("refresh_token").toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoogleDriveService::Connect() {
|
|
|
|
google_drive::ConnectResponse* response = client_->Connect(refresh_token());
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(response, SIGNAL(Finished()), this,
|
|
|
|
SLOT(ConnectFinished(google_drive::ConnectResponse*)), response);
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
2012-08-17 22:48:45 +02:00
|
|
|
void GoogleDriveService::ForgetCredentials() {
|
|
|
|
client_->ForgetCredentials();
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
s.remove("refresh_token");
|
|
|
|
s.remove("user_email");
|
|
|
|
}
|
|
|
|
|
2012-12-05 12:57:10 +01:00
|
|
|
void GoogleDriveService::ListChanges(const QString& cursor) {
|
2014-02-07 16:34:20 +01:00
|
|
|
google_drive::ListChangesResponse* changes_response =
|
|
|
|
client_->ListChanges(cursor);
|
2012-12-05 12:57:10 +01:00
|
|
|
connect(changes_response, SIGNAL(FilesFound(QList<google_drive::File>)),
|
|
|
|
SLOT(FilesFound(QList<google_drive::File>)));
|
|
|
|
connect(changes_response, SIGNAL(FilesDeleted(QList<QUrl>)),
|
|
|
|
SLOT(FilesDeleted(QList<QUrl>)));
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(changes_response, SIGNAL(Finished()), this,
|
|
|
|
SLOT(ListChangesFinished(google_drive::ListChangesResponse*)),
|
2012-12-05 12:57:10 +01:00
|
|
|
changes_response);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GoogleDriveService::ListChangesFinished(
|
|
|
|
google_drive::ListChangesResponse* changes_response) {
|
2012-12-05 12:57:10 +01:00
|
|
|
changes_response->deleteLater();
|
2014-03-30 07:01:58 +02:00
|
|
|
|
|
|
|
const QString cursor = changes_response->next_cursor();
|
|
|
|
if (is_indexing()) {
|
|
|
|
// Only save the cursor after all the songs have been indexed - that way if
|
|
|
|
// Clementine is closed it'll resume next time.
|
|
|
|
NewClosure(this, SIGNAL(AllIndexingTasksFinished()),
|
|
|
|
this, SLOT(SaveCursor(QString)),
|
|
|
|
cursor);
|
|
|
|
} else {
|
|
|
|
SaveCursor(cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GoogleDriveService::SaveCursor(const QString& cursor) {
|
2012-12-05 12:57:10 +01:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
2014-03-30 07:01:58 +02:00
|
|
|
s.setValue("cursor", cursor);
|
2012-07-31 11:57:04 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GoogleDriveService::ConnectFinished(
|
|
|
|
google_drive::ConnectResponse* response) {
|
2012-07-28 18:18:03 +02:00
|
|
|
response->deleteLater();
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-07-28 18:18:03 +02:00
|
|
|
// Save the refresh token
|
2012-07-26 16:55:59 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
2012-07-28 18:18:03 +02:00
|
|
|
s.setValue("refresh_token", response->refresh_token());
|
2012-08-24 22:26:10 +02:00
|
|
|
|
|
|
|
if (!response->user_email().isEmpty()) {
|
|
|
|
// We only fetch the user's email address the first time we authenticate.
|
|
|
|
s.setValue("user_email", response->user_email());
|
|
|
|
}
|
2012-08-17 22:48:45 +02:00
|
|
|
|
|
|
|
emit Connected();
|
2012-07-26 16:55:59 +02:00
|
|
|
|
2012-12-05 12:57:10 +01:00
|
|
|
// Find all the changes since the last check.
|
|
|
|
ListChanges(s.value("cursor").toString());
|
2012-07-28 18:18:03 +02:00
|
|
|
}
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-08-10 20:46:26 +02:00
|
|
|
void GoogleDriveService::EnsureConnected() {
|
|
|
|
if (client_->is_authenticated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
connect(client_, SIGNAL(Authenticated()), &loop, SLOT(quit()));
|
|
|
|
Connect();
|
|
|
|
loop.exec();
|
|
|
|
}
|
|
|
|
|
2012-07-28 18:18:03 +02:00
|
|
|
void GoogleDriveService::FilesFound(const QList<google_drive::File>& files) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const google_drive::File& file : files) {
|
2012-12-06 14:23:27 +01:00
|
|
|
if (!IsSupportedMimeType(file.mime_type())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme("googledrive");
|
|
|
|
url.setPath(file.id());
|
|
|
|
|
|
|
|
Song song;
|
|
|
|
// Add some extra tags from the Google Drive metadata.
|
|
|
|
song.set_etag(file.etag().remove('"'));
|
|
|
|
song.set_mtime(file.modified_date().toTime_t());
|
|
|
|
song.set_ctime(file.created_date().toTime_t());
|
|
|
|
song.set_comment(file.description());
|
|
|
|
song.set_directory_id(0);
|
|
|
|
song.set_url(QUrl(url));
|
|
|
|
song.set_filesize(file.size());
|
|
|
|
|
|
|
|
// Use the Google Drive title if we couldn't read tags from the file.
|
|
|
|
if (song.title().isEmpty()) {
|
|
|
|
song.set_title(file.title());
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
MaybeAddFileToDatabase(song, file.mime_type(), file.download_url(),
|
|
|
|
QString("Bearer %1").arg(client_->access_token()));
|
2012-07-27 16:04:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 12:57:10 +01:00
|
|
|
void GoogleDriveService::FilesDeleted(const QList<QUrl>& files) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QUrl& url : files) {
|
2012-12-05 12:57:10 +01:00
|
|
|
Song song = library_backend_->GetSongByUrl(url);
|
|
|
|
qLog(Debug) << "Deleting:" << url << song.title();
|
|
|
|
if (song.is_valid()) {
|
|
|
|
library_backend_->DeleteSongs(SongList() << song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 16:35:57 +02:00
|
|
|
QUrl GoogleDriveService::GetStreamingUrlFromSongId(const QString& id) {
|
2012-08-10 20:46:26 +02:00
|
|
|
EnsureConnected();
|
2012-07-28 18:18:03 +02:00
|
|
|
QScopedPointer<google_drive::GetFileResponse> response(client_->GetFile(id));
|
|
|
|
|
2012-07-26 16:35:57 +02:00
|
|
|
QEventLoop loop;
|
2012-07-28 18:18:03 +02:00
|
|
|
connect(response.data(), SIGNAL(Finished()), &loop, SLOT(quit()));
|
2012-07-26 16:35:57 +02:00
|
|
|
loop.exec();
|
|
|
|
|
2012-07-28 18:18:03 +02:00
|
|
|
QUrl url(response->file().download_url());
|
2012-11-02 16:47:40 +01:00
|
|
|
url.addQueryItem("access_token", client_->access_token());
|
2012-07-28 18:18:03 +02:00
|
|
|
return url;
|
2012-07-26 16:35:57 +02:00
|
|
|
}
|
2012-08-24 20:33:05 +02:00
|
|
|
|
|
|
|
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(
|
2014-02-07 16:34:20 +01:00
|
|
|
QIcon(":/providers/googledrive.png"), tr("Open in Google Drive"), this,
|
|
|
|
SLOT(OpenWithDrive()));
|
2012-08-24 20:33:05 +02:00
|
|
|
context_menu_->addSeparator();
|
2014-02-07 16:34:20 +01:00
|
|
|
context_menu_->addAction(IconLoader::Load("download"), tr("Cover Manager"),
|
|
|
|
this, SLOT(ShowCoverManager()));
|
|
|
|
context_menu_->addAction(IconLoader::Load("configure"), tr("Configure..."),
|
2012-08-24 20:33:05 +02:00
|
|
|
this, SLOT(ShowSettingsDialog()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only show some actions if there are real songs selected
|
|
|
|
bool songs_selected = false;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& index : model()->selected_indexes()) {
|
2012-08-24 20:33:05 +02:00
|
|
|
const int type = index.data(LibraryModel::Role_Type).toInt();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (type == LibraryItem::Type_Song || type == LibraryItem::Type_Container) {
|
2012-08-24 20:33:05 +02:00
|
|
|
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;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& index : model()->selected_indexes()) {
|
2012-08-24 20:33:05 +02:00
|
|
|
if (index.model() == library_sort_model_) {
|
|
|
|
library_indexes << library_sort_model_->mapToSource(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the library for the songs for these indexes.
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& song : library_model_->GetChildSongs(library_indexes)) {
|
2012-08-24 20:33:05 +02:00
|
|
|
QDesktopServices::openUrl(
|
2014-02-07 16:34:20 +01:00
|
|
|
QUrl(QString(kDriveEditFileUrl).arg(song.url().path())));
|
2012-08-24 20:33:05 +02:00
|
|
|
}
|
|
|
|
}
|