Add down synchronization

This commit is contained in:
benweet 2013-04-01 17:46:48 +01:00
parent fef879fe50
commit 01f824567b
6 changed files with 296 additions and 249 deletions

View File

@ -1 +1 @@
CACHE MANIFEST # v5 CACHE: index.html css/bootstrap.css css/jgrowl.css css/main.css js/async-runner.js js/base64.js js/bootstrap.js js/gdrive.js js/jquery.jgrowl.js js/jquery.js js/jquery.layout.js js/jquery-ui.custom.js js/main.js js/Markdown.Converter.js js/Markdown.Editor.js js/Markdown.Sanitizer.js img/ajax-loader.gif img/gdrive.png img/glyphicons-halflings.png img/glyphicons-halflings-white.png img/stackedit-16.png img/stackedit-32.ico NETWORK: * CACHE MANIFEST # v5 CACHE: index.html css/bootstrap.css css/jgrowl.css css/main.css js/async-runner.js js/base64.js js/bootstrap.js js/gdrive.js js/jquery.jgrowl.js js/jquery.js js/jquery.layout.js js/jquery-ui.custom.js js/main.js js/Markdown.Converter.js js/Markdown.Editor.js js/Markdown.Sanitizer.js js/synchronizer.js img/ajax-loader.gif img/gdrive.png img/glyphicons-halflings.png img/glyphicons-halflings-white.png img/stackedit-16.png img/stackedit-32.ico NETWORK: *

View File

@ -211,6 +211,13 @@ hr {
background-position: 0 0; background-position: 0 0;
} }
.icon-dropbox {
background-image: url("../img/dropbox.png") !important;
width: 16px;
height: 16px;
background-position: 0 0;
}
.icon-spinner { .icon-spinner {
background-image: url("../img/ajax-loader.gif"); background-image: url("../img/ajax-loader.gif");
width: 43px; width: 43px;

View File

@ -22,6 +22,7 @@
<script type="text/javascript" src="js/main.js"></script> <script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/async-runner.js"></script> <script type="text/javascript" src="js/async-runner.js"></script>
<script type="text/javascript" src="js/gdrive.js"></script> <script type="text/javascript" src="js/gdrive.js"></script>
<script type="text/javascript" src="js/synchronizer.js"></script>
<script type="text/javascript" src="js/config.custo.js"></script> <script type="text/javascript" src="js/config.custo.js"></script>
<script> <script>
(function(i, s, o, g, r, a, m) { (function(i, s, o, g, r, a, m) {
@ -71,6 +72,9 @@
<li><a class="action-upload-gdrive" href="#" <li><a class="action-upload-gdrive" href="#"
title="Save the current file on Google Drive and synchonize it"><i title="Save the current file on Google Drive and synchonize it"><i
class="icon-gdrive"></i> Synchronize on Google Drive</a></li> class="icon-gdrive"></i> Synchronize on Google Drive</a></li>
<li><a class="action-upload-dropbox" href="#"
title="Save the current file on Dropbox and synchonize it"><i
class="icon-dropbox"></i> Synchronize on Dropbox</a></li>
<li><a href="#" <li><a href="#"
title="Change the current file synchronized locations" title="Change the current file synchronized locations"
data-toggle="modal" data-target="#modal-manage-sync" data-toggle="modal" data-target="#modal-manage-sync"

View File

@ -31,14 +31,12 @@ var gdrive = (function($) {
gdriveDelayedFunction = function() { gdriveDelayedFunction = function() {
asyncTask.success(); asyncTask.success();
}; };
$ $.ajax({
.ajax( url : "https://apis.google.com/js/client.js?onload=runGdriveDelayedFunction",
{ dataType : "script", timeout : AJAX_TIMEOUT
url : "https://apis.google.com/js/client.js?onload=runGdriveDelayedFunction", }).fail(function() {
dataType : "script", timeout : AJAX_TIMEOUT }).fail( asyncTask.error();
function() { });
asyncTask.error();
});
}; };
asyncTask.onSuccess = function() { asyncTask.onSuccess = function() {
gdriveDelayedFunction = undefined; gdriveDelayedFunction = undefined;
@ -76,7 +74,7 @@ var gdrive = (function($) {
return; return;
} }
if (immediate === false) { if (immediate === false) {
showMessage("Please make sure the Google authorization popup is not blocked by your browser..."); showMessage("Please make sure the Google authorization popup is not blocked by your browser.");
} }
gapi.auth.authorize({ 'client_id' : GOOGLE_CLIENT_ID, gapi.auth.authorize({ 'client_id' : GOOGLE_CLIENT_ID,
'scope' : SCOPES, 'immediate' : immediate }, function( 'scope' : SCOPES, 'immediate' : immediate }, function(
@ -106,10 +104,9 @@ var gdrive = (function($) {
}); });
} }
function handleError(response, asyncTask, callback) { function handleError(error, asyncTask, callback) {
var errorMsg = undefined; var errorMsg = undefined;
if (response && response.error) { if (error) {
var error = response.error;
// Try to analyze the error // Try to analyze the error
if (error.code >= 500 && error.code < 600) { if (error.code >= 500 && error.code < 600) {
errorMsg = "Google Drive is not accessible."; errorMsg = "Google Drive is not accessible.";
@ -119,7 +116,7 @@ var gdrive = (function($) {
} else if (error.code === 401) { } else if (error.code === 401) {
authenticated = false; authenticated = false;
errorMsg = "Access to Google Drive is not authorized."; errorMsg = "Access to Google Drive is not authorized.";
} else if (error.code === -1) { } else if (error.code <= 0) {
connected = false; connected = false;
authenticated = false; authenticated = false;
onOffline(); onOffline();
@ -190,16 +187,17 @@ var gdrive = (function($) {
asyncTask.success(); asyncTask.success();
return; return;
} }
var error = response.error;
// If file has been removed from Google Drive // If file has been removed from Google Drive
if(fileId !== undefined && response.error.code === 404) { if(error !== undefined && fileId !== undefined && error.code === 404) {
showMessage('"' + title + '" has been removed from Google Drive.'); showMessage('"' + title + '" has been removed from Google Drive.');
fileManager.removeSync(SYNC_PROVIDER_GDRIVE + fileId); fileManager.removeSync(SYNC_PROVIDER_GDRIVE + fileId);
fileManager.updateFileTitles(); fileManager.updateFileTitles();
// Avoid error analyzed by handleError // Avoid error analyzed by handleError
response = undefined; error = undefined;
} }
// Handle error // Handle error
handleError(response, asyncTask, callback); handleError(error, asyncTask, callback);
}); });
}; };
asyncTask.onSuccess = function() { asyncTask.onSuccess = function() {
@ -223,29 +221,28 @@ var gdrive = (function($) {
var nextPageToken = undefined; var nextPageToken = undefined;
var asyncTask = {}; var asyncTask = {};
asyncTask.run = function() { asyncTask.run = function() {
request request.execute(function(response) {
.execute(function(response) { if (response && response.largestChangeId) {
if (response && response.largestChangeId) { // Retrieve success
// Retrieve success newChangeId = response.largestChangeId;
newChangeId = response.largestChangeId; nextPageToken = response.nextPageToken;
nextPageToken = response.nextPageToken; if (response.items !== undefined) {
if (response.items !== undefined) { for ( var i = 0; i < response.items.length; i++) {
for ( var i = 0; i < response.items.length; i++) { var item = response.items[i];
var item = response.items[i]; var etag = localStorage[SYNC_PROVIDER_GDRIVE
var etag = localStorage[SYNC_PROVIDER_GDRIVE + item.fileId + ".etag"];
+ item.fileId + ".etag"]; if (etag
if (etag && (item.deleted === true || item.file.etag != etag)) {
&& (item.deleted === true || item.file.etag != etag)) { changes.push(item);
changes.push(item);
}
} }
} }
asyncTask.success();
return;
} }
// Handle error asyncTask.success();
handleError(response, asyncTask, callback); return;
}); }
// Handle error
handleError(response.error, asyncTask, callback);
});
}; };
asyncTask.onSuccess = function() { asyncTask.onSuccess = function() {
if (nextPageToken !== undefined) { if (nextPageToken !== undefined) {
@ -297,19 +294,22 @@ var gdrive = (function($) {
var asyncTask = {}; var asyncTask = {};
asyncTask.run = function() { asyncTask.run = function() {
var accessToken = gapi.auth.getToken().access_token; var accessToken = gapi.auth.getToken().access_token;
$ $.ajax({
.ajax( url : file.downloadUrl,
{ headers : { "Authorization" : "Bearer "
url : file.downloadUrl, + accessToken }, dataType : "text",
headers : { "Authorization" : "Bearer " timeout : AJAX_TIMEOUT
+ accessToken }, dataType : "text", }).done(function(data, textStatus, jqXHR) {
timeout : AJAX_TIMEOUT }).done( file.content = data;
function(data, textStatus, jqXHR) { asyncTask.success();
file.content = data; }).fail(function(jqXHR) {
asyncTask.success(); var error = {
}).fail(function() { code: jqXHR.status,
asyncTask.error(); message: jqXHR.statusText
}); };
// Handle error
handleError(error, asyncTask, callback);
});
}; };
asyncTask.onSuccess = function() { asyncTask.onSuccess = function() {
gdrive.downloadContent(objects, callback, result); gdrive.downloadContent(objects, callback, result);

View File

@ -23,19 +23,20 @@ var CHECK_ONLINE_PERIOD = 60000;
var offline = false; var offline = false;
var offlineTime = currentTime; var offlineTime = currentTime;
function onOffline() { function onOffline() {
offline = true;
offlineTime = currentTime; offlineTime = currentTime;
if ($(".msg-offline").length === 0) if(offline === false) {
offline = true;
showMessage("You are offline.", "icon-exclamation-sign msg-offline", { showMessage("You are offline.", "icon-exclamation-sign msg-offline", {
sticky : true, close : function() { sticky : true, close : function() {
showMessage("You are back online!", "icon-signal"); showMessage("You are back online!", "icon-signal");
} }); } });
}
} }
function onOnline() { function onOnline() {
offline = false;
$(".msg-offline").parents(".jGrowl-notification").trigger( $(".msg-offline").parents(".jGrowl-notification").trigger(
'jGrowl.beforeClose'); 'jGrowl.beforeClose');
offline = false;
} }
function checkOnline() { function checkOnline() {
@ -52,185 +53,7 @@ function checkOnline() {
} }
} }
var SYNC_DOWN_PERIOD = 60000; var DEFAULT_FILE_TITLE = "Filename";
var SYNC_PROVIDER_GDRIVE = "sync.gdrive.";
var syncGoogleDrive = false;
var synchronizer = (function($) {
var synchronizer = {};
// A synchronization queue containing fileIndex that has to be synchronized
var syncQueue = undefined;
synchronizer.init = function() {
syncQueue = ";";
// Load the queue from localStorage in case a previous synchronization
// was aborted
if (localStorage["sync.queue"]) {
syncQueue = localStorage["sync.queue"];
}
if (localStorage["sync.current"]) {
this.addFile(localStorage["sync.current"]);
}
};
// Add a file to the synchronization queue
synchronizer.addFile = function(fileIndex) {
if (syncQueue.indexOf(";" + fileIndex + ";") === -1) {
syncQueue += fileIndex + ";";
localStorage["sync.queue"] = syncQueue;
}
};
// Recursive function to upload a single file on multiple locations
function fileUp(fileSyncIndexList, content, title) {
if (fileSyncIndexList.length === 0) {
localStorage.removeItem("sync.current");
uploadRunning = false;
// run the next file synchronization
synchronizer.syncUp();
return;
}
var fileSyncIndex = fileSyncIndexList.pop();
// Try to find the provider
if (fileSyncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
var id = fileSyncIndex.substring(SYNC_PROVIDER_GDRIVE.length);
gdrive.updateFile(id, title, content, function(result) {
if (result === undefined && offline === true) {
// If we detect offline mode we put the fileIndex back in
// the queue
synchronizer.addFile(localStorage["sync.current"]);
localStorage.removeItem("sync.current");
uploadRunning = false;
return;
}
fileUp(fileSyncIndexList, content, title);
});
} else {
fileUp(fileSyncIndexList, content, title);
}
}
var uploadRunning = false;
synchronizer.syncUp = function() {
// If syncUp is already running or nothing to synchronize or offline
if (uploadRunning || syncQueue.length === 1 || offline) {
return;
}
uploadRunning = true;
// Dequeue the fileIndex
var separatorPos = syncQueue.indexOf(";", 1);
var fileIndex = syncQueue.substring(1, separatorPos);
localStorage["sync.current"] = fileIndex;
syncQueue = syncQueue.substring(separatorPos);
localStorage["sync.queue"] = syncQueue;
var content = localStorage[fileIndex + ".content"];
var title = localStorage[fileIndex + ".title"];
// Parse the list of synchronized locations associated to the file
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
fileUp(fileSyncIndexList, content, title);
};
function syncDownGdrive(callback) {
if (syncGoogleDrive === false) {
callback();
return;
}
var lastChangeId = parseInt(localStorage[SYNC_PROVIDER_GDRIVE
+ "lastChangeId"]);
gdrive.checkUpdates(lastChangeId, function(changes, newChangeId) {
if (changes === undefined) {
callback();
return;
}
gdrive.downloadContent(changes, function(changes) {
if (changes === undefined) {
callback();
return;
}
var updateFileTitles = false;
for ( var i = 0; i < changes.length; i++) {
var change = changes[i];
var fileSyncIndex = SYNC_PROVIDER_GDRIVE + change.fileId;
var fileIndexList = localStorage["file.list"].split(";");
var fileIndex = undefined;
// Look for local file associated to this synchronized location
for ( var i = 1; i < fileIndexList.length - 1; i++) {
var tempFileIndex = fileIndexList[i];
var sync = localStorage[tempFileIndex + ".sync"];
if (sync.indexOf(";" + fileSyncIndex + ";") !== -1) {
fileIndex = tempFileIndex;
break;
}
}
// No file corresponding (this should never happen...)
if(fileIndex === undefined) {
// We can remove the stored etag
localStorage.removeItem(fileSyncIndex + ".etag");
continue;
}
var title = localStorage[fileIndex + ".title"];
// File deleted
if (change.deleted === true) {
fileManager.removeSync(fileSyncIndex);
updateFileTitles = true;
showMessage('"' + title + '" has been removed from Google Drive.');
continue;
}
var content = localStorage[fileIndex + ".content"];
var file = change.file;
// File title changed
if(title != file.title) {
localStorage[fileIndex + ".title"] = file.title;
updateFileTitles = true;
showMessage('"' + title + '" has been renamed to "' + file.title + '" on Google Drive.');
}
// File content changed
if(content != file.content) {
localStorage[fileIndex + ".content"] = file.content;
showMessage('"' + file.title + '" has been updated from Google Drive.');
if(fileIndex == localStorage["file.current"]) {
updateFileTitles = false; // Done by next function
fileManager.selectFile();
}
}
// Update file etag
localStorage[fileSyncIndex + ".etag"] = file.etag;
// Synchronize file to others locations
synchronizer.addFile(fileIndex);
}
if(updateFileTitles) {
fileManager.updateFileTitles();
}
localStorage[SYNC_PROVIDER_GDRIVE
+ "lastChangeId"] = newChangeId;
callback();
});
});
}
var downloadRunning = false;
var lastSyncDown = 0;
synchronizer.syncDown = function() {
// If syncDown is already running or timeout is not reached or offline
if (downloadRunning || lastSyncDown + SYNC_DOWN_PERIOD > currentTime
|| offline) {
return;
}
downloadRunning = true;
lastSyncDown = currentTime;
syncDownGdrive(function() {
downloadRunning = false;
});
};
return synchronizer;
})(jQuery);
var fileManager = (function($) { var fileManager = (function($) {
var fileManager = {}; var fileManager = {};
@ -245,16 +68,15 @@ var fileManager = (function($) {
window.setInterval(function() { window.setInterval(function() {
currentTime = new Date().getTime(); currentTime = new Date().getTime();
fileManager.saveFile(); fileManager.saveFile();
synchronizer.syncDown(); synchronizer.sync();
synchronizer.syncUp();
asyncTaskRunner.runTask(); asyncTaskRunner.runTask();
checkOnline(); checkOnline();
}, 1000); }, 1000);
$(".action-create-file").click(function() { $(".action-create-file").click(function() {
fileManager.saveFile(); fileManager.saveFile();
fileManager.createFile(); var fileIndex = fileManager.createFile();
fileManager.selectFile(); fileManager.selectFile(fileIndex);
$("#file-title").click(); $("#file-title").click();
}); });
$(".action-remove-file").click(function() { $(".action-remove-file").click(function() {
@ -294,10 +116,13 @@ var fileManager = (function($) {
window.open(uriContent, 'file'); window.open(uriContent, 'file');
}); });
$(".action-upload-gdrive").click(uploadGdrive); $(".action-upload-gdrive").click(uploadGdrive);
$(".action-upload-dropbox").click(function() {
showMessage("Sorry, Dropbox synchronization is not yet available.");
});
}; };
var fileDescList = []; var fileDescList = [];
fileManager.selectFile = function() { fileManager.selectFile = function(fileIndex) {
// If file system does not exist // If file system does not exist
if (!localStorage["file.counter"] || !localStorage["file.list"]) { if (!localStorage["file.counter"] || !localStorage["file.list"]) {
localStorage.clear(); localStorage.clear();
@ -306,8 +131,14 @@ var fileManager = (function($) {
} }
// If no file create one // If no file create one
if (localStorage["file.list"].length === 1) { if (localStorage["file.list"].length === 1) {
this.createFile(); fileIndex = this.createFile();
} }
fileIndex = fileIndex || localStorage["file.current"];
if(fileIndex !== undefined) {
localStorage["file.current"] = fileIndex;
}
// Update the file titles // Update the file titles
this.updateFileTitles(); this.updateFileTitles();
// Update the editor // Update the editor
@ -318,20 +149,33 @@ var fileManager = (function($) {
}); });
}; };
fileManager.createFile = function(title) { fileManager.createFile = function(title, content) {
content = content || "";
if (!title) { if (!title) {
title = "Filename"; // Create a file title
title = DEFAULT_FILE_TITLE;
function exists(title) {
for ( var i = 0; i < fileDescList.length; i++) {
if(fileDescList[i].title == title) {
return true;
}
}
}
var indicator = 2;
while(exists(title)) {
title = DEFAULT_FILE_TITLE + indicator++;
}
} }
// Create the fileIndex // Create the fileIndex
var fileCounter = parseInt(localStorage["file.counter"]); var fileCounter = parseInt(localStorage["file.counter"]);
var fileIndex = "file." + fileCounter; var fileIndex = "file." + fileCounter;
// Create the file in the localStorage // Create the file in the localStorage
localStorage[fileIndex + ".content"] = ""; localStorage[fileIndex + ".content"] = content;
localStorage[fileIndex + ".title"] = title; localStorage[fileIndex + ".title"] = title;
localStorage[fileIndex + ".sync"] = ";"; localStorage[fileIndex + ".sync"] = ";";
localStorage["file.counter"] = fileCounter + 1; localStorage["file.counter"] = fileCounter + 1;
localStorage["file.list"] += fileIndex + ";"; localStorage["file.list"] += fileIndex + ";";
localStorage["file.current"] = fileIndex; return fileIndex;
}; };
fileManager.deleteFile = function() { fileManager.deleteFile = function() {
@ -357,7 +201,7 @@ var fileManager = (function($) {
var content = $("#wmd-input").val(); var content = $("#wmd-input").val();
var fileIndex = localStorage["file.current"]; var fileIndex = localStorage["file.current"];
localStorage[fileIndex + ".content"] = content; localStorage[fileIndex + ".content"] = content;
synchronizer.addFile(fileIndex); synchronizer.addFileForUpload(fileIndex);
save = false; save = false;
} }
}; };
@ -379,9 +223,11 @@ var fileManager = (function($) {
return 0; return 0;
}); });
var fileIndex = localStorage["file.current"];
// If no default file take first one // If no default file take first one
if (!localStorage["file.current"]) { if (!fileIndex) {
localStorage["file.current"] = fileDescList[0].index; fileIndex = fileDescList[0].index;
localStorage["file.current"] = fileIndex;
} }
syncGoogleDrive = false; syncGoogleDrive = false;
@ -395,15 +241,15 @@ var fileManager = (function($) {
return result; return result;
} }
// Update the the file title and the file selector // Update the file title
var fileIndex = localStorage["file.current"];
var title = localStorage[fileIndex + ".title"]; var title = localStorage[fileIndex + ".title"];
document.title = "StackEdit - " + title; document.title = "StackEdit - " + title;
$("#file-title").html(composeTitle(fileIndex)); $("#file-title").html(composeTitle(fileIndex));
$(".file-title").text(title); $(".file-title").text(title);
$("#file-title-input").val(title); $("#file-title-input").val(title);
// Update the file selector
$("#file-selector").empty(); $("#file-selector").empty();
for ( var i = 0; i < fileDescList.length; i++) { for ( var i = 0; i < fileDescList.length; i++) {
var fileDesc = fileDescList[i]; var fileDesc = fileDescList[i];
var a = $("<a>").html(composeTitle(fileDesc.index)); var a = $("<a>").html(composeTitle(fileDesc.index));
@ -595,11 +441,12 @@ var core = (function($) {
$(function() { $(function() {
// jGrowl configuration
$.jGrowl.defaults.life = 5000; $.jGrowl.defaults.life = 5000;
$.jGrowl.defaults.closer = false; $.jGrowl.defaults.closer = false;
$.jGrowl.defaults.closeTemplate = ''; $.jGrowl.defaults.closeTemplate = '';
$.jGrowl.defaults.position = 'bottom-right'; $.jGrowl.defaults.position = 'bottom-right';
core.init(); core.init();
// listen to online/offline events // listen to online/offline events

189
js/synchronizer.js Normal file
View File

@ -0,0 +1,189 @@
var SYNC_PERIOD = 30000;
var SYNC_PROVIDER_GDRIVE = "sync.gdrive.";
var syncGoogleDrive = false;
var synchronizer = (function() {
var synchronizer = {};
// A synchronization queue containing fileIndex that has to be synchronized
var syncUpQueue = undefined;
synchronizer.init = function() {
syncUpQueue = ";";
// Load the queue from localStorage in case a previous synchronization
// was aborted
if (localStorage["sync.queue"]) {
syncUpQueue = localStorage["sync.queue"];
}
if (localStorage["sync.current"]) {
this.addFileForUpload(localStorage["sync.current"]);
}
};
// Add a file to the synchronization queue
synchronizer.addFileForUpload = function(fileIndex) {
if (syncUpQueue.indexOf(";" + fileIndex + ";") === -1) {
syncUpQueue += fileIndex + ";";
localStorage["sync.queue"] = syncUpQueue;
}
};
// Recursive function to upload a single file on multiple locations
function fileUp(fileSyncIndexList, content, title, callback) {
if (fileSyncIndexList.length === 0) {
localStorage.removeItem("sync.current");
// run the next file synchronization
syncUp(callback);
return;
}
var fileSyncIndex = fileSyncIndexList.pop();
// Try to find the provider
if (fileSyncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
var id = fileSyncIndex.substring(SYNC_PROVIDER_GDRIVE.length);
gdrive.updateFile(id, title, content, function(result) {
if (result === undefined && offline === true) {
// If we detect offline mode we put the fileIndex back in
// the queue
synchronizer.addFileForUpload(localStorage["sync.current"]);
localStorage.removeItem("sync.current");
callback();
return;
}
fileUp(fileSyncIndexList, content, title, callback);
});
} else {
fileUp(fileSyncIndexList, content, title, callback);
}
}
function syncUp(callback) {
// If nothing to synchronize
if (syncUpQueue.length === 1) {
callback();
return;
}
// Dequeue the fileIndex
var separatorPos = syncUpQueue.indexOf(";", 1);
var fileIndex = syncUpQueue.substring(1, separatorPos);
localStorage["sync.current"] = fileIndex;
syncUpQueue = syncUpQueue.substring(separatorPos);
localStorage["sync.queue"] = syncUpQueue;
var content = localStorage[fileIndex + ".content"];
var title = localStorage[fileIndex + ".title"];
// Parse the list of synchronized locations associated to the file
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
fileUp(fileSyncIndexList, content, title, callback);
};
function syncDownGdrive(callback) {
if (syncGoogleDrive === false) {
callback();
return;
}
var lastChangeId = parseInt(localStorage[SYNC_PROVIDER_GDRIVE
+ "lastChangeId"]);
gdrive.checkUpdates(lastChangeId, function(changes, newChangeId) {
if (changes === undefined) {
callback();
return;
}
gdrive.downloadContent(changes, function(changes) {
if (changes === undefined) {
callback();
return;
}
var updateFileTitles = false;
for ( var i = 0; i < changes.length; i++) {
var change = changes[i];
var fileSyncIndex = SYNC_PROVIDER_GDRIVE + change.fileId;
var fileIndexList = localStorage["file.list"].split(";");
var fileIndex = undefined;
// Look for local file associated to this synchronized location
for ( var i = 1; i < fileIndexList.length - 1; i++) {
var tempFileIndex = fileIndexList[i];
var sync = localStorage[tempFileIndex + ".sync"];
if (sync.indexOf(";" + fileSyncIndex + ";") !== -1) {
fileIndex = tempFileIndex;
break;
}
}
// No file corresponding (this should never happen...)
if(fileIndex === undefined) {
// We can remove the stored etag
localStorage.removeItem(fileSyncIndex + ".etag");
continue;
}
var title = localStorage[fileIndex + ".title"];
// File deleted
if (change.deleted === true) {
fileManager.removeSync(fileSyncIndex);
updateFileTitles = true;
showMessage('"' + title + '" has been removed from Google Drive.');
continue;
}
var content = localStorage[fileIndex + ".content"];
var file = change.file;
var titleChanged = title != file.title;
var contentChanged = content != file.content;
// If file is in the upload queue we have a conflict
if ((titleChanged || contentChanged) && syncUpQueue.indexOf(";" + fileIndex + ";") !== -1) {
fileManager.createFile(title + " (backup)", content);
updateFileTitles = true;
showMessage('Conflict detected on "' + title + '". A backup has been created locally.');
}
// If file title changed
if(titleChanged) {
localStorage[fileIndex + ".title"] = file.title;
updateFileTitles = true;
showMessage('"' + title + '" has been renamed to "' + file.title + '" on Google Drive.');
}
// If file content changed
if(contentChanged) {
localStorage[fileIndex + ".content"] = file.content;
showMessage('"' + file.title + '" has been updated from Google Drive.');
if(fileIndex == localStorage["file.current"]) {
updateFileTitles = false; // Done by next function
fileManager.selectFile();
}
}
// Update file etag
localStorage[fileSyncIndex + ".etag"] = file.etag;
// Synchronize file to others locations
synchronizer.addFileForUpload(fileIndex);
}
if(updateFileTitles) {
fileManager.updateFileTitles();
}
localStorage[SYNC_PROVIDER_GDRIVE
+ "lastChangeId"] = newChangeId;
callback();
});
});
}
function syncDown(callback) {
syncDownGdrive(callback);
};
var syncRunning = false;
var lastSync = 0;
synchronizer.sync = function() {
// If sync is already running or timeout is not reached or offline
if (syncRunning || lastSync + SYNC_PERIOD > currentTime || offline) {
return;
}
syncRunning = true;
lastSync = currentTime;
syncDown(function() {
syncUp(function() {
syncRunning = false;
});
});
};
return synchronizer;
})();