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.Note;
\r
32 import com.evernote.edam.type.Notebook;
\r
33 import com.evernote.edam.type.SavedSearch;
\r
34 import com.evernote.edam.type.Tag;
\r
35 import com.trolltech.qt.QThread;
\r
36 import com.trolltech.qt.core.QDateTime;
\r
37 import com.trolltech.qt.gui.QImage;
\r
38 import com.trolltech.qt.gui.QPixmap;
\r
39 import com.trolltech.qt.sql.QSqlQuery;
\r
40 import com.trolltech.qt.xml.QDomAttr;
\r
41 import com.trolltech.qt.xml.QDomDocument;
\r
42 import com.trolltech.qt.xml.QDomElement;
\r
43 import com.trolltech.qt.xml.QDomNodeList;
\r
45 import cx.fbn.nevernote.Global;
\r
46 import cx.fbn.nevernote.filters.EnSearch;
\r
47 import cx.fbn.nevernote.filters.NotebookCounter;
\r
48 import cx.fbn.nevernote.filters.TagCounter;
\r
49 import cx.fbn.nevernote.gui.NoteTableModel;
\r
50 import cx.fbn.nevernote.signals.NotebookSignal;
\r
51 import cx.fbn.nevernote.signals.StatusSignal;
\r
52 import cx.fbn.nevernote.signals.TagSignal;
\r
53 import cx.fbn.nevernote.signals.ThreadSignal;
\r
54 import cx.fbn.nevernote.signals.TrashSignal;
\r
55 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
56 import cx.fbn.nevernote.threads.CounterRunner;
\r
57 import cx.fbn.nevernote.threads.SaveRunner;
\r
60 public class ListManager {
\r
63 private final ApplicationLogger logger;
\r
64 DatabaseConnection conn;
\r
65 QSqlQuery deleteWords;
\r
66 QSqlQuery insertWords;
\r
68 private List<Tag> tagIndex;
\r
69 private List<Notebook> notebookIndex;
\r
70 private List<Notebook> archiveNotebookIndex;
\r
71 private List<String> localNotebookIndex;
\r
73 private List<SavedSearch> searchIndex;
\r
75 private List<String> selectedNotebooks;
\r
76 private final NoteTableModel noteModel;
\r
79 private List<String> selectedTags;
\r
80 private String selectedSearch;
\r
81 ThreadSignal signals;
\r
82 public StatusSignal status;
\r
83 private final CounterRunner notebookCounterRunner;
\r
84 private final QThread notebookThread;
\r
85 private final CounterRunner tagCounterRunner;
\r
86 private final QThread tagThread;
\r
88 private final CounterRunner trashCounterRunner;
\r
89 private final QThread trashThread;
\r
90 public TrashSignal trashSignal;
\r
92 private List<NotebookCounter> notebookCounter; // count of displayed notes in each notebook
\r
93 private List<TagCounter> tagCounter; // count of displayed notes for each tag
\r
95 private EnSearch enSearch;
\r
96 private boolean enSearchChanged;
\r
97 public HashMap<String, String> wordMap;
\r
98 public TagSignal tagSignal;
\r
99 public NotebookSignal notebookSignal;
\r
100 private int trashCount;
\r
101 public SaveRunner saveRunner; // Thread used to save content. Used because the xml conversion is slowwwww
\r
102 QThread saveThread;
\r
104 // private final HashMap<String, QImage> thumbnailList;
\r
107 public ListManager(DatabaseConnection d, ApplicationLogger l) {
\r
111 status = new StatusSignal();
\r
112 signals = new ThreadSignal();
\r
114 // setup index locks
\r
115 enSearchChanged = false;
\r
118 noteModel = new NoteTableModel(this);
\r
119 selectedTags = new ArrayList<String>();
\r
121 notebookCounter = new ArrayList<NotebookCounter>();
\r
122 tagCounter = new ArrayList<TagCounter>();
\r
123 selectedNotebooks = new ArrayList<String>();
\r
127 notebookSignal = new NotebookSignal();
\r
128 notebookCounterRunner = new CounterRunner("notebook_counter.log", CounterRunner.NOTEBOOK, Global.getDatabaseUrl(), Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
\r
129 notebookCounterRunner.setNoteIndex(getNoteIndex());
\r
130 notebookCounterRunner.notebookSignal.countsChanged.connect(this, "setNotebookCounter(List)");
\r
131 notebookThread = new QThread(notebookCounterRunner, "Notebook Counter Thread");
\r
132 notebookThread.start();
\r
134 tagSignal = new TagSignal();
\r
135 tagCounterRunner = new CounterRunner("tag_counter.log", CounterRunner.TAG, Global.getDatabaseUrl(), Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
\r
136 tagCounterRunner.setNoteIndex(getNoteIndex());
\r
137 tagCounterRunner.tagSignal.countsChanged.connect(this, "setTagCounter(List)");
\r
138 tagThread = new QThread(tagCounterRunner, "Tag Counter Thread");
\r
141 trashSignal = new TrashSignal();
\r
142 trashCounterRunner = new CounterRunner("trash_counter.log", CounterRunner.TRASH, Global.getDatabaseUrl(), Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
\r
143 trashCounterRunner.trashSignal.countChanged.connect(this, "trashSignalReceiver(Integer)");
\r
144 trashThread = new QThread(trashCounterRunner, "Trash Counter Thread");
\r
145 trashThread.start();
\r
146 reloadTrashCount();
\r
148 wordMap = new HashMap<String, String>();
\r
149 tagSignal = new TagSignal();
\r
151 logger.log(logger.EXTREME, "Setting save thread");
\r
152 saveRunner = new SaveRunner("saveRunner.log", Global.getDatabaseUrl(), Global.getDatabaseUserid(), Global.getDatabaseUserPassword(), Global.cipherPassword);
\r
153 saveThread = new QThread(saveRunner, "Save Runner Thread");
\r
154 saveThread.start();
\r
156 // thumbnailList = conn.getNoteTable().getThumbnails();
\r
157 // thumbnailList = new HashMap<String,QImage>();
\r
159 loadNoteTitleColors();
\r
163 public void stop() {
\r
164 saveRunner.addWork("stop", "");
\r
165 tagCounterRunner.release(CounterRunner.EXIT);
\r
166 notebookCounterRunner.release(CounterRunner.EXIT);
\r
167 trashCounterRunner.release(CounterRunner.EXIT);
\r
169 logger.log(logger.MEDIUM, "Waiting for notebookCounterThread to stop");
\r
171 notebookThread.join();
\r
172 } catch (InterruptedException e) {
\r
173 e.printStackTrace();
\r
176 logger.log(logger.MEDIUM, "Waiting for tagCounterThread to stop");
\r
179 } catch (InterruptedException e) {
\r
180 e.printStackTrace();
\r
183 logger.log(logger.MEDIUM, "Waiting for trashThread to stop");
\r
185 trashThread.join();
\r
186 } catch (InterruptedException e) {
\r
187 e.printStackTrace();
\r
191 logger.log(logger.MEDIUM, "Waiting for saveThread to stop");
\r
193 saveThread.join(0);
\r
194 } catch (InterruptedException e) {
\r
195 e.printStackTrace();
\r
201 //***************************************************************
\r
202 //***************************************************************
\r
203 //* Refresh lists after a db sync
\r
204 //***************************************************************
\r
205 //***************************************************************
\r
206 public void refreshLists(Note n, boolean dirty, String content) {
\r
208 // conn.getNoteTable().updateNoteContent(n.getGuid(), n.getContent());
\r
209 saveRunner.addWork(n.getGuid(), content);
\r
210 conn.getNoteTable().updateNoteTitle(n.getGuid(), n.getTitle());
\r
213 setSavedSearchIndex(conn.getSavedSearchTable().getAll());
\r
214 setTagIndex(conn.getTagTable().getAll());
\r
215 setNotebookIndex(conn.getNotebookTable().getAll());
\r
217 List<Notebook> local = conn.getNotebookTable().getAllLocal();
\r
218 localNotebookIndex = new ArrayList<String>();
\r
219 for (int i=0; i<local.size(); i++)
\r
220 localNotebookIndex.add(local.get(i).getGuid());
\r
222 noteModel.setMasterNoteIndex(conn.getNoteTable().getAllNotes());
\r
223 // For performance reasons, we didn't get the tags for every note individually. We now need to
\r
225 List<cx.fbn.nevernote.sql.NoteTagsRecord> noteTags = conn.getNoteTable().noteTagsTable.getAllNoteTags();
\r
226 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
227 List<String> tags = new ArrayList<String>();
\r
228 List<String> names = new ArrayList<String>();
\r
229 for (int j=0; j<noteTags.size(); j++) {
\r
230 if (getMasterNoteIndex().get(i).getGuid().equals(noteTags.get(j).noteGuid)) {
\r
231 tags.add(noteTags.get(j).tagGuid);
\r
232 names.add(getTagNameByGuid(noteTags.get(j).tagGuid));
\r
236 getMasterNoteIndex().get(i).setTagGuids(tags);
\r
237 getMasterNoteIndex().get(i).setTagNames(names);
\r
241 setUnsynchronizedNotes(conn.getNoteTable().getUnsynchronizedGUIDs());
\r
243 enSearchChanged = true;
\r
246 public void reloadIndexes() {
\r
247 setUnsynchronizedNotes(conn.getNoteTable().getUnsynchronizedGUIDs());
\r
249 List<Notebook> local = conn.getNotebookTable().getAllLocal();
\r
250 localNotebookIndex = new ArrayList<String>();
\r
251 for (int i=0; i<local.size(); i++)
\r
252 localNotebookIndex.add(local.get(i).getGuid());
\r
255 setTagIndex(conn.getTagTable().getAll());
\r
257 setNotebookIndex(conn.getNotebookTable().getAll());
\r
258 // load archived notebooks (if note using the EN interface)
\r
259 setArchiveNotebookIndex(conn.getNotebookTable().getAllArchived());
\r
260 // load saved search index
\r
261 setSavedSearchIndex(conn.getSavedSearchTable().getAll());
\r
262 // Load search helper utility
\r
263 enSearch = new EnSearch(conn, logger, "", getTagIndex(), Global.getMinimumWordLength(), Global.getRecognitionWeight());
\r
264 logger.log(logger.HIGH, "Building note index");
\r
266 // if (getMasterNoteIndex() == null) {
\r
267 noteModel.setMasterNoteIndex(conn.getNoteTable().getAllNotes());
\r
269 // For performance reasons, we didn't get the tags for every note individually. We now need to
\r
271 List<cx.fbn.nevernote.sql.NoteTagsRecord> noteTags = conn.getNoteTable().noteTagsTable.getAllNoteTags();
\r
272 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
273 List<String> tags = new ArrayList<String>();
\r
274 List<String> names = new ArrayList<String>();
\r
275 for (int j=0; j<noteTags.size(); j++) {
\r
276 if (getMasterNoteIndex().get(i).getGuid().equals(noteTags.get(j).noteGuid)) {
\r
277 tags.add(noteTags.get(j).tagGuid);
\r
278 names.add(getTagNameByGuid(noteTags.get(j).tagGuid));
\r
282 getMasterNoteIndex().get(i).setTagGuids(tags);
\r
283 getMasterNoteIndex().get(i).setTagNames(names);
\r
286 setNoteIndex(getMasterNoteIndex());
\r
290 //***************************************************************
\r
291 //***************************************************************
\r
292 //* selected notebooks
\r
293 //***************************************************************
\r
294 //***************************************************************
\r
295 // Return the selected notebook(s)
\r
296 public List<String> getSelectedNotebooks() {
\r
297 return selectedNotebooks;
\r
299 // Set the current selected notebook(s)
\r
300 public void setSelectedNotebooks(List <String> s) {
\r
302 s = new ArrayList<String>();
\r
303 selectedNotebooks = s;
\r
307 //***************************************************************
\r
308 //***************************************************************
\r
309 //** These functions deal with setting & retrieving the master lists
\r
310 //***************************************************************
\r
311 //***************************************************************
\r
312 // Get the note table model
\r
313 public NoteTableModel getNoteTableModel() {
\r
316 // save the saved search index
\r
317 private void setSavedSearchIndex(List<SavedSearch> t) {
\r
320 // Retrieve the Tag index
\r
321 public List<SavedSearch> getSavedSearchIndex() {
\r
322 return searchIndex;
\r
325 // save the tag index
\r
326 private void setTagIndex(List<Tag> t) {
\r
329 // Retrieve the Tag index
\r
330 public List<Tag> getTagIndex() {
\r
333 private void setNotebookIndex(List<Notebook> t) {
\r
336 private void setArchiveNotebookIndex(List<Notebook> t) {
\r
337 archiveNotebookIndex = t;
\r
339 // Retrieve the Notebook index
\r
340 public List<Notebook> getNotebookIndex() {
\r
341 return notebookIndex;
\r
344 public List<Notebook> getArchiveNotebookIndex() {
\r
345 return archiveNotebookIndex;
\r
347 // Save the current note list
\r
348 private void setNoteIndex(List<Note> n) {
\r
349 noteModel.setNoteIndex(n);
\r
351 // Get the note index
\r
352 public synchronized List<Note> getNoteIndex() {
\r
353 return noteModel.getNoteIndex();
\r
355 // Save the count of notes per notebook
\r
356 public void setNotebookCounter(List<NotebookCounter> n) {
\r
357 notebookCounter = n;
\r
358 notebookSignal.refreshNotebookTreeCounts.emit(getNotebookIndex(), notebookCounter);
\r
360 public List<NotebookCounter> getNotebookCounter() {
\r
361 return notebookCounter;
\r
363 // Save the count of notes for each tag
\r
364 public void setTagCounter(List<TagCounter> n) {
\r
366 tagSignal.refreshTagTreeCounts.emit(tagCounter);
\r
368 public List<TagCounter> getTagCounter() {
\r
371 public List<String> getLocalNotebooks() {
\r
372 return localNotebookIndex;
\r
374 // Unsynchronized Note List
\r
375 public List<String> getUnsynchronizedNotes() {
\r
376 return noteModel.getUnsynchronizedNotes();
\r
378 public void setUnsynchronizedNotes(List<String> l) {
\r
379 noteModel.setUnsynchronizedNotes(l);
\r
381 // Return a count of items in the trash
\r
382 public int getTrashCount() {
\r
385 // get the EnSearch variable
\r
386 public EnSearch getEnSearch() {
\r
389 public List<Note> getMasterNoteIndex() {
\r
390 return noteModel.getMasterNoteIndex();
\r
393 // public HashMap<String, QImage> getThumbnails() {
\r
394 // return thumbnailList;
\r
396 public QImage getThumbnail(String guid) {
\r
397 // if (getThumbnails().containsKey(guid))
\r
398 // return getThumbnails().get(guid);
\r
400 QImage img = new QImage();
\r
401 img = QImage.fromData(conn.getNoteTable().getThumbnail(guid));
\r
402 if (img == null || img.isNull())
\r
404 //getThumbnails().put(guid, img);
\r
407 public QPixmap getThumbnailPixmap(String guid) {
\r
408 // if (getThumbnails().containsKey(guid))
\r
409 // return getThumbnails().get(guid);
\r
411 QPixmap img = new QPixmap();
\r
412 img.loadFromData(conn.getNoteTable().getThumbnail(guid));
\r
413 if (img == null || img.isNull())
\r
415 //getThumbnails().put(guid, img);
\r
418 //***************************************************************
\r
419 //***************************************************************
\r
420 //** These functions deal with setting & retrieving filters
\r
421 //***************************************************************
\r
422 //***************************************************************
\r
423 public void setEnSearch(String t) {
\r
424 enSearch = new EnSearch(conn,logger, t, getTagIndex(), Global.getMinimumWordLength(), Global.getRecognitionWeight());
\r
425 enSearchChanged = true;
\r
427 // Save search tags
\r
428 public void setSelectedTags(List<String> selectedTags) {
\r
429 this.selectedTags = selectedTags;
\r
431 // Save seleceted search
\r
432 public void setSelectedSavedSearch(String s) {
\r
433 this.selectedSearch = s;
\r
436 public List<String> getSelectedTags() {
\r
437 return selectedTags;
\r
439 // Get saved search
\r
440 public String getSelectedSearch() {
\r
441 return selectedSearch;
\r
447 //***************************************************************
\r
448 //***************************************************************
\r
449 //** Note functions
\r
450 //***************************************************************
\r
451 //***************************************************************
\r
453 public void saveNoteTags(String noteGuid, List<String> tags) {
\r
454 logger.log(logger.HIGH, "Entering ListManager.saveNoteTags");
\r
456 conn.getNoteTable().noteTagsTable.deleteNoteTag(noteGuid);
\r
457 List<String> tagGuids = new ArrayList<String>();
\r
458 boolean newTagCreated = false;
\r
460 for (int i=0; i<tags.size(); i++) {
\r
461 tagName = tags.get(i);
\r
462 boolean found = false;
\r
463 for (int j=0; j<tagIndex.size(); j++) {
\r
464 if (tagIndex.get(j).getName().equalsIgnoreCase(tagName)) {
\r
465 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, tagIndex.get(j).getGuid());
\r
466 tagGuids.add(tagIndex.get(j).getGuid());
\r
467 j=tagIndex.size()+1;
\r
472 Tag nTag = new Tag();
\r
473 nTag.setName(tagName);
\r
474 Calendar currentTime = new GregorianCalendar();
\r
475 Long l = new Long(currentTime.getTimeInMillis());
\r
477 while (l==prevTime) {
\r
478 currentTime = new GregorianCalendar();
\r
479 l=currentTime.getTimeInMillis();
\r
481 String randint = new String(Long.toString(l));
\r
483 nTag.setUpdateSequenceNum(0);
\r
484 nTag.setGuid(randint);
\r
485 conn.getTagTable().addTag(nTag, true);
\r
486 getTagIndex().add(nTag);
\r
487 conn.getNoteTable().noteTagsTable.saveNoteTag(noteGuid, nTag.getGuid());
\r
488 tagGuids.add(nTag.getGuid());
\r
489 newTagCreated = true;
\r
493 for (int i=0; i<getNoteIndex().size(); i++) {
\r
494 if (getNoteIndex().get(i).getGuid().equals(noteGuid)) {
\r
495 getNoteIndex().get(i).setTagNames(tags);
\r
496 getNoteIndex().get(i).setTagGuids(tagGuids);
\r
497 i=getNoteIndex().size()+1;
\r
501 tagSignal.listChanged.emit();
\r
502 logger.log(logger.HIGH, "Leaving ListManager.saveNoteTags");
\r
505 public void deleteNote(String guid) {
\r
506 trashCounterRunner.abortCount = true;
\r
507 Calendar currentTime = new GregorianCalendar();
\r
508 Long l = new Long(currentTime.getTimeInMillis());
\r
510 while (l==prevTime) {
\r
511 currentTime = new GregorianCalendar();
\r
512 l=currentTime.getTimeInMillis();
\r
515 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
516 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
517 getMasterNoteIndex().get(i).setActive(false);
\r
518 getMasterNoteIndex().get(i).setDeleted(l);
\r
519 i=getMasterNoteIndex().size();
\r
522 for (int i=0; i<getNoteIndex().size(); i++) {
\r
523 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
524 getNoteIndex().get(i).setActive(false);
\r
525 getNoteIndex().get(i).setDeleted(l);
\r
526 i=getNoteIndex().size();
\r
529 conn.getNoteTable().deleteNote(guid);
\r
530 reloadTrashCount();
\r
533 public void restoreNote(String guid) {
\r
534 trashCounterRunner.abortCount = true;
\r
535 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
536 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
537 getMasterNoteIndex().get(i).setActive(true);
\r
538 getMasterNoteIndex().get(i).setDeleted(0);
\r
539 i=getMasterNoteIndex().size();
\r
542 for (int i=0; i<getNoteIndex().size(); i++) {
\r
543 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
544 getNoteIndex().get(i).setActive(true);
\r
545 getNoteIndex().get(i).setDeleted(0);
\r
546 i=getNoteIndex().size();
\r
549 conn.getNoteTable().restoreNote(guid);
\r
550 reloadTrashCount();
\r
552 public void updateNote(Note n) {
\r
554 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
555 if (getMasterNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
556 getMasterNoteIndex().remove(i);
\r
557 getMasterNoteIndex().add(n);
\r
560 for (int i=0; i<getNoteIndex().size(); i++) {
\r
561 if (getNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
562 getNoteIndex().get(i).setActive(true);
\r
563 getNoteIndex().get(i).setDeleted(0);
\r
564 i=getNoteIndex().size();
\r
567 conn.getNoteTable().updateNote(n, true);
\r
570 public void addNote(Note n) {
\r
571 noteModel.addNote(n);
\r
574 public void expungeNote(String guid) {
\r
575 trashCounterRunner.abortCount = true;
\r
576 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
577 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
578 getMasterNoteIndex().remove(i);
\r
579 i=getMasterNoteIndex().size();
\r
582 for (int i=0; i<getNoteIndex().size(); i++) {
\r
583 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
584 getNoteIndex().remove(i);
\r
585 i=getNoteIndex().size();
\r
588 conn.getNoteTable().expungeNote(guid, false, true);
\r
589 reloadTrashCount();
\r
592 public void emptyTrash() {
\r
593 trashCounterRunner.abortCount = true;
\r
594 for (int i=getMasterNoteIndex().size()-1; i>=0; i--) {
\r
595 if (!getMasterNoteIndex().get(i).isActive()) {
\r
596 getMasterNoteIndex().remove(i);
\r
600 for (int i=getNoteIndex().size()-1; i>=0; i--) {
\r
601 if (!getNoteIndex().get(i).isActive()) {
\r
602 getNoteIndex().remove(i);
\r
606 conn.getNoteTable().expungeAllDeletedNotes();
\r
607 reloadTrashCount();
\r
609 // The trash counter thread has produced a result
\r
610 @SuppressWarnings("unused")
\r
611 private void trashSignalReceiver(Integer i) {
\r
613 trashSignal.countChanged.emit(i);
\r
615 // Update note contents
\r
616 public void updateNoteContent(String guid, String content) {
\r
617 logger.log(logger.HIGH, "Entering ListManager.updateNoteContent");
\r
618 // EnmlConverter enml = new EnmlConverter(logger);
\r
619 // String text = enml.convert(guid, content);
\r
621 // Update the list tables
\r
622 /* for (int i=0; i<masterNoteIndex.size(); i++) {
\r
623 if (masterNoteIndex.get(i).getGuid().equals(guid)) {
\r
624 masterNoteIndex.get(i).setContent(text);
\r
625 i = masterNoteIndex.size();
\r
628 // Update the list tables
\r
629 for (int i=0; i<getNoteIndex().size(); i++) {
\r
630 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
631 getNoteIndex().get(i).setContent(text);
\r
632 i = getNoteIndex().size();
\r
636 // Check if any new tags were encountered
\r
637 /* if (enml.saveInvalidXML) {
\r
638 List<String> elements = Global.invalidElements;
\r
639 for (int i=0; i<elements.size(); i++) {
\r
640 conn.getInvalidXMLTable().addInvalidElement(elements.get(i));
\r
642 for (String key : Global.invalidAttributes.keySet()) {
\r
643 ArrayList<String> attributes = Global.invalidAttributes.get(key);
\r
644 for (int i=0; i<attributes.size(); i++) {
\r
645 conn.getInvalidXMLTable().addInvalidAttribute(key, attributes.get(i));
\r
650 saveRunner.addWork(guid, content);
\r
651 // conn.getNoteTable().updateNoteContent(guid, content);
\r
652 logger.log(logger.HIGH, "Leaving ListManager.updateNoteContent");
\r
654 // Update a note creation date
\r
655 public void updateNoteCreatedDate(String guid, QDateTime date) {
\r
656 noteModel.updateNoteCreatedDate(guid, date);
\r
657 conn.getNoteTable().updateNoteCreatedDate(guid, date);
\r
659 // Subject date has been changed
\r
660 public void updateNoteSubjectDate(String guid, QDateTime date) {
\r
661 noteModel.updateNoteSubjectDate(guid, date);
\r
662 conn.getNoteTable().updateNoteSubjectDate(guid, date);
\r
664 // Author has changed
\r
665 public void updateNoteAuthor(String guid, String author) {
\r
666 noteModel.updateNoteAuthor(guid, author);
\r
667 conn.getNoteTable().updateNoteAuthor(guid, author);
\r
669 // Author has changed
\r
670 public void updateNoteGeoTag(String guid, Double lon, Double lat, Double alt) {
\r
671 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
672 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
673 getMasterNoteIndex().get(i).getAttributes().setLongitude(lon);
\r
674 getMasterNoteIndex().get(i).getAttributes().setLongitudeIsSet(true);
\r
675 getMasterNoteIndex().get(i).getAttributes().setLatitude(lat);
\r
676 getMasterNoteIndex().get(i).getAttributes().setLatitudeIsSet(true);
\r
677 getMasterNoteIndex().get(i).getAttributes().setAltitude(alt);
\r
678 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);
\r
679 i = getMasterNoteIndex().size();
\r
682 // Update the list tables
\r
683 for (int i=0; i<getNoteIndex().size(); i++) {
\r
684 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
685 getNoteIndex().get(i).getAttributes().setLongitude(lon);
\r
686 getNoteIndex().get(i).getAttributes().setLongitudeIsSet(true);
\r
687 getNoteIndex().get(i).getAttributes().setLatitude(lat);
\r
688 getNoteIndex().get(i).getAttributes().setLatitudeIsSet(true);
\r
689 getNoteIndex().get(i).getAttributes().setAltitude(alt);
\r
690 getNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);
\r
691 i = getNoteIndex().size();
\r
694 conn.getNoteTable().updateNoteGeoTags(guid, lon, lat, alt);
\r
696 // Source URL changed
\r
697 public void updateNoteSourceUrl(String guid, String url) {
\r
698 noteModel.updateNoteSourceUrl(guid, url);
\r
699 conn.getNoteTable().updateNoteSourceUrl(guid, url);
\r
701 // Update a note last changed date
\r
702 public void updateNoteAlteredDate(String guid, QDateTime date) {
\r
703 noteModel.updateNoteChangedDate(guid, date);
\r
704 conn.getNoteTable().updateNoteAlteredDate(guid, date);
\r
706 // Update a note title
\r
707 public void updateNoteTitle(String guid, String title) {
\r
708 logger.log(logger.HIGH, "Entering ListManager.updateNoteTitle");
\r
709 conn.getNoteTable().updateNoteTitle(guid, title);
\r
710 noteModel.updateNoteTitle(guid, title);
\r
711 logger.log(logger.HIGH, "Leaving ListManager.updateNoteTitle");
\r
713 // Update a note's notebook
\r
714 public void updateNoteNotebook(String guid, String notebookGuid) {
\r
715 logger.log(logger.HIGH, "Entering ListManager.updateNoteNotebook");
\r
716 noteModel.updateNoteNotebook(guid, notebookGuid);
\r
717 conn.getNoteTable().updateNoteNotebook(guid, notebookGuid, true);
\r
718 logger.log(logger.HIGH, "Leaving ListManager.updateNoteNotebook");
\r
720 // Update a note sequence number
\r
721 public void updateNoteSequence(String guid, int sequence) {
\r
722 logger.log(logger.HIGH, "Entering ListManager.updateNoteSequence");
\r
724 conn.getNoteTable().updateNoteSequence(guid, sequence);
\r
726 for (int i=0; i<noteModel.getMasterNoteIndex().size(); i++) {
\r
727 if (noteModel.getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
728 noteModel.getMasterNoteIndex().get(i).setUpdateSequenceNum(sequence);
\r
729 i=noteModel.getMasterNoteIndex().size()+1;
\r
733 for (int i=0; i<getNoteIndex().size(); i++) {
\r
734 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
735 getNoteIndex().get(i).setUpdateSequenceNum(sequence);
\r
736 i=getNoteIndex().size()+1;
\r
739 logger.log(logger.HIGH, "Leaving ListManager.updateNoteSequence");
\r
741 public void updateNoteGuid(String oldGuid, String newGuid, boolean updateDatabase) {
\r
742 logger.log(logger.HIGH, "Entering ListManager.updateNoteGuid");
\r
743 if (updateDatabase)
\r
744 conn.getNoteTable().updateNoteGuid(oldGuid, newGuid);
\r
745 noteModel.updateNoteGuid(oldGuid, newGuid);
\r
746 logger.log(logger.HIGH, "Leaving ListManager.updateNoteGuid");
\r
751 //************************************************************************************
\r
752 //************************************************************************************
\r
754 //************************************************************************************
\r
755 //************************************************************************************
\r
756 // Update a tag sequence number
\r
757 public void updateTagSequence(String guid, int sequence) {
\r
758 logger.log(logger.HIGH, "Entering ListManager.updateTagSequence");
\r
760 conn.getTagTable().updateTagSequence(guid, sequence);
\r
761 for (int i=0; i<tagIndex.size(); i++) {
\r
762 if (tagIndex.get(i).getGuid().equals(guid)) {
\r
763 getTagIndex().get(i).setUpdateSequenceNum(sequence);
\r
764 i=tagIndex.size()+1;
\r
767 logger.log(logger.HIGH, "Leaving ListManager.updateTagSequence");
\r
769 // Update a tag guid number
\r
770 public void updateTagGuid(String oldGuid, String newGuid) {
\r
771 logger.log(logger.HIGH, "Entering ListManager.updateTagGuid");
\r
773 conn.getTagTable().updateTagGuid(oldGuid, newGuid);
\r
774 for (int i=0; i<tagIndex.size(); i++) {
\r
775 if (tagIndex.get(i).getGuid().equals(oldGuid)) {
\r
776 tagIndex.get(i).setGuid(newGuid);
\r
777 i=tagIndex.size()+1;
\r
780 logger.log(logger.HIGH, "Leaving ListManager.updateTagGuid");
\r
785 //************************************************************************************
\r
786 //************************************************************************************
\r
787 //** Notebook functions
\r
788 //************************************************************************************
\r
789 //************************************************************************************
\r
790 // Delete a notebook
\r
791 public void deleteNotebook(String guid) {
\r
792 for (int i=0; i<getNotebookIndex().size(); i++) {
\r
793 if (getNotebookIndex().get(i).getGuid().equals(guid)) {
\r
794 getNotebookIndex().remove(i);
\r
795 i=getMasterNoteIndex().size();
\r
798 conn.getNotebookTable().expungeNotebook(guid, true);
\r
801 public void renameStack(String oldName, String newName) {
\r
802 for (int i=0; i<getNotebookIndex().size(); i++) {
\r
803 if (getNotebookIndex().get(i).getStack() != null &&
\r
804 getNotebookIndex().get(i).getStack().equalsIgnoreCase(oldName)) {
\r
805 getNotebookIndex().get(i).setStack(newName);
\r
809 // Update a notebook sequence number
\r
810 public void updateNotebookSequence(String guid, int sequence) {
\r
811 logger.log(logger.HIGH, "Entering ListManager.updateNotebookSequence");
\r
813 conn.getNotebookTable().updateNotebookSequence(guid, sequence);
\r
815 for (int i=0; i<notebookIndex.size(); i++) {
\r
816 if (notebookIndex.get(i).getGuid().equals(guid)) {
\r
817 notebookIndex.get(i).setUpdateSequenceNum(sequence);
\r
818 i=notebookIndex.size()+1;
\r
821 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookSequence");
\r
824 // Update a notebook Guid number
\r
825 public void updateNotebookGuid(String oldGuid, String newGuid) {
\r
826 logger.log(logger.HIGH, "Entering ListManager.updateNotebookGuid");
\r
828 conn.getNotebookTable().updateNotebookGuid(oldGuid, newGuid);
\r
830 for (int i=0; i<notebookIndex.size(); i++) {
\r
831 if (notebookIndex.get(i).getGuid().equals(oldGuid)) {
\r
832 notebookIndex.get(i).setGuid(newGuid);
\r
833 i=notebookIndex.size()+1;
\r
836 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookGuid");
\r
839 // Update a notebook Guid number
\r
840 public void updateNotebookStack(String oldGuid, String stack) {
\r
841 logger.log(logger.HIGH, "Entering ListManager.updateNotebookGuid");
\r
843 conn.getNotebookTable().setStack(oldGuid, stack);
\r
845 for (int i=0; i<notebookIndex.size(); i++) {
\r
846 if (notebookIndex.get(i).getGuid().equals(oldGuid)) {
\r
847 notebookIndex.get(i).setStack(stack);
\r
848 i=notebookIndex.size()+1;
\r
851 logger.log(logger.HIGH, "Leaving ListManager.updateNotebookGuid");
\r
856 //************************************************************************************
\r
857 //************************************************************************************
\r
858 //** Load and filter the note index
\r
859 //************************************************************************************
\r
860 //************************************************************************************
\r
862 public void noteDownloaded(Note n) {
\r
863 boolean found = false;
\r
864 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
865 if (getMasterNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
866 getMasterNoteIndex().set(i,n);
\r
868 i=getMasterNoteIndex().size();
\r
873 getMasterNoteIndex().add(n);
\r
875 for (int i=0; i<getNoteIndex().size(); i++) {
\r
876 if (getNoteIndex().get(i).getGuid().equals(n.getGuid())) {
\r
877 if (filterRecord(getNoteIndex().get(i)))
\r
878 getNoteIndex().add(n);
\r
879 getNoteIndex().remove(i);
\r
880 i=getNoteIndex().size();
\r
884 if (filterRecord(n))
\r
885 getNoteIndex().add(n);
\r
888 // Check if a note matches the currently selected notebooks, tags, or attribute searches.
\r
889 public boolean filterRecord(Note n) {
\r
891 boolean goodNotebook = false;
\r
892 boolean goodTag = false;
\r
893 boolean goodStatus = false;
\r
895 // Check note status
\r
896 if (!n.isActive() && Global.showDeleted)
\r
899 if (n.isActive() && !Global.showDeleted)
\r
903 // Begin filtering results
\r
905 goodNotebook = filterByNotebook(n.getNotebookGuid());
\r
907 goodTag = filterByTag(n.getTagGuids());
\r
909 boolean goodCreatedBefore = false;
\r
910 boolean goodCreatedSince = false;
\r
911 boolean goodChangedBefore = false;
\r
912 boolean goodChangedSince = false;
\r
913 boolean goodContains = false;
\r
914 if (!Global.createdBeforeFilter.hasSelection())
\r
915 goodCreatedBefore = true;
\r
917 goodCreatedBefore = Global.createdBeforeFilter.check(n);
\r
919 if (!Global.createdSinceFilter.hasSelection())
\r
920 goodCreatedSince = true;
\r
922 goodCreatedSince = Global.createdSinceFilter.check(n);
\r
924 if (!Global.changedBeforeFilter.hasSelection())
\r
925 goodChangedBefore = true;
\r
927 goodChangedBefore = Global.changedBeforeFilter.check(n);
\r
928 if (!Global.changedSinceFilter.hasSelection())
\r
929 goodChangedSince = true;
\r
931 goodChangedSince = Global.changedSinceFilter.check(n);
\r
932 if (!Global.containsFilter.hasSelection())
\r
933 goodContains = true;
\r
935 goodContains = Global.containsFilter.check(conn.getNoteTable(), n);
\r
937 if (goodCreatedSince && goodCreatedBefore && goodChangedSince && goodChangedBefore && goodContains)
\r
943 // Load the note index based upon what the user wants.
\r
944 public void loadNotesIndex() {
\r
945 logger.log(logger.EXTREME, "Entering ListManager.loadNotesIndex()");
\r
946 tagCounterRunner.abortCount = true;
\r
947 notebookCounterRunner.abortCount = true;
\r
948 trashCounterRunner.abortCount = true;
\r
950 List<Note> matches;
\r
951 if (enSearchChanged || getMasterNoteIndex() == null)
\r
952 matches = enSearch.matchWords();
\r
954 matches = getMasterNoteIndex();
\r
956 if (matches == null)
\r
957 matches = getMasterNoteIndex();
\r
959 setNoteIndex(new ArrayList<Note>());
\r
960 for (int i=0; i<matches.size(); i++) {
\r
961 if (filterRecord(matches.get(i)))
\r
962 getNoteIndex().add(matches.get(i));
\r
964 countNotebookResults(getNoteIndex());
\r
965 countTagResults(getNoteIndex());
\r
966 enSearchChanged = false;
\r
967 reloadTrashCount();
\r
968 logger.log(logger.EXTREME, "Leaving ListManager.loadNotesIndex()");
\r
970 public void countNotebookResults(List<Note> index) {
\r
971 logger.log(logger.EXTREME, "Entering ListManager.countNotebookResults()");
\r
972 if (!Global.mimicEvernoteInterface) {
\r
973 notebookCounterRunner.setNoteIndex(index);
\r
974 notebookCounterRunner.release(CounterRunner.NOTEBOOK);
\r
976 notebookCounterRunner.setNoteIndex(getMasterNoteIndex());
\r
977 notebookCounterRunner.release(CounterRunner.NOTEBOOK_ALL);
\r
979 logger.log(logger.EXTREME, "Leaving ListManager.countNotebookResults()");
\r
981 public void countTagResults(List<Note> index) {
\r
982 logger.log(logger.EXTREME, "Entering ListManager.countTagResults");
\r
983 if (!Global.tagBehavior().equalsIgnoreCase("DoNothing")) {
\r
984 tagCounterRunner.setNoteIndex(index);
\r
985 tagCounterRunner.release(CounterRunner.TAG);
\r
987 tagCounterRunner.setNoteIndex(null);
\r
988 tagCounterRunner.release(CounterRunner.TAG_ALL);
\r
990 logger.log(logger.EXTREME, "Leaving ListManager.countTagResults()");
\r
992 // Update the count of items in the trash
\r
993 public void reloadTrashCount() {
\r
994 logger.log(logger.EXTREME, "Entering ListManager.reloadTrashCount");
\r
995 trashCounterRunner.setNoteIndex(getNoteIndex());
\r
996 trashCounterRunner.release(CounterRunner.TRASH);
\r
997 logger.log(logger.EXTREME, "Leaving ListManager.reloadTrashCount");
\r
1000 private boolean filterByNotebook(String guid) {
\r
1001 boolean good = false;
\r
1002 if (selectedNotebooks.size() == 0)
\r
1004 if (!good && selectedNotebooks.contains(guid))
\r
1007 for (int i=0; i<getArchiveNotebookIndex().size() && good; i++) {
\r
1008 if (guid.equals(getArchiveNotebookIndex().get(i).getGuid())) {
\r
1015 private boolean filterByTag(List<String> noteTags) {
\r
1016 if (noteTags == null || selectedTags == null)
\r
1019 if (selectedTags.size() == 0)
\r
1022 for (int i=0; i<selectedTags.size(); i++) {
\r
1023 String selectedGuid = selectedTags.get(i);
\r
1024 if (noteTags.contains(selectedGuid))
\r
1030 public void setNoteSynchronized(String guid, boolean value) {
\r
1031 getNoteTableModel().updateNoteSyncStatus(guid, value);
\r
1034 public void updateNoteTitleColor(String guid, Integer color) {
\r
1035 noteModel.updateNoteTitleColor(guid, color);
\r
1036 conn.getNoteTable().setNoteTitleColor(guid, color);
\r
1038 public void loadNoteTitleColors() {
\r
1039 List<Pair<String,Integer>> colors = conn.getNoteTable().getNoteTitleColors();
\r
1040 if (noteModel.getTitleColors() == null)
\r
1041 noteModel.setTitleColors(new HashMap<String,Integer>());
\r
1043 noteModel.getTitleColors().clear();
\r
1044 for (int i=0; i<colors.size(); i++) {
\r
1045 noteModel.getTitleColors().put(colors.get(i).getFirst(), colors.get(i).getSecond());
\r
1049 //********************************************************************************
\r
1050 //********************************************************************************
\r
1051 //* Support signals from the index thread
\r
1052 //********************************************************************************
\r
1053 //********************************************************************************
\r
1054 // Reset a flag if an index is needed
\r
1055 public void setIndexNeeded(String guid, String type, Boolean b) {
\r
1056 if (Global.keepRunning && type.equalsIgnoreCase("content"))
\r
1057 conn.getNoteTable().setIndexNeeded(guid, false);
\r
1058 if (Global.keepRunning && type.equalsIgnoreCase("resource")) {
\r
1059 conn.getNoteTable().noteResourceTable.setIndexNeeded(guid, b);
\r
1063 public boolean threadCheck(int id) {
\r
1064 if (id == Global.notebookCounterThreadId)
\r
1065 return notebookThread.isAlive();
\r
1066 if (id == Global.tagCounterThreadId)
\r
1067 return tagThread.isAlive();
\r
1068 if (id == Global.trashCounterThreadId)
\r
1069 return trashThread.isAlive();
\r
1070 if (id == Global.saveThreadId)
\r
1071 return saveThread.isAlive();
\r
1077 //********************************************************************************
\r
1078 //********************************************************************************
\r
1079 //* Utility Functions
\r
1080 //********************************************************************************
\r
1081 //********************************************************************************
\r
1082 public void compactDatabase() {
\r
1083 conn.compactDatabase();
\r
1084 // IndexConnection idx = new IndexConnection(logger, "nevernote-compact");
\r
1086 // idx.dbShutdown();
\r
1089 // Rebuild the note HTML to something usable
\r
1090 public List<String> scanNoteForResources(Note n) {
\r
1091 logger.log(logger.HIGH, "Entering ListManager.scanNoteForResources");
\r
1092 logger.log(logger.EXTREME, "Note guid: " +n.getGuid());
\r
1093 QDomDocument doc = new QDomDocument();
\r
1094 QDomDocument.Result result = doc.setContent(n.getContent());
\r
1095 if (!result.success) {
\r
1096 logger.log(logger.MEDIUM, "Parse error when scanning note for resources.");
\r
1097 logger.log(logger.MEDIUM, "Note guid: " +n.getGuid());
\r
1101 List<String> returnArray = new ArrayList<String>();
\r
1102 QDomNodeList anchors = doc.elementsByTagName("en-media");
\r
1103 for (int i=0; i<anchors.length(); i++) {
\r
1104 QDomElement enmedia = anchors.at(i).toElement();
\r
1105 if (enmedia.hasAttribute("type")) {
\r
1106 QDomAttr hash = enmedia.attributeNode("hash");
\r
1107 returnArray.add(hash.value().toString());
\r
1110 logger.log(logger.HIGH, "Leaving ListManager.scanNoteForResources");
\r
1111 return returnArray;
\r
1113 // Given a list of tags, produce a string list of tag names
\r
1114 public String getTagNamesForNote(Note n) {
\r
1115 StringBuffer buffer = new StringBuffer(100);
\r
1116 Vector<String> v = new Vector<String>();
\r
1117 List<String> guids = n.getTagGuids();
\r
1119 if (guids == null)
\r
1122 for (int i=0; i<guids.size(); i++) {
\r
1123 v.add(getTagNameByGuid(guids.get(i)));
\r
1125 Comparator<String> comparator = Collections.reverseOrder();
\r
1126 Collections.sort(v,comparator);
\r
1127 Collections.reverse(v);
\r
1129 for (int i = 0; i<v.size(); i++) {
\r
1131 buffer.append(", ");
\r
1132 buffer.append(v.get(i));
\r
1135 return buffer.toString();
\r
1137 // Get a tag name when given a tag guid
\r
1138 public String getTagNameByGuid(String guid) {
\r
1139 for (int i=0; i<getTagIndex().size(); i++) {
\r
1140 String s = getTagIndex().get(i).getGuid();
\r
1141 if (s.equals(guid)) {
\r
1142 return getTagIndex().get(i).getName();
\r
1147 // For a notebook guid, return the name
\r
1148 public String getNotebookNameByGuid(String guid) {
\r
1149 if (notebookIndex == null)
\r
1151 for (int i=0; i<notebookIndex.size(); i++) {
\r
1152 String s = notebookIndex.get(i).getGuid();
\r
1153 if (s.equals(guid)) {
\r
1154 return notebookIndex.get(i).getName();
\r