summaryrefslogtreecommitdiffstats
path: root/apps/files_external/js/google.js
blob: ede5fb2e775a54a3cc544933fdd177d16ebb6bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$(document).ready(function() {

	$('#externalStorage tbody tr').each(function() {
		if ($(this).find('.backend').data('class') == 'OC_Filestorage_Google') {
			var token = $(this).find('[data-parameter="token"]');
			var token_secret = $(this).find('[data-parameter="token_secret"]');
			if ($(token).val() == '' && $(token).val() == '') {
				$(this).find('.configuration').append('<a class="button google">Grant access</a>');
			} else  {
				var params = {};
				window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
					params[key] = value;
				});
				var access = true;
				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);
							$(token_secret).val(result.access_token_secret);
							OC.MountConfig.saveStorage(tr);
						} else {
							access = false;
							OC.dialogs.alert(result.data.message, 'Error configuring Google Drive storage');
						}
					});
				}
				if (access && $(this).find('.configuration #granted').length == 0) {
					$(this).find('.configuration').append('<span id="granted" style="padding-left:0.5em;">Access granted</span>');
				}
			}
		}
	});

	$('.google').live('click', function(event) {
		event.preventDefault();
		var tr = $(this).parent().parent();
		var token = $(this).parent().find('[data-parameter="token"]');
		var token_secret = $(this).parent().find('[data-parameter="token_secret"]');
		$.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 1, callback: window.location.href }, function(result) {
			if (result && result.status == 'success') {
				$(token).val(result.data.request_token);
				$(token_secret).val(result.data.request_token_secret);
				OC.MountConfig.saveStorage(tr);
				window.location = result.data.url;
			} else {
				OC.dialogs.alert(result.data.message, 'Error configuring Google Drive storage');
			}
		});
	});

});