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.threads;
\r
22 import java.util.ArrayList;
\r
23 import java.util.List;
\r
24 import java.util.Vector;
\r
25 import java.util.concurrent.LinkedBlockingQueue;
\r
27 import com.evernote.edam.type.Note;
\r
28 import com.evernote.edam.type.Notebook;
\r
29 import com.evernote.edam.type.Tag;
\r
30 import com.trolltech.qt.core.QMutex;
\r
31 import com.trolltech.qt.core.QObject;
\r
33 import cx.fbn.nevernote.Global;
\r
34 import cx.fbn.nevernote.filters.NotebookCounter;
\r
35 import cx.fbn.nevernote.filters.TagCounter;
\r
36 import cx.fbn.nevernote.signals.NotebookSignal;
\r
37 import cx.fbn.nevernote.signals.TagSignal;
\r
38 import cx.fbn.nevernote.signals.TrashSignal;
\r
39 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
40 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
41 import cx.fbn.nevernote.utilities.Pair;
\r
43 public class CounterRunner extends QObject implements Runnable {
\r
45 private final ApplicationLogger logger;
\r
46 private volatile boolean keepRunning;
\r
48 public volatile NotebookSignal notebookSignal;
\r
49 public volatile TrashSignal trashSignal;
\r
50 public volatile TagSignal tagSignal;
\r
51 private volatile Vector<String> notebookIndex;
\r
52 private volatile Vector<String> noteIndex;
\r
53 private volatile Vector<Boolean> activeIndex;
\r
55 public QMutex threadLock;
\r
57 public static int EXIT=0;
\r
58 public static int NOTEBOOK=1;
\r
59 public static int TAG=2;
\r
60 public static int TRASH=3;
\r
61 public static int TAG_ALL = 4;
\r
62 public static int NOTEBOOK_ALL = 5;
\r
64 public boolean ready = false;
\r
65 public boolean abortCount = false;
\r
66 private final DatabaseConnection conn;
\r
68 private volatile LinkedBlockingQueue<Integer> readyQueue = new LinkedBlockingQueue<Integer>();
\r
71 //*********************************************
\r
73 //*********************************************
\r
74 public CounterRunner(String logname, int t, String u, String i, String r, String uid, String pswd, String cpswd) {
\r
77 threadLock = new QMutex();
\r
78 logger = new ApplicationLogger(logname);
\r
79 // setAutoDelete(false);
\r
80 conn = new DatabaseConnection(logger, u, i, r, uid, pswd, cpswd, 300);
\r
82 notebookSignal = new NotebookSignal();
\r
83 tagSignal = new TagSignal();
\r
84 trashSignal = new TrashSignal();
\r
86 notebookIndex = new Vector<String>();
\r
87 activeIndex = new Vector<Boolean>();
\r
88 noteIndex = new Vector<String>();
\r
93 //*********************************************
\r
95 //*********************************************
\r
98 boolean keepRunning = true;
\r
100 thread().setPriority(Thread.MIN_PRIORITY);
\r
101 while(keepRunning) {
\r
105 type = readyQueue.take();
\r
108 keepRunning = false;
\r
109 if (type == NOTEBOOK)
\r
110 countNotebookResults();
\r
111 if (type == NOTEBOOK_ALL)
\r
112 countNotebookResults();
\r
115 if (type == TAG_ALL)
\r
118 countTrashResults();
\r
119 threadLock.unlock();
\r
120 } catch (InterruptedException e) {}
\r
127 public void setNoteIndex(List<Note> idx) {
\r
130 abortCount = false;
\r
131 notebookIndex.clear();
\r
132 activeIndex.clear();
\r
135 for (int i=0; i<idx.size(); i++) {
\r
136 if (Global.showDeleted && !idx.get(i).isActive()) {
\r
137 notebookIndex.add(new String(idx.get(i).getNotebookGuid()));
\r
138 noteIndex.add(new String(idx.get(i).getGuid()));
\r
139 activeIndex.add(new Boolean(idx.get(i).isActive()));
\r
141 if (!Global.showDeleted && idx.get(i).isActive()) {
\r
142 notebookIndex.add(new String(idx.get(i).getNotebookGuid()));
\r
143 noteIndex.add(new String(idx.get(i).getGuid()));
\r
144 activeIndex.add(new Boolean(idx.get(i).isActive()));
\r
148 threadLock.unlock();
\r
150 public void release(int type) {
\r
151 readyQueue.add(type);
\r
154 //*********************************************
\r
155 //* Getter & Setter method to tell the thread *
\r
156 //* to keep running. *
\r
157 //*********************************************
\r
158 public void setKeepRunning(boolean b) {
\r
161 public boolean keepRunning() {
\r
162 return keepRunning;
\r
166 //*********************************************
\r
167 //* Do the actual counting *
\r
168 //*********************************************
\r
169 private void countNotebookResults() {
\r
170 logger.log(logger.EXTREME, "Entering ListManager.countNotebookResults");
\r
173 List<NotebookCounter> nCounter = new ArrayList<NotebookCounter>();
\r
176 List<Notebook> books = conn.getNotebookTable().getAll();
\r
181 if (type == NOTEBOOK_ALL) {
\r
182 for (int i=0; i<books.size(); i++) {
\r
186 nCounter.add(new NotebookCounter());
\r
187 nCounter.get(i).setCount(0);
\r
188 nCounter.get(i).setGuid(books.get(i).getGuid());
\r
192 List<Pair<String, Integer>> notebookCounts = conn.getNotebookTable().getNotebookCounts();
\r
195 for (int i=0; notebookCounts != null && i<notebookCounts.size(); i++) {
\r
198 for (int j=0; j<nCounter.size(); j++) {
\r
202 if (notebookCounts.get(i).getFirst().equals(nCounter.get(j).getGuid())) {
\r
203 nCounter.get(j).setCount(notebookCounts.get(i).getSecond());
\r
211 notebookSignal.countsChanged.emit(nCounter);
\r
217 for (int i=notebookIndex.size()-1; i>=0 && keepRunning; i--) {
\r
220 boolean notebookFound = false;
\r
221 for (int j=0; j<nCounter.size() && keepRunning; j++) {
\r
224 if (nCounter.get(j).getGuid().equals(notebookIndex.get(i))) {
\r
225 notebookFound = true;
\r
226 if (activeIndex.get(i)) {
\r
227 int c = nCounter.get(j).getCount()+1;
\r
228 nCounter.get(j).setCount(c);
\r
235 if (!notebookFound) {
\r
236 NotebookCounter newCounter = new NotebookCounter();
\r
237 newCounter.setGuid(notebookIndex.get(i));
\r
238 newCounter.setCount(1);
\r
239 nCounter.add(newCounter);
\r
244 notebookSignal.countsChanged.emit(nCounter);
\r
245 logger.log(logger.EXTREME, "Leaving ListManager.countNotebookResults()");
\r
249 private void countTagResults() {
\r
250 logger.log(logger.EXTREME, "Entering ListManager.countTagResults");
\r
251 List<TagCounter> counter = new ArrayList<TagCounter>();
\r
252 List<Tag> allTags = conn.getTagTable().getAll();
\r
256 if (allTags == null)
\r
258 for (int k=0; k<allTags.size() && keepRunning; k++) {
\r
259 TagCounter newCounter = new TagCounter();
\r
260 newCounter.setGuid(allTags.get(k).getGuid());
\r
261 newCounter.setCount(0);
\r
262 counter.add(newCounter);
\r
265 if (type == TAG_ALL) {
\r
266 List<Pair<String, Integer>> tagCounts = conn.getNoteTable().noteTagsTable.getTagCounts();
\r
269 for (int i=0; tagCounts != null && i<tagCounts.size(); i++) {
\r
272 for (int j=0; j<counter.size(); j++) {
\r
275 if (tagCounts.get(i).getFirst().equals(counter.get(j).getGuid())) {
\r
278 counter.get(j).setCount(tagCounts.get(i).getSecond());
\r
285 tagSignal.countsChanged.emit(counter);
\r
292 List<cx.fbn.nevernote.sql.NoteTagsRecord> tags = conn.getNoteTable().noteTagsTable.getAllNoteTags();
\r
293 for (int i=noteIndex.size()-1; i>=0; i--) {
\r
296 String note = noteIndex.get(i);
\r
297 for (int x=0; tags!= null && x<tags.size() && keepRunning; x++) {
\r
300 String tag = tags.get(x).tagGuid;
\r
301 for (int j=0; j<counter.size() && keepRunning; j++) {
\r
304 if (counter.get(j).getGuid().equals(tag) && note.equals(tags.get(x).noteGuid)) {
\r
305 int c = counter.get(j).getCount()+1;
\r
306 counter.get(j).setCount(c);
\r
313 tagSignal.countsChanged.emit(counter);
\r
314 logger.log(logger.EXTREME, "Leaving ListManager.countTagResults()");
\r
318 private void countTrashResults() {
\r
319 logger.log(logger.EXTREME, "Entering CounterRunner.countTrashResults()");
\r
323 Integer tCounter = conn.getNoteTable().getDeletedCount();
\r
328 trashSignal.countChanged.emit(tCounter);
\r
329 logger.log(logger.EXTREME, "Leaving CounterRunner.countTrashResults()");
\r