import java.util.Calendar;\r
import java.util.Date;\r
import java.util.GregorianCalendar;\r
+import java.util.HashMap;\r
import java.util.List;\r
+import java.util.TreeSet;\r
import java.util.Vector;\r
import java.util.concurrent.LinkedBlockingQueue;\r
\r
import com.evernote.edam.error.EDAMSystemException;\r
import com.evernote.edam.error.EDAMUserException;\r
import com.evernote.edam.notestore.NoteStore;\r
+import com.evernote.edam.notestore.NoteStore.Client;\r
import com.evernote.edam.notestore.SyncChunk;\r
import com.evernote.edam.notestore.SyncState;\r
import com.evernote.edam.type.Data;\r
public volatile boolean keepRunning;\r
public volatile String authToken;\r
private long evernoteUpdateCount;\r
+ private final String userAgent = "NeverNote/" + System.getProperty("os.name")\r
+ +"/"+System.getProperty("java.vendor") + "/"\r
+ + System.getProperty("java.version") +";";\r
\r
- public volatile NoteStore.Client noteStore;\r
+ public volatile NoteStore.Client localNoteStore;\r
private UserStore.Client userStore;\r
\r
public volatile StatusSignal status;\r
public int updateSequenceNumber;\r
private boolean refreshNeeded;\r
private volatile LinkedBlockingQueue<String> workQueue;\r
-// private static int MAX_EMPTY_QUEUE_COUNT = 1;\r
private static int MAX_QUEUED_WAITING = 1000;\r
String dbuid;\r
String dburl;\r
+ String indexUrl;\r
+ String resourceUrl;\r
String dbpswd;\r
String dbcpswd;\r
+ private final TreeSet<String> ignoreTags;\r
+ private final TreeSet<String> ignoreNotebooks;\r
+ private final TreeSet<String> ignoreLinkedNotebooks;\r
+ private HashMap<String,String> badTagSync;\r
\r
\r
\r
- public SyncRunner(String logname, String u, String uid, String pswd, String cpswd) {\r
+ public SyncRunner(String logname, String u, String i, String r, String uid, String pswd, String cpswd) {\r
logger = new ApplicationLogger(logname);\r
\r
noteSignal = new NoteSignal();\r
searchSignal = new SavedSearchSignal();\r
syncSignal = new SyncSignal();\r
resourceSignal = new NoteResourceSignal();\r
+ resourceUrl = r;\r
+ indexUrl = i;\r
dbuid = uid;\r
dburl = u;\r
dbpswd = pswd;\r
authRefreshNeeded = false;\r
keepRunning = true;\r
idle = true;\r
- noteStore = null;\r
- userStore = null;\r
- authToken = null;\r
disableUploads = false;\r
+ ignoreTags = new TreeSet<String>();\r
+ ignoreNotebooks = new TreeSet<String>();\r
+ ignoreLinkedNotebooks = new TreeSet<String>();\r
+ \r
// setAutoDelete(false);\r
workQueue=new LinkedBlockingQueue<String>(MAX_QUEUED_WAITING);\r
}\r
public void run() {\r
try {\r
logger.log(logger.EXTREME, "Starting thread");\r
- conn = new DatabaseConnection(logger, dburl, dbuid, dbpswd, dbcpswd);\r
+ conn = new DatabaseConnection(logger, dburl, indexUrl, resourceUrl, dbuid, dbpswd, dbcpswd, 200);\r
while(keepRunning) {\r
String work = workQueue.take();\r
logger.log(logger.EXTREME, "Work found: " +work);\r
- if (work.equalsIgnoreCase("stop"))\r
+ if (work.equalsIgnoreCase("stop")) {\r
+ idle=false;\r
return;\r
+ }\r
idle=false;\r
error=false;\r
- if (authRefreshNeeded == true) {\r
+ if (authRefreshNeeded == true || !isConnected) {\r
logger.log(logger.EXTREME, "Refreshing connection");\r
refreshConnection();\r
}\r
updateSequenceNumber = conn.getSyncTable().getUpdateSequenceNumber();\r
try {\r
logger.log(logger.EXTREME, "Beginning sync");\r
- evernoteSync();\r
+ evernoteSync(localNoteStore);\r
logger.log(logger.EXTREME, "Sync finished");\r
} catch (UnknownHostException e) {\r
status.message.emit(e.getMessage());\r
}\r
}\r
- dirtyNoteGuids = null;\r
idle=true;\r
logger.log(logger.EXTREME, "Signaling refresh finished. refreshNeeded=" +refreshNeeded);\r
syncSignal.finished.emit(refreshNeeded);\r
+ if (error) {\r
+ syncSignal.errorDisconnect.emit();\r
+ status.message.emit(tr("Error synchronizing - see log for details."));\r
+ }\r
}\r
} \r
catch (InterruptedException e1) {\r
}\r
public void setNoteStore(NoteStore.Client c) {\r
logger.log(logger.EXTREME, "Setting NoteStore in sync thread");\r
- noteStore = c;\r
+ localNoteStore = c;\r
}\r
public void setUserStore(UserStore.Client c) {\r
logger.log(logger.EXTREME, "Setting UserStore in sync thread");\r
//***************************************************************\r
// Synchronize changes with Evernote\r
@SuppressWarnings("unused")\r
- private void evernoteSync() throws java.net.UnknownHostException {\r
+ private void evernoteSync(Client noteStore) throws java.net.UnknownHostException {\r
logger.log(logger.HIGH, "Entering SyncRunner.evernoteSync");\r
+ \r
+ // Rebuild list of tags & notebooks to ignore\r
+ ignoreNotebooks.clear();\r
+ List<String> ignore = conn.getSyncTable().getIgnoreRecords("NOTEBOOK");\r
+ for (int i=0; i<ignore.size(); i++) \r
+ ignoreNotebooks.add(ignore.get(i));\r
+ \r
+ ignore.clear();\r
+ ignore = conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK");\r
+ for (int i=0; i<ignore.size(); i++) \r
+ ignoreLinkedNotebooks.add(ignore.get(i));\r
+ \r
+ ignoreTags.clear();\r
+ ignore = conn.getSyncTable().getIgnoreRecords("TAG");\r
+ for (int i=0; i<ignore.size(); i++) \r
+ ignoreTags.add(ignore.get(i));\r
\r
+ // Make sure we are connected & should keep running\r
if (isConnected && keepRunning) {\r
error = false;\r
logger.log(logger.EXTREME, "Synchronizing with Evernote");\r
e1.printStackTrace();\r
status.message.emit(tr("User exception getting user account information. Aborting sync and disconnecting"));\r
syncSignal.errorDisconnect.emit();\r
+ error = true;\r
enDisconnect();\r
return;\r
} catch (EDAMSystemException e1) {\r
e1.printStackTrace();\r
status.message.emit(tr("System error user account information. Aborting sync and disconnecting!"));\r
syncSignal.errorDisconnect.emit();\r
+ error = true;\r
enDisconnect();\r
return;\r
} catch (TException e1) {\r
e1.printStackTrace();\r
syncSignal.errorDisconnect.emit();\r
+ error = true;\r
status.message.emit(tr("Transaction error getting user account information. Aborting sync and disconnecting!"));\r
enDisconnect();\r
return;\r
String syncNotebooks = conn.getSyncTable().getRecord("FullNotebookSync");\r
String syncInkNoteImages = conn.getSyncTable().getRecord("FullInkNoteImageSync");\r
if (syncLinked != null) {\r
- downloadAllLinkedNotebooks();\r
+ downloadAllLinkedNotebooks(localNoteStore);\r
}\r
if (syncShared != null) {\r
- downloadAllSharedNotebooks();\r
+ downloadAllSharedNotebooks(localNoteStore);\r
}\r
if (syncNotebooks != null) {\r
- downloadAllNotebooks();\r
+ downloadAllNotebooks(localNoteStore);\r
}\r
\r
if (syncInkNoteImages != null) {\r
logger.log(logger.EXTREME, "Refresh needed is true");\r
refreshNeeded = true;\r
logger.log(logger.EXTREME, "Downloading changes");\r
- syncRemoteToLocal();\r
+ syncRemoteToLocal(localNoteStore);\r
}\r
\r
//*****************************************\r
logger.log(logger.EXTREME, "Uploading changes");\r
// Synchronize remote changes\r
if (!error)\r
- syncExpunged();\r
+ syncExpunged(localNoteStore);\r
if (!error)\r
- syncLocalTags();\r
+ syncLocalTags(localNoteStore);\r
if (!error)\r
- syncLocalNotebooks();\r
+ syncLocalNotebooks(localNoteStore);\r
if (!error)\r
- syncLocalLinkedNotebooks();\r
+ syncLocalLinkedNotebooks(localNoteStore);\r
if (!error) \r
- syncDeletedNotes();\r
+ syncDeletedNotes(localNoteStore);\r
if (!error)\r
syncLocalNotes();\r
if (!error)\r
- syncLocalSavedSearches();\r
+ syncLocalSavedSearches(localNoteStore);\r
}\r
\r
+ status.message.emit(tr("Cleaning up"));\r
+ List<String> notes = conn.getNoteTable().expungeIgnoreSynchronizedNotes(conn.getSyncTable().getIgnoreRecords("NOTEBOOK"), \r
+ conn.getSyncTable().getIgnoreRecords("TAG"), conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK"));\r
+ if (notes.size() > 0)\r
+ syncSignal.refreshLists.emit();\r
+ \r
//*****************************************\r
//* End of synchronization\r
//*****************************************\r
}\r
\r
// Sync deleted items with Evernote\r
- private void syncExpunged() {\r
+ private void syncExpunged(Client noteStore) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncExpunged");\r
\r
List<DeletedItemRecord> expunged = conn.getDeletedTable().getAllDeleted();\r
boolean error = false;\r
for (int i=0; i<expunged.size() && keepRunning; i++) {\r
- \r
+\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
try {\r
if (expunged.get(i).type.equalsIgnoreCase("TAG")) {\r
if (expunged.get(i).type.equalsIgnoreCase("NOTE")) {\r
logger.log(logger.EXTREME, "Note expunged");\r
updateSequenceNumber = noteStore.deleteNote(authToken, expunged.get(i).guid);\r
+ refreshNeeded = true;\r
conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
}\r
if (expunged.get(i).type.equalsIgnoreCase("SAVEDSEARCH")) {\r
logger.log(logger.HIGH, "Leaving SyncRunner.syncExpunged");\r
\r
}\r
- private void syncDeletedNotes() {\r
+ private void syncDeletedNotes(Client noteStore) {\r
if (syncDeletedContent)\r
return;\r
logger.log(logger.HIGH, "Entering SyncRunner.syncDeletedNotes");\r
for (int i=0; i<notes.size() && keepRunning; i++) {\r
\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
Note enNote = notes.get(i);\r
try {\r
List<Note> notes = conn.getNoteTable().getDirty();\r
// Sync the local notebooks with Evernote's\r
for (int i=0; i<notes.size() && keepRunning; i++) {\r
- syncLocalNote(notes.get(i), authToken);\r
+ syncLocalNote(localNoteStore, notes.get(i), authToken);\r
}\r
logger.log(logger.HIGH, "Entering SyncRunner.syncNotes");\r
\r
}\r
// Sync notes with Evernote\r
- private void syncLocalNote(Note enNote, String token) {\r
+ private void syncLocalNote(Client noteStore, Note enNote, String token) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncNotes");\r
status.message.emit(tr("Sending local notes."));\r
\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
if (enNote.isActive()) {\r
try {\r
}\r
\r
// Sync Notebooks with Evernote\r
- private void syncLocalNotebooks() {\r
+ private void syncLocalNotebooks(Client noteStore) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncLocalNotebooks");\r
\r
status.message.emit(tr("Sending local notebooks."));\r
for (int i=0; i<notebooks.size() && keepRunning; i++) {\r
\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
Notebook enNotebook = notebooks.get(i);\r
try {\r
\r
}\r
// Sync Tags with Evernote\r
- private void syncLocalTags() {\r
+ private void syncLocalTags(Client noteStore) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncLocalTags");\r
List<Tag> remoteList = new ArrayList<Tag>();\r
status.message.emit(tr("Sending local tags."));\r
\r
int sequence;\r
\r
+ if (badTagSync == null)\r
+ badTagSync = new HashMap<String,String>();\r
+ else\r
+ badTagSync.clear();\r
+ \r
Tag enTag = findNextTag();\r
while(enTag!=null) {\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
try {\r
if (enTag.getUpdateSequenceNum() > 0) {\r
updateSequenceNumber = sequence;\r
conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
} catch (EDAMUserException e) {\r
- logger.log(logger.LOW, "*** EDAM User Excepton syncLocalTags");\r
- logger.log(logger.LOW, e.toString()); \r
+ logger.log(logger.LOW, "*** EDAM User Excepton syncLocalTags: " +enTag.getName());\r
+ logger.log(logger.LOW, e.toString());\r
+ badTagSync.put(enTag.getGuid(),null);\r
error = true;\r
} catch (EDAMSystemException e) {\r
- logger.log(logger.LOW, "** EDAM System Excepton syncLocalTags");\r
+ logger.log(logger.LOW, "** EDAM System Excepton syncLocalTags: " +enTag.getName());\r
logger.log(logger.LOW, e.toString()); \r
+ badTagSync.put(enTag.getGuid(),null);\r
error = true;\r
} catch (EDAMNotFoundException e) {\r
- logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalTags");\r
- logger.log(logger.LOW, e.toString()); \r
+ logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalTags: " +enTag.getName());\r
+ logger.log(logger.LOW, e.toString());\r
+ badTagSync.put(enTag.getGuid(),null);\r
error = true;\r
} catch (TException e) {\r
- logger.log(logger.LOW, "*** EDAM TExcepton syncLocalTags");\r
- logger.log(logger.LOW, e.toString()); \r
+ logger.log(logger.LOW, "*** EDAM TExcepton syncLocalTags: " +enTag.getName());\r
+ logger.log(logger.LOW, e.toString());\r
+ badTagSync.put(enTag.getGuid(),null);\r
error = true;\r
} \r
\r
}\r
logger.log(logger.HIGH, "Leaving SyncRunner.syncLocalTags");\r
}\r
- private void syncLocalLinkedNotebooks() {\r
+ private void syncLocalLinkedNotebooks(Client noteStore) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncLocalLinkedNotebooks");\r
\r
List<String> list = conn.getLinkedNotebookTable().getDirtyGuids();\r
logger.log(logger.HIGH, "Leaving SyncRunner.syncLocalLinkedNotebooks");\r
}\r
// Sync Saved Searches with Evernote\r
- private void syncLocalSavedSearches() {\r
+ private void syncLocalSavedSearches(Client noteStore) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncLocalSavedSearches");\r
List<SavedSearch> remoteList = new ArrayList<SavedSearch>();\r
status.message.emit(tr("Sending saved searches."));\r
for (int i=0; i<searches.size() && keepRunning; i++) {\r
\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
SavedSearch enSearch = searches.get(i);\r
try {\r
error = true;\r
} \r
}\r
+\r
logger.log(logger.HIGH, "Entering SyncRunner.syncLocalSavedSearches");\r
} \r
\r
// Sync evernote changes with local database\r
- private void syncRemoteToLocal() {\r
+ private void syncRemoteToLocal(Client noteStore) {\r
logger.log(logger.HIGH, "Entering SyncRunner.syncRemoteToLocal");\r
\r
List<Note> dirtyNotes = conn.getNoteTable().getDirty();\r
while(more && keepRunning) {\r
\r
if (authRefreshNeeded)\r
- refreshConnection();\r
+ if (!refreshConnection())\r
+ return;\r
\r
- chunk = null;\r
int sequence = updateSequenceNumber;\r
try {\r
+// conn.beginTransaction();\r
logger.log(logger.EXTREME, "Getting chunk from Evernote");\r
chunk = noteStore.getSyncChunk(authToken, sequence, chunkSize, fullSync);\r
} catch (EDAMUserException e) {\r
syncRemoteTags(chunk.getTags());\r
syncRemoteSavedSearches(chunk.getSearches());\r
syncRemoteNotebooks(chunk.getNotebooks());\r
- syncRemoteNotes(chunk.getNotes(), fullSync, authToken);\r
- syncRemoteResources(chunk.getResources());\r
+ syncRemoteNotes(noteStore, chunk.getNotes(), fullSync, authToken);\r
+ syncRemoteResources(noteStore, chunk.getResources());\r
syncRemoteLinkedNotebooks(chunk.getLinkedNotebooks());\r
+ \r
+ // Signal about any updated notes to invalidate the cache\r
+ for (int i=0; i<chunk.getNotesSize(); i++) \r
+ noteSignal.noteChanged.emit(chunk.getNotes().get(i).getGuid(), null); \r
syncExpungedNotes(chunk);\r
\r
\r
\r
\r
// Save the chunk sequence number\r
- if (!error && chunk.getChunkHighUSN() > 0) {\r
+ if (!error && chunk.getChunkHighUSN() > 0 && keepRunning) {\r
logger.log(logger.EXTREME, "emitting sequence number to main thread");\r
updateSequenceNumber = chunk.getChunkHighUSN();\r
conn.getSyncTable().setLastSequenceDate(chunk.getCurrentTime());\r
conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
+// conn.commitTransaction();\r
}\r
\r
\r
pct = pct/evernoteUpdateCount;\r
status.message.emit(tr("Downloading ") +new Long(pct).toString()+tr("% complete."));\r
}\r
+// conn.commitTransaction();\r
}\r
-\r
logger.log(logger.HIGH, "Leaving SyncRunner.syncRemoteToLocal");\r
}\r
// Sync expunged notes\r
// List<SharedNotebook> books = noteStore.getSharedNotebookByAuth(authToken);\r
// }\r
// Sync remote Resources\r
- private void syncRemoteResources(List<Resource> resource) {\r
+ private void syncRemoteResources(Client noteStore, List<Resource> resource) {\r
logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteResources");\r
if (resource != null) {\r
for (int i=0; i<resource.size() && keepRunning; i++) {\r
- syncRemoteResource(resource.get(i), authToken);\r
+ syncRemoteResource(noteStore, resource.get(i), authToken);\r
}\r
}\r
logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteResources");\r
}\r
// Sync remote resource\r
- private void syncRemoteResource(Resource resource, String authToken) {\r
+ private void syncRemoteResource(Client noteStore, Resource resource, String authToken) {\r
// This is how the logic for this works.\r
// 1.) If the resource is not in the local database, we add it.\r
// 2.) If a copy of the resource is in the local database and the note isn't dirty, we update the local copy\r
// is a conflict. The note conflict should get a copy of the resource at that time.\r
\r
boolean saveNeeded = false;\r
- /* #1 */ Resource r = getEvernoteResource(resource.getGuid(), true,true,true, authToken);\r
+ /* #1 */ Resource r = getEvernoteResource(noteStore, resource.getGuid(), true,true,true, authToken);\r
Resource l = conn.getNoteTable().noteResourceTable.getNoteResource(r.getGuid(), false);\r
if (l == null) {\r
saveNeeded = true;\r
\r
}\r
// Sync remote notes\r
- private void syncRemoteNotes(List<Note> note, boolean fullSync, String token) {\r
+ private void syncRemoteNotes(Client noteStore, List<Note> note, boolean fullSync, String token) {\r
logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotes");\r
if (note != null) {\r
for (int i=0; i<note.size() && keepRunning; i++) {\r
- Note n = getEvernoteNote(note.get(i).getGuid(), true, fullSync, true,true, token);\r
+ Note n = getEvernoteNote(noteStore, note.get(i).getGuid(), true, fullSync, true,true, token);\r
syncRemoteNote(n, fullSync, token);\r
}\r
}\r
if (conflictingNote)\r
moveConflictingNote(n.getGuid());\r
}\r
- if (conflictingNote || fullSync) {\r
+ boolean ignoreNote = false;\r
+ if (ignoreNotebooks.contains(n.getNotebookGuid()))\r
+ ignoreNote = true;\r
+ for (int i=0; i<n.getTagGuidsSize(); i++) {\r
+ if (ignoreTags.contains(n.getTagGuids().get(i))) {\r
+ ignoreNote = true;\r
+ i=n.getTagGuidsSize();\r
+ }\r
+ }\r
+ \r
+ if ((conflictingNote || fullSync) && !ignoreNote) {\r
logger.log(logger.EXTREME, "Saving Note");\r
conn.getNoteTable().syncNote(n, false);\r
- noteSignal.noteChanged.emit(n.getGuid(), null); // Signal to ivalidate note cache\r
+ // The following was commented out because it caused a race condition on the database where resources \r
+ // may be lost. We do the same thing elsewhere;.\r
+// noteSignal.noteChanged.emit(n.getGuid(), null); // Signal to ivalidate note cache \r
noteSignal.noteDownloaded.emit(n, true); // Signal to add note to index\r
logger.log(logger.EXTREME, "Note Saved");\r
if (fullSync && n.getResources() != null) {\r
}\r
}\r
}\r
- private Note getEvernoteNote(String guid, boolean withContent, boolean withResourceData, boolean withResourceRecognition, boolean withResourceAlternateData, String token) { \r
+ private Note getEvernoteNote(Client noteStore, String guid, boolean withContent, boolean withResourceData, boolean withResourceRecognition, boolean withResourceAlternateData, String token) { \r
Note n = null;\r
try {\r
logger.log(logger.EXTREME, "Retrieving note " +guid);\r
}\r
return n;\r
}\r
- private Resource getEvernoteResource(String guid, boolean withData, boolean withRecognition, boolean withAttributes, String token) { \r
+ private Resource getEvernoteResource(Client noteStore, String guid, boolean withData, boolean withRecognition, boolean withAttributes, String token) { \r
Resource n = null;\r
try {\r
logger.log(logger.EXTREME, "Retrieving resource " +guid);\r
newBook.setPublished(false);\r
\r
conn.getNotebookTable().addNotebook(newBook, false, true);\r
+ notebookSignal.listChanged.emit();\r
notebookGuid = newBook.getGuid();\r
+ refreshNeeded = true;\r
}\r
\r
// Now that we have a good notebook guid, we need to move the conflicting note\r
conn.getNoteTable().updateNoteGuid(guid, newGuid);\r
conn.getNoteTable().updateNoteNotebook(newGuid, notebookGuid, true);\r
\r
- \r
+ noteSignal.notebookChanged.emit(newGuid, notebookGuid);\r
+ refreshNeeded = true;\r
noteSignal.guidChanged.emit(guid,newGuid);\r
}\r
\r
// Find the parent. If the parent has a sequence > 0 then it is a good\r
// parent.\r
for (int i=0; i<tags.size() && keepRunning; i++) {\r
- if (tags.get(i).getParentGuid() == null) {\r
- logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - tag found without parent");\r
- return tags.get(i);\r
- }\r
- Tag parentTag = conn.getTagTable().getTag(tags.get(i).getParentGuid());\r
- if (parentTag.getUpdateSequenceNum() > 0) {\r
- logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - tag found");\r
- return tags.get(i);\r
+ if (!badTagSync.containsKey(tags.get(i).getGuid())) {\r
+ if (tags.get(i).getParentGuid() == null) {\r
+ logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - tag found without parent");\r
+ return tags.get(i);\r
+ }\r
+ Tag parentTag = conn.getTagTable().getTag(tags.get(i).getParentGuid());\r
+ if (parentTag.getUpdateSequenceNum() > 0) {\r
+ logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - tag found");\r
+ return tags.get(i);\r
+ }\r
}\r
}\r
\r
public boolean enConnect() {\r
try {\r
userStoreTrans = new THttpClient(userStoreUrl);\r
+ userStoreTrans.setCustomHeader("User-Agent", userAgent);\r
} catch (TTransportException e) {\r
QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "Transport Excepton", e.getLocalizedMessage());\r
mb.exec();\r
authToken = authResult.getAuthenticationToken(); \r
noteStoreUrl = noteStoreUrlBase + user.getShardId();\r
syncSignal.saveAuthToken.emit(authToken);\r
- syncSignal.saveNoteStore.emit(noteStore);\r
+ syncSignal.saveNoteStore.emit(localNoteStore);\r
\r
\r
try {\r
noteStoreTrans = new THttpClient(noteStoreUrl);\r
+ noteStoreTrans.setCustomHeader("User-Agent", userAgent);\r
} catch (TTransportException e) {\r
QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "Transport Excepton", e.getLocalizedMessage());\r
mb.exec();\r
isConnected = false;\r
} \r
noteStoreProt = new TBinaryProtocol(noteStoreTrans);\r
- noteStore = \r
+ localNoteStore = \r
new NoteStore.Client(noteStoreProt, noteStoreProt); \r
isConnected = true;\r
authTimeRemaining = authResult.getExpiration() - authResult.getCurrentTime();\r
isConnected = false;\r
}\r
// Refresh the connection\r
- private synchronized void refreshConnection() {\r
+ private synchronized boolean refreshConnection() {\r
logger.log(logger.EXTREME, "Entering SyncRunner.refreshConnection()");\r
// Calendar cal = Calendar.getInstance();\r
\r
// If we are not connected let's get out of here\r
if (!isConnected)\r
- return;\r
+ return false;\r
\r
// If we fail too many times, then let's give up.\r
if (failedRefreshes >=5) {\r
logger.log(logger.EXTREME, "Refresh attempts have failed. Disconnecting.");\r
isConnected = false;\r
- return;\r
+ status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
+ return false;\r
}\r
\r
// If this is the first time through, then we need to set this\r
if (userStore != null && authToken != null) \r
newAuth = userStore.refreshAuthentication(authToken); \r
else\r
- return;\r
+ return false;\r
logger.log(logger.EXTREME, "UserStore.refreshAuthentication has succeeded.");\r
} catch (EDAMUserException e) {\r
e.printStackTrace();\r
syncSignal.authRefreshComplete.emit(false);\r
failedRefreshes++;\r
- return;\r
+ return false;\r
} catch (EDAMSystemException e) {\r
e.printStackTrace();\r
syncSignal.authRefreshComplete.emit(false);\r
failedRefreshes++;\r
- return; \r
+ return false; \r
} catch (TException e) { \r
e.printStackTrace();\r
syncSignal.authRefreshComplete.emit(false);\r
failedRefreshes++;\r
- return;\r
+ return false;\r
}\r
\r
// If we didn't get a good auth, then we've failed\r
if (newAuth == null) {\r
failedRefreshes++;\r
+ status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
logger.log(logger.EXTREME, "Authentication failure #" +failedRefreshes);\r
+ status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
syncSignal.authRefreshComplete.emit(false);\r
- return;\r
+ return false;\r
}\r
\r
// We got a good token. Now we need to setup the time to renew it.\r
// failedRefreshes++;\r
// syncSignal.authRefreshComplete.emit(false);\r
// }\r
+ \r
+ return true;\r
}\r
\r
public synchronized boolean addWork(String request) {\r
//*********************************************************\r
//* Special download instructions. Used for DB upgrades\r
//*********************************************************\r
- private void downloadAllSharedNotebooks() {\r
+ private void downloadAllSharedNotebooks(Client noteStore) {\r
try {\r
List<SharedNotebook> books = noteStore.listSharedNotebooks(authToken);\r
logger.log(logger.LOW, "Shared notebooks found = " +books.size());\r
logger.log(logger.LOW, e1.getMessage());\r
}\r
}\r
- private void downloadAllNotebooks() {\r
+ private void downloadAllNotebooks(Client noteStore) {\r
try {\r
List<Notebook> books = noteStore.listNotebooks(authToken);\r
logger.log(logger.LOW, "Shared notebooks found = " +books.size());\r
return;\r
}\r
}\r
- private void downloadAllLinkedNotebooks() {\r
+ private void downloadAllLinkedNotebooks(Client noteStore) {\r
try {\r
List<LinkedNotebook> books = noteStore.listLinkedNotebooks(authToken);\r
logger.log(logger.LOW, "Linked notebooks found = " +books.size());\r
try {\r
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));\r
} catch (UnsupportedEncodingException e1) {\r
- // TODO Auto-generated catch block\r
e1.printStackTrace();\r
}\r
try {\r
logger.log(logger.MEDIUM, "Authenticating Shared Notebooks");\r
status.message.emit(tr("Synchronizing shared notebooks."));\r
List<LinkedNotebook> books = conn.getLinkedNotebookTable().getAll();\r
+\r
for (int i=0; i<books.size(); i++) {\r
try {\r
- long lastSyncDate = conn.getLinkedNotebookTable().getLastSequenceDate(books.get(i).getGuid());\r
- int lastSequenceNumber = conn.getLinkedNotebookTable().getLastSequenceNumber(books.get(i).getGuid());\r
- linkedAuthResult = noteStore.authenticateToSharedNotebook(books.get(i).getShareKey(), authToken);\r
- SyncState linkedSyncState = \r
- noteStore.getLinkedNotebookSyncState(linkedAuthResult.getAuthenticationToken(), books.get(i));\r
- if (linkedSyncState.getUpdateCount() > lastSequenceNumber) {\r
- if (lastSyncDate < linkedSyncState.getFullSyncBefore()) {\r
- lastSequenceNumber = 0;\r
- }\r
- syncLinkedNotebook(books.get(i), lastSequenceNumber, linkedSyncState.getUpdateCount());\r
- }\r
+ long lastSyncDate = conn.getLinkedNotebookTable().getLastSequenceDate(books.get(i).getGuid());\r
+ int lastSequenceNumber = conn.getLinkedNotebookTable().getLastSequenceNumber(books.get(i).getGuid());\r
+\r
+ // Authenticate to the owner's shard\r
+ String linkedNoteStoreUrl = noteStoreUrlBase + books.get(i).getShardId();\r
+ THttpClient linkedNoteStoreTrans = new THttpClient(linkedNoteStoreUrl);\r
+ TBinaryProtocol linkedNoteStoreProt = new TBinaryProtocol(linkedNoteStoreTrans);\r
+ Client linkedNoteStore = new NoteStore.Client(linkedNoteStoreProt, linkedNoteStoreProt); \r
+ \r
+ linkedAuthResult = linkedNoteStore.authenticateToSharedNotebook(books.get(i).getShareKey(), authToken);\r
+ SyncState linkedSyncState = \r
+ linkedNoteStore.getLinkedNotebookSyncState(linkedAuthResult.getAuthenticationToken(), books.get(i));\r
+ if (linkedSyncState.getUpdateCount() > lastSequenceNumber) {\r
+ if (lastSyncDate < linkedSyncState.getFullSyncBefore()) {\r
+ lastSequenceNumber = 0;\r
+ } \r
+ syncLinkedNotebook(linkedNoteStore, books.get(i), lastSequenceNumber, linkedSyncState.getUpdateCount());\r
+ }\r
\r
// Synchronize local changes\r
- syncLocalLinkedNoteChanges(books.get(i));\r
+ syncLocalLinkedNoteChanges(linkedNoteStore, books.get(i));\r
\r
} catch (EDAMUserException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (EDAMNotFoundException e) {\r
status.message.emit(tr("Error synchronizing \" " +\r
error = true;\r
e.printStackTrace();\r
} catch (EDAMSystemException e) {\r
- // TODO Auto-generated catch block\r
+ logger.log(logger.LOW, "System error authenticating against shared notebook. "+\r
+ "Key: "+books.get(i).getShareKey() +" Error:" +e.getMessage());\r
e.printStackTrace();\r
} catch (TException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
}\r
//**************************************************************\r
//* Linked notebook contents (from someone else's account)\r
//*************************************************************\r
- private void syncLinkedNotebook(LinkedNotebook book, int usn, int highSequence) {\r
+ private void syncLinkedNotebook(Client linkedNoteStore, LinkedNotebook book, int usn, int highSequence) {\r
+ if (ignoreLinkedNotebooks.contains(book.getGuid()))\r
+ return;\r
+ List<Note> dirtyNotes = conn.getNoteTable().getDirtyLinkedNotes();\r
+ if (dirtyNoteGuids == null) \r
+ dirtyNoteGuids = new Vector<String>();\r
+\r
+ for (int i=0; i<dirtyNotes.size() && keepRunning; i++) {\r
+ dirtyNoteGuids.add(dirtyNotes.get(i).getGuid());\r
+ }\r
boolean fullSync = false;\r
if (usn == 0)\r
fullSync = true;\r
while (usn < highSequence) {\r
+ refreshNeeded = true;\r
try {\r
SyncChunk chunk = \r
- noteStore.getLinkedNotebookSyncChunk(authToken, book, usn, 10, fullSync);\r
-// noteStore.getLinkedNotebookSyncChunk(linkedAuthResult.getAuthenticationToken(), book, usn, 10, fullSync);\r
+ linkedNoteStore.getLinkedNotebookSyncChunk(authToken, book, usn, 10, fullSync);\r
+ \r
+ // Expunge notes\r
+ syncExpungedNotes(chunk);\r
\r
- syncRemoteNotes(chunk.getNotes(), fullSync, linkedAuthResult.getAuthenticationToken());\r
- findNewLinkedTags(chunk.getNotes(), linkedAuthResult.getAuthenticationToken());\r
+ syncRemoteNotes(linkedNoteStore, chunk.getNotes(), fullSync, linkedAuthResult.getAuthenticationToken());\r
+ findNewLinkedTags(linkedNoteStore, chunk.getNotes(), linkedAuthResult.getAuthenticationToken());\r
+ // Sync resources\r
for (int i=0; i<chunk.getResourcesSize(); i++) {\r
- syncRemoteResource(chunk.getResources().get(i), linkedAuthResult.getAuthenticationToken());\r
+ syncRemoteResource(linkedNoteStore, chunk.getResources().get(i), linkedAuthResult.getAuthenticationToken());\r
}\r
- syncRemoteLinkedNotebooks(chunk.getNotebooks(), false, book);\r
-// String notebookGuid = conn.getLinkedNotebookTable().getNotebookGuid(book.getGuid());\r
- SharedNotebook s = noteStore.getSharedNotebookByAuth(linkedAuthResult.getAuthenticationToken());\r
+ syncRemoteLinkedNotebooks(linkedNoteStore, chunk.getNotebooks(), false, book);\r
+ SharedNotebook s = linkedNoteStore.getSharedNotebookByAuth(linkedAuthResult.getAuthenticationToken());\r
syncLinkedTags(chunk.getTags(), s.getNotebookGuid());\r
\r
- \r
- // Expunge records\r
+ // Go through & signal any notes that have changed so we can refresh the user's view\r
+ for (int i=0; i<chunk.getNotesSize(); i++) \r
+ noteSignal.noteChanged.emit(chunk.getNotes().get(i).getGuid(), null);\r
+\r
+ // Expunge Notebook records\r
for (int i=0; i<chunk.getExpungedLinkedNotebooksSize(); i++) {\r
conn.getLinkedNotebookTable().expungeNotebook(chunk.getExpungedLinkedNotebooks().get(i), false);\r
}\r
+ \r
usn = chunk.getChunkHighUSN();\r
conn.getLinkedNotebookTable().setLastSequenceDate(book.getGuid(),chunk.getCurrentTime());\r
conn.getLinkedNotebookTable().setLastSequenceNumber(book.getGuid(),chunk.getChunkHighUSN());\r
} catch (EDAMUserException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (EDAMSystemException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (EDAMNotFoundException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (TException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
}\r
}\r
\r
// Sync notebooks from a linked notebook\r
- private void syncRemoteLinkedNotebooks(List<Notebook> notebooks, boolean readOnly, LinkedNotebook linked) {\r
+ private void syncRemoteLinkedNotebooks(Client noteStore, List<Notebook> notebooks, boolean readOnly, LinkedNotebook linked) {\r
logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotebooks");\r
if (notebooks != null) {\r
for (int i=0; i<notebooks.size() && keepRunning; i++) {\r
logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteNotebooks");\r
}\r
\r
- private void findNewLinkedTags(List<Note> newNotes, String token) {\r
+ private void findNewLinkedTags(Client noteStore, List<Note> newNotes, String token) {\r
if (newNotes == null)\r
return;\r
for (int i=0; i<newNotes.size(); i++) {\r
newTag = noteStore.getTag(token, tag);\r
conn.getTagTable().addTag(newTag, false);\r
} catch (EDAMUserException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (EDAMSystemException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (EDAMNotFoundException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (TException e) {\r
- // TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
\r
}\r
\r
// Synchronize changes locally done to linked notes\r
- private void syncLocalLinkedNoteChanges(LinkedNotebook book) {\r
+ private void syncLocalLinkedNoteChanges(Client noteStore, LinkedNotebook book) {\r
String notebookGuid = conn.getLinkedNotebookTable().getNotebookGuid(book.getGuid());\r
List<Note> notes = conn.getNoteTable().getDirtyLinked(notebookGuid);\r
for (int i=0; i<notes.size(); i++) {\r
- syncLocalNote(notes.get(i), linkedAuthResult.getAuthenticationToken());\r
+ syncLocalNote(noteStore, notes.get(i), linkedAuthResult.getAuthenticationToken());\r
}\r
}\r
\r