OSDN Git Service

Add selective sync, cleanup debug messages, & correct network sync not disconnecting...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / SyncRunner.java
index bce4fa8..57fb0fb 100644 (file)
@@ -29,6 +29,7 @@ import java.util.ArrayList;
 import java.util.Calendar;\r
 import java.util.Date;\r
 import java.util.GregorianCalendar;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Vector;\r
 import java.util.concurrent.LinkedBlockingQueue;\r
@@ -132,12 +133,13 @@ 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 HashMap<String,String> ignoreTags;\r
+               private final HashMap<String,String> ignoreNotebooks;\r
        \r
                \r
                \r
@@ -168,6 +170,9 @@ public class SyncRunner extends QObject implements Runnable {
                userStore = null;\r
                authToken = null;\r
                disableUploads = false;\r
+               ignoreTags = new HashMap<String,String>();\r
+               ignoreNotebooks = new HashMap<String,String>();\r
+               \r
 //             setAutoDelete(false);\r
                workQueue=new LinkedBlockingQueue<String>(MAX_QUEUED_WAITING);\r
        }\r
@@ -183,7 +188,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 +209,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 +261,19 @@ 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.put(ignore.get(i),"");\r
+               \r
+               ignoreTags.clear();\r
+               ignore = conn.getSyncTable().getIgnoreRecords("TAG");\r
+               for (int i=0; i<ignore.size(); i++) \r
+                       ignoreTags.put(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 +289,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 +411,11 @@ 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"), conn.getSyncTable().getIgnoreRecords("TAG"));\r
+                       if (notes.size() > 0)\r
+                               syncSignal.refreshLists.emit();\r
+                       \r
                        //*****************************************\r
                        //* End of synchronization\r
                        //*****************************************\r
@@ -421,7 +450,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 +510,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 +577,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 +677,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 +766,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 +898,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 +978,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 +1227,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.containsKey(n.getNotebookGuid()))\r
+                               ignoreNote = true;\r
+                       for (int i=0; i<n.getTagGuidsSize(); i++) {\r
+                               if (ignoreTags.containsKey(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 +1529,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 +1559,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 +1603,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