OSDN Git Service

Add the ability to encrypt & decrypt the database more easily.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / Global.java
index 11f2106..8a082dc 100644 (file)
@@ -21,69 +21,55 @@ package cx.fbn.nevernote;
 \r
 \r
 //import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
+\r
 import java.io.PrintStream;\r
 import java.text.SimpleDateFormat;\r
 import java.util.ArrayList;\r
 import java.util.Calendar;\r
 import java.util.HashMap;\r
 import java.util.List;\r
-import java.util.concurrent.ArrayBlockingQueue;\r
 \r
 import com.evernote.edam.type.Accounting;\r
 import com.evernote.edam.type.PrivilegeLevel;\r
 import com.evernote.edam.type.User;\r
 import com.evernote.edam.type.UserAttributes;\r
 import com.trolltech.qt.core.QByteArray;\r
-import com.trolltech.qt.core.QMutex;\r
 import com.trolltech.qt.core.QSettings;\r
 import com.trolltech.qt.gui.QPalette;\r
 \r
+import cx.fbn.nevernote.config.FileManager;\r
+import cx.fbn.nevernote.config.InitializationException;\r
+import cx.fbn.nevernote.config.StartupConfig;\r
 import cx.fbn.nevernote.gui.ContainsAttributeFilterTable;\r
 import cx.fbn.nevernote.gui.DateAttributeFilterTable;\r
 import cx.fbn.nevernote.gui.ShortcutKeys;\r
-import cx.fbn.nevernote.signals.DBRunnerSignal;\r
-import cx.fbn.nevernote.threads.DBRunner;\r
 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
 \r
 public class Global {\r
-       public static String version = "0.88";\r
+       public static String version = "0.90";\r
     public static String username = ""; \r
-    public static String password = ""; \r
-\r
-    public static DBRunner                             dbRunner;                       // Database thread\r
-    public static DBRunnerSignal               dbRunnerSignal;         // Signals to the database runner\r
-    public static QMutex                               dbrunnerWorkLock;       // mutex lock for work queue\r
+    public static String password = "";     \r
     \r
 \r
-    public static int mainThreadId=0;\r
-    private static ArrayBlockingQueue<Boolean> mainThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int mainThreadId=0;\r
     \r
-    public static int syncThreadId=1;\r
-    private static ArrayBlockingQueue<Boolean> syncThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int syncThreadId=1;\r
     \r
-    public static int tagCounterThreadId=2;\r
-    private static ArrayBlockingQueue<Boolean> tagCounterThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int tagCounterThreadId=2;\r
     \r
-    public static int trashCounterThreadId=3;   // This should always be the highest thread ID\r
-    private static ArrayBlockingQueue<Boolean> trashCounterThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int trashCounterThreadId=3;   // This should always be the highest thread ID\r
 \r
-    public static int indexThreadId=4;         // Thread for indexing words\r
-    private static ArrayBlockingQueue<Boolean> indexThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int indexThreadId=4;           // Thread for indexing words\r
 \r
-    public static int saveThreadId=5;          // Thread used for processing data to saving content\r
-    private static ArrayBlockingQueue<Boolean> saveThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int saveThreadId=5;    // Thread used for processing data to saving content\r
 \r
-    public static int notebookCounterThreadId=6;   // Notebook Thread\r
-    private static ArrayBlockingQueue<Boolean> notebookCounterThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int notebookCounterThreadId=6;   // Notebook Thread\r
 \r
-    public static int indexThread03Id=7;   // unused\r
-    private static ArrayBlockingQueue<Boolean> indexThread03ThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int indexThread03Id=7;   // unused\r
 \r
-    public static int indexThread04Id=8;   // unused\r
-    private static ArrayBlockingQueue<Boolean> indexThread04ThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
+    public static final int indexThread04Id=8;   // unused\r
     \r
-    public static int dbThreadId=9;   // This should always be the highest thread ID\r
+    public static final int dbThreadId=9;   // This should always be the highest thread ID\r
     \r
     \r
     public static HashMap<String,String> passwordSafe = new HashMap<String, String>();\r
@@ -93,7 +79,6 @@ public class Global {
     public static long authTimeRemaining;\r
     public static long authRefreshTime;\r
     public static long failedRefreshes = 0;\r
-    public static String currentDir;\r
     public static boolean keepRunning;\r
     \r
     public static String userStoreUrl;\r
@@ -127,19 +112,16 @@ public class Global {
        public static String attachmentNameDelimeter = "------";\r
        \r
        public static String    databaseName = new String("NeverNote");\r
-       public static String    indexDatabaseName = new String("IndexDatabase.db");\r
        public static DateAttributeFilterTable createdSinceFilter;\r
        public static DateAttributeFilterTable createdBeforeFilter;\r
        public static DateAttributeFilterTable changedSinceFilter;\r
        public static DateAttributeFilterTable changedBeforeFilter;\r
        public static ContainsAttributeFilterTable containsFilter;\r
-//     public static DBLock                            dbLock;\r
-//     public static DBLock                            indexLock;\r
        public static ApplicationLogger    logger;\r
        PrintStream stdoutStream;\r
        public static QPalette                          originalPalette;\r
        public static ShortcutKeys                      shortcutKeys;\r
-       public static boolean                           disableViewing = false;\r
+       public static boolean                           disableViewing;\r
        \r
        public static List<String>                              invalidElements = new ArrayList<String>();\r
        public static HashMap<String, ArrayList<String>>        invalidAttributes = new HashMap<String, ArrayList<String>>();\r
@@ -149,18 +131,22 @@ public class Global {
        \r
        static Calendar startTraceTime;\r
        static Calendar intervalTraceTime;\r
+\r
+       private static FileManager fileManager;\r
        \r
     // Do initial setup \r
-    public static void setup()  {\r
-               if (name == null)\r
-                       name = "NeverNote";\r
-               settings = new QSettings("fbn.cx", name);\r
-                   currentDir = getDirectoryPath();\r
+    public static void setup(StartupConfig startupConfig) throws InitializationException  {\r
+        settings = new QSettings("fbn.cx", startupConfig.getName());\r
+        disableViewing = startupConfig.getDisableViewing();\r
+\r
+        fileManager = new FileManager(startupConfig.getHomeDirPath(), startupConfig.getProgramDirPath());\r
+\r
+\r
                        getServer();\r
                        settings.beginGroup("General");\r
                        String regex = (String) settings.value("regex", "[,\\s]+");\r
                        setWordRegex(regex);\r
-                       String wordString = settings.value("wordLength", "4").toString();\r
+                       String wordString = settings.value("minimumWordLength", "4").toString();\r
                        Integer wordLen = new Integer(wordString);\r
                        Global.minimumWordCount = wordLen;\r
                        settings.endGroup();\r
@@ -172,36 +158,13 @@ public class Global {
                        keepRunning = true;\r
                        disableUploads = disableUploads();\r
                        enableCarriageReturnFix = enableCarriageReturnFix();\r
-//                     dbLock = new DBLock();\r
-//                     indexLock = new DBLock();\r
                        logger = new ApplicationLogger("global.log");\r
                        shortcutKeys = new ShortcutKeys();\r
-                       dbrunnerWorkLock = new QMutex();\r
                        mimicEvernoteInterface = getMimicEvernoteInterface();\r
                        resourceMap = new HashMap<String,String>();\r
                                \r
     }\r
-    public static void setName(String n) {\r
-       if (!n.trim().equals("")) {\r
-               name = "NeverNote-"+n;\r
-       }\r
-    }\r
-    public static String getDirectoryPath() {\r
-               if (currentDir == null) {\r
-                       currentDir = System.getProperty("user.dir");\r
-               }\r
-               if (!currentDir.substring(currentDir.length()-1).equals(File.separator)) {\r
-                       currentDir = currentDir+File.separator;\r
-               }\r
-               return currentDir;\r
-    }\r
-    public static void setDirectoryPath(String path) {\r
-       if (path.trim().equals(""))\r
-               path = System.getProperty("user.dir");\r
-       if (!path.substring(path.length()-1).equals(File.separator)) \r
-                       path = path+File.separator;\r
-       currentDir = path;\r
-    }\r
+\r
     public static String getWordRegex() {\r
        return wordRegex;\r
     }\r
@@ -540,6 +503,23 @@ public class Global {
                        settings.setValue("pdfPreview", "false");\r
                settings.endGroup();\r
     }\r
+    public static boolean newNoteWithSelectedTags() {\r
+               settings.beginGroup("General");\r
+               String text = (String)settings.value("newNoteWithSelectedTags", "false");\r
+               settings.endGroup();\r
+               if (text.equalsIgnoreCase("true"))\r
+                       return true;\r
+               else\r
+                       return false;\r
+    }\r
+    public static void setNewNoteWithSelectedTags(boolean val) {\r
+               settings.beginGroup("General");\r
+               if (val)\r
+                       settings.setValue("newNoteWithSelectedTags", "true");\r
+               else\r
+                       settings.setValue("newNoteWithSelectedTags", "false");\r
+               settings.endGroup();\r
+    }\r
     public static void setMinimumWordLength(int len) {\r
                settings.beginGroup("General");\r
                settings.setValue("minimumWordLength", len);\r
@@ -877,9 +857,14 @@ public class Global {
                String val  = (String)settings.value("DatabaseURL", "");\r
                settings.endGroup();\r
                if (val.equals(""))\r
-                       val = "jdbc:h2:"+Global.getDirectoryPath() +File.separator +"db" +File.separator +Global.databaseName;\r
+                       val = "jdbc:h2:"+Global.getFileManager().getDbDirPath(Global.databaseName);\r
                return val;\r
     }\r
+    public static void setDatabaseUrl(String value) {\r
+               settings.beginGroup("General");\r
+               settings.setValue("DatabaseURL", value);\r
+               settings.endGroup();\r
+    }\r
     public static String getDatabaseUserid() {\r
                settings.beginGroup("General");\r
                String val  = (String)settings.value("databaseUserid", "");\r
@@ -921,69 +906,27 @@ public class Global {
                settings.endGroup();\r
     }\r
     \r
-    public static void dbWait() {\r
- //    Global.dbThreadWait.wait(Global.dbThreadWaitMutex);\r
-    }\r
-    public static void dbContinue() {\r
-//     Global.dbThreadWait.wakeAll();\r
-    }\r
-    public static synchronized void dbClientWait(int id) {\r
-       if (id == mainThreadId) {\r
-                       try {mainThreadWaiter.take(); } catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == syncThreadId) {\r
-                       try {syncThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == tagCounterThreadId) {\r
-               try {tagCounterThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == trashCounterThreadId) {\r
-               try {trashCounterThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == indexThreadId) {\r
-               try {indexThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == saveThreadId) {\r
-               try {saveThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == notebookCounterThreadId) {\r
-               try {notebookCounterThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == indexThread03Id) {\r
-               try {indexThread03ThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
-       if (id == indexThread04Id) {\r
-               try {indexThread04ThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
-       }\r
+    // Get/Set interval when the index thread wakes up.\r
+    public static void setIndexThreadSleepInterval(int sleep) {\r
+               settings.beginGroup("General");\r
+               settings.setValue("IndexThreadSleepInterval", sleep);\r
+               settings.endGroup();\r
     }\r
-    public static void dbClientContinue(int id) {\r
-       if (id == mainThreadId) {\r
-                       try { mainThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-       }\r
-       if (id == syncThreadId) {\r
-                       try { syncThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-       }\r
-               if (id == tagCounterThreadId) { \r
-                       try { tagCounterThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-               }\r
-               if (id == trashCounterThreadId) { \r
-                       try { trashCounterThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-               }\r
-               if (id == indexThreadId) { \r
-                       try { indexThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-               }\r
-               if (id == saveThreadId) { \r
-                       try { saveThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-               }\r
-               if (id == notebookCounterThreadId) { \r
-                       try { notebookCounterThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-               }\r
-               if (id == indexThread03Id) { \r
-                       try { indexThread03ThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
-               }\r
-               if (id == indexThread04Id) { \r
-                       try { indexThread04ThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
+    public static int getIndexThreadSleepInterval() {\r
+               settings.beginGroup("General");\r
+               Integer sleep;\r
+               try {\r
+                       String val  = (String)settings.value("IndexThreadSleepInterval", "300");\r
+                       sleep = new Integer(val.trim());\r
+               } catch (Exception e) {\r
+                       try {\r
+                               sleep = (Integer)settings.value("IndexThreadSleepInterval", 0);\r
+                       } catch (Exception e1) {\r
+                               sleep = 300;\r
+                       }\r
                }\r
+               settings.endGroup();\r
+               return sleep;\r
     }\r
     \r
     public static void saveState(String name, QByteArray state) {\r
@@ -1133,5 +1076,14 @@ public class Global {
                startTraceTime = null;\r
        }\r
 \r
+\r
+    public static FileManager getFileManager() {\r
+        return fileManager;\r
+    }\r
+    public static boolean getDisableViewing() {\r
+        return disableViewing;\r
+    }\r
+    \r
+\r
 }\r
 \r