OSDN Git Service

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