/*\ title: $:/plugins/dropbox/dropbox.js type: application/javascript module-type: startup Startup the Dropbox integration plugin \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; // Obfuscated API key var apiKey = "m+qwjj8wFRA=|1TSoitGS9Nz2RTwv+jrUJnsAj0yy57NhQJ4TkZ/+Hw=="; // Tiddler titles var titleIsLoggedIn = "$:/plugins/dropbox/IsLoggedIn", titleUserName = "$:/plugins/dropbox/UserName"; $tw.plugins.dropbox = { client: null // Dropbox.js client object }; // Error handling $tw.plugins.dropbox.showError = function(error) { alert("Dropbox error: " + error); console.log("Dropbox error: " + error); }; // Authenticate $tw.plugins.dropbox.login = function() { $tw.plugins.dropbox.client.authenticate(function(error, client) { if(error) { return $tw.plugins.dropbox.showError(error); } // Mark us as logged in $tw.wiki.addTiddler({title: titleIsLoggedIn, text: "yes"}); // Get user information $tw.plugins.dropbox.getUserInfo(); // Get tiddler file metadata $tw.plugins.dropbox.loadTiddlerFiles("/My TiddlyWiki/tiddlers",function() { console.log("Loaded all tiddlers",$tw.wiki.tiddlers); }); }); }; // Get user information $tw.plugins.dropbox.getUserInfo = function() { $tw.plugins.dropbox.client.getUserInfo(function(error,userInfo) { if(error) { return $tw.plugins.dropbox.showError(error); } // Save the username $tw.wiki.addTiddler({title: titleUserName, text: userInfo.name}); }); }; // Logout $tw.plugins.dropbox.logout = function() { $tw.plugins.dropbox.client.signOut(function(error) { if(error) { return $tw.plugins.dropbox.showError(error); } // Mark us as logged out $tw.wiki.deleteTiddler(titleUserName); $tw.wiki.addTiddler({title: titleIsLoggedIn, text: "no"}); }); }; // Load tiddler files from a folder $tw.plugins.dropbox.loadTiddlerFiles = function(path,callback) { // First get the list of tiddler files $tw.plugins.dropbox.client.stat(path,{readDir: true},function(error,stat,stats) { if(error) { return $tw.plugins.dropbox.showError(error); } // Process the files via an asynchronous queue, with concurrency set to 2 at a time var q = async.queue(function(task,callback) { $tw.plugins.dropbox.loadTiddlerFile(task.path,task.type,task.stats,callback); }, 2); // Call the callback when we've processed all the files q.drain = callback; // Push a task onto the queue for each file to be processed for(var s=0; s> 18; // 16515072 = (2^6 - 1) << 18 b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12 c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6 d = chunk & 63; // 63 = 2^6 - 1 // Convert the raw binary segments to the appropriate ASCII encoding base64.push(charmap[a],charmap[b],charmap[c],charmap[d]); } // Deal with the remaining bytes and padding if(byteRemainder === 1) { chunk = data[mainLength]; a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2 // Set the 4 least significant bits to zero b = (chunk & 3) << 4; // 3 = 2^2 - 1 base64.push(charmap[a],charmap[b],"=="); } else if(byteRemainder === 2) { chunk = (data[mainLength] << 8) | data[mainLength + 1]; a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10 b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4 // Set the 2 least significant bits to zero c = (chunk & 15) << 2; // 15 = 2^4 - 1 base64.push(charmap[a],charmap[b],charmap[c],"="); } return base64.join(""); }; exports.startup = function() { if(!$tw.browser) { return; } // Mark us as not logged in $tw.wiki.addTiddler({title: titleIsLoggedIn, text: "no"}); // Initialise Dropbox for sandbox access $tw.plugins.dropbox.client = new Dropbox.Client({key: apiKey, sandbox: true}); // Use the basic redirection authentication driver $tw.plugins.dropbox.client.authDriver(new Dropbox.Drivers.Redirect({rememberUser: true})); // Authenticate ourselves $tw.plugins.dropbox.login(); }; })();