enDisconnect();
return;
}
// Get sync state
SyncState syncState = null;
try {
logger.log(logger.EXTREME, "Getting sync state");
syncState = noteStore.getSyncState(authToken);
syncSignal.saveUploadAmount.emit(syncState.getUploaded());
syncSignal.saveEvernoteUpdateCount.emit(syncState.getUpdateCount());
evernoteUpdateCount = syncState.getUpdateCount();
} catch (EDAMUserException e) {
e.printStackTrace();
status.message.emit(tr("Error getting sync state! Aborting sync and disconnecting!"));
syncSignal.errorDisconnect.emit();
enDisconnect();
return;
} catch (EDAMSystemException e) {
e.printStackTrace();
status.message.emit(tr("Error getting sync state! Aborting sync and disconnecting!"));
syncSignal.errorDisconnect.emit();
enDisconnect();
return;
} catch (TException e) {
e.printStackTrace();
status.message.emit(tr("Error getting sync state! Aborting sync and disconnecting!"));
syncSignal.errorDisconnect.emit();
enDisconnect();
return;
}
if (syncState == null) {
logger.log(logger.EXTREME, "Sync State is null");
status.message.emit(tr("Syncronization Error!"));
return;
}
// Determine what to do.
// If we need to do a full sync.
logger.log(logger.LOW, "Full Sequence Before: " +syncState.getFullSyncBefore());
logger.log(logger.LOW, "Last Sequence Date: " +sequenceDate);
logger.log(logger.LOW, "Var Last Sequence Number: " +updateSequenceNumber);
logger.log(logger.LOW, "DB Last Sequence Number: " + conn.getSyncTable().getUpdateSequenceNumber());
if (syncState.getFullSyncBefore() > sequenceDate) {
logger.log(logger.EXTREME, "Full sequence date has expired");
sequenceDate = 0;
conn.getSyncTable().setLastSequenceDate(0);
updateSequenceNumber = 0;
conn.getSyncTable().setUpdateSequenceNumber(0);
}
// Check for "special" sync instructions
String syncLinked = conn.getSyncTable().getRecord("FullLinkedNotebookSync");
String syncShared = conn.getSyncTable().getRecord("FullSharedNotebookSync");
String syncNotebooks = conn.getSyncTable().getRecord("FullNotebookSync");
String syncInkNoteImages = conn.getSyncTable().getRecord("FullInkNoteImageSync");
if (syncLinked != null) {
downloadAllLinkedNotebooks(localNoteStore);
}
if (syncShared != null) {
downloadAllSharedNotebooks(localNoteStore);
}
if (syncNotebooks != null) {
downloadAllNotebooks(localNoteStore);
}
if (syncInkNoteImages != null) {
List<String> guids = conn.getNoteTable().noteResourceTable.findInkNotes();
for (int i=0; i<guids.size(); i++) {
downloadInkNoteImage(guids.get(i), authToken);
}
conn.getSyncTable().deleteRecord("FullInkNoteImageSync");
}
// If there are remote changes
logger.log(logger.LOW, "Update Count: " +syncState.getUpdateCount());
logger.log(logger.LOW, "Last Update Count: " +updateSequenceNumber);
if (syncState.getUpdateCount() > updateSequenceNumber) {
logger.log(logger.EXTREME, "Refresh needed is true");
refreshNeeded = true;
logger.log(logger.EXTREME, "Downloading changes");
syncRemoteToLocal(localNoteStore);
}
//*****************************************
//* Sync linked/shared notebooks
//*****************************************
//syncLinkedNotebooks();
//conn.getNoteTable().getDirty();
//disableUploads = true; /// DELETE THIS LINE!!!!
if (!disableUploads) {
logger.log(logger.EXTREME, "Uploading changes");
// Synchronize remote changes
if (!error)
syncExpunged(localNoteStore);
if (!error)
syncLocalTags(localNoteStore);
if (!error)
syncLocalNotebooks(localNoteStore);
if (!error)
syncLocalLinkedNotebooks(localNoteStore);
if (!error)
syncDeletedNotes(localNoteStore);
if (!error)
syncLocalNotes();
if (!error)
syncLocalSavedSearches(localNoteStore);
}
status.message.emit(tr("Cleaning up"));
List<String> notes = conn.getNoteTable().expungeIgnoreSynchronizedNotes(conn.getSyncTable().getIgnoreRecords("NOTEBOOK"),
conn.getSyncTable().getIgnoreRecords("TAG"), conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK"));
if (notes.size() > 0)
syncSignal.refreshLists.emit();
//*****************************************
//* End of synchronization
//*****************************************
if (refreshNeeded)
syncSignal.refreshLists.emit();
if (!error) {
logger.log(logger.LOW, "Sync completed. Errors=" +error);
if (!disableUploads)
status.message.emit(tr("Synchronizing complete"));
else
status.message.emit(tr("Download syncronization complete. Uploads have been disabled."));
logger.log(logger.EXTREME, "Saving sync time");
if (syncState.getCurrentTime() > sequenceDate)
sequenceDate = syncState.getCurrentTime();
if (syncState.getUpdateCount() > updateSequenceNumber)
updateSequenceNumber = syncState.getUpdateCount();
conn.getSyncTable().setLastSequenceDate(sequenceDate);
conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);
}
}
logger.log(logger.HIGH, "Leaving SyncRunner.evernoteSync");