From 51ba5930afe79254b9138cda75c5e2e2693f8795 Mon Sep 17 00:00:00 2001 From: benweet Date: Tue, 16 Apr 2013 16:02:24 +0100 Subject: [PATCH] Support for Dropbox publishing --- index.html | 11 +- js/async-runner.js | 8 +- js/config.js | 3 +- js/dropbox-helper.js | 490 ++++++++++++++++++++--------------------- js/dropbox-provider.js | 36 +++ js/file-manager.js | 8 +- js/publisher.js | 3 +- js/synchronizer.js | 26 +-- 8 files changed, 315 insertions(+), 270 deletions(-) create mode 100644 js/dropbox-provider.js diff --git a/index.html b/index.html index 855170c4..07ee38bb 100644 --- a/index.html +++ b/index.html @@ -88,6 +88,8 @@ @@ -269,7 +271,7 @@ +
Format
diff --git a/js/async-runner.js b/js/async-runner.js index f4302018..3d1895a0 100644 --- a/js/async-runner.js +++ b/js/async-runner.js @@ -106,10 +106,16 @@ define(["underscore"], function() { asyncTaskRunner.runTask(); }; + // Change current task timeout + asyncTaskRunner.setCurrentTaskTimeout = function(timeout) { + if(currentTask !== undefined) { + currentTask.timeout = timeout; + } + }; + asyncTaskRunner.init = function(coreModule) { core = coreModule; }; return asyncTaskRunner; }); - diff --git a/js/config.js b/js/config.js index 8bdbabff..6ef60f86 100644 --- a/js/config.js +++ b/js/config.js @@ -15,8 +15,9 @@ var USER_IDLE_THRESHOLD = 300000; var SYNC_PROVIDER_GDRIVE = "sync.gdrive."; var SYNC_PROVIDER_DROPBOX = "sync.dropbox."; var PROVIDER_TYPE_PUBLISH_FLAG = 1; -var PROVIDER_GITHUB = "github"; var PROVIDER_BLOGGER = "blogger"; +var PROVIDER_DROPBOX = "dropbox"; +var PROVIDER_GITHUB = "github"; // Use by Google's client.js var delayedFunction = undefined; diff --git a/js/dropbox-helper.js b/js/dropbox-helper.js index b60aef2e..e8786a88 100644 --- a/js/dropbox-helper.js +++ b/js/dropbox-helper.js @@ -12,28 +12,20 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) { // Try to connect dropbox by downloading client.js function connect(callback) { callback = callback || core.doNothing; - var asyncTask = {}; - asyncTask.run = function() { - if(core.isOffline === true) { - client = undefined; - core.showMessage("Operation not available in offline mode."); - asyncTask.error(); - return; - } - if (client !== undefined) { - asyncTask.success(); - return; - } - $.ajax({ - url : "lib/dropbox.min.js", - dataType : "script", timeout : AJAX_TIMEOUT - }).done(function() { - asyncTask.success(); - }).fail(function() { - asyncTask.error(); - }); - }; - asyncTask.onSuccess = function() { + if(core.isOffline === true) { + client = undefined; + core.showMessage("Operation not available in offline mode."); + callback(true); + return; + } + if (client !== undefined) { + callback(); + return; + } + $.ajax({ + url : "lib/dropbox.min.js", + dataType : "script", timeout : AJAX_TIMEOUT + }).done(function() { client = new Dropbox.Client({ key: DROPBOX_APP_KEY, secret: DROPBOX_APP_SECRET @@ -43,12 +35,10 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) { rememberUser: true })); callback(); - }; - asyncTask.onError = function() { + }).fail(function() { core.setOffline(); - callback(); - }; - asyncTaskRunner.addTask(asyncTask); + callback(true); + }); } // Try to authenticate with Oauth @@ -57,61 +47,51 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) { if (immediate === undefined) { immediate = true; } - connect(function() { - if (client === undefined) { + connect(function(error) { + if (error) { + callback(error); + return; + } + if (authenticated === true) { callback(); return; } - - var asyncTask = {}; - asyncTask.run = function() { - if (authenticated === true) { - asyncTask.success(); + if (immediate === false) { + core.showMessage("Please make sure the Dropbox authorization popup is not blocked by your browser."); + asyncTaskRunner.setCurrentTaskTimeout(AUTH_POPUP_TIMEOUT); + } + client.authenticate({interactive: !immediate}, function(error, client) { + if (client.authState === Dropbox.Client.DONE) { + callback(); return; } - if (immediate === false) { - core.showMessage("Please make sure the Dropbox authorization popup is not blocked by your browser."); - } - client.authenticate({interactive: !immediate}, function(error, client) { - if (client.authState !== Dropbox.Client.DONE) { - // Handle error - asyncTask.error(); - return; - } - asyncTask.success(); - }); - }; - asyncTask.onSuccess = function() { - callback(); - }; - asyncTask.onError = function() { // If immediate did not work retry without immediate flag if (client !== undefined && immediate === true) { authenticate(callback, false); return; } - callback(); - }; - asyncTaskRunner.addTask(asyncTask); + // Notify error + callback(true); + }); }); } dropboxHelper.upload = function(path, content, callback) { callback = callback || core.doNothing; - authenticate(function() { - if (client === undefined) { - callback(); - return; - } + var syncIndex = undefined; + var asyncTask = {}; + asyncTask.run = function() { + authenticate(function(error) { + if (error) { + handleError(error, asyncTask, callback); + return; + } - var fileSyncIndex = undefined; - var asyncTask = {}; - asyncTask.run = function() { client.writeFile(path, content, function(error, stat) { if (!error) { - fileSyncIndex = SYNC_PROVIDER_DROPBOX + encodeURIComponent(stat.path.toLowerCase()); - localStorage[fileSyncIndex + ".version"] = stat.versionTag; + syncIndex = SYNC_PROVIDER_DROPBOX + encodeURIComponent(stat.path.toLowerCase()); + localStorage[syncIndex + ".version"] = stat.versionTag; asyncTask.success(); return; } @@ -121,154 +101,168 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) { } handleError(error, asyncTask, callback); }); - }; - asyncTask.onSuccess = function() { - callback(fileSyncIndex); - }; - asyncTask.onError = function() { - callback(); - }; - asyncTaskRunner.addTask(asyncTask); - }); + }); + }; + asyncTask.onSuccess = function() { + callback(undefined, syncIndex); + }; + asyncTask.onError = function() { + callback(true); + }; + asyncTaskRunner.addTask(asyncTask); }; dropboxHelper.checkUpdates = function(lastChangeId, callback) { callback = callback || core.doNothing; - authenticate(function() { - if (client === undefined) { - callback(); - return; - } - - var changes = []; - var newChangeId = lastChangeId || 0; + var changes = []; + var newChangeId = lastChangeId || 0; + var asyncTask = {}; + asyncTask.run = function() { function retrievePageOfChanges(changeId) { - var shouldPullAgain = false; - var asyncTask = {}; - asyncTask.run = function() { - client.pullChanges(changeId, function(error, pullChanges) { - if (pullChanges && pullChanges.cursorTag) { - // Retrieve success - newChangeId = pullChanges.cursor(); - shouldPullAgain = pullChanges.shouldPullAgain; - if(pullChanges.changes !== undefined) { - for(var i=0; i