OSDN Git Service

Added the ability to merge tags and corrected some shortcuts.
[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         private void mergeTags() {
2303                 List<Tag> tags = new ArrayList<Tag>();
2304                 List<QTreeWidgetItem> selections = tagTree.selectedItems();
2305                 for (int i=0; i<selections.size(); i++) {
2306                         Tag record = new Tag();
2307                         record.setGuid(selections.get(i).text(2));
2308                         record.setName(selections.get(i).text(0));
2309                         tags.add(record);
2310                 }
2311
2312                 TagMerge mergeDialog = new TagMerge(tags);
2313                 mergeDialog.exec();
2314                 if (!mergeDialog.okClicked())
2315                         return;
2316                 String newGuid = mergeDialog.getNewTagGuid();
2317                 
2318                 for (int i=0; i<tags.size(); i++) {
2319                         if (!tags.get(i).getGuid().equals(newGuid)) {
2320                                 List<String> noteGuids = conn.getNoteTable().noteTagsTable.getTagNotes(tags.get(i).getGuid());
2321                                 for (int j=0; j<noteGuids.size(); j++) {
2322                                         String noteGuid = noteGuids.get(j);
2323                                         conn.getNoteTable().noteTagsTable.deleteNoteTag(noteGuid);
2324                                         if (!conn.getNoteTable().noteTagsTable.checkNoteNoteTags(noteGuid, newGuid))
2325                                                 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, newGuid);
2326                                 }
2327                         }
2328                 }
2329                 listManager.reloadIndexes();
2330         }
2331         
2332     //***************************************************************
2333     //***************************************************************
2334     //** These functions deal with Saved Search menu items
2335     //***************************************************************
2336     //***************************************************************
2337         // Add a new notebook
2338         @SuppressWarnings("unused")
2339         private void addSavedSearch() {
2340                 logger.log(logger.HIGH, "Inside NeverNote.addSavedSearch");
2341                 SavedSearchEdit edit = new SavedSearchEdit();
2342                 edit.setSearchList(listManager.getSavedSearchIndex());
2343                 edit.exec();
2344         
2345                 if (!edit.okPressed())
2346                         return;
2347         
2348                 Calendar currentTime = new GregorianCalendar();         
2349                 Long l = new Long(currentTime.getTimeInMillis());
2350                 String randint = new String(Long.toString(l));
2351         
2352                 SavedSearch search = new SavedSearch();
2353                 search.setUpdateSequenceNum(0);
2354                 search.setGuid(randint);
2355                 search.setName(edit.getName());
2356                 search.setQuery(edit.getQuery());
2357                 search.setFormat(QueryFormat.USER);
2358                 listManager.getSavedSearchIndex().add(search);
2359                 conn.getSavedSearchTable().addSavedSearch(search, true);
2360                 savedSearchIndexUpdated();
2361                 logger.log(logger.HIGH, "Leaving NeverNote.addSavedSearch");
2362         }
2363         // Edit an existing tag
2364         @SuppressWarnings("unused")
2365         private void editSavedSearch() {
2366                 logger.log(logger.HIGH, "Entering NeverNote.editSavedSearch");
2367                 SavedSearchEdit edit = new SavedSearchEdit();
2368                 edit.setTitle(tr("Edit Search"));
2369                 List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2370                 QTreeWidgetItem currentSelection;
2371                 currentSelection = selections.get(0);
2372                 String guid = currentSelection.text(1);
2373                 SavedSearch s = conn.getSavedSearchTable().getSavedSearch(guid);
2374                 edit.setName(currentSelection.text(0));
2375                 edit.setQuery(s.getQuery());
2376                 edit.setSearchList(listManager.getSavedSearchIndex());
2377                 edit.exec();
2378         
2379                 if (!edit.okPressed())
2380                         return;
2381         
2382                 List<SavedSearch> list = listManager.getSavedSearchIndex();
2383                 SavedSearch search = null;
2384                 boolean found = false;
2385                 for (int i=0; i<list.size(); i++) {
2386                         search = list.get(i);
2387                         if (search.getGuid().equals(guid)) {
2388                                 i=list.size();
2389                                 found = true;
2390                         }
2391                 }
2392                 if (!found)
2393                         return;
2394                 search.setName(edit.getName());
2395                 search.setQuery(edit.getQuery());
2396                 conn.getSavedSearchTable().updateSavedSearch(search, true);
2397                 savedSearchIndexUpdated();
2398                 logger.log(logger.HIGH, "Leaving NeverNote.editSavedSearch");
2399         }
2400         // Delete an existing tag
2401         @SuppressWarnings("unused")
2402         private void deleteSavedSearch() {
2403                 logger.log(logger.HIGH, "Entering NeverNote.deleteSavedSearch");
2404                 
2405                 if (QMessageBox.question(this, "Confirmation", "Delete the selected search?",
2406                         QMessageBox.StandardButton.Yes, 
2407                         QMessageBox.StandardButton.No)==StandardButton.No.value()) {
2408                                                         return;
2409                 }
2410                 
2411                 List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2412         for (int i=selections.size()-1; i>=0; i--) {
2413                 QTreeWidgetItem currentSelection;
2414                 currentSelection = selections.get(i);
2415                 for (int j=0; j<listManager.getSavedSearchIndex().size(); j++) {
2416                         if (listManager.getSavedSearchIndex().get(j).getGuid().equals(currentSelection.text(1))) {
2417                                 conn.getSavedSearchTable().expungeSavedSearch(listManager.getSavedSearchIndex().get(j).getGuid(), true);
2418                                 listManager.getSavedSearchIndex().remove(j);
2419                                 j=listManager.getSavedSearchIndex().size()+1;
2420                         }
2421                 }
2422                 selections.remove(i);
2423         }
2424         savedSearchIndexUpdated();
2425         logger.log(logger.HIGH, "Leaving NeverNote.deleteSavedSearch");
2426         }
2427     // Setup the tree containing the user's tags
2428     private void initializeSavedSearchTree() {
2429         logger.log(logger.HIGH, "Entering NeverNote.initializeSavedSearchTree");
2430         savedSearchTree.itemSelectionChanged.connect(this, "savedSearchTreeSelection()");
2431         logger.log(logger.HIGH, "Leaving NeverNote.initializeSavedSearchTree");
2432     }
2433     // Listener when a tag is selected
2434     @SuppressWarnings("unused")
2435         private void savedSearchTreeSelection() {
2436         logger.log(logger.HIGH, "Entering NeverNote.savedSearchTreeSelection");
2437
2438         clearNotebookFilter();
2439         clearTagFilter();
2440         clearTrashFilter();
2441         clearAttributeFilter();
2442         
2443         String currentGuid = selectedSavedSearchGUID;
2444         menuBar.savedSearchEditAction.setEnabled(true);
2445         menuBar.savedSearchDeleteAction.setEnabled(true);
2446         menuBar.savedSearchIconAction.setEnabled(true);
2447         List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2448         QTreeWidgetItem currentSelection;
2449         selectedSavedSearchGUID = "";
2450         for (int i=0; i<selections.size(); i++) {
2451                 currentSelection = selections.get(i);
2452                 if (currentSelection.text(1).equals(currentGuid)) {
2453                         currentSelection.setSelected(false);
2454                 } else {
2455                         selectedSavedSearchGUID = currentSelection.text(1);
2456                 }
2457 //              i = selections.size() +1;
2458         }
2459         
2460         // There is the potential for no notebooks to be selected if this 
2461         // happens then we make it look like all notebooks were selecetd.
2462         // If that happens, just select the "all notebooks"
2463         if (selections.size()==0) {
2464                 clearSavedSearchFilter();
2465         }
2466         listManager.setSelectedSavedSearch(selectedSavedSearchGUID);
2467         
2468         logger.log(logger.HIGH, "Leaving NeverNote.savedSearchTreeSelection");
2469     }
2470     private void clearSavedSearchFilter() {
2471         menuBar.savedSearchEditAction.setEnabled(false);
2472         menuBar.savedSearchDeleteAction.setEnabled(false);
2473         menuBar.savedSearchIconAction.setEnabled(false);
2474         savedSearchTree.blockSignals(true);
2475         savedSearchTree.clearSelection();
2476         savedSearchTree.blockSignals(false);
2477         selectedSavedSearchGUID = "";
2478         searchField.setEditText("");
2479         searchPerformed = false;
2480         listManager.setSelectedSavedSearch(selectedSavedSearchGUID);
2481     }
2482     // trigger the tag index to be refreshed
2483         private void savedSearchIndexUpdated() { 
2484                 if (selectedSavedSearchGUID == null)
2485                         selectedSavedSearchGUID = new String();
2486                 savedSearchTree.blockSignals(true);
2487                 savedSearchTree.setIcons(conn.getSavedSearchTable().getAllIcons());
2488         savedSearchTree.load(listManager.getSavedSearchIndex());
2489         savedSearchTree.selectGuid(selectedSavedSearchGUID);
2490         savedSearchTree.blockSignals(false);
2491     }
2492     // trigger when the saved search selection changes
2493     @SuppressWarnings("unused")
2494         private void updateSavedSearchSelection() {
2495                 logger.log(logger.HIGH, "Entering NeverNote.updateSavedSearchSelection()");
2496                 
2497         menuBar.savedSearchEditAction.setEnabled(true);
2498         menuBar.savedSearchDeleteAction.setEnabled(true);
2499         menuBar.savedSearchIconAction.setEnabled(true);
2500         List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2501
2502         if (selections.size() > 0) {
2503                 menuBar.savedSearchEditAction.setEnabled(true);
2504                 menuBar.savedSearchDeleteAction.setEnabled(true);
2505                 menuBar.savedSearchIconAction.setEnabled(true);
2506                 selectedSavedSearchGUID = selections.get(0).text(1);
2507                 SavedSearch s = conn.getSavedSearchTable().getSavedSearch(selectedSavedSearchGUID);
2508                 searchField.setEditText(s.getQuery());
2509         } else { 
2510                 menuBar.savedSearchEditAction.setEnabled(false);
2511                 menuBar.savedSearchDeleteAction.setEnabled(false);
2512                 menuBar.savedSearchIconAction.setEnabled(false);
2513                 selectedSavedSearchGUID = "";
2514                 searchField.setEditText("");
2515         }
2516         searchFieldChanged();
2517         
2518                 logger.log(logger.HIGH, "Leaving NeverNote.updateSavedSearchSelection()");
2519
2520         
2521     }
2522     // Show/Hide note information
2523         @SuppressWarnings("unused")
2524         private void toggleSavedSearchWindow() {
2525                 logger.log(logger.HIGH, "Entering NeverNote.toggleSavedSearchWindow");
2526         if (savedSearchTree.isVisible())
2527                 savedSearchTree.hide();
2528         else
2529                 savedSearchTree.show();
2530         menuBar.hideSavedSearches.setChecked(savedSearchTree.isVisible());
2531                                 
2532                 Global.saveWindowVisible("savedSearchTree", savedSearchTree.isVisible());
2533         logger.log(logger.HIGH, "Leaving NeverNote.toggleSavedSearchWindow");
2534     }
2535         // Change the icon for a saved search
2536         @SuppressWarnings("unused")
2537         private void setSavedSearchIcon() {
2538                 QTreeWidgetItem currentSelection;
2539                 List<QTreeWidgetItem> selections = savedSearchTree.selectedItems();
2540                 if (selections.size() == 0)
2541                         return;
2542                 
2543                 currentSelection = selections.get(0);   
2544                 String guid = currentSelection.text(1);
2545
2546                 QIcon currentIcon = currentSelection.icon(0);
2547                 QIcon icon = conn.getSavedSearchTable().getIcon(guid);
2548                 SetIcon dialog;
2549                 if (icon == null) {
2550                         dialog = new SetIcon(currentIcon, saveLastPath);
2551                         dialog.setUseDefaultIcon(true);
2552                 } else {
2553                         dialog = new SetIcon(icon, saveLastPath);
2554                         dialog.setUseDefaultIcon(false);
2555                 }
2556                 dialog.exec();
2557                 if (dialog.okPressed()) {
2558                 saveLastPath = dialog.getPath();
2559                         QIcon newIcon = dialog.getIcon();
2560                         conn.getSavedSearchTable().setIcon(guid, newIcon, dialog.getFileType());
2561                         if (newIcon == null) 
2562                                 newIcon = new QIcon(iconPath+"search.png");
2563                         currentSelection.setIcon(0, newIcon);
2564                 }
2565         
2566         }
2567         
2568         
2569         
2570         
2571     //***************************************************************
2572     //***************************************************************
2573     //** These functions deal with Help menu & tool menu items
2574     //***************************************************************
2575     //***************************************************************
2576         // Show database status
2577         @SuppressWarnings("unused")
2578         private void databaseStatus() {
2579                 waitCursor(true);
2580                 indexRunner.interrupt = true;
2581                 int dirty = conn.getNoteTable().getDirtyCount();
2582                 int unindexed = conn.getNoteTable().getUnindexedCount();
2583                 DatabaseStatus status = new DatabaseStatus();
2584                 status.setUnsynchronized(dirty);
2585                 status.setUnindexed(unindexed);
2586                 status.setNoteCount(conn.getNoteTable().getNoteCount());
2587                 status.setNotebookCount(listManager.getNotebookIndex().size());
2588                 status.setUnindexedResourceCount(conn.getNoteTable().noteResourceTable.getUnindexedCount());
2589                 status.setSavedSearchCount(listManager.getSavedSearchIndex().size());
2590                 status.setTagCount(listManager.getTagIndex().size());
2591                 status.setResourceCount(conn.getNoteTable().noteResourceTable.getResourceCount());
2592                 status.setWordCount(conn.getWordsTable().getWordCount());
2593                 waitCursor(false);
2594                 status.exec();
2595         }
2596         // Compact the database
2597         @SuppressWarnings("unused")
2598         private void compactDatabase() {
2599         logger.log(logger.HIGH, "Entering NeverNote.compactDatabase");
2600                 if (QMessageBox.question(this, tr("Confirmation"), tr("This will free unused space in the database, "+
2601                                 "but please be aware that depending upon the size of your database this can be time consuming " +
2602                                 "and NeverNote will be unresponsive until it is complete.  Do you wish to continue?"),
2603                                 QMessageBox.StandardButton.Yes, 
2604                                 QMessageBox.StandardButton.No)==StandardButton.No.value() && Global.verifyDelete() == true) {
2605                                                         return;
2606                 }
2607                 setMessage("Compacting database.");
2608                 waitCursor(true);
2609                 listManager.compactDatabase();
2610                 waitCursor(false);
2611                 setMessage("Database compact is complete.");            
2612         logger.log(logger.HIGH, "Leaving NeverNote.compactDatabase");
2613     }
2614         @SuppressWarnings("unused")
2615         private void accountInformation() {
2616                 logger.log(logger.HIGH, "Entering NeverNote.accountInformation");
2617                 AccountDialog dialog = new AccountDialog();
2618                 dialog.show();
2619                 logger.log(logger.HIGH, "Leaving NeverNote.accountInformation");
2620         }
2621         @SuppressWarnings("unused")
2622         private void releaseNotes() {
2623                 logger.log(logger.HIGH, "Entering NeverNote.releaseNotes");
2624                 QDialog dialog = new QDialog(this);
2625                 QHBoxLayout layout = new QHBoxLayout();
2626                 QTextEdit textBox = new QTextEdit();
2627                 layout.addWidget(textBox);
2628                 textBox.setReadOnly(true);
2629                 QFile file = new QFile(Global.getFileManager().getHomeDirPath("release.txt"));
2630                 if (!file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly,
2631                 QIODevice.OpenModeFlag.Text)))
2632                         return;
2633                 textBox.setText(file.readAll().toString());
2634                 file.close();
2635                 dialog.setWindowTitle(tr("Release Notes"));
2636                 dialog.setLayout(layout);
2637                 dialog.show();
2638                 logger.log(logger.HIGH, "Leaving NeverNote.releaseNotes");
2639         }
2640         // Called when user picks Log from the help menu
2641         @SuppressWarnings("unused")
2642         private void logger() {
2643                 logger.log(logger.HIGH, "Entering NeverNote.logger");
2644                 LogFileDialog dialog = new LogFileDialog(emitLog);
2645                 dialog.exec();
2646                 logger.log(logger.HIGH, "Leaving NeverNote.logger");
2647         }
2648         // Menu option "help/about" was selected
2649         @SuppressWarnings("unused")
2650         private void about() {
2651                 logger.log(logger.HIGH, "Entering NeverNote.about");
2652                 QMessageBox.about(this, 
2653                                                 tr("About NeverNote"),
2654                                                 tr("<h4><center><b>NeverNote</b></center></h4><hr><center>Version ")
2655                                                 +Global.version
2656                                                 +tr("<hr></center>Evernote"
2657                                                                 +"An Open Source Evernote Client.<br><br>" 
2658                                                                 +"Licensed under GPL v2.  <br><hr><br>"
2659                                                                 +"Evernote is copyright 2001-2010 by Evernote Corporation<br>"
2660                                                                 +"Jambi and QT are the licensed trademark of Nokia Corporation<br>"
2661                                                                 +"PDFRenderer is licened under the LGPL<br>"
2662                                                                 +"JTidy is copyrighted under the World Wide Web Consortium<br>"
2663                                                                 +"Apache Common Utilities licensed under the Apache License Version 2.0<br>"
2664                                                                 +"Jazzy is licened under the LGPL<br>"
2665                                                                 +"Java is a registered trademark of Oracle Corporation.<br><hr>"
2666                                                                 +"Special thanks to:<br>BitRock InstallBuilder for the Windows installer"
2667                                                                 +"<br>CodeCogs (www.codecogs.com) for the LaTeX image rendering."));
2668                 logger.log(logger.HIGH, "Leaving NeverNote.about");
2669         }
2670         // Hide the entire left hand side
2671         @SuppressWarnings("unused")
2672         private void toggleLeftSide() {
2673                 boolean hidden;
2674                 
2675                 hidden = !menuBar.hideLeftSide.isChecked();
2676                 menuBar.hideLeftSide.setChecked(!hidden);
2677                 
2678                 if (!hidden) 
2679                         leftSplitter1.setHidden(true);
2680                 else
2681                         leftSplitter1.setHidden(false);
2682                 
2683                 Global.saveWindowVisible("leftPanel", hidden);
2684                 
2685         }
2686         public void checkForUpdates() {