OSDN Git Service

Cleanup public linked notebook table logic.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / SyncRunner.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 package cx.fbn.nevernote.threads;\r
20 \r
21 import java.io.BufferedOutputStream;\r
22 import java.io.File;\r
23 import java.io.FileOutputStream;\r
24 import java.io.IOException;\r
25 import java.io.InputStream;\r
26 import java.io.UnsupportedEncodingException;\r
27 import java.net.UnknownHostException;\r
28 import java.util.ArrayList;\r
29 import java.util.Calendar;\r
30 import java.util.Date;\r
31 import java.util.GregorianCalendar;\r
32 import java.util.HashMap;\r
33 import java.util.List;\r
34 import java.util.TreeSet;\r
35 import java.util.Vector;\r
36 import java.util.concurrent.LinkedBlockingQueue;\r
37 \r
38 import org.apache.http.HttpEntity;\r
39 import org.apache.http.HttpResponse;\r
40 import org.apache.http.NameValuePair;\r
41 import org.apache.http.client.ClientProtocolException;\r
42 import org.apache.http.client.HttpClient;\r
43 import org.apache.http.client.entity.UrlEncodedFormEntity;\r
44 import org.apache.http.client.methods.HttpPost;\r
45 import org.apache.http.impl.client.DefaultHttpClient;\r
46 import org.apache.http.message.BasicNameValuePair;\r
47 import org.apache.http.protocol.HTTP;\r
48 import org.apache.thrift.TException;\r
49 import org.apache.thrift.protocol.TBinaryProtocol;\r
50 import org.apache.thrift.transport.THttpClient;\r
51 import org.apache.thrift.transport.TTransportException;\r
52 \r
53 import com.evernote.edam.error.EDAMNotFoundException;\r
54 import com.evernote.edam.error.EDAMSystemException;\r
55 import com.evernote.edam.error.EDAMUserException;\r
56 import com.evernote.edam.notestore.NoteStore;\r
57 import com.evernote.edam.notestore.NoteStore.Client;\r
58 import com.evernote.edam.notestore.SyncChunk;\r
59 import com.evernote.edam.notestore.SyncState;\r
60 import com.evernote.edam.type.Data;\r
61 import com.evernote.edam.type.LinkedNotebook;\r
62 import com.evernote.edam.type.Note;\r
63 import com.evernote.edam.type.Notebook;\r
64 import com.evernote.edam.type.Resource;\r
65 import com.evernote.edam.type.SavedSearch;\r
66 import com.evernote.edam.type.SharedNotebook;\r
67 import com.evernote.edam.type.Tag;\r
68 import com.evernote.edam.type.User;\r
69 import com.evernote.edam.userstore.AuthenticationResult;\r
70 import com.evernote.edam.userstore.UserStore;\r
71 import com.trolltech.qt.core.QByteArray;\r
72 import com.trolltech.qt.core.QFile;\r
73 import com.trolltech.qt.core.QIODevice.OpenModeFlag;\r
74 import com.trolltech.qt.core.QObject;\r
75 import com.trolltech.qt.gui.QMessageBox;\r
76 \r
77 import cx.fbn.nevernote.signals.NoteIndexSignal;\r
78 import cx.fbn.nevernote.signals.NoteResourceSignal;\r
79 import cx.fbn.nevernote.signals.NoteSignal;\r
80 import cx.fbn.nevernote.signals.NotebookSignal;\r
81 import cx.fbn.nevernote.signals.SavedSearchSignal;\r
82 import cx.fbn.nevernote.signals.StatusSignal;\r
83 import cx.fbn.nevernote.signals.SyncSignal;\r
84 import cx.fbn.nevernote.signals.TagSignal;\r
85 import cx.fbn.nevernote.sql.DatabaseConnection;\r
86 import cx.fbn.nevernote.sql.DeletedItemRecord;\r
87 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
88 \r
89 public class SyncRunner extends QObject implements Runnable {\r
90         \r
91         private final ApplicationLogger logger;\r
92                 private DatabaseConnection              conn;\r
93                 private boolean                                 idle;\r
94                 public boolean                                  error;\r
95                 public volatile boolean                 isConnected;\r
96                 public volatile boolean                 keepRunning;\r
97                 public volatile String                  authToken;\r
98                 private long                                    evernoteUpdateCount;\r
99                 private final String userAgent = "NeverNote/" + System.getProperty("os.name")\r
100                                                                 +"/"+System.getProperty("java.vendor") + "/"\r
101                                                                 + System.getProperty("java.version") +";";\r
102                 \r
103                 public volatile NoteStore.Client                localNoteStore;\r
104                 private UserStore.Client                                userStore;\r
105                 \r
106                 public volatile StatusSignal                    status;\r
107                 public volatile TagSignal                               tagSignal;\r
108                 public volatile NotebookSignal                  notebookSignal;\r
109                 public volatile NoteIndexSignal                 noteIndexSignal;\r
110                 public volatile NoteSignal                              noteSignal;\r
111                 public volatile SavedSearchSignal               searchSignal;\r
112                 public volatile NoteResourceSignal              resourceSignal;\r
113                 public volatile SyncSignal                              syncSignal;\r
114                 public volatile boolean                                 authRefreshNeeded;\r
115                 public volatile boolean                                 syncNeeded;\r
116                 public volatile boolean                                 disableUploads;\r
117                 public volatile boolean                                 syncDeletedContent;\r
118                 private volatile Vector<String>                 dirtyNoteGuids;\r
119                 \r
120             public volatile String username = ""; \r
121             public volatile String password = ""; \r
122                 public volatile String userStoreUrl;\r
123             private final static String consumerKey = "baumgarte"; \r
124             private final static String consumerSecret = "eb8b5740e17cb55f";\r
125             public String noteStoreUrlBase;\r
126             private THttpClient userStoreTrans;\r
127             private TBinaryProtocol userStoreProt;\r
128             private AuthenticationResult authResult;\r
129             private AuthenticationResult linkedAuthResult;\r
130             private User user; \r
131             private long authTimeRemaining;\r
132             public long authRefreshTime;\r
133             public long failedRefreshes = 0;\r
134             public  THttpClient noteStoreTrans;\r
135             public TBinaryProtocol noteStoreProt;\r
136             public String noteStoreUrl;\r
137             public long sequenceDate;\r
138             public int updateSequenceNumber;\r
139             private boolean refreshNeeded;\r
140             private volatile LinkedBlockingQueue<String> workQueue;\r
141                 private static int MAX_QUEUED_WAITING = 1000;\r
142                 String dbuid;\r
143                 String dburl;\r
144                 String indexUrl;\r
145                 String resourceUrl;\r
146                 String dbpswd;\r
147                 String dbcpswd;\r
148                 private final TreeSet<String> ignoreTags;\r
149                 private final TreeSet<String> ignoreNotebooks;\r
150                 private final TreeSet<String> ignoreLinkedNotebooks;\r
151                 private HashMap<String,String> badTagSync;\r
152         \r
153                 \r
154                 \r
155         public SyncRunner(String logname, String u, String i, String r, String uid, String pswd, String cpswd) {\r
156                 logger = new ApplicationLogger(logname);\r
157                 \r
158                 noteSignal = new NoteSignal();\r
159                 status = new StatusSignal();\r
160                 tagSignal = new TagSignal();\r
161                 notebookSignal = new NotebookSignal();\r
162                 noteIndexSignal = new NoteIndexSignal();\r
163                 noteSignal = new NoteSignal();\r
164                 searchSignal = new SavedSearchSignal();\r
165                 syncSignal = new SyncSignal();\r
166                 resourceSignal = new NoteResourceSignal();\r
167                 resourceUrl = r;\r
168                 indexUrl = i;\r
169                 dbuid = uid;\r
170                 dburl = u;\r
171                 dbpswd = pswd;\r
172                 dbcpswd = cpswd;\r
173 //              this.setAutoDelete(false);\r
174                 \r
175                 isConnected = false;\r
176                 syncNeeded = false;\r
177                 authRefreshNeeded = false;\r
178                 keepRunning = true;\r
179                 idle = true;\r
180                 disableUploads = false;\r
181                 ignoreTags = new TreeSet<String>();\r
182                 ignoreNotebooks = new TreeSet<String>();\r
183                 ignoreLinkedNotebooks = new TreeSet<String>();\r
184                 \r
185 //              setAutoDelete(false);\r
186                 workQueue=new LinkedBlockingQueue<String>(MAX_QUEUED_WAITING);\r
187         }\r
188         @Override\r
189         public void run() {\r
190                 try {\r
191                         logger.log(logger.EXTREME, "Starting thread");\r
192                         conn = new DatabaseConnection(logger, dburl, indexUrl, resourceUrl, dbuid, dbpswd, dbcpswd, 200);\r
193                         while(keepRunning) {\r
194                                 String work = workQueue.take();\r
195                                 logger.log(logger.EXTREME, "Work found: " +work);\r
196                                 if (work.equalsIgnoreCase("stop")) {\r
197                                         idle=false;\r
198                                         return;\r
199                                 }\r
200                                 idle=false;\r
201                                 error=false;\r
202                                 if (authRefreshNeeded == true || !isConnected) {\r
203                                         logger.log(logger.EXTREME, "Refreshing connection");\r
204                                         refreshConnection();\r
205                                 }\r
206                                 if (syncNeeded) {\r
207                                         logger.log(logger.EXTREME, "SyncNeeded is true");\r
208                                         refreshNeeded=false;\r
209                                         sequenceDate = conn.getSyncTable().getLastSequenceDate();\r
210                                         updateSequenceNumber = conn.getSyncTable().getUpdateSequenceNumber();\r
211                                         try {\r
212                                                 logger.log(logger.EXTREME, "Beginning sync");\r
213                                                 evernoteSync(localNoteStore);\r
214                                                 logger.log(logger.EXTREME, "Sync finished");\r
215                                         } catch (UnknownHostException e) {\r
216                                                 status.message.emit(e.getMessage());\r
217                                         }\r
218                                 }\r
219                                 idle=true;\r
220                                 logger.log(logger.EXTREME, "Signaling refresh finished.  refreshNeeded=" +refreshNeeded);\r
221                                 syncSignal.finished.emit(refreshNeeded);\r
222                                 if (error) {\r
223                                         syncSignal.errorDisconnect.emit();\r
224                                         status.message.emit(tr("Error synchronizing - see log for details."));\r
225                                 }\r
226                         }\r
227                 }       \r
228                 catch (InterruptedException e1) {\r
229                         e1.printStackTrace();\r
230                 }\r
231                 conn.dbShutdown();\r
232         }\r
233 \r
234         \r
235         public DatabaseConnection getConnection() {\r
236                 return conn;\r
237         }\r
238 \r
239         public boolean isIdle() {\r
240                 return idle;\r
241         }\r
242 \r
243 \r
244         public void setConnected(boolean c) {\r
245                 isConnected = c;\r
246         }\r
247         public void setKeepRunning(boolean r) {\r
248                 logger.log(logger.EXTREME, "Setting keepRunning=" +r);\r
249                 keepRunning = r;\r
250         }\r
251         public void setNoteStore(NoteStore.Client c) {\r
252                 logger.log(logger.EXTREME, "Setting NoteStore in sync thread");\r
253                 localNoteStore = c;\r
254         }\r
255         public void setUserStore(UserStore.Client c) {\r
256                 logger.log(logger.EXTREME, "Setting UserStore in sync thread");\r
257                 userStore = c;\r
258         }\r
259 \r
260         public void setEvernoteUpdateCount(long s) {\r
261                 logger.log(logger.EXTREME, "Setting Update Count in sync thread");\r
262                 evernoteUpdateCount = s;\r
263         }\r
264         \r
265         //***************************************************************\r
266     //***************************************************************\r
267     //** These functions deal with Evernote communications\r
268     //***************************************************************\r
269     //***************************************************************\r
270         // Synchronize changes with Evernote\r
271         @SuppressWarnings("unused")\r
272         private void evernoteSync(Client noteStore) throws java.net.UnknownHostException {\r
273                 logger.log(logger.HIGH, "Entering SyncRunner.evernoteSync");\r
274                 \r
275                 // Rebuild list of tags & notebooks to ignore\r
276                 ignoreNotebooks.clear();\r
277                 List<String> ignore = conn.getSyncTable().getIgnoreRecords("NOTEBOOK");\r
278                 for (int i=0; i<ignore.size(); i++) \r
279                         ignoreNotebooks.add(ignore.get(i));\r
280                 \r
281                 ignore.clear();\r
282                 ignore = conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK");\r
283                 for (int i=0; i<ignore.size(); i++) \r
284                         ignoreLinkedNotebooks.add(ignore.get(i));\r
285                 \r
286                 ignoreTags.clear();\r
287                 ignore = conn.getSyncTable().getIgnoreRecords("TAG");\r
288                 for (int i=0; i<ignore.size(); i++) \r
289                         ignoreTags.add(ignore.get(i));\r
290 \r
291                 // Make sure we are connected & should keep running\r
292                 if (isConnected && keepRunning) {\r
293                         error = false;\r
294                         logger.log(logger.EXTREME, "Synchronizing with Evernote");\r
295                         status.message.emit(tr("Synchronizing with Evernote"));\r
296                         \r
297                         // Get user information\r
298                         try {\r
299                                 logger.log(logger.EXTREME, "getting user from userstore");\r
300                                 User user = userStore.getUser(authToken);\r
301                                 logger.log(logger.EXTREME, "Saving user information");\r
302                                 syncSignal.saveUserInformation.emit(user);\r
303                         } catch (EDAMUserException e1) {\r
304                                 e1.printStackTrace();\r
305                                 status.message.emit(tr("User exception getting user account information.  Aborting sync and disconnecting"));\r
306                                 syncSignal.errorDisconnect.emit();\r
307                                 error = true;\r
308                                 enDisconnect();\r
309                                 return;\r
310                         } catch (EDAMSystemException e1) {\r
311                                 e1.printStackTrace();\r
312                                 status.message.emit(tr("System error user account information.  Aborting sync and disconnecting!"));\r
313                                 syncSignal.errorDisconnect.emit();\r
314                                 error = true;\r
315                                 enDisconnect();\r
316                                 return;\r
317                         } catch (TException e1) {\r
318                                 e1.printStackTrace();\r
319                                 syncSignal.errorDisconnect.emit();\r
320                                 error = true;\r
321                                 status.message.emit(tr("Transaction error getting user account information.  Aborting sync and disconnecting!"));\r
322                                 enDisconnect();\r
323                                 return;\r
324                         }\r
325                         \r
326                         // Get sync state\r
327                         SyncState syncState = null;\r
328                         try {   \r
329                                 logger.log(logger.EXTREME, "Getting sync state");\r
330                                 syncState = noteStore.getSyncState(authToken);  \r
331                                 syncSignal.saveUploadAmount.emit(syncState.getUploaded());\r
332                                 syncSignal.saveEvernoteUpdateCount.emit(syncState.getUpdateCount());\r
333                                 evernoteUpdateCount = syncState.getUpdateCount();\r
334                         } catch (EDAMUserException e) {\r
335                                 e.printStackTrace();\r
336                                 status.message.emit(tr("Error getting sync state! Aborting sync and disconnecting!"));\r
337                                 syncSignal.errorDisconnect.emit();\r
338                                 enDisconnect();\r
339                                 return;\r
340                         } catch (EDAMSystemException e) {\r
341                                 e.printStackTrace();\r
342                                 status.message.emit(tr("Error getting sync state! Aborting sync and disconnecting!"));\r
343                                 syncSignal.errorDisconnect.emit();\r
344                                 enDisconnect();\r
345                                 return;\r
346                         } catch (TException e) {\r
347                                 e.printStackTrace();\r
348                                 status.message.emit(tr("Error getting sync state! Aborting sync and disconnecting!"));\r
349                                 syncSignal.errorDisconnect.emit();\r
350                                 enDisconnect();\r
351                                 return;\r
352                         }\r
353                         \r
354                         if (syncState == null) {\r
355                                 logger.log(logger.EXTREME, "Sync State is null");\r
356                                 status.message.emit(tr("Syncronization Error!"));\r
357                                 return;\r
358                         }\r
359 \r
360                         // Determine what to do. \r
361                         // If we need to do a full sync.\r
362                         logger.log(logger.LOW, "Full Sequence Before: " +syncState.getFullSyncBefore());\r
363                         logger.log(logger.LOW, "Last Sequence Date: " +sequenceDate);\r
364                         if (syncState.getFullSyncBefore() > sequenceDate) {\r
365                                 logger.log(logger.EXTREME, "Full sequence date has expired");\r
366                                 sequenceDate = 0;\r
367                                 conn.getSyncTable().setLastSequenceDate(0);\r
368                                 updateSequenceNumber = 0;\r
369                                 conn.getSyncTable().setUpdateSequenceNumber(0);\r
370                         }\r
371                         // Check for "special" sync instructions\r
372                         String syncLinked = conn.getSyncTable().getRecord("FullLinkedNotebookSync");\r
373                         String syncShared = conn.getSyncTable().getRecord("FullLinkedNotebookSync");\r
374                         String syncNotebooks = conn.getSyncTable().getRecord("FullNotebookSync");\r
375                         String syncInkNoteImages = conn.getSyncTable().getRecord("FullInkNoteImageSync");\r
376                         if (syncLinked != null) {\r
377                                 downloadAllLinkedNotebooks(localNoteStore);\r
378                         }\r
379                         if (syncShared != null) {\r
380                                 downloadAllSharedNotebooks(localNoteStore);\r
381                         }\r
382                         if (syncNotebooks != null) {\r
383                                 downloadAllNotebooks(localNoteStore);\r
384                         }\r
385                         \r
386                         if (syncInkNoteImages != null) {\r
387                                 List<String> guids = conn.getNoteTable().noteResourceTable.findInkNotes();\r
388                                 for (int i=0; i<guids.size(); i++) {\r
389                                         downloadInkNoteImage(guids.get(i), authToken);\r
390                                 }\r
391                                 conn.getSyncTable().deleteRecord("FullInkNoteImageSync");\r
392                         }\r
393                         \r
394                         // If there are remote changes\r
395                         logger.log(logger.LOW, "Update Count: " +syncState.getUpdateCount());\r
396                         logger.log(logger.LOW, "Last Update Count: " +updateSequenceNumber);\r
397                         \r
398                         if (syncState.getUpdateCount() > updateSequenceNumber) {\r
399                                 logger.log(logger.EXTREME, "Refresh needed is true");\r
400                                 refreshNeeded = true;\r
401                                 logger.log(logger.EXTREME, "Downloading changes");\r
402                                 syncRemoteToLocal(localNoteStore);\r
403                         }\r
404                         \r
405                         //*****************************************\r
406                         //* Sync linked/shared notebooks \r
407                         //*****************************************\r
408                         syncLinkedNotebooks();\r
409                         \r
410                         if (!disableUploads) {\r
411                                 logger.log(logger.EXTREME, "Uploading changes");\r
412                                 // Synchronize remote changes\r
413                                 if (!error)\r
414                                         syncExpunged(localNoteStore);\r
415                                 if (!error)\r
416                                         syncLocalTags(localNoteStore);\r
417                                 if (!error)\r
418                                         syncLocalNotebooks(localNoteStore);\r
419                                 if (!error)\r
420                                         syncLocalLinkedNotebooks(localNoteStore);\r
421                                 if (!error) \r
422                                         syncDeletedNotes(localNoteStore);\r
423                                 if (!error)\r
424                                         syncLocalNotes();\r
425                                 if (!error)\r
426                                         syncLocalSavedSearches(localNoteStore);\r
427                         }\r
428                         \r
429                         status.message.emit(tr("Cleaning up"));\r
430                         List<String> notes = conn.getNoteTable().expungeIgnoreSynchronizedNotes(conn.getSyncTable().getIgnoreRecords("NOTEBOOK"), \r
431                                         conn.getSyncTable().getIgnoreRecords("TAG"), conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK"));\r
432                         if (notes.size() > 0)\r
433                                 syncSignal.refreshLists.emit();\r
434                         \r
435                         //*****************************************\r
436                         //* End of synchronization\r
437                         //*****************************************\r
438                         if (refreshNeeded)\r
439                                 syncSignal.refreshLists.emit();\r
440                         \r
441                         if (!error) {\r
442                                 logger.log(logger.EXTREME, "Sync completed.  Errors=" +error);\r
443                                 if (!disableUploads) \r
444                                         status.message.emit(tr("Synchronizing complete"));\r
445                                 else\r
446                                         status.message.emit(tr("Download syncronization complete.  Uploads have been disabled."));\r
447                                 \r
448                                 logger.log(logger.EXTREME, "Saving sync time");\r
449                                 if (syncState.getCurrentTime() > sequenceDate)\r
450                                         sequenceDate = syncState.getCurrentTime();\r
451                                 if (syncState.getUpdateCount() > updateSequenceNumber)\r
452                                         updateSequenceNumber = syncState.getUpdateCount();\r
453                                 conn.getSyncTable().setLastSequenceDate(sequenceDate);\r
454                                 conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
455                         }\r
456                 }\r
457                 logger.log(logger.HIGH, "Leaving SyncRunner.evernoteSync");\r
458         }\r
459         \r
460         // Sync deleted items with Evernote\r
461         private void syncExpunged(Client noteStore) {\r
462                 logger.log(logger.HIGH, "Entering SyncRunner.syncExpunged");\r
463                 \r
464                 List<DeletedItemRecord> expunged = conn.getDeletedTable().getAllDeleted();\r
465                 boolean error = false;\r
466                 for (int i=0; i<expunged.size() && keepRunning; i++) {\r
467 \r
468                         if (authRefreshNeeded)\r
469                                 if (!refreshConnection())\r
470                                         return;\r
471 \r
472                         try {\r
473                                 if (expunged.get(i).type.equalsIgnoreCase("TAG")) {\r
474                                         logger.log(logger.EXTREME, "Tag expunged");\r
475                                         updateSequenceNumber = noteStore.expungeTag(authToken, expunged.get(i).guid);\r
476                                         conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
477                                         \r
478                                 }\r
479                                 if      (expunged.get(i).type.equalsIgnoreCase("NOTEBOOK")) {\r
480                                         logger.log(logger.EXTREME, "Notebook expunged");\r
481                                         updateSequenceNumber = noteStore.expungeNotebook(authToken, expunged.get(i).guid);\r
482                                         conn.getSyncTable().setLastSequenceDate(sequenceDate);\r
483                                 }\r
484                                 if (expunged.get(i).type.equalsIgnoreCase("NOTE")) {\r
485                                         logger.log(logger.EXTREME, "Note expunged");\r
486                                         updateSequenceNumber = noteStore.deleteNote(authToken, expunged.get(i).guid);\r
487                                         refreshNeeded = true;\r
488                                         conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
489                                 }\r
490                                 if (expunged.get(i).type.equalsIgnoreCase("SAVEDSEARCH")) {\r
491                                         logger.log(logger.EXTREME, "saved search expunged");\r
492                                         updateSequenceNumber = noteStore.expungeSearch(authToken, expunged.get(i).guid);\r
493                                         conn.getSyncTable().setLastSequenceDate(sequenceDate);\r
494                                 }\r
495                         } catch (EDAMUserException e) {\r
496                                 logger.log(logger.LOW, "EDAM User Excepton in syncExpunged: " +expunged.get(i).guid);\r
497                                 logger.log(logger.LOW, e.getStackTrace());\r
498                                 error = true;\r
499                         } catch (EDAMSystemException e) {\r
500                                 logger.log(logger.LOW, "EDAM System Excepton in syncExpunged: "+expunged.get(i).guid);\r
501                                 logger.log(logger.LOW, e.getStackTrace());\r
502                                 error=true;\r
503                         } catch (EDAMNotFoundException e) {\r
504                                 logger.log(logger.LOW, "EDAM Not Found Excepton in syncExpunged: "+expunged.get(i).guid);\r
505 //                              logger.log(logger.LOW, e.getStackTrace());\r
506                                 //error=true;\r
507                         } catch (TException e) {\r
508                                 logger.log(logger.LOW, "EDAM TExcepton in syncExpunged: "+expunged.get(i).guid);\r
509                                 logger.log(logger.LOW, e.getStackTrace());\r
510                                 error=true;\r
511                         }\r
512                 }\r
513                 if (!error)\r
514                         conn.getDeletedTable().expungeAllDeletedRecords();\r
515                 \r
516                 logger.log(logger.HIGH, "Leaving SyncRunner.syncExpunged");\r
517 \r
518         }\r
519         private void syncDeletedNotes(Client noteStore) {\r
520                 if (syncDeletedContent)\r
521                         return;\r
522                 logger.log(logger.HIGH, "Entering SyncRunner.syncDeletedNotes");\r
523                 status.message.emit(tr("Synchronizing deleted notes."));\r
524 \r
525                 List<Note> notes = conn.getNoteTable().getDirty();\r
526                 // Sync the local notebooks with Evernote's\r
527                 for (int i=0; i<notes.size() && keepRunning; i++) {\r
528                         \r
529                         if (authRefreshNeeded)\r
530                                 if (!refreshConnection())\r
531                                         return;\r
532                         \r
533                         Note enNote = notes.get(i);\r
534                         try {\r
535                                 if (enNote.getUpdateSequenceNum() > 0 && (enNote.isActive() == false || enNote.getDeleted() > 0)) {\r
536                                         if (syncDeletedContent) {\r
537                                                 logger.log(logger.EXTREME, "Deleted note found & synch content selected");\r
538                                                 Note delNote = conn.getNoteTable().getNote(enNote.getGuid(), true, true, true, true, true);\r
539                                                 delNote = getNoteContent(delNote);\r
540                                                 delNote = noteStore.updateNote(authToken, delNote);\r
541                                                 enNote.setUpdateSequenceNum(delNote.getUpdateSequenceNum());\r
542                                                 conn.getNoteTable().updateNoteSequence(enNote.getGuid(), enNote.getUpdateSequenceNum());\r
543                                         } else {\r
544                                                 logger.log(logger.EXTREME, "Deleted note found & sync content not selected");\r
545                                                 int usn = noteStore.deleteNote(authToken, enNote.getGuid());\r
546                                                 enNote.setUpdateSequenceNum(usn);\r
547                                                 conn.getNoteTable().updateNoteSequence(enNote.getGuid(), enNote.getUpdateSequenceNum());                                                \r
548                                         }\r
549                                         logger.log(logger.EXTREME, "Resetting deleted dirty flag");\r
550                                         conn.getNoteTable().resetDirtyFlag(enNote.getGuid());\r
551                                         updateSequenceNumber = enNote.getUpdateSequenceNum();\r
552                                         logger.log(logger.EXTREME, "Saving sequence number");\r
553                                         conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
554                                 }                               \r
555                         } catch (EDAMUserException e) {\r
556                                 //logger.log(logger.LOW, "*** EDAM User Excepton syncLocalNotes "+e);\r
557                                 //status.message.emit("Error sending local note: " +e.getParameter());\r
558                                 //logger.log(logger.LOW, e.toString()); \r
559                                 //error = true;\r
560                         } catch (EDAMSystemException e) {\r
561                                 logger.log(logger.LOW, "*** EDAM System Excepton syncLocalNotes "+e);\r
562                                 status.message.emit(tr("Error: ") +e);\r
563                                 logger.log(logger.LOW, e.toString());           \r
564                                 error = true;\r
565                         } catch (EDAMNotFoundException e) {\r
566                                 //logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalNotes " +e);\r
567                                 //status.message.emit("Error deleting local note: " +e +" - Continuing");\r
568                                 //logger.log(logger.LOW, e.toString());         \r
569                                 //error = true;\r
570                         } catch (TException e) {\r
571                                 logger.log(logger.LOW, "*** EDAM TExcepton syncLocalNotes "+e);\r
572                                 status.message.emit(tr("Error sending local note: ") +e);\r
573                                 logger.log(logger.LOW, e.toString());   \r
574                                 error = true;\r
575                         }               \r
576                 }\r
577         }\r
578         // Sync notes with Evernote\r
579         private void syncLocalNotes() {\r
580                 logger.log(logger.HIGH, "Entering SyncRunner.syncNotes");\r
581                 status.message.emit(tr("Sending local notes."));\r
582 \r
583                 List<Note> notes = conn.getNoteTable().getDirty();\r
584                 // Sync the local notebooks with Evernote's\r
585                 for (int i=0; i<notes.size() && keepRunning; i++) {\r
586                         syncLocalNote(localNoteStore, notes.get(i), authToken);\r
587                 }\r
588                 logger.log(logger.HIGH, "Entering SyncRunner.syncNotes");\r
589 \r
590         }\r
591         // Sync notes with Evernote\r
592         private void syncLocalNote(Client noteStore, Note enNote, String token) {\r
593                 logger.log(logger.HIGH, "Entering SyncRunner.syncNotes");\r
594                 status.message.emit(tr("Sending local notes."));\r
595 \r
596                 if (authRefreshNeeded)\r
597                         if (!refreshConnection())\r
598                                 return;\r
599                         \r
600                 if (enNote.isActive()) {\r
601                         try {\r
602                                 logger.log(logger.EXTREME, "Active dirty note found - non new");\r
603                                 if (enNote.getUpdateSequenceNum() > 0) {\r
604                                         enNote = getNoteContent(enNote);\r
605                                         logger.log(logger.MEDIUM, "Updating note : "+ enNote.getGuid() +" <title>" +enNote.getTitle()+"</title>");\r
606                                         enNote = noteStore.updateNote(token, enNote);\r
607                                 } else { \r
608                                         logger.log(logger.EXTREME, "Active dirty found - new note");\r
609                                         logger.log(logger.MEDIUM, "Creating note : "+ enNote.getGuid() +" <title>" +enNote.getTitle()+"</title>");\r
610                                         String oldGuid = enNote.getGuid();\r
611                                         enNote = getNoteContent(enNote);\r
612                                         enNote = noteStore.createNote(token, enNote);\r
613                                         noteSignal.guidChanged.emit(oldGuid, enNote.getGuid());\r
614                                         conn.getNoteTable().updateNoteGuid(oldGuid, enNote.getGuid());\r
615                                 }\r
616                                 updateSequenceNumber = enNote.getUpdateSequenceNum();\r
617                                 logger.log(logger.EXTREME, "Saving note");\r
618                                 conn.getNoteTable().updateNoteSequence(enNote.getGuid(), enNote.getUpdateSequenceNum());\r
619                                 List<Resource> rl = enNote.getResources();\r
620                                 logger.log(logger.EXTREME, "Getting note resources");\r
621                                 for (int j=0; j<enNote.getResourcesSize() && keepRunning; j++) {\r
622                                         Resource newRes = rl.get(j);\r
623                                         Data d = newRes.getData();\r
624                                         if (d!=null) {  \r
625                                                 logger.log(logger.EXTREME, "Calculating resource hash");\r
626                                                 String hash = byteArrayToHexString(d.getBodyHash());\r
627                                                 logger.log(logger.EXTREME, "updating resources by hash");\r
628                                                 String oldGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(enNote.getGuid(), hash);\r
629                                                 conn.getNoteTable().updateNoteResourceGuidbyHash(enNote.getGuid(), newRes.getGuid(), hash);\r
630                                                 resourceSignal.resourceGuidChanged.emit(enNote.getGuid(), oldGuid, newRes.getGuid());\r
631                                         }\r
632                                 }\r
633                                 logger.log(logger.EXTREME, "Resetting note dirty flag");\r
634                                 conn.getNoteTable().resetDirtyFlag(enNote.getGuid());\r
635                                 updateSequenceNumber = enNote.getUpdateSequenceNum();\r
636                                 logger.log(logger.EXTREME, "Emitting note sequence number change");\r
637                                 conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
638 \r
639                         } catch (EDAMUserException e) {\r
640                                 logger.log(logger.LOW, "*** EDAM User Excepton syncLocalNotes "+e);\r
641                                 status.message.emit(tr("Error sending local note: ")     +e.getParameter());\r
642                                 logger.log(logger.LOW, e.toString());   \r
643                                 error = true;\r
644                         } catch (EDAMSystemException e) {\r
645                                 logger.log(logger.LOW, "*** EDAM System Excepton syncLocalNotes "+e);\r
646                                 status.message.emit(tr("Error: ") +e);\r
647                                 logger.log(logger.LOW, e.toString());           \r
648                                 error = true;\r
649                         } catch (EDAMNotFoundException e) {\r
650                                 logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalNotes " +e);\r
651                                 status.message.emit(tr("Error sending local note: ") +e);\r
652                                 logger.log(logger.LOW, e.toString());   \r
653                                 error = true;\r
654                         } catch (TException e) {\r
655                                 logger.log(logger.LOW, "*** EDAM TExcepton syncLocalNotes "+e);\r
656                                 status.message.emit(tr("Error sending local note: ") +e);\r
657                                 logger.log(logger.LOW, e.toString());   \r
658                                 error = true;\r
659                         }\r
660                 }\r
661                 logger.log(logger.HIGH, "Leaving SyncRunner.syncLocalNote");\r
662 \r
663         }\r
664 \r
665         // Sync Notebooks with Evernote\r
666         private void syncLocalNotebooks(Client noteStore) {\r
667                 logger.log(logger.HIGH, "Entering SyncRunner.syncLocalNotebooks");\r
668                 \r
669                 status.message.emit(tr("Sending local notebooks."));\r
670                 List<Notebook> remoteList = new ArrayList<Notebook>();\r
671                 try {\r
672                         logger.log(logger.EXTREME, "Getting remote notebooks to compare with local");\r
673                         remoteList = noteStore.listNotebooks(authToken);\r
674                 } catch (EDAMUserException e1) {\r
675                         logger.log(logger.LOW, "*** EDAM User Excepton syncLocalNotebooks getting remote Notebook List");\r
676                         status.message.emit(tr("Error: ") +e1);\r
677                         logger.log(logger.LOW, e1.toString());          \r
678                         error = true;\r
679                 } catch (EDAMSystemException e1) {\r
680                         logger.log(logger.LOW, "*** EDAM System Excepton syncLocalNotebooks getting remote Notebook List");\r
681                         status.message.emit(tr("Error: ") +e1);\r
682                         logger.log(logger.LOW, e1.toString());  \r
683                         error = true;\r
684                 } catch (TException e1) {\r
685                         logger.log(logger.LOW, "*** EDAM Transaction Excepton syncLocalNotebooks getting remote Notebook List");\r
686                         status.message.emit(tr("Error: ") +e1);\r
687                         logger.log(logger.LOW, e1.toString());  \r
688                         error = true;\r
689                 }\r
690                 logger.log(logger.EXTREME, "Getting local dirty notebooks");\r
691                 List<Notebook> notebooks = conn.getNotebookTable().getDirty();\r
692                 int sequence;\r
693                 // Sync the local notebooks with Evernote's\r
694                 for (int i=0; i<notebooks.size() && keepRunning; i++) {\r
695                         \r
696                         if (authRefreshNeeded)\r
697                                 if (!refreshConnection())\r
698                                         return;\r
699                         \r
700                         Notebook enNotebook = notebooks.get(i);\r
701                         try {\r
702                                 if (enNotebook.getUpdateSequenceNum() > 0) {\r
703                                         logger.log(logger.EXTREME, "Existing notebook is dirty");\r
704                                         sequence = noteStore.updateNotebook(authToken, enNotebook);\r
705                                 } else {\r
706                                         logger.log(logger.EXTREME, "New dirty notebook found");\r
707                                         String oldGuid = enNotebook.getGuid();\r
708                                         boolean found = false;\r
709                                         \r
710                                         // Look for a notebook with the same name.  If one is found, we don't need \r
711                                         // to create another one\r
712                                         logger.log(logger.EXTREME, "Looking for matching notebook name");\r
713                                         for (int k=0; k<remoteList.size() && !found && keepRunning; k++) {\r
714                                                 if (remoteList.get(k).getName().equalsIgnoreCase(enNotebook.getName())) {\r
715                                                         enNotebook = remoteList.get(k);\r
716                                                         logger.log(logger.EXTREME, "Matching notebook found");\r
717                                                         found = true;\r
718                                                 }\r
719                                         }\r
720                                         if (!found)\r
721                                                 enNotebook = noteStore.createNotebook(authToken, enNotebook);\r
722                                         \r
723                                         logger.log(logger.EXTREME, "Updating notebook in database");\r
724                                         conn.getNotebookTable().updateNotebookGuid(oldGuid, enNotebook.getGuid());\r
725                                         sequence = enNotebook.getUpdateSequenceNum();\r
726                                 }\r
727                                 logger.log(logger.EXTREME, "Updating notebook sequence in database");\r
728                                 conn.getNotebookTable().updateNotebookSequence(enNotebook.getGuid(), sequence);\r
729                                 logger.log(logger.EXTREME, "Resetting dirty flag in notebook");\r
730                                 conn.getNotebookTable().resetDirtyFlag(enNotebook.getGuid());\r
731                                 updateSequenceNumber = sequence;\r
732                                 logger.log(logger.EXTREME, "Emitting sequence number to main thread");\r
733                                 conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
734                         } catch (EDAMUserException e) {\r
735                                 logger.log(logger.LOW, "*** EDAM User Excepton syncLocalNotebooks");\r
736                                 logger.log(logger.LOW, e.toString());   \r
737                                 error = true;\r
738                         } catch (EDAMSystemException e) {\r
739                                 logger.log(logger.LOW, "*** EDAM System Excepton syncLocalNotebooks");\r
740                                 logger.log(logger.LOW, e.toString());           \r
741                                 error = true;\r
742                         } catch (EDAMNotFoundException e) {\r
743                                 logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalNotebooks");\r
744                                 logger.log(logger.LOW, e.toString());           \r
745                                 error = true;\r
746                         } catch (TException e) {\r
747                                 logger.log(logger.LOW, "*** EDAM TExcepton syncLocalNotebooks");\r
748                                 logger.log(logger.LOW, e.toString());   \r
749                                 error = true;\r
750                         }               \r
751                 }\r
752                 logger.log(logger.HIGH, "Leaving SyncRunner.syncLocalNotebooks");\r
753 \r
754         }\r
755         // Sync Tags with Evernote\r
756         private void syncLocalTags(Client noteStore) {\r
757                 logger.log(logger.HIGH, "Entering SyncRunner.syncLocalTags");\r
758                 List<Tag> remoteList = new ArrayList<Tag>();\r
759                 status.message.emit(tr("Sending local tags."));\r
760                 \r
761                 try {\r
762                         logger.log(logger.EXTREME, "Getting remote tags to compare names with the local tags");\r
763                         remoteList = noteStore.listTags(authToken);\r
764                 } catch (EDAMUserException e1) {\r
765                         logger.log(logger.LOW, "*** EDAM User Excepton syncLocalTags getting remote Tag List");\r
766                         status.message.emit(tr("Error: ") +e1);\r
767                         logger.log(logger.LOW, e1.toString());  \r
768                         error = true;\r
769                 } catch (EDAMSystemException e1) {\r
770                         logger.log(logger.LOW, "*** EDAM System Excepton syncLocalTags getting remote Tag List");\r
771                         status.message.emit(tr("Error: ") +e1);\r
772                         logger.log(logger.LOW, e1.toString());          \r
773                         error = true;\r
774                 } catch (TException e1) {\r
775                         logger.log(logger.LOW, "*** EDAM Transaction Excepton syncLocalTags getting remote Tag List");\r
776                         status.message.emit(tr("Error: ") +e1);\r
777                         logger.log(logger.LOW, e1.toString());  \r
778                         error = true;\r
779                 }               \r
780                 \r
781                 int sequence;\r
782                 \r
783                 if (badTagSync == null)\r
784                         badTagSync = new HashMap<String,String>();\r
785                 else\r
786                         badTagSync.clear();\r
787                 \r
788                 Tag enTag = findNextTag();\r
789                 while(enTag!=null) {\r
790                         if (authRefreshNeeded)\r
791                                 if (!refreshConnection())\r
792                                         return;\r
793 \r
794                         try {\r
795                                 if (enTag.getUpdateSequenceNum() > 0) {\r
796                                         logger.log(logger.EXTREME, "Updating tag");\r
797                                         sequence = noteStore.updateTag(authToken, enTag);\r
798                                 } else {\r
799                                         \r
800                                         // Look for a tag with the same name.  If one is found, we don't need \r
801                                         // to create another one\r
802                                         logger.log(logger.EXTREME, "New tag.  Comparing with remote names");\r
803                                         boolean found = false;\r
804                                         String oldGuid = enTag.getGuid();\r
805                                         for (int k=0; k<remoteList.size() && !found && keepRunning; k++) {\r
806                                                 if (remoteList.get(k).getName().equalsIgnoreCase(enTag.getName())) {\r
807                                                         conn.getTagTable().updateTagGuid(enTag.getGuid(), remoteList.get(k).getGuid());\r
808                                                         enTag = remoteList.get(k);\r
809                                                         logger.log(logger.EXTREME, "Matching tag name found");\r
810                                                         found = true;\r
811                                                 }\r
812                                         }\r
813                                         if (!found)\r
814                                                 enTag = noteStore.createTag(authToken, enTag);\r
815                                         else\r
816                                                 enTag.setUpdateSequenceNum(noteStore.updateTag(authToken,enTag));\r
817                                         sequence = enTag.getUpdateSequenceNum();\r
818                                         if (!oldGuid.equals(enTag.getGuid())) {\r
819                                                 logger.log(logger.EXTREME, "Updating tag guid");\r
820                                                 conn.getTagTable().updateTagGuid(oldGuid, enTag.getGuid());\r
821                                         }\r
822                                 }\r
823                                 logger.log(logger.EXTREME, "Updating tag sequence number");\r
824                                 conn.getTagTable().updateTagSequence(enTag.getGuid(), sequence);\r
825                                 logger.log(logger.EXTREME, "Resetting tag dirty flag");\r
826                                 conn.getTagTable().resetDirtyFlag(enTag.getGuid());\r
827                                 logger.log(logger.EXTREME, "Emitting sequence number to the main thread.");\r
828                                 updateSequenceNumber = sequence;\r
829                                 conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
830                         } catch (EDAMUserException e) {\r
831                                 logger.log(logger.LOW, "*** EDAM User Excepton syncLocalTags: " +enTag.getName());\r
832                                 logger.log(logger.LOW, e.toString());\r
833                                 badTagSync.put(enTag.getGuid(),null);\r
834                                 error = true;\r
835                         } catch (EDAMSystemException e) {\r
836                                 logger.log(logger.LOW, "** EDAM System Excepton syncLocalTags: " +enTag.getName());\r
837                                 logger.log(logger.LOW, e.toString());   \r
838                                 badTagSync.put(enTag.getGuid(),null);\r
839                                 error = true;\r
840                         } catch (EDAMNotFoundException e) {\r
841                                 logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalTags: " +enTag.getName());\r
842                                 logger.log(logger.LOW, e.toString());\r
843                                 badTagSync.put(enTag.getGuid(),null);\r
844                                 error = true;\r
845                         } catch (TException e) {\r
846                                 logger.log(logger.LOW, "*** EDAM TExcepton syncLocalTags: " +enTag.getName());\r
847                                 logger.log(logger.LOW, e.toString());\r
848                                 badTagSync.put(enTag.getGuid(),null);\r
849                                 error = true;\r
850                         }       \r
851                         \r
852                         // Find the next tag\r
853                         logger.log(logger.EXTREME, "Finding next tag");\r
854                         enTag = findNextTag();\r
855                 }\r
856                 logger.log(logger.HIGH, "Leaving SyncRunner.syncLocalTags");\r
857         }\r
858         private void syncLocalLinkedNotebooks(Client noteStore) {\r
859                 logger.log(logger.HIGH, "Entering SyncRunner.syncLocalLinkedNotebooks");\r
860                 \r
861                 List<String> list = conn.getLinkedNotebookTable().getDirtyGuids();\r
862                 for (int i=0; i<list.size(); i++) {\r
863                         LinkedNotebook book = conn.getLinkedNotebookTable().getNotebook(list.get(i));\r
864                         try {\r
865                                 noteStore.updateLinkedNotebook(authToken, book);\r
866                         } catch (EDAMUserException e) {\r
867                                 logger.log(logger.LOW, "*** EDAM User Excepton syncLocalLinkedNotebooks");\r
868                                 status.message.emit(tr("Error: ") +e);\r
869                                 logger.log(logger.LOW, e.toString());           \r
870                                 error = true;\r
871                                 e.printStackTrace();\r
872                         } catch (EDAMNotFoundException e) {\r
873                                 logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalLinkedNotebooks");\r
874                                 status.message.emit(tr("Error: ") +e);\r
875                                 logger.log(logger.LOW, e.toString());           \r
876                                 error = true;\r
877                                 e.printStackTrace();\r
878                         } catch (EDAMSystemException e) {\r
879                                 logger.log(logger.LOW, "*** EDAM System Excepton syncLocalLinkedNotebooks");\r
880                                 status.message.emit(tr("Error: ") +e);\r
881                                 logger.log(logger.LOW, e.toString());           \r
882                                 error = true;\r
883                                 e.printStackTrace();\r
884                         } catch (TException e) {\r
885                                 logger.log(logger.LOW, "*** EDAM TExcepton syncLocalLinkedNotebooks");\r
886                                 status.message.emit(tr("Error: ") +e);\r
887                                 logger.log(logger.LOW, e.toString());           \r
888                                 error = true;\r
889                                 e.printStackTrace();\r
890                         }\r
891                 }\r
892                 logger.log(logger.HIGH, "Leaving SyncRunner.syncLocalLinkedNotebooks");\r
893         }\r
894         // Sync Saved Searches with Evernote\r
895         private void syncLocalSavedSearches(Client noteStore) {\r
896                 logger.log(logger.HIGH, "Entering SyncRunner.syncLocalSavedSearches");\r
897                 List<SavedSearch> remoteList = new ArrayList<SavedSearch>();\r
898                 status.message.emit(tr("Sending saved searches."));\r
899         \r
900                 logger.log(logger.EXTREME, "Getting saved searches to compare with local");\r
901                 try {\r
902                         remoteList = noteStore.listSearches(authToken);\r
903                 } catch (EDAMUserException e1) {\r
904                         logger.log(logger.LOW, "*** EDAM User Excepton syncLocalTags getting remote saved search List");\r
905                         status.message.emit(tr("Error: ") +e1);\r
906                         logger.log(logger.LOW, e1.toString());  \r
907                         error = true;\r
908                 } catch (EDAMSystemException e1) {\r
909                         logger.log(logger.LOW, "*** EDAM System Excepton syncLocalTags getting remote saved search List");\r
910                         status.message.emit(tr("Error: ") +e1);\r
911                         logger.log(logger.LOW, e1.toString());          \r
912                         error = true;\r
913                 } catch (TException e1) {\r
914                         logger.log(logger.LOW, "*** EDAM Transaction Excepton syncLocalTags getting remote saved search List");\r
915                         status.message.emit(tr("Error: ") +e1);\r
916                         logger.log(logger.LOW, e1.toString());  \r
917                         error = true;\r
918                 }               \r
919                 \r
920                 List<SavedSearch> searches = conn.getSavedSearchTable().getDirty();\r
921                 int sequence;\r
922                 // Sync the local notebooks with Evernote's\r
923                 logger.log(logger.EXTREME, "Beginning to send saved searches");\r
924                 for (int i=0; i<searches.size() &&  keepRunning; i++) {\r
925                         \r
926                         if (authRefreshNeeded)\r
927                                 if (!refreshConnection())\r
928                                         return;\r
929                         \r
930                         SavedSearch enSearch = searches.get(i);\r
931                         try {\r
932                                 if (enSearch.getUpdateSequenceNum() > 0) \r
933                                         sequence = noteStore.updateSearch(authToken, enSearch);\r
934                                 else {\r
935                                         logger.log(logger.EXTREME, "New saved search found.");\r
936                                         // Look for a tag with the same name.  If one is found, we don't need \r
937                                         // to create another one\r
938                                         boolean found = false;\r
939                                         logger.log(logger.EXTREME, "Matching remote saved search names with local");\r
940                                         for (int k=0; k<remoteList.size() && !found && keepRunning; k++) {\r
941                                                 if (remoteList.get(k).getName().equalsIgnoreCase(enSearch.getName())) {\r
942                                                         enSearch = remoteList.get(k);\r
943                                                         found = true;\r
944                                                         logger.log(logger.EXTREME, "Matching saved search found");\r
945                                                         sequence = enSearch.getUpdateSequenceNum();\r
946                                                 }\r
947                                         }\r
948 \r
949                                         String oldGuid = enSearch.getGuid();\r
950                                         if (!found)\r
951                                                 enSearch = noteStore.createSearch(authToken, enSearch);\r
952                                         sequence = enSearch.getUpdateSequenceNum();\r
953                                         logger.log(logger.EXTREME, "Updating tag guid in local database");\r
954                                         conn.getTagTable().updateTagGuid(oldGuid, enSearch.getGuid());\r
955                                 }\r
956                                 logger.log(logger.EXTREME, "Updating tag sequence in local database");\r
957                                 conn.getSavedSearchTable().updateSavedSearchSequence(enSearch.getGuid(), sequence);\r
958                                 logger.log(logger.EXTREME, "Resetting tag dirty flag");\r
959                                 conn.getSavedSearchTable().resetDirtyFlag(enSearch.getGuid());\r
960                                 logger.log(logger.EXTREME, "Emitting sequence number to the main thread.");\r
961                                 updateSequenceNumber = sequence;\r
962                                 conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
963                         } catch (EDAMUserException e) {\r
964                                 logger.log(logger.LOW, "*** EDAM User Excepton syncLocalTags");\r
965                                 logger.log(logger.LOW, e.toString());   \r
966                                 error = true;\r
967                         } catch (EDAMSystemException e) {\r
968                                 logger.log(logger.LOW, "** EDAM System Excepton syncLocalTags");\r
969                                 logger.log(logger.LOW, e.toString());   \r
970                                 error = true;\r
971                         } catch (EDAMNotFoundException e) {\r
972                                 logger.log(logger.LOW, "*** EDAM Not Found Excepton syncLocalTags");\r
973                                 logger.log(logger.LOW, e.toString());   \r
974                                 error = true;\r
975                         } catch (TException e) {\r
976                                 logger.log(logger.LOW, "*** EDAM TExcepton syncLocalTags");\r
977                                 logger.log(logger.LOW, e.toString());   \r
978                                 error = true;\r
979                         }               \r
980                 }\r
981 \r
982                 logger.log(logger.HIGH, "Entering SyncRunner.syncLocalSavedSearches");\r
983         }       \r
984 \r
985         // Sync evernote changes with local database\r
986         private void syncRemoteToLocal(Client noteStore) {\r
987                 logger.log(logger.HIGH, "Entering SyncRunner.syncRemoteToLocal");\r
988 \r
989                 List<Note> dirtyNotes = conn.getNoteTable().getDirty();\r
990                 dirtyNoteGuids = new Vector<String>();\r
991                 for (int i=0; i<dirtyNotes.size() && keepRunning; i++) {\r
992                         dirtyNoteGuids.add(dirtyNotes.get(i).getGuid());\r
993                 }\r
994                 \r
995                 int chunkSize = 10;\r
996                 SyncChunk chunk = null;\r
997                 boolean fullSync = false;\r
998                 boolean more = true;\r
999                 \r
1000                 if (updateSequenceNumber == 0)\r
1001                         fullSync = true;\r
1002                 \r
1003                 status.message.emit(tr("Downloading 0% complete."));\r
1004                 \r
1005                 while(more &&  keepRunning) {\r
1006                         \r
1007                         if (authRefreshNeeded)\r
1008                                 if (!refreshConnection())\r
1009                                         return;\r
1010                         \r
1011                         int sequence = updateSequenceNumber;\r
1012                         try {\r
1013 //                              conn.beginTransaction();\r
1014                                 logger.log(logger.EXTREME, "Getting chunk from Evernote");\r
1015                                 chunk = noteStore.getSyncChunk(authToken, sequence, chunkSize, fullSync);\r
1016                         } catch (EDAMUserException e) {\r
1017                                 error = true;\r
1018                                 e.printStackTrace();\r
1019                                 status.message.emit(e.getMessage());\r
1020                         } catch (EDAMSystemException e) {\r
1021                                 error = true;\r
1022                                 e.printStackTrace();\r
1023                                 status.message.emit(e.getMessage());\r
1024                         } catch (TException e) {\r
1025                                 error = true;\r
1026                                 e.printStackTrace();\r
1027                                 status.message.emit(e.getMessage());\r
1028                         } \r
1029                         if (error || chunk == null) \r
1030                                 return;\r
1031                                 \r
1032                 \r
1033                         \r
1034                         syncRemoteTags(chunk.getTags());\r
1035                         syncRemoteSavedSearches(chunk.getSearches());\r
1036                         syncRemoteNotebooks(chunk.getNotebooks());\r
1037                         syncRemoteNotes(noteStore, chunk.getNotes(), fullSync, authToken);\r
1038                         syncRemoteResources(noteStore, chunk.getResources());\r
1039                         syncRemoteLinkedNotebooks(chunk.getLinkedNotebooks());\r
1040                         \r
1041                         // Signal about any updated notes to invalidate the cache\r
1042                         for (int i=0; i<chunk.getNotesSize(); i++) \r
1043                                 noteSignal.noteChanged.emit(chunk.getNotes().get(i).getGuid(), null); \r
1044                         syncExpungedNotes(chunk);\r
1045                         \r
1046                         \r
1047                         // Check for more notes\r
1048                         if (chunk.getChunkHighUSN() <= updateSequenceNumber) \r
1049                                 more = false;\r
1050                         if (error)\r
1051                                 more = false;\r
1052                         logger.log(logger.EXTREME, "More notes? " +more);\r
1053 \r
1054                         \r
1055                         // Save the chunk sequence number\r
1056                         if (!error && chunk.getChunkHighUSN() > 0 && keepRunning) {\r
1057                                 logger.log(logger.EXTREME, "emitting sequence number to main thread");\r
1058                                 updateSequenceNumber = chunk.getChunkHighUSN();\r
1059                                 conn.getSyncTable().setLastSequenceDate(chunk.getCurrentTime());\r
1060                                 conn.getSyncTable().setUpdateSequenceNumber(updateSequenceNumber);\r
1061 //                              conn.commitTransaction();\r
1062                         }\r
1063                         \r
1064                         \r
1065                         if (more) {\r
1066                                 long pct = chunk.getChunkHighUSN() * 100;\r
1067                                 conn.getSyncTable().setLastSequenceDate(chunk.getCurrentTime());\r
1068                                 pct = pct/evernoteUpdateCount;\r
1069                                 status.message.emit(tr("Downloading ") +new Long(pct).toString()+tr("% complete."));\r
1070                         }\r
1071 //                      conn.commitTransaction();\r
1072                 }\r
1073                 logger.log(logger.HIGH, "Leaving SyncRunner.syncRemoteToLocal");\r
1074         }\r
1075         // Sync expunged notes\r
1076         private void syncExpungedNotes(SyncChunk chunk) {\r
1077                 // Do the local deletes\r
1078                 logger.log(logger.EXTREME, "Doing local deletes");\r
1079                 List<String> guid = chunk.getExpungedNotes();\r
1080                 if (guid != null) {\r
1081                         for (int i=0; i<guid.size() && keepRunning; i++) {\r
1082                                 String notebookGuid = "";\r
1083                                 Note localNote = conn.getNoteTable().getNote(guid.get(i), false, false, false, false, false);\r
1084                                 if (localNote != null) {\r
1085                                         conn.getNoteTable().updateNoteSequence(guid.get(i), 0);\r
1086                                         notebookGuid = localNote.getNotebookGuid();\r
1087                                 }\r
1088                                 if (!conn.getNotebookTable().isNotebookLocal(notebookGuid)) {\r
1089                                         logger.log(logger.EXTREME, "Expunging local note from database");\r
1090                                         conn.getNoteTable().expungeNote(guid.get(i), true, false);\r
1091                                 }\r
1092                         }\r
1093                 }\r
1094                 guid = chunk.getExpungedNotebooks();\r
1095                 if (guid != null)\r
1096                         for (int i=0; i<guid.size() && keepRunning; i++) {\r
1097                                 logger.log(logger.EXTREME, "Expunging local notebook from database");\r
1098                                 conn.getNotebookTable().expungeNotebook(guid.get(i), false);\r
1099                         }\r
1100                 guid = chunk.getExpungedTags();\r
1101                 if (guid != null)\r
1102                         for (int i=0; i<guid.size() && keepRunning; i++) {\r
1103                                 logger.log(logger.EXTREME, "Expunging tags from local database");\r
1104                                 conn.getTagTable().expungeTag(guid.get(i), false);\r
1105                         }\r
1106                 guid = chunk.getExpungedSearches();\r
1107                 if (guid != null) \r
1108                         for (int i=0; i<guid.size() && keepRunning; i++) {\r
1109                                 logger.log(logger.EXTREME, "Expunging saved search from local database");\r
1110                                 conn.getSavedSearchTable().expungeSavedSearch(guid.get(i), false);\r
1111                         }\r
1112                 guid = chunk.getExpungedLinkedNotebooks();\r
1113                 if (guid != null) \r
1114                         for (int i=0; i<guid.size() && keepRunning; i++) {\r
1115                                 logger.log(logger.EXTREME, "Expunging linked notebook from local database");\r
1116                                 conn.getLinkedNotebookTable().expungeNotebook(guid.get(i), false);\r
1117                         }\r
1118 \r
1119         }\r
1120         // Sync remote tags\r
1121         private void syncRemoteTags(List<Tag> tags) {\r
1122                 logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteTags");\r
1123                 if (tags != null) {\r
1124                         for (int i=0; i<tags.size() && keepRunning; i++) {\r
1125                                 String oldGuid;\r
1126                                 oldGuid = conn.getTagTable().findTagByName(tags.get(i).getName());\r
1127                                 if (oldGuid != null && !tags.get(i).getGuid().equalsIgnoreCase(oldGuid))\r
1128                                         conn.getTagTable().updateTagGuid(oldGuid, tags.get(i).getGuid());\r
1129                                 conn.getTagTable().syncTag(tags.get(i), false);\r
1130                         }\r
1131                 }\r
1132                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteTags");\r
1133         }\r
1134         // Sync remote saved searches\r
1135         private void syncRemoteSavedSearches(List<SavedSearch> searches) {\r
1136                 logger.log(logger.EXTREME, "Entering SyncRunner.syncSavedSearches");\r
1137                 if (searches != null) {\r
1138                         for (int i=0; i<searches.size() && keepRunning; i++) {\r
1139                                 String oldGuid;\r
1140                                 oldGuid = conn.getSavedSearchTable().findSavedSearchByName(searches.get(i).getName());\r
1141                                 if (oldGuid != null && !searches.get(i).getGuid().equalsIgnoreCase(oldGuid))\r
1142                                         conn.getSavedSearchTable().updateSavedSearchGuid(oldGuid, searches.get(i).getGuid());\r
1143                                 conn.getSavedSearchTable().syncSavedSearch(searches.get(i), false);\r
1144                         }\r
1145                 }\r
1146                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncSavedSearches");\r
1147         }\r
1148         // Sync remote linked notebooks\r
1149         private void syncRemoteLinkedNotebooks(List<LinkedNotebook> books) {\r
1150                 logger.log(logger.EXTREME, "Entering SyncRunner.syncLinkedNotebooks");\r
1151                 if (books != null) {\r
1152                         for (int i=0; i<books.size() && keepRunning; i++) {\r
1153                                 conn.getLinkedNotebookTable().updateNotebook(books.get(i), false); \r
1154                         }\r
1155                 }\r
1156                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncLinkedNotebooks");\r
1157         }\r
1158         // Sync remote Notebooks 2\r
1159         private void syncRemoteNotebooks(List<Notebook> notebooks) {\r
1160                 logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotebooks");\r
1161                 if (notebooks != null) {\r
1162                         for (int i=0; i<notebooks.size() && keepRunning; i++) {\r
1163                                 String oldGuid;\r
1164                                 oldGuid = conn.getNotebookTable().findNotebookByName(notebooks.get(i).getName());\r
1165                                 if (oldGuid != null && !conn.getNotebookTable().isNotebookLocal(oldGuid) && !notebooks.get(i).getGuid().equalsIgnoreCase(oldGuid))\r
1166                                         conn.getNotebookTable().updateNotebookGuid(oldGuid, notebooks.get(i).getGuid());\r
1167                                 conn.getNotebookTable().syncNotebook(notebooks.get(i), false); \r
1168                                 \r
1169                                 // Synchronize shared notebook information\r
1170 //                              if (notebooks.get(i).getSharedNotebookIdsSize() > 0) {\r
1171 //                                      conn.getSharedNotebookTable().expungeNotebookByGuid(notebooks.get(i).getGuid(), false);\r
1172 //                                      for (int j=0; j<notebooks.get(i).getSharedNotebookIdsSize(); j++) {\r
1173 //                                              syncRemoteSharedNotebook(notebooks.get(i).getGuid(), notebooks.get(i).getSharedNotebookIds().get(j), authToken);\r
1174 //                                      }\r
1175 //                              }\r
1176                         }\r
1177                 }                       \r
1178                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteNotebooks");\r
1179         }\r
1180         // Sync remote shared notebook\r
1181 //      private void syncRemoteSharedNotebook(String guid, Long id, String token) {\r
1182 //              List<SharedNotebook> books = noteStore.getSharedNotebookByAuth(authToken);\r
1183 //      }\r
1184         // Sync remote Resources\r
1185         private void syncRemoteResources(Client noteStore, List<Resource> resource) {\r
1186                 logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteResources");\r
1187                 if (resource != null) {\r
1188                         for (int i=0; i<resource.size() && keepRunning; i++) {\r
1189                                 syncRemoteResource(noteStore, resource.get(i), authToken);\r
1190                         }\r
1191                 }\r
1192                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteResources");\r
1193         }\r
1194         // Sync remote resource\r
1195         private void syncRemoteResource(Client noteStore, Resource resource, String authToken) {\r
1196                 // This is how the logic for this works.\r
1197                 // 1.) If the resource is not in the local database, we add it.\r
1198                 // 2.) If a copy of the resource is in the local database and the note isn't dirty, we update the local copy\r
1199                 // 3.) If a copy of the resource is in the local databbase and it is dirty and the hash doesn't match, we ignore it because there\r
1200                 //     is a conflict.  The note conflict should get a copy of the resource at that time.\r
1201                 \r
1202                 boolean saveNeeded = false;\r
1203                 /* #1 */                Resource r = getEvernoteResource(noteStore, resource.getGuid(), true,true,true, authToken);\r
1204                                                 Resource l = conn.getNoteTable().noteResourceTable.getNoteResource(r.getGuid(), false);\r
1205                                                 if (l == null) {\r
1206                                                         saveNeeded = true;\r
1207                                                 } else {\r
1208                 /* #2 */                        boolean isNoteDirty = conn.getNoteTable().isNoteDirty(r.getNoteGuid());\r
1209                                                         if (!isNoteDirty)\r
1210                                                                 saveNeeded = true;\r
1211                                                         else {\r
1212                 /* #3 */                                String remoteHash = "";\r
1213                                                                 if (r != null && r.getData() != null && r.getData().getBodyHash() != null)\r
1214                                                                         remoteHash = byteArrayToHexString(r.getData().getBodyHash());\r
1215                                                                 String localHash = "";\r
1216                                                                 if (l != null && l.getData() != null && l.getData().getBodyHash() != null)\r
1217                                                                         remoteHash = byteArrayToHexString(l.getData().getBodyHash());\r
1218                                                 \r
1219                                                                 if (localHash.equalsIgnoreCase(remoteHash))\r
1220                                                                         saveNeeded = true;\r
1221                                                         }\r
1222                                                 }\r
1223                                                 \r
1224                                                 if (saveNeeded) \r
1225                                                         conn.getNoteTable().noteResourceTable.updateNoteResource(r, false);\r
1226                                                 if (r.getMime().equalsIgnoreCase("application/vnd.evernote.ink"))\r
1227                                                         downloadInkNoteImage(r.getGuid(), authToken);\r
1228 \r
1229         }\r
1230         // Sync remote notes\r
1231         private void syncRemoteNotes(Client noteStore, List<Note> note, boolean fullSync, String token) {\r
1232                 logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotes");\r
1233                 if (note != null) {\r
1234                         for (int i=0; i<note.size() && keepRunning; i++) {\r
1235                                 Note n = getEvernoteNote(noteStore, note.get(i).getGuid(), true, fullSync, true,true, token);\r
1236                                 syncRemoteNote(n, fullSync, token);\r
1237                         }\r
1238                 }\r
1239                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteNotes");\r
1240         }\r
1241         private void syncRemoteNote(Note n, boolean fullSync, String token) {\r
1242                 if (n!=null) {\r
1243                         \r
1244                         // Basically, this is how the sync logic for a note works.\r
1245                         // If the remote note has changed and the local has not, we\r
1246                         // accept the change.\r
1247                         // If both the local & remote have changed but the sequence\r
1248                         // numbers are the same, we don't accept the change.  This\r
1249                         // seems to happen when attachments are indexed by the server.\r
1250                         // If both the local & remote have changed and the sequence numbers\r
1251                         // are different we move the local copy to a local notebook (making sure\r
1252                         // to copy all resources) and we accept the new one.                    \r
1253                         boolean conflictingNote = true;\r
1254                         logger.log(logger.EXTREME, "Checking for duplicate note " +n.getGuid());\r
1255                         if (dirtyNoteGuids != null && dirtyNoteGuids.contains(n.getGuid())) { \r
1256                                 logger.log(logger.EXTREME, "Conflict check beginning");\r
1257                                 conflictingNote = checkForConflict(n);\r
1258                                 logger.log(logger.EXTREME, "Conflict check results " +conflictingNote);\r
1259                                 if (conflictingNote)\r
1260                                         moveConflictingNote(n.getGuid());\r
1261                         }\r
1262                         boolean ignoreNote = false;\r
1263                         if (ignoreNotebooks.contains(n.getNotebookGuid()))\r
1264                                 ignoreNote = true;\r
1265                         for (int i=0; i<n.getTagGuidsSize(); i++) {\r
1266                                 if (ignoreTags.contains(n.getTagGuids().get(i))) {\r
1267                                         ignoreNote = true;\r
1268                                         i=n.getTagGuidsSize();\r
1269                                 }\r
1270                         }\r
1271                                 \r
1272                         if ((conflictingNote || fullSync) && !ignoreNote) {\r
1273                                 logger.log(logger.EXTREME, "Saving Note");\r
1274                                 conn.getNoteTable().syncNote(n, false);\r
1275                                 // The following was commented out because it caused a race condition on the database where resources \r
1276                                 // may be lost.  We do the same thing elsewhere;.\r
1277 //                              noteSignal.noteChanged.emit(n.getGuid(), null);   // Signal to ivalidate note cache \r
1278                                 noteSignal.noteDownloaded.emit(n, true);                // Signal to add note to index\r
1279                                         logger.log(logger.EXTREME, "Note Saved");\r
1280                                 if (fullSync && n.getResources() != null) {\r
1281                                         for (int q=0; q<n.getResources().size() && keepRunning; q++) {\r
1282                                                 logger.log(logger.EXTREME, "Getting note resources.");\r
1283                                                 conn.getNoteTable().noteResourceTable.updateNoteResource(n.getResources().get(q), false);\r
1284                                                 if (n.getResources().get(q).getMime().equalsIgnoreCase("application/vnd.evernote.ink"))\r
1285                                                         downloadInkNoteImage(n.getResources().get(q).getGuid(), token);\r
1286                                         }\r
1287                                 }\r
1288                         }\r
1289                 }\r
1290         }\r
1291         private Note getEvernoteNote(Client noteStore, String guid, boolean withContent, boolean withResourceData, boolean withResourceRecognition, boolean withResourceAlternateData, String token) { \r
1292                 Note n = null;\r
1293                 try {\r
1294                         logger.log(logger.EXTREME, "Retrieving note " +guid);\r
1295                         n = noteStore.getNote(token, guid, withContent, withResourceData, withResourceRecognition, withResourceAlternateData);\r
1296                         logger.log(logger.EXTREME, "Note " +guid +" has been retrieved.");\r
1297                 } catch (EDAMUserException e) {\r
1298                         logger.log(logger.LOW, "*** EDAM User Excepton getEvernoteNote");\r
1299                         logger.log(logger.LOW, e.toString());   \r
1300                         error = true;\r
1301                         e.printStackTrace();\r
1302                 } catch (EDAMSystemException e) {\r
1303                         logger.log(logger.LOW, "*** EDAM System Excepton getEvernoteNote");\r
1304                         logger.log(logger.LOW, e.toString());   \r
1305                         error = true;\r
1306                         e.printStackTrace();\r
1307                 } catch (EDAMNotFoundException e) {\r
1308                         logger.log(logger.LOW, "*** EDAM Not Found Excepton getEvernoteNote");\r
1309                         logger.log(logger.LOW, e.toString());   \r
1310                         error = true;\r
1311                         e.printStackTrace();\r
1312                 } catch (TException e) {\r
1313                         logger.log(logger.LOW, "*** EDAM TExcepton getEvernoteNote");\r
1314                         logger.log(logger.LOW, e.toString());   \r
1315                         error = true;\r
1316                         e.printStackTrace();\r
1317                 }\r
1318                 return n;\r
1319         }\r
1320         private Resource getEvernoteResource(Client noteStore, String guid, boolean withData, boolean withRecognition, boolean withAttributes, String token) { \r
1321                 Resource n = null;\r
1322                 try {\r
1323                         logger.log(logger.EXTREME, "Retrieving resource " +guid);\r
1324                         n = noteStore.getResource(token, guid, withData, withRecognition, withAttributes, withAttributes);\r
1325                         logger.log(logger.EXTREME, "Resource " +guid +" has been retrieved.");\r
1326                 } catch (EDAMUserException e) {\r
1327                         logger.log(logger.LOW, "*** EDAM User Excepton getEvernoteNote");\r
1328                         logger.log(logger.LOW, e.toString());   \r
1329                         error = true;\r
1330                         e.printStackTrace();\r
1331                 } catch (EDAMSystemException e) {\r
1332                         logger.log(logger.LOW, "*** EDAM System Excepton getEvernoteNote");\r
1333                         logger.log(logger.LOW, e.toString());   \r
1334                         error = true;\r
1335                         e.printStackTrace();\r
1336                 } catch (EDAMNotFoundException e) {\r
1337                         logger.log(logger.LOW, "*** EDAM Not Found Excepton getEvernoteNote");\r
1338                         logger.log(logger.LOW, e.toString());   \r
1339                         error = true;\r
1340                         e.printStackTrace();\r
1341                 } catch (TException e) {\r
1342                         logger.log(logger.LOW, "*** EDAM TExcepton getEvernoteNote");\r
1343                         logger.log(logger.LOW, e.toString());   \r
1344                         error = true;\r
1345                         e.printStackTrace();\r
1346                 }\r
1347                 return n;\r
1348         }\r
1349 \r
1350         \r
1351         private boolean checkForConflict(Note n) {\r
1352                 logger.log(logger.EXTREME, "Checking note sequence number  " +n.getGuid());\r
1353                 Note oldNote = conn.getNoteTable().getNote(n.getGuid(), false, false, false, false, false);\r
1354                 logger.log(logger.EXTREME, "Local/Remote sequence numbers: " +oldNote.getUpdateSequenceNum()+"/"+n.getUpdateSequenceNum());\r
1355                 if (oldNote.getUpdateSequenceNum() == n.getUpdateSequenceNum())\r
1356                         return false;\r
1357                 return true;\r
1358         }\r
1359         \r
1360         private void moveConflictingNote(String guid) {\r
1361                 logger.log(logger.EXTREME, "Conflicting change found for note " +guid);\r
1362                 List<Notebook> books = conn.getNotebookTable().getAllLocal();\r
1363                 String notebookGuid = null;\r
1364                 for (int i=0; i<books.size() && keepRunning; i++) {\r
1365                         if (books.get(i).getName().equalsIgnoreCase("Conflicting Changes (local)") ||\r
1366                                         books.get(i).getName().equalsIgnoreCase("Conflicting Changes")) {\r
1367                                 notebookGuid = books.get(i).getGuid();\r
1368                                 i=books.size();\r
1369                         }\r
1370                 }\r
1371                 \r
1372                 if (notebookGuid == null) {\r
1373                         logger.log(logger.EXTREME, "Building conflicting change notebook " +guid);\r
1374                         Calendar currentTime = new GregorianCalendar();\r
1375                         Long l = new Long(currentTime.getTimeInMillis());\r
1376                         long prevTime = l;\r
1377                         while (prevTime==l) {\r
1378                                 currentTime = new GregorianCalendar();\r
1379                                 l=currentTime.getTimeInMillis();\r
1380                         }\r
1381                         String randint = new String(Long.toString(l));\r
1382                 \r
1383                         Notebook newBook = new Notebook();\r
1384                         newBook.setUpdateSequenceNum(0);\r
1385                         newBook.setGuid(randint);\r
1386                         newBook.setName("Conflicting Changes");\r
1387                         newBook.setServiceCreated(new Date().getTime());\r
1388                         newBook.setServiceUpdated(new Date().getTime());\r
1389                         newBook.setDefaultNotebook(false);\r
1390                         newBook.setPublished(false);\r
1391                         \r
1392                         conn.getNotebookTable().addNotebook(newBook, false, true);\r
1393                         notebookSignal.listChanged.emit();\r
1394                         notebookGuid = newBook.getGuid();\r
1395                         refreshNeeded = true;\r
1396                 }\r
1397                 \r
1398                 // Now that we have a good notebook guid, we need to move the conflicting note\r
1399                 // to the local notebook\r
1400                 logger.log(logger.EXTREME, "Moving conflicting note " +guid);\r
1401                 Calendar currentTime = new GregorianCalendar();\r
1402                 Long l = new Long(currentTime.getTimeInMillis());\r
1403                 long prevTime = l;\r
1404                 while (prevTime==l) {\r
1405                         currentTime = new GregorianCalendar();\r
1406                         l = currentTime.getTimeInMillis();\r
1407                 }\r
1408                 String newGuid = new String(Long.toString(l));\r
1409                                         \r
1410                 Note oldNote = conn.getNoteTable().getNote(guid, true, true, false, false, false);\r
1411                 for (int i=0; i<oldNote.getResources().size() && keepRunning; i++) {\r
1412                         l = new Long(currentTime.getTimeInMillis());\r
1413                         String newResG = new String(Long.toString(l));\r
1414                         String oldResG = oldNote.getResources().get(i).getGuid();\r
1415                         conn.getNoteTable().noteResourceTable.resetUpdateSequenceNumber(oldResG, true);\r
1416                         conn.getNoteTable().noteResourceTable.updateNoteResourceGuid(oldResG, newResG, true);\r
1417                 }\r
1418                 \r
1419                 conn.getNoteTable().resetNoteSequence(guid);\r
1420                 conn.getNoteTable().updateNoteGuid(guid, newGuid);\r
1421                 conn.getNoteTable().updateNoteNotebook(newGuid, notebookGuid, true);\r
1422                 \r
1423                 noteSignal.notebookChanged.emit(newGuid, notebookGuid);\r
1424                 refreshNeeded = true;\r
1425                 noteSignal.guidChanged.emit(guid,newGuid);\r
1426         }\r
1427         \r
1428 \r
1429 \r
1430         \r
1431         //******************************************************\r
1432         //******************************************************\r
1433         //** Utility Functions\r
1434         //******************************************************\r
1435         //******************************************************\r
1436         // Convert a byte array to a hex string\r
1437         private static String byteArrayToHexString(byte data[]) {\r
1438                 StringBuffer buf = new StringBuffer();\r
1439             for (byte element : data) {\r
1440                 int halfbyte = (element >>> 4) & 0x0F;\r
1441                 int two_halfs = 0;\r
1442                 do {\r
1443                         if ((0 <= halfbyte) && (halfbyte <= 9))\r
1444                                buf.append((char) ('0' + halfbyte));\r
1445                            else\r
1446                                 buf.append((char) ('a' + (halfbyte - 10)));\r
1447                         halfbyte = element & 0x0F;\r
1448                 } while(two_halfs++ < 1);\r
1449             }\r
1450             return buf.toString();              \r
1451         }\r
1452 \r
1453         \r
1454         \r
1455         //*******************************************************\r
1456         //* Find dirty tags, which do not have newly created parents\r
1457         //*******************************************************\r
1458         private Tag findNextTag() {\r
1459                 logger.log(logger.HIGH, "Entering SyncRunner.findNextTag");\r
1460                 Tag nextTag = null;\r
1461                 List<Tag> tags = conn.getTagTable().getDirty();\r
1462                 \r
1463                 // Find the parent.  If the parent has a sequence > 0 then it is a good\r
1464                 // parent.\r
1465                 for (int i=0; i<tags.size() && keepRunning; i++) {\r
1466                         if (!badTagSync.containsKey(tags.get(i).getGuid())) {\r
1467                                 if (tags.get(i).getParentGuid() == null) {\r
1468                                         logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - tag found without parent");\r
1469                                         return tags.get(i);\r
1470                                 }\r
1471                                 Tag parentTag = conn.getTagTable().getTag(tags.get(i).getParentGuid());\r
1472                                 if (parentTag.getUpdateSequenceNum() > 0) {\r
1473                                         logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - tag found");\r
1474                                         return tags.get(i);\r
1475                                 }\r
1476                         }\r
1477                 }\r
1478                 \r
1479                 logger.log(logger.HIGH, "Leaving SyncRunner.findNextTag - no tags returned");\r
1480                 return nextTag;\r
1481         }\r
1482         \r
1483         \r
1484            // Connect to Evernote\r
1485     public boolean enConnect()  {\r
1486         try {\r
1487                         userStoreTrans = new THttpClient(userStoreUrl);\r
1488                         userStoreTrans.setCustomHeader("User-Agent", userAgent);\r
1489                 } catch (TTransportException e) {\r
1490                         QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "Transport Excepton", e.getLocalizedMessage());\r
1491                         mb.exec();\r
1492                         e.printStackTrace();\r
1493                 }\r
1494                 userStoreProt = new TBinaryProtocol(userStoreTrans);\r
1495             userStore = new UserStore.Client(userStoreProt, userStoreProt);\r
1496             syncSignal.saveUserStore.emit(userStore);\r
1497             try {\r
1498                         authResult = userStore.authenticate(username, password, consumerKey, consumerSecret);\r
1499                 } catch (EDAMUserException e) {\r
1500                         QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "Error", "Incorrect username/password");\r
1501                         mb.exec();\r
1502                         isConnected = false;\r
1503                         return false;\r
1504                 } catch (EDAMSystemException e) {\r
1505                         QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "EDAM System Excepton", e.getLocalizedMessage());\r
1506                         mb.exec();\r
1507                         e.printStackTrace();\r
1508                         isConnected = false;\r
1509                 } catch (TException e) {\r
1510                         QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "Transport Excepton", e.getLocalizedMessage());\r
1511                         mb.exec();\r
1512                         e.printStackTrace();\r
1513                         isConnected = false;\r
1514                 }\r
1515                 \r
1516             boolean versionOk = false;\r
1517                 try {\r
1518                         versionOk = userStore.checkVersion("NeverNote", \r
1519                     com.evernote.edam.userstore.Constants.EDAM_VERSION_MAJOR, \r
1520                       com.evernote.edam.userstore.Constants.EDAM_VERSION_MINOR);\r
1521                 } catch (TException e) {\r
1522                         e.printStackTrace();\r
1523                         isConnected = false;\r
1524                 } \r
1525             if (!versionOk) { \r
1526                 System.err.println("Incomatible EDAM client protocol version"); \r
1527                 isConnected = false;\r
1528             }\r
1529             if (authResult != null) {\r
1530                 user = authResult.getUser(); \r
1531                 authToken = authResult.getAuthenticationToken(); \r
1532                 noteStoreUrl = noteStoreUrlBase + user.getShardId();\r
1533                 syncSignal.saveAuthToken.emit(authToken);\r
1534                 syncSignal.saveNoteStore.emit(localNoteStore);\r
1535                 \r
1536          \r
1537                 try {\r
1538                         noteStoreTrans = new THttpClient(noteStoreUrl);\r
1539                         noteStoreTrans.setCustomHeader("User-Agent", userAgent);\r
1540                 } catch (TTransportException e) {\r
1541                         QMessageBox mb = new QMessageBox(QMessageBox.Icon.Critical, "Transport Excepton", e.getLocalizedMessage());\r
1542                         mb.exec();\r
1543                         e.printStackTrace();\r
1544                         isConnected = false;\r
1545                 } \r
1546                 noteStoreProt = new TBinaryProtocol(noteStoreTrans);\r
1547                 localNoteStore = \r
1548                         new NoteStore.Client(noteStoreProt, noteStoreProt); \r
1549                 isConnected = true;\r
1550                 authTimeRemaining = authResult.getExpiration() - authResult.getCurrentTime();\r
1551                 authRefreshTime = authTimeRemaining / 2;\r
1552             }\r
1553             \r
1554                 // Get user information\r
1555                 try {\r
1556                         User user = userStore.getUser(authToken);\r
1557                         syncSignal.saveUserInformation.emit(user);\r
1558                 } catch (EDAMUserException e1) {\r
1559                         e1.printStackTrace();\r
1560                 } catch (EDAMSystemException e1) {\r
1561                         e1.printStackTrace();\r
1562                 } catch (TException e1) {\r
1563                         e1.printStackTrace();\r
1564                 }\r
1565             \r
1566             return isConnected;\r
1567     }\r
1568         // Disconnect from the database                         \r
1569     public void enDisconnect() {\r
1570         isConnected = false;\r
1571     }\r
1572     // Refresh the connection\r
1573     private synchronized boolean refreshConnection() {\r
1574                 logger.log(logger.EXTREME, "Entering SyncRunner.refreshConnection()");\r
1575 //        Calendar cal = Calendar.getInstance();\r
1576                 \r
1577         // If we are not connected let's get out of here\r
1578         if (!isConnected)\r
1579                 return false;\r
1580         \r
1581                 // If we fail too many times, then let's give up.\r
1582                 if (failedRefreshes >=5) {\r
1583                         logger.log(logger.EXTREME, "Refresh attempts have failed.  Disconnecting.");\r
1584                         isConnected = false;\r
1585                         status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
1586                         return false;\r
1587                 }\r
1588         \r
1589                 // If this is the first time through, then we need to set this\r
1590 //              if (authRefreshTime == 0 || cal.getTimeInMillis() > authRefreshTime) \r
1591 //                      authRefreshTime = cal.getTimeInMillis();\r
1592                 \r
1593  //             // Default to checking again in 5 min.  This in case we fail.\r
1594  //             authRefreshTime = authRefreshTime +(5*60*1000);     \r
1595 \r
1596                 // Try to get a new token\r
1597                 AuthenticationResult newAuth = null; \r
1598                 logger.log(logger.EXTREME, "Beginning to try authentication refresh");\r
1599         try {\r
1600                 if (userStore != null && authToken != null) \r
1601                         newAuth = userStore.refreshAuthentication(authToken); \r
1602                 else\r
1603                         return false;\r
1604                 logger.log(logger.EXTREME, "UserStore.refreshAuthentication has succeeded.");\r
1605                 } catch (EDAMUserException e) {\r
1606                         e.printStackTrace();\r
1607                         syncSignal.authRefreshComplete.emit(false);\r
1608                         failedRefreshes++;\r
1609                         return false;\r
1610                 } catch (EDAMSystemException e) {\r
1611                         e.printStackTrace();\r
1612                         syncSignal.authRefreshComplete.emit(false);\r
1613                         failedRefreshes++;\r
1614                         return false;           \r
1615                 } catch (TException e) { \r
1616                         e.printStackTrace();\r
1617                         syncSignal.authRefreshComplete.emit(false);\r
1618                         failedRefreshes++;\r
1619                         return false;\r
1620                 }\r
1621                 \r
1622                 // If we didn't get a good auth, then we've failed\r
1623                 if (newAuth == null) {\r
1624                         failedRefreshes++;\r
1625                         status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
1626                         logger.log(logger.EXTREME, "Authentication failure #" +failedRefreshes);\r
1627                         status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
1628                         syncSignal.authRefreshComplete.emit(false);\r
1629                         return false;\r
1630                 }\r
1631                 \r
1632                 // We got a good token.  Now we need to setup the time to renew it.\r
1633                 logger.log(logger.EXTREME, "Saving authentication tokens");\r
1634                 authResult = newAuth;\r
1635                 authToken = new String(newAuth.getAuthenticationToken());\r
1636 //              authTimeRemaining = authResult.getExpiration() - authResult.getCurrentTime();\r
1637 //              authRefreshTime = cal.getTimeInMillis() + (authTimeRemaining/4);        \r
1638                 failedRefreshes=0;\r
1639                 syncSignal.authRefreshComplete.emit(true);\r
1640                 authRefreshNeeded = false;\r
1641                         \r
1642                 // This should never happen, but if it does we consider this a faild attempt.\r
1643 //              if (authTimeRemaining <= 0) {\r
1644 //                      failedRefreshes++;\r
1645 //                      syncSignal.authRefreshComplete.emit(false);\r
1646 //              }\r
1647                 \r
1648                 return true;\r
1649     }\r
1650     \r
1651         public synchronized boolean addWork(String request) {\r
1652                 if (workQueue.offer(request))\r
1653                         return true;\r
1654                 return false;\r
1655         }\r
1656     \r
1657     private Note getNoteContent(Note n) {\r
1658         n.setContent(conn.getNoteTable().getNoteContentBinary(n.getGuid()));\r
1659         return n;\r
1660     }\r
1661 \r
1662 \r
1663 \r
1664     //*********************************************************\r
1665     //* Special download instructions.  Used for DB upgrades\r
1666     //*********************************************************\r
1667     private void downloadAllSharedNotebooks(Client noteStore) {\r
1668         try {\r
1669                         List<SharedNotebook> books = noteStore.listSharedNotebooks(authToken);\r
1670                         logger.log(logger.LOW, "Shared notebooks found = " +books.size());\r
1671                         for (int i=0; i<books.size(); i++) {\r
1672                                 conn.getSharedNotebookTable().updateNotebook(books.get(i), false);\r
1673                         }\r
1674                         conn.getSyncTable().deleteRecord("FullSharedNotebookSync");\r
1675                 } catch (EDAMUserException e1) {\r
1676                         e1.printStackTrace();\r
1677                         status.message.emit(tr("User exception Listing shared notebooks."));\r
1678                         logger.log(logger.LOW, e1.getMessage());\r
1679                         return;\r
1680                 } catch (EDAMSystemException e1) {\r
1681                         e1.printStackTrace();\r
1682                         status.message.emit(tr("System exception Listing shared notebooks."));\r
1683                         logger.log(logger.LOW, e1.getMessage());\r
1684                         return;\r
1685                 } catch (TException e1) {\r
1686                         e1.printStackTrace();\r
1687                         status.message.emit(tr("Transaction exception Listing shared notebooks."));\r
1688                         logger.log(logger.LOW, e1.getMessage());\r
1689                         return;\r
1690                 } catch (EDAMNotFoundException e1) {\r
1691                         e1.printStackTrace();\r
1692                         status.message.emit(tr("EDAM Not Found exception Listing shared notebooks."));\r
1693                         logger.log(logger.LOW, e1.getMessage());\r
1694                 }\r
1695     }\r
1696     private void downloadAllNotebooks(Client noteStore) {\r
1697         try {\r
1698                         List<Notebook> books = noteStore.listNotebooks(authToken);\r
1699                         logger.log(logger.LOW, "Shared notebooks found = " +books.size());\r
1700                         for (int i=0; i<books.size(); i++) {\r
1701                                 conn.getNotebookTable().updateNotebook(books.get(i), false);\r
1702                         }\r
1703                         conn.getSyncTable().deleteRecord("FullNotebookSync");\r
1704                 } catch (EDAMUserException e1) {\r
1705                         e1.printStackTrace();\r
1706                         status.message.emit(tr("User exception Listing notebooks."));\r
1707                         logger.log(logger.LOW, e1.getMessage());\r
1708                         return;\r
1709                 } catch (EDAMSystemException e1) {\r
1710                         e1.printStackTrace();\r
1711                         status.message.emit(tr("System exception Listing notebooks."));\r
1712                         logger.log(logger.LOW, e1.getMessage());\r
1713                         return;\r
1714                 } catch (TException e1) {\r
1715                         e1.printStackTrace();\r
1716                         status.message.emit(tr("Transaction exception Listing notebooks."));\r
1717                         logger.log(logger.LOW, e1.getMessage());\r
1718                         return;\r
1719                 }\r
1720     }\r
1721     private void downloadAllLinkedNotebooks(Client noteStore) {\r
1722         try {\r
1723                         List<LinkedNotebook> books = noteStore.listLinkedNotebooks(authToken);\r
1724                         logger.log(logger.LOW, "Linked notebooks found = " +books.size());\r
1725                         for (int i=0; i<books.size(); i++) {\r
1726                                 conn.getLinkedNotebookTable().updateNotebook(books.get(i), false);\r
1727                         }\r
1728                         conn.getSyncTable().deleteRecord("FullLinkedNotebookSync");\r
1729                 } catch (EDAMUserException e1) {\r
1730                         e1.printStackTrace();\r
1731                         status.message.emit(tr("User exception Listing linked notebooks."));\r
1732                         logger.log(logger.LOW, e1.getMessage());\r
1733                         return;\r
1734                 } catch (EDAMSystemException e1) {\r
1735                         e1.printStackTrace();\r
1736                         status.message.emit(tr("System exception Listing linked notebooks."));\r
1737                         logger.log(logger.LOW, e1.getMessage());\r
1738                         return;\r
1739                 } catch (TException e1) {\r
1740                         e1.printStackTrace();\r
1741                         status.message.emit(tr("Transaction exception Listing lineked notebooks."));\r
1742                         logger.log(logger.LOW, e1.getMessage());\r
1743                         return;\r
1744                 } catch (EDAMNotFoundException e1) {\r
1745                         e1.printStackTrace();\r
1746                         status.message.emit(tr("EDAM Not Found exception Listing linked notebooks."));\r
1747                         logger.log(logger.LOW, e1.getMessage());\r
1748                 }\r
1749     }\r
1750 \r
1751     \r
1752     private void downloadInkNoteImage(String guid, String authToken) {\r
1753                 String urlBase = noteStoreUrl.replace("/edam/note/", "/shard/") + "/res/"+guid+".ink?slice=";\r
1754 //              urlBase = "https://www.evernote.com/shard/s1/res/52b567a9-54ae-4a08-afc5-d5bae275b2a8.ink?slice=";\r
1755                 Integer slice = 1;\r
1756                 Resource r = conn.getNoteTable().noteResourceTable.getNoteResource(guid, false);\r
1757                 conn.getInkImagesTable().expungeImage(r.getGuid());\r
1758                 int sliceCount = 1+((r.getHeight()-1)/480);\r
1759                 HttpClient http = new DefaultHttpClient();\r
1760         for (int i=0; i<sliceCount; i++) {\r
1761                 String url = urlBase + slice.toString();\r
1762                 HttpPost post = new HttpPost(url);\r
1763                 post.getParams().setParameter("auth", authToken);\r
1764                 List <NameValuePair> nvps = new ArrayList <NameValuePair>();\r
1765             nvps.add(new BasicNameValuePair("auth", authToken));\r
1766 \r
1767             try {\r
1768                                 post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));\r
1769                         } catch (UnsupportedEncodingException e1) {\r
1770                                 e1.printStackTrace();\r
1771                         }\r
1772                 try {\r
1773                         HttpResponse response = http.execute(post);\r
1774                         HttpEntity resEntity = response.getEntity();\r
1775                         InputStream is = resEntity.getContent();\r
1776                         QByteArray data = writeToFile(is);\r
1777                         conn.getInkImagesTable().saveImage(guid, slice, data);\r
1778                         } catch (ClientProtocolException e) {\r
1779                                 e.printStackTrace();\r
1780                         } catch (IOException e) {\r
1781                                 e.printStackTrace();\r
1782                         }\r
1783 \r
1784                         slice++;\r
1785         }\r
1786         http.getConnectionManager().shutdown(); \r
1787                 noteSignal.noteChanged.emit(r.getNoteGuid(), null);   // Signal to ivalidate note cache\r
1788     }\r
1789     \r
1790     \r
1791     public QByteArray writeToFile(InputStream iStream) throws IOException {\r
1792 \r
1793             File temp = File.createTempFile("nn-inknote-temp", ".png");\r
1794 \r
1795             // Save InputStream to the file.\r
1796             BufferedOutputStream fOut = null;\r
1797             try {\r
1798               fOut = new BufferedOutputStream(new FileOutputStream(temp));\r
1799               byte[] buffer = new byte[32 * 1024];\r
1800               int bytesRead = 0;\r
1801               while ((bytesRead = iStream.read(buffer)) != -1) {\r
1802                 fOut.write(buffer, 0, bytesRead);\r
1803               }\r
1804             }\r
1805             finally {\r
1806                 iStream.close();\r
1807                 fOut.close();\r
1808             }\r
1809             QFile tempFile = new QFile(temp.getAbsoluteFile().toString());\r
1810             tempFile.open(OpenModeFlag.ReadOnly);\r
1811             QByteArray data = tempFile.readAll();\r
1812             tempFile.close();\r
1813             tempFile.remove();\r
1814             return data;\r
1815     }\r
1816     \r
1817     \r
1818         //******************************************\r
1819         //* Begin syncing shared notebooks \r
1820         //******************************************\r
1821     private void syncLinkedNotebooks() {\r
1822         logger.log(logger.MEDIUM, "Authenticating Shared Notebooks");\r
1823         status.message.emit(tr("Synchronizing shared notebooks."));\r
1824         List<LinkedNotebook> books = conn.getLinkedNotebookTable().getAll();\r
1825 \r
1826         for (int i=0; i<books.size(); i++) {\r
1827                 try {\r
1828                                 long lastSyncDate = conn.getLinkedNotebookTable().getLastSequenceDate(books.get(i).getGuid());\r
1829                                 int lastSequenceNumber = conn.getLinkedNotebookTable().getLastSequenceNumber(books.get(i).getGuid());\r
1830 \r
1831                                 // Authenticate to the owner's shard\r
1832                                 String linkedNoteStoreUrl       = noteStoreUrlBase + books.get(i).getShardId();\r
1833                                 THttpClient linkedNoteStoreTrans        = new THttpClient(linkedNoteStoreUrl);\r
1834                                 TBinaryProtocol linkedNoteStoreProt     = new TBinaryProtocol(linkedNoteStoreTrans);\r
1835                                 Client linkedNoteStore = new NoteStore.Client(linkedNoteStoreProt, linkedNoteStoreProt);                                \r
1836 \r
1837                                 linkedAuthResult = null;\r
1838                                 if (books.get(i).getShareKey() != null)\r
1839                                         linkedAuthResult = linkedNoteStore.authenticateToSharedNotebook(books.get(i).getShareKey(), authToken);\r
1840                                 else {\r
1841                                         linkedAuthResult = new AuthenticationResult();\r
1842                                         linkedAuthResult.setAuthenticationToken("");\r
1843                                 }\r
1844                                 SyncState linkedSyncState = \r
1845                                         linkedNoteStore.getLinkedNotebookSyncState(linkedAuthResult.getAuthenticationToken(), books.get(i));\r
1846                                 if (linkedSyncState.getUpdateCount() > lastSequenceNumber) {\r
1847                                         if (lastSyncDate < linkedSyncState.getFullSyncBefore()) {\r
1848                                                 lastSequenceNumber = 0;\r
1849                                         } \r
1850                                         syncLinkedNotebook(linkedNoteStore, books.get(i), \r
1851                                                         lastSequenceNumber, linkedSyncState.getUpdateCount(), linkedAuthResult.getAuthenticationToken());\r
1852                                 }\r
1853                         \r
1854                         // Synchronize local changes\r
1855                         syncLocalLinkedNoteChanges(linkedNoteStore, books.get(i));\r
1856                                 \r
1857                 } catch (EDAMUserException e) {\r
1858                         e.printStackTrace();\r
1859                 } catch (EDAMNotFoundException e) {\r
1860                         status.message.emit(tr("Error synchronizing \" " +\r
1861                                         books.get(i).getShareName()+"\". Please verify you still have access to that shared notebook."));\r
1862                         error = true;\r
1863                         e.printStackTrace();\r
1864                 } catch (EDAMSystemException e) {\r
1865                         logger.log(logger.LOW, "System error authenticating against shared notebook. "+\r
1866                                         "Key: "+books.get(i).getShareKey() +" Error:" +e.getMessage());\r
1867                         e.printStackTrace();\r
1868                 } catch (TException e) {\r
1869                         e.printStackTrace();\r
1870                 }\r
1871         }\r
1872         \r
1873         // Cleanup tags\r
1874         conn.getTagTable().removeUnusedLinkedTags();\r
1875         conn.getTagTable().cleanupTags();\r
1876         tagSignal.listChanged.emit();\r
1877         }\r
1878 \r
1879     \r
1880     //**************************************************************\r
1881     //* Linked notebook contents (from someone else's account)\r
1882     //*************************************************************\r
1883         private void syncLinkedNotebook(Client linkedNoteStore, LinkedNotebook book, int usn, int highSequence, String token) {\r
1884                 if (ignoreLinkedNotebooks.contains(book.getGuid()))\r
1885                         return;\r
1886                 List<Note> dirtyNotes = conn.getNoteTable().getDirtyLinkedNotes();\r
1887                 if (dirtyNoteGuids == null) \r
1888                         dirtyNoteGuids = new Vector<String>();\r
1889 \r
1890                 for (int i=0; i<dirtyNotes.size() && keepRunning; i++) {\r
1891                         dirtyNoteGuids.add(dirtyNotes.get(i).getGuid());\r
1892                 }\r
1893                 boolean fullSync = false;\r
1894                 if (usn == 0)\r
1895                         fullSync = true;\r
1896                 boolean syncError = false;\r
1897                 while (usn < highSequence && !syncError) {\r
1898                         refreshNeeded = true;\r
1899                         try {\r
1900                                 SyncChunk chunk = \r
1901                                         linkedNoteStore.getLinkedNotebookSyncChunk(token, book, usn, 10, fullSync);\r
1902                                 \r
1903                                 // Expunge notes\r
1904                                 syncExpungedNotes(chunk);\r
1905 \r
1906                                 syncRemoteNotes(linkedNoteStore, chunk.getNotes(), fullSync, linkedAuthResult.getAuthenticationToken());\r
1907                                 findNewLinkedTags(linkedNoteStore, chunk.getNotes(), linkedAuthResult.getAuthenticationToken());\r
1908                                 // Sync resources\r
1909                                 for (int i=0; i<chunk.getResourcesSize(); i++) {\r
1910                                         syncRemoteResource(linkedNoteStore, chunk.getResources().get(i), linkedAuthResult.getAuthenticationToken());\r
1911                                 }\r
1912                                 syncRemoteLinkedNotebooks(linkedNoteStore, chunk.getNotebooks(), false, book);\r
1913 //                              SharedNotebook s = linkedNoteStore.getSharedNotebookByAuth(linkedAuthResult.getAuthenticationToken());\r
1914 //                              syncLinkedTags(chunk.getTags(), s.getNotebookGuid());\r
1915                                 syncLinkedTags(chunk.getTags(), book.getGuid());\r
1916                                 \r
1917                                 // Go through & signal any notes that have changed so we can refresh the user's view\r
1918                                 for (int i=0; i<chunk.getNotesSize(); i++) \r
1919                                         noteSignal.noteChanged.emit(chunk.getNotes().get(i).getGuid(), null);\r
1920 \r
1921                                 // Expunge Notebook records\r
1922                                 for (int i=0; i<chunk.getExpungedLinkedNotebooksSize(); i++) {\r
1923                                         conn.getLinkedNotebookTable().expungeNotebook(chunk.getExpungedLinkedNotebooks().get(i), false);\r
1924                                 }\r
1925                                 \r
1926                                 usn = chunk.getChunkHighUSN();\r
1927                                 conn.getLinkedNotebookTable().setLastSequenceDate(book.getGuid(),chunk.getCurrentTime());\r
1928                                 conn.getLinkedNotebookTable().setLastSequenceNumber(book.getGuid(),chunk.getChunkHighUSN());\r
1929                         } catch (EDAMUserException e) {\r
1930                                 syncError = true;\r
1931                                 status.message.emit(tr("EDAM UserException synchronizing linked notbook.  See the log for datails."));\r
1932                                 e.printStackTrace();\r
1933                                 logger.log(logger.LOW, e.getMessage());\r
1934                         } catch (EDAMSystemException e) {\r
1935                                 syncError = true;\r
1936                                 status.message.emit(tr("EDAM SystemException synchronizing linked notbook.  See the log for datails."));\r
1937                                 e.printStackTrace();\r
1938                                 logger.log(logger.LOW, e.getMessage());\r
1939                         } catch (EDAMNotFoundException e) {\r
1940                                 syncError = true;\r
1941                                 status.message.emit(tr("EDAM NotFoundException synchronizing linked notbook.  See the log for datails."));\r
1942                                 e.printStackTrace();   /// DELETE OLD NOTEBOOKS HERE\r
1943                                 logger.log(logger.LOW, e.getMessage());\r
1944                         } catch (TException e) {\r
1945                                 syncError = true;\r
1946                                 status.message.emit(tr("EDAM TException synchronizing linked notbook.  See the log for datails."));\r
1947                                 e.printStackTrace();\r
1948                                 logger.log(logger.LOW, e.getMessage());\r
1949                         }\r
1950                 }\r
1951         }\r
1952         // Sync remote tags\r
1953         private void syncLinkedTags(List<Tag> tags, String notebookGuid) {\r
1954                 logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteTags");\r
1955                 if (tags != null) {\r
1956                         for (int i=0; i<tags.size() && keepRunning; i++) {\r
1957                                 conn.getTagTable().syncLinkedTag(tags.get(i), notebookGuid, false);\r
1958                         }\r
1959                 }\r
1960                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteTags");\r
1961         }\r
1962         \r
1963         // Sync notebooks from a linked notebook\r
1964         private void syncRemoteLinkedNotebooks(Client noteStore, List<Notebook> notebooks, boolean readOnly, LinkedNotebook linked) {\r
1965                 logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotebooks");\r
1966                 if (notebooks != null) {\r
1967                         for (int i=0; i<notebooks.size() && keepRunning; i++) {\r
1968                                 try {\r
1969                                         if (!linkedAuthResult.getAuthenticationToken().equals("")) {\r
1970                                                 SharedNotebook s = noteStore.getSharedNotebookByAuth(linkedAuthResult.getAuthenticationToken());\r
1971                                                 conn.getLinkedNotebookTable().setNotebookGuid(s.getShareKey(), s.getNotebookGuid());\r
1972                                                 readOnly = !s.isNotebookModifiable();\r
1973                                         } else {\r
1974                                                 readOnly = true;\r
1975                                         }\r
1976                                         notebooks.get(i).setName(linked.getShareName());\r
1977                                         notebooks.get(i).setDefaultNotebook(false);\r
1978                                         conn.getNotebookTable().syncLinkedNotebook(notebooks.get(i), false, readOnly); \r
1979                                 } catch (EDAMUserException e) {\r
1980                                         readOnly = true;\r
1981                                         e.printStackTrace();\r
1982                                 } catch (EDAMNotFoundException e) {\r
1983                                         readOnly = true;\r
1984                                         e.printStackTrace();\r
1985                                 } catch (EDAMSystemException e) {\r
1986                                         readOnly = true;\r
1987                                         e.printStackTrace();\r
1988                                 } catch (TException e) {\r
1989                                         readOnly = true;\r
1990                                         e.printStackTrace();\r
1991                                 }\r
1992 \r
1993                         }\r
1994                 }                       \r
1995                 logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteNotebooks");\r
1996         }\r
1997 \r
1998         private void findNewLinkedTags(Client noteStore, List<Note> newNotes, String token) {\r
1999                 if (newNotes == null)\r
2000                         return;\r
2001                 for (int i=0; i<newNotes.size(); i++) {\r
2002                         Note n = newNotes.get(i);\r
2003                         for (int j=0; j<n.getTagGuidsSize(); j++) {\r
2004                                 String tag = n.getTagGuids().get(j);\r
2005                                 if (!conn.getTagTable().exists(tag)) {\r
2006                                         Tag newTag;\r
2007                                         try {\r
2008                                                 newTag = noteStore.getTag(token, tag);\r
2009                                                 conn.getTagTable().addTag(newTag, false);\r
2010                                         } catch (EDAMUserException e) {\r
2011                                                 e.printStackTrace();\r
2012                                         } catch (EDAMSystemException e) {\r
2013                                                 e.printStackTrace();\r
2014                                         } catch (EDAMNotFoundException e) {\r
2015                                                 e.printStackTrace();\r
2016                                         } catch (TException e) {\r
2017                                                 e.printStackTrace();\r
2018                                         }\r
2019                                         \r
2020                                 }\r
2021                         }\r
2022                 }\r
2023         }\r
2024 \r
2025         // Synchronize changes locally done to linked notes\r
2026         private void syncLocalLinkedNoteChanges(Client noteStore, LinkedNotebook book) {\r
2027                 String notebookGuid = conn.getLinkedNotebookTable().getNotebookGuid(book.getGuid());\r
2028                 List<Note> notes = conn.getNoteTable().getDirtyLinked(notebookGuid);\r
2029                 for (int i=0; i<notes.size(); i++) {\r
2030                         syncLocalNote(noteStore, notes.get(i), linkedAuthResult.getAuthenticationToken());\r
2031                 }\r
2032         }\r
2033 \r
2034 }\r