From 7ca7ecbe99d3c59e152dc02ebc22b113a1136e30 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Fri, 24 Aug 2012 21:26:10 +0100 Subject: [PATCH] When the user opens the Google Drive section for the first time, open the settings dialog to prompt them to login instead of opening a browser to start OAuth straight away. Also fix a bug where the user's email address would be lost on subsequent authentications. --- src/internet/googledriveservice.cpp | 19 ++++++++++++++----- src/internet/googledriveservice.h | 1 + 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/internet/googledriveservice.cpp b/src/internet/googledriveservice.cpp index 740e35a14..8e6fe95d2 100644 --- a/src/internet/googledriveservice.cpp +++ b/src/internet/googledriveservice.cpp @@ -72,7 +72,9 @@ QStandardItem* GoogleDriveService::CreateRootItem() { void GoogleDriveService::LazyPopulate(QStandardItem* item) { switch (item->data(InternetModel::Role_Type).toInt()) { case InternetModel::Type_Service: - if (!client_->is_authenticated()) { + if (refresh_token().isEmpty()) { + ShowSettingsDialog(); + } else if (!client_->is_authenticated()) { Connect(); } library_model_->Init(); @@ -84,12 +86,15 @@ void GoogleDriveService::LazyPopulate(QStandardItem* item) { } } -void GoogleDriveService::Connect() { +QString GoogleDriveService::refresh_token() const { QSettings s; s.beginGroup(kSettingsGroup); - google_drive::ConnectResponse* response = - client_->Connect(s.value("refresh_token").toString()); + return s.value("refresh_token").toString(); +} + +void GoogleDriveService::Connect() { + google_drive::ConnectResponse* response = client_->Connect(refresh_token()); NewClosure(response, SIGNAL(Finished()), this, SLOT(ConnectFinished(google_drive::ConnectResponse*)), response); @@ -122,7 +127,11 @@ void GoogleDriveService::ConnectFinished(google_drive::ConnectResponse* response QSettings s; s.beginGroup(kSettingsGroup); s.setValue("refresh_token", response->refresh_token()); - s.setValue("user_email", response->user_email()); + + 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()); + } emit Connected(); diff --git a/src/internet/googledriveservice.h b/src/internet/googledriveservice.h index 59327a23a..d80fd305e 100644 --- a/src/internet/googledriveservice.h +++ b/src/internet/googledriveservice.h @@ -30,6 +30,7 @@ class GoogleDriveService : public InternetService { static const char* kSettingsGroup; google_drive::Client* client() const { return client_; } + QString refresh_token() const; QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item);