OSDN Git Service

Save/Restore main window state
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / utilities / ListManager.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.utilities;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.Calendar;\r
24 import java.util.Collections;\r
25 import java.util.Comparator;\r
26 import java.util.GregorianCalendar;\r
27 import java.util.HashMap;\r
28 import java.util.List;\r
29 import java.util.Vector;\r
30 \r
31 import com.evernote.edam.type.LinkedNotebook;\r
32 import com.evernote.edam.type.Note;\r
33 import com.evernote.edam.type.Notebook;\r
34 import com.evernote.edam.type.SavedSearch;\r
35 import com.evernote.edam.type.Tag;\r
36 import com.trolltech.qt.QThread;\r
37 import com.trolltech.qt.core.QDateTime;\r
38 import com.trolltech.qt.gui.QImage;\r
39 import com.trolltech.qt.gui.QPixmap;\r
40 import com.trolltech.qt.sql.QSqlQuery;\r
41 import com.trolltech.qt.xml.QDomAttr;\r
42 import com.trolltech.qt.xml.QDomDocument;\r
43 import com.trolltech.qt.xml.QDomElement;\r
44 import com.trolltech.qt.xml.QDomNodeList;\r
45 \r
46 import cx.fbn.nevernote.Global;\r
47 import cx.fbn.nevernote.filters.EnSearch;\r
48 import cx.fbn.nevernote.filters.NotebookCounter;\r
49 import cx.fbn.nevernote.filters.TagCounter;\r
50 import cx.fbn.nevernote.gui.NoteTableModel;\r
51 import cx.fbn.nevernote.signals.NotebookSignal;\r
52 import cx.fbn.nevernote.signals.StatusSignal;\r
53 import cx.fbn.nevernote.signals.TagSignal;\r
54 import cx.fbn.nevernote.signals.ThreadSignal;\r
55 import cx.fbn.nevernote.signals.TrashSignal;\r
56 import cx.fbn.nevernote.sql.DatabaseConnection;\r
57 import cx.fbn.nevernote.threads.CounterRunner;\r
58 import cx.fbn.nevernote.threads.SaveRunner;\r
59 \r
60 \r
61 public class ListManager  {\r
62 \r
63         \r
64         private final ApplicationLogger logger;  \r
65         DatabaseConnection                              conn;\r
66         QSqlQuery                                               deleteWords;\r
67         QSqlQuery                                               insertWords;\r
68         \r
69         private List<Tag>                               tagIndex;\r
70         private List<Notebook>                  notebookIndex;\r
71         private List<Notebook>                  archiveNotebookIndex;\r
72         private List<String>                    localNotebookIndex;\r
73         \r
74         private List<LinkedNotebook>    linkedNotebookIndex;\r
75 \r
76         private List<SavedSearch>               searchIndex;\r
77 \r
78         private List<String>                    selectedNotebooks;\r
79         private final NoteTableModel                    noteModel;\r
80         \r
81         \r
82         private List<String>                    selectedTags;\r
83         private String                                  selectedSearch;\r
84         ThreadSignal                                    signals;\r
85         public StatusSignal                             status;\r
86         private final CounterRunner             notebookCounterRunner;\r
87         private final QThread                   notebookThread;\r
88         private final CounterRunner             tagCounterRunner;\r
89         private final QThread                   tagThread;\r
90         \r
91         private final CounterRunner             trashCounterRunner;\r
92         private final QThread                   trashThread;\r
93         public TrashSignal                              trashSignal;\r
94         \r
95         private List<NotebookCounter>   notebookCounter;                                // count of displayed notes in each notebook\r
96         private List<TagCounter>                tagCounter;                                             // count of displayed notes for each tag\r
97         \r
98         private EnSearch                                enSearch;\r
99         private boolean                                 enSearchChanged;\r
100         public HashMap<String, String>  wordMap;\r
101         public TagSignal                                tagSignal;\r
102         public NotebookSignal                   notebookSignal;\r
103         public boolean                                  refreshCounters;                        // Used to control when to recount lists\r
104         private int                                             trashCount;\r
105     public SaveRunner                           saveRunner;                                     // Thread used to save content.  Used because the xml conversion is slowwwww\r
106     QThread                                                     saveThread;\r
107         \r
108 //    private final HashMap<String, QImage> thumbnailList;\r
109     \r
110         // Constructor\r
111         public ListManager(DatabaseConnection d, ApplicationLogger l) {\r
112                 conn = d;\r
113                 logger = l;\r
114                         \r
115                 conn.getTagTable().cleanupTags();\r
116         status = new StatusSignal();\r
117                 signals = new ThreadSignal();\r
118                 \r
119                 // setup index locks\r
120                 enSearchChanged = false;\r
121                 \r
122                 // Setup arrays\r
123                 noteModel = new NoteTableModel(this);\r
124                 selectedTags = new ArrayList<String>();\r
125 \r
126                 notebookCounter = new ArrayList<NotebookCounter>();\r
127                 tagCounter = new ArrayList<TagCounter>();\r
128                 selectedNotebooks = new ArrayList<String>();\r
129                                 \r
130                 reloadIndexes();\r
131                 \r
132                 notebookSignal = new NotebookSignal();\r
133                 notebookCounterRunner = new CounterRunner("notebook_counter.log", CounterRunner.NOTEBOOK, \r
134                                                 Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),\r
135                                                 Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);\r
136                 notebookCounterRunner.setNoteIndex(getNoteIndex());\r
137                 notebookCounterRunner.notebookSignal.countsChanged.connect(this, "setNotebookCounter(List)");\r
138                 notebookThread = new QThread(notebookCounterRunner, "Notebook Counter Thread");\r
139                 notebookThread.start();\r
140                 \r
141                 tagSignal = new TagSignal();\r
142                 tagCounterRunner = new CounterRunner("tag_counter.log", CounterRunner.TAG, \r
143                                 Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),\r
144                                 Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);\r
145                 tagCounterRunner.setNoteIndex(getNoteIndex());\r
146                 tagCounterRunner.tagSignal.countsChanged.connect(this, "setTagCounter(List)");\r
147                 tagThread = new QThread(tagCounterRunner, "Tag Counter Thread");\r
148                 tagThread.start();\r
149                 \r
150                 trashSignal = new TrashSignal();\r
151                 trashCounterRunner = new CounterRunner("trash_counter.log", CounterRunner.TRASH, \r
152                                 Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),\r
153                                 Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);\r
154                 trashCounterRunner.trashSignal.countChanged.connect(this, "trashSignalReceiver(Integer)");\r
155                 trashThread = new QThread(trashCounterRunner, "Trash Counter Thread");\r
156                 trashThread.start();\r
157 //              reloadTrashCount();\r
158                 \r
159                 wordMap = new HashMap<String, String>();\r
160                 tagSignal = new TagSignal();\r
161                 \r
162                 logger.log(logger.EXTREME, "Setting save thread");\r
163                 saveRunner = new SaveRunner("saveRunner.log", \r
164                                 Global.getDatabaseUrl(), Global.getIndexDatabaseUrl(), Global.getResourceDatabaseUrl(),\r
165                                 Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);\r
166                 saveThread = new QThread(saveRunner, "Save Runner Thread");\r
167                 saveThread.start();\r
168                 \r
169 //              thumbnailList = conn.getNoteTable().getThumbnails();\r
170 //              thumbnailList = new HashMap<String,QImage>();\r
171                 \r
172                 linkedNotebookIndex = conn.getLinkedNotebookTable().getAll();\r
173                 loadNoteTitleColors();\r
174                 refreshCounters = true;\r
175                 refreshCounters();\r
176                                 \r
177         }\r
178         \r
179         public void stop() {\r
180                 saveRunner.addWork("stop", "");\r
181                 tagCounterRunner.release(CounterRunner.EXIT);\r
182                 notebookCounterRunner.release(CounterRunner.EXIT);\r
183                 trashCounterRunner.release(CounterRunner.EXIT);\r
184                 \r
185                 logger.log(logger.MEDIUM, "Waiting for notebookCounterThread to stop");                         \r
186                 try {\r
187                         notebookThread.join();\r
188                 } catch (InterruptedException e) {\r
189                         e.printStackTrace();\r
190                 }\r
191                 \r
192                 logger.log(logger.MEDIUM, "Waiting for tagCounterThread to stop");                      \r
193                 try {\r
194                         tagThread.join();\r
195                 } catch (InterruptedException e) {\r
196                         e.printStackTrace();\r
197                 }\r
198 \r
199                 logger.log(logger.MEDIUM, "Waiting for trashThread to stop");                   \r
200                 try {\r
201                         trashThread.join();\r
202                 } catch (InterruptedException e) {\r
203                         e.printStackTrace();\r
204                 }\r
205 \r
206 \r
207                 logger.log(logger.MEDIUM, "Waiting for saveThread to stop");                    \r
208                 try {\r
209                         saveThread.join(0);\r
210                 } catch (InterruptedException e) {\r
211                         e.printStackTrace();\r
212                 }\r
213 \r
214         }\r
215 \r
216         //***************************************************************\r
217         //***************************************************************\r
218         //* Refresh lists after a db sync\r
219         //***************************************************************\r
220         //***************************************************************\r
221         public void refreshLists(Note n, boolean dirty, String content) {\r
222                 if (dirty) {\r
223 //                      conn.getNoteTable().updateNoteContent(n.getGuid(), n.getContent());\r
224                         saveRunner.addWork(n.getGuid(), content);\r
225                         conn.getNoteTable().updateNoteTitle(n.getGuid(), n.getTitle());\r
226                 }\r
227                 \r
228                 setSavedSearchIndex(conn.getSavedSearchTable().getAll());\r
229                 setTagIndex(conn.getTagTable().getAll());\r
230                 setNotebookIndex(conn.getNotebookTable().getAll());\r
231                 \r
232                 List<Notebook> local = conn.getNotebookTable().getAllLocal();\r
233                 localNotebookIndex = new ArrayList<String>();\r
234                 for (int i=0; i<local.size(); i++)\r
235                         localNotebookIndex.add(local.get(i).getGuid());\r
236                 \r
237                 noteModel.setMasterNoteIndex(conn.getNoteTable().getAllNotes());\r
238                 // For performance reasons, we didn't get the tags for every note individually.  We now need to \r
239                 // get them\r
240                 List<cx.fbn.nevernote.sql.NoteTagsRecord> noteTags = conn.getNoteTable().noteTagsTable.getAllNoteTags();\r
241                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
242                         List<String> tags = new ArrayList<String>();\r
243                         List<String> names = new ArrayList<String>();\r
244                         for (int j=0; j<noteTags.size(); j++) {\r
245                                 if (getMasterNoteIndex().get(i).getGuid().equals(noteTags.get(j).noteGuid)) {\r
246                                         tags.add(noteTags.get(j).tagGuid);\r
247                                         names.add(getTagNameByGuid(noteTags.get(j).tagGuid));\r
248                                 }\r
249                         }\r
250                         \r
251                         getMasterNoteIndex().get(i).setTagGuids(tags);\r
252                         getMasterNoteIndex().get(i).setTagNames(names);\r
253                 }\r
254                 \r
255                 \r
256                 setUnsynchronizedNotes(conn.getNoteTable().getUnsynchronizedGUIDs());\r
257                 \r
258                 linkedNotebookIndex = conn.getLinkedNotebookTable().getAll();\r
259                 \r
260                 enSearchChanged = true;\r
261         }\r
262 \r
263         public void reloadTagIndex() {\r
264                 setTagIndex(conn.getTagTable().getAll());       \r
265         }\r
266         public void reloadIndexes() {\r
267                 setUnsynchronizedNotes(conn.getNoteTable().getUnsynchronizedGUIDs());\r
268 \r
269                 List<Notebook> local = conn.getNotebookTable().getAllLocal();\r
270                 localNotebookIndex = new ArrayList<String>();\r
271                 for (int i=0; i<local.size(); i++)\r
272                         localNotebookIndex.add(local.get(i).getGuid());\r
273                 \r
274                 reloadTagIndex();\r
275                 // Load notebooks\r
276                 setNotebookIndex(conn.getNotebookTable().getAll());\r
277                 // load archived notebooks (if note using the EN interface)\r
278                 setArchiveNotebookIndex(conn.getNotebookTable().getAllArchived());\r
279                 // load saved search index\r
280                 setSavedSearchIndex(conn.getSavedSearchTable().getAll());\r
281                 // Load search helper utility\r
282                 enSearch = new EnSearch(conn,  logger, "", getTagIndex(), Global.getRecognitionWeight());\r
283                 logger.log(logger.HIGH, "Building note index");\r
284 \r
285 //              if (getMasterNoteIndex() == null) { \r
286                         noteModel.setMasterNoteIndex(conn.getNoteTable().getAllNotes());\r
287 //              }\r
288                 // For performance reasons, we didn't get the tags for every note individually.  We now need to \r
289                 // get them\r
290                 List<cx.fbn.nevernote.sql.NoteTagsRecord> noteTags = conn.getNoteTable().noteTagsTable.getAllNoteTags();\r
291                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
292                         List<String> tags = new ArrayList<String>();\r
293                         List<String> names = new ArrayList<String>();\r
294                         for (int j=0; j<noteTags.size(); j++) {\r
295                                 if (getMasterNoteIndex().get(i).getGuid().equals(noteTags.get(j).noteGuid)) {\r
296                                         tags.add(noteTags.get(j).tagGuid);\r
297                                         names.add(getTagNameByGuid(noteTags.get(j).tagGuid));\r
298                                 }\r
299                         }\r
300                         \r
301                         getMasterNoteIndex().get(i).setTagGuids(tags);\r
302                         getMasterNoteIndex().get(i).setTagNames(names);\r
303                 }\r
304                 \r
305                 setNoteIndex(getMasterNoteIndex());\r
306 \r
307         }\r
308         \r
309         //***************************************************************\r
310         //***************************************************************\r
311         //* selected notebooks\r
312         //***************************************************************\r
313         //***************************************************************\r
314         // Return the selected notebook(s)\r
315         public List<String> getSelectedNotebooks() {\r
316                 return selectedNotebooks;\r
317         }\r
318         // Set the current selected notebook(s)\r
319         public void setSelectedNotebooks(List <String> s) {\r
320                 if (s == null) \r
321                         s = new ArrayList<String>();\r
322                 selectedNotebooks = s;\r
323         }\r
324         \r
325                                 \r
326     //***************************************************************\r
327     //***************************************************************\r
328     //** These functions deal with setting & retrieving the master lists\r
329     //***************************************************************\r
330     //***************************************************************\r
331         // Get the note table model\r
332         public NoteTableModel getNoteTableModel() {\r
333                 return noteModel;\r
334         }\r
335         // save the saved search index\r
336         private void setSavedSearchIndex(List<SavedSearch> t) {\r
337                 searchIndex = t;\r
338         }\r
339         // Retrieve the Tag index\r
340         public List<SavedSearch> getSavedSearchIndex() {\r
341                 return searchIndex;\r
342 \r
343         }\r
344         // save the tag index\r
345         private void setTagIndex(List<Tag> t) {\r
346                 tagIndex = t;\r
347         }       \r
348         // Retrieve the Tag index\r
349         public List<Tag> getTagIndex() {\r
350                 return tagIndex;\r
351         }\r
352         private void setNotebookIndex(List<Notebook> t) {\r
353                 notebookIndex = t;\r
354         }\r
355         private void setArchiveNotebookIndex(List<Notebook> t) {\r
356                 archiveNotebookIndex = t;\r
357         }\r
358         // Retrieve the Notebook index\r
359         public List<Notebook> getNotebookIndex() {\r
360                 return notebookIndex;\r
361 \r
362         }\r
363         public List<LinkedNotebook> getLinkedNotebookIndex() {\r
364                 return linkedNotebookIndex;\r
365         }\r
366         public List<Notebook> getArchiveNotebookIndex() {\r
367                 return archiveNotebookIndex;\r
368         }\r
369         // Save the current note list\r
370         private void setNoteIndex(List<Note> n) {\r
371                 noteModel.setNoteIndex(n);\r
372         }\r
373         // Get the note index\r
374         public synchronized List<Note> getNoteIndex() {\r
375                 return noteModel.getNoteIndex();\r
376         }\r
377         // Save the count of notes per notebook\r
378         public void setNotebookCounter(List<NotebookCounter> n) {\r
379                 notebookCounter = n;\r
380                 notebookSignal.refreshNotebookTreeCounts.emit(getNotebookIndex(), notebookCounter);\r
381         }\r
382         public List<NotebookCounter> getNotebookCounter() {\r
383                 return notebookCounter;\r
384         }\r
385         // Save the count of notes for each tag\r
386         public void setTagCounter(List<TagCounter> n) {\r
387                 tagCounter = n;\r
388                 tagSignal.refreshTagTreeCounts.emit(tagCounter);\r
389         }\r
390         public List<TagCounter> getTagCounter() {\r
391                 return tagCounter;\r
392         }\r
393         public List<String> getLocalNotebooks() {\r
394                 return localNotebookIndex;\r
395         }\r
396         // Unsynchronized Note List\r
397         public List<String> getUnsynchronizedNotes() {\r
398                 return noteModel.getUnsynchronizedNotes();\r
399         }\r
400         public void setUnsynchronizedNotes(List<String> l) {\r
401                 noteModel.setUnsynchronizedNotes(l);\r
402         }\r
403         // Return a count of items in the trash\r
404         public int getTrashCount() {\r
405                 return trashCount;\r
406         }\r
407         // get the EnSearch variable\r
408         public EnSearch getEnSearch() {\r
409                 return enSearch;\r
410         }\r
411         public List<Note> getMasterNoteIndex() {\r
412                 return noteModel.getMasterNoteIndex();\r
413         }\r
414         // Thumbnails\r
415 //      public HashMap<String, QImage> getThumbnails() {\r
416 //              return thumbnailList;\r
417 //      }\r
418         public QImage getThumbnail(String guid) {\r
419 //              if (getThumbnails().containsKey(guid))\r
420 //                      return getThumbnails().get(guid);\r
421                 \r
422                 QImage img = new QImage();\r
423                 img = QImage.fromData(conn.getNoteTable().getThumbnail(guid));\r
424                 if (img == null || img.isNull()) \r
425                         return null;\r
426                 //getThumbnails().put(guid, img);\r
427                 return img;\r
428         }\r
429         public QPixmap getThumbnailPixmap(String guid) {\r
430 //              if (getThumbnails().containsKey(guid))\r
431 //                      return getThumbnails().get(guid);\r
432                 \r
433                 QPixmap img = new QPixmap();\r
434                 img.loadFromData(conn.getNoteTable().getThumbnail(guid));\r
435                 if (img == null || img.isNull()) \r
436                         return null;\r
437                 //getThumbnails().put(guid, img);\r
438                 return img;\r
439         }\r
440     //***************************************************************\r
441     //***************************************************************\r
442     //** These functions deal with setting & retrieving filters\r
443     //***************************************************************\r
444     //***************************************************************\r
445         public void setEnSearch(String t) {\r
446                 enSearch = new EnSearch(conn,logger, t, getTagIndex(), Global.getRecognitionWeight());\r
447                 enSearchChanged = true;\r
448         }\r
449         // Save search tags\r
450         public void setSelectedTags(List<String> selectedTags) {\r
451                 this.selectedTags = selectedTags;\r
452         }\r
453         // Save seleceted search\r
454         public void setSelectedSavedSearch(String s) {\r
455                 this.selectedSearch = s;\r
456         }\r
457         // Get search tags\r
458         public List<String> getSelectedTags() {\r
459                 return selectedTags;\r
460         }\r
461         // Get saved search\r
462         public String getSelectedSearch() {\r
463                 return selectedSearch;\r
464         }\r
465         \r
466         \r
467         \r
468         \r
469     //***************************************************************\r
470     //***************************************************************\r
471     //** Note functions\r
472     //***************************************************************\r
473     //***************************************************************\r
474         // Save Note Tags\r
475         public void saveNoteTags(String noteGuid, List<String> tags) {\r
476                 logger.log(logger.HIGH, "Entering ListManager.saveNoteTags");\r
477                 String tagName;\r
478                 conn.getNoteTable().noteTagsTable.deleteNoteTag(noteGuid);\r
479                 List<String> tagGuids = new ArrayList<String>();\r
480                 boolean newTagCreated = false;\r
481                 \r
482                 for (int i=0; i<tags.size(); i++) {\r
483                         tagName = tags.get(i);\r
484                         boolean found = false;\r
485                         for (int j=0; j<tagIndex.size(); j++) {\r
486                                 if (tagIndex.get(j).getName().equalsIgnoreCase(tagName)) {\r
487                                         conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, tagIndex.get(j).getGuid());\r
488                                         tagGuids.add(tagIndex.get(j).getGuid());\r
489                                         j=tagIndex.size()+1;\r
490                                         found = true;\r
491                                 }\r
492                         }\r
493                         if (!found) {\r
494                                 Tag nTag = new Tag();\r
495                                 nTag.setName(tagName);\r
496                                 Calendar currentTime = new GregorianCalendar();\r
497                                 Long l = new Long(currentTime.getTimeInMillis());\r
498                                 long prevTime = l;\r
499                                 while (l==prevTime) {\r
500                                         currentTime = new GregorianCalendar();\r
501                                         l=currentTime.getTimeInMillis();\r
502                                 }\r
503                                 String randint = new String(Long.toString(l));\r
504                         \r
505                                 nTag.setUpdateSequenceNum(0);\r
506                                 nTag.setGuid(randint);\r
507                                 conn.getTagTable().addTag(nTag, true);\r
508                                 getTagIndex().add(nTag);\r
509                                 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, nTag.getGuid());\r
510                                 tagGuids.add(nTag.getGuid());\r
511                                 newTagCreated = true;\r
512                         }\r
513                 }\r
514                 \r
515                 for (int i=0; i<getNoteIndex().size(); i++) {\r
516                         if (getNoteIndex().get(i).getGuid().equals(noteGuid)) {\r
517                                 getNoteIndex().get(i).setTagNames(tags);\r
518                                 getNoteIndex().get(i).setTagGuids(tagGuids);\r
519                                 i=getNoteIndex().size()+1;\r
520                         }\r
521                 }\r
522                 if (newTagCreated)\r
523                         tagSignal.listChanged.emit();\r
524                 logger.log(logger.HIGH, "Leaving ListManager.saveNoteTags");\r
525         }\r
526         // Delete a note\r
527         public void deleteNote(String guid) {\r
528                 trashCounterRunner.abortCount = true;\r
529                 Calendar currentTime = new GregorianCalendar();\r
530                 Long l = new Long(currentTime.getTimeInMillis());\r
531                 long prevTime = l;\r
532                 while (l==prevTime) {\r
533                         currentTime = new GregorianCalendar();\r
534                         l=currentTime.getTimeInMillis();\r
535                 }\r
536                 \r
537                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
538                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
539                                 getMasterNoteIndex().get(i).setActive(false);\r
540                                 getMasterNoteIndex().get(i).setDeleted(l);\r
541                                 i=getMasterNoteIndex().size();\r
542                         }\r
543                 }\r
544                 for (int i=0; i<getNoteIndex().size(); i++) {\r
545                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
546                                 getNoteIndex().get(i).setActive(false);\r
547                                 getNoteIndex().get(i).setDeleted(l);\r
548                                 i=getNoteIndex().size();\r
549                         }\r
550                 }\r
551                 conn.getNoteTable().deleteNote(guid);\r
552                 reloadTrashCount();\r
553         }\r
554         // Delete a note\r
555         public void restoreNote(String guid) {\r
556                 trashCounterRunner.abortCount = true;\r
557                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
558                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
559                                 getMasterNoteIndex().get(i).setActive(true);\r
560                                 getMasterNoteIndex().get(i).setDeleted(0);\r
561                                 i=getMasterNoteIndex().size();\r
562                         }\r
563                 }\r
564                 for (int i=0; i<getNoteIndex().size(); i++) {\r
565                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
566                                 getNoteIndex().get(i).setActive(true);\r
567                                 getNoteIndex().get(i).setDeleted(0);\r
568                                 i=getNoteIndex().size();\r
569                         }\r
570                 }\r
571                 conn.getNoteTable().restoreNote(guid);\r
572                 reloadTrashCount();\r
573         }\r
574         public void updateNote(Note n) {\r
575                 \r
576                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
577                         if (getMasterNoteIndex().get(i).getGuid().equals(n.getGuid())) {\r
578                                 getMasterNoteIndex().remove(i);\r
579                                 getMasterNoteIndex().add(n);\r
580                         }\r
581                 }\r
582                 for (int i=0; i<getNoteIndex().size(); i++) {\r
583                         if (getNoteIndex().get(i).getGuid().equals(n.getGuid())) {\r
584                                 getNoteIndex().get(i).setActive(true);\r
585                                 getNoteIndex().get(i).setDeleted(0);\r
586                                 i=getNoteIndex().size();\r
587                         }\r
588                 }\r
589                 conn.getNoteTable().updateNote(n, true);\r
590         }\r
591         // Add a note.  \r
592         public void addNote(Note n) {\r
593                 noteModel.addNote(n);\r
594         }\r
595         // Expunge a note\r
596         public void expungeNote(String guid) {\r
597                 trashCounterRunner.abortCount = true;\r
598                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
599                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
600                                 getMasterNoteIndex().remove(i);\r
601                                 i=getMasterNoteIndex().size();\r
602                         }\r
603                 }\r
604                 for (int i=0; i<getNoteIndex().size(); i++) {\r
605                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
606                                 getNoteIndex().remove(i);\r
607                                 i=getNoteIndex().size();\r
608                         }\r
609                 }\r
610                 conn.getNoteTable().expungeNote(guid, false, true);\r
611                 reloadTrashCount();\r
612         }\r
613         // Expunge a note\r
614         public void emptyTrash() {\r
615                 trashCounterRunner.abortCount = true;           \r
616                 for (int i=getMasterNoteIndex().size()-1; i>=0; i--) {\r
617                         if (!getMasterNoteIndex().get(i).isActive()) {\r
618                                 getMasterNoteIndex().remove(i);\r
619                         }\r
620                 }\r
621                 \r
622                 for (int i=getNoteIndex().size()-1; i>=0; i--) {\r
623                         if (!getNoteIndex().get(i).isActive()) {\r
624                                 getNoteIndex().remove(i);\r
625                         } \r
626                 }\r
627 \r
628                 conn.getNoteTable().expungeAllDeletedNotes();\r
629                 reloadTrashCount();\r
630         }\r
631         // The trash counter thread has produced a result\r
632         @SuppressWarnings("unused")\r
633         private void trashSignalReceiver(Integer i) {\r
634                 trashCount = i;\r
635                 trashSignal.countChanged.emit(i);\r
636         }\r
637         // Update note contents\r
638         public void updateNoteContent(String guid, String content) {\r
639                 logger.log(logger.HIGH, "Entering ListManager.updateNoteContent");\r
640 //              EnmlConverter enml = new EnmlConverter(logger);\r
641 //              String text = enml.convert(guid, content);\r
642                 \r
643                 // Update the list tables \r
644 /*              for (int i=0; i<masterNoteIndex.size(); i++) {\r
645                         if (masterNoteIndex.get(i).getGuid().equals(guid)) {\r
646                                 masterNoteIndex.get(i).setContent(text);\r
647                                 i = masterNoteIndex.size();\r
648                         }\r
649                 }\r
650                 // Update the list tables \r
651                 for (int i=0; i<getNoteIndex().size(); i++) {\r
652                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
653                                 getNoteIndex().get(i).setContent(text);\r
654                                 i = getNoteIndex().size();\r
655                         }\r
656                 }\r
657 */              \r
658                 // Check if any new tags were encountered\r
659 /*              if (enml.saveInvalidXML) {\r
660                         List<String> elements = Global.invalidElements;\r
661                         for (int i=0; i<elements.size(); i++) {\r
662                                 conn.getInvalidXMLTable().addInvalidElement(elements.get(i));\r
663                         }\r
664                         for (String key : Global.invalidAttributes.keySet()) {\r
665                                 ArrayList<String> attributes = Global.invalidAttributes.get(key);\r
666                                 for (int i=0; i<attributes.size(); i++) {\r
667                                         conn.getInvalidXMLTable().addInvalidAttribute(key, attributes.get(i));\r
668                                 }\r
669                         }\r
670                 }\r
671 */\r
672                 saveRunner.addWork(guid, content);\r
673 //              conn.getNoteTable().updateNoteContent(guid, content);\r
674                 logger.log(logger.HIGH, "Leaving ListManager.updateNoteContent");\r
675         }\r
676         // Update a note creation date\r
677         public void updateNoteCreatedDate(String guid, QDateTime date) {\r
678                 noteModel.updateNoteCreatedDate(guid, date);\r
679                 conn.getNoteTable().updateNoteCreatedDate(guid, date);\r
680         }\r
681         // Subject date has been changed\r
682         public void updateNoteSubjectDate(String guid, QDateTime date) {\r
683                 noteModel.updateNoteSubjectDate(guid, date);\r
684                 conn.getNoteTable().updateNoteSubjectDate(guid, date);\r
685         }\r
686         // Author has changed\r
687         public void updateNoteAuthor(String guid, String author) {\r
688                 noteModel.updateNoteAuthor(guid, author);\r
689                 conn.getNoteTable().updateNoteAuthor(guid, author);\r
690         }\r
691         // Author has changed\r
692         public void updateNoteGeoTag(String guid, Double lon, Double lat, Double alt) {\r
693                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
694                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
695                                 getMasterNoteIndex().get(i).getAttributes().setLongitude(lon);\r
696                                 getMasterNoteIndex().get(i).getAttributes().setLongitudeIsSet(true);\r
697                                 getMasterNoteIndex().get(i).getAttributes().setLatitude(lat);\r
698                                 getMasterNoteIndex().get(i).getAttributes().setLatitudeIsSet(true);\r
699                                 getMasterNoteIndex().get(i).getAttributes().setAltitude(alt);\r
700                                 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);\r
701                                 i = getMasterNoteIndex().size();\r
702                         }       \r
703                 }\r
704                 // Update the list tables \r
705                 for (int i=0; i<getNoteIndex().size(); i++) {\r
706                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
707                                 getNoteIndex().get(i).getAttributes().setLongitude(lon);\r
708                                 getNoteIndex().get(i).getAttributes().setLongitudeIsSet(true);\r
709                                 getNoteIndex().get(i).getAttributes().setLatitude(lat);\r
710                                 getNoteIndex().get(i).getAttributes().setLatitudeIsSet(true);\r
711                                 getNoteIndex().get(i).getAttributes().setAltitude(alt);\r
712                                 getNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);\r
713                                 i = getNoteIndex().size();\r
714                         }\r
715                 }\r
716                 conn.getNoteTable().updateNoteGeoTags(guid, lon, lat, alt);\r
717         }\r
718         // Source URL changed\r
719         public void updateNoteSourceUrl(String guid, String url) {\r
720                 noteModel.updateNoteSourceUrl(guid, url);\r
721                 conn.getNoteTable().updateNoteSourceUrl(guid, url);\r
722         }\r
723         // Update a note last changed date\r
724         public void updateNoteAlteredDate(String guid, QDateTime date) {\r
725                 noteModel.updateNoteChangedDate(guid, date);\r
726                 conn.getNoteTable().updateNoteAlteredDate(guid, date);\r
727         }\r
728         // Update a note title\r
729         public void updateNoteTitle(String guid, String title) {\r
730                 logger.log(logger.HIGH, "Entering ListManager.updateNoteTitle");\r
731                 conn.getNoteTable().updateNoteTitle(guid, title);\r
732                 noteModel.updateNoteTitle(guid, title);\r
733                 logger.log(logger.HIGH, "Leaving ListManager.updateNoteTitle");\r
734         }\r
735         // Update a note's notebook\r
736         public void updateNoteNotebook(String guid, String notebookGuid) {\r
737                 logger.log(logger.HIGH, "Entering ListManager.updateNoteNotebook");\r
738                 noteModel.updateNoteNotebook(guid, notebookGuid);\r
739                 conn.getNoteTable().updateNoteNotebook(guid, notebookGuid, true);\r
740                 logger.log(logger.HIGH, "Leaving ListManager.updateNoteNotebook");\r
741         }\r
742         // Update a note sequence number\r
743         public void updateNoteSequence(String guid, int sequence) {\r
744                 logger.log(logger.HIGH, "Entering ListManager.updateNoteSequence");\r
745 \r
746                 conn.getNoteTable().updateNoteSequence(guid, sequence);\r
747                 \r
748                 for (int i=0; i<noteModel.getMasterNoteIndex().size(); i++) {\r
749                         if (noteModel.getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
750                                 noteModel.getMasterNoteIndex().get(i).setUpdateSequenceNum(sequence);\r
751                                 i=noteModel.getMasterNoteIndex().size()+1;\r
752                         }\r
753                 }\r
754                 \r
755                 for (int i=0; i<getNoteIndex().size(); i++) {\r
756                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
757                                 getNoteIndex().get(i).setUpdateSequenceNum(sequence);\r
758                                 i=getNoteIndex().size()+1;\r
759                         }\r
760                 }\r
761                 logger.log(logger.HIGH, "Leaving ListManager.updateNoteSequence");\r
762         }\r
763         public void updateNoteGuid(String oldGuid, String newGuid, boolean updateDatabase) {\r
764                 logger.log(logger.HIGH, "Entering ListManager.updateNoteGuid");\r
765                 if (updateDatabase) \r
766                         conn.getNoteTable().updateNoteGuid(oldGuid, newGuid);\r
767                 noteModel.updateNoteGuid(oldGuid, newGuid);\r
768                 logger.log(logger.HIGH, "Leaving ListManager.updateNoteGuid");\r
769 \r
770         }\r
771 \r
772         \r
773         //************************************************************************************\r
774         //************************************************************************************\r
775         //**  Tag functions\r
776         //************************************************************************************\r
777         //************************************************************************************  \r
778         // Update a tag sequence number\r
779         public void updateTagSequence(String guid, int sequence) {\r
780                 logger.log(logger.HIGH, "Entering ListManager.updateTagSequence");\r
781 \r
782                 conn.getTagTable().updateTagSequence(guid, sequence);   \r
783                 for (int i=0; i<tagIndex.size(); i++) {\r
784                         if (tagIndex.get(i).getGuid().equals(guid)) {\r
785                                 getTagIndex().get(i).setUpdateSequenceNum(sequence);\r
786                                 i=tagIndex.size()+1;\r
787                         }\r
788                 }\r
789                 logger.log(logger.HIGH, "Leaving ListManager.updateTagSequence");\r
790         }\r
791         // Update a tag guid number\r
792         public void updateTagGuid(String oldGuid, String newGuid) {\r
793                 logger.log(logger.HIGH, "Entering ListManager.updateTagGuid");\r
794 \r
795                 conn.getTagTable().updateTagGuid(oldGuid, newGuid);     \r
796                 for (int i=0; i<tagIndex.size(); i++) {\r
797                         if (tagIndex.get(i).getGuid().equals(oldGuid)) {\r
798                                 tagIndex.get(i).setGuid(newGuid);\r
799                                 i=tagIndex.size()+1;\r
800                         }\r
801                 }\r
802                 logger.log(logger.HIGH, "Leaving ListManager.updateTagGuid");\r
803 \r
804         }\r
805 \r
806 \r
807         //************************************************************************************\r
808         //************************************************************************************\r
809         //**  Notebook functions\r
810         //************************************************************************************\r
811         //************************************************************************************  \r
812         // Delete a notebook\r
813         public void deleteNotebook(String guid) {\r
814                 for (int i=0; i<getNotebookIndex().size(); i++) {\r
815                         if (getNotebookIndex().get(i).getGuid().equals(guid)) {\r
816                                 getNotebookIndex().remove(i);\r
817                                 i=getMasterNoteIndex().size();\r
818                         }\r
819                 }\r
820                 conn.getNotebookTable().expungeNotebook(guid, true);            \r
821         }\r
822         // Rename a stack\r
823         public void renameStack(String oldName, String newName) {\r
824                 for (int i=0; i<getNotebookIndex().size(); i++) {\r
825                         if (getNotebookIndex().get(i).getStack() != null && \r
826                                         getNotebookIndex().get(i).getStack().equalsIgnoreCase(oldName)) {\r
827                                 getNotebookIndex().get(i).setStack(newName);\r
828                         }\r
829                 }       \r
830         }\r
831         // Update a notebook sequence number\r
832         public void updateNotebookSequence(String guid, int sequence) {\r
833                 logger.log(logger.HIGH, "Entering ListManager.updateNotebookSequence");\r
834 \r
835                 conn.getNotebookTable().updateNotebookSequence(guid, sequence);\r
836                 \r
837                 for (int i=0; i<notebookIndex.size(); i++) {\r
838                         if (notebookIndex.get(i).getGuid().equals(guid)) {\r
839                                 notebookIndex.get(i).setUpdateSequenceNum(sequence);\r
840                                 i=notebookIndex.size()+1;\r
841                         }\r
842                 }\r
843                 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookSequence");\r
844 \r
845         }\r
846         // Update a notebook Guid number\r
847         public void updateNotebookGuid(String oldGuid, String newGuid) {\r
848                 logger.log(logger.HIGH, "Entering ListManager.updateNotebookGuid");\r
849 \r
850                 conn.getNotebookTable().updateNotebookGuid(oldGuid, newGuid);\r
851                 \r
852                 for (int i=0; i<notebookIndex.size(); i++) {\r
853                         if (notebookIndex.get(i).getGuid().equals(oldGuid)) {\r
854                                 notebookIndex.get(i).setGuid(newGuid);\r
855                                 i=notebookIndex.size()+1;\r
856                         }\r
857                 }\r
858                 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookGuid");\r
859 \r
860         }\r
861         // Update a notebook Guid number\r
862         public void updateNotebookStack(String oldGuid, String stack) {\r
863                 logger.log(logger.HIGH, "Entering ListManager.updateNotebookGuid");\r
864 \r
865                 conn.getNotebookTable().setStack(oldGuid, stack);\r
866                 \r
867                 for (int i=0; i<notebookIndex.size(); i++) {\r
868                         if (notebookIndex.get(i).getGuid().equals(oldGuid)) {\r
869                                 notebookIndex.get(i).setStack(stack);\r
870                                 i=notebookIndex.size()+1;\r
871                         }\r
872                 }\r
873                 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookGuid");\r
874 \r
875         }\r
876         \r
877         \r
878         //************************************************************************************\r
879         //************************************************************************************\r
880         //**  Load and filter the note index\r
881         //************************************************************************************\r
882         //************************************************************************************\r
883         \r
884         public void noteDownloaded(Note n) {\r
885                 boolean found = false;\r
886                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
887                         if (getMasterNoteIndex().get(i).getGuid().equals(n.getGuid())) {\r
888                                 getMasterNoteIndex().set(i,n);\r
889                                 found = true;\r
890                                 i=getMasterNoteIndex().size();\r
891                         }\r
892                 }\r
893                 \r
894                 if (!found)\r
895                         getMasterNoteIndex().add(n);\r
896                 \r
897                 for (int i=0; i<getNoteIndex().size(); i++) {\r
898                         if (getNoteIndex().get(i).getGuid().equals(n.getGuid())) {\r
899                                 if (filterRecord(getNoteIndex().get(i)))\r
900                                         getNoteIndex().add(n);\r
901                                 getNoteIndex().remove(i);\r
902                                 i=getNoteIndex().size();\r
903                         }\r
904                 }\r
905                 \r
906                 if (filterRecord(n))\r
907                         getNoteIndex().add(n);\r
908                 \r
909         }\r
910         // Check if a note matches the currently selected notebooks, tags, or attribute searches.\r
911         public boolean filterRecord(Note n) {\r
912                                 \r
913                 boolean goodNotebook = false;\r
914                 boolean goodTag = false;\r
915                 boolean goodStatus = false;\r
916                         \r
917                 // Check note status\r
918                 if (!n.isActive() && Global.showDeleted)\r
919                         return true;\r
920                 else {\r
921                         if (n.isActive() && !Global.showDeleted)\r
922                                 goodStatus = true;\r
923                 }\r
924                 \r
925                 // Begin filtering results\r
926                 if (goodStatus)\r
927                         goodNotebook = filterByNotebook(n.getNotebookGuid());\r
928                 if (goodNotebook) \r
929                         goodTag = filterByTag(n.getTagGuids());\r
930                 if (goodTag) {\r
931                         boolean goodCreatedBefore = false;\r
932                         boolean goodCreatedSince = false;\r
933                         boolean goodChangedBefore = false;\r
934                         boolean goodChangedSince = false;\r
935                         boolean goodContains = false;\r
936                         if (!Global.createdBeforeFilter.hasSelection())\r
937                                 goodCreatedBefore = true;\r
938                         else\r
939                                 goodCreatedBefore = Global.createdBeforeFilter.check(n);\r
940                                 \r
941                         if (!Global.createdSinceFilter.hasSelection())\r
942                                 goodCreatedSince = true;\r
943                         else\r
944                                 goodCreatedSince = Global.createdSinceFilter.check(n);\r
945                                 \r
946                         if (!Global.changedBeforeFilter.hasSelection())\r
947                                 goodChangedBefore = true;\r
948                         else\r
949                                 goodChangedBefore = Global.changedBeforeFilter.check(n);\r
950                                 if (!Global.changedSinceFilter.hasSelection())\r
951                                 goodChangedSince = true;\r
952                         else\r
953                                 goodChangedSince = Global.changedSinceFilter.check(n);\r
954                         if (!Global.containsFilter.hasSelection())\r
955                                 goodContains = true;\r
956                         else\r
957                                 goodContains = Global.containsFilter.check(conn.getNoteTable(), n);\r
958                                 \r
959                         if (goodCreatedSince && goodCreatedBefore && goodChangedSince && goodChangedBefore && goodContains)\r
960                                 return true;\r
961                 }       \r
962                 return false;\r
963         }\r
964         \r
965         // Trigger a recount of counters\r
966         public void refreshCounters() {\r
967 //              refreshCounters= false;\r
968                 if (!refreshCounters)\r
969                         return;\r
970                 refreshCounters = false;\r
971                 tagCounterRunner.abortCount = true;\r
972                 notebookCounterRunner.abortCount = true;\r
973                 trashCounterRunner.abortCount = true;\r
974                 countNotebookResults(getNoteIndex());\r
975                 countTagResults(getNoteIndex());\r
976                 reloadTrashCount();\r
977 \r
978         }\r
979         // Load the note index based upon what the user wants.\r
980         public void loadNotesIndex() {\r
981                 logger.log(logger.EXTREME, "Entering ListManager.loadNotesIndex()");\r
982                 \r
983                 List<Note> matches;\r
984                 if (enSearchChanged || getMasterNoteIndex() == null)\r
985                         matches = enSearch.matchWords();\r
986                 else\r
987                         matches = getMasterNoteIndex();\r
988                 \r
989                 if (matches == null)\r
990                         matches = getMasterNoteIndex();\r
991                 \r
992                 setNoteIndex(new ArrayList<Note>());\r
993                 for (int i=0; i<matches.size(); i++) {\r
994                         if (filterRecord(matches.get(i)))\r
995                                 getNoteIndex().add(matches.get(i));\r
996                 }\r
997                 refreshCounters = true;\r
998                 enSearchChanged = false;\r
999                 logger.log(logger.EXTREME, "Leaving ListManager.loadNotesIndex()");\r
1000         }\r
1001         public void countNotebookResults(List<Note> index) {\r
1002                 logger.log(logger.EXTREME, "Entering ListManager.countNotebookResults()");\r
1003                 notebookCounterRunner.abortCount = true;\r
1004                 if (!Global.mimicEvernoteInterface) \r
1005                         notebookCounterRunner.setNoteIndex(index);\r
1006                 else \r
1007                         notebookCounterRunner.setNoteIndex(getMasterNoteIndex());\r
1008                 notebookCounterRunner.release(CounterRunner.NOTEBOOK);\r
1009                 logger.log(logger.EXTREME, "Leaving ListManager.countNotebookResults()");\r
1010         }\r
1011         public void countTagResults(List<Note> index) {\r
1012                 logger.log(logger.EXTREME, "Entering ListManager.countTagResults");\r
1013                 trashCounterRunner.abortCount = true;\r
1014                 if (!Global.tagBehavior().equalsIgnoreCase("DoNothing")) \r
1015                         tagCounterRunner.setNoteIndex(index);\r
1016                 else\r
1017                         tagCounterRunner.setNoteIndex(getMasterNoteIndex());\r
1018                 tagCounterRunner.release(CounterRunner.TAG);\r
1019                 logger.log(logger.EXTREME, "Leaving ListManager.countTagResults()");\r
1020         }\r
1021         // Update the count of items in the trash\r
1022         public void reloadTrashCount() {\r
1023                 logger.log(logger.EXTREME, "Entering ListManager.reloadTrashCount");\r
1024                 trashCounterRunner.abortCount = true;\r
1025                 trashCounterRunner.setNoteIndex(getNoteIndex());\r
1026                 trashCounterRunner.release(CounterRunner.TRASH);\r
1027                 logger.log(logger.EXTREME, "Leaving ListManager.reloadTrashCount");\r
1028         }       \r
1029         \r
1030         private boolean filterByNotebook(String guid) {\r
1031                 boolean good = false;\r
1032                 if (selectedNotebooks.size() == 0)\r
1033                         good = true;\r
1034                 if (!good && selectedNotebooks.contains(guid)) \r
1035                         good = true;\r
1036 \r
1037                 for (int i=0; i<getArchiveNotebookIndex().size() && good; i++) {\r
1038                         if (guid.equals(getArchiveNotebookIndex().get(i).getGuid())) {\r
1039                                 good = false;\r
1040                                 return good;\r
1041                         }\r
1042                 }\r
1043                 return good;\r
1044         }\r
1045         private boolean filterByTag(List<String> noteTags) {\r
1046                 if (noteTags == null || selectedTags == null)\r
1047                         return true;\r
1048                 \r
1049                 if (selectedTags.size() == 0) \r
1050                         return true;\r
1051                 \r
1052                 if (!Global.anyTagSelectionMatch()) {\r
1053                         for (int i=0; i<selectedTags.size(); i++) {\r
1054                                 String selectedGuid = selectedTags.get(i);\r
1055                                 if (!noteTags.contains(selectedGuid))\r
1056                                         return false;\r
1057                         }\r
1058                         return true;\r
1059                 } else {\r
1060                         for (int i=0; i<selectedTags.size(); i++) {\r
1061                                 String selectedGuid = selectedTags.get(i);\r
1062                                 if (noteTags.contains(selectedGuid))\r
1063                                                 return true;\r
1064                         }\r
1065                         return false;\r
1066                 }\r
1067         }\r
1068 \r
1069         public void setNoteSynchronized(String guid, boolean value) {\r
1070                 getNoteTableModel().updateNoteSyncStatus(guid, value);\r
1071         }\r
1072         \r
1073         public void updateNoteTitleColor(String guid, Integer color) {\r
1074                 noteModel.updateNoteTitleColor(guid, color);\r
1075                 conn.getNoteTable().setNoteTitleColor(guid, color);\r
1076         }\r
1077         public void loadNoteTitleColors() {\r
1078                 List<Pair<String,Integer>> colors = conn.getNoteTable().getNoteTitleColors();\r
1079                 if (noteModel.getTitleColors() == null)\r
1080                         noteModel.setTitleColors(new HashMap<String,Integer>());\r
1081                 else\r
1082                         noteModel.getTitleColors().clear();\r
1083                 for (int i=0; i<colors.size(); i++) {\r
1084                         noteModel.getTitleColors().put(colors.get(i).getFirst(), colors.get(i).getSecond());\r
1085                 }\r
1086         }\r
1087         \r
1088         //********************************************************************************\r
1089         //********************************************************************************\r
1090         //* Support signals from the index thread\r
1091         //********************************************************************************\r
1092         //********************************************************************************\r
1093         // Reset a flag if an index is needed\r
1094         public void setIndexNeeded(String guid, String type, Boolean b) {\r
1095                 if (Global.keepRunning && type.equalsIgnoreCase("content"))\r
1096                         conn.getNoteTable().setIndexNeeded(guid, false);\r
1097                 if (Global.keepRunning && type.equalsIgnoreCase("resource")) {\r
1098                         conn.getNoteTable().noteResourceTable.setIndexNeeded(guid, b);\r
1099                 }\r
1100         }\r
1101         \r
1102         public boolean threadCheck(int id) {\r
1103                 if (id == Global.notebookCounterThreadId) \r
1104                         return notebookThread.isAlive();\r
1105                 if (id == Global.tagCounterThreadId) \r
1106                         return tagThread.isAlive();\r
1107                 if (id == Global.trashCounterThreadId) \r
1108                         return trashThread.isAlive();\r
1109                 if (id == Global.saveThreadId) \r
1110                         return saveThread.isAlive();\r
1111                 return false;\r
1112         }\r
1113         \r
1114         \r
1115         \r
1116         //********************************************************************************\r
1117         //********************************************************************************\r
1118         //* Utility Functions\r
1119         //********************************************************************************\r
1120         //********************************************************************************\r
1121         public void compactDatabase() {\r
1122                 conn.compactDatabase();\r
1123 //              IndexConnection idx = new IndexConnection(logger, "nevernote-compact");\r
1124 //              idx.dbSetup();\r
1125 //              idx.dbShutdown();\r
1126         }\r
1127 \r
1128         // Rebuild the note HTML to something usable\r
1129         public List<String> scanNoteForResources(Note n) {\r
1130                 logger.log(logger.HIGH, "Entering ListManager.scanNoteForResources");\r
1131                 logger.log(logger.EXTREME, "Note guid: " +n.getGuid());\r
1132                 QDomDocument doc = new QDomDocument();\r
1133                 QDomDocument.Result result = doc.setContent(n.getContent());\r
1134                 if (!result.success) {\r
1135                         logger.log(logger.MEDIUM, "Parse error when scanning note for resources.");\r
1136                         logger.log(logger.MEDIUM, "Note guid: " +n.getGuid());\r
1137                         return null;\r
1138                 }\r
1139                                 \r
1140                 List<String> returnArray = new ArrayList<String>();\r
1141                 QDomNodeList anchors = doc.elementsByTagName("en-media");\r
1142                 for (int i=0; i<anchors.length(); i++) {\r
1143                         QDomElement enmedia = anchors.at(i).toElement();\r
1144                         if (enmedia.hasAttribute("type")) {\r
1145                                 QDomAttr hash = enmedia.attributeNode("hash");\r
1146                                 returnArray.add(hash.value().toString());\r
1147                         }\r
1148                 }\r
1149                 logger.log(logger.HIGH, "Leaving ListManager.scanNoteForResources");\r
1150                 return returnArray;\r
1151         }\r
1152         // Given a list of tags, produce a string list of tag names\r
1153         public String getTagNamesForNote(Note n) {\r
1154                 StringBuffer buffer = new StringBuffer(100);\r
1155                 Vector<String> v = new Vector<String>();\r
1156                 List<String> guids = n.getTagGuids();\r
1157                 \r
1158                 if (guids == null) \r
1159                         return "";\r
1160                 \r
1161                 for (int i=0; i<guids.size(); i++) {\r
1162                         v.add(getTagNameByGuid(guids.get(i)));\r
1163                 }\r
1164                 Comparator<String> comparator = Collections.reverseOrder();\r
1165                 Collections.sort(v,comparator);\r
1166                 Collections.reverse(v);\r
1167                 \r
1168                 for (int i = 0; i<v.size(); i++) {\r
1169                         if (i>0) \r
1170                                 buffer.append(", ");\r
1171                         buffer.append(v.get(i));\r
1172                 }\r
1173                 \r
1174                 return buffer.toString();\r
1175         }\r
1176         // Get a tag name when given a tag guid\r
1177         public String getTagNameByGuid(String guid) {\r
1178                 for (int i=0; i<getTagIndex().size(); i++) {\r
1179                         String s = getTagIndex().get(i).getGuid();\r
1180                         if (s.equals(guid)) { \r
1181                                 return getTagIndex().get(i).getName();\r
1182                         }\r
1183                 }\r
1184                 return "";\r
1185         }\r
1186         // For a notebook guid, return the name\r
1187         public String getNotebookNameByGuid(String guid) {\r
1188                 if (notebookIndex == null)\r
1189                         return null;\r
1190                 for (int i=0; i<notebookIndex.size(); i++) {\r
1191                         String s = notebookIndex.get(i).getGuid();\r
1192                         if (s.equals(guid)) { \r
1193                                 return notebookIndex.get(i).getName();\r
1194                         }\r
1195                 }\r
1196                 return "";\r
1197         }\r
1198         \r
1199         \r
1200 }\r