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.sql.DatabaseConnection;
\r
33 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
34 import cx.fbn.nevernote.utilities.Pair;
\r
36 public class SaveRunner extends QObject implements Runnable {
\r
38 private final ApplicationLogger logger;
\r
39 private volatile boolean keepRunning;
\r
40 public QMutex threadLock;
\r
41 private final DatabaseConnection conn;
\r
42 private boolean idle;
\r
44 private volatile LinkedBlockingQueue<Pair<String, String>> workQueue = new LinkedBlockingQueue<Pair<String, String>>();
\r
47 //*********************************************
\r
49 //*********************************************
\r
50 public SaveRunner(String logname, String u, String uid, String pswd, String cpswd) {
\r
51 logger = new ApplicationLogger(logname);
\r
52 conn = new DatabaseConnection(logger, u, uid, pswd, cpswd);
\r
53 threadLock = new QMutex();
\r
59 //*********************************************
\r
61 //*********************************************
\r
64 thread().setPriority(Thread.MIN_PRIORITY);
\r
65 boolean keepRunning = true;
\r
67 while(keepRunning) {
\r
69 Pair<String, String> content;
\r
71 content = workQueue.take();
\r
72 if (!content.getFirst().equalsIgnoreCase("stop")) {
\r
75 // This is a bit of a hack. It causes this thread to pause for 0.2 seconds.
\r
76 // This helps make sure that the main thread gets to the
\r
77 // database first when switching notes, othrewise it really
\r
78 // slows things down when fetching new notes.
\r
79 GregorianCalendar now = new GregorianCalendar();
\r
80 long prev = now.getTimeInMillis();
\r
82 while (prev>now.getTimeInMillis()) {
\r
83 now = new GregorianCalendar();
\r
86 updateNoteContent(content.getFirst(), content.getSecond());
\r
90 threadLock.unlock();
\r
91 } catch (InterruptedException e) { }
\r
97 public synchronized void addWork(String guid, String content) {
\r
98 while(workQueue.size() > 0) {}
\r
99 Pair<String, String> pair = new Pair<String, String>(guid, content);
\r
100 workQueue.offer(pair);
\r
103 public synchronized void release(String guid, String content) {
\r
104 Pair<String, String> pair = new Pair<String, String>(guid, content);
\r
105 workQueue.add(pair);
\r
108 public synchronized int getWorkQueueSize() {
\r
109 return workQueue.size();
\r
113 //*********************************************
\r
114 //* Getter & Setter method to tell the thread *
\r
115 //* to keep running. *
\r
116 //*********************************************
\r
117 public void setKeepRunning(boolean b) {
\r
120 public boolean keepRunning() {
\r
121 return keepRunning;
\r
124 public boolean isIdle() {
\r
129 //*********************************************
\r
130 //* Do the actual work *
\r
131 //*********************************************
\r
132 public void updateNoteContent(String guid, String content) {
\r
133 logger.log(logger.HIGH, "Entering ListManager.updateNoteContent");
\r
135 // Actually save the content
\r
136 EnmlConverter enml = new EnmlConverter(logger);
\r
137 String newContent = enml.convert(guid, content);
\r
138 String fixedContent = enml.fixEnXMLCrap(newContent);
\r
139 conn.getNoteTable().updateNoteContent(guid, fixedContent);
\r
142 logger.log(logger.EXTREME, "Saving new note resources");
\r
143 List<Resource> oldResources = conn.getNoteTable().noteResourceTable.getNoteResources(guid, false);
\r
144 List<String> newResources = enml.getResources();
\r
145 removeObsoleteResources(oldResources, newResources);
\r
147 logger.log(logger.HIGH, "Leaving ListManager.updateNoteContent");
\r
150 // Remove resources that are no longer needed
\r
151 private void removeObsoleteResources(List<Resource> oldResources, List<String> newResources) {
\r
152 if (oldResources == null || oldResources.size() == 0)
\r
154 if (newResources == null || newResources.size() == 0) {
\r
155 for (int i=0; i<oldResources.size(); i++) {
\r
156 conn.getNoteTable().noteResourceTable.expungeNoteResource(oldResources.get(i).getGuid());
\r
159 for (int i=0; i<oldResources.size(); i++) {
\r
160 boolean matchFound = false;
\r
161 for (int j=0; j<newResources.size(); j++) {
\r
162 if (newResources.get(j).equalsIgnoreCase(oldResources.get(i).getGuid()))
\r
164 if (Global.resourceMap.get(newResources.get(j))!= null) {
\r
165 if (Global.resourceMap.get(newResources.get(j)).equalsIgnoreCase(oldResources.get(i).getGuid()))
\r
169 j = newResources.size();
\r
172 conn.getNoteTable().noteResourceTable.expungeNoteResource(oldResources.get(i).getGuid());
\r