From d65cb7ae0954e89de0a1b56170ffab683c39e3cc Mon Sep 17 00:00:00 2001
From: Michael Gapczynski <mtgap@owncloud.com>
Date: Wed, 2 Jan 2013 11:54:21 -0500
Subject: Fix 'Grant Access' button for multiple Google Drive mount points and
 prevent multiple scripts being added

---
 apps/files_external/appinfo/update.php     |  5 +++++
 apps/files_external/js/google.js           | 26 ++++++++++++++++++--------
 apps/files_external/templates/settings.php |  5 ++---
 3 files changed, 25 insertions(+), 11 deletions(-)
 create mode 100644 apps/files_external/appinfo/update.php

diff --git a/apps/files_external/appinfo/update.php b/apps/files_external/appinfo/update.php
new file mode 100644
index 00000000000..15d7e4168ee
--- /dev/null
+++ b/apps/files_external/appinfo/update.php
@@ -0,0 +1,5 @@
+<?php
+$installedVersion = OCP\Config::getAppValue('files_external', 'installed_version');
+if (version_compare($installedVersion, '0.2', '<')) {
+	
+}
\ No newline at end of file
diff --git a/apps/files_external/js/google.js b/apps/files_external/js/google.js
index a3b3e9cb3d2..89daa0078e8 100644
--- a/apps/files_external/js/google.js
+++ b/apps/files_external/js/google.js
@@ -1,18 +1,27 @@
 $(document).ready(function() {
 
-	$('#externalStorage tbody tr.OC_Filestorage_Google').each(function() {
-		var configured = $(this).find('[data-parameter="configured"]');
+	$('#externalStorage tbody tr.OC_Filestorage_Google').each(function(index, tr) {
+		setupGoogleRow(tr);
+	});
+
+	$('#selectBackend').live('change', function() {
+		if ($(this).val() == 'OC_Filestorage_Google') {
+			setupGoogleRow($('#externalStorage tbody>tr:last').prev('tr'));
+		}
+	});
+
+	function setupGoogleRow(tr) {
+		var configured = $(tr).find('[data-parameter="configured"]');
 		if ($(configured).val() == 'true') {
-			$(this).find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
+			$(tr).find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
 		} else {
-			var token = $(this).find('[data-parameter="token"]');
-			var token_secret = $(this).find('[data-parameter="token_secret"]');
+			var token = $(tr).find('[data-parameter="token"]');
+			var token_secret = $(tr).find('[data-parameter="token_secret"]');
 			var params = {};
 			window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
 				params[key] = value;
 			});
 			if (params['oauth_token'] !== undefined && params['oauth_verifier'] !== undefined && decodeURIComponent(params['oauth_token']) == $(token).val()) {
-				var tr = $(this);
 				$.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 2, oauth_verifier: params['oauth_verifier'], request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) {
 					if (result && result.status == 'success') {
 						$(token).val(result.access_token);
@@ -22,13 +31,14 @@ $(document).ready(function() {
 						$(tr).find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
 					} else {
 						OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring Google Drive storage'));
+						onGoogleInputsChange(tr);
 					}
 				});
 			} else {
-				onGoogleInputsChange(this);
+				onGoogleInputsChange(tr);
 			}
 		}
-	});
+	}
 
 	$('#externalStorage tbody tr.OC_Filestorage_Google td').live('paste', function() {
 		var tr = $(this).parent();
diff --git a/apps/files_external/templates/settings.php b/apps/files_external/templates/settings.php
index 9702ebfb4c7..6f22de392fa 100644
--- a/apps/files_external/templates/settings.php
+++ b/apps/files_external/templates/settings.php
@@ -73,9 +73,8 @@
 									<?php endif; ?>
 								<?php endif; ?>
 							<?php endforeach; ?>
-							<?php if (isset($_['backends'][$mount['class']]['custom'])): ?>
-								<?php OCP\Util::addScript('files_external',
-														  $_['backends'][$mount['class']]['custom']); ?>
+							<?php if (isset($_['backends'][$mount['class']]['custom']) && !in_array('files_external/js/'.$_['backends'][$mount['class']]['custom'], \OC_Util::$scripts)): ?>
+								<?php OCP\Util::addScript('files_external', $_['backends'][$mount['class']]['custom']); ?>
 							<?php endif; ?>
 						<?php endif; ?>
 					</td>
-- 
cgit v1.2.3