OSDN Git Service

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