OSDN Git Service

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