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.GregorianCalendar;
\r
23 import java.util.List;
\r
24 import java.util.concurrent.LinkedBlockingQueue;
\r
26 import com.evernote.edam.type.Resource;
\r
27 import com.trolltech.qt.core.QMutex;
\r
28 import com.trolltech.qt.core.QObject;
\r
30 import cx.fbn.nevernote.Global;
\r
31 import cx.fbn.nevernote.evernote.EnmlConverter;
\r
32 import cx.fbn.nevernote.signals.NoteSignal;
\r
33 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
34 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
35 import cx.fbn.nevernote.utilities.Pair;
\r
37 public class SaveRunner extends QObject implements Runnable {
\r
39 private final ApplicationLogger logger;
\r
40 public volatile boolean keepRunning;
\r
41 public QMutex threadLock;
\r
42 private final DatabaseConnection conn;
\r
43 private boolean idle;
\r
44 public NoteSignal noteSignals;
\r
46 private volatile LinkedBlockingQueue<Pair<String, String>> workQueue = new LinkedBlockingQueue<Pair<String, String>>();
\r
49 //*********************************************
\r
51 //*********************************************
\r
52 public SaveRunner(String logname, String u, String uid, String pswd, String cpswd) {
\r
53 logger = new ApplicationLogger(logname);
\r
54 conn = new DatabaseConnection(logger, u, uid, pswd, cpswd, 0);
\r
55 threadLock = new QMutex();
\r
57 noteSignals = new NoteSignal();
\r
62 //*********************************************
\r
64 //*********************************************
\r
67 thread().setPriority(Thread.MIN_PRIORITY);
\r
68 boolean keepRunning = true;
\r
70 while(keepRunning) {
\r
72 Pair<String, String> content;
\r
74 content = workQueue.take();
\r
75 if (!content.getFirst().equalsIgnoreCase("stop")) {
\r
78 // This is a bit of a hack. It causes this thread to pause for 0.2 seconds.
\r
79 // This helps make sure that the main thread gets to the
\r
80 // database first when switching notes, othrewise it really
\r
81 // slows things down when fetching new notes.
\r
82 GregorianCalendar now = new GregorianCalendar();
\r
83 long prev = now.getTimeInMillis();
\r
85 while (prev>now.getTimeInMillis()) {
\r
86 now = new GregorianCalendar();
\r
89 updateNoteContent(content.getFirst(), content.getSecond());
\r
93 threadLock.unlock();
\r
94 } catch (InterruptedException e) { }
\r
100 public synchronized void addWork(String guid, String content) {
\r
101 while(workQueue.size() > 0) {}
\r
102 Pair<String, String> pair = new Pair<String, String>(guid, content);
\r
103 workQueue.offer(pair);
\r
106 public synchronized void release(String guid, String content) {
\r
107 Pair<String, String> pair = new Pair<String, String>(guid, content);
\r
108 workQueue.add(pair);
\r
111 public synchronized int getWorkQueueSize() {
\r
112 return workQueue.size();
\r
116 //*********************************************
\r
117 //* Getter & Setter method to tell the thread *
\r
118 //* to keep running. *
\r
119 //*********************************************
\r
120 public void setKeepRunning(boolean b) {
\r
123 public boolean keepRunning() {
\r
124 return keepRunning;
\r
127 public boolean isIdle() {
\r
132 //*********************************************
\r
133 //* Do the actual work *
\r
134 //*********************************************
\r
135 public void updateNoteContent(String guid, String content) {
\r
136 logger.log(logger.HIGH, "Entering ListManager.updateNoteContent");
\r
138 // Actually save the content
\r
139 EnmlConverter enml = new EnmlConverter(logger);
\r
140 String newContent = enml.convert(guid, content);
\r
141 String fixedContent = enml.fixEnXMLCrap(newContent);
\r
142 if (fixedContent != null) {
\r
143 conn.getNoteTable().updateNoteContent(guid, fixedContent);
\r
144 logger.log(logger.EXTREME, "Saving new note resources");
\r
145 List<Resource> oldResources = conn.getNoteTable().noteResourceTable.getNoteResources(guid, false);
\r
146 List<String> newResources = enml.getResources();
\r
147 removeObsoleteResources(oldResources, newResources);
\r
149 noteSignals.noteSaveRunnerError.emit(guid, null);
\r
151 logger.log(logger.HIGH, "Leaving ListManager.updateNoteContent");
\r
154 // Remove resources that are no longer needed
\r
155 private void removeObsoleteResources(List<Resource> oldResources, List<String> newResources) {
\r
156 if (oldResources == null || oldResources.size() == 0)
\r
158 if (newResources == null || newResources.size() == 0) {
\r
159 for (int i=0; i<oldResources.size(); i++) {
\r
160 conn.getNoteTable().noteResourceTable.expungeNoteResource(oldResources.get(i).getGuid());
\r
163 for (int i=0; i<oldResources.size(); i++) {
\r
164 boolean matchFound = false;
\r
165 for (int j=0; j<newResources.size(); j++) {
\r
166 if (newResources.get(j).equalsIgnoreCase(oldResources.get(i).getGuid()))
\r
168 if (Global.resourceMap.get(newResources.get(j))!= null) {
\r
169 if (Global.resourceMap.get(newResources.get(j)).equalsIgnoreCase(oldResources.get(i).getGuid()))
\r
173 j = newResources.size();
\r
176 conn.getNoteTable().noteResourceTable.expungeNoteResource(oldResources.get(i).getGuid());
\r