OSDN Git Service

Cleanup public linked notebook table logic.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / NeverNote.java
1 /*
2  * This file is part of NeverNote 
3  * Copyright 2009 Randy Baumgarte
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
8  * Software distributed under the License is distributed
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10  * express or implied. See the GPL for the specific language
11  * governing rights and limitations.
12  *
13  * You should have received a copy of the GPL along with this
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html
15  * or write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18 */
19 package cx.fbn.nevernote;
20 import java.awt.Desktop;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.net.Authenticator;
25 import java.net.PasswordAuthentication;
26 import java.security.MessageDigest;
27 import java.security.NoSuchAlgorithmException;
28 import java.sql.Connection;
29 import java.sql.DriverManager;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Calendar;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.Comparator;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.SortedMap;
44 import java.util.Vector;
45
46 import org.apache.log4j.Level;
47 import org.apache.log4j.Logger;
48 import org.apache.thrift.TException;
49 import org.h2.tools.ChangeFileEncryption;
50
51 import com.evernote.edam.error.EDAMNotFoundException;
52 import com.evernote.edam.error.EDAMSystemException;
53 import com.evernote.edam.error.EDAMUserException;
54 import com.evernote.edam.notestore.NoteFilter;
55 import com.evernote.edam.notestore.NoteVersionId;
56 import com.evernote.edam.type.Data;
57 import com.evernote.edam.type.LinkedNotebook;
58 import com.evernote.edam.type.Note;
59 import com.evernote.edam.type.NoteAttributes;
60 import com.evernote.edam.type.Notebook;
61 import com.evernote.edam.type.Publishing;
62 import com.evernote.edam.type.QueryFormat;
63 import com.evernote.edam.type.Resource;
64 import com.evernote.edam.type.SavedSearch;
65 import com.evernote.edam.type.Tag;
66 import com.evernote.edam.type.User;
67 import com.trolltech.qt.QThread;
68 import com.trolltech.qt.core.QByteArray;
69 import com.trolltech.qt.core.QDateTime;
70 import com.trolltech.qt.core.QDir;
71 import com.trolltech.qt.core.QEvent;
72 import com.trolltech.qt.core.QFile;
73 import com.trolltech.qt.core.QFileInfo;
74 import com.trolltech.qt.core.QFileSystemWatcher;
75 import com.trolltech.qt.core.QIODevice;
76 import com.trolltech.qt.core.QIODevice.OpenModeFlag;
77 import com.trolltech.qt.core.QLocale;
78 import com.trolltech.qt.core.QModelIndex;
79 import com.trolltech.qt.core.QSize;
80 import com.trolltech.qt.core.QTemporaryFile;
81 import com.trolltech.qt.core.QTextCodec;
82 import com.trolltech.qt.core.QThreadPool;
83 import com.trolltech.qt.core.QTimer;
84 import com.trolltech.qt.core.QTranslator;
85 import com.trolltech.qt.core.QUrl;
86 import com.trolltech.qt.core.Qt;
87 import com.trolltech.qt.core.Qt.BGMode;
88 import com.trolltech.qt.core.Qt.ItemDataRole;
89 import com.trolltech.qt.core.Qt.SortOrder;
90 import com.trolltech.qt.core.Qt.WidgetAttribute;
91 import com.trolltech.qt.gui.QAbstractItemView;
92 import com.trolltech.qt.gui.QAbstractItemView.ScrollHint;
93 import com.trolltech.qt.gui.QAction;
94 import com.trolltech.qt.gui.QApplication;
95 import com.trolltech.qt.gui.QCloseEvent;
96 import com.trolltech.qt.gui.QColor;
97 import com.trolltech.qt.gui.QComboBox;
98 import com.trolltech.qt.gui.QCursor;
99 import com.trolltech.qt.gui.QDesktopServices;
100 import com.trolltech.qt.gui.QDialog;
101 import com.trolltech.qt.gui.QFileDialog;
102 import com.trolltech.qt.gui.QFileDialog.AcceptMode;
103 import com.trolltech.qt.gui.QFileDialog.FileMode;
104 import com.trolltech.qt.gui.QGridLayout;
105 import com.trolltech.qt.gui.QHBoxLayout;
106 import com.trolltech.qt.gui.QIcon;
107 import com.trolltech.qt.gui.QImage;
108 import com.trolltech.qt.gui.QKeySequence;
109 import com.trolltech.qt.gui.QLabel;
110 import com.trolltech.qt.gui.QMainWindow;
111 import com.trolltech.qt.gui.QMenu;
112 import com.trolltech.qt.gui.QMessageBox;
113 import com.trolltech.qt.gui.QMessageBox.StandardButton;
114 import com.trolltech.qt.gui.QPainter;
115 import com.trolltech.qt.gui.QPalette.ColorRole;
116 import com.trolltech.qt.gui.QPixmap;
117 import com.trolltech.qt.gui.QPrintDialog;
118 import com.trolltech.qt.gui.QPrinter;
119 import com.trolltech.qt.gui.QShortcut;
120 import com.trolltech.qt.gui.QSizePolicy;
121 import com.trolltech.qt.gui.QSizePolicy.Policy;
122 import com.trolltech.qt.gui.QSpinBox;
123 import com.trolltech.qt.gui.QSplashScreen;
124 import com.trolltech.qt.gui.QSplitter;
125 import com.trolltech.qt.gui.QStatusBar;
126 import com.trolltech.qt.gui.QSystemTrayIcon;
127 import com.trolltech.qt.gui.QTableWidgetItem;
128 import com.trolltech.qt.gui.QTextEdit;
129 import com.trolltech.qt.gui.QToolBar;
130 import com.trolltech.qt.gui.QTreeWidgetItem;
131 import com.trolltech.qt.network.QNetworkAccessManager;
132 import com.trolltech.qt.network.QNetworkReply;
133 import com.trolltech.qt.network.QNetworkRequest;
134 import com.trolltech.qt.webkit.QWebPage.WebAction;
135 import com.trolltech.qt.webkit.QWebSettings;
136
137 import cx.fbn.nevernote.config.InitializationException;
138 import cx.fbn.nevernote.config.StartupConfig;
139 import cx.fbn.nevernote.dialog.AccountDialog;
140 import cx.fbn.nevernote.dialog.ConfigDialog;
141 import cx.fbn.nevernote.dialog.DBEncryptDialog;
142 import cx.fbn.nevernote.dialog.DatabaseLoginDialog;
143 import cx.fbn.nevernote.dialog.DatabaseStatus;
144 import cx.fbn.nevernote.dialog.FindDialog;
145 import cx.fbn.nevernote.dialog.IgnoreSync;
146 import cx.fbn.nevernote.dialog.LogFileDialog;
147 import cx.fbn.nevernote.dialog.LoginDialog;
148 import cx.fbn.nevernote.dialog.NotebookArchive;
149 import cx.fbn.nevernote.dialog.NotebookEdit;
150 import cx.fbn.nevernote.dialog.OnlineNoteHistory;
151 import cx.fbn.nevernote.dialog.PublishNotebook;
152 import cx.fbn.nevernote.dialog.SavedSearchEdit;
153 import cx.fbn.nevernote.dialog.SetIcon;
154 import cx.fbn.nevernote.dialog.ShareNotebook;
155 import cx.fbn.nevernote.dialog.StackNotebook;
156 import cx.fbn.nevernote.dialog.TagEdit;
157 import cx.fbn.nevernote.dialog.TagMerge;
158 import cx.fbn.nevernote.dialog.ThumbnailViewer;
159 import cx.fbn.nevernote.dialog.UpgradeAvailableDialog;
160 import cx.fbn.nevernote.dialog.WatchFolder;
161 import cx.fbn.nevernote.filters.FilterEditorNotebooks;
162 import cx.fbn.nevernote.filters.FilterEditorTags;
163 import cx.fbn.nevernote.gui.AttributeTreeWidget;
164 import cx.fbn.nevernote.gui.BrowserWindow;
165 import cx.fbn.nevernote.gui.DateAttributeFilterTable;
166 import cx.fbn.nevernote.gui.ExternalBrowse;
167 import cx.fbn.nevernote.gui.MainMenuBar;
168 import cx.fbn.nevernote.gui.NotebookTreeWidget;
169 import cx.fbn.nevernote.gui.SavedSearchTreeWidget;
170 import cx.fbn.nevernote.gui.SearchPanel;
171 import cx.fbn.nevernote.gui.TableView;
172 import cx.fbn.nevernote.gui.TagTreeWidget;
173 import cx.fbn.nevernote.gui.Thumbnailer;
174 import cx.fbn.nevernote.gui.TrashTreeWidget;
175 import cx.fbn.nevernote.gui.controls.QuotaProgressBar;
176 import cx.fbn.nevernote.sql.DatabaseConnection;
177 import cx.fbn.nevernote.sql.WatchFolderRecord;
178 import cx.fbn.nevernote.threads.IndexRunner;
179 import cx.fbn.nevernote.threads.SyncRunner;
180 import cx.fbn.nevernote.threads.ThumbnailRunner;
181 import cx.fbn.nevernote.utilities.AESEncrypter;
182 import cx.fbn.nevernote.utilities.ApplicationLogger;
183 import cx.fbn.nevernote.utilities.FileImporter;
184 import cx.fbn.nevernote.utilities.FileUtils;
185 import cx.fbn.nevernote.utilities.ListManager;
186 import cx.fbn.nevernote.utilities.SyncTimes;
187 import cx.fbn.nevernote.xml.ExportData;
188 import cx.fbn.nevernote.xml.ImportData;
189 import cx.fbn.nevernote.xml.NoteFormatter;
190
191
192 public class NeverNote extends QMainWindow{
193         
194         QStatusBar                              statusBar;                                      // Application status bar
195         
196         DatabaseConnection              conn;
197         
198         MainMenuBar                             menuBar;                                        // Main menu bar
199         FindDialog                              find;                                           // Text search in note dialog
200         List<String>                    emitLog;                                        // Messages displayed in the status bar;
201         QSystemTrayIcon                 trayIcon;                                       // little tray icon
202         QMenu                                   trayMenu;                                       // System tray menu
203         QAction                                 trayExitAction;                         // Exit the application
204         QAction                                 trayShowAction;                         // toggle the show/hide action          
205         QAction                                 trayAddNoteAction;                      // Add a note from the system tray
206         QNetworkAccessManager   versionChecker;                         // Used when checking for new versions
207         
208     NotebookTreeWidget          notebookTree;                           // List of notebooks
209     AttributeTreeWidget         attributeTree;                          // List of note attributes
210     TagTreeWidget                       tagTree;                                        // list of user created tags
211     SavedSearchTreeWidget       savedSearchTree;                        // list of saved searches
212     TrashTreeWidget                     trashTree;                                      // Trashcan
213     TableView                           noteTableView;                          //      List of notes (the widget).
214
215     public BrowserWindow        browserWindow;                          // Window containing browser & labels
216     public QToolBar             toolBar;                                        // The tool bar under the menu
217     QComboBox                           searchField;                            // search filter bar on the toolbar;
218     QShortcut                           searchShortcut;                         // Shortcut to search bar
219     boolean                                     searchPerformed = false;        // Search was done?
220     QuotaProgressBar            quotaBar;                                       // The current quota usage
221     
222     ApplicationLogger           logger;
223     List<String>                        selectedNotebookGUIDs;          // List of notebook GUIDs
224     List<String>                        selectedTagGUIDs;                       // List of selected tag GUIDs
225     List<String>                        selectedNoteGUIDs;                      // List of selected notes
226     String                                      selectedSavedSearchGUID;        // Currently selected saved searches
227     private final HashMap<String, ExternalBrowse>       externalWindows;        // Notes being edited by an external window;
228     
229     NoteFilter                          filter;                                         // Note filter
230     String                                      currentNoteGuid;                        // GUID of the current note 
231     Note                                        currentNote;                            // The currently viewed note
232     boolean                                     noteDirty;                                      // Has the note been changed?
233     boolean                             inkNote;                   // if this is an ink note, it is read only
234     boolean                                     readOnly;                                       // Is this note read-only?
235         
236   
237     ListManager                         listManager;                                    // DB runnable task
238     
239     List<QTemporaryFile>        tempFiles;                                      // Array of temporary files;
240     
241     QTimer                                      indexTimer;                                     // timer to start the index thread
242     IndexRunner                         indexRunner;                            // thread to index notes
243     QThread                                     indexThread;
244     
245     QTimer                                      syncTimer;                                      // Sync on an interval
246     QTimer                                      syncDelayTimer;                         // Sync delay to free up database
247     SyncRunner                          syncRunner;                                     // thread to do a sync.
248     QThread                                     syncThread;                                     // Thread which talks to evernote
249     ThumbnailRunner                     thumbnailRunner;                        // Runner for thumbnail thread
250     QThread                                     thumbnailThread;                        // Thread that generates pretty pictures
251     QTimer                                      saveTimer;                                      // Timer to save note contents
252     
253     QTimer                                      authTimer;                                      // Refresh authentication
254     QTimer                                      externalFileSaveTimer;          // Save files altered externally
255     QTimer                                      thumbnailTimer;                         // Wakeup & scan for thumbnails
256     List<String>                        externalFiles;                          // External files to save later
257     List<String>                        importFilesKeep;                        // Auto-import files to save later
258     List<String>                        importFilesDelete;                      // Auto-import files to save later
259     
260     int                                         indexTime;                                      // how often to try and index
261     boolean                                     indexRunning;                           // Is indexing running?
262     boolean                                     indexDisabled;                          // Is indexing disabled?
263     
264     int                                         syncThreadsReady;                       // number of sync threads that are free
265     int                                         syncTime;                                       // Sync interval
266     boolean                                     syncRunning;                            // Is sync running?
267     boolean                                     automaticSync;                          // do sync automatically?
268     QTreeWidgetItem                     attributeTreeSelected;
269
270     QAction                             prevButton;                                     // Go to the previous item viewed
271     QAction                             nextButton;                                     // Go to the next item in the history
272     QAction                             downButton;                                     // Go to the next item in the list
273     QAction                             upButton;                                       // Go to the prev. item in the list;
274     QAction                             synchronizeButton;                      // Synchronize with Evernote
275     QAction                             allNotesButton;                         // Reset & view all notes
276     QTimer                              synchronizeAnimationTimer;      // Timer to change animation button
277     int                                 synchronizeIconAngle;           // Used to rotate sync icon
278     QAction                     printButton;                            // Print Button
279     QAction                             tagButton;                                      // Tag edit button
280     QAction                             attributeButton;                        // Attribute information button
281     QAction                     emailButton;                            // Email button
282     QAction                     deleteButton;                           // Delete button
283     QAction                             newButton;                                      // new Note Button;
284     QSpinBox                    zoomSpinner;                            // Zoom zoom
285     QAction                             searchClearButton;                      // Clear the search field
286     
287     SearchPanel                 searchLayout;                           // Widget to hold search field, zoom, & quota
288     
289     QSplitter                   mainLeftRightSplitter;          // main splitter for left/right side
290     QSplitter                   leftSplitter1;                          // first left hand splitter
291     QSplitter                   browserIndexSplitter;           // splitter between note index & note text
292     
293     QFileSystemWatcher  importKeepWatcher;                      // Watch & keep auto-import
294     QFileSystemWatcher  importDeleteWatcher;            // Watch & Delete auto-import
295     List<String>                importedFiles;                          // History of imported files (so we don't import twice)
296     
297     OnlineNoteHistory   historyWindow;                          // online history window 
298     List<NoteVersionId> versions;                                       // history versions
299     
300     QTimer                              threadMonitorTimer;                     // Timer to watch threads.
301     int                                 dbThreadDeadCount=0;            // number of consecutive dead times for the db thread
302     int                                 syncThreadDeadCount=0;          // number of consecutive dead times for the sync thread
303     int                                 indexThreadDeadCount=0;         // number of consecutive dead times for the index thread
304     int                                 notebookThreadDeadCount=0;      // number of consecutive dead times for the notebook thread
305     int                                 tagDeadCount=0;                         // number of consecutive dead times for the tag thread
306     int                                 trashDeadCount=0;                       // number of consecutive dead times for the trash thread
307     int                                 saveThreadDeadCount=0;          // number of consecutive dead times for the save thread
308     boolean                             disableTagThreadCheck=false;
309     boolean                             disableNotebookThreadCheck=false;
310     boolean                             disableTrashThreadCheck=false;
311     boolean                             disableSaveThreadCheck=false;
312     boolean                             disableSyncThreadCheck=false;
313     boolean                             disableIndexThreadCheck=false;
314     
315     HashMap<String, String>             noteCache;                      // Cash of note content 
316     HashMap<String, Boolean>    readOnlyCache;          // List of cashe notes that are read-only
317     HashMap<String, Boolean>    inkNoteCache;           // List of cache notes that are ink notes 
318     List<String>                historyGuids;                           // GUIDs of previously viewed items
319     int                                 historyPosition;                        // Position within the viewed items
320     boolean                             fromHistory;                            // Is this from the history queue?
321     String                              trashNoteGuid;                          // Guid to restore / set into or out of trash to save position
322     List<Thumbnailer>   thumbGenerators;                                // generate preview image
323     ThumbnailViewer             thumbnailViewer;                        // View preview thumbnail; 
324     boolean                             encryptOnShutdown;                      // should I encrypt when I close?
325     boolean                             decryptOnShutdown;                      // should I decrypt on shutdown;
326     String                              encryptCipher;                          // What cipher should I use?
327     Signal0                     minimizeToTray;
328     boolean                             windowMaximized = false;        // Keep track of the window state for restores
329     List<String>                pdfReadyQueue;                          // Queue of PDFs that are ready to be rendered.
330     List<QPixmap>               syncIcons;                                      // Array of icons used in sync animation
331     private boolean             closeAction = false;            // Used to say when to close or when to minimize
332     private static Logger log = Logger.getLogger(NeverNote.class); 
333     private String              saveLastPath;                           // last path we used
334     private final QTimer                messageTimer;                           // Timer to clear the status message.
335     private long                blockTime;                                      // When the app. is blocked, this is the time to unblock it.
336     
337     
338     String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
339         
340         
341     //***************************************************************
342     //***************************************************************
343     //** Constructor & main entry point
344     //***************************************************************
345     //***************************************************************
346     // Application Constructor  
347         @SuppressWarnings("static-access")
348         public NeverNote(DatabaseConnection dbConn)  {
349                 conn = dbConn;          
350                 if (conn.getConnection() == null) {
351                         String msg = new String(tr("Unable to connect to the database.\n\nThe most probable reason is that some other process\n" +
352                                 "is accessing the database or NeverNote is already running.\n\n" +
353                                 "Please end any other process or shutdown the other NeverNote before starting.\n\nExiting program."));
354                         
355             QMessageBox.critical(null, tr("Database Connection Error") ,msg);
356                         System.exit(16);
357                 }
358                 setObjectName("mainWindow");
359 //              thread().setPriority(Thread.MAX_PRIORITY);
360                 
361                 logger = new ApplicationLogger("nevernote.log");
362                 logger.log(logger.HIGH, "Starting Application");
363                 
364                 decryptOnShutdown = false;
365                 encryptOnShutdown = false;
366                 conn.checkDatabaseVersion();
367                 
368                 
369                 
370                 // Start building the invalid XML tables
371                 Global.invalidElements = conn.getInvalidXMLTable().getInvalidElements();
372                 List<String> elements = conn.getInvalidXMLTable().getInvalidAttributeElements();
373                 
374                 for (int i=0; i<elements.size(); i++) {
375                         Global.invalidAttributes.put(elements.get(i), conn.getInvalidXMLTable().getInvalidAttributes(elements.get(i)));
376                 }
377                 
378                 logger.log(logger.EXTREME, "Starting GUI build");
379
380                 QTranslator nevernoteTranslator = new QTranslator();
381                 nevernoteTranslator.load(Global.getFileManager().getTranslateFilePath("nevernote_" + QLocale.system().name() + ".qm"));
382                 QApplication.instance().installTranslator(nevernoteTranslator);
383
384                 Global.originalPalette = QApplication.palette();
385                 QApplication.setStyle(Global.getStyle());
386                 if (Global.useStandardPalette())
387                         QApplication.setPalette(QApplication.style().standardPalette());
388         setWindowTitle("NeverNote");
389
390         mainLeftRightSplitter = new QSplitter();
391         setCentralWidget(mainLeftRightSplitter);
392         leftSplitter1 = new QSplitter();
393         leftSplitter1.setOrientation(Qt.Orientation.Vertical);
394                 
395         browserIndexSplitter = new QSplitter();
396         browserIndexSplitter.setOrientation(Qt.Orientation.Vertical);
397         
398         //* Setup threads & thread timers
399         int indexRunnerCount = Global.getIndexThreads();
400         indexRunnerCount = 1;
401         QThreadPool.globalInstance().setMaxThreadCount(indexRunnerCount+5);     // increase max thread count
402
403                 logger.log(logger.EXTREME, "Building list manager");
404         listManager = new ListManager(conn, logger);
405         
406                 logger.log(logger.EXTREME, "Building index runners & timers");
407         indexRunner = new IndexRunner("indexRunner.log", 
408                         Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),
409                         Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
410                 indexThread = new QThread(indexRunner, "Index Thread");
411         indexRunner.indexAttachmentsLocally = Global.indexAttachmentsLocally();
412                 indexThread.start();
413                 
414         synchronizeAnimationTimer = new QTimer();
415         synchronizeAnimationTimer.timeout.connect(this, "updateSyncButton()");
416         
417                 indexTimer = new QTimer();
418                 indexTime = 1000*Global.getIndexThreadSleepInterval();  
419                 indexTimer.start(indexTime);  // Start indexing timer
420                 indexTimer.timeout.connect(this, "indexTimer()");
421                 indexDisabled = false;
422                 indexRunning = false;
423                                 
424                 logger.log(logger.EXTREME, "Setting sync thread & timers");
425                 syncThreadsReady=1;
426                 syncRunner = new SyncRunner("syncRunner.log", 
427                                 Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),
428                                 Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
429                 syncTime = new SyncTimes().timeValue(Global.getSyncInterval());
430                 syncTimer = new QTimer();
431                 syncTimer.timeout.connect(this, "syncTimer()");
432         syncRunner.status.message.connect(this, "setMessage(String)");
433         syncRunner.syncSignal.finished.connect(this, "syncThreadComplete(Boolean)");
434         syncRunner.syncSignal.errorDisconnect.connect(this, "remoteErrorDisconnect()");
435         syncRunning = false;    
436                 if (syncTime > 0) {
437                         automaticSync = true;
438                         syncTimer.start(syncTime*60*1000);
439                 } else {
440                         automaticSync = false;
441                         syncTimer.stop();
442                 }
443                 syncRunner.setEvernoteUpdateCount(Global.getEvernoteUpdateCount());
444                 syncThread = new QThread(syncRunner, "Synchronization Thread");
445                 syncThread.start();
446                 
447                 
448                 logger.log(logger.EXTREME, "Starting thumnail thread");
449                 pdfReadyQueue = new ArrayList<String>();
450                 thumbnailRunner = new ThumbnailRunner("thumbnailRunner.log", 
451                                 Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),
452                                 Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
453                 thumbnailThread = new QThread(thumbnailRunner, "Thumbnail Thread");
454                 thumbnailRunner.noteSignal.thumbnailPageReady.connect(this, "thumbnailHTMLReady(String,QByteArray,Integer)");
455                 thumbnailThread.start();
456                 thumbGenerators = new ArrayList<Thumbnailer>();
457                 thumbnailTimer = new QTimer();
458                 thumbnailTimer.timeout.connect(this, "thumbnailTimer()");
459                 thumbnailTimer();
460                 thumbnailTimer.setInterval(500*1000);  // Thumbnail every minute
461                 thumbnailTimer.start();
462                 
463                 logger.log(logger.EXTREME, "Starting authentication timer");
464                 authTimer = new QTimer();
465                 authTimer.timeout.connect(this, "authTimer()");
466                 authTimer.start(1000*60*15);
467                 syncRunner.syncSignal.authRefreshComplete.connect(this, "authRefreshComplete(boolean)");
468                 
469                 logger.log(logger.EXTREME, "Setting save note timer");
470                 saveTimer = new QTimer();
471                 saveTimer.timeout.connect(this, "saveNote()");
472                 if (Global.getAutoSaveInterval() > 0) {
473                         saveTimer.setInterval(1000*60*Global.getAutoSaveInterval()); 
474                         saveTimer.start();
475                 }
476                 listManager.saveRunner.noteSignals.noteSaveRunnerError.connect(this, "saveRunnerError(String, String)");
477                 
478                 logger.log(logger.EXTREME, "Starting external file monitor timer");
479                 externalFileSaveTimer = new QTimer();
480                 externalFileSaveTimer.timeout.connect(this, "externalFileEditedSaver()");
481                 externalFileSaveTimer.setInterval(1000*5);   // save every 5 seconds;
482                 externalFiles = new ArrayList<String>();
483                 importFilesDelete = new ArrayList<String>();
484                 importFilesKeep = new ArrayList<String>();
485                 externalFileSaveTimer.start();
486                 
487         notebookTree = new NotebookTreeWidget(conn);
488         attributeTree = new AttributeTreeWidget();
489         tagTree = new TagTreeWidget(conn);
490         savedSearchTree = new SavedSearchTreeWidget();
491         trashTree = new TrashTreeWidget();
492         noteTableView = new TableView(logger, listManager);
493         
494         
495         searchField = new QComboBox();
496         searchField.setObjectName("searchField");
497         //setStyleSheet("QComboBox#searchField { background-color: yellow }");
498         searchField.setEditable(true);
499         searchField.activatedIndex.connect(this, "searchFieldChanged()");
500         searchField.setDuplicatesEnabled(false);
501         searchField.editTextChanged.connect(this,"searchFieldTextChanged(String)");
502         searchShortcut = new QShortcut(this);
503         setupShortcut(searchShortcut, "Focus_Search");
504         searchShortcut.activated.connect(this, "focusSearch()");
505         
506         quotaBar = new QuotaProgressBar();
507         // Setup the zoom
508         zoomSpinner = new QSpinBox();
509         zoomSpinner.setMinimum(10);
510         zoomSpinner.setMaximum(1000);
511         zoomSpinner.setAccelerated(true);
512         zoomSpinner.setSingleStep(10);
513         zoomSpinner.setValue(100);
514         zoomSpinner.valueChanged.connect(this, "zoomChanged()");
515         
516         searchLayout = new SearchPanel(searchField, quotaBar, notebookTree, zoomSpinner);
517         
518         
519         QGridLayout leftGrid = new QGridLayout();
520         leftSplitter1.setContentsMargins(5, 0, 0, 7);
521         leftSplitter1.setLayout(leftGrid);
522         leftGrid.addWidget(searchLayout,1,1);
523         leftGrid.addWidget(tagTree,2,1);
524         leftGrid.addWidget(attributeTree,3,1);
525         leftGrid.addWidget(savedSearchTree,4,1);
526         leftGrid.addWidget(trashTree,5, 1);
527         
528         // Setup the browser window
529         noteCache = new HashMap<String,String>();
530         readOnlyCache = new HashMap<String, Boolean>();
531         inkNoteCache = new HashMap<String, Boolean>();
532         browserWindow = new BrowserWindow(conn);
533
534         mainLeftRightSplitter.addWidget(leftSplitter1);
535         mainLeftRightSplitter.addWidget(browserIndexSplitter);
536         
537         if (Global.getListView() == Global.View_List_Wide) {
538                 browserIndexSplitter.addWidget(noteTableView);
539                 browserIndexSplitter.addWidget(browserWindow); 
540         } else {
541                 mainLeftRightSplitter.addWidget(noteTableView);
542                 mainLeftRightSplitter.addWidget(browserWindow); 
543         }
544         
545         // Setup the thumbnail viewer
546         thumbnailViewer = new ThumbnailViewer();
547         thumbnailViewer.upArrow.connect(this, "upAction()");
548         thumbnailViewer.downArrow.connect(this, "downAction()");
549         thumbnailViewer.leftArrow.connect(this, "nextViewedAction()");
550         thumbnailViewer.rightArrow.connect(this, "previousViewedAction()");
551         
552         //Setup external browser manager
553         externalWindows = new HashMap<String, ExternalBrowse>();
554
555         listManager.loadNotesIndex();
556         initializeNotebookTree();
557         initializeTagTree();
558         initializeSavedSearchTree();
559         attributeTree.itemClicked.connect(this, "attributeTreeClicked(QTreeWidgetItem, Integer)");
560         attributeTreeSelected = null;
561         initializeNoteTable();    
562
563                 selectedNoteGUIDs = new ArrayList<String>();
564                 statusBar = new QStatusBar();
565                 setStatusBar(statusBar);
566                 menuBar = new MainMenuBar(this);
567                 emitLog = new ArrayList<String>();
568                 
569                 tagTree.setDeleteAction(menuBar.tagDeleteAction);
570                 tagTree.setMergeAction(menuBar.tagMergeAction);
571                 tagTree.setEditAction(menuBar.tagEditAction);
572                 tagTree.setAddAction(menuBar.tagAddAction);
573                 tagTree.setIconAction(menuBar.tagIconAction);
574                 tagTree.setVisible(Global.isWindowVisible("tagTree"));
575                 leftSplitter1.setVisible(Global.isWindowVisible("leftPanel"));
576                 tagTree.noteSignal.tagsAdded.connect(this, "tagsAdded(String, String)");
577                 menuBar.hideTags.setChecked(Global.isWindowVisible("tagTree"));
578                 listManager.tagSignal.listChanged.connect(this, "reloadTagTree()");
579                 
580                 if (!Global.isWindowVisible("zoom")) {
581                         searchLayout.hideZoom();
582                         menuBar.hideZoom.setChecked(false);
583                 } 
584         
585                 notebookTree.setDeleteAction(menuBar.notebookDeleteAction);
586                 notebookTree.setEditAction(menuBar.notebookEditAction);
587                 notebookTree.setAddAction(menuBar.notebookAddAction);
588                 notebookTree.setIconAction(menuBar.notebookIconAction);
589                 notebookTree.setStackAction(menuBar.notebookStackAction);
590                 notebookTree.setPublishAction(menuBar.notebookPublishAction);
591                 notebookTree.setShareAction(menuBar.notebookShareAction);
592                 notebookTree.setVisible(Global.isWindowVisible("notebookTree"));
593                 notebookTree.noteSignal.notebookChanged.connect(this, "updateNoteNotebook(String, String)");
594                 notebookTree.noteSignal.tagsChanged.connect(this, "updateNoteTags(String, List)");
595             notebookTree.noteSignal.tagsChanged.connect(this, "updateListTags(String, List)");
596                 menuBar.hideNotebooks.setChecked(Global.isWindowVisible("notebookTree"));
597
598                 savedSearchTree.setAddAction(menuBar.savedSearchAddAction);
599                 savedSearchTree.setEditAction(menuBar.savedSearchEditAction);
600                 savedSearchTree.setDeleteAction(menuBar.savedSearchDeleteAction);
601                 savedSearchTree.setIconAction(menuBar.savedSearchIconAction);
602                 savedSearchTree.itemSelectionChanged.connect(this, "updateSavedSearchSelection()");
603                 savedSearchTree.setVisible(Global.isWindowVisible("savedSearchTree"));
604                 menuBar.hideSavedSearches.setChecked(Global.isWindowVisible("savedSearchTree"));
605                         
606                 noteTableView.setAddAction(menuBar.noteAdd);
607                 noteTableView.setDeleteAction(menuBar.noteDelete);
608                 noteTableView.setRestoreAction(menuBar.noteRestoreAction);
609                 noteTableView.setNoteDuplicateAction(menuBar.noteDuplicateAction);
610                 noteTableView.setNoteHistoryAction(menuBar.noteOnlineHistoryAction);
611                 noteTableView.noteSignal.titleColorChanged.connect(this, "titleColorChanged(Integer)");
612                 noteTableView.setMergeNotesAction(menuBar.noteMergeAction);
613                 noteTableView.doubleClicked.connect(this, "listDoubleClick()");
614                 listManager.trashSignal.countChanged.connect(trashTree, "updateCounts(Integer)");
615                 
616                 quotaBar.setMouseClickAction(menuBar.accountAction);
617                 
618                 trashTree.load();
619         trashTree.itemSelectionChanged.connect(this, "trashTreeSelection()");
620                 trashTree.setEmptyAction(menuBar.emptyTrashAction);
621                 trashTree.setVisible(Global.isWindowVisible("trashTree"));
622                 menuBar.hideTrash.setChecked(Global.isWindowVisible("trashTree"));
623                 trashTree.updateCounts(listManager.getTrashCount());
624                 attributeTree.setVisible(Global.isWindowVisible("attributeTree"));
625                 menuBar.hideAttributes.setChecked(Global.isWindowVisible("attributeTree"));
626
627                 noteTableView.setVisible(Global.isWindowVisible("noteList"));
628                 menuBar.hideNoteList.setChecked(Global.isWindowVisible("noteList"));
629                 
630                 if (!Global.isWindowVisible("editorButtonBar"))
631                         toggleEditorButtonBar();
632                 if (!Global.isWindowVisible("leftPanel"))
633                         menuBar.hideLeftSide.setChecked(true);
634                 if (Global.isWindowVisible("noteInformation"))
635                         toggleNoteInformation();
636                 quotaBar.setVisible(Global.isWindowVisible("quota"));
637                 if (!quotaBar.isVisible())
638                         menuBar.hideQuota.setChecked(false);
639                 searchField.setVisible(Global.isWindowVisible("searchField"));
640                 if (!searchField.isVisible())
641                         menuBar.hideSearch.setChecked(false);
642                 
643                 if (searchField.isHidden() && quotaBar.isHidden() && zoomSpinner.isHidden() && notebookTree.isHidden())
644                         searchLayout.hide();
645                 
646                 setMenuBar(menuBar);
647                 setupToolBar();
648                 find = new FindDialog();
649                 find.getOkButton().clicked.connect(this, "doFindText()");
650                 
651                 // Setup the tray icon menu bar
652                 trayShowAction = new QAction("Show/Hide", this);
653                 trayExitAction = new QAction("Exit", this);
654                 trayAddNoteAction = new QAction("Add Note", this);
655                 
656                 trayExitAction.triggered.connect(this, "closeNeverNote()");
657                 trayAddNoteAction.triggered.connect(this, "addNote()");
658                 trayShowAction.triggered.connect(this, "trayToggleVisible()");
659                 
660                 trayMenu = new QMenu(this);
661                 trayMenu.addAction(trayAddNoteAction);
662                 trayMenu.addAction(trayShowAction);
663                 trayMenu.addAction(trayExitAction);
664                 
665                 
666                 trayIcon = new QSystemTrayIcon(this);
667                 trayIcon.setToolTip("NeverNote");
668                 trayIcon.setContextMenu(trayMenu);
669                 trayIcon.activated.connect(this, "trayActivated(com.trolltech.qt.gui.QSystemTrayIcon$ActivationReason)");
670
671                 currentNoteGuid="";
672                 currentNoteGuid = Global.getLastViewedNoteGuid();
673         historyGuids = new ArrayList<String>();
674         historyPosition = 0;
675         fromHistory = false;
676                 noteDirty = false;
677                 if (!currentNoteGuid.trim().equals("")) {
678                         currentNote = conn.getNoteTable().getNote(currentNoteGuid, true,true,false,false,true);
679                 }
680                 
681                 noteIndexUpdated(true);
682                 showColumns();
683                 menuBar.showEditorBar.setChecked(Global.isWindowVisible("editorButtonBar"));
684                 if (menuBar.showEditorBar.isChecked())
685                 showEditorButtons(browserWindow);
686                 tagIndexUpdated(true);
687                 savedSearchIndexUpdated();
688                 notebookIndexUpdated();
689                 updateQuotaBar();
690         setupSyncSignalListeners();        
691         setupBrowserSignalListeners();
692         setupIndexListeners();
693               
694         
695         tagTree.tagSignal.listChanged.connect(this, "tagIndexUpdated()");
696         tagTree.showAllTags(true);
697
698                 QIcon appIcon = new QIcon(iconPath+"nevernote.png");
699         setWindowIcon(appIcon);
700         trayIcon.setIcon(appIcon);
701         if (Global.showTrayIcon())
702                 trayIcon.show();
703         else
704                 trayIcon.hide();
705         
706         scrollToGuid(currentNoteGuid);
707         if (Global.automaticLogin()) {
708                 remoteConnect();
709                 if (Global.isConnected)
710                         syncTimer();
711         }
712         setupFolderImports();
713         
714         loadStyleSheet();
715         restoreWindowState(true);
716         
717         if (Global.mimicEvernoteInterface) {
718                 notebookTree.selectGuid("");
719         }
720         
721         threadMonitorTimer = new QTimer();
722         threadMonitorTimer.timeout.connect(this, "threadMonitorCheck()");
723         threadMonitorTimer.start(1000*10);  // Check for threads every 10 seconds;              
724         
725         historyGuids.add(currentNoteGuid);
726         historyPosition = 1;
727         
728         menuBar.blockSignals(true);
729         menuBar.narrowListView.blockSignals(true);
730         menuBar.wideListView.blockSignals(true);
731         if (Global.getListView() == Global.View_List_Narrow) { 
732                 menuBar.narrowListView.setChecked(true);
733         }
734         else{ 
735                 menuBar.wideListView.setChecked(true);
736         }
737         menuBar.blockSignals(false);
738         menuBar.narrowListView.blockSignals(false);
739         menuBar.wideListView.blockSignals(false);
740
741         if (Global.getListView() == Global.View_List_Wide) {
742                 browserIndexSplitter.addWidget(noteTableView);
743                 browserIndexSplitter.addWidget(browserWindow); 
744         } else {
745                 mainLeftRightSplitter.addWidget(noteTableView);
746                 mainLeftRightSplitter.addWidget(browserWindow); 
747         }
748         
749                 messageTimer = new QTimer();
750                 messageTimer.timeout.connect(this, "clearMessage()");
751                 messageTimer.setInterval(1000*15);
752                 clearMessage();
753         
754         int sortCol = Global.getSortColumn();
755                 int sortOrder = Global.getSortOrder();
756                 noteTableView.sortByColumn(sortCol, SortOrder.resolve(sortOrder));
757                 if (Global.checkVersionUpgrade())
758                         checkForUpdates();
759         }
760                 
761         // Main entry point
762         public static void main(String[] args) {
763                 log.setLevel(Level.FATAL);
764                 QApplication.initialize(args);
765                 QPixmap pixmap = new QPixmap("classpath:cx/fbn/nevernote/icons/splash_logo.png");
766                 QSplashScreen splash = new QSplashScreen(pixmap);
767                 boolean showSplash;
768                 
769                 DatabaseConnection dbConn;
770
771         try {
772             initializeGlobalSettings(args);
773
774             showSplash = Global.isWindowVisible("SplashScreen");
775             if (showSplash)
776                 splash.show();
777
778             dbConn = setupDatabaseConnection();
779
780             // Must be last stage of setup - only safe once DB is open hence we know we are the only instance running
781             Global.getFileManager().purgeResDirectory(true);
782
783         } catch (InitializationException e) {
784             // Fatal
785             e.printStackTrace();
786             QMessageBox.critical(null, "Startup error", "Aborting: " + e.getMessage());
787             return;
788         }
789
790         NeverNote application = new NeverNote(dbConn);
791
792                 application.setAttribute(WidgetAttribute.WA_DeleteOnClose, true);
793                 if (Global.startMinimized()) 
794                         application.showMinimized();
795                 else {
796                         if (Global.wasWindowMaximized())
797                                 application.showMaximized();
798                         else
799                                 application.show();
800                 }
801                 
802                 if (showSplash)
803                         splash.finish(application);
804                 QApplication.exec();
805                 System.out.println("Goodbye.");
806                 QApplication.exit();
807         }
808
809     /**
810      * Open the internal database, or create if not present
811      *
812      * @throws InitializationException when opening the database fails, e.g. because another process has it locked
813      */
814     private static DatabaseConnection setupDatabaseConnection() throws InitializationException {
815         ApplicationLogger logger = new ApplicationLogger("nevernote-database.log");
816         
817         File f = Global.getFileManager().getDbDirFile(Global.databaseName + ".h2.db");
818                 boolean dbExists = f.exists(); 
819                 if (!dbExists)
820                         Global.setDatabaseUrl("");
821         
822         if (Global.getDatabaseUrl().toUpperCase().indexOf("CIPHER=") > -1) {
823             boolean goodCheck = false;
824             while (!goodCheck) {
825                 DatabaseLoginDialog dialog = new DatabaseLoginDialog();
826                 dialog.exec();
827                 if (!dialog.okPressed())
828                     System.exit(0);
829                 Global.cipherPassword = dialog.getPassword();
830                 goodCheck = databaseCheck(Global.getDatabaseUrl(), Global.getDatabaseUserid(),
831                         Global.getDatabaseUserPassword(), Global.cipherPassword);
832             }
833         }
834         DatabaseConnection dbConn = new DatabaseConnection(logger,Global.getDatabaseUrl(), 
835                         Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),
836                         Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword, 0);
837        return dbConn;
838     }
839     
840     // Encrypt the database upon shutdown
841     private void encryptOnShutdown() {
842         String dbPath= Global.getFileManager().getDbDirPath("");
843         String dbName = "NeverNote";
844         try {
845                 Statement st = conn.getConnection().createStatement();  
846                 st.execute("shutdown");
847                 if (QMessageBox.question(this, tr("Are you sure"), 
848                                 tr("Are you sure you wish to encrypt the database?"),
849                                 QMessageBox.StandardButton.Yes, 
850                                 QMessageBox.StandardButton.No) == StandardButton.Yes.value()) {
851                         ChangeFileEncryption.execute(dbPath, dbName, encryptCipher, null, Global.cipherPassword.toCharArray(), true);
852                         Global.setDatabaseUrl(Global.getDatabaseUrl() + ";CIPHER="+encryptCipher);
853                         QMessageBox.information(this, "Encryption Complete", "Encryption is complete");
854                 }
855         } catch (SQLException e) {
856                         e.printStackTrace();
857                 }       
858     }
859     
860     // Decrypt the database upon shutdown
861     private void decryptOnShutdown() {
862         String dbPath= Global.getFileManager().getDbDirPath("");
863         String dbName = "NeverNote";
864         try {
865                 Statement st = conn.getConnection().createStatement();  
866                 st.execute("shutdown");
867                 if (Global.getDatabaseUrl().toUpperCase().indexOf(";CIPHER=AES") > -1)
868                         encryptCipher = "AES";
869                 else
870                         encryptCipher = "XTEA";
871                 if (QMessageBox.question(this, tr("Confirmation"), tr("Are you sure", 
872                                 "Are you sure you wish to decrypt the database?"),
873                                 QMessageBox.StandardButton.Yes, 
874                                 QMessageBox.StandardButton.No) == StandardButton.Yes.value()) {
875
876                         ChangeFileEncryption.execute(dbPath, dbName, encryptCipher, Global.cipherPassword.toCharArray(), null, true);
877                         Global.setDatabaseUrl("");
878                         QMessageBox.information(this, tr("Decryption Complete"), tr("Decryption is complete"));
879                 }
880                 } catch (SQLException e) {
881                         e.printStackTrace();
882                 }       
883     }
884     /**
885      * Encrypt/Decrypt the local database
886      **/
887     public void doDatabaseEncrypt() {
888         // The database is not currently encrypted
889         if (Global.getDatabaseUrl().toUpperCase().indexOf("CIPHER=") == -1) {
890                 if (QMessageBox.question(this, tr("Confirmation"), tr("Encrypting the database is used" +
891                                 "to enhance security and is performed\nupon shutdown, but please be aware that if"+
892                                 " you lose the password your\nis lost forever.\n\nIt is highly recommended you " +
893                                 "perform a backup and/or fully synchronize\n prior to executing this funtction.\n\n" +
894                                 "Do you wish to proceed?"),
895                                 QMessageBox.StandardButton.Yes, 
896                                 QMessageBox.StandardButton.No)==StandardButton.No.value()) {
897                                 return;
898                 }
899                 DBEncryptDialog dialog = new DBEncryptDialog();
900                 dialog.exec();
901                 if (dialog.okPressed()) {
902                         Global.cipherPassword = dialog.getPassword();
903                         encryptOnShutdown  = true;
904                         encryptCipher = dialog.getEncryptionMethod();
905                 }
906         } else {
907             DBEncryptDialog dialog = new DBEncryptDialog();
908             dialog.setWindowTitle("Database Decryption");
909             dialog.hideEncryption();
910             dialog.exec();
911             if (dialog.okPressed()) {
912                 if (!dialog.getPassword().equals(Global.cipherPassword)) {
913                         QMessageBox.critical(null, tr("Incorrect Password"), tr("Incorrect Password"));
914                         return;
915                 }
916                 decryptOnShutdown  = true;
917                 encryptCipher = "";
918             }
919         }
920         return;
921     }
922
923         private static void initializeGlobalSettings(String[] args) throws InitializationException {
924                 StartupConfig   startupConfig = new StartupConfig();
925
926         for (String arg : args) {
927             String lower = arg.toLowerCase();
928             if (lower.startsWith("--name="))
929                startupConfig.setName(arg.substring(arg.indexOf('=') + 1));
930             if (lower.startsWith("--home="))
931                startupConfig.setHomeDirPath(arg.substring(arg.indexOf('=') + 1));
932             if (lower.startsWith("--disable-viewing"))
933                startupConfig.setDisableViewing(true);
934         }
935                 Global.setup(startupConfig);
936     }
937
938     // Exit point
939         @Override
940         public void closeEvent(QCloseEvent event) {     
941                 if (Global.minimizeOnClose() && !closeAction && Global.showTrayIcon()) {
942                         event.ignore();
943                         hide();
944                         return;
945                 }
946                 logger.log(logger.HIGH, "Entering NeverNote.closeEvent");
947                 waitCursor(true);
948                 
949                 if (currentNote!= null & browserWindow!=null) {
950                         if (!currentNote.getTitle().equals(browserWindow.getTitle()))
951                                 conn.getNoteTable().updateNoteTitle(currentNote.getGuid(), browserWindow.getTitle());
952                 }
953                 saveNote();
954                 setMessage(tr("Beginning shutdown."));
955                 
956                 // Close down external windows
957                 Collection<ExternalBrowse> windows = externalWindows.values();
958                 Iterator<ExternalBrowse> iterator = windows.iterator();
959                 while (iterator.hasNext()) {
960                         ExternalBrowse browser = iterator.next();
961                         browser.windowClosing.disconnect();
962                         browser.close();
963                 }
964                 
965                 
966                 externalFileEditedSaver();
967                 if (Global.isConnected && Global.synchronizeOnClose()) {
968                         setMessage(tr("Performing synchronization before closing."));
969                         syncRunner.syncNeeded = true;
970                         syncRunner.addWork("SYNC");
971                 } else {
972                         syncRunner.keepRunning = false;
973                 }
974                 syncRunner.addWork("STOP");
975                 setMessage("Closing Program.");
976                 threadMonitorTimer.stop();
977
978                 thumbnailRunner.addWork("STOP");
979                 indexRunner.addWork("STOP");
980                 saveNote();
981                 listManager.stop();
982                 saveWindowState();
983
984                 if (tempFiles != null)
985                         tempFiles.clear();
986
987                 browserWindow.noteSignal.tagsChanged.disconnect();
988                 browserWindow.noteSignal.titleChanged.disconnect();
989                 browserWindow.noteSignal.noteChanged.disconnect();
990                 browserWindow.noteSignal.notebookChanged.disconnect();
991                 browserWindow.noteSignal.createdDateChanged.disconnect();
992                 browserWindow.noteSignal.alteredDateChanged.disconnect();
993                 syncRunner.searchSignal.listChanged.disconnect();
994                 syncRunner.tagSignal.listChanged.disconnect();
995         syncRunner.notebookSignal.listChanged.disconnect();
996         syncRunner.noteIndexSignal.listChanged.disconnect();
997
998                 if (isVisible())
999                         Global.saveWindowVisible("toolBar", toolBar.isVisible());
1000                 saveNoteColumnPositions();
1001                 saveNoteIndexWidth();
1002                 
1003                 int width = notebookTree.columnWidth(0);
1004                 Global.setColumnWidth("notebookTreeName", width);
1005                 width = tagTree.columnWidth(0);
1006                 Global.setColumnWidth("tagTreeName", width);
1007                 
1008                 Global.saveWindowMaximized(isMaximized());
1009                 Global.saveCurrentNoteGuid(currentNoteGuid);
1010                         
1011                 int sortCol = noteTableView.proxyModel.sortColumn();
1012                 int sortOrder = noteTableView.proxyModel.sortOrder().value();
1013                 Global.setSortColumn(sortCol);
1014                 Global.setSortOrder(sortOrder);
1015                 
1016                 hide();
1017                 trayIcon.hide();
1018                 Global.keepRunning = false;
1019                 try {
1020                         logger.log(logger.MEDIUM, "Waiting for indexThread to stop");
1021                         if (indexRunner.thread().isAlive())
1022                                 indexRunner.thread().join(50);
1023                         if (!indexRunner.thread().isAlive())
1024                                 logger.log(logger.MEDIUM, "Index thread has stopped");
1025                         else {
1026                                 logger.log(logger.MEDIUM, "Index thread still running - interrupting");
1027                                 indexRunner.thread().interrupt();
1028                         }
1029                 } catch (InterruptedException e1) {
1030                         e1.printStackTrace();
1031                 }
1032                 
1033                 if (!syncRunner.thread().isAlive()) {
1034                         logger.log(logger.MEDIUM, "Waiting for syncThread to stop");
1035                         if (syncRunner.thread().isAlive()) {
1036                                 System.out.println(tr("Synchronizing.  Please be patient."));
1037                                 for(;syncRunner.thread().isAlive();) {
1038                                         try {
1039                                                 wait(10);
1040                                         } catch (InterruptedException e) {
1041                                                 e.printStackTrace();
1042                                         }
1043                                 }
1044                         }
1045                         logger.log(logger.MEDIUM, "Sync thread has stopped");
1046                 }
1047
1048                 if (encryptOnShutdown) {
1049                         encryptOnShutdown();
1050                 }
1051                 if (decryptOnShutdown) {
1052                         decryptOnShutdown();
1053                 }
1054                 try {
1055                         Global.getFileManager().purgeResDirectory(false);
1056                 } catch (InitializationException e) {
1057                         System.out.println(tr("Empty res directory purge failed"));
1058                         e.printStackTrace();
1059                 }
1060                 logger.log(logger.HIGH, "Leaving NeverNote.closeEvent");
1061         }
1062
1063         @SuppressWarnings("unused")
1064         private void closeNeverNote() {
1065                 closeAction = true;
1066                 close();
1067         }
1068         public void setMessage(String s) {
1069                 logger.log(logger.HIGH, "Entering NeverNote.setMessage");
1070                 
1071                 statusBar.show();
1072                 logger.log(logger.HIGH, "Message: " +s);
1073                 statusBar.showMessage(s);
1074                 emitLog.add(s);
1075                 
1076
1077                 messageTimer.stop();
1078                 messageTimer.setSingleShot(true);
1079                 messageTimer.start();
1080                 
1081                 
1082                 logger.log(logger.HIGH, "Leaving NeverNote.setMessage");
1083         }
1084         
1085         private void clearMessage() {
1086                 statusBar.clearMessage();
1087                 statusBar.hide();
1088         }
1089                 
1090         private void waitCursor(boolean wait) {
1091                 if (wait) {
1092                         if (QApplication.overrideCursor() == null)
1093                                 QApplication.setOverrideCursor(new QCursor(Qt.CursorShape.WaitCursor));
1094                 }
1095                 else {
1096                         while (QApplication.overrideCursor() != null)
1097                                 QApplication.restoreOverrideCursor();
1098                 }
1099                 listManager.refreshCounters();
1100         }
1101         
1102         private void setupIndexListeners() {
1103 //              indexRunner.noteSignal.noteIndexed.connect(this, "indexThreadComplete(String)");
1104 //              indexRunner.resourceSignal.resourceIndexed.connect(this, "indexThreadComplete(String)");
1105                 indexRunner.signal.indexStarted.connect(this, "indexStarted()");
1106                 indexRunner.signal.indexFinished.connect(this, "indexComplete()");
1107         }
1108         private void setupSyncSignalListeners() {
1109                 syncRunner.tagSignal.listChanged.connect(this, "tagIndexUpdated()");
1110         syncRunner.searchSignal.listChanged.connect(this, "savedSearchIndexUpdated()");
1111         syncRunner.notebookSignal.listChanged.connect(this, "notebookIndexUpdated()");
1112         syncRunner.noteIndexSignal.listChanged.connect(this, "noteIndexUpdated(boolean)");
1113         syncRunner.noteSignal.quotaChanged.connect(this, "updateQuotaBar()");
1114         
1115                 syncRunner.syncSignal.saveUploadAmount.connect(this,"saveUploadAmount(long)");
1116                 syncRunner.syncSignal.saveUserInformation.connect(this,"saveUserInformation(User)");
1117                 syncRunner.syncSignal.saveEvernoteUpdateCount.connect(this,"saveEvernoteUpdateCount(int)");
1118                 
1119                 syncRunner.noteSignal.guidChanged.connect(this, "noteGuidChanged(String, String)");
1120                 syncRunner.noteSignal.noteChanged.connect(this, "invalidateNoteCache(String, String)");
1121                 syncRunner.resourceSignal.resourceGuidChanged.connect(this, "noteResourceGuidChanged(String,String,String)");
1122                 syncRunner.noteSignal.noteDownloaded.connect(listManager, "noteDownloaded(Note)");
1123                 syncRunner.noteSignal.notebookChanged.connect(this, "updateNoteNotebook(String, String)");
1124                 
1125                 syncRunner.syncSignal.refreshLists.connect(this, "refreshLists()");
1126         }
1127         
1128         private void setupBrowserSignalListeners() {
1129                 setupBrowserWindowListeners(browserWindow, true);
1130         }
1131
1132         private void setupBrowserWindowListeners(BrowserWindow browser, boolean master) {
1133                 browser.fileWatcher.fileChanged.connect(this, "externalFileEdited(String)");
1134                 browser.noteSignal.tagsChanged.connect(this, "updateNoteTags(String, List)");
1135             browser.noteSignal.tagsChanged.connect(this, "updateListTags(String, List)");
1136             if (master) browser.noteSignal.noteChanged.connect(this, "setNoteDirty()");
1137             browser.noteSignal.titleChanged.connect(listManager, "updateNoteTitle(String, String)");
1138             browser.noteSignal.titleChanged.connect(this, "updateNoteTitle(String, String)");
1139             browser.noteSignal.notebookChanged.connect(this, "updateNoteNotebook(String, String)");
1140             browser.noteSignal.createdDateChanged.connect(listManager, "updateNoteCreatedDate(String, QDateTime)");
1141             browser.noteSignal.alteredDateChanged.connect(listManager, "updateNoteAlteredDate(String, QDateTime)");
1142             browser.noteSignal.subjectDateChanged.connect(listManager, "updateNoteSubjectDate(String, QDateTime)");
1143             browser.noteSignal.authorChanged.connect(listManager, "updateNoteAuthor(String, String)");
1144             browser.noteSignal.geoChanged.connect(listManager, "updateNoteGeoTag(String, Double,Double,Double)");
1145             browser.noteSignal.geoChanged.connect(this, "setNoteDirty()");
1146             browser.noteSignal.sourceUrlChanged.connect(listManager, "updateNoteSourceUrl(String, String)");
1147             if (master) browser.focusLost.connect(this, "saveNote()");
1148             browser.resourceSignal.contentChanged.connect(this, "externalFileEdited(String)");
1149         }
1150
1151         //**************************************************
1152         //* Setup shortcuts
1153         //**************************************************
1154         private void setupShortcut(QShortcut action, String text) {
1155                 if (!Global.shortcutKeys.containsAction(text))
1156                         return;
1157                 action.setKey(new QKeySequence(Global.shortcutKeys.getShortcut(text)));
1158         }
1159         
1160         //***************************************************************
1161         //***************************************************************
1162         //* Settings and look & feel
1163         //***************************************************************
1164         //***************************************************************
1165         @SuppressWarnings("unused")
1166         private void settings() {
1167                 logger.log(logger.HIGH, "Entering NeverNote.settings");
1168                 saveNoteColumnPositions();
1169                 saveNoteIndexWidth();
1170                 showColumns();
1171         ConfigDialog settings = new ConfigDialog(this);
1172         String dateFormat = Global.getDateFormat();
1173         String timeFormat = Global.getTimeFormat();
1174         
1175                 indexTime = 1000*Global.getIndexThreadSleepInterval();  
1176                 indexTimer.start(indexTime);  // reset indexing timer
1177         
1178         settings.exec();
1179         indexRunner.indexAttachmentsLocally = Global.indexAttachmentsLocally();
1180         if (Global.showTrayIcon())
1181                 trayIcon.show();
1182         else
1183                 trayIcon.hide();
1184         showColumns();
1185         if (menuBar.showEditorBar.isChecked())
1186                 showEditorButtons(browserWindow);
1187         
1188         // Reset the save timer
1189         if (Global.getAutoSaveInterval() > 0)
1190                         saveTimer.setInterval(1000*60*Global.getAutoSaveInterval());
1191         else
1192                 saveTimer.stop();
1193         
1194         // This is a hack to force a reload of the index in case the date or time changed.
1195 //        if (!dateFormat.equals(Global.getDateFormat()) ||
1196 //                      !timeFormat.equals(Global.getTimeFormat())) {
1197                 noteCache.clear();
1198                 readOnlyCache.clear();
1199                 inkNoteCache.clear();
1200                 noteIndexUpdated(true);
1201 //        }
1202         
1203         logger.log(logger.HIGH, "Leaving NeverNote.settings");
1204         }
1205         // Restore things to the way they were
1206         private void restoreWindowState(boolean mainWindow) {
1207                 // We need to name things or this doesn't work.
1208                 setObjectName("NeverNote");
1209         restoreState(Global.restoreState(objectName()));
1210                 mainLeftRightSplitter.setObjectName("mainLeftRightSplitter");
1211                 browserIndexSplitter.setObjectName("browserIndexSplitter");
1212                 leftSplitter1.setObjectName("leftSplitter1");   
1213                 
1214                 // Restore the actual positions.
1215                 if (mainWindow)
1216                         restoreGeometry(Global.restoreGeometry(objectName()));
1217         mainLeftRightSplitter.restoreState(Global.restoreState(mainLeftRightSplitter.objectName()));
1218         browserIndexSplitter.restoreState(Global.restoreState(browserIndexSplitter.objectName()));
1219         leftSplitter1.restoreState(Global.restoreState(leftSplitter1.objectName()));
1220        
1221         }
1222         // Save window positions for the next start
1223         private void saveWindowState() {
1224                 Global.saveGeometry(objectName(), saveGeometry());
1225                 Global.saveState(mainLeftRightSplitter.objectName(), mainLeftRightSplitter.saveState());
1226                 Global.saveState(browserIndexSplitter.objectName(), browserIndexSplitter.saveState());
1227                 Global.saveState(leftSplitter1.objectName(), leftSplitter1.saveState());
1228                 Global.saveState(objectName(), saveState());
1229         }    
1230         // Load the style sheet
1231         private void loadStyleSheet() {
1232                 String styleSheetName = "default.qss";
1233                 if (Global.getStyle().equalsIgnoreCase("cleanlooks"))
1234                                 styleSheetName = "default-cleanlooks.qss";
1235                 String fileName = Global.getFileManager().getQssDirPathUser("default.qss");
1236                 QFile file = new QFile(fileName);
1237                 
1238                 // If a user default.qss doesn't exist, we use the one shipped with NeverNote
1239                 if (!file.exists()) {
1240                         fileName = Global.getFileManager().getQssDirPath(styleSheetName);
1241                         file = new QFile(fileName);
1242                 }
1243                 file.open(OpenModeFlag.ReadOnly);
1244                 String styleSheet = file.readAll().toString();
1245                 file.close();
1246                 setStyleSheet(styleSheet);
1247         }
1248         // Save column positions for the next time
1249         private void saveNoteColumnPositions() {
1250                 int position = noteTableView.header.visualIndex(Global.noteTableCreationPosition);
1251                 Global.setColumnPosition("noteTableCreationPosition", position);
1252                 position = noteTableView.header.visualIndex(Global.noteTableTagPosition);
1253                 Global.setColumnPosition("noteTableTagPosition", position);
1254                 position = noteTableView.header.visualIndex(Global.noteTableNotebookPosition);
1255                 Global.setColumnPosition("noteTableNotebookPosition", position);
1256                 position = noteTableView.header.visualIndex(Global.noteTableChangedPosition);
1257                 Global.setColumnPosition("noteTableChangedPosition", position);
1258                 position = noteTableView.header.visualIndex(Global.noteTableAuthorPosition);
1259                 Global.setColumnPosition("noteTableAuthorPosition", position);
1260                 position = noteTableView.header.visualIndex(Global.noteTableSourceUrlPosition);
1261                 Global.setColumnPosition("noteTableSourceUrlPosition", position);
1262                 position = noteTableView.header.visualIndex(Global.noteTableSubjectDatePosition);
1263                 Global.setColumnPosition("noteTableSubjectDatePosition", position);
1264                 position = noteTableView.header.visualIndex(Global.noteTableTitlePosition);
1265                 Global.setColumnPosition("noteTableTitlePosition", position);
1266                 position = noteTableView.header.visualIndex(Global.noteTableSynchronizedPosition);
1267                 Global.setColumnPosition("noteTableSynchronizedPosition", position);
1268                 position = noteTableView.header.visualIndex(Global.noteTableGuidPosition);
1269                 Global.setColumnPosition("noteTableGuidPosition", position);
1270                 position = noteTableView.header.visualIndex(Global.noteTableThumbnailPosition);
1271                 Global.setColumnPosition("noteTableThumbnailPosition", position);
1272
1273         }
1274         // Save column widths for the next time
1275         private void saveNoteIndexWidth() {
1276                 int width;
1277         width = noteTableView.getColumnWidth(Global.noteTableCreationPosition);
1278         Global.setColumnWidth("noteTableCreationPosition", width);
1279                 width = noteTableView.getColumnWidth(Global.noteTableChangedPosition);
1280                 Global.setColumnWidth("noteTableChangedPosition", width);
1281                 width = noteTableView.getColumnWidth(Global.noteTableGuidPosition);
1282                 Global.setColumnWidth("noteTableGuidPosition", width);
1283                 width = noteTableView.getColumnWidth(Global.noteTableNotebookPosition);
1284                 Global.setColumnWidth("noteTableNotebookPosition", width);
1285                 width = noteTableView.getColumnWidth(Global.noteTableTagPosition);
1286                 Global.setColumnWidth("noteTableTagPosition", width);
1287                 width = noteTableView.getColumnWidth(Global.noteTableTitlePosition);
1288                 Global.setColumnWidth("noteTableTitlePosition", width);
1289                 width = noteTableView.getColumnWidth(Global.noteTableSourceUrlPosition);
1290                 Global.setColumnWidth("noteTableSourceUrlPosition", width);
1291                 width = noteTableView.getColumnWidth(Global.noteTableAuthorPosition);
1292                 Global.setColumnWidth("noteTableAuthorPosition", width);
1293                 width = noteTableView.getColumnWidth(Global.noteTableSubjectDatePosition);
1294                 Global.setColumnWidth("noteTableSubjectDatePosition", width);
1295                 width = noteTableView.getColumnWidth(Global.noteTableSynchronizedPosition);
1296                 Global.setColumnWidth("noteTableSynchronizedPosition", width);
1297                 width = noteTableView.getColumnWidth(Global.noteTableThumbnailPosition);
1298                 Global.setColumnWidth("noteTableThumbnailPosition", width);
1299                 width = noteTableView.getColumnWidth(Global.noteTableGuidPosition);
1300                 Global.setColumnWidth("noteTableGuidPosition", width);
1301         }
1302         
1303         @SuppressWarnings("unused")
1304         private void toggleSearchWindow() {
1305                 logger.log(logger.HIGH, "Entering NeverNote.toggleSearchWindow");
1306         searchLayout.toggleSearchField();
1307         menuBar.hideSearch.setChecked(searchField.isVisible());
1308         Global.saveWindowVisible("searchField", searchField.isVisible());
1309         logger.log(logger.HIGH, "Leaving NeverNote.toggleSearchWindow");
1310     }   
1311         @SuppressWarnings("unused")
1312         private void toggleQuotaWindow() {
1313                 logger.log(logger.HIGH, "Entering NeverNote.toggleQuotaWindow");
1314         searchLayout.toggleQuotaBar();
1315         menuBar.hideQuota.setChecked(quotaBar.isVisible());
1316         Global.saveWindowVisible("quota", quotaBar.isVisible());
1317         logger.log(logger.HIGH, "Leaving NeverNote.toggleQuotaWindow");
1318     }   
1319         @SuppressWarnings("unused")
1320         private void toggleZoomWindow() {
1321                 logger.log(logger.HIGH, "Entering NeverNote.toggleZoomWindow");
1322         searchLayout.toggleZoom();
1323         menuBar.hideZoom.setChecked(zoomSpinner.isVisible());
1324         Global.saveWindowVisible("zoom", zoomSpinner.isVisible());
1325         logger.log(logger.HIGH, "Leaving NeverNote.toggleZoomWindow");
1326     }   
1327         
1328         
1329         
1330     //***************************************************************
1331     //***************************************************************
1332     //** These functions deal with Notebook menu items
1333     //***************************************************************
1334     //***************************************************************
1335     // Setup the tree containing the user's notebooks.
1336     private void initializeNotebookTree() {       
1337         logger.log(logger.HIGH, "Entering NeverNote.initializeNotebookTree");
1338 //      notebookTree.itemClicked.connect(this, "notebookTreeSelection()");
1339         notebookTree.selectionSignal.connect(this, "notebookTreeSelection()");
1340         listManager.notebookSignal.refreshNotebookTreeCounts.connect(notebookTree, "updateCounts(List, List)");
1341         logger.log(logger.HIGH, "Leaving NeverNote.initializeNotebookTree");
1342     }   
1343     // Listener when a notebook is selected
1344         private void notebookTreeSelection() {
1345                 logger.log(logger.HIGH, "Entering NeverNote.notebookTreeSelection");
1346
1347                 clearTrashFilter();
1348                 clearAttributeFilter();
1349                 clearSavedSearchFilter();
1350                 if (Global.mimicEvernoteInterface) {
1351                         clearTagFilter();
1352                         searchField.clear();
1353                 }
1354                 menuBar.noteRestoreAction.setVisible(false);            
1355         menuBar.notebookEditAction.setEnabled(true);
1356         menuBar.notebookDeleteAction.setEnabled(true);
1357         menuBar.notebookPublishAction.setEnabled(true);
1358         menuBar.notebookShareAction.setEnabled(true);
1359         menuBar.notebookIconAction.setEnabled(true);
1360         menuBar.notebookStackAction.setEnabled(true);
1361         List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1362         selectedNotebookGUIDs.clear();
1363                 String guid = "";
1364                 String stackName = "";
1365                 if (selections.size() > 0) {
1366                 guid = (selections.get(0).text(2));
1367                 stackName = selections.get(0).text(0);
1368         }
1369                 if (!Global.mimicEvernoteInterface) {
1370                         // If no notebooks are selected, we make it look like the "all notebooks" one was selected
1371                         if (selections.size()==0) {
1372                                 selectedNotebookGUIDs.clear();
1373                                 for (int i=0; i < listManager.getNotebookIndex().size(); i++) {
1374                                         selectedNotebookGUIDs.add(listManager.getNotebookIndex().get(i).getGuid());
1375                                 }
1376                                 menuBar.notebookEditAction.setEnabled(false);
1377                                 menuBar.notebookDeleteAction.setEnabled(false);
1378                                 menuBar.notebookStackAction.setEnabled(false);
1379                                 menuBar.notebookIconAction.setEnabled(false);
1380                         }
1381                 }
1382         if (!guid.equals("") && !guid.equals("STACK")) {
1383                 selectedNotebookGUIDs.add(guid);
1384                 menuBar.notebookIconAction.setEnabled(true);
1385         } else {
1386                 menuBar.notebookIconAction.setEnabled(true);
1387                         for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
1388                                 Notebook book = listManager.getNotebookIndex().get(j);
1389                                 if (book.getStack() != null && book.getStack().equalsIgnoreCase(stackName))
1390                                         selectedNotebookGUIDs.add(book.getGuid());
1391                         }
1392         }
1393         listManager.setSelectedNotebooks(selectedNotebookGUIDs);
1394         listManager.loadNotesIndex();
1395         noteIndexUpdated(false);
1396         refreshEvernoteNote(true);
1397         listManager.refreshCounters = true;
1398         listManager.refreshCounters();
1399                 logger.log(logger.HIGH, "Leaving NeverNote.notebookTreeSelection");
1400
1401     }
1402     private void clearNotebookFilter() {
1403         notebookTree.blockSignals(true);
1404         notebookTree.clearSelection();
1405                 menuBar.noteRestoreAction.setVisible(false);
1406         menuBar.notebookEditAction.setEnabled(false);
1407         menuBar.notebookDeleteAction.setEnabled(false);
1408         selectedNotebookGUIDs.clear();
1409         listManager.setSelectedNotebooks(selectedNotebookGUIDs);
1410         notebookTree.blockSignals(false);
1411     }
1412         // Triggered when the notebook DB has been updated
1413         private void notebookIndexUpdated() {
1414                 logger.log(logger.HIGH, "Entering NeverNote.notebookIndexUpdated");
1415         
1416                 // Get the possible icons
1417                 HashMap<String, QIcon> icons = conn.getNotebookTable().getAllIcons();
1418         notebookTree.setIcons(icons);
1419         
1420         if (selectedNotebookGUIDs == null)
1421                         selectedNotebookGUIDs = new ArrayList<String>();
1422                 List<Notebook> books = conn.getNotebookTable().getAll();
1423                 for (int i=books.size()-1; i>=0; i--) {
1424                         for (int j=0; j<listManager.getArchiveNotebookIndex().size(); j++) {
1425                                 if (listManager.getArchiveNotebookIndex().get(j).getGuid().equals(books.get(i).getGuid())) {
1426                                         books.remove(i);
1427                                         j=listManager.getArchiveNotebookIndex().size();
1428                                 }
1429                         }
1430                 }
1431                 
1432                 
1433                 listManager.countNotebookResults(listManager.getNoteIndex());
1434                 notebookTree.blockSignals(true);
1435         notebookTree.load(books, listManager.getLocalNotebooks());
1436         for (int i=selectedNotebookGUIDs.size()-1; i>=0; i--) {
1437                 boolean found = notebookTree.selectGuid(selectedNotebookGUIDs.get(i));
1438                 if (!found)
1439                         selectedNotebookGUIDs.remove(i);
1440         }
1441         listManager.refreshCounters = true;
1442         listManager.refreshCounters();
1443         notebookTree.blockSignals(false);
1444         
1445                 logger.log(logger.HIGH, "Leaving NeverNote.notebookIndexUpdated");
1446     }
1447     // Show/Hide note information
1448         @SuppressWarnings("unused")
1449         private void toggleNotebookWindow() {
1450                 logger.log(logger.HIGH, "Entering NeverNote.toggleNotebookWindow");
1451                 searchLayout.toggleNotebook();
1452         menuBar.hideNotebooks.setChecked(notebookTree.isVisible());
1453         Global.saveWindowVisible("notebookTree", notebookTree.isVisible());
1454         logger.log(logger.HIGH, "Leaving NeverNote.toggleNotebookWindow");
1455     }   
1456         // Add a new notebook
1457         @SuppressWarnings("unused")
1458         private void addNotebook() {
1459                 logger.log(logger.HIGH, "Inside NeverNote.addNotebook");
1460                 NotebookEdit edit = new NotebookEdit();
1461                 edit.setNotebooks(listManager.getNotebookIndex());
1462                 edit.exec();
1463         
1464                 if (!edit.okPressed())
1465                         return;
1466         
1467                 Calendar currentTime = new GregorianCalendar();
1468                 Long l = new Long(currentTime.getTimeInMillis());
1469                 String randint = new String(Long.toString(l));
1470         
1471                 Notebook newBook = new Notebook();
1472                 newBook.setUpdateSequenceNum(0);
1473                 newBook.setGuid(randint);
1474                 newBook.setName(edit.getNotebook());
1475                 newBook.setServiceCreated(new Date().getTime());
1476                 newBook.setServiceUpdated(new Date().getTime());
1477                 newBook.setDefaultNotebook(false);
1478                 newBook.setPublished(false);
1479                 
1480                 listManager.getNotebookIndex().add(newBook);
1481                 if (edit.isLocal())
1482                         listManager.getLocalNotebooks().add(newBook.getGuid());
1483                 conn.getNotebookTable().addNotebook(newBook, true, edit.isLocal());
1484                 notebookIndexUpdated();
1485                 listManager.countNotebookResults(listManager.getNoteIndex());
1486 //              notebookTree.updateCounts(listManager.getNotebookIndex(), listManager.getNotebookCounter());
1487                 logger.log(logger.HIGH, "Leaving NeverNote.addNotebook");
1488         }
1489         // Edit an existing notebook
1490         @SuppressWarnings("unused")
1491         private void stackNotebook() {
1492                 logger.log(logger.HIGH, "Entering NeverNote.stackNotebook");
1493                 StackNotebook edit = new StackNotebook();
1494                 
1495                 List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1496                 QTreeWidgetItem currentSelection;
1497                 for (int i=0; i<selections.size(); i++) {
1498                         currentSelection = selections.get(0);
1499                         String guid = currentSelection.text(2);
1500                         if (guid.equalsIgnoreCase("")) {
1501                                  QMessageBox.critical(this, tr("Unable To Stack") ,tr("You can't stack the \"All Notebooks\" item."));
1502                                  return;
1503                         }
1504                         if (guid.equalsIgnoreCase("STACK")) {
1505                                  QMessageBox.critical(this, tr("Unable To Stack") ,tr("You can't stack a stack."));
1506                                  return;
1507                         }
1508                 }
1509
1510                 edit.setStackNames(conn.getNotebookTable().getAllStackNames());
1511
1512                 
1513                 edit.exec();
1514         
1515                 if (!edit.okPressed())
1516                         return;
1517         
1518                 String stack = edit.getStackName();
1519                 
1520                 for (int i=0; i<selections.size(); i++) {
1521                         currentSelection = selections.get(i);
1522                         String guid = currentSelection.text(2);
1523                         listManager.updateNotebookStack(guid, stack);
1524                 }
1525                 notebookIndexUpdated();
1526                 logger.log(logger.HIGH, "Leaving NeverNote.stackNotebook");
1527         }
1528         // Edit an existing notebook
1529         @SuppressWarnings("unused")
1530         private void editNotebook() {
1531                 logger.log(logger.HIGH, "Entering NeverNote.editNotebook");
1532                 NotebookEdit edit = new NotebookEdit();
1533                 
1534                 List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1535                 QTreeWidgetItem currentSelection;
1536                 currentSelection = selections.get(0);
1537                 edit.setNotebook(currentSelection.text(0));
1538                 
1539                 String guid = currentSelection.text(2);
1540                 if (!guid.equalsIgnoreCase("STACK")) {
1541                         edit.setTitle(tr("Edit Notebook"));
1542                         edit.setNotebooks(listManager.getNotebookIndex());
1543                         edit.setLocalCheckboxEnabled(false);
1544                         for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1545                                 if (listManager.getNotebookIndex().get(i).getGuid().equals(guid)) {
1546                                         edit.setDefaultNotebook(listManager.getNotebookIndex().get(i).isDefaultNotebook());
1547                                         i=listManager.getNotebookIndex().size();
1548                                 }
1549                         }
1550                 } else {
1551                         edit.setTitle(tr("Edit Stack"));
1552                         edit.setStacks(conn.getNotebookTable().getAllStackNames());
1553                         edit.hideLocalCheckbox();
1554                         edit.hideDefaultCheckbox();
1555                 }
1556                 
1557                 edit.exec();
1558         
1559                 if (!edit.okPressed())
1560                         return;
1561         
1562                 
1563                 if (guid.equalsIgnoreCase("STACK")) {
1564                         conn.getNotebookTable().renameStacks(currentSelection.text(0), edit.getNotebook());
1565                         for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
1566                                 if (listManager.getNotebookIndex().get(j).getStack() != null && 
1567                                         listManager.getNotebookIndex().get(j).getStack().equalsIgnoreCase(currentSelection.text(0)))
1568                                                 listManager.getNotebookIndex().get(j).setStack(edit.getNotebook());
1569                         }
1570                         conn.getNotebookTable().renameStacks(currentSelection.text(0), edit.getNotebook());
1571                         currentSelection.setText(0, edit.getNotebook());
1572                         return;
1573                 }
1574                 
1575                 updateListNotebookName(currentSelection.text(0), edit.getNotebook());
1576                 currentSelection.setText(0, edit.getNotebook());
1577                 
1578                 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1579                         if (listManager.getNotebookIndex().get(i).getGuid().equals(guid)) {
1580                                 listManager.getNotebookIndex().get(i).setName(edit.getNotebook());
1581                                 if (!listManager.getNotebookIndex().get(i).isDefaultNotebook() && edit.isDefaultNotebook()) {
1582                                         for (int j=0; j<listManager.getNotebookIndex().size(); j++)
1583                                                 listManager.getNotebookIndex().get(j).setDefaultNotebook(false);
1584                                         listManager.getNotebookIndex().get(i).setDefaultNotebook(true);
1585                                         conn.getNotebookTable().setDefaultNotebook(listManager.getNotebookIndex().get(i).getGuid());
1586                                 }
1587                                 conn.getNotebookTable().updateNotebook(listManager.getNotebookIndex().get(i), true);
1588                                 if (conn.getNotebookTable().isLinked(listManager.getNotebookIndex().get(i).getGuid())) {
1589                                         LinkedNotebook linkedNotebook = conn.getLinkedNotebookTable().getByNotebookGuid(listManager.getNotebookIndex().get(i).getGuid());
1590                                         linkedNotebook.setShareName(edit.getNotebook());
1591                                         conn.getLinkedNotebookTable().updateNotebook(linkedNotebook, true);
1592                                 }
1593                                 i=listManager.getNotebookIndex().size();
1594                         }
1595                 }
1596                 
1597                 // Build a list of non-closed notebooks
1598                 List<Notebook> nbooks = new ArrayList<Notebook>();
1599                 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1600                         boolean found=false;
1601                         for (int j=0; j<listManager.getArchiveNotebookIndex().size(); j++) {
1602                                 if (listManager.getArchiveNotebookIndex().get(j).getGuid().equals(listManager.getNotebookIndex().get(i).getGuid()))
1603                                         found = true;
1604                         }
1605                         if (!found)
1606                                 nbooks.add(listManager.getNotebookIndex().get(i));
1607                 }
1608                 
1609                 
1610                 FilterEditorNotebooks notebookFilter = new FilterEditorNotebooks(conn, logger);
1611                 List<Notebook> filteredBooks = notebookFilter.getValidNotebooks(currentNote, listManager.getNotebookIndex());
1612                 browserWindow.setNotebookList(filteredBooks);
1613                 Iterator<String> set = externalWindows.keySet().iterator();
1614                 while(set.hasNext())
1615                         externalWindows.get(set.next()).getBrowserWindow().setNotebookList(filteredBooks);
1616                 logger.log(logger.HIGH, "Leaving NeverNote.editNotebook");
1617         }
1618         // Publish a notebook
1619         @SuppressWarnings("unused")
1620         private void publishNotebook() {
1621                 List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1622                 QTreeWidgetItem currentSelection;
1623                 currentSelection = selections.get(0);
1624                 String guid = currentSelection.text(2);
1625
1626                 if (guid.equalsIgnoreCase("STACK") || guid.equalsIgnoreCase(""))
1627                         return;
1628                 
1629                 Notebook n = null;
1630                 int position = 0;
1631                 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1632                         if (guid.equals(listManager.getNotebookIndex().get(i).getGuid())) {
1633                                 n = listManager.getNotebookIndex().get(i);
1634                                 position = i;
1635                                 i = listManager.getNotebookIndex().size();
1636                         }
1637                 }
1638                 if (n == null)
1639                         return;
1640                 
1641                 PublishNotebook publish = new PublishNotebook(Global.username, Global.getServer(), n);
1642                 publish.exec();
1643                 
1644                 if (!publish.okClicked()) 
1645                         return;
1646                 
1647                 Publishing p = publish.getPublishing();
1648                 boolean isPublished = !publish.isStopPressed();
1649                 conn.getNotebookTable().setPublishing(n.getGuid(), isPublished, p);
1650                 n.setPublished(isPublished);
1651                 n.setPublishing(p);
1652                 listManager.getNotebookIndex().set(position, n);
1653                 notebookIndexUpdated();
1654         }
1655         // Publish a notebook
1656         @SuppressWarnings("unused")
1657         private void shareNotebook() {
1658                 List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1659                 QTreeWidgetItem currentSelection;
1660                 currentSelection = selections.get(0);
1661                 String guid = currentSelection.text(2);
1662
1663                 if (guid.equalsIgnoreCase("STACK") || guid.equalsIgnoreCase(""))
1664                         return;
1665                 
1666                 Notebook n = null;;
1667                 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1668                         if (guid.equals(listManager.getNotebookIndex().get(i).getGuid())) {
1669                                 n = listManager.getNotebookIndex().get(i);
1670                                 i = listManager.getNotebookIndex().size();
1671                         }
1672                 }
1673                                 
1674                 String authToken = null;
1675                 if (syncRunner.isConnected)
1676                         authToken = syncRunner.authToken;
1677                 ShareNotebook share = new ShareNotebook(n.getName(), conn, n, syncRunner);
1678                 share.exec();
1679                 
1680         }
1681
1682         // Delete an existing notebook
1683         @SuppressWarnings("unused")
1684         private void deleteNotebook() {
1685                 logger.log(logger.HIGH, "Entering NeverNote.deleteNotebook");
1686                 boolean stacksFound = false;
1687                 boolean notebooksFound = false;
1688                 boolean assigned = false;
1689                 // Check if any notes have this notebook
1690                 List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1691         for (int i=0; i<selections.size(); i++) {
1692                 QTreeWidgetItem currentSelection;
1693                 currentSelection = selections.get(i);
1694                 String guid = currentSelection.text(2);
1695                 if (!guid.equalsIgnoreCase("STACK")) {
1696                         notebooksFound = true;
1697                         for (int j=0; j<listManager.getNoteIndex().size(); j++) {
1698                                 String noteGuid = listManager.getNoteIndex().get(j).getNotebookGuid();
1699                                 if (noteGuid.equals(guid)) {
1700                                         assigned = true;
1701                                         j=listManager.getNoteIndex().size();
1702                                         i=selections.size();
1703                                 }
1704                         }
1705                 } else {
1706                         stacksFound = true;
1707                 }
1708         }
1709                 if (assigned) {
1710                         QMessageBox.information(this, tr("Unable to Delete"), tr("Some of the selected notebook(s) contain notes.\n"+
1711                                         "Please delete the notes or move them to another notebook before deleting any notebooks."));
1712                         return;
1713                 }
1714                 
1715                 if (conn.getNotebookTable().getAll().size() == 1) {
1716                         QMessageBox.information(this, tr("Unable to Delete"), tr("You must have at least one notebook."));
1717                         return;
1718                 }
1719         
1720         // If all notebooks are clear, verify the delete
1721                 String msg1 = new String(tr("Delete selected notebooks?"));
1722                 String msg2 = new String(tr("Remove selected stacks (notebooks will not be deleted)?"));
1723                 String msg3 = new String(tr("Delete selected notebooks & remove stacks? Notebooks under the stacks are" +
1724                                 " not deleted unless selected?"));
1725                 String msg = "";
1726                 if (stacksFound && notebooksFound)
1727                         msg = msg3;
1728                 if (!stacksFound && notebooksFound)
1729                         msg = msg1;
1730                 if (stacksFound && !notebooksFound)
1731                         msg = msg2;
1732                 if (QMessageBox.question(this, tr("Confirmation"), msg,
1733                         QMessageBox.StandardButton.Yes, 
1734                         QMessageBox.StandardButton.No)==StandardButton.No.value()) {
1735                         return;
1736                 }
1737                 
1738                 // If confirmed, delete the notebook
1739         for (int i=selections.size()-1; i>=0; i--) {
1740                 QTreeWidgetItem currentSelection;
1741                 currentSelection = selections.get(i);
1742                 String guid = currentSelection.text(2);
1743                 if (currentSelection.text(2).equalsIgnoreCase("STACK")) {
1744                         conn.getNotebookTable().renameStacks(currentSelection.text(0), "");
1745                         listManager.renameStack(currentSelection.text(0), "");
1746                 } else {
1747                         conn.getNotebookTable().expungeNotebook(guid, true);
1748                         listManager.deleteNotebook(guid);
1749                 }
1750         }
1751
1752                 notebookIndexUpdated();
1753 //        notebookTreeSelection();
1754 //        notebookTree.load(listManager.getNotebookIndex(), listManager.getLocalNotebooks());
1755 //        listManager.countNotebookResults(listManager.getNoteIndex());
1756         logger.log(logger.HIGH, "Entering NeverNote.deleteNotebook");
1757         }
1758         // A note's notebook has been updated
1759         @SuppressWarnings("unused")
1760         private void updateNoteNotebook(String guid, String notebookGuid) {
1761                 
1762                 // Update the list manager
1763                 listManager.updateNoteNotebook(guid, notebookGuid);
1764                 listManager.countNotebookResults(listManager.getNoteIndex());
1765 //              notebookTree.updateCounts(listManager.getNotebookIndex(), listManager.getNotebookCounter());    
1766                 
1767                 // Find the name of the notebook
1768                 String notebookName = null;
1769                 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1770                         if (listManager.getNotebookIndex().get(i).getGuid().equals(notebookGuid)) {
1771                                 notebookName = listManager.getNotebookIndex().get(i).getName();
1772                                 break;
1773                         }
1774                 }
1775                 
1776                 // If we found the name, update the browser window
1777                 if (notebookName != null) {
1778                         updateListNoteNotebook(guid, notebookName);
1779                         if (guid.equals(currentNoteGuid)) {
1780                                 int pos =  browserWindow.notebookBox.findText(notebookName);
1781                                 if (pos >=0)
1782                                         browserWindow.notebookBox.setCurrentIndex(pos);
1783                         }
1784                 }
1785                 
1786                 // If we're dealing with the current note, then we need to be sure and update the notebook there
1787                 if (guid.equals(currentNoteGuid)) {
1788                         if (currentNote != null) {
1789                                 currentNote.setNotebookGuid(notebookGuid);
1790                         }
1791                 }
1792         }
1793         // Open/close notebooks
1794         @SuppressWarnings("unused")
1795         private void closeNotebooks() {
1796                 NotebookArchive na = new NotebookArchive(listManager.getNotebookIndex(), listManager.getArchiveNotebookIndex());
1797                 na.exec();
1798                 if (!na.okClicked())
1799                         return;
1800                 
1801                 waitCursor(true);
1802                 listManager.getArchiveNotebookIndex().clear();
1803                 
1804                 for (int i=na.getClosedBookList().count()-1; i>=0; i--) {
1805                         String text = na.getClosedBookList().takeItem(i).text();
1806                         for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
1807                                 if (listManager.getNotebookIndex().get(j).getName().equalsIgnoreCase(text)) {
1808                                         Notebook n = listManager.getNotebookIndex().get(j);
1809                                         conn.getNotebookTable().setArchived(n.getGuid(),true);
1810                                         listManager.getArchiveNotebookIndex().add(n);
1811                                         j=listManager.getNotebookIndex().size();
1812                                 }
1813                         }
1814                 }
1815                 
1816                 for (int i=na.getOpenBookList().count()-1; i>=0; i--) {
1817                         String text = na.getOpenBookList().takeItem(i).text();
1818                         for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
1819                                 if (listManager.getNotebookIndex().get(j).getName().equalsIgnoreCase(text)) {
1820                                         Notebook n = listManager.getNotebookIndex().get(j);
1821                                         conn.getNotebookTable().setArchived(n.getGuid(),false);
1822                                         j=listManager.getNotebookIndex().size();
1823                                 }
1824                         }
1825                 }
1826                 notebookTreeSelection();
1827                 listManager.loadNotesIndex();
1828                 notebookIndexUpdated();
1829                 noteIndexUpdated(false);
1830                 reloadTagTree(true);
1831 //              noteIndexUpdated(false);
1832                 
1833                 // Build a list of non-closed notebooks
1834                 List<Notebook> nbooks = new ArrayList<Notebook>();
1835                 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
1836                         boolean found=false;
1837                         for (int j=0; j<listManager.getArchiveNotebookIndex().size(); j++) {
1838                                 if (listManager.getArchiveNotebookIndex().get(j).getGuid().equals(listManager.getNotebookIndex().get(i).getGuid()))
1839                                         found = true;
1840                         }
1841                         if (!found)
1842                                 nbooks.add(listManager.getNotebookIndex().get(i));
1843                 }
1844                 
1845                 FilterEditorNotebooks notebookFilter = new FilterEditorNotebooks(conn, logger);
1846                 List<Notebook> filteredBooks = notebookFilter.getValidNotebooks(currentNote, listManager.getNotebookIndex());
1847                 browserWindow.setNotebookList(filteredBooks);
1848                 
1849                 // Update any external windows
1850                 Iterator<String> set = externalWindows.keySet().iterator();
1851                 while(set.hasNext())
1852                         externalWindows.get(set.next()).getBrowserWindow().setNotebookList(filteredBooks);
1853                 
1854                 waitCursor(false);
1855         }
1856         // Change the notebook's icon
1857         @SuppressWarnings("unused")
1858         private void setNotebookIcon() {
1859                 boolean stackSelected = false;
1860                 boolean allNotebookSelected = false;
1861                 
1862                 QTreeWidgetItem currentSelection;
1863                 List<QTreeWidgetItem> selections = notebookTree.selectedItems();
1864                 if (selections.size() == 0)
1865                         return;
1866                 
1867                 currentSelection = selections.get(0);   
1868                 String guid = currentSelection.text(2);
1869                 if (guid.equalsIgnoreCase(""))
1870                         allNotebookSelected = true;
1871                 if (guid.equalsIgnoreCase("STACK"))
1872                         stackSelected = true;
1873
1874                 QIcon currentIcon = currentSelection.icon(0);
1875                 QIcon icon;
1876                 SetIcon dialog;
1877                 
1878                 if (!stackSelected && !allNotebookSelected) {
1879                         icon = conn.getNotebookTable().getIcon(guid);
1880                         if (icon == null) {
1881                                 dialog = new SetIcon(currentIcon, saveLastPath);
1882                                 dialog.setUseDefaultIcon(true);
1883                         } else {
1884                                 dialog = new SetIcon(icon, saveLastPath);
1885                                 dialog.setUseDefaultIcon(false);
1886                         }
1887                 } else {
1888                         if (stackSelected) {
1889                                 icon = conn.getSystemIconTable().getIcon(currentSelection.text(0), "STACK");
1890                         } else {
1891                                 icon = conn.getSystemIconTable().getIcon(currentSelection.text(0), "ALLNOTEBOOK");                              
1892                         }
1893                         if (icon == null) {
1894                                 dialog = new SetIcon(currentIcon, saveLastPath);
1895                                 dialog.setUseDefaultIcon(true);
1896                         } else {
1897                                 dialog = new SetIcon(icon, saveLastPath);
1898                                 dialog.setUseDefaultIcon(false);
1899                         }
1900                 }
1901                 dialog.exec();
1902                 if (dialog.okPressed()) {
1903                 saveLastPath = dialog.getPath();
1904
1905                         QIcon newIcon = dialog.getIcon();
1906                         if (stackSelected) {
1907                                 conn.getSystemIconTable().setIcon(currentSelection.text(0), "STACK", newIcon, dialog.getFileType());
1908                                 if (newIcon == null) {
1909                                         newIcon = new QIcon(iconPath+"books2.png");
1910                                 }
1911                                 currentSelection.setIcon(0,newIcon);
1912                                 return;
1913                         }
1914                         if (allNotebookSelected) {
1915                                 conn.getSystemIconTable().setIcon(currentSelection.text(0), "ALLNOTEBOOK", newIcon, dialog.getFileType());
1916                                 if (newIcon == null) {
1917                                         newIcon = new QIcon(iconPath+"notebook-green.png");
1918                                 }
1919                                 currentSelection.setIcon(0,newIcon);
1920                                 return;
1921                         }
1922                         conn.getNotebookTable().setIcon(guid, newIcon, dialog.getFileType());
1923                         if (newIcon == null) {
1924                                 boolean isPublished = false;;
1925                                 boolean found = false;
1926                                 for (int i=0; i<listManager.getNotebookIndex().size() && !found; i++) {
1927                                         if (listManager.getNotebookIndex().get(i).getGuid().equals(guid)) {
1928                                                 isPublished = listManager.getNotebookIndex().get(i).isPublished();
1929                                                 found = true;
1930                                         }
1931                                 }
1932                                 newIcon = notebookTree.findDefaultIcon(guid, currentSelection.text(1), listManager.getLocalNotebooks(), isPublished);
1933                         }
1934                         currentSelection.setIcon(0, newIcon);
1935                 }
1936         
1937         }
1938         
1939         
1940     //***************************************************************
1941     //***************************************************************
1942     //** These functions deal with Tag menu items
1943     //***************************************************************
1944     //***************************************************************
1945         // Add a new notebook
1946         @SuppressWarnings("unused")
1947         private void addTag() {
1948                 logger.log(logger.HIGH, "Inside NeverNote.addTag");
1949                 TagEdit edit = new TagEdit();
1950                 edit.setTagList(listManager.getTagIndex());
1951
1952                 List<QTreeWidgetItem> selections = tagTree.selectedItems();
1953                 QTreeWidgetItem currentSelection = null;
1954                 if (selections.size() > 0) {
1955                         currentSelection = selections.get(0);
1956                         edit.setParentTag(currentSelection.text(0));
1957                 }
1958
1959                 edit.exec();
1960         
1961                 if (!edit.okPressed())
1962                         return;
1963         
1964                 Calendar currentTime = new GregorianCalendar();
1965                 Long l = new Long(currentTime.getTimeInMillis());
1966                 String randint = new String(Long.toString(l));
1967         
1968                 Tag newTag = new Tag();
1969                 newTag.setUpdateSequenceNum(0);
1970                 newTag.setGuid(randint);
1971                 newTag.setName(edit.getTag());
1972                 if (edit.getParentTag().isChecked()) {
1973                         newTag.setParentGuid(currentSelection.text(2));
1974                         newTag.setParentGuidIsSet(true);
1975                         currentSelection.setExpanded(true);
1976                 }
1977                 conn.getTagTable().addTag(newTag, true);
1978                 listManager.getTagIndex().add(newTag);
1979                 reloadTagTree(true);
1980                 
1981                 logger.log(logger.HIGH, "Leaving NeverNote.addTag");
1982         }
1983         @SuppressWarnings("unused")
1984         private void reloadTagTree() {
1985                 reloadTagTree(false);
1986         }
1987         private void reloadTagTree(boolean reload) {
1988                 logger.log(logger.HIGH, "Entering NeverNote.reloadTagTree");
1989                 tagIndexUpdated(reload);
1990                 boolean filter = false;
1991                 if (reload)
1992                         listManager.countTagResults(listManager.getNoteIndex());
1993                 if (notebookTree.selectedItems().size() > 0 
1994                                                   && !notebookTree.selectedItems().get(0).text(0).equalsIgnoreCase("All Notebooks"))
1995                                                   filter = true;
1996                 if (tagTree.selectedItems().size() > 0)
1997                         filter = true;
1998                 tagTree.showAllTags(!filter);
1999                 logger.log(logger.HIGH, "Leaving NeverNote.reloadTagTree");
2000         }
2001         // Edit an existing tag
2002         @SuppressWarnings("unused")
2003         private void editTag() {
2004                 logger.log(logger.HIGH, "Entering NeverNote.editTag");
2005                 TagEdit edit = new TagEdit();
2006                 edit.setTitle("Edit Tag");
2007                 List<QTreeWidgetItem> selections = tagTree.selectedItems();
2008                 QTreeWidgetItem currentSelection;
2009                 currentSelection = selections.get(0);
2010                 edit.setTag(currentSelection.text(0));
2011                 edit.setTagList(listManager.getTagIndex());
2012                 edit.exec();
2013         
2014                 if (!edit.okPressed())
2015                         return;
2016         
2017                 String guid = currentSelection.text(2);
2018                 currentSelection.setText(0,edit.getTag());
2019                 
2020                 for (int i=0; i<listManager.getTagIndex().size(); i++) {
2021                         if (listManager.getTagIndex().get(i).getGuid().equals(guid)) {
2022                                 listManager.getTagIndex().get(i).setName(edit.getTag());
2023                                 conn.getTagTable().updateTag(listManager.getTagIndex().get(i), true);
2024                                 updateListTagName(guid);
2025                                 if (currentNote != null && currentNote.getTagGuids().contains(guid))
2026                                         browserWindow.setTag(getTagNamesForNote(currentNote));
2027                                 logger.log(logger.HIGH, "Leaving NeverNote.editTag");
2028                                 return;
2029                         }
2030                 }
2031                 browserWindow.setTag(getTagNamesForNote(currentNote));
2032                 logger.log(logger.HIGH, "Leaving NeverNote.editTag...");
2033         }
2034         // Delete an existing tag
2035         @SuppressWarnings("unused")
2036         private void deleteTag() {
2037                 logger.log(logger.HIGH, "Entering NeverNote.deleteTag");
2038                 
2039                 if (QMessageBox.question(this, tr("Confirmation"), tr("Delete the selected tags?"),
2040                         QMessageBox.StandardButton.Yes, 
2041                         QMessageBox.StandardButton.No)==StandardButton.No.value()) {
2042                                                         return;
2043                 }
2044                 
2045                 List<QTreeWidgetItem> selections = tagTree.selectedItems();
2046         for (int i=selections.size()-1; i>=0; i--) {
2047                 QTreeWidgetItem currentSelection;
2048                 currentSelection = selections.get(i);                   
2049                 removeTagItem(currentSelection.text(2));
2050         }
2051         tagIndexUpdated(true);
2052         tagTreeSelection();
2053         listManager.countTagResults(listManager.getNoteIndex());
2054 //              tagTree.updateCounts(listManager.getTagCounter());
2055         logger.log(logger.HIGH, "Leaving NeverNote.deleteTag");
2056         }
2057         // Remove a tag tree item.  Go recursively down & remove the children too
2058         private void removeTagItem(String guid) {
2059         for (int j=listManager.getTagIndex().size()-1; j>=0; j--) {             
2060                 String parent = listManager.getTagIndex().get(j).getParentGuid();
2061                 if (parent != null && parent.equals(guid)) {            
2062                         //Remove this tag's children
2063                         removeTagItem(listManager.getTagIndex().get(j).getGuid());
2064                 }
2065         }
2066         //Now, remove this tag
2067         removeListTagName(guid);
2068         conn.getTagTable().expungeTag(guid, true);                      
2069         for (int a=0; a<listManager.getTagIndex().size(); a++) {
2070                 if (listManager.getTagIndex().get(a).getGuid().equals(guid)) {
2071                         listManager.getTagIndex().remove(a);
2072                         return;
2073                 }
2074         }
2075         }
2076         // Setup the tree containing the user's tags
2077     private void initializeTagTree() {
2078         logger.log(logger.HIGH, "Entering NeverNote.initializeTagTree");
2079 //      tagTree.itemSelectionChanged.connect(this, "tagTreeSelection()");
2080 //      tagTree.itemClicked.connect(this, "tagTreeSelection()");
2081         tagTree.selectionSignal.connect(this, "tagTreeSelection()");
2082         listManager.tagSignal.refreshTagTreeCounts.connect(tagTree, "updateCounts(List)");
2083         logger.log(logger.HIGH, "Leaving NeverNote.initializeTagTree");
2084     }
2085     // Listener when a tag is selected
2086         private void tagTreeSelection() {
2087         logger.log(logger.HIGH, "Entering NeverNote.tagTreeSelection");
2088                 
2089         clearTrashFilter();
2090         clearAttributeFilter();
2091         clearSavedSearchFilter();
2092         
2093                 menuBar.noteRestoreAction.setVisible(false);
2094                 
2095         List<QTreeWidgetItem> selections = tagTree.selectedItems();
2096         QTreeWidgetItem currentSelection;
2097         selectedTagGUIDs.clear();
2098         for (int i=0; i<selections.size(); i++) {
2099                 currentSelection = selections.get(i);
2100                 selectedTagGUIDs.add(currentSelection.text(2));
2101         }
2102         if (selections.size() > 0) {
2103                 menuBar.tagEditAction.setEnabled(true);
2104                 menuBar.tagDeleteAction.setEnabled(true);
2105                 menuBar.tagIconAction.setEnabled(true);
2106         }
2107         else {
2108                 menuBar.tagEditAction.setEnabled(false);
2109                 menuBar.tagDeleteAction.setEnabled(false);
2110                 menuBar.tagIconAction.setEnabled(true);
2111         }
2112         if (selections.size() > 1)
2113                 menuBar.tagMergeAction.setEnabled(true);
2114         else
2115                 menuBar.tagMergeAction.setEnabled(false);
2116         listManager.setSelectedTags(selectedTagGUIDs);
2117         listManager.loadNotesIndex();
2118         noteIndexUpdated(false);
2119         refreshEvernoteNote(true);
2120         listManager.refreshCounters = true;
2121         listManager.refreshCounters();
2122         logger.log(logger.HIGH, "Leaving NeverNote.tagTreeSelection");
2123     }
2124     // trigger the tag index to be refreshed
2125     @SuppressWarnings("unused")
2126         private void tagIndexUpdated() {
2127         tagIndexUpdated(true);
2128     }
2129     private void tagIndexUpdated(boolean reload) {
2130         logger.log(logger.HIGH, "Entering NeverNote.tagIndexUpdated");
2131                 if (selectedTagGUIDs == null)
2132                         selectedTagGUIDs = new ArrayList<String>();
2133                 if (reload)
2134                         listManager.reloadTagIndex();
2135
2136                 tagTree.blockSignals(true);
2137                 if (reload) {
2138                         tagTree.setIcons(conn.getTagTable().getAllIcons());
2139                         tagTree.load(listManager.getTagIndex());
2140                 }
2141
2142         for (int i=selectedTagGUIDs.size()-1; i>=0; i--) {
2143                 boolean found = tagTree.selectGuid(selectedTagGUIDs.get(i));
2144                 if (!found)
2145                         selectedTagGUIDs.remove(i);
2146         }
2147         tagTree.blockSignals(false);
2148         
2149                 browserWindow.setTag(getTagNamesForNote(currentNote));
2150         logger.log(logger.HIGH, "Leaving NeverNote.tagIndexUpdated");
2151     }   
2152     // Show/Hide note information
2153         @SuppressWarnings("unused")
2154         private void toggleTagWindow() {
2155                 logger.log(logger.HIGH, "Entering NeverNote.toggleTagWindow");
2156         if (tagTree.isVisible())
2157                 tagTree.hide();
2158         else
2159                 tagTree.show();
2160         menuBar.hideTags.setChecked(tagTree.isVisible());
2161         Global.saveWindowVisible("tagTree", tagTree.isVisible());
2162         logger.log(logger.HIGH, "Leaving NeverNote.toggleTagWindow");
2163     }   
2164         // A note's tags have been updated
2165         @SuppressWarnings("unused")
2166         private void updateNoteTags(String guid, List<String> tags) {
2167                 // Save any new tags.  We'll need them later.
2168                 List<String> newTags = new ArrayList<String>();
2169                 for (int i=0; i<tags.size(); i++) {
2170                         if (conn.getTagTable().findTagByName(tags.get(i))==null) 
2171                                 newTags.add(tags.get(i));
2172                 }
2173                 
2174                 listManager.saveNoteTags(guid, tags);
2175                 listManager.countTagResults(listManager.getNoteIndex());
2176                 StringBuffer names = new StringBuffer("");
2177                 for (int i=0; i<tags.size(); i++) {
2178                         names = names.append(tags.get(i));
2179                         if (i<tags.size()-1) {
2180                                 names.append(Global.tagDelimeter + " ");
2181                         }
2182                 }
2183                 browserWindow.setTag(names.toString());
2184                 noteDirty = true;
2185                 
2186                 // Now, we need to add any new tags to the tag tree
2187                 for (int i=0; i<newTags.size(); i++) 
2188                         tagTree.insertTag(newTags.get(i), conn.getTagTable().findTagByName(newTags.get(i)));
2189         }
2190         // Get a string containing all tag names for a note
2191         private String getTagNamesForNote(Note n) {
2192                 logger.log(logger.HIGH, "Entering NeverNote.getTagNamesForNote");
2193                 if (n==null || n.getGuid() == null || n.getGuid().equals(""))
2194                         return "";
2195                 StringBuffer buffer = new StringBuffer(100);
2196                 Vector<String> v = new Vector<String>();
2197                 List<String> guids = n.getTagGuids();
2198                 
2199                 if (guids == null) 
2200                         return "";
2201                 
2202                 for (int i=0; i<guids.size(); i++) {
2203                         v.add(listManager.getTagNameByGuid(guids.get(i)));
2204                 }
2205                 Comparator<String> comparator = Collections.reverseOrder();
2206                 Collections.sort(v,comparator);
2207                 Collections.reverse(v);
2208                 
2209                 for (int i = 0; i<v.size(); i++) {
2210                         if (i>0) 
2211                                 buffer.append(", ");
2212                         buffer.append(v.get(i));
2213                 }
2214                 
2215                 logger.log(logger.HIGH, "Leaving NeverNote.getTagNamesForNote");
2216                 return buffer.toString();
2217         }       
2218         // Tags were added via dropping notes from the note list
2219         @SuppressWarnings("unused")
2220         private void tagsAdded(String noteGuid, String tagGuid) {
2221                 String tagName = null;
2222                 for (int i=0; i<listManager.getTagIndex().size(); i++) {
2223                         if (listManager.getTagIndex().get(i).getGuid().equals(tagGuid)) {
2224                                 tagName = listManager.getTagIndex().get(i).getName();
2225                                 i=listManager.getTagIndex().size();
2226                         }
2227                 }
2228                 if (tagName == null)
2229                         return;
2230                 
2231                 for (int i=0; i<listManager.getMasterNoteIndex().size(); i++) {
2232                         if (listManager.getMasterNoteIndex().get(i).getGuid().equals(noteGuid)) {
2233                                 List<String> tagNames = new ArrayList<String>();
2234                                 tagNames.add(new String(tagName));
2235                                 Note n = listManager.getMasterNoteIndex().get(i);
2236                                 for (int j=0; j<n.getTagNames().size(); j++) {
2237                                         tagNames.add(new String(n.getTagNames().get(j)));
2238                                 }
2239                                 listManager.getNoteTableModel().updateNoteTags(noteGuid, n.getTagGuids(), tagNames);
2240                                 if (n.getGuid().equals(currentNoteGuid)) {
2241                                         Collections.sort(tagNames);
2242                                         String display = "";
2243                                         for (int j=0; j<tagNames.size(); j++) {
2244                                                 display = display+tagNames.get(j);
2245                                                 if (j+2<tagNames.size()) 
2246                                                         display = display+Global.tagDelimeter+" ";
2247                                         }
2248                                         browserWindow.setTag(display);
2249                                 }
2250                                 i=listManager.getMasterNoteIndex().size();
2251                         }
2252                 }
2253                 
2254                 
2255                 listManager.getNoteTableModel().updateNoteSyncStatus(noteGuid, false);
2256         }
2257         private void clearTagFilter() {
2258                 tagTree.blockSignals(true);
2259                 tagTree.clearSelection();
2260                 menuBar.noteRestoreAction.setVisible(false);
2261                 menuBar.tagEditAction.setEnabled(false);
2262                 menuBar.tagMergeAction.setEnabled(false);
2263                 menuBar.tagDeleteAction.setEnabled(false);
2264                 menuBar.tagIconAction.setEnabled(false);
2265                 selectedTagGUIDs.clear();
2266         listManager.setSelectedTags(selectedTagGUIDs);
2267         tagTree.blockSignals(false);
2268         }
2269         // Change the icon for a tag
2270         @SuppressWarnings("unused")
2271         private void setTagIcon() {
2272                 QTreeWidgetItem currentSelection;
2273                 List<QTreeWidgetItem> selections = tagTree.selectedItems();
2274                 if (selections.size() == 0)
2275                         return;
2276                 
2277                 currentSelection = selections.get(0);   
2278                 String guid = currentSelection.text(2);
2279
2280                 QIcon currentIcon = currentSelection.icon(0);
2281                 QIcon icon = conn.getTagTable().getIcon(guid);
2282                 SetIcon dialog;
2283                 if (icon == null) {
2284                         dialog = new SetIcon(currentIcon, saveLastPath);
2285                         dialog.setUseDefaultIcon(true);
2286                 } else {
2287                         dialog = new SetIcon(icon, saveLastPath);
2288                         dialog.setUseDefaultIcon(false);
2289                 }
2290                 dialog.exec();
2291                 if (dialog.okPressed()) {
2292                 saveLastPath = dialog.getPath();
2293                         QIcon newIcon = dialog.getIcon();
2294                         conn.getTagTable().setIcon(guid, newIcon, dialog.getFileType());
2295                         if (newIcon == null) 
2296                                 newIcon = new QIcon(iconPath+"tag.png");
2297                         currentSelection.setIcon(0, newIcon);
2298                 }
2299         
2300         }
2301         // Merge tags
2302         @SuppressWarnings("unused")
2303         private void mergeTags() {
2304                 List<Tag> tags = new ArrayList<Tag>();
2305                 List<QTreeWidgetItem> selections = tagTree.selectedItems();
2306                 for (int i=0; i<selections.size(); i++) {
2307                         Tag record = new Tag();
2308                         record.setGuid(selections.get(i).text(2));
2309                         record.setName(selections.get(i).text(0));
2310                         tags.add(record);
2311                 }
2312
2313                 TagMerge mergeDialog = new TagMerge(tags);
2314                 mergeDialog.exec();
2315                 if (!mergeDialog.okClicked())
2316                         return;
2317                 String newGuid = mergeDialog.getNewTagGuid();
2318                 
2319                 for (int i=0; i<tags.size(); i++) {
2320                         if (!tags.get(i).getGuid().equals(newGuid)) {
2321                                 List<String> noteGuids = conn.getNoteTable().noteTagsTable.getTagNotes(tags.get(i).getGuid());
2322                                 for (int j=0; j<noteGuids.size(); j++) {
2323                                         String noteGuid = noteGuids.get(j);
2324                                         conn.getNoteTable().noteTagsTable.deleteNoteTag(noteGuid);
2325                                         if (!conn.getNoteTable().noteTagsTable.checkNoteNoteTags(noteGuid, newGuid))
2326                                                 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, newGuid);
2327                                 }
2328                         }
2329                 }
2330                 listManager.reloadIndexes();
2331         }
2332         
2333     //***************************************************************
2334     //***************************************************************
2335     //** These functions deal with Saved Search menu items
2336     //***************************************************************
2337     //***************************************************************
2338         // Add a new notebook
2339         @SuppressWarnings("unused")
2340         private void addSavedSearch() {
2341                 logger.log(logger.HIGH, "Inside NeverNote.addSavedSearch");
2342                 SavedSearchEdit edit = new SavedSearchEdit();
2343                 edit.setSearchList(listManager.getSavedSearchIndex());
2344                 edit.exec();
2345         
2346                 if (!edit.okPressed())
2347                         return;
2348         
2349                 Calendar currentTime = new GregorianCalendar();         
2350                 Long l = new Long(currentTime.getTimeInMillis());
2351                 String randint = new String(Long.toString(l));
2352         
2353                 SavedSearch search = new SavedSearch();
2354                 search.setUpdateSequenceNum(0);
2355                 search.setGuid(randint);
2356                 search.setName(edit.getName());
2357                 search.setQuery(edit.getQuery());
2358                 search.setFormat(QueryFormat.USER);
2359                 listManager.getSavedSearchIndex().add(search);
2360                 conn.getSavedSearchTable().addSavedSearch(search, true);
2361                 savedSearchIndexUpdated();
2362                 logger.log(logger.HIGH, "Leaving NeverNote.addSavedSearch");
2363         }
2364         // Edit an existing tag
2365         @SuppressWarnings("unused")
2366         private void editSavedSearch() {
2367                 logger.log(logger.HIGH, "Entering NeverNote.editSavedSearch");
2368                 SavedSearchEdit edit = new SavedSearchEdit();
2369                 edit.setTitle(tr("Edit Search"));
2370                 List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2371                 QTreeWidgetItem currentSelection;
2372                 currentSelection = selections.get(0);
2373                 String guid = currentSelection.text(1);
2374                 SavedSearch s = conn.getSavedSearchTable().getSavedSearch(guid);
2375                 edit.setName(currentSelection.text(0));
2376                 edit.setQuery(s.getQuery());
2377                 edit.setSearchList(listManager.getSavedSearchIndex());
2378                 edit.exec();
2379         
2380                 if (!edit.okPressed())
2381                         return;
2382         
2383                 List<SavedSearch> list = listManager.getSavedSearchIndex();
2384                 SavedSearch search = null;
2385                 boolean found = false;
2386                 for (int i=0; i<list.size(); i++) {
2387                         search = list.get(i);
2388                         if (search.getGuid().equals(guid)) {
2389                                 i=list.size();
2390                                 found = true;
2391                         }
2392                 }
2393                 if (!found)
2394                         return;
2395                 search.setName(edit.getName());
2396                 search.setQuery(edit.getQuery());
2397                 conn.getSavedSearchTable().updateSavedSearch(search, true);
2398                 savedSearchIndexUpdated();
2399                 logger.log(logger.HIGH, "Leaving NeverNote.editSavedSearch");
2400         }
2401         // Delete an existing tag
2402         @SuppressWarnings("unused")
2403         private void deleteSavedSearch() {
2404                 logger.log(logger.HIGH, "Entering NeverNote.deleteSavedSearch");
2405                 
2406                 if (QMessageBox.question(this, "Confirmation", "Delete the selected search?",
2407                         QMessageBox.StandardButton.Yes, 
2408                         QMessageBox.StandardButton.No)==StandardButton.No.value()) {
2409                                                         return;
2410                 }
2411                 
2412                 List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2413         for (int i=selections.size()-1; i>=0; i--) {
2414                 QTreeWidgetItem currentSelection;
2415                 currentSelection = selections.get(i);
2416                 for (int j=0; j<listManager.getSavedSearchIndex().size(); j++) {
2417                         if (listManager.getSavedSearchIndex().get(j).getGuid().equals(currentSelection.text(1))) {
2418                                 conn.getSavedSearchTable().expungeSavedSearch(listManager.getSavedSearchIndex().get(j).getGuid(), true);
2419                                 listManager.getSavedSearchIndex().remove(j);
2420                                 j=listManager.getSavedSearchIndex().size()+1;
2421                         }
2422                 }
2423                 selections.remove(i);
2424         }
2425         savedSearchIndexUpdated();
2426         logger.log(logger.HIGH, "Leaving NeverNote.deleteSavedSearch");
2427         }
2428     // Setup the tree containing the user's tags
2429     private void initializeSavedSearchTree() {
2430         logger.log(logger.HIGH, "Entering NeverNote.initializeSavedSearchTree");
2431         savedSearchTree.itemSelectionChanged.connect(this, "savedSearchTreeSelection()");
2432         logger.log(logger.HIGH, "Leaving NeverNote.initializeSavedSearchTree");
2433     }
2434     // Listener when a tag is selected
2435     @SuppressWarnings("unused")
2436         private void savedSearchTreeSelection() {
2437         logger.log(logger.HIGH, "Entering NeverNote.savedSearchTreeSelection");
2438
2439         clearNotebookFilter();
2440         clearTagFilter();
2441         clearTrashFilter();
2442         clearAttributeFilter();
2443         
2444         String currentGuid = selectedSavedSearchGUID;
2445         menuBar.savedSearchEditAction.setEnabled(true);
2446         menuBar.savedSearchDeleteAction.setEnabled(true);
2447         menuBar.savedSearchIconAction.setEnabled(true);
2448         List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2449         QTreeWidgetItem currentSelection;
2450         selectedSavedSearchGUID = "";
2451         for (int i=0; i<selections.size(); i++) {
2452                 currentSelection = selections.get(i);
2453                 if (currentSelection.text(1).equals(currentGuid)) {
2454                         currentSelection.setSelected(false);
2455                 } else {
2456                         selectedSavedSearchGUID = currentSelection.text(1);
2457                 }
2458 //              i = selections.size() +1;
2459         }
2460         
2461         // There is the potential for no notebooks to be selected if this 
2462         // happens then we make it look like all notebooks were selecetd.
2463         // If that happens, just select the "all notebooks"
2464         if (selections.size()==0) {
2465                 clearSavedSearchFilter();
2466         }
2467         listManager.setSelectedSavedSearch(selectedSavedSearchGUID);
2468         
2469         logger.log(logger.HIGH, "Leaving NeverNote.savedSearchTreeSelection");
2470     }
2471     private void clearSavedSearchFilter() {
2472         menuBar.savedSearchEditAction.setEnabled(false);
2473         menuBar.savedSearchDeleteAction.setEnabled(false);
2474         menuBar.savedSearchIconAction.setEnabled(false);
2475         savedSearchTree.blockSignals(true);
2476         savedSearchTree.clearSelection();
2477         savedSearchTree.blockSignals(false);
2478         selectedSavedSearchGUID = "";
2479         searchField.setEditText("");
2480         searchPerformed = false;
2481         listManager.setSelectedSavedSearch(selectedSavedSearchGUID);
2482     }
2483     // trigger the tag index to be refreshed
2484         private void savedSearchIndexUpdated() { 
2485                 if (selectedSavedSearchGUID == null)
2486                         selectedSavedSearchGUID = new String();
2487                 savedSearchTree.blockSignals(true);
2488                 savedSearchTree.setIcons(conn.getSavedSearchTable().getAllIcons());
2489         savedSearchTree.load(listManager.getSavedSearchIndex());
2490         savedSearchTree.selectGuid(selectedSavedSearchGUID);
2491         savedSearchTree.blockSignals(false);
2492     }
2493     // trigger when the saved search selection changes
2494     @SuppressWarnings("unused")
2495         private void updateSavedSearchSelection() {
2496                 logger.log(logger.HIGH, "Entering NeverNote.updateSavedSearchSelection()");
2497                 
2498         menuBar.savedSearchEditAction.setEnabled(true);
2499         menuBar.savedSearchDeleteAction.setEnabled(true);
2500         menuBar.savedSearchIconAction.setEnabled(true);
2501         List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2502
2503         if (selections.size() > 0) {
2504                 menuBar.savedSearchEditAction.setEnabled(true);
2505                 menuBar.savedSearchDeleteAction.setEnabled(true);
2506                 menuBar.savedSearchIconAction.setEnabled(true);
2507                 selectedSavedSearchGUID = selections.get(0).text(1);
2508                 SavedSearch s = conn.getSavedSearchTable().getSavedSearch(selectedSavedSearchGUID);
2509                 searchField.setEditText(s.getQuery());
2510         } else { 
2511                 menuBar.savedSearchEditAction.setEnabled(false);
2512                 menuBar.savedSearchDeleteAction.setEnabled(false);
2513                 menuBar.savedSearchIconAction.setEnabled(false);
2514                 selectedSavedSearchGUID = "";
2515                 searchField.setEditText("");
2516         }
2517         searchFieldChanged();
2518         
2519                 logger.log(logger.HIGH, "Leaving NeverNote.updateSavedSearchSelection()");
2520
2521         
2522     }
2523     // Show/Hide note information
2524         @SuppressWarnings("unused")
2525         private void toggleSavedSearchWindow() {
2526                 logger.log(logger.HIGH, "Entering NeverNote.toggleSavedSearchWindow");
2527         if (savedSearchTree.isVisible())
2528                 savedSearchTree.hide();
2529         else
2530                 savedSearchTree.show();
2531         menuBar.hideSavedSearches.setChecked(savedSearchTree.isVisible());
2532                                 
2533                 Global.saveWindowVisible("savedSearchTree", savedSearchTree.isVisible());
2534         logger.log(logger.HIGH, "Leaving NeverNote.toggleSavedSearchWindow");
2535     }
2536         // Change the icon for a saved search
2537         @SuppressWarnings("unused")
2538         private void setSavedSearchIcon() {
2539                 QTreeWidgetItem currentSelection;
2540                 List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2541                 if (selections.size() == 0)
2542                         return;
2543                 
2544                 currentSelection = selections.get(0);   
2545                 String guid = currentSelection.text(1);
2546
2547                 QIcon currentIcon = currentSelection.icon(0);
2548                 QIcon icon = conn.getSavedSearchTable().getIcon(guid);
2549                 SetIcon dialog;
2550                 if (icon == null) {
2551                         dialog = new SetIcon(currentIcon, saveLastPath);
2552                         dialog.setUseDefaultIcon(true);
2553                 } else {
2554                         dialog = new SetIcon(icon, saveLastPath);
2555                         dialog.setUseDefaultIcon(false);
2556                 }
2557                 dialog.exec();
2558                 if (dialog.okPressed()) {
2559                 saveLastPath = dialog.getPath();
2560                         QIcon newIcon = dialog.getIcon();
2561                         conn.getSavedSearchTable().setIcon(guid, newIcon, dialog.getFileType());
2562                         if (newIcon == null) 
2563                                 newIcon = new QIcon(iconPath+"search.png");
2564                         currentSelection.setIcon(0, newIcon);
2565                 }
2566         
2567         }
2568         
2569         
2570         
2571         
2572     //***************************************************************
2573     //***************************************************************
2574     //** These functions deal with Help menu & tool menu items
2575     //***************************************************************
2576     //***************************************************************
2577         // Show database status
2578         @SuppressWarnings("unused")
2579         private void databaseStatus() {
2580                 waitCursor(true);
2581                 indexRunner.interrupt = true;
2582                 int dirty = conn.getNoteTable().getDirtyCount();
2583                 int unindexed = conn.getNoteTable().getUnindexedCount();
2584                 DatabaseStatus status = new DatabaseStatus();
2585                 status.setUnsynchronized(dirty);
2586                 status.setUnindexed(unindexed);
2587                 status.setNoteCount(conn.getNoteTable().getNoteCount());
2588                 status.setNotebookCount(listManager.getNotebookIndex().size());
2589                 status.setUnindexedResourceCount(conn.getNoteTable().noteResourceTable.getUnindexedCount());
2590                 status.setSavedSearchCount(listManager.getSavedSearchIndex().size());
2591                 status.setTagCount(listManager.getTagIndex().size());
2592                 status.setResourceCount(conn.getNoteTable().noteResourceTable.getResourceCount());
2593                 status.setWordCount(conn.getWordsTable().getWordCount());
2594                 waitCursor(false);
2595                 status.exec();
2596         }
2597         // Compact the database
2598         @SuppressWarnings("unused")
2599         private void compactDatabase() {
2600         logger.log(logger.HIGH, "Entering NeverNote.compactDatabase");
2601                 if (QMessageBox.question(this, tr("Confirmation"), tr("This will free unused space in the database, "+
2602                                 "but please be aware that depending upon the size of your database this can be time consuming " +
2603                                 "and NeverNote will be unresponsive until it is complete.  Do you wish to continue?"),
2604                                 QMessageBox.StandardButton.Yes, 
2605                                 QMessageBox.StandardButton.No)==StandardButton.No.value() && Global.verifyDelete() == true) {
2606                                                         return;
2607                 }
2608                 setMessage("Compacting database.");
2609                 waitCursor(true);
2610                 listManager.compactDatabase();
2611                 waitCursor(false);
2612                 setMessage("Database compact is complete.");            
2613         logger.log(logger.HIGH, "Leaving NeverNote.compactDatabase");
2614     }
2615         @SuppressWarnings("unused")
2616         private void accountInformation() {
2617                 logger.log(logger.HIGH, "Entering NeverNote.accountInformation");
2618                 AccountDialog dialog = new AccountDialog();
2619                 dialog.show();
2620                 logger.log(logger.HIGH, "Leaving NeverNote.accountInformation");
2621         }
2622         @SuppressWarnings("unused")
2623         private void releaseNotes() {
2624                 logger.log(logger.HIGH, "Entering NeverNote.releaseNotes");
2625                 QDialog dialog = new QDialog(this);
2626                 QHBoxLayout layout = new QHBoxLayout();
2627                 QTextEdit textBox = new QTextEdit();
2628                 layout.addWidget(textBox);
2629                 textBox.setReadOnly(true);
2630                 QFile file = new QFile(Global.getFileManager().getHomeDirPath("release.txt"));
2631                 if (!file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly,
2632                 QIODevice.OpenModeFlag.Text)))
2633                         return;
2634                 textBox.setText(file.readAll().toString());
2635                 file.close();
2636                 dialog.setWindowTitle(tr("Release Notes"));
2637                 dialog.setLayout(layout);
2638                 dialog.show();
2639                 logger.log(logger.HIGH, "Leaving NeverNote.releaseNotes");
2640         }
2641         // Called when user picks Log from the help menu
2642         @SuppressWarnings("unused")
2643         private void logger() {
2644                 logger.log(logger.HIGH, "Entering NeverNote.logger");
2645                 LogFileDialog dialog = new LogFileDialog(emitLog);
2646                 dialog.exec();
2647                 logger.log(logger.HIGH, "Leaving NeverNote.logger");
2648         }
2649         // Menu option "help/about" was selected
2650         @SuppressWarnings("unused")
2651         private void about() {
2652                 logger.log(logger.HIGH, "Entering NeverNote.about");
2653                 QMessageBox.about(this, 
2654                                                 tr("About NeverNote"),
2655                                                 tr("<h4><center><b>NeverNote</b></center></h4><hr><center>Version ")
2656                                                 +Global.version
2657                                                 +tr("<hr></center>Evernote"
2658                                                                 +"An Open Source Evernote Client.<br><br>" 
2659                                                                 +"Licensed under GPL v2.  <br><hr><br>"
2660                                                                 +"Evernote is copyright 2001-2010 by Evernote Corporation<br>"
2661                                                                 +"Jambi and QT are the licensed trademark of Nokia Corporation<br>"
2662                                                                 +"PDFRenderer is licened under the LGPL<br>"
2663                                                                 +"JTidy is copyrighted under the World Wide Web Consortium<br>"
2664                                                                 +"Apache Common Utilities licensed under the Apache License Version 2.0<br>"
2665                                                                 +"Jazzy is licened under the LGPL<br>"
2666                                                                 +"Java is a registered trademark of Oracle Corporation.<br><hr>"
2667                                                                 +"Special thanks to:<br>BitRock InstallBuilder for the Windows installer"
2668                                                                 +"<br>CodeCogs (www.codecogs.com) for the LaTeX image rendering."));
2669                 logger.log(logger.HIGH, "Leaving NeverNote.about");
2670         }
2671         // Hide the entire left hand side
2672         @SuppressWarnings("unused")
2673         private void toggleLeftSide() {
2674                 boolean hidden;
2675                 
2676                 hidden = !menuBar.hideLeftSide.isChecked();
2677                 menuBar.hideLeftSide.setChecked(!hidden);
2678                 
2679                 if (!hidden) 
2680                         leftSplitter1.setHidden(true);
2681                 else
2682                         leftSplitter1.setHidden(false);
2683                 
2684                 Global.saveWindowVisible("leftPanel", hidden);
2685                 
2686         }
2687         public void checkForUpdates() {
2688                 // Send off thread to check for a new version
2689                 versionChecker = new QNetworkAccessManager(this);
2690                 versionChecker.finished.connect(this, "upgradeFileRead(QNetworkReply)");
2691                 QNetworkRequest request = new QNetworkRequest();
2692                 request.setUrl(new QUrl(Global.getUpdatesAvailableUrl()));
2693                 versionChecker.get(request);
2694         }
2695         @SuppressWarnings("unused")
2696         private void upgradeFileRead(QNetworkReply reply) {
2697                 if (!reply.isReadable())
2698                         return;
2699                 
2700                 String winVersion = Global.version;
2701                 String osxVersion = Global.version;
2702                 String linuxVersion = Global.version;
2703                 String linux64Version = Global.version;
2704                 String version = Global.version;
2705                 
2706                 // Determine the versions available
2707                 QByteArray data = reply.readLine();
2708                 while (data != null && !reply.atEnd()) {
2709                         String line = data.toString();
2710                         String lineVersion;
2711                         if (line.contains(":")) 
2712                                 lineVersion = line.substring(line.indexOf(":")+1).replace(" ", "").replace("\n", "");
2713                         else
2714                                 lineVersion = "";
2715                         if (line.toLowerCase().contains("windows")) 
2716                                 winVersion = lineVersion;
2717                         else if (line.toLowerCase().contains("os-x")) 
2718                                 osxVersion = lineVersion;
2719                         else if (line.toLowerCase().contains("linux amd64")) 
2720                                 linux64Version = lineVersion;
2721                         else if (line.toLowerCase().contains("linux i386")) 
2722                                 linuxVersion = lineVersion;
2723                         else if (line.toLowerCase().contains("default")) 
2724                                 version = lineVersion;
2725                         
2726                         // Read the next line
2727                         data = reply.readLine();
2728                 }
2729                 
2730                 // Now we need to determine what system we are on.
2731                 if (System.getProperty("os.name").toLowerCase().contains("windows"))
2732                         version = winVersion;
2733                 if (System.getProperty("os.name").toLowerCase().contains("mac os"))
2734                         version = osxVersion;
2735                 if (System.getProperty("os.name").toLowerCase().contains("Linux")) {
2736                         if (System.getProperty("os.arch").contains("amd64") ||
2737                                 System.getProperty("os.arch").contains("x86_64"))
2738                                         version = linux64Version;
2739                         else
2740                                 version = linuxVersion;
2741                 }
2742                 
2743                 
2744                 for (String validVersion : Global.validVersions) {
2745                         if (version.equals(validVersion))
2746                                 return;
2747                 }
2748                 
2749                 UpgradeAvailableDialog dialog = new UpgradeAvailableDialog();
2750                 dialog.exec();
2751                 if (dialog.remindMe())
2752                         Global.setCheckVersionUpgrade(true);
2753                 else
2754                         Global.setCheckVersionUpgrade(false);
2755         }
2756                 
2757         
2758     //***************************************************************
2759     //***************************************************************
2760     //** These functions deal with the Toolbar
2761     //***************************************************************
2762     //*************************************************************** 
2763         @SuppressWarnings("unused")
2764         private void focusSearch() {
2765                 searchField.setFocus();
2766         }
2767
2768         // Text in the search bar has been cleared
2769         private void searchFieldCleared() {
2770                 saveNote();
2771                 
2772                 // This is done because we want to force a reload of
2773                 // images.  Some images we may want to highlight the text.
2774                 readOnlyCache.clear();
2775                 inkNoteCache.clear();
2776                 noteCache.clear();
2777                 QWebSettings.setMaximumPagesInCache(0);
2778                 QWebSettings.setObjectCacheCapacities(0, 0, 0);
2779         
2780                 searchField.setEditText("");
2781                 saveNoteColumnPositions();
2782                 saveNoteIndexWidth();
2783                 noteIndexUpdated(true);
2784                 if (currentNote == null && listManager.getNoteIndex().size() > 0) {
2785                         currentNote = listManager.getNoteIndex().get(0);
2786                         currentNoteGuid = currentNote.getGuid();
2787                 }
2788                 refreshEvernoteNote(true);
2789                 if (currentNote != null)
2790                         loadNoteBrowserInformation(browserWindow);
2791         }
2792         // text in the search bar changed.  We only use this to tell if it was cleared, 
2793         // otherwise we trigger off searchFieldChanged.
2794         @SuppressWarnings("unused")
2795         private void searchFieldTextChanged(String text) {
2796                 QWebSettings.setMaximumPagesInCache(0);
2797                 QWebSettings.setObjectCacheCapacities(0, 0, 0);
2798
2799                 if (text.trim().equals("")) {
2800                         searchFieldCleared();
2801                         if (searchPerformed) {
2802
2803                                 // This is done because we want to force a reload of
2804                                 // images.  Some images we may want to highlight the text.
2805                                 noteCache.clear();
2806                                 readOnlyCache.clear();
2807                                 inkNoteCache.clear();
2808                                 
2809                                 listManager.setEnSearch("");
2810                                 listManager.loadNotesIndex();
2811                                 refreshEvernoteNote(true);
2812                                 noteIndexUpdated(false);
2813                                 refreshEvernoteNote(true);
2814                         }
2815                         searchPerformed = false;
2816                 }
2817         }
2818     // Text in the toolbar has changed
2819     private void searchFieldChanged() {
2820         logger.log(logger.HIGH, "Entering NeverNote.searchFieldChanged");
2821         noteCache.clear();
2822         readOnlyCache.clear();
2823         inkNoteCache.clear();
2824         saveNoteColumnPositions();
2825         saveNoteIndexWidth();
2826         String text = searchField.currentText();
2827         listManager.setEnSearch(text.trim());
2828         listManager.loadNotesIndex();
2829         noteIndexUpdated(false);
2830         refreshEvernoteNote(true);
2831         searchPerformed = true;
2832         logger.log(logger.HIGH, "Leaving NeverNote.searchFieldChanged");
2833     }
2834
2835     // Build the window tool bar
2836     private void setupToolBar() {
2837         logger.log(logger.HIGH, "Entering NeverNote.setupToolBar");
2838         toolBar = addToolBar(tr("Tool Bar"));   
2839         toolBar.setObjectName("toolBar");
2840         menuBar.setupToolBarVisible();
2841         if (!Global.isWindowVisible("toolBar"))
2842                 toolBar.setVisible(false);
2843         else
2844                 toolBar.setVisible(true);
2845
2846 //      toolBar.addWidget(menuBar);
2847 //      menuBar.setSizePolicy(Policy.Minimum, Policy.Minimum);
2848 //      toolBar.addSeparator();
2849         prevButton = toolBar.addAction("Previous");
2850         QIcon prevIcon = new QIcon(iconPath+"back.png");
2851         prevButton.setIcon(prevIcon);
2852         prevButton.triggered.connect(this, "previousViewedAction()");   
2853         togglePrevArrowButton(Global.isToolbarButtonVisible("prevArrow"));
2854         
2855         nextButton = toolBar.addAction("Next");
2856         QIcon nextIcon = new QIcon(iconPath+"forward.png");
2857         nextButton.setIcon(nextIcon);
2858         nextButton.triggered.connect(this, "nextViewedAction()");       
2859         toggleNextArrowButton(Global.isToolbarButtonVisible("nextArrow"));
2860         
2861         upButton = toolBar.addAction("Up");
2862         QIcon upIcon = new QIcon(iconPath+"up.png");
2863         upButton.setIcon(upIcon);
2864         upButton.triggered.connect(this, "upAction()");         
2865         toggleUpArrowButton(Global.isToolbarButtonVisible("upArrow"));
2866
2867         
2868         downButton = toolBar.addAction("Down");
2869         QIcon downIcon = new QIcon(iconPath+"down.png");
2870         downButton.setIcon(downIcon);
2871         downButton.triggered.connect(this, "downAction()");
2872         toggleDownArrowButton(Global.isToolbarButtonVisible("downArrow"));
2873         
2874         synchronizeButton = toolBar.addAction("Synchronize");
2875         synchronizeButton.setIcon(new QIcon(iconPath+"synchronize.png"));
2876         synchronizeIconAngle = 0;
2877         synchronizeButton.triggered.connect(this, "evernoteSync()");
2878         toggleSynchronizeButton(Global.isToolbarButtonVisible("synchronize"));
2879         
2880         printButton = toolBar.addAction("Print");
2881         QIcon printIcon = new QIcon(iconPath+"print.png");
2882         printButton.setIcon(printIcon);
2883         printButton.triggered.connect(this, "printNote()");
2884         togglePrintButton(Global.isToolbarButtonVisible("print"));
2885
2886         tagButton = toolBar.addAction("Tag"); 
2887         QIcon tagIcon = new QIcon(iconPath+"tag.png");
2888         tagButton.setIcon(tagIcon);
2889         tagButton.triggered.connect(browserWindow, "modifyTags()");
2890         toggleTagButton(Global.isToolbarButtonVisible("tag"));
2891
2892         attributeButton = toolBar.addAction("Attributes"); 
2893         QIcon attributeIcon = new QIcon(iconPath+"attribute.png");
2894         attributeButton.setIcon(attributeIcon);
2895         attributeButton.triggered.connect(this, "toggleNoteInformation()");
2896         toggleAttributeButton(Global.isToolbarButtonVisible("attribute"));
2897                 
2898         emailButton = toolBar.addAction("Email");
2899         QIcon emailIcon = new QIcon(iconPath+"email.png");
2900         emailButton.setIcon(emailIcon);
2901         emailButton.triggered.connect(this, "emailNote()");
2902         toggleEmailButton(Global.isToolbarButtonVisible("email"));
2903
2904         deleteButton = toolBar.addAction("Delete");     
2905         QIcon deleteIcon = new QIcon(iconPath+"delete.png");
2906         deleteButton.setIcon(deleteIcon);
2907         deleteButton.triggered.connect(this, "deleteNote()");
2908         toggleDeleteButton(Global.isToolbarButtonVisible("delete"));
2909
2910         newButton = toolBar.addAction("New");
2911         QIcon newIcon = new QIcon(iconPath+"new.png");
2912         newButton.triggered.connect(this, "addNote()");
2913         newButton.setIcon(newIcon);
2914         toggleNewButton(Global.isToolbarButtonVisible("new"));
2915         
2916         allNotesButton = toolBar.addAction("All Notes");
2917         QIcon allIcon = new QIcon(iconPath+"books.png");
2918         allNotesButton.triggered.connect(this, "allNotes()");
2919         allNotesButton.setIcon(allIcon);
2920         toggleAllNotesButton(Global.isToolbarButtonVisible("allNotes"));
2921         
2922         //toolBar.addSeparator();
2923         //toolBar.addWidget(new QLabel(tr("Quota:")));
2924         //toolBar.addWidget(quotaBar);
2925         //quotaBar.setSizePolicy(Policy.Minimum, Policy.Minimum);
2926         updateQuotaBar();
2927         //toolBar.addSeparator();
2928         
2929         //toolBar.addWidget(new QLabel(tr("Zoom")));
2930         //toolBar.addWidget(zoomSpinner);
2931         
2932         //toolBar.addWidget(new QLabel("                    "));
2933         //toolBar.addSeparator();
2934         //toolBar.addWidget(new QLabel(tr("  Search:")));
2935         //toolBar.addWidget(searchField);
2936         QSizePolicy sizePolicy = new QSizePolicy();
2937         sizePolicy.setHorizontalPolicy(Policy.MinimumExpanding);
2938         QLabel spacer = new QLabel("");
2939         spacer.setSizePolicy(sizePolicy);
2940         toolBar.addWidget(spacer);
2941         //searchField.setInsertPolicy(InsertPolicy.InsertAtTop);
2942
2943         //searchClearButton = toolBar.addAction("Search Clear");
2944         //QIcon searchClearIcon = new QIcon(iconPath+"searchclear.png");
2945         //searchClearButton.setIcon(searchClearIcon);
2946         //searchClearButton.triggered.connect(this, "searchFieldCleared()");
2947         //toggleSearchClearButton(Global.isToolbarButtonVisible("searchClear"));
2948
2949         logger.log(logger.HIGH, "Leaving NeverNote.setupToolBar");
2950     }
2951     // Update the sychronize button picture
2952     @Override
2953         public QMenu createPopupMenu() {
2954         QMenu contextMenu = super.createPopupMenu();
2955         
2956         contextMenu.addSeparator();
2957         QAction prevAction = addContextAction("prevArrow", tr("Previous Arrow"));
2958         contextMenu.addAction(prevAction);
2959         prevAction.triggered.connect(this, "togglePrevArrowButton(Boolean)");
2960
2961         QAction nextAction = addContextAction("nextArrow", tr("Next Arrow"));
2962         contextMenu.addAction(nextAction);
2963         nextAction.triggered.connect(this, "toggleNextArrowButton(Boolean)");
2964
2965         QAction upAction = addContextAction("upArrow", tr("Up Arrow"));
2966         contextMenu.addAction(upAction);
2967         upAction.triggered.connect(this, "toggleUpArrowButton(Boolean)");
2968
2969         QAction downAction = addContextAction("downArrow", tr("Down Arrow"));
2970         contextMenu.addAction(downAction);
2971         downAction.triggered.connect(this, "toggleDownArrowButton(Boolean)");
2972
2973         QAction synchronizeAction = addContextAction("synchronize", tr("Synchronize"));
2974         contextMenu.addAction(synchronizeAction);
2975         synchronizeAction.triggered.connect(this, "toggleSynchronizeButton(Boolean)");
2976
2977         QAction printAction = addContextAction("print", tr("Print"));
2978         contextMenu.addAction(printAction);
2979         printAction.triggered.connect(this, "togglePrintButton(Boolean)");
2980
2981         QAction tagAction = addContextAction("tag", tr("Tag"));
2982         contextMenu.addAction(tagAction);
2983         tagAction.triggered.connect(this, "toggleTagButton(Boolean)");
2984         
2985         QAction attributeAction = addContextAction("attribute", tr("Attribute"));
2986         contextMenu.addAction(attributeAction);
2987         attributeAction.triggered.connect(this, "toggleAttributeButton(Boolean)");
2988         
2989         QAction emailAction = addContextAction("email", tr("Email"));
2990         contextMenu.addAction(emailAction);
2991         emailAction.triggered.connect(this, "toggleEmailButton(Boolean)");
2992
2993         QAction deleteAction = addContextAction("delete", tr("Delete"));
2994         contextMenu.addAction(deleteAction);
2995         deleteAction.triggered.connect(this, "toggleDeleteButton(Boolean)");
2996
2997         QAction newAction = addContextAction("new", tr("Add"));
2998         contextMenu.addAction(newAction);
2999         newAction.triggered.connect(this, "toggleNewButton(Boolean)");
3000
3001         QAction allNotesAction = addContextAction("allNotes", tr("All Notes"));
3002         contextMenu.addAction(allNotesAction);
3003         allNotesAction.triggered.connect(this, "toggleAllNotesButton(Boolean)");
3004         
3005         QAction searchClearAction = addContextAction("searchClear", tr("Search Clear"));
3006         contextMenu.addAction(searchClearAction);
3007         searchClearAction.triggered.connect(this, "toggleSearchClearButton(Boolean)");
3008         
3009         return contextMenu;
3010         
3011     }
3012     private QAction addContextAction(String config, String name) {
3013         QAction newAction = new QAction(this);
3014                 newAction.setText(name);
3015                 newAction.setCheckable(true);
3016                 newAction.setChecked(Global.isToolbarButtonVisible(config));
3017                 return newAction;
3018     }
3019     private void togglePrevArrowButton(Boolean toggle) {
3020                 prevButton.setVisible(toggle);
3021                 Global.saveToolbarButtonsVisible("prevArrow", toggle);
3022     }
3023     private void toggleNextArrowButton(Boolean toggle) {
3024                 nextButton.setVisible(toggle);
3025                 Global.saveToolbarButtonsVisible("nextArrow", toggle);
3026     }
3027     private void toggleUpArrowButton(Boolean toggle) {
3028                 upButton.setVisible(toggle);
3029                 Global.saveToolbarButtonsVisible("upArrow", toggle);
3030     }
3031     private void toggleDownArrowButton(Boolean toggle) {
3032                 downButton.setVisible(toggle);
3033                 Global.saveToolbarButtonsVisible("downArrow", toggle);
3034     }
3035     private void toggleSynchronizeButton(Boolean toggle) {
3036                 synchronizeButton.setVisible(toggle);
3037                 Global.saveToolbarButtonsVisible("synchronize", toggle);
3038     }
3039     private void togglePrintButton(Boolean toggle) {
3040                 printButton.setVisible(toggle);
3041                 Global.saveToolbarButtonsVisible("print", toggle);
3042     }
3043     private void toggleTagButton(Boolean toggle) {
3044                 tagButton.setVisible(toggle);
3045                 Global.saveToolbarButtonsVisible("tag", toggle);
3046     }
3047     private void toggleAttributeButton(Boolean toggle) {
3048                 attributeButton.setVisible(toggle);
3049                 Global.saveToolbarButtonsVisible("attribute", toggle);
3050     }