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.concurrent.LinkedBlockingQueue;
\r
26 import com.evernote.edam.type.Note;
\r
27 import com.trolltech.qt.core.QByteArray;
\r
28 import com.trolltech.qt.core.QFile;
\r
29 import com.trolltech.qt.core.QIODevice.OpenModeFlag;
\r
30 import com.trolltech.qt.core.QObject;
\r
31 import com.trolltech.qt.core.QTemporaryFile;
\r
33 import cx.fbn.nevernote.Global;
\r
34 import cx.fbn.nevernote.signals.NoteSignal;
\r
35 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
36 import cx.fbn.nevernote.utilities.ApplicationLogger;
\r
37 import cx.fbn.nevernote.xml.NoteFormatter;
\r
39 public class ThumbnailRunner extends QObject implements Runnable {
\r
41 private final ApplicationLogger logger;
\r
42 private String guid;
\r
43 public NoteSignal noteSignal;
\r
44 private boolean keepRunning;
\r
45 private final DatabaseConnection conn;
\r
46 private volatile LinkedBlockingQueue<String> workQueue;
\r
47 private static int MAX_QUEUED_WAITING = 1000;
\r
51 public ThumbnailRunner(String logname, String u, String uid, String pswd, String cpswd) {
\r
52 logger = new ApplicationLogger(logname);
\r
53 conn = new DatabaseConnection(logger, u, uid, pswd, cpswd);
\r
54 noteSignal = new NoteSignal();
\r
57 workQueue=new LinkedBlockingQueue<String>(MAX_QUEUED_WAITING);
\r
63 thread().setPriority(Thread.MIN_PRIORITY);
\r
65 logger.log(logger.MEDIUM, "Starting thumbnail thread ");
\r
66 while (keepRunning) {
\r
68 String work = workQueue.take();
\r
69 if (work.startsWith("GENERATE")) {
\r
70 work = work.replace("GENERATE ", "");
\r
72 generateThumbnail();
\r
74 if (work.startsWith("SCAN")) {
\r
77 if (work.startsWith("STOP")) {
\r
78 logger.log(logger.MEDIUM, "Stopping thumbail thread");
\r
79 keepRunning = false;
\r
81 } catch (InterruptedException e) {
\r
82 // TODO Auto-generated catch block
\r
83 e.printStackTrace();
\r
90 private void scanDatabase() {
\r
91 // If there is already work in the queue, that takes priority
\r
92 logger.log(logger.HIGH, "Scanning database for notes needing thumbnail");
\r
93 if (workQueue.size() > 0)
\r
96 // Find a few records that need thumbnails
\r
97 List<String> guids = conn.getNoteTable().findThumbnailsNeeded();
\r
98 logger.log(logger.HIGH, guids.size() +" records returned");
\r
99 for (int i=0; i<guids.size() && keepRunning; i++) {
\r
100 guid = guids.get(i);
\r
101 logger.log(logger.HIGH, "Working on:" +guids.get(i));
\r
102 generateThumbnail();
\r
104 logger.log(logger.HIGH, "Scan completed");
\r
108 public synchronized boolean addWork(String request) {
\r
109 if (workQueue.size() == 0) {
\r
110 workQueue.offer(request);
\r
116 public synchronized int getWorkQueueSize() {
\r
117 return workQueue.size();
\r
120 private void generateThumbnail() {
\r
121 QByteArray js = new QByteArray();
\r
122 logger.log(logger.HIGH, "Starting thumbnail for " +guid);
\r
123 ArrayList<QTemporaryFile> tempFiles = new ArrayList<QTemporaryFile>();
\r
124 Note currentNote = conn.getNoteTable().getNote(guid,true,true,false,true,false);
\r
125 NoteFormatter formatter = new NoteFormatter(logger, conn, tempFiles);
\r
126 currentNote = conn.getNoteTable().getNote(guid, true, true, false, true, false);
\r
127 formatter.setNote(currentNote, true);
\r
128 formatter.setHighlight(null);
\r
129 js.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
\r
130 js.append("<style type=\"text/css\">.en-crypt-temp { border-collapse:collapse; border-style:solid; border-color:blue; padding:0.0mm 0.0mm 0.0mm 0.0mm; }</style>");
\r
131 js.append("<style type=\"text/css\">en-hilight { background-color: rgb(255,255,0) }</style>");
\r
132 js.append("<style> img { max-width:100%; }</style>");
\r
133 js.append("<style type=\"text/css\">en-spell { text-decoration: none; border-bottom: dotted 1px #cc0000; }</style>");
\r
134 js.append("</head>");
\r
135 js.append(formatter.rebuildNoteHTML());
\r
136 js.append("</HTML>");
\r
137 js.replace("<!DOCTYPE en-note SYSTEM 'http://xml.evernote.com/pub/enml.dtd'>", "");
\r
138 js.replace("<!DOCTYPE en-note SYSTEM 'http://xml.evernote.com/pub/enml2.dtd'>", "");
\r
139 js.replace("<?xml version='1.0' encoding='UTF-8'?>", "");
\r
140 String fileName = Global.getFileManager().getResDirPath("thumbnail-" + guid + ".html");
\r
141 QFile tFile = new QFile(fileName);
\r
142 tFile.open(OpenModeFlag.WriteOnly);
\r
145 logger.log(logger.HIGH, "Thumbnail file ready");
\r
146 noteSignal.thumbnailPageReady.emit(guid, fileName);
\r