OSDN Git Service

Add Linked notebooks to the ignore sync process.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / SyncRunner.java
index bce4fa8..1fcc42a 100644 (file)
@@ -30,6 +30,7 @@ import java.util.Calendar;
 import java.util.Date;\r
 import java.util.GregorianCalendar;\r
 import java.util.List;\r
+import java.util.TreeSet;\r
 import java.util.Vector;\r
 import java.util.concurrent.LinkedBlockingQueue;\r
 \r
@@ -132,12 +133,14 @@ public class SyncRunner extends QObject implements Runnable {
            public int updateSequenceNumber;\r
            private boolean refreshNeeded;\r
            private volatile LinkedBlockingQueue<String> workQueue;\r
-//             private static int MAX_EMPTY_QUEUE_COUNT = 1;\r
                private static int MAX_QUEUED_WAITING = 1000;\r
                String dbuid;\r
                String dburl;\r
                String dbpswd;\r
                String dbcpswd;\r
+               private final TreeSet<String> ignoreTags;\r
+               private final TreeSet<String> ignoreNotebooks;\r
+               private final TreeSet<String> ignoreLinkedNotebooks;\r
        \r
                \r
                \r
@@ -168,6 +171,10 @@ public class SyncRunner extends QObject implements Runnable {
                userStore = null;\r
                authToken = null;\r
                disableUploads = false;\r
+               ignoreTags = new TreeSet<String>();\r
+               ignoreNotebooks = new TreeSet<String>();\r
+               ignoreLinkedNotebooks = new TreeSet<String>();\r
+               \r
 //             setAutoDelete(false);\r
                workQueue=new LinkedBlockingQueue<String>(MAX_QUEUED_WAITING);\r
        }\r
@@ -183,7 +190,7 @@ public class SyncRunner extends QObject implements Runnable {
                                        return;\r
                                idle=false;\r
                                error=false;\r
-                               if (authRefreshNeeded == true) {\r
+                               if (authRefreshNeeded == true || !isConnected) {\r
                                        logger.log(logger.EXTREME, "Refreshing connection");\r
                                        refreshConnection();\r
                                }\r
@@ -204,6 +211,10 @@ public class SyncRunner extends QObject implements Runnable {
                                idle=true;\r
                                logger.log(logger.EXTREME, "Signaling refresh finished.  refreshNeeded=" +refreshNeeded);\r
                                syncSignal.finished.emit(refreshNeeded);\r
+                               if (error) {\r
+                                       syncSignal.errorDisconnect.emit();\r
+                                       status.message.emit(tr("Error synchronizing - see log for details."));\r
+                               }\r
                        }\r
                }       \r
                catch (InterruptedException e1) {\r
@@ -252,7 +263,24 @@ public class SyncRunner extends QObject implements Runnable {
        @SuppressWarnings("unused")\r
        private void evernoteSync() throws java.net.UnknownHostException {\r
                logger.log(logger.HIGH, "Entering SyncRunner.evernoteSync");\r
+               \r
+               // Rebuild list of tags & notebooks to ignore\r
+               ignoreNotebooks.clear();\r
+               List<String> ignore = conn.getSyncTable().getIgnoreRecords("NOTEBOOK");\r
+               for (int i=0; i<ignore.size(); i++) \r
+                       ignoreNotebooks.add(ignore.get(i));\r
+               \r
+               ignore.clear();\r
+               ignore = conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK");\r
+               for (int i=0; i<ignore.size(); i++) \r
+                       ignoreLinkedNotebooks.add(ignore.get(i));\r
+               \r
+               ignoreTags.clear();\r
+               ignore = conn.getSyncTable().getIgnoreRecords("TAG");\r
+               for (int i=0; i<ignore.size(); i++) \r
+                       ignoreTags.add(ignore.get(i));\r
 \r
+               // Make sure we are connected & should keep running\r
                if (isConnected && keepRunning) {\r
                        error = false;\r
                        logger.log(logger.EXTREME, "Synchronizing with Evernote");\r
@@ -268,17 +296,20 @@ public class SyncRunner extends QObject implements Runnable {
                                e1.printStackTrace();\r
                                status.message.emit(tr("User exception getting user account information.  Aborting sync and disconnecting"));\r
                                syncSignal.errorDisconnect.emit();\r
+                               error = true;\r
                                enDisconnect();\r
                                return;\r
                        } catch (EDAMSystemException e1) {\r
                                e1.printStackTrace();\r
                                status.message.emit(tr("System error user account information.  Aborting sync and disconnecting!"));\r
                                syncSignal.errorDisconnect.emit();\r
+                               error = true;\r
                                enDisconnect();\r
                                return;\r
                        } catch (TException e1) {\r
                                e1.printStackTrace();\r
                                syncSignal.errorDisconnect.emit();\r
+                               error = true;\r
                                status.message.emit(tr("Transaction error getting user account information.  Aborting sync and disconnecting!"));\r
                                enDisconnect();\r
                                return;\r
@@ -387,6 +418,12 @@ public class SyncRunner extends QObject implements Runnable {
                                        syncLocalSavedSearches();\r
                        }\r
                        \r
+                       status.message.emit(tr("Cleaning up"));\r
+                       List<String> notes = conn.getNoteTable().expungeIgnoreSynchronizedNotes(conn.getSyncTable().getIgnoreRecords("NOTEBOOK"), \r
+                                       conn.getSyncTable().getIgnoreRecords("TAG"), conn.getSyncTable().getIgnoreRecords("LINKEDNOTEBOOK"));\r
+                       if (notes.size() > 0)\r
+                               syncSignal.refreshLists.emit();\r
+                       \r
                        //*****************************************\r
                        //* End of synchronization\r
                        //*****************************************\r
@@ -421,7 +458,8 @@ public class SyncRunner extends QObject implements Runnable {
                for (int i=0; i<expunged.size() && keepRunning; i++) {\r
                        \r
                        if (authRefreshNeeded)\r
-                               refreshConnection();\r
+                               if (!refreshConnection())\r
+                                       return;\r
 \r
                        try {\r
                                if (expunged.get(i).type.equalsIgnoreCase("TAG")) {\r
@@ -480,7 +518,8 @@ public class SyncRunner extends QObject implements Runnable {
                for (int i=0; i<notes.size() && keepRunning; i++) {\r
                        \r
                        if (authRefreshNeeded)\r
-                               refreshConnection();\r
+                               if (!refreshConnection())\r
+                                       return;\r
                        \r
                        Note enNote = notes.get(i);\r
                        try {\r
@@ -546,7 +585,8 @@ public class SyncRunner extends QObject implements Runnable {
                status.message.emit(tr("Sending local notes."));\r
 \r
                if (authRefreshNeeded)\r
-                       refreshConnection();\r
+                       if (!refreshConnection())\r
+                               return;\r
                        \r
                if (enNote.isActive()) {\r
                        try {\r
@@ -645,7 +685,8 @@ public class SyncRunner extends QObject implements Runnable {
                for (int i=0; i<notebooks.size() && keepRunning; i++) {\r
                        \r
                        if (authRefreshNeeded)\r
-                               refreshConnection();\r
+                               if (!refreshConnection())\r
+                                       return;\r
                        \r
                        Notebook enNotebook = notebooks.get(i);\r
                        try {\r
@@ -733,7 +774,8 @@ public class SyncRunner extends QObject implements Runnable {
                Tag enTag = findNextTag();\r
                while(enTag!=null) {\r
                        if (authRefreshNeeded)\r
-                               refreshConnection();\r
+                               if (!refreshConnection())\r
+                                       return;\r
 \r
                        try {\r
                                if (enTag.getUpdateSequenceNum() > 0) {\r
@@ -864,7 +906,8 @@ public class SyncRunner extends QObject implements Runnable {
                for (int i=0; i<searches.size() &&  keepRunning; i++) {\r
                        \r
                        if (authRefreshNeeded)\r
-                               refreshConnection();\r
+                               if (!refreshConnection())\r
+                                       return;\r
                        \r
                        SavedSearch enSearch = searches.get(i);\r
                        try {\r
@@ -943,7 +986,8 @@ public class SyncRunner extends QObject implements Runnable {
                while(more &&  keepRunning) {\r
                        \r
                        if (authRefreshNeeded)\r
-                               refreshConnection();\r
+                               if (!refreshConnection())\r
+                                       return;\r
                        \r
                        chunk = null;\r
                        int sequence = updateSequenceNumber;\r
@@ -1191,7 +1235,17 @@ public class SyncRunner extends QObject implements Runnable {
                                if (conflictingNote)\r
                                        moveConflictingNote(n.getGuid());\r
                        }\r
-                       if (conflictingNote || fullSync) {\r
+                       boolean ignoreNote = false;\r
+                       if (ignoreNotebooks.contains(n.getNotebookGuid()))\r
+                               ignoreNote = true;\r
+                       for (int i=0; i<n.getTagGuidsSize(); i++) {\r
+                               if (ignoreTags.contains(n.getTagGuids().get(i))) {\r
+                                       ignoreNote = true;\r
+                                       i=n.getTagGuidsSize();\r
+                               }\r
+                       }\r
+                               \r
+                       if ((conflictingNote || fullSync) && !ignoreNote) {\r
                                logger.log(logger.EXTREME, "Saving Note");\r
                                conn.getNoteTable().syncNote(n, false);\r
                                noteSignal.noteChanged.emit(n.getGuid(), null);   // Signal to ivalidate note cache\r
@@ -1483,19 +1537,20 @@ public class SyncRunner extends QObject implements Runnable {
        isConnected = false;\r
     }\r
     // Refresh the connection\r
-    private synchronized void refreshConnection() {\r
+    private synchronized boolean refreshConnection() {\r
                logger.log(logger.EXTREME, "Entering SyncRunner.refreshConnection()");\r
 //        Calendar cal = Calendar.getInstance();\r
                \r
         // If we are not connected let's get out of here\r
         if (!isConnected)\r
-               return;\r
+               return false;\r
         \r
                // If we fail too many times, then let's give up.\r
                if (failedRefreshes >=5) {\r
                        logger.log(logger.EXTREME, "Refresh attempts have failed.  Disconnecting.");\r
                        isConnected = false;\r
-                       return;\r
+                       status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
+                       return false;\r
                }\r
         \r
                // If this is the first time through, then we need to set this\r
@@ -1512,31 +1567,33 @@ public class SyncRunner extends QObject implements Runnable {
                if (userStore != null && authToken != null) \r
                        newAuth = userStore.refreshAuthentication(authToken); \r
                else\r
-                       return;\r
+                       return false;\r
                logger.log(logger.EXTREME, "UserStore.refreshAuthentication has succeeded.");\r
                } catch (EDAMUserException e) {\r
                        e.printStackTrace();\r
                        syncSignal.authRefreshComplete.emit(false);\r
                        failedRefreshes++;\r
-                       return;\r
+                       return false;\r
                } catch (EDAMSystemException e) {\r
                        e.printStackTrace();\r
                        syncSignal.authRefreshComplete.emit(false);\r
                        failedRefreshes++;\r
-                       return;         \r
+                       return false;           \r
                } catch (TException e) { \r
                        e.printStackTrace();\r
                        syncSignal.authRefreshComplete.emit(false);\r
                        failedRefreshes++;\r
-                       return;\r
+                       return false;\r
                }\r
                \r
                // If we didn't get a good auth, then we've failed\r
                if (newAuth == null) {\r
                        failedRefreshes++;\r
+                       status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
                        logger.log(logger.EXTREME, "Authentication failure #" +failedRefreshes);\r
+                       status.message.emit(tr("Unable to synchronize - Authentication failed"));\r
                        syncSignal.authRefreshComplete.emit(false);\r
-                       return;\r
+                       return false;\r
                }\r
                \r
                // We got a good token.  Now we need to setup the time to renew it.\r
@@ -1554,6 +1611,8 @@ public class SyncRunner extends QObject implements Runnable {
 //                     failedRefreshes++;\r
 //                     syncSignal.authRefreshComplete.emit(false);\r
 //             }\r
+               \r
+               return true;\r
     }\r
     \r
        public synchronized boolean addWork(String request) {\r
@@ -1733,17 +1792,17 @@ public class SyncRunner extends QObject implements Runnable {
        List<LinkedNotebook> books = conn.getLinkedNotebookTable().getAll();\r
        for (int i=0; i<books.size(); i++) {\r
                try {\r
-                       long lastSyncDate = conn.getLinkedNotebookTable().getLastSequenceDate(books.get(i).getGuid());\r
-                       int lastSequenceNumber = conn.getLinkedNotebookTable().getLastSequenceNumber(books.get(i).getGuid());\r
-                       linkedAuthResult = noteStore.authenticateToSharedNotebook(books.get(i).getShareKey(), authToken);\r
-                       SyncState linkedSyncState = \r
-                               noteStore.getLinkedNotebookSyncState(linkedAuthResult.getAuthenticationToken(), books.get(i));\r
-                       if (linkedSyncState.getUpdateCount() > lastSequenceNumber) {\r
-                               if (lastSyncDate < linkedSyncState.getFullSyncBefore()) {\r
-                                       lastSequenceNumber = 0;\r
-                               }\r
+                               long lastSyncDate = conn.getLinkedNotebookTable().getLastSequenceDate(books.get(i).getGuid());\r
+                               int lastSequenceNumber = conn.getLinkedNotebookTable().getLastSequenceNumber(books.get(i).getGuid());\r
+                               linkedAuthResult = noteStore.authenticateToSharedNotebook(books.get(i).getShareKey(), authToken);\r
+                               SyncState linkedSyncState = \r
+                                       noteStore.getLinkedNotebookSyncState(linkedAuthResult.getAuthenticationToken(), books.get(i));\r
+                               if (linkedSyncState.getUpdateCount() > lastSequenceNumber) {\r
+                                       if (lastSyncDate < linkedSyncState.getFullSyncBefore()) {\r
+                                               lastSequenceNumber = 0;\r
+                                       } \r
                                        syncLinkedNotebook(books.get(i), lastSequenceNumber, linkedSyncState.getUpdateCount());\r
-                       }\r
+                               }\r
                        \r
                        // Synchronize local changes\r
                        syncLocalLinkedNoteChanges(books.get(i));\r
@@ -1783,15 +1842,14 @@ public class SyncRunner extends QObject implements Runnable {
                        try {\r
                                SyncChunk chunk = \r
                                        noteStore.getLinkedNotebookSyncChunk(authToken, book, usn, 10, fullSync);\r
-//                                     noteStore.getLinkedNotebookSyncChunk(linkedAuthResult.getAuthenticationToken(), book, usn, 10, fullSync);\r
 \r
-                               syncRemoteNotes(chunk.getNotes(), fullSync, linkedAuthResult.getAuthenticationToken());\r
+                               if (!ignoreLinkedNotebooks.contains(book.getGuid()))\r
+                                       syncRemoteNotes(chunk.getNotes(), fullSync, linkedAuthResult.getAuthenticationToken());\r
                                findNewLinkedTags(chunk.getNotes(), linkedAuthResult.getAuthenticationToken());\r
                                for (int i=0; i<chunk.getResourcesSize(); i++) {\r
                                        syncRemoteResource(chunk.getResources().get(i), linkedAuthResult.getAuthenticationToken());\r
                                }\r
                                syncRemoteLinkedNotebooks(chunk.getNotebooks(), false, book);\r
-//                             String notebookGuid = conn.getLinkedNotebookTable().getNotebookGuid(book.getGuid());\r
                                SharedNotebook s = noteStore.getSharedNotebookByAuth(linkedAuthResult.getAuthenticationToken());\r
                                syncLinkedTags(chunk.getTags(), s.getNotebookGuid());\r
                                \r