OSDN Git Service

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