OSDN Git Service

Add new FileManager class, accessible as Global.getFileManager
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / Global.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote;\r
21 \r
22 \r
23 //import java.io.ByteArrayOutputStream;\r
24 import java.io.File;\r
25 import java.io.PrintStream;\r
26 import java.text.SimpleDateFormat;\r
27 import java.util.ArrayList;\r
28 import java.util.Calendar;\r
29 import java.util.HashMap;\r
30 import java.util.List;\r
31 import java.util.concurrent.ArrayBlockingQueue;\r
32 \r
33 import com.evernote.edam.type.Accounting;\r
34 import com.evernote.edam.type.PrivilegeLevel;\r
35 import com.evernote.edam.type.User;\r
36 import com.evernote.edam.type.UserAttributes;\r
37 import com.trolltech.qt.core.QByteArray;\r
38 import com.trolltech.qt.core.QMutex;\r
39 import com.trolltech.qt.core.QSettings;\r
40 import com.trolltech.qt.gui.QPalette;\r
41 \r
42 import cx.fbn.nevernote.config.FileManager;\r
43 import cx.fbn.nevernote.config.InitializationException;\r
44 import cx.fbn.nevernote.config.StartupConfig;\r
45 import cx.fbn.nevernote.gui.ContainsAttributeFilterTable;\r
46 import cx.fbn.nevernote.gui.DateAttributeFilterTable;\r
47 import cx.fbn.nevernote.gui.ShortcutKeys;\r
48 import cx.fbn.nevernote.signals.DBRunnerSignal;\r
49 import cx.fbn.nevernote.threads.DBRunner;\r
50 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
51 \r
52 public class Global {\r
53         public static String version = "0.88";\r
54     public static String username = ""; \r
55     public static String password = ""; \r
56 \r
57     public static DBRunner                              dbRunner;                       // Database thread\r
58     public static DBRunnerSignal                dbRunnerSignal;         // Signals to the database runner\r
59     public static QMutex                                dbrunnerWorkLock;       // mutex lock for work queue\r
60     \r
61 \r
62     public static int mainThreadId=0;\r
63     private static ArrayBlockingQueue<Boolean> mainThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
64     \r
65     public static int syncThreadId=1;\r
66     private static ArrayBlockingQueue<Boolean> syncThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
67     \r
68     public static int tagCounterThreadId=2;\r
69     private static ArrayBlockingQueue<Boolean> tagCounterThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
70     \r
71     public static int trashCounterThreadId=3;   // This should always be the highest thread ID\r
72     private static ArrayBlockingQueue<Boolean> trashCounterThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
73 \r
74     public static int indexThreadId=4;          // Thread for indexing words\r
75     private static ArrayBlockingQueue<Boolean> indexThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
76 \r
77     public static int saveThreadId=5;           // Thread used for processing data to saving content\r
78     private static ArrayBlockingQueue<Boolean> saveThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
79 \r
80     public static int notebookCounterThreadId=6;   // Notebook Thread\r
81     private static ArrayBlockingQueue<Boolean> notebookCounterThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
82 \r
83     public static int indexThread03Id=7;   // unused\r
84     private static ArrayBlockingQueue<Boolean> indexThread03ThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
85 \r
86     public static int indexThread04Id=8;   // unused\r
87     private static ArrayBlockingQueue<Boolean> indexThread04ThreadWaiter = new ArrayBlockingQueue<Boolean>(1);\r
88     \r
89     public static int dbThreadId=9;   // This should always be the highest thread ID\r
90     \r
91     \r
92     public static HashMap<String,String> passwordSafe = new HashMap<String, String>();\r
93     public static List<String> passwordRemember = new ArrayList<String>();\r
94     public static String currentNotebookGuid;\r
95     public static User user; \r
96     public static long authTimeRemaining;\r
97     public static long authRefreshTime;\r
98     public static long failedRefreshes = 0;\r
99     public static String currentDir;\r
100     public static boolean keepRunning;\r
101     \r
102     public static String userStoreUrl;\r
103     public static String noteStoreUrl;\r
104     public static String noteStoreUrlBase;\r
105     \r
106     public static int noteTableCreationPosition = 0;\r
107     public static int noteTableTitlePosition = 1;\r
108     public static int noteTableTagPosition = 2;\r
109     public static int noteTableNotebookPosition = 3;\r
110     public static int noteTableChangedPosition = 4;\r
111     public static int noteTableGuidPosition = 5;\r
112     public static int noteTableAuthorPosition = 6;\r
113     public static int noteTableSourceUrlPosition = 7;\r
114     public static int noteTableSubjectDatePosition = 8;\r
115     public static int noteTableSynchronizedPosition = 9;\r
116     public static int noteTableColumnCount = 10;\r
117     public static Integer cryptCounter = 0;\r
118     \r
119     public static int minimumWordCount = 2;\r
120     private static String wordRegex;\r
121     public static boolean enableCarriageReturnFix = false;\r
122     \r
123     public static QSettings     settings;\r
124     public static boolean isConnected;\r
125     public static boolean showDeleted = false;\r
126     public static boolean disableUploads = false;\r
127         public static int messageLevel;\r
128         public static String tagDelimeter = ",";\r
129         public static String attachmentNameDelimeter = "------";\r
130         \r
131         public static String    databaseName = new String("NeverNote");\r
132         public static String    indexDatabaseName = new String("IndexDatabase.db");\r
133         public static DateAttributeFilterTable createdSinceFilter;\r
134         public static DateAttributeFilterTable createdBeforeFilter;\r
135         public static DateAttributeFilterTable changedSinceFilter;\r
136         public static DateAttributeFilterTable changedBeforeFilter;\r
137         public static ContainsAttributeFilterTable containsFilter;\r
138 //      public static DBLock                            dbLock;\r
139 //      public static DBLock                            indexLock;\r
140         public static ApplicationLogger    logger;\r
141         PrintStream stdoutStream;\r
142         public static QPalette                          originalPalette;\r
143         public static ShortcutKeys                      shortcutKeys;\r
144         private static boolean                          disableViewing;\r
145         \r
146         public static List<String>                              invalidElements = new ArrayList<String>();\r
147         public static HashMap<String, ArrayList<String>>        invalidAttributes = new HashMap<String, ArrayList<String>>();\r
148         public static boolean mimicEvernoteInterface;\r
149         public static HashMap<String,String> resourceMap;\r
150         public static String cipherPassword = "";\r
151         \r
152         static Calendar startTraceTime;\r
153         static Calendar intervalTraceTime;\r
154 \r
155         private static FileManager fileManager;\r
156 \r
157     // Do initial setup \r
158     public static void setup(StartupConfig startupConfig) throws InitializationException  {\r
159         settings = new QSettings("fbn.cx", startupConfig.getName());\r
160         disableViewing = startupConfig.getDisableViewing();\r
161 \r
162         fileManager = new FileManager(startupConfig.getHomeDirPath());\r
163         currentDir = fileManager.getHomeDirPath();\r
164 \r
165                         getServer();\r
166                         settings.beginGroup("General");\r
167                         String regex = (String) settings.value("regex", "[,\\s]+");\r
168                         setWordRegex(regex);\r
169                         String wordString = settings.value("wordLength", "4").toString();\r
170                         Integer wordLen = new Integer(wordString);\r
171                         Global.minimumWordCount = wordLen;\r
172                         settings.endGroup();\r
173                         settings.beginGroup("Debug");\r
174                         String msglevel = (String) settings.value("messageLevel", "Low");\r
175                         settings.endGroup();\r
176                         messageLevel = 1;\r
177                         setMessageLevel(msglevel);\r
178                         keepRunning = true;\r
179                         disableUploads = disableUploads();\r
180                         enableCarriageReturnFix = enableCarriageReturnFix();\r
181 //                      dbLock = new DBLock();\r
182 //                      indexLock = new DBLock();\r
183                         logger = new ApplicationLogger("global.log");\r
184                         shortcutKeys = new ShortcutKeys();\r
185                         dbrunnerWorkLock = new QMutex();\r
186                         mimicEvernoteInterface = getMimicEvernoteInterface();\r
187                         resourceMap = new HashMap<String,String>();\r
188                                 \r
189     }\r
190 \r
191     public static String getDirectoryPath() {\r
192                 if (currentDir == null) {\r
193                         currentDir = System.getProperty("user.dir");\r
194                 }\r
195                 if (!currentDir.substring(currentDir.length()-1).equals(File.separator)) {\r
196                         currentDir = currentDir+File.separator;\r
197                 }\r
198                 return currentDir;\r
199     }\r
200 \r
201     public static String getWordRegex() {\r
202         return wordRegex;\r
203     }\r
204     public static void setWordRegex(String r) {\r
205         wordRegex = r;\r
206     }\r
207     public static void setMessageLevel(String msglevel) {\r
208         if (msglevel.equalsIgnoreCase("low")) \r
209                         messageLevel = 1;\r
210                 if (msglevel.equalsIgnoreCase("medium")) \r
211                         messageLevel = 2;\r
212                 if (msglevel.equalsIgnoreCase("high")) \r
213                                 messageLevel = 3;\r
214                 if (msglevel.equalsIgnoreCase("extreme")) \r
215                                         messageLevel = 4;\r
216                 settings.beginGroup("Debug");\r
217                 settings.setValue("messageLevel", msglevel);\r
218                 settings.endGroup();            \r
219     }\r
220 \r
221     public static void saveUserInformation(User user) {\r
222         settings.beginGroup("User");\r
223                 settings.setValue("id", user.getId());\r
224                 settings.setValue("username", user.getUsername());\r
225                 settings.setValue("email", user.getEmail());\r
226                 settings.setValue("name", user.getName());\r
227                 settings.setValue("timezone", user.getTimezone());\r
228                 settings.setValue("privilege", user.getPrivilege().getValue());\r
229                 settings.setValue("created", user.getCreated());\r
230                 settings.setValue("updated", user.getUpdated());\r
231                 settings.setValue("deleted", user.getDeleted());\r
232                 settings.endGroup();\r
233                 isPremium();\r
234                 if (user.getAttributes()!=null)\r
235                         saveUserAttributes(user.getAttributes());\r
236                 if (user.getAccounting()!=null)\r
237                         saveUserAccounting(user.getAccounting());\r
238 \r
239     }\r
240     public static void saveUserAttributes(UserAttributes attrib) {\r
241         settings.beginGroup("UserAttributes");\r
242                 settings.setValue("defaultLocationName", attrib.getDefaultLocationName());\r
243                 settings.setValue("defaultLatitude", attrib.getDefaultLocationName());\r
244                 settings.setValue("defaultLongitude", attrib.getDefaultLocationName());\r
245                 settings.setValue("incomingEmailAddress", attrib.getIncomingEmailAddress());\r
246                 settings.endGroup();\r
247     }\r
248     public static UserAttributes getUserAttributes() {\r
249         settings.beginGroup("UserAttributes");\r
250         UserAttributes attrib = new UserAttributes();\r
251                 attrib.setDefaultLocationName((String)settings.value("defaultLocationName",""));\r
252                 attrib.setDefaultLatitudeIsSet(false);\r
253                 attrib.setDefaultLongitudeIsSet(false);\r
254                 attrib.setIncomingEmailAddress((String)settings.value("incomingEmailAddress", ""));\r
255                 settings.endGroup();\r
256                 return attrib;\r
257     }\r
258     public static void saveUserAccounting(Accounting acc) {\r
259         settings.beginGroup("UserAccounting");\r
260                 settings.setValue("uploadLimit", acc.getUploadLimit());\r
261                 settings.setValue("uploadLimitEnd", acc.getUploadLimitEnd());\r
262                 settings.setValue("uploadLimitNextMonth", acc.getUploadLimitNextMonth());\r
263                 settings.setValue("premiumServiceStart", acc.getPremiumServiceStart());\r
264                 settings.setValue("nextPaymentDue", acc.getNextPaymentDue());\r
265                 settings.setValue("uploadAmount", acc.getUpdated());\r
266                 settings.endGroup();\r
267     }\r
268     public static long getUploadLimitEnd() {\r
269         Long limit;\r
270         settings.beginGroup("UserAccounting");\r
271         \r
272         // Upload limit\r
273                 try {\r
274                         String val  = (String)settings.value("uploadLimitEnd", "0");\r
275                         limit = new Long(val.trim());\r
276                 } catch (Exception e) {\r
277                         try {\r
278                                 limit = (Long)settings.value("uploadLimitEnd", 0);\r
279                         } catch (Exception e1) {\r
280                                 limit = new Long(0);\r
281                         }\r
282                 }\r
283         \r
284                 // return value\r
285         settings.endGroup();\r
286         return limit;\r
287     }\r
288     public static void saveUploadAmount(long amount) {\r
289         settings.beginGroup("UserAccounting");\r
290                 settings.setValue("uploadAmount", amount);\r
291                 settings.endGroup();\r
292    }\r
293     public static long getUploadAmount() {\r
294                 long amt=0;\r
295                 settings.beginGroup("UserAccounting");\r
296                 try {\r
297                         String num = (String)settings.value("uploadAmount", "0");\r
298                         amt = new Long(num.trim());\r
299                 } catch (Exception e) {\r
300                         try {\r
301                                 amt = (Integer)settings.value("uploadAmount", 0);\r
302                         } catch (Exception e1) {\r
303                                 amt = 0;\r
304                         }\r
305                 }\r
306                 settings.endGroup();\r
307                 return amt;\r
308     }\r
309     public static void saveEvernoteUpdateCount(long amount) {\r
310         settings.beginGroup("UserAccounting");\r
311                 settings.setValue("updateCount", amount);\r
312                 settings.endGroup();\r
313     }\r
314     public static long getEvernoteUpdateCount() {\r
315                 long amt;\r
316                 settings.beginGroup("UserAccounting");\r
317                 try {\r
318                         String num = (String)settings.value("updateCount", new Long(0).toString());\r
319                         amt = new Long(num.trim());\r
320                 } catch (java.lang.ClassCastException e) {\r
321                         amt = 0;\r
322                 }\r
323                 settings.endGroup();\r
324                 return amt;\r
325     }\r
326     public static boolean isPremium() {\r
327                 int level;\r
328                 settings.beginGroup("User");\r
329                 try {\r
330                         String num = (String)settings.value("privilege", "1");\r
331                         level = new Integer(num.trim());\r
332                 } catch (java.lang.ClassCastException e) {\r
333                         try {\r
334                                 level = (Integer)settings.value("privilege", 1);\r
335                         } catch (Exception e1) {\r
336                                 level = 1;\r
337                         }\r
338                 }\r
339                 settings.endGroup();\r
340                 PrivilegeLevel userLevel = PrivilegeLevel.findByValue(level);\r
341                 if (userLevel == PrivilegeLevel.NORMAL)\r
342                         return false;\r
343                 return true;\r
344                 \r
345    }\r
346     public static long getUploadLimit() {\r
347                 settings.beginGroup("UserAccounting");\r
348                 long limit;\r
349                 try {\r
350                         String num = (String)settings.value("uploadLimit", new Long(0).toString());\r
351                         limit = new Long(num.trim());\r
352                 } catch (java.lang.ClassCastException e) {\r
353                         limit = 0;\r
354                 }\r
355                 settings.endGroup();\r
356                 return limit;\r
357     }\r
358     public static boolean showTrayIcon() {\r
359                 settings.beginGroup("General");\r
360                 String max = (String) settings.value("showTrayIcon", "true");\r
361                 settings.endGroup();\r
362                 if (!max.equalsIgnoreCase("true"))\r
363                         return false;\r
364                 return true;    \r
365     }\r
366     public static void setShowTrayIcon(boolean val) {\r
367                 settings.beginGroup("General");\r
368                 if (val)\r
369                         settings.setValue("showTrayIcon", "true");\r
370                 else\r
371                         settings.setValue("showTrayIcon", "false");\r
372                 settings.endGroup();\r
373     }\r
374     public static boolean wasWindowMaximized() {\r
375                 settings.beginGroup("General");\r
376                 String max = (String) settings.value("isMaximized", "true");\r
377                 settings.endGroup();\r
378                 if (!max.equalsIgnoreCase("true"))\r
379                         return false;\r
380                 return true;    \r
381     }\r
382     public static void saveWindowMaximized(boolean isMax) {\r
383                 settings.beginGroup("General");\r
384                 if (isMax)\r
385                         settings.setValue("isMaximized", "true");\r
386                 else\r
387                         settings.setValue("isMaximized", "false");\r
388                 settings.endGroup();\r
389     }\r
390     public static String getLastViewedNoteGuid() {\r
391                 settings.beginGroup("General");\r
392                 String guid = (String) settings.value("lastViewedNote", "");\r
393                 settings.endGroup();\r
394                 return guid;    \r
395     }\r
396     public static void saveCurrentNoteGuid(String guid) {\r
397                 settings.beginGroup("General");\r
398                 if (guid != null)\r
399                         settings.setValue("lastViewedNote", guid);\r
400                 else\r
401                         settings.setValue("lastViewedNote", "");\r
402                 settings.endGroup();\r
403     }\r
404     public static void setSortColumn(int i) {\r
405                 settings.beginGroup("General");\r
406                 settings.setValue("sortColumn", i);\r
407                 settings.endGroup();\r
408     }\r
409     public static int getSortColumn() {;\r
410         settings.beginGroup("General");\r
411         int order;      \r
412         try {\r
413                 String val  = settings.value("sortColumn", new Integer(0)).toString();\r
414                 order = new Integer(val.trim());\r
415         } catch (Exception e) {\r
416                 try {\r
417                         order = (Integer)settings.value("sortColumn", 0);\r
418                 } catch (Exception e1) {\r
419                     order = 0;\r
420                 }\r
421         }\r
422         \r
423         settings.endGroup();\r
424         return order;\r
425     }\r
426     public static void setSortOrder(int i) {\r
427                 settings.beginGroup("General");\r
428                 settings.setValue("sortOrder", i);\r
429                 settings.endGroup();\r
430     }\r
431     public static int getSortOrder() {\r
432                 settings.beginGroup("General");\r
433                 int order;      \r
434                 try {\r
435                         String val  = settings.value("sortOrder", new Integer(0)).toString();\r
436                         order = new Integer(val.trim());\r
437                 } catch (Exception e) {\r
438                         try {\r
439                                 order = (Integer)settings.value("sortOrder", 0);\r
440                         } catch (Exception e1) {\r
441                             order = 0;\r
442                         }\r
443                 }\r
444                 \r
445                 settings.endGroup();\r
446                 return order;\r
447     }\r
448     public static boolean automaticLogin() {\r
449                 settings.beginGroup("General");\r
450                 String text = (String)settings.value("automaticLogin", "false");\r
451                 settings.endGroup();\r
452                 if (text.equalsIgnoreCase("true"))\r
453                         return true;\r
454                 else\r
455                         return false;           \r
456     }\r
457     public static void setAutomaticLogin(boolean val) {\r
458                 settings.beginGroup("General");\r
459                 if (val)\r
460                         settings.setValue("automaticLogin", "true");\r
461                 else\r
462                         settings.setValue("automaticLogin", "false");\r
463                 settings.endGroup();\r
464     }\r
465     public static boolean rememberPassword() {\r
466                 settings.beginGroup("General");\r
467                 String text = (String)settings.value("rememberPassword", "false");\r
468                 settings.endGroup();\r
469                 if (text.equalsIgnoreCase("true"))\r
470                         return true;\r
471                 else\r
472                         return false;           \r
473     }\r
474     public static void setRememberPassword(boolean val) {\r
475                 settings.beginGroup("General");\r
476                 if (val)\r
477                         settings.setValue("rememberPassword", "true");\r
478                 else\r
479                         settings.setValue("rememberPassword", "false");\r
480                 settings.endGroup();\r
481     }\r
482     public static void setServer(String server) {\r
483                 settings.beginGroup("General");\r
484                 settings.setValue("server", server);\r
485                 settings.endGroup();            \r
486     }\r
487     public static String getServer() {\r
488                 settings.beginGroup("General");\r
489                 String text = (String)settings.value("server", "www.evernote.com");\r
490                 if (text.equals("www.evernote.com")) {\r
491                         userStoreUrl = "https://www.evernote.com/edam/user";\r
492                     noteStoreUrlBase = "www.evernote.com/edam/note/";   \r
493                 } else {\r
494                         userStoreUrl = "https://sandbox.evernote.com/edam/user";\r
495                         noteStoreUrlBase = "sandbox.evernote.com/edam/note/";\r
496                 }\r
497                 settings.endGroup();\r
498                 if (isPremium())\r
499                         noteStoreUrlBase = "https://" + noteStoreUrlBase;\r
500                 else\r
501                         noteStoreUrlBase = "http://" + noteStoreUrlBase;\r
502                 return text;\r
503     }\r
504     public static boolean disableUploads() {\r
505                 settings.beginGroup("General");\r
506                 String text = (String)settings.value("disableUploads", "false");\r
507                 settings.endGroup();\r
508                 if (text.equalsIgnoreCase("true"))\r
509                         return true;\r
510                 else\r
511                         return false;\r
512     }\r
513     public static void setDisableUploads(boolean val) {\r
514                 settings.beginGroup("General");\r
515                 if (val)\r
516                         settings.setValue("disableUploads", "true");\r
517                 else\r
518                         settings.setValue("disableUploads", "false");\r
519                 settings.endGroup();\r
520                 disableUploads = val;\r
521     }\r
522     public static boolean pdfPreview() {\r
523                 settings.beginGroup("General");\r
524                 String text = (String)settings.value("pdfPreview", "true");\r
525                 settings.endGroup();\r
526                 if (text.equalsIgnoreCase("true"))\r
527                         return true;\r
528                 else\r
529                         return false;\r
530     }\r
531     public static void setPdfPreview(boolean val) {\r
532                 settings.beginGroup("General");\r
533                 if (val)\r
534                         settings.setValue("pdfPreview", "true");\r
535                 else\r
536                         settings.setValue("pdfPreview", "false");\r
537                 settings.endGroup();\r
538     }\r
539     public static void setMinimumWordLength(int len) {\r
540                 settings.beginGroup("General");\r
541                 settings.setValue("minimumWordLength", len);\r
542                 settings.endGroup();            \r
543     }\r
544     public static int getMinimumWordLength() {\r
545                 settings.beginGroup("General");\r
546                 Integer len = 4;\r
547                 try {\r
548                         String val  = (String)settings.value("minimumWordLength", "4");\r
549                         len = new Integer(val);\r
550                 } catch (Exception e) {\r
551                         try {\r
552                                 len = (Integer)settings.value("minimumWordLength", 4);\r
553                         } catch (Exception e1) {\r
554                                 len = 4;\r
555                         }\r
556                 }\r
557                 settings.endGroup();\r
558                 return len;\r
559                 \r
560     }\r
561     public static void setRecognitionWeight(int len) {\r
562                 settings.beginGroup("General");\r
563                 settings.setValue("recognitionWeight", len);\r
564                 settings.endGroup();            \r
565     }\r
566     public static int getRecognitionWeight() {\r
567                 settings.beginGroup("General");\r
568                 Integer len;\r
569                 try {\r
570                         len = (Integer)settings.value("recognitionWeight", 80);\r
571                 } catch (Exception e) {\r
572                         len = 80;\r
573                 }\r
574                 settings.endGroup();\r
575                 return len;\r
576     }\r
577     public static String getMessageLevel() {\r
578                 settings.beginGroup("Debug");\r
579                 String text = (String)settings.value("messageLevel", "Low");\r
580                 settings.endGroup();\r
581                 return text;\r
582     }\r
583     public static void setDateFormat(String format) {\r
584                 settings.beginGroup("General");\r
585                 settings.setValue("dateFormat", format);\r
586                 settings.endGroup();            \r
587     }\r
588     public static String getDateFormat() {\r
589                 settings.beginGroup("General");\r
590                 String text = (String)settings.value("dateFormat", "MM/dd/yyyy");\r
591                 settings.endGroup();\r
592                 return text;\r
593     }\r
594     public static void setTimeFormat(String format) {\r
595                 settings.beginGroup("General");\r
596                 settings.setValue("timeFormat", format);\r
597                 settings.endGroup();            \r
598     }\r
599     public static String getTimeFormat() {\r
600                 settings.beginGroup("General");\r
601                 String text = (String)settings.value("timeFormat", "HH:mm:ss");\r
602                 settings.endGroup();\r
603                 return text;\r
604     }\r
605     public static String getSyncInterval() {\r
606                 settings.beginGroup("General");\r
607                 String text = (String)settings.value("syncInterval", "15 minutes");\r
608                 settings.endGroup();\r
609                 return text;            \r
610     }\r
611     public static void setSyncInterval(String format) {\r
612                 settings.beginGroup("General");\r
613                 settings.setValue("syncInterval", format);\r
614                 settings.endGroup();            \r
615     }\r
616     public static void setColumnWidth(String col, int width) {\r
617                 settings.beginGroup("ColumnWidths");\r
618                 settings.setValue(col, width);\r
619                 settings.endGroup();\r
620     }\r
621     public static int getColumnWidth(String col) {\r
622                 settings.beginGroup("ColumnWidths");\r
623                 Integer width;\r
624                 try {\r
625                         String val  = (String)settings.value(col, "0");\r
626                         width = new Integer(val.trim());\r
627                 } catch (Exception e) {\r
628                         try {\r
629                                 width = (Integer)settings.value(col, 0);\r
630                         } catch (Exception e1) {\r
631                                 width = 0;\r
632                         }\r
633                 }\r
634                 settings.endGroup();\r
635                 return width;\r
636     }\r
637     public static void setColumnPosition(String col, int width) {\r
638                 settings.beginGroup("ColumnPosition");\r
639                 settings.setValue(col, width);\r
640                 settings.endGroup();\r
641     }\r
642     public static int getColumnPosition(String col) {\r
643                 settings.beginGroup("ColumnPosition");\r
644                 Integer width;\r
645                 try {\r
646                         String val  = (String)settings.value(col, "-1");\r
647                         width = new Integer(val.trim());\r
648                 } catch (Exception e) {\r
649                         try {\r
650                                 width = (Integer)settings.value(col, 0);\r
651                         } catch (Exception e1) {\r
652                                 width = 0;\r
653                         }\r
654                 }\r
655                 settings.endGroup();\r
656                 return width;\r
657     }\r
658     public static boolean verifyDelete() {\r
659                 settings.beginGroup("General");\r
660                 String text = (String)settings.value("verifyDelete", "true");\r
661                 settings.endGroup();\r
662                 if (text.equalsIgnoreCase("true"))\r
663                         return true;\r
664                 else\r
665                         return false;           \r
666     }\r
667     public static void setVerifyDelete(boolean val) {\r
668                 settings.beginGroup("General");\r
669                 if (val)\r
670                         settings.setValue("verifyDelete", "true");\r
671                 else\r
672                         settings.setValue("verifyDelete", "false");\r
673                 settings.endGroup();\r
674     }\r
675     public static boolean synchronizeDeletedContent() {\r
676                 settings.beginGroup("General");\r
677                 String text = (String)settings.value("syncDeletedContent", "false");\r
678                 settings.endGroup();\r
679                 if (text.equalsIgnoreCase("true"))\r
680                         return true;\r
681                 else\r
682                         return false;           \r
683     }\r
684     public static void setSynchronizeDeletedContent(boolean val) {\r
685                 settings.beginGroup("General");\r
686                 if (val)\r
687                         settings.setValue("syncDeletedContent", "true");\r
688                 else\r
689                         settings.setValue("syncDeletedContent", "false");\r
690                 settings.endGroup();\r
691     }\r
692     public static boolean isWindowVisible(String window) {\r
693                 settings.beginGroup("WindowsVisible");\r
694                 String text = (String)settings.value(window, "true");\r
695                 settings.endGroup();\r
696                 if (text.equalsIgnoreCase("true"))\r
697                         return true;\r
698                 else\r
699                         return false;           \r
700     }\r
701     public static void saveWindowVisible(String window, boolean val) {\r
702                 settings.beginGroup("WindowsVisible");\r
703                 if (val)\r
704                         settings.setValue(window, "true");\r
705                 else\r
706                         settings.setValue(window, "false");\r
707                 settings.endGroup();\r
708     }\r
709     public static boolean isColumnVisible(String window) {\r
710                 settings.beginGroup("ColumnsVisible");\r
711                 String text = (String)settings.value(window, "true");\r
712                 settings.endGroup();\r
713                 if (text.equalsIgnoreCase("true"))\r
714                         return true;\r
715                 else\r
716                         return false;   \r
717     }\r
718     public static void saveColumnVisible(String column, boolean val) {\r
719                 settings.beginGroup("ColumnsVisible");\r
720                 if (val)\r
721                         settings.setValue(column, "true");\r
722                 else\r
723                         settings.setValue(column, "false");\r
724                 settings.endGroup();\r
725     }\r
726     public static boolean isEditorButtonVisible(String window) {\r
727                 settings.beginGroup("EditorButtonsVisible");\r
728                 String text = (String)settings.value(window, "true");\r
729                 settings.endGroup();\r
730                 if (text.equalsIgnoreCase("true"))\r
731                         return true;\r
732                 else\r
733                         return false;   \r
734     }\r
735     public static void saveEditorButtonsVisible(String column, boolean val) {\r
736                 settings.beginGroup("EditorButtonsVisible");\r
737                 if (val)\r
738                         settings.setValue(column, "true");\r
739                 else\r
740                         settings.setValue(column, "false");\r
741                 settings.endGroup();\r
742     }\r
743     public static boolean enableCarriageReturnFix() {\r
744                 settings.beginGroup("Debug");\r
745                 String text = (String)settings.value("enableCarriageReturnFix", "false");\r
746                 settings.endGroup();\r
747                 if (text.equalsIgnoreCase("true"))\r
748                         return true;\r
749                 else\r
750                         return false;   \r
751     }\r
752     public static void saveCarriageReturnFix(boolean val) {\r
753                 settings.beginGroup("Debug");\r
754                 if (val)\r
755                         settings.setValue("enableCarriageReturnFix", "true");\r
756                 else\r
757                         settings.setValue("enableCarriageReturnFix", "false");\r
758                 settings.endGroup();\r
759     }\r
760     public static void setIndexThreads(int val) {\r
761                 settings.beginGroup("General");\r
762                 settings.setValue("indexThreads", val);\r
763                 settings.endGroup();\r
764     }\r
765     public static int getIndexThreads() {\r
766                 settings.beginGroup("General");\r
767                 Integer threads;\r
768                 try {\r
769                         String val  = (String)settings.value("indexThreads", "1");\r
770                         threads = new Integer(val.trim());\r
771                 } catch (Exception e) {\r
772                         try {\r
773                                 threads = (Integer)settings.value("indexThreads", 1);\r
774                         } catch (Exception e1) {\r
775                                 threads = 1;\r
776                         }\r
777                 }\r
778                 settings.endGroup();\r
779                 threads = 1;\r
780                 return threads;\r
781     }\r
782     public static void setZoomFactor(double val) {\r
783                 settings.beginGroup("General");\r
784                 settings.setValue("zoomFactor", val);\r
785                 settings.endGroup();\r
786     }\r
787     public static double getZoomFactor() {\r
788                 settings.beginGroup("General");\r
789                 Double threads;\r
790                 try {\r
791                         String val  = (String)settings.value("zoomFactor", "1.0");\r
792                         threads = new Double(val.trim());\r
793                 } catch (Exception e) {\r
794                         try {\r
795                                 threads = (Double)settings.value("zoomFactor", 1.0);\r
796                         } catch (Exception e1) {\r
797                                 threads = new Double(1);\r
798                         }\r
799                 }\r
800                 settings.endGroup();\r
801                 return threads;\r
802     }\r
803     public static void setTextSizeMultiplier(double val) {\r
804                 settings.beginGroup("General");\r
805                 settings.setValue("textMultiplier", val);\r
806                 settings.endGroup();\r
807     }\r
808     public static double getTextSizeMultiplier() {\r
809                 settings.beginGroup("General");\r
810                 Double threads;\r
811                 try {\r
812                         String val  = (String)settings.value("textMultiplier", "1");\r
813                         threads = new Double(val.trim());\r
814                 } catch (Exception e) {\r
815                         try {\r
816                                 threads = (Double)settings.value("textMultiplier", 1);\r
817                         } catch (Exception e1) {\r
818                                 threads = new Double(1);\r
819                         }\r
820                 }\r
821                 settings.endGroup();\r
822                 return threads;\r
823     }\r
824 \r
825     public static boolean getMimicEvernoteInterface() {\r
826                 settings.beginGroup("General");\r
827                 String text = (String)settings.value("mimicEvernoteInterface", "true");\r
828                 settings.endGroup();\r
829                 if (text.equalsIgnoreCase("true"))\r
830                         return true;\r
831                 else\r
832                         return false;\r
833     }\r
834     public static void setMimicEvernoteInterface(boolean value) {\r
835         settings.beginGroup("General");\r
836         if (value)\r
837                 settings.setValue("mimicEvernoteInterface", "true");\r
838         else\r
839                 settings.setValue("mimicEvernoteInterface", "false"); \r
840         settings.endGroup();\r
841     }\r
842     \r
843     public static boolean synchronizeOnClose() {\r
844                 settings.beginGroup("General");\r
845                 String text = (String)settings.value("synchronizeOnClose", "false");\r
846                 settings.endGroup();\r
847                 if (text.equalsIgnoreCase("true"))\r
848                         return true;\r
849                 else\r
850                         return false;\r
851     }\r
852     public static void setSynchronizeOnClose(boolean val) {\r
853                 settings.beginGroup("General");\r
854                 if (val)\r
855                         settings.setValue("synchronizeOnClose", "true");\r
856                 else\r
857                         settings.setValue("synchronizeOnClose", "false");\r
858                 settings.endGroup();\r
859     }\r
860     public static void setDatabaseVersion(String version) {\r
861                 settings.beginGroup("General");\r
862                 settings.setValue("databaseVersion", version);\r
863                 settings.endGroup();\r
864     }\r
865     public static String getDatabaseVersion() {\r
866                 settings.beginGroup("General");\r
867                 String val  = (String)settings.value("databaseVersion", "0.70");\r
868                 settings.endGroup();\r
869                 return val;\r
870     }\r
871     public static String getDatabaseUrl() {\r
872                 settings.beginGroup("General");\r
873                 String val  = (String)settings.value("DatabaseURL", "");\r
874                 settings.endGroup();\r
875                 if (val.equals(""))\r
876                         val = "jdbc:h2:"+Global.getDirectoryPath() +File.separator +"db" +File.separator +Global.databaseName;\r
877                 return val;\r
878     }\r
879     public static String getDatabaseUserid() {\r
880                 settings.beginGroup("General");\r
881                 String val  = (String)settings.value("databaseUserid", "");\r
882                 settings.endGroup();\r
883                 return val;\r
884     }\r
885     public static String getDatabaseUserPassword() {\r
886                 settings.beginGroup("General");\r
887                 String val  = (String)settings.value("databaseUserPassword", "");\r
888                 settings.endGroup();\r
889                 return val;\r
890     }\r
891     public static void setStyle(String style) {\r
892                 settings.beginGroup("General");\r
893                 settings.setValue("style", style);\r
894                 settings.endGroup();\r
895     }\r
896     public static String getStyle() {\r
897                 settings.beginGroup("General");\r
898                 String val  = (String)settings.value("style", "");\r
899                 settings.endGroup();\r
900                 return val;\r
901     }\r
902     public static boolean useStandardPalette() {\r
903                 settings.beginGroup("General");\r
904                 String text = (String)settings.value("standardPalette", "true");\r
905                 settings.endGroup();\r
906                 if (text.equalsIgnoreCase("true"))\r
907                         return true;\r
908                 else\r
909                         return false;\r
910     }\r
911     public static void setStandardPalette(boolean val) {\r
912                 settings.beginGroup("General");\r
913                 if (val)\r
914                         settings.setValue("standardPalette", "true");\r
915                 else\r
916                         settings.setValue("standardPalette", "false");\r
917                 settings.endGroup();\r
918     }\r
919     \r
920     public static void dbWait() {\r
921  //     Global.dbThreadWait.wait(Global.dbThreadWaitMutex);\r
922     }\r
923     public static void dbContinue() {\r
924 //      Global.dbThreadWait.wakeAll();\r
925     }\r
926     public static synchronized void dbClientWait(int id) {\r
927         if (id == mainThreadId) {\r
928                         try {mainThreadWaiter.take(); } catch (InterruptedException e) {e.printStackTrace();}\r
929         }\r
930         if (id == syncThreadId) {\r
931                         try {syncThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
932         }\r
933         if (id == tagCounterThreadId) {\r
934                 try {tagCounterThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
935         }\r
936         if (id == trashCounterThreadId) {\r
937                 try {trashCounterThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
938         }\r
939         if (id == indexThreadId) {\r
940                 try {indexThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
941         }\r
942         if (id == saveThreadId) {\r
943                 try {saveThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
944         }\r
945         if (id == notebookCounterThreadId) {\r
946                 try {notebookCounterThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
947         }\r
948         if (id == indexThread03Id) {\r
949                 try {indexThread03ThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
950         }\r
951         if (id == indexThread04Id) {\r
952                 try {indexThread04ThreadWaiter.take();} catch (InterruptedException e) {e.printStackTrace();}\r
953         }\r
954     }\r
955     public static void dbClientContinue(int id) {\r
956         if (id == mainThreadId) {\r
957                         try { mainThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
958         }\r
959         if (id == syncThreadId) {\r
960                         try { syncThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
961         }\r
962                 if (id == tagCounterThreadId) { \r
963                         try { tagCounterThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
964                 }\r
965                 if (id == trashCounterThreadId) { \r
966                         try { trashCounterThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
967                 }\r
968                 if (id == indexThreadId) { \r
969                         try { indexThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
970                 }\r
971                 if (id == saveThreadId) { \r
972                         try { saveThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
973                 }\r
974                 if (id == notebookCounterThreadId) { \r
975                         try { notebookCounterThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
976                 }\r
977                 if (id == indexThread03Id) { \r
978                         try { indexThread03ThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
979                 }\r
980                 if (id == indexThread04Id) { \r
981                         try { indexThread04ThreadWaiter.put(true); } catch (InterruptedException e) { e.printStackTrace();}\r
982                 }\r
983     }\r
984     \r
985     public static void saveState(String name, QByteArray state) {\r
986                 settings.beginGroup("SaveState");\r
987                 settings.setValue(name, state);\r
988                 settings.endGroup();\r
989     }\r
990     \r
991     public static QByteArray restoreState(String name) {\r
992                 settings.beginGroup("SaveState");\r
993                 QByteArray state = (QByteArray)settings.value(name);\r
994                 settings.endGroup();\r
995                 return state;\r
996     }\r
997     public static void saveGeometry(String name, QByteArray state) {\r
998                 settings.beginGroup("SaveGeometry");\r
999                 settings.setValue(name, state);\r
1000                 settings.endGroup();\r
1001     }\r
1002     \r
1003     public static QByteArray restoreGeometry(String name) {\r
1004                 settings.beginGroup("SaveGeometry");\r
1005                 QByteArray state = (QByteArray)settings.value(name);\r
1006                 settings.endGroup();\r
1007                 return state;\r
1008     }\r
1009     \r
1010     public static void setAutoSaveInterval(int interval) {\r
1011                 settings.beginGroup("General");\r
1012                 settings.setValue("autoSaveInterval", interval);\r
1013                 settings.endGroup();\r
1014     }\r
1015     public static int getAutoSaveInterval() {\r
1016                 settings.beginGroup("General");\r
1017                 Integer value;\r
1018                 try {\r
1019                         String val  = (String)settings.value("autoSaveInterval", "5");\r
1020                         value = new Integer(val.trim());\r
1021                 } catch (Exception e) {\r
1022                         try {\r
1023                                 value = (Integer)settings.value("autoSaveInterval", 5);\r
1024                         } catch (Exception e1) {\r
1025                                 value = 0;\r
1026                         }\r
1027                 }\r
1028                 settings.endGroup();\r
1029                 return value;\r
1030     }\r
1031      \r
1032     // Add invalid attributes\r
1033     public static void addInvalidAttribute(String element, String attribute) {\r
1034         \r
1035                 List<String> attributes = invalidAttributes.get(element);\r
1036                 if (attributes != null) {\r
1037                         for (int i=0; i<attributes.size(); i++)\r
1038                                 if (attribute.equalsIgnoreCase(attributes.get(i))) {\r
1039                                         return;\r
1040                         }\r
1041         }\r
1042         \r
1043         ArrayList<String> attributeList;\r
1044         if (!invalidAttributes.containsKey(element)) {\r
1045                 attributeList = new ArrayList<String>();\r
1046                 attributeList.add(attribute);\r
1047                 invalidAttributes.put(element, attributeList);\r
1048         }\r
1049         else {\r
1050                 attributeList = invalidAttributes.get(element);\r
1051                 attributeList.add(attribute);\r
1052                 invalidAttributes.put(element,attributeList);\r
1053         }\r
1054     }\r
1055 \r
1056     \r
1057     // Add invalid attributes\r
1058     public static void addInvalidElement(String element) {\r
1059                 for (int i=0; i<invalidElements.size(); i++) {\r
1060                         if (element.equalsIgnoreCase(invalidElements.get(i)))\r
1061                                 return;\r
1062                 }\r
1063         invalidElements.add(element);\r
1064     }\r
1065     \r
1066     // Convert a byte array to a hex string\r
1067         public static String byteArrayToHexString(byte data[]) {\r
1068                 StringBuffer buf = new StringBuffer();\r
1069             for (byte element : data) {\r
1070                 int halfbyte = (element >>> 4) & 0x0F;\r
1071                 int two_halfs = 0;\r
1072                 do {\r
1073                         if ((0 <= halfbyte) && (halfbyte <= 9))\r
1074                                buf.append((char) ('0' + halfbyte));\r
1075                            else\r
1076                                 buf.append((char) ('a' + (halfbyte - 10)));\r
1077                         halfbyte = element & 0x0F;\r
1078                 } while(two_halfs++ < 1);\r
1079             }\r
1080             return buf.toString();              \r
1081         }\r
1082 \r
1083         \r
1084         // What to do with inactive tags?\r
1085         public static String tagBehavior() {\r
1086                 settings.beginGroup("General");\r
1087                 String text = (String)settings.value("tagBehavior", "DoNothing");\r
1088                 settings.endGroup();\r
1089                 return text;\r
1090         }\r
1091         // What to do with inactive tags?\r
1092         public static void setTagBehavior(String value) {\r
1093                 settings.beginGroup("General");\r
1094                 settings.setValue("tagBehavior", value);\r
1095                 settings.endGroup();\r
1096         }\r
1097 \r
1098         \r
1099         // Print date/time.  Used mainly for performance tracing\r
1100         public static void trace(boolean resetInterval) {\r
1101                 String fmt = "MM/dd/yy HH:mm:ss.SSSSSS";\r
1102                 String dateTimeFormat = new String(fmt);\r
1103                 SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);\r
1104                 Calendar cal = Calendar.getInstance();\r
1105                 if (intervalTraceTime == null) \r
1106                         intervalTraceTime = Calendar.getInstance();\r
1107                 if (startTraceTime == null)\r
1108                         startTraceTime = Calendar.getInstance();\r
1109                 \r
1110                 float interval = (cal.getTimeInMillis() - intervalTraceTime.getTimeInMillis());\r
1111                 float total = (cal.getTimeInMillis() - startTraceTime.getTimeInMillis());\r
1112                 \r
1113 //              if (interval > 00.0) {\r
1114                         StackTraceElement[] exceptions = Thread.currentThread().getStackTrace();\r
1115                         System.out.println("------------------------------------------");\r
1116 \r
1117                         System.out.println("Date/Time " +simple.format(cal.getTime()));\r
1118                         System.out.format("Interval Time: %-10.6f%n", interval);\r
1119                         System.out.format("Total Time: %-10.6f%n", total);\r
1120                         for (int i=2; i<5 && i<exceptions.length; i++) {\r
1121                                 System.out.println(exceptions[i]);\r
1122                         }\r
1123 //              }\r
1124                 if (resetInterval)\r
1125                         intervalTraceTime = cal;\r
1126         }\r
1127         public static void traceReset() {\r
1128                 intervalTraceTime = null;\r
1129                 startTraceTime = null;\r
1130         }\r
1131 \r
1132     public static FileManager getFileManager() {\r
1133         return fileManager;\r
1134     }\r
1135 \r
1136     public static boolean getDisableViewing() {\r
1137         return disableViewing;\r
1138     }\r
1139 }\r
1140 \r