define(["jquery", "async-runner"], function($, asyncTaskRunner) { // Dependencies var core = undefined; var fileManager = undefined; var client = undefined; var authenticated = false; var dropboxHelper = {}; // 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.js", dataType : "script", timeout : AJAX_TIMEOUT }).done(function() { asyncTask.success(); }).fail(function() { asyncTask.error(); }); }; asyncTask.onSuccess = function() { client = new Dropbox.Client({ key: DROPBOX_APP_KEY, secret: DROPBOX_APP_SECRET }); client.authDriver(new Dropbox.Drivers.Popup({ receiverUrl: "http://localhost/dropbox-oauth-receiver.html", rememberUser: true })); callback(); }; asyncTask.onError = function() { core.setOffline(); callback(); }; asyncTaskRunner.addTask(asyncTask); } // Try to authenticate with Oauth function authenticate(callback, immediate) { callback = callback || core.doNothing; if (immediate === undefined) { immediate = true; } connect(function() { if (client === undefined) { callback(); return; } var asyncTask = {}; asyncTask.run = function() { if (authenticated === true) { asyncTask.success(); 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); }); } dropboxHelper.upload = function(path, content, callback) { callback = callback || core.doNothing; authenticate(function() { if (client === undefined) { 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; asyncTask.success(); return; } // Handle error if(error.status === Dropbox.ApiError.INVALID_PARAM) { error = 'Could not upload document into path "' + path + '".'; } handleError(error, asyncTask, callback); }); }; asyncTask.onSuccess = function() { callback(fileSyncIndex); }; 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; 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:"\|?\*]+$/)) { core.showError('"' + path + '" contains invalid characters.'); return undefined; } if(path.indexOf("/") !== 0) { return "/" + path; } return path; }; return dropboxHelper; });