2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
20 package cx.fbn.nevernote.utilities;
\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
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
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
61 public class ListManager {
\r
64 private final ApplicationLogger logger;
\r
65 DatabaseConnection conn;
\r
66 QSqlQuery deleteWords;
\r
67 QSqlQuery insertWords;
\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
74 private List<LinkedNotebook> linkedNotebookIndex;
\r
76 private List<SavedSearch> searchIndex;
\r
78 private List<String> selectedNotebooks;
\r
79 private final NoteTableModel noteModel;
\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
91 private final CounterRunner trashCounterRunner;
\r
92 private final QThread trashThread;
\r
93 public TrashSignal trashSignal;
\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
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
107 // private final HashMap<String, QImage> thumbnailList;
\r
110 public ListManager(DatabaseConnection d, ApplicationLogger l) {
\r
114 status = new StatusSignal();
\r
115 signals = new ThreadSignal();
\r
117 // setup index locks
\r
118 enSearchChanged = false;
\r
121 noteModel = new NoteTableModel(this);
\r
122 selectedTags = new ArrayList<String>();
\r
124 notebookCounter = new ArrayList<NotebookCounter>();
\r
125 tagCounter = new ArrayList<TagCounter>();
\r
126 selectedNotebooks = new ArrayList<String>();
\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
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
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
151 wordMap = new HashMap<String, String>();
\r
152 tagSignal = new TagSignal();
\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
159 // thumbnailList = conn.getNoteTable().getThumbnails();
\r
160 // thumbnailList = new HashMap<String,QImage>();
\r
162 linkedNotebookIndex = conn.getLinkedNotebookTable().getAll();
\r
163 loadNoteTitleColors();
\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
173 logger.log(logger.MEDIUM, "Waiting for notebookCounterThread to stop");
\r
175 notebookThread.join();
\r
176 } catch (InterruptedException e) {
\r
177 e.printStackTrace();
\r
180 logger.log(logger.MEDIUM, "Waiting for tagCounterThread to stop");
\r
183 } catch (InterruptedException e) {
\r
184 e.printStackTrace();
\r
187 logger.log(logger.MEDIUM, "Waiting for trashThread to stop");
\r
189 trashThread.join();
\r
190 } catch (InterruptedException e) {
\r
191 e.printStackTrace();
\r
195 logger.log(logger.MEDIUM, "Waiting for saveThread to stop");
\r
197 saveThread.join(0);
\r
198 } catch (InterruptedException e) {
\r
199 e.printStackTrace();
\r
204 //***************************************************************
\r
205 //***************************************************************
\r
206 //* Refresh lists after a db sync
\r
207 //***************************************************************
\r
208 //***************************************************************
\r
209 public void refreshLists(Note n, boolean dirty, String content) {
\r
211 // conn.getNoteTable().updateNoteContent(n.getGuid(), n.getContent());
\r
212 saveRunner.addWork(n.getGuid(), content);
\r
213 conn.getNoteTable().updateNoteTitle(n.getGuid(), n.getTitle());
\r
216 setSavedSearchIndex(conn.getSavedSearchTable().getAll());
\r
217 setTagIndex(conn.getTagTable().getAll());
\r
218 setNotebookIndex(conn.getNotebookTable().getAll());
\r
220 List<Notebook> local = conn.getNotebookTable().getAllLocal();
\r
221 localNotebookIndex = new ArrayList<String>();
\r
222 for (int i=0; i<local.size(); i++)
\r
223 localNotebookIndex.add(local.get(i).getGuid());
\r
225 noteModel.setMasterNoteIndex(conn.getNoteTable().getAllNotes());
\r
226 // For performance reasons, we didn't get the tags for every note individually. We now need to
\r
228 List<cx.fbn.nevernote.sql.NoteTagsRecord> noteTags = conn.getNoteTable().noteTagsTable.getAllNoteTags();
\r
229 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
230 List<String> tags = new ArrayList<String>();
\r
231 List<String> names = new ArrayList<String>();
\r
232 for (int j=0; j<noteTags.size(); j++) {
\r
233 if (getMasterNoteIndex().get(i).getGuid().equals(noteTags.get(j).noteGuid)) {
\r
234 tags.add(noteTags.get(j).tagGuid);
\r
235 names.add(getTagNameByGuid(noteTags.get(j).tagGuid));
\r
239 getMasterNoteIndex().get(i).setTagGuids(tags);
\r
240 getMasterNoteIndex().get(i).setTagNames(names);
\r
244 setUnsynchronizedNotes(conn.getNoteTable().getUnsynchronizedGUIDs());
\r
246 linkedNotebookIndex = conn.getLinkedNotebookTable().getAll();
\r
248 enSearchChanged = true;
\r
251 public void reloadTagIndex() {
\r
252 setTagIndex(conn.getTagTable().getAll());
\r
254 public void reloadIndexes() {
\r
255 setUnsynchronizedNotes(conn.getNoteTable().getUnsynchronizedGUIDs());
\r
257 List<Notebook> local = conn.getNotebookTable().getAllLocal();
\r
258 localNotebookIndex = new ArrayList<String>();
\r
259 for (int i=0; i<local.size(); i++)
\r
260 localNotebookIndex.add(local.get(i).getGuid());
\r
264 setNotebookIndex(conn.getNotebookTable().getAll());
\r
265 // load archived notebooks (if note using the EN interface)
\r
266 setArchiveNotebookIndex(conn.getNotebookTable().getAllArchived());
\r
267 // load saved search index
\r
268 setSavedSearchIndex(conn.getSavedSearchTable().getAll());
\r
269 // Load search helper utility
\r
270 enSearch = new EnSearch(conn, logger, "", getTagIndex(), Global.getRecognitionWeight());
\r
271 logger.log(logger.HIGH, "Building note index");
\r
273 // if (getMasterNoteIndex() == null) {
\r
274 noteModel.setMasterNoteIndex(conn.getNoteTable().getAllNotes());
\r
276 // For performance reasons, we didn't get the tags for every note individually. We now need to
\r
278 List<cx.fbn.nevernote.sql.NoteTagsRecord> noteTags = conn.getNoteTable().noteTagsTable.getAllNoteTags();
\r
279 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
280 List<String> tags = new ArrayList<String>();
\r
281 List<String> names = new ArrayList<String>();
\r
282 for (int j=0; j<noteTags.size(); j++) {
\r
283 if (getMasterNoteIndex().get(i).getGuid().equals(noteTags.get(j).noteGuid)) {
\r
284 tags.add(noteTags.get(j).tagGuid);
\r
285 names.add(getTagNameByGuid(noteTags.get(j).tagGuid));
\r
289 getMasterNoteIndex().get(i).setTagGuids(tags);
\r
290 getMasterNoteIndex().get(i).setTagNames(names);
\r
293 setNoteIndex(getMasterNoteIndex());
\r
297 //***************************************************************
\r
298 //***************************************************************
\r
299 //* selected notebooks
\r
300 //***************************************************************
\r
301 //***************************************************************
\r
302 // Return the selected notebook(s)
\r
303 public List<String> getSelectedNotebooks() {
\r
304 return selectedNotebooks;
\r
306 // Set the current selected notebook(s)
\r
307 public void setSelectedNotebooks(List <String> s) {
\r
309 s = new ArrayList<String>();
\r
310 selectedNotebooks = s;
\r
314 //***************************************************************
\r
315 //***************************************************************
\r
316 //** These functions deal with setting & retrieving the master lists
\r
317 //***************************************************************
\r
318 //***************************************************************
\r
319 // Get the note table model
\r
320 public NoteTableModel getNoteTableModel() {
\r
323 // save the saved search index
\r
324 private void setSavedSearchIndex(List<SavedSearch> t) {
\r
327 // Retrieve the Tag index
\r
328 public List<SavedSearch> getSavedSearchIndex() {
\r
329 return searchIndex;
\r
332 // save the tag index
\r
333 private void setTagIndex(List<Tag> t) {
\r
336 // Retrieve the Tag index
\r
337 public List<Tag> getTagIndex() {
\r
340 private void setNotebookIndex(List<Notebook> t) {
\r
343 private void setArchiveNotebookIndex(List<Notebook> t) {
\r
344 archiveNotebookIndex = t;
\r
346 // Retrieve the Notebook index
\r
347 public List<Notebook> getNotebookIndex() {
\r
348 return notebookIndex;
\r
351 public List<LinkedNotebook> getLinkedNotebookIndex() {
\r
352 return linkedNotebookIndex;
\r
354 public List<Notebook> getArchiveNotebookIndex() {
\r
355 return archiveNotebookIndex;
\r
357 // Save the current note list
\r
358 private void setNoteIndex(List<Note> n) {
\r
359 noteModel.setNoteIndex(n);
\r
361 // Get the note index
\r
362 public synchronized List<Note> getNoteIndex() {
\r
363 return noteModel.getNoteIndex();
\r
365 // Save the count of notes per notebook
\r
366 public void setNotebookCounter(List<NotebookCounter> n) {
\r
367 notebookCounter = n;
\r
368 notebookSignal.refreshNotebookTreeCounts.emit(getNotebookIndex(), notebookCounter);
\r
370 public List<NotebookCounter> getNotebookCounter() {
\r
371 return notebookCounter;
\r
373 // Save the count of notes for each tag
\r
374 public void setTagCounter(List<TagCounter> n) {
\r
376 tagSignal.refreshTagTreeCounts.emit(tagCounter);
\r
378 public List<TagCounter> getTagCounter() {
\r
381 public List<String> getLocalNotebooks() {
\r
382 return localNotebookIndex;
\r
384 // Unsynchronized Note List
\r
385 public List<String> getUnsynchronizedNotes() {
\r
386 return noteModel.getUnsynchronizedNotes();
\r
388 public void setUnsynchronizedNotes(List<String> l) {
\r
389 noteModel.setUnsynchronizedNotes(l);
\r
391 // Return a count of items in the trash
\r
392 public int getTrashCount() {
\r
395 // get the EnSearch variable
\r
396 public EnSearch getEnSearch() {
\r
399 public List<Note> getMasterNoteIndex() {
\r
400 return noteModel.getMasterNoteIndex();
\r
403 // public HashMap<String, QImage> getThumbnails() {
\r
404 // return thumbnailList;
\r
406 public QImage getThumbnail(String guid) {
\r
407 // if (getThumbnails().containsKey(guid))
\r
408 // return getThumbnails().get(guid);
\r
410 QImage img = new QImage();
\r
411 img = QImage.fromData(conn.getNoteTable().getThumbnail(guid));
\r
412 if (img == null || img.isNull())
\r
414 //getThumbnails().put(guid, img);
\r
417 public QPixmap getThumbnailPixmap(String guid) {
\r
418 // if (getThumbnails().containsKey(guid))
\r
419 // return getThumbnails().get(guid);
\r
421 QPixmap img = new QPixmap();
\r
422 img.loadFromData(conn.getNoteTable().getThumbnail(guid));
\r
423 if (img == null || img.isNull())
\r
425 //getThumbnails().put(guid, img);
\r
428 //***************************************************************
\r
429 //***************************************************************
\r
430 //** These functions deal with setting & retrieving filters
\r
431 //***************************************************************
\r
432 //***************************************************************
\r
433 public void setEnSearch(String t) {
\r
434 enSearch = new EnSearch(conn,logger, t, getTagIndex(), Global.getRecognitionWeight());
\r
435 enSearchChanged = true;
\r
437 // Save search tags
\r
438 public void setSelectedTags(List<String> selectedTags) {
\r
439 this.selectedTags = selectedTags;
\r
441 // Save seleceted search
\r
442 public void setSelectedSavedSearch(String s) {
\r
443 this.selectedSearch = s;
\r
446 public List<String> getSelectedTags() {
\r
447 return selectedTags;
\r
449 // Get saved search
\r
450 public String getSelectedSearch() {
\r
451 return selectedSearch;
\r
457 //***************************************************************
\r
458 //***************************************************************
\r
459 //** Note functions
\r
460 //***************************************************************
\r
461 //***************************************************************
\r
463 public void saveNoteTags(String noteGuid, List<String> tags) {
\r
464 logger.log(logger.HIGH, "Entering ListManager.saveNoteTags");
\r
466 conn.getNoteTable().noteTagsTable.deleteNoteTag(noteGuid);
\r
467 List<String> tagGuids = new ArrayList<String>();
\r
468 boolean newTagCreated = false;
\r
470 for (int i=0; i<tags.size(); i++) {
\r
471 tagName = tags.get(i);
\r
472 boolean found = false;
\r
473 for (int j=0; j<tagIndex.size(); j++) {
\r
474 if (tagIndex.get(j).getName().equalsIgnoreCase(tagName)) {
\r
475 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, tagIndex.get(j).getGuid());
\r
476 tagGuids.add(tagIndex.get(j).getGuid());
\r
477 j=tagIndex.size()+1;
\r
482 Tag nTag = new Tag();
\r
483 nTag.setName(tagName);
\r
484 Calendar currentTime = new GregorianCalendar();
\r
485 Long l = new Long(currentTime.getTimeInMillis());
\r
487 while (l==prevTime) {
\r
488 currentTime = new GregorianCalendar();
\r
489 l=currentTime.getTimeInMillis();
\r
491 String randint = new String(Long.toString(l));
\r
493 nTag.setUpdateSequenceNum(0);
\r
494 nTag.setGuid(randint);
\r
495 conn.getTagTable().addTag(nTag, true);
\r
496 getTagIndex().add(nTag);
\r
497 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, nTag.getGuid());
\r
498 tagGuids.add(nTag.getGuid());
\r
499 newTagCreated = true;
\r
503 for (int i=0; i<getNoteIndex().size(); i++) {
\r
504 if (getNoteIndex().get(i).getGuid().equals(noteGuid)) {
\r
505 getNoteIndex().get(i).setTagNames(tags);
\r
506 getNoteIndex().get(i).setTagGuids(tagGuids);
\r
507 i=getNoteIndex().size()+1;
\r
511 tagSignal.listChanged.emit();
\r
512 logger.log(logger.HIGH, "Leaving ListManager.saveNoteTags");
\r
515 public void deleteNote(String guid) {
\r
516 trashCounterRunner.abortCount = true;
\r
517 Calendar currentTime = new GregorianCalendar();
\r
518 Long l = new Long(currentTime.getTimeInMillis());
\r
520 while (l==prevTime) {
\r
521 currentTime = new GregorianCalendar();
\r
522 l=currentTime.getTimeInMillis();
\r
525 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
526 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
527 getMasterNoteIndex().get(i).setActive(false);
\r
528 getMasterNoteIndex().get(i).setDeleted(l);
\r
529 i=getMasterNoteIndex().size();
\r
532 for (int i=0; i<getNoteIndex().size(); i++) {
\r
533 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
534 getNoteIndex().get(i).setActive(false);
\r
535 getNoteIndex().get(i).setDeleted(l);
\r
536 i=getNoteIndex().size();
\r
539 conn.getNoteTable().deleteNote(guid);
\r
540 reloadTrashCount();
\r
543 public void restoreNote(String guid) {
\r
544 trashCounterRunner.abortCount = true;
\r
545 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
546 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
547 getMasterNoteIndex().get(i).setActive(true);
\r
548 getMasterNoteIndex().get(i).setDeleted(0);
\r
549 i=getMasterNoteIndex().size();
\r
552 for (int i=0; i<getNoteIndex().size(); i++) {
\r
553 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
554 getNoteIndex().get(i).setActive(true);
\r
555 getNoteIndex().get(i).setDeleted(0);
\r
556 i=getNoteIndex().size();
\r
559 conn.getNoteTable().restoreNote(guid);
\r
560 reloadTrashCount();
\r
562 public void updateNote(Note n) {
\r
564 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
565 if (getMasterNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
566 getMasterNoteIndex().remove(i);
\r
567 getMasterNoteIndex().add(n);
\r
570 for (int i=0; i<getNoteIndex().size(); i++) {
\r
571 if (getNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
572 getNoteIndex().get(i).setActive(true);
\r
573 getNoteIndex().get(i).setDeleted(0);
\r
574 i=getNoteIndex().size();
\r
577 conn.getNoteTable().updateNote(n, true);
\r
580 public void addNote(Note n) {
\r
581 noteModel.addNote(n);
\r
584 public void expungeNote(String guid) {
\r
585 trashCounterRunner.abortCount = true;
\r
586 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
587 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
588 getMasterNoteIndex().remove(i);
\r
589 i=getMasterNoteIndex().size();
\r
592 for (int i=0; i<getNoteIndex().size(); i++) {
\r
593 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
594 getNoteIndex().remove(i);
\r
595 i=getNoteIndex().size();
\r
598 conn.getNoteTable().expungeNote(guid, false, true);
\r
599 reloadTrashCount();
\r
602 public void emptyTrash() {
\r
603 trashCounterRunner.abortCount = true;
\r
604 for (int i=getMasterNoteIndex().size()-1; i>=0; i--) {
\r
605 if (!getMasterNoteIndex().get(i).isActive()) {
\r
606 getMasterNoteIndex().remove(i);
\r
610 for (int i=getNoteIndex().size()-1; i>=0; i--) {
\r
611 if (!getNoteIndex().get(i).isActive()) {
\r
612 getNoteIndex().remove(i);
\r
616 conn.getNoteTable().expungeAllDeletedNotes();
\r
617 reloadTrashCount();
\r
619 // The trash counter thread has produced a result
\r
620 @SuppressWarnings("unused")
\r
621 private void trashSignalReceiver(Integer i) {
\r
623 trashSignal.countChanged.emit(i);
\r
625 // Update note contents
\r
626 public void updateNoteContent(String guid, String content) {
\r
627 logger.log(logger.HIGH, "Entering ListManager.updateNoteContent");
\r
628 // EnmlConverter enml = new EnmlConverter(logger);
\r
629 // String text = enml.convert(guid, content);
\r
631 // Update the list tables
\r
632 /* for (int i=0; i<masterNoteIndex.size(); i++) {
\r
633 if (masterNoteIndex.get(i).getGuid().equals(guid)) {
\r
634 masterNoteIndex.get(i).setContent(text);
\r
635 i = masterNoteIndex.size();
\r
638 // Update the list tables
\r
639 for (int i=0; i<getNoteIndex().size(); i++) {
\r
640 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
641 getNoteIndex().get(i).setContent(text);
\r
642 i = getNoteIndex().size();
\r
646 // Check if any new tags were encountered
\r
647 /* if (enml.saveInvalidXML) {
\r
648 List<String> elements = Global.invalidElements;
\r
649 for (int i=0; i<elements.size(); i++) {
\r
650 conn.getInvalidXMLTable().addInvalidElement(elements.get(i));
\r
652 for (String key : Global.invalidAttributes.keySet()) {
\r
653 ArrayList<String> attributes = Global.invalidAttributes.get(key);
\r
654 for (int i=0; i<attributes.size(); i++) {
\r
655 conn.getInvalidXMLTable().addInvalidAttribute(key, attributes.get(i));
\r
660 saveRunner.addWork(guid, content);
\r
661 // conn.getNoteTable().updateNoteContent(guid, content);
\r
662 logger.log(logger.HIGH, "Leaving ListManager.updateNoteContent");
\r
664 // Update a note creation date
\r
665 public void updateNoteCreatedDate(String guid, QDateTime date) {
\r
666 noteModel.updateNoteCreatedDate(guid, date);
\r
667 conn.getNoteTable().updateNoteCreatedDate(guid, date);
\r
669 // Subject date has been changed
\r
670 public void updateNoteSubjectDate(String guid, QDateTime date) {
\r
671 noteModel.updateNoteSubjectDate(guid, date);
\r
672 conn.getNoteTable().updateNoteSubjectDate(guid, date);
\r
674 // Author has changed
\r
675 public void updateNoteAuthor(String guid, String author) {
\r
676 noteModel.updateNoteAuthor(guid, author);
\r
677 conn.getNoteTable().updateNoteAuthor(guid, author);
\r
679 // Author has changed
\r
680 public void updateNoteGeoTag(String guid, Double lon, Double lat, Double alt) {
\r
681 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
682 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
683 getMasterNoteIndex().get(i).getAttributes().setLongitude(lon);
\r
684 getMasterNoteIndex().get(i).getAttributes().setLongitudeIsSet(true);
\r
685 getMasterNoteIndex().get(i).getAttributes().setLatitude(lat);
\r
686 getMasterNoteIndex().get(i).getAttributes().setLatitudeIsSet(true);
\r
687 getMasterNoteIndex().get(i).getAttributes().setAltitude(alt);
\r
688 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);
\r
689 i = getMasterNoteIndex().size();
\r
692 // Update the list tables
\r
693 for (int i=0; i<getNoteIndex().size(); i++) {
\r
694 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
695 getNoteIndex().get(i).getAttributes().setLongitude(lon);
\r
696 getNoteIndex().get(i).getAttributes().setLongitudeIsSet(true);
\r
697 getNoteIndex().get(i).getAttributes().setLatitude(lat);
\r
698 getNoteIndex().get(i).getAttributes().setLatitudeIsSet(true);
\r
699 getNoteIndex().get(i).getAttributes().setAltitude(alt);
\r
700 getNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);
\r
701 i = getNoteIndex().size();
\r
704 conn.getNoteTable().updateNoteGeoTags(guid, lon, lat, alt);
\r
706 // Source URL changed
\r
707 public void updateNoteSourceUrl(String guid, String url) {
\r
708 noteModel.updateNoteSourceUrl(guid, url);
\r
709 conn.getNoteTable().updateNoteSourceUrl(guid, url);
\r
711 // Update a note last changed date
\r
712 public void updateNoteAlteredDate(String guid, QDateTime date) {
\r
713 noteModel.updateNoteChangedDate(guid, date);
\r
714 conn.getNoteTable().updateNoteAlteredDate(guid, date);
\r
716 // Update a note title
\r
717 public void updateNoteTitle(String guid, String title) {
\r
718 logger.log(logger.HIGH, "Entering ListManager.updateNoteTitle");
\r
719 conn.getNoteTable().updateNoteTitle(guid, title);
\r
720 noteModel.updateNoteTitle(guid, title);
\r
721 logger.log(logger.HIGH, "Leaving ListManager.updateNoteTitle");
\r
723 // Update a note's notebook
\r
724 public void updateNoteNotebook(String guid, String notebookGuid) {
\r
725 logger.log(logger.HIGH, "Entering ListManager.updateNoteNotebook");
\r
726 noteModel.updateNoteNotebook(guid, notebookGuid);
\r
727 conn.getNoteTable().updateNoteNotebook(guid, notebookGuid, true);
\r
728 logger.log(logger.HIGH, "Leaving ListManager.updateNoteNotebook");
\r
730 // Update a note sequence number
\r
731 public void updateNoteSequence(String guid, int sequence) {
\r
732 logger.log(logger.HIGH, "Entering ListManager.updateNoteSequence");
\r
734 conn.getNoteTable().updateNoteSequence(guid, sequence);
\r
736 for (int i=0; i<noteModel.getMasterNoteIndex().size(); i++) {
\r
737 if (noteModel.getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
738 noteModel.getMasterNoteIndex().get(i).setUpdateSequenceNum(sequence);
\r
739 i=noteModel.getMasterNoteIndex().size()+1;
\r
743 for (int i=0; i<getNoteIndex().size(); i++) {
\r
744 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
745 getNoteIndex().get(i).setUpdateSequenceNum(sequence);
\r
746 i=getNoteIndex().size()+1;
\r
749 logger.log(logger.HIGH, "Leaving ListManager.updateNoteSequence");
\r
751 public void updateNoteGuid(String oldGuid, String newGuid, boolean updateDatabase) {
\r
752 logger.log(logger.HIGH, "Entering ListManager.updateNoteGuid");
\r
753 if (updateDatabase)
\r
754 conn.getNoteTable().updateNoteGuid(oldGuid, newGuid);
\r
755 noteModel.updateNoteGuid(oldGuid, newGuid);
\r
756 logger.log(logger.HIGH, "Leaving ListManager.updateNoteGuid");
\r
761 //************************************************************************************
\r
762 //************************************************************************************
\r
764 //************************************************************************************
\r
765 //************************************************************************************
\r
766 // Update a tag sequence number
\r
767 public void updateTagSequence(String guid, int sequence) {
\r
768 logger.log(logger.HIGH, "Entering ListManager.updateTagSequence");
\r
770 conn.getTagTable().updateTagSequence(guid, sequence);
\r
771 for (int i=0; i<tagIndex.size(); i++) {
\r
772 if (tagIndex.get(i).getGuid().equals(guid)) {
\r
773 getTagIndex().get(i).setUpdateSequenceNum(sequence);
\r
774 i=tagIndex.size()+1;
\r
777 logger.log(logger.HIGH, "Leaving ListManager.updateTagSequence");
\r
779 // Update a tag guid number
\r
780 public void updateTagGuid(String oldGuid, String newGuid) {
\r
781 logger.log(logger.HIGH, "Entering ListManager.updateTagGuid");
\r
783 conn.getTagTable().updateTagGuid(oldGuid, newGuid);
\r
784 for (int i=0; i<tagIndex.size(); i++) {
\r
785 if (tagIndex.get(i).getGuid().equals(oldGuid)) {
\r
786 tagIndex.get(i).setGuid(newGuid);
\r
787 i=tagIndex.size()+1;
\r
790 logger.log(logger.HIGH, "Leaving ListManager.updateTagGuid");
\r
795 //************************************************************************************
\r
796 //************************************************************************************
\r
797 //** Notebook functions
\r
798 //************************************************************************************
\r
799 //************************************************************************************
\r
800 // Delete a notebook
\r
801 public void deleteNotebook(String guid) {
\r
802 for (int i=0; i<getNotebookIndex().size(); i++) {
\r
803 if (getNotebookIndex().get(i).getGuid().equals(guid)) {
\r
804 getNotebookIndex().remove(i);
\r
805 i=getMasterNoteIndex().size();
\r
808 conn.getNotebookTable().expungeNotebook(guid, true);
\r
811 public void renameStack(String oldName, String newName) {
\r
812 for (int i=0; i<getNotebookIndex().size(); i++) {
\r
813 if (getNotebookIndex().get(i).getStack() != null &&
\r
814 getNotebookIndex().get(i).getStack().equalsIgnoreCase(oldName)) {
\r
815 getNotebookIndex().get(i).setStack(newName);
\r
819 // Update a notebook sequence number
\r
820 public void updateNotebookSequence(String guid, int sequence) {
\r
821 logger.log(logger.HIGH, "Entering ListManager.updateNotebookSequence");
\r
823 conn.getNotebookTable().updateNotebookSequence(guid, sequence);
\r
825 for (int i=0; i<notebookIndex.size(); i++) {
\r
826 if (notebookIndex.get(i).getGuid().equals(guid)) {
\r
827 notebookIndex.get(i).setUpdateSequenceNum(sequence);
\r
828 i=notebookIndex.size()+1;
\r
831 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookSequence");
\r
834 // Update a notebook Guid number
\r
835 public void updateNotebookGuid(String oldGuid, String newGuid) {
\r
836 logger.log(logger.HIGH, "Entering ListManager.updateNotebookGuid");
\r
838 conn.getNotebookTable().updateNotebookGuid(oldGuid, newGuid);
\r
840 for (int i=0; i<notebookIndex.size(); i++) {
\r
841 if (notebookIndex.get(i).getGuid().equals(oldGuid)) {
\r
842 notebookIndex.get(i).setGuid(newGuid);
\r
843 i=notebookIndex.size()+1;
\r
846 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookGuid");
\r
849 // Update a notebook Guid number
\r
850 public void updateNotebookStack(String oldGuid, String stack) {
\r
851 logger.log(logger.HIGH, "Entering ListManager.updateNotebookGuid");
\r
853 conn.getNotebookTable().setStack(oldGuid, stack);
\r
855 for (int i=0; i<notebookIndex.size(); i++) {
\r
856 if (notebookIndex.get(i).getGuid().equals(oldGuid)) {
\r
857 notebookIndex.get(i).setStack(stack);
\r
858 i=notebookIndex.size()+1;
\r
861 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookGuid");
\r
866 //************************************************************************************
\r
867 //************************************************************************************
\r
868 //** Load and filter the note index
\r
869 //************************************************************************************
\r
870 //************************************************************************************
\r
872 public void noteDownloaded(Note n) {
\r
873 boolean found = false;
\r
874 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
875 if (getMasterNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
876 getMasterNoteIndex().set(i,n);
\r
878 i=getMasterNoteIndex().size();
\r
883 getMasterNoteIndex().add(n);
\r
885 for (int i=0; i<getNoteIndex().size(); i++) {
\r
886 if (getNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
887 if (filterRecord(getNoteIndex().get(i)))
\r
888 getNoteIndex().add(n);
\r
889 getNoteIndex().remove(i);
\r
890 i=getNoteIndex().size();
\r
894 if (filterRecord(n))
\r
895 getNoteIndex().add(n);
\r
898 // Check if a note matches the currently selected notebooks, tags, or attribute searches.
\r
899 public boolean filterRecord(Note n) {
\r
901 boolean goodNotebook = false;
\r
902 boolean goodTag = false;
\r
903 boolean goodStatus = false;
\r
905 // Check note status
\r
906 if (!n.isActive() && Global.showDeleted)
\r
909 if (n.isActive() && !Global.showDeleted)
\r
913 // Begin filtering results
\r
915 goodNotebook = filterByNotebook(n.getNotebookGuid());
\r
917 goodTag = filterByTag(n.getTagGuids());
\r
919 boolean goodCreatedBefore = false;
\r
920 boolean goodCreatedSince = false;
\r
921 boolean goodChangedBefore = false;
\r
922 boolean goodChangedSince = false;
\r
923 boolean goodContains = false;
\r
924 if (!Global.createdBeforeFilter.hasSelection())
\r
925 goodCreatedBefore = true;
\r
927 goodCreatedBefore = Global.createdBeforeFilter.check(n);
\r
929 if (!Global.createdSinceFilter.hasSelection())
\r
930 goodCreatedSince = true;
\r
932 goodCreatedSince = Global.createdSinceFilter.check(n);
\r
934 if (!Global.changedBeforeFilter.hasSelection())
\r
935 goodChangedBefore = true;
\r
937 goodChangedBefore = Global.changedBeforeFilter.check(n);
\r
938 if (!Global.changedSinceFilter.hasSelection())
\r
939 goodChangedSince = true;
\r
941 goodChangedSince = Global.changedSinceFilter.check(n);
\r
942 if (!Global.containsFilter.hasSelection())
\r
943 goodContains = true;
\r
945 goodContains = Global.containsFilter.check(conn.getNoteTable(), n);
\r
947 if (goodCreatedSince && goodCreatedBefore && goodChangedSince && goodChangedBefore && goodContains)
\r
953 // Load the note index based upon what the user wants.
\r
954 public void loadNotesIndex() {
\r
955 logger.log(logger.EXTREME, "Entering ListManager.loadNotesIndex()");
\r
956 tagCounterRunner.abortCount = true;
\r
957 notebookCounterRunner.abortCount = true;
\r
958 trashCounterRunner.abortCount = true;
\r
960 List<Note> matches;
\r
961 if (enSearchChanged || getMasterNoteIndex() == null)
\r
962 matches = enSearch.matchWords();
\r
964 matches = getMasterNoteIndex();
\r
966 if (matches == null)
\r
967 matches = getMasterNoteIndex();
\r
969 setNoteIndex(new ArrayList<Note>());
\r
970 for (int i=0; i<matches.size(); i++) {
\r
971 if (filterRecord(matches.get(i)))
\r
972 getNoteIndex().add(matches.get(i));
\r
974 countNotebookResults(getNoteIndex());
\r
975 countTagResults(getNoteIndex());
\r
976 enSearchChanged = false;
\r
977 reloadTrashCount();
\r
978 logger.log(logger.EXTREME, "Leaving ListManager.loadNotesIndex()");
\r
980 public void countNotebookResults(List<Note> index) {
\r
981 logger.log(logger.EXTREME, "Entering ListManager.countNotebookResults()");
\r
982 if (!Global.mimicEvernoteInterface) {
\r
983 notebookCounterRunner.setNoteIndex(index);
\r
984 notebookCounterRunner.release(CounterRunner.NOTEBOOK);
\r
986 notebookCounterRunner.setNoteIndex(getMasterNoteIndex());
\r
987 notebookCounterRunner.release(CounterRunner.NOTEBOOK_ALL);
\r
989 logger.log(logger.EXTREME, "Leaving ListManager.countNotebookResults()");
\r
991 public void countTagResults(List<Note> index) {
\r
992 logger.log(logger.EXTREME, "Entering ListManager.countTagResults");
\r
993 if (!Global.tagBehavior().equalsIgnoreCase("DoNothing")) {
\r
994 tagCounterRunner.setNoteIndex(index);
\r
995 tagCounterRunner.release(CounterRunner.TAG);
\r
997 tagCounterRunner.setNoteIndex(null);
\r
998 tagCounterRunner.release(CounterRunner.TAG_ALL);
\r
1000 logger.log(logger.EXTREME, "Leaving ListManager.countTagResults()");
\r
1002 // Update the count of items in the trash
\r
1003 public void reloadTrashCount() {
\r
1004 logger.log(logger.EXTREME, "Entering ListManager.reloadTrashCount");
\r
1005 trashCounterRunner.setNoteIndex(getNoteIndex());
\r
1006 trashCounterRunner.release(CounterRunner.TRASH);
\r
1007 logger.log(logger.EXTREME, "Leaving ListManager.reloadTrashCount");
\r
1010 private boolean filterByNotebook(String guid) {
\r
1011 boolean good = false;
\r
1012 if (selectedNotebooks.size() == 0)
\r
1014 if (!good && selectedNotebooks.contains(guid))
\r
1017 for (int i=0; i<getArchiveNotebookIndex().size() && good; i++) {
\r
1018 if (guid.equals(getArchiveNotebookIndex().get(i).getGuid())) {
\r
1025 private boolean filterByTag(List<String> noteTags) {
\r
1026 if (noteTags == null || selectedTags == null)
\r
1029 if (selectedTags.size() == 0)
\r
1032 for (int i=0; i<selectedTags.size(); i++) {
\r
1033 String selectedGuid = selectedTags.get(i);
\r
1034 if (noteTags.contains(selectedGuid))
\r
1040 public void setNoteSynchronized(String guid, boolean value) {
\r
1041 getNoteTableModel().updateNoteSyncStatus(guid, value);
\r
1044 public void updateNoteTitleColor(String guid, Integer color) {
\r
1045 noteModel.updateNoteTitleColor(guid, color);
\r
1046 conn.getNoteTable().setNoteTitleColor(guid, color);
\r
1048 public void loadNoteTitleColors() {
\r
1049 List<Pair<String,Integer>> colors = conn.getNoteTable().getNoteTitleColors();
\r
1050 if (noteModel.getTitleColors() == null)
\r
1051 noteModel.setTitleColors(new HashMap<String,Integer>());
\r
1053 noteModel.getTitleColors().clear();
\r
1054 for (int i=0; i<colors.size(); i++) {
\r
1055 noteModel.getTitleColors().put(colors.get(i).getFirst(), colors.get(i).getSecond());
\r
1059 //********************************************************************************
\r
1060 //********************************************************************************
\r
1061 //* Support signals from the index thread
\r
1062 //********************************************************************************
\r
1063 //********************************************************************************
\r
1064 // Reset a flag if an index is needed
\r
1065 public void setIndexNeeded(String guid, String type, Boolean b) {
\r
1066 if (Global.keepRunning && type.equalsIgnoreCase("content"))
\r
1067 conn.getNoteTable().setIndexNeeded(guid, false);
\r
1068 if (Global.keepRunning && type.equalsIgnoreCase("resource")) {
\r
1069 conn.getNoteTable().noteResourceTable.setIndexNeeded(guid, b);
\r
1073 public boolean threadCheck(int id) {
\r
1074 if (id == Global.notebookCounterThreadId)
\r
1075 return notebookThread.isAlive();
\r
1076 if (id == Global.tagCounterThreadId)
\r
1077 return tagThread.isAlive();
\r
1078 if (id == Global.trashCounterThreadId)
\r
1079 return trashThread.isAlive();
\r
1080 if (id == Global.saveThreadId)
\r
1081 return saveThread.isAlive();
\r
1087 //********************************************************************************
\r
1088 //********************************************************************************
\r
1089 //* Utility Functions
\r
1090 //********************************************************************************
\r
1091 //********************************************************************************
\r
1092 public void compactDatabase() {
\r
1093 conn.compactDatabase();
\r
1094 // IndexConnection idx = new IndexConnection(logger, "nevernote-compact");
\r
1096 // idx.dbShutdown();
\r
1099 // Rebuild the note HTML to something usable
\r
1100 public List<String> scanNoteForResources(Note n) {
\r
1101 logger.log(logger.HIGH, "Entering ListManager.scanNoteForResources");
\r
1102 logger.log(logger.EXTREME, "Note guid: " +n.getGuid());
\r
1103 QDomDocument doc = new QDomDocument();
\r
1104 QDomDocument.Result result = doc.setContent(n.getContent());
\r
1105 if (!result.success) {
\r
1106 logger.log(logger.MEDIUM, "Parse error when scanning note for resources.");
\r
1107 logger.log(logger.MEDIUM, "Note guid: " +n.getGuid());
\r
1111 List<String> returnArray = new ArrayList<String>();
\r
1112 QDomNodeList anchors = doc.elementsByTagName("en-media");
\r
1113 for (int i=0; i<anchors.length(); i++) {
\r
1114 QDomElement enmedia = anchors.at(i).toElement();
\r
1115 if (enmedia.hasAttribute("type")) {
\r
1116 QDomAttr hash = enmedia.attributeNode("hash");
\r
1117 returnArray.add(hash.value().toString());
\r
1120 logger.log(logger.HIGH, "Leaving ListManager.scanNoteForResources");
\r
1121 return returnArray;
\r
1123 // Given a list of tags, produce a string list of tag names
\r
1124 public String getTagNamesForNote(Note n) {
\r
1125 StringBuffer buffer = new StringBuffer(100);
\r
1126 Vector<String> v = new Vector<String>();
\r
1127 List<String> guids = n.getTagGuids();
\r
1129 if (guids == null)
\r
1132 for (int i=0; i<guids.size(); i++) {
\r
1133 v.add(getTagNameByGuid(guids.get(i)));
\r
1135 Comparator<String> comparator = Collections.reverseOrder();
\r
1136 Collections.sort(v,comparator);
\r
1137 Collections.reverse(v);
\r
1139 for (int i = 0; i<v.size(); i++) {
\r
1141 buffer.append(", ");
\r
1142 buffer.append(v.get(i));
\r
1145 return buffer.toString();
\r
1147 // Get a tag name when given a tag guid
\r
1148 public String getTagNameByGuid(String guid) {
\r
1149 for (int i=0; i<getTagIndex().size(); i++) {
\r
1150 String s = getTagIndex().get(i).getGuid();
\r
1151 if (s.equals(guid)) {
\r
1152 return getTagIndex().get(i).getName();
\r
1157 // For a notebook guid, return the name
\r
1158 public String getNotebookNameByGuid(String guid) {
\r
1159 if (notebookIndex == null)
\r
1161 for (int i=0; i<notebookIndex.size(); i++) {
\r
1162 String s = notebookIndex.get(i).getGuid();
\r
1163 if (s.equals(guid)) {
\r
1164 return notebookIndex.get(i).getName();
\r