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; 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 }); client.authDriver(new Dropbox.Drivers.Popup({ receiverUrl: BASE_URL + "dropbox-oauth-receiver.html", rememberUser: true })); callback(); }).fail(function() { core.setOffline(); callback(true); }); } // Try to authenticate with Oauth function authenticate(callback, immediate) { callback = callback || core.doNothing; if (immediate === undefined) { immediate = true; } connect(function(error) { if (error) { callback(error); return; } if (authenticated === true) { callback(); return; } 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 did not work retry without immediate flag if (client !== undefined && immediate === true) { authenticate(callback, false); return; } // Notify error callback(true); }); }); } dropboxHelper.upload = function(path, content, callback) { callback = callback || core.doNothing; var syncIndex = undefined; var asyncTask = {}; asyncTask.run = function() { authenticate(function(error) { if (error) { handleError(error, asyncTask, callback); return; } client.writeFile(path, content, function(error, stat) { if (!error) { syncIndex = SYNC_PROVIDER_DROPBOX + encodeURIComponent(stat.path.toLowerCase()); localStorage[syncIndex + ".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(undefined, syncIndex); }; asyncTask.onError = function() { callback(true); }; asyncTaskRunner.addTask(asyncTask); }; dropboxHelper.checkUpdates = function(lastChangeId, callback) { callback = callback || core.doNothing; var changes = []; var newChangeId = lastChangeId || 0; var asyncTask = {}; asyncTask.run = function() { function retrievePageOfChanges(changeId) { if(asyncTask.finished === true) { return; } authenticate(function(error) { if (error) { handleError(error, asyncTask, callback); return; } client.pullChanges(changeId, function(error, pullChanges) { if (error) { handleError(error, asyncTask, callback); return; } // Retrieve success newChangeId = pullChanges.cursor(); 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; });