OSDN Git Service

NeverNote 0.88.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / CounterRunner.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.threads;\r
21 \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
26 \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
32 \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.sql.runners.NoteTagsRecord;\r
41 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
42 import cx.fbn.nevernote.utilities.Pair;\r
43 \r
44 public class CounterRunner extends QObject implements Runnable {\r
45          \r
46         private final ApplicationLogger         logger;\r
47         private volatile boolean                        keepRunning;\r
48         public int                                                      ID;\r
49         public volatile NotebookSignal          notebookSignal;\r
50         public volatile TrashSignal                     trashSignal;\r
51         public volatile TagSignal                       tagSignal;\r
52         private volatile Vector<String>         notebookIndex;\r
53         private volatile Vector<String>         noteIndex;\r
54         private volatile Vector<Boolean>        activeIndex;\r
55         public int                                                      type;\r
56         public QMutex                                           threadLock;\r
57         \r
58         public static int                                       EXIT=0;\r
59         public static int                                       NOTEBOOK=1;\r
60         public static int                                       TAG=2;\r
61         public static int                                       TRASH=3;\r
62         public static int                                       TAG_ALL = 4;\r
63         public static int                                       NOTEBOOK_ALL = 5;\r
64         \r
65         public boolean                                          ready = false;\r
66         public boolean                                          abortCount = false;\r
67         private volatile LinkedBlockingQueue<Integer> readyQueue = new LinkedBlockingQueue<Integer>();\r
68         \r
69         \r
70         //*********************************************\r
71         //* Constructor                               *\r
72         //*********************************************\r
73         public CounterRunner(String logname, int t) {\r
74                 type = t;\r
75                 threadLock = new QMutex();\r
76                 logger = new ApplicationLogger(logname);\r
77 //              setAutoDelete(false);   \r
78                 keepRunning = true;\r
79                 notebookSignal = new NotebookSignal();\r
80                 tagSignal = new TagSignal();\r
81                 trashSignal = new TrashSignal();\r
82                 \r
83                 notebookIndex = new Vector<String>();\r
84                 activeIndex = new Vector<Boolean>();\r
85                 noteIndex = new Vector<String>();\r
86         }\r
87         \r
88         \r
89         \r
90         //*********************************************\r
91         //* Run unit                                  *\r
92         //*********************************************\r
93         @Override\r
94         public void run() {\r
95                 boolean keepRunning = true;\r
96                 \r
97                 thread().setPriority(Thread.MIN_PRIORITY);\r
98                 while(keepRunning) {\r
99                         ready = true;\r
100                         try {\r
101                                 \r
102                                 type = readyQueue.take();\r
103                                 threadLock.lock();\r
104                                 if (type == EXIT)\r
105                                         keepRunning = false;\r
106                                 if (type == NOTEBOOK)\r
107                                         countNotebookResults();\r
108                                 if (type == NOTEBOOK_ALL)\r
109                                         countNotebookResults();\r
110                                 if (type == TAG)\r
111                                         countTagResults();\r
112                                 if (type == TAG_ALL)\r
113                                         countTagResults();\r
114                                 if (type == TRASH)\r
115                                         countTrashResults();\r
116                                 threadLock.unlock();\r
117                         } catch (InterruptedException e) {}\r
118                 }\r
119         }\r
120         \r
121         \r
122         \r
123         public void setNoteIndex(List<Note> idx) {\r
124                 abortCount = true;\r
125                 threadLock.lock();\r
126                 abortCount = false;\r
127                 notebookIndex.clear();\r
128                 activeIndex.clear();\r
129                 noteIndex.clear();\r
130                 if (idx != null) {\r
131                         for (int i=0; i<idx.size(); i++) {\r
132                                 if (Global.showDeleted && !idx.get(i).isActive()) {\r
133                                         notebookIndex.add(new String(idx.get(i).getNotebookGuid()));\r
134                                         noteIndex.add(new String(idx.get(i).getGuid()));\r
135                                         activeIndex.add(new Boolean(idx.get(i).isActive()));\r
136                                 }  \r
137                                 if (!Global.showDeleted && idx.get(i).isActive()) {\r
138                                         notebookIndex.add(new String(idx.get(i).getNotebookGuid()));\r
139                                         noteIndex.add(new String(idx.get(i).getGuid()));\r
140                                         activeIndex.add(new Boolean(idx.get(i).isActive()));                                    \r
141                                 }\r
142                         }\r
143                 }\r
144                 threadLock.unlock();\r
145         }\r
146         public void release(int type) {\r
147                 readyQueue.add(type);\r
148         }\r
149         \r
150         //*********************************************\r
151         //* Getter & Setter method to tell the thread *\r
152         //* to keep running.                          *\r
153         //*********************************************\r
154         public void setKeepRunning(boolean b) {\r
155                 keepRunning = b;\r
156         }\r
157         public boolean keepRunning() {\r
158                 return keepRunning;\r
159         }\r
160         \r
161         \r
162         //*********************************************\r
163         //* Do the actual counting                    *\r
164         //*********************************************\r
165         private void countNotebookResults() {\r
166                 logger.log(logger.EXTREME, "Entering ListManager.countNotebookResults");                \r
167                 if (abortCount)\r
168                         return;\r
169                 DatabaseConnection conn = new DatabaseConnection(logger, Global.tagCounterThreadId);\r
170                 List<NotebookCounter> nCounter = new ArrayList<NotebookCounter>();\r
171                 if (abortCount)\r
172                         return;\r
173                 List<Notebook> books = conn.getNotebookTable().getAll();\r
174                                 \r
175                 if (abortCount)\r
176                         return;\r
177 \r
178                 if (type == NOTEBOOK_ALL) {\r
179                         for (int i=0; i<books.size(); i++) {\r
180                                 if (abortCount)\r
181                                         return;\r
182 \r
183                                 nCounter.add(new NotebookCounter());\r
184                                 nCounter.get(i).setCount(0);\r
185                                 nCounter.get(i).setGuid(books.get(i).getGuid());\r
186                         }\r
187                         if (abortCount)\r
188                                 return;\r
189                         List<Pair<String, Integer>> notebookCounts = conn.getNotebookTable().getNotebookCounts();\r
190                         if (abortCount)\r
191                                 return;\r
192                         for (int i=0; notebookCounts != null && i<notebookCounts.size(); i++) {\r
193                                 if (abortCount)\r
194                                         return;\r
195                                 for (int j=0; j<nCounter.size(); j++) {\r
196                                         if (abortCount)\r
197                                                 return;\r
198 \r
199                                         if (notebookCounts.get(i).getFirst().equals(nCounter.get(j).getGuid())) {\r
200                                                 nCounter.get(j).setCount(notebookCounts.get(i).getSecond());\r
201                                                 j=nCounter.size();\r
202                                         }\r
203                                 }\r
204                         }\r
205                         if (abortCount)\r
206                                 return;\r
207 \r
208                         notebookSignal.countsChanged.emit(nCounter);\r
209                         return;\r
210                 }\r
211                 \r
212                 if (abortCount)\r
213                         return;\r
214                 for (int i=notebookIndex.size()-1; i>=0 && keepRunning; i--) {\r
215                         if (abortCount)\r
216                                 return;\r
217                         boolean notebookFound = false;\r
218                         for (int j=0; j<nCounter.size() && keepRunning; j++) {\r
219                                 if (abortCount)\r
220                                         return;\r
221                                 if (nCounter.get(j).getGuid().equals(notebookIndex.get(i))) {\r
222                                         notebookFound = true;\r
223                                         if (activeIndex.get(i)) {\r
224                                                 int c = nCounter.get(j).getCount()+1;\r
225                                                 nCounter.get(j).setCount(c);\r
226                                         }\r
227                                         j=nCounter.size();\r
228                                 }\r
229                         }\r
230                         if (abortCount)\r
231                                 return;\r
232                         if (!notebookFound) {\r
233                                 NotebookCounter newCounter = new NotebookCounter();\r
234                                 newCounter.setGuid(notebookIndex.get(i));\r
235                                 newCounter.setCount(1);\r
236                                 nCounter.add(newCounter);\r
237                         }\r
238                 }\r
239                 if (abortCount)\r
240                         return;\r
241                 notebookSignal.countsChanged.emit(nCounter);\r
242                 logger.log(logger.EXTREME, "Leaving ListManager.countNotebookResults()");\r
243         }\r
244         \r
245         \r
246         private void countTagResults() {\r
247                 logger.log(logger.EXTREME, "Entering ListManager.countTagResults");             \r
248                 DatabaseConnection conn = new DatabaseConnection(logger, Global.tagCounterThreadId);\r
249                 List<TagCounter> counter = new ArrayList<TagCounter>();\r
250                 List<Tag> allTags = conn.getTagTable().getAll();\r
251                 \r
252                 if (abortCount) \r
253                         return;\r
254                 if (allTags == null)\r
255                         return;\r
256                 for (int k=0; k<allTags.size() && keepRunning; k++) {\r
257                         TagCounter newCounter = new TagCounter();\r
258                         newCounter.setGuid(allTags.get(k).getGuid());\r
259                         newCounter.setCount(0);\r
260                         counter.add(newCounter);\r
261                 }\r
262                 \r
263                 if (type == TAG_ALL) {\r
264                         List<Pair<String, Integer>> tagCounts = conn.getNoteTable().noteTagsTable.getTagCounts();\r
265                         if (abortCount)\r
266                                 return;\r
267                         for (int i=0; tagCounts != null &&  i<tagCounts.size(); i++) {\r
268                                 if (abortCount)\r
269                                         return;\r
270                                 for (int j=0; j<counter.size(); j++) {\r
271                                         if (abortCount)\r
272                                                 return;\r
273                                         if (tagCounts.get(i).getFirst().equals(counter.get(j).getGuid())) {\r
274                                                 if (abortCount)\r
275                                                         return;\r
276                                                 counter.get(j).setCount(tagCounts.get(i).getSecond());\r
277                                                 j=counter.size();\r
278                                         }\r
279                                 }\r
280                         }\r
281                         if (abortCount)\r
282                                 return;\r
283                         tagSignal.countsChanged.emit(counter);\r
284                         return;\r
285                 }\r
286                 \r
287                 \r
288                 if (abortCount)\r
289                         return;\r
290                 List<NoteTagsRecord> tags = conn.getNoteTable().noteTagsTable.getAllNoteTags();\r
291                 for (int i=noteIndex.size()-1; i>=0; i--) {\r
292                         if (abortCount)\r
293                                 return;\r
294                         String note = noteIndex.get(i);\r
295                         for (int x=0; tags!= null && x<tags.size() && keepRunning; x++) {\r
296                                 if (abortCount)\r
297                                         return;\r
298                                 String tag = tags.get(x).tagGuid;\r
299                                 for (int j=0; j<counter.size() && keepRunning; j++) {\r
300                                         if (abortCount)\r
301                                                 return;\r
302                                         if (counter.get(j).getGuid().equals(tag) && note.equals(tags.get(x).noteGuid)) {\r
303                                                 int c = counter.get(j).getCount()+1;\r
304                                                 counter.get(j).setCount(c);\r
305                                         }\r
306                                 }\r
307                         }\r
308                 }\r
309                 if (abortCount)\r
310                         return;\r
311                 tagSignal.countsChanged.emit(counter);\r
312                 logger.log(logger.EXTREME, "Leaving ListManager.countTagResults()");\r
313         }\r
314         \r
315         \r
316         private void countTrashResults() {\r
317                 logger.log(logger.EXTREME, "Entering CounterRunner.countTrashResults()");               \r
318                 DatabaseConnection conn = new DatabaseConnection(logger, Global.trashCounterThreadId);\r
319                 if (abortCount)\r
320                         return;\r
321 \r
322                 Integer tCounter = conn.getNoteTable().getDeletedCount();\r
323                 \r
324                 if (abortCount)\r
325                         return;\r
326 \r
327                 trashSignal.countChanged.emit(tCounter);\r
328                 logger.log(logger.EXTREME, "Leaving CounterRunner.countTrashResults()");\r
329         }\r
330 \r
331 }\r