OSDN Git Service

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