2013-04-21 00:07:27 +00:00
|
|
|
define(["jquery", "core", "google-helper", "underscore"], function($, core, googleHelper) {
|
2013-04-20 00:14:20 +00:00
|
|
|
|
|
|
|
var gdriveProvider = {
|
|
|
|
providerId: PROVIDER_GDRIVE,
|
2013-04-21 00:07:27 +00:00
|
|
|
providerName: "Google Drive",
|
2013-04-20 17:40:05 +00:00
|
|
|
defaultPublishFormat: "template",
|
|
|
|
useSync: false
|
2013-04-20 00:14:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function createSyncAttributes(id, etag, content, title) {
|
|
|
|
var syncAttributes = {};
|
|
|
|
syncAttributes.provider = PROVIDER_GDRIVE;
|
|
|
|
syncAttributes.id = id;
|
|
|
|
syncAttributes.etag = etag;
|
|
|
|
syncAttributes.contentCRC = core.crc32(content);
|
|
|
|
syncAttributes.titleCRC = core.crc32(title);
|
2013-04-21 00:07:27 +00:00
|
|
|
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
|
2013-04-20 00:14:20 +00:00
|
|
|
localStorage[syncIndex] = JSON.stringify(syncAttributes);
|
|
|
|
return syncIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
function importFilesFromIds(ids) {
|
|
|
|
googleHelper.downloadMetadata(ids, function(error, result) {
|
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
googleHelper.downloadContent(result, function(error, result) {
|
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.each(result, function(file) {
|
|
|
|
var syncIndex = createSyncAttributes(file.id, file.etag, file.content, file.title);
|
2013-04-21 00:07:27 +00:00
|
|
|
var fileIndex = core.fileManager.createFile(file.title, file.content, [syncIndex]);
|
|
|
|
core.fileManager.selectFile(fileIndex);
|
2013-04-20 00:14:20 +00:00
|
|
|
core.showMessage('"' + file.title + '" imported successfully from Google Drive.');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
gdriveProvider.importFiles = function() {
|
|
|
|
googleHelper.picker(function(error, ids) {
|
|
|
|
if(ids === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var importIds = [];
|
|
|
|
_.each(ids, function(id) {
|
|
|
|
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
|
2013-04-21 00:07:27 +00:00
|
|
|
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
|
2013-04-20 00:14:20 +00:00
|
|
|
if(fileIndex !== undefined) {
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
|
|
|
core.showError('"' + title + '" was already imported');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
importIds.push(id);
|
|
|
|
});
|
|
|
|
importFilesFromIds(importIds);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
gdriveProvider.exportFile = function(event, title, content, callback) {
|
2013-04-20 17:40:05 +00:00
|
|
|
googleHelper.upload(undefined, undefined, title, content, undefined, function(error, result) {
|
2013-04-20 00:14:20 +00:00
|
|
|
if (error) {
|
|
|
|
callback(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var syncIndex = createSyncAttributes(result.id, result.etag, content, title);
|
|
|
|
callback(undefined, syncIndex);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
gdriveProvider.exportManual = function(event, title, content, callback) {
|
|
|
|
var id = core.getInputValue($("#input-sync-manual-gdrive-id"), event);
|
|
|
|
if(!id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check that file is not synchronized with an other one
|
|
|
|
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
|
2013-04-21 00:07:27 +00:00
|
|
|
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
|
2013-04-20 00:14:20 +00:00
|
|
|
if(fileIndex !== undefined) {
|
|
|
|
var existingTitle = localStorage[fileIndex + ".title"];
|
|
|
|
core.showError('File ID is already synchronized with "' + existingTitle + '"');
|
|
|
|
callback(true);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-20 17:40:05 +00:00
|
|
|
googleHelper.upload(id, undefined, title, content, undefined, function(error, result) {
|
2013-04-20 00:14:20 +00:00
|
|
|
if (error) {
|
|
|
|
callback(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var syncIndex = createSyncAttributes(result.id, result.etag, content, title);
|
|
|
|
callback(undefined, syncIndex);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-20 17:40:05 +00:00
|
|
|
gdriveProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
|
|
|
|
var syncContentCRC = syncAttributes.contentCRC;
|
|
|
|
var syncTitleCRC = syncAttributes.titleCRC;
|
|
|
|
// Skip if CRC has not changed
|
|
|
|
if(uploadContentCRC == syncContentCRC && uploadTitleCRC == syncTitleCRC) {
|
|
|
|
callback(undefined, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
googleHelper.upload(syncAttributes.id, undefined, uploadTitle, uploadContent, syncAttributes.etag, function(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
syncAttributes.etag = result.etag;
|
|
|
|
syncAttributes.contentCRC = uploadContentCRC;
|
|
|
|
syncAttributes.titleCRC = uploadTitleCRC;
|
|
|
|
callback(undefined, true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-21 00:07:27 +00:00
|
|
|
gdriveProvider.syncDown = function(callback) {
|
2013-04-20 17:40:05 +00:00
|
|
|
if (gdriveProvider.useSync === false) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var lastChangeId = parseInt(localStorage[PROVIDER_GDRIVE + ".lastChangeId"]);
|
|
|
|
googleHelper.checkChanges(lastChangeId, function(error, changes, newChangeId) {
|
|
|
|
if (error) {
|
|
|
|
callback(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var interestingChanges = [];
|
|
|
|
_.each(changes, function(change) {
|
2013-04-21 00:07:27 +00:00
|
|
|
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + change.fileId;
|
2013-04-20 17:40:05 +00:00
|
|
|
var serializedAttributes = localStorage[syncIndex];
|
|
|
|
if(serializedAttributes === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Store syncIndex to avoid 2 times formating
|
|
|
|
change.syncIndex = syncIndex;
|
|
|
|
// Delete
|
|
|
|
if(change.deleted === true) {
|
|
|
|
interestingChanges.push(change);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Modify
|
|
|
|
var syncAttributes = JSON.parse(serializedAttributes);
|
|
|
|
if(syncAttributes.etag != change.file.etag) {
|
|
|
|
interestingChanges.push(change);
|
|
|
|
// Store syncAttributes to avoid 2 times parsing
|
|
|
|
change.syncAttributes = syncAttributes;
|
|
|
|
}
|
|
|
|
});
|
2013-04-21 00:07:27 +00:00
|
|
|
googleHelper.downloadContent(interestingChanges, function(error, changes) {
|
2013-04-20 17:40:05 +00:00
|
|
|
if (error) {
|
|
|
|
callback(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var updateFileTitles = false;
|
|
|
|
_.each(changes, function(change) {
|
|
|
|
var syncIndex = change.syncIndex;
|
2013-04-21 00:07:27 +00:00
|
|
|
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
|
2013-04-20 17:40:05 +00:00
|
|
|
// No file corresponding (file may have been deleted locally)
|
|
|
|
if(fileIndex === undefined) {
|
2013-04-21 00:07:27 +00:00
|
|
|
core.fileManager.removeSync(syncIndex);
|
2013-04-20 17:40:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var localTitle = localStorage[fileIndex + ".title"];
|
|
|
|
// File deleted
|
|
|
|
if (change.deleted === true) {
|
2013-04-21 00:07:27 +00:00
|
|
|
core.fileManager.removeSync(syncIndex);
|
2013-04-20 17:40:05 +00:00
|
|
|
updateFileTitles = true;
|
|
|
|
core.showMessage('"' + localTitle + '" has been removed from Google Drive.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var syncAttributes = change.syncAttributes;
|
|
|
|
var localTitleChanged = syncAttributes.titleCRC != core.crc32(localTitle);
|
|
|
|
var localContent = localStorage[fileIndex + ".content"];
|
|
|
|
var localContentChanged = syncAttributes.contentCRC != core.crc32(localContent);
|
|
|
|
var file = change.file;
|
|
|
|
var fileTitleChanged = localTitle != file.title;
|
|
|
|
var fileContentChanged = localContent != file.content;
|
|
|
|
// Conflict detection
|
|
|
|
if ((fileTitleChanged === true && localTitleChanged === true)
|
|
|
|
|| (fileContentChanged === true && localContentChanged === true)) {
|
2013-04-21 00:07:27 +00:00
|
|
|
core.fileManager.createFile(localTitle + " (backup)", localContent);
|
2013-04-20 17:40:05 +00:00
|
|
|
updateFileTitles = true;
|
|
|
|
core.showMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
|
|
|
|
}
|
|
|
|
// If file title changed
|
|
|
|
if(fileTitleChanged) {
|
|
|
|
localStorage[fileIndex + ".title"] = file.title;
|
|
|
|
updateFileTitles = true;
|
|
|
|
core.showMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
|
|
|
|
}
|
|
|
|
// If file content changed
|
|
|
|
if(fileContentChanged) {
|
|
|
|
localStorage[fileIndex + ".content"] = file.content;
|
|
|
|
core.showMessage('"' + file.title + '" has been updated from Google Drive.');
|
2013-04-21 00:07:27 +00:00
|
|
|
if(core.fileManager.isCurrentFileIndex(fileIndex)) {
|
2013-04-20 17:40:05 +00:00
|
|
|
updateFileTitles = false; // Done by next function
|
2013-04-21 00:07:27 +00:00
|
|
|
core.fileManager.selectFile(); // Refresh editor
|
2013-04-20 17:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update syncAttributes
|
|
|
|
syncAttributes.etag = file.etag;
|
|
|
|
syncAttributes.contentCRC = core.crc32(file.content);
|
|
|
|
syncAttributes.titleCRC = core.crc32(file.title);
|
|
|
|
localStorage[syncIndex] = JSON.stringify(syncAttributes);
|
|
|
|
});
|
|
|
|
if(updateFileTitles) {
|
2013-04-21 00:07:27 +00:00
|
|
|
core.fileManager.updateFileTitles();
|
2013-04-20 17:40:05 +00:00
|
|
|
}
|
|
|
|
localStorage[PROVIDER_GDRIVE + ".lastChangeId"] = newChangeId;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
2013-04-21 00:07:27 +00:00
|
|
|
};
|
2013-04-20 17:40:05 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
gdriveProvider.publish = function(publishAttributes, title, content, callback) {
|
2013-04-21 00:07:27 +00:00
|
|
|
googleHelper.upload(
|
|
|
|
publishAttributes.fileId,
|
|
|
|
undefined,
|
|
|
|
publishAttributes.fileName || title,
|
|
|
|
content,
|
|
|
|
undefined,
|
|
|
|
function(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
publishAttributes.fileId = result.id;
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
2013-04-20 00:14:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
gdriveProvider.newPublishAttributes = function(event) {
|
|
|
|
var publishAttributes = {};
|
|
|
|
publishAttributes.fileId = $("#input-publish-gdrive-fileid").val() || undefined;
|
2013-04-21 00:07:27 +00:00
|
|
|
publishAttributes.fileName = $("#input-publish-gdrive-filename").val() || undefined;
|
2013-04-20 00:14:20 +00:00
|
|
|
if(event.isPropagationStopped()) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return publishAttributes;
|
|
|
|
};
|
|
|
|
|
2013-04-22 01:04:12 +00:00
|
|
|
core.onReady(function() {
|
2013-04-21 00:07:27 +00:00
|
|
|
var state = localStorage[PROVIDER_GDRIVE + ".state"];
|
2013-04-20 00:14:20 +00:00
|
|
|
if(state === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-21 00:07:27 +00:00
|
|
|
localStorage.removeItem(PROVIDER_GDRIVE + ".state");
|
2013-04-20 00:14:20 +00:00
|
|
|
state = JSON.parse(state);
|
|
|
|
if (state.action == "create") {
|
|
|
|
googleHelper.upload(undefined, state.folderId, GDRIVE_DEFAULT_FILE_TITLE,
|
2013-04-20 17:40:05 +00:00
|
|
|
"", undefined, function(error, file) {
|
2013-04-20 00:14:20 +00:00
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var syncIndex = createSyncAttributes(file.id, file.etag, file.content, file.title);
|
2013-04-22 00:39:33 +00:00
|
|
|
var fileIndex = core.fileManager.createFile(file.title, file.content, [syncIndex]);
|
|
|
|
core.fileManager.selectFile(fileIndex);
|
2013-04-20 00:14:20 +00:00
|
|
|
core.showMessage('"' + file.title + '" created successfully on Google Drive.');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (state.action == "open") {
|
|
|
|
var importIds = [];
|
|
|
|
_.each(state.ids, function(id) {
|
|
|
|
var syncIndex = "sync." + PROVIDER_GDRIVE + "." + id;
|
2013-04-22 00:39:33 +00:00
|
|
|
var fileIndex = core.fileManager.getFileIndexFromSync(syncIndex);
|
2013-04-20 00:14:20 +00:00
|
|
|
if(fileIndex !== undefined) {
|
2013-04-22 00:39:33 +00:00
|
|
|
core.fileManager.selectFile(fileIndex);
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
importIds.push(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
importFilesFromIds(importIds);
|
|
|
|
}
|
2013-04-21 00:07:27 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
|
|
|
|
return gdriveProvider;
|
|
|
|
});
|