OSDN Git Service

Cleanup of initial sync with ink notes and added the ability to highlight words in...
authorRandy Baumgarte <randy@fbn.cx>
Mon, 29 Nov 2010 00:32:39 +0000 (19:32 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Wed, 15 Dec 2010 15:16:35 +0000 (10:16 -0500)
src/cx/fbn/nevernote/Global.java
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/config/FileManager.java
src/cx/fbn/nevernote/sql/DatabaseConnection.java
src/cx/fbn/nevernote/sql/NotebookTable.java
src/cx/fbn/nevernote/threads/SyncRunner.java
src/cx/fbn/nevernote/xml/NoteFormatter.java

index 2581e33..90f9cc9 100644 (file)
@@ -563,7 +563,7 @@ public class Global {
                settings.beginGroup("General");\r
                Integer len;\r
                try {\r
                settings.beginGroup("General");\r
                Integer len;\r
                try {\r
-                       len = (Integer)settings.value("recognitionWeight", 80);\r
+                       len = (Integer)settings.value("recognitionWeight", 30);\r
                } catch (Exception e) {\r
                        len = 80;\r
                }\r
                } catch (Exception e) {\r
                        len = 80;\r
                }\r
index 0de1986..e4d8397 100644 (file)
@@ -696,7 +696,7 @@ public class NeverNote extends QMainWindow{
             dbConn = setupDatabaseConnection();
 
             // Must be last stage of setup - only safe once DB is open hence we know we are the only instance running
             dbConn = setupDatabaseConnection();
 
             // Must be last stage of setup - only safe once DB is open hence we know we are the only instance running
-            Global.getFileManager().purgeResDirectory();
+            Global.getFileManager().purgeResDirectory(true);
 
         } catch (InitializationException e) {
             // Fatal
 
         } catch (InitializationException e) {
             // Fatal
@@ -2478,9 +2478,18 @@ public class NeverNote extends QMainWindow{
        // Text in the search bar has been cleared
        private void searchFieldCleared() {
                saveNote();
        // Text in the search bar has been cleared
        private void searchFieldCleared() {
                saveNote();
+               
+               // This is done because we want to force a reload of
+               // images.  Some images we may want to highlight the text.
+               readOnlyCache.clear();
+               noteCache.clear();
+               QWebSettings.setMaximumPagesInCache(0);
+               QWebSettings.setObjectCacheCapacities(0, 0, 0);
+        
                searchField.setEditText("");
                saveNoteColumnPositions();
                saveNoteIndexWidth();
                searchField.setEditText("");
                saveNoteColumnPositions();
                saveNoteIndexWidth();
+               loadNoteBrowserInformation(browserWindow);
        }
        // text in the search bar changed.  We only use this to tell if it was cleared, 
        // otherwise we trigger off searchFieldChanged.
        }
        // text in the search bar changed.  We only use this to tell if it was cleared, 
        // otherwise we trigger off searchFieldChanged.
@@ -2489,11 +2498,15 @@ public class NeverNote extends QMainWindow{
                if (text.trim().equals("")) {
                        searchFieldCleared();
                        if (searchPerformed) {
                if (text.trim().equals("")) {
                        searchFieldCleared();
                        if (searchPerformed) {
+
+                               // This is done because we want to force a reload of
+                               // images.  Some images we may want to highlight the text.
                                noteCache.clear();
                                readOnlyCache.clear();
                                noteCache.clear();
                                readOnlyCache.clear();
+                               QWebSettings.setMaximumPagesInCache(0);
+                               QWebSettings.setObjectCacheCapacities(0, 0, 0);
+                               
                                listManager.setEnSearch("");
                                listManager.setEnSearch("");
-/////                          listManager.clearNoteIndexSearch();
-                               //noteIndexUpdated(true);
                                listManager.loadNotesIndex();
                                refreshEvernoteNote(true);
                                noteIndexUpdated(false);
                                listManager.loadNotesIndex();
                                refreshEvernoteNote(true);
                                noteIndexUpdated(false);
@@ -3901,7 +3914,7 @@ public class NeverNote extends QMainWindow{
        }
 
        private void loadNoteBrowserInformation(BrowserWindow browser) {
        }
 
        private void loadNoteBrowserInformation(BrowserWindow browser) {
-               NoteFormatter formatter = new NoteFormatter(logger, conn, tempFiles);
+               NoteFormatter   formatter = new NoteFormatter(logger, conn, tempFiles);
                formatter.setNote(currentNote, Global.pdfPreview());
                formatter.setHighlight(listManager.getEnSearch());
                QByteArray js;
                formatter.setNote(currentNote, Global.pdfPreview());
                formatter.setHighlight(listManager.getEnSearch());
                QByteArray js;
index eda582b..5f51497 100644 (file)
@@ -265,7 +265,7 @@ public class FileManager {
      *
      * @throws InitializationException for file deletion failures
      */
      *
      * @throws InitializationException for file deletion failures
      */
-    private static void deleteTopLevelFiles(File dir) throws InitializationException {
+    private static void deleteTopLevelFiles(File dir, boolean exitOnFail) throws InitializationException {
         File[] toDelete = dir.listFiles(new FileFilter() {
             @Override
             public boolean accept(File pathname) {
         File[] toDelete = dir.listFiles(new FileFilter() {
             @Override
             public boolean accept(File pathname) {
@@ -273,7 +273,7 @@ public class FileManager {
             }
         });
         for (File f : toDelete) {
             }
         });
         for (File f : toDelete) {
-            if (!f.delete()) {
+            if (!f.delete() && exitOnFail) {
                 throw new InitializationException("Failed to delete file: '" + f + "'");
             }
         }
                 throw new InitializationException("Failed to delete file: '" + f + "'");
             }
         }
@@ -330,7 +330,7 @@ public class FileManager {
     /**
      * Called at startup to purge files from 'res' directory.
      */
     /**
      * Called at startup to purge files from 'res' directory.
      */
-    public void purgeResDirectory() throws InitializationException {
-        deleteTopLevelFiles(resDir);
+    public void purgeResDirectory(boolean exitOnFail) throws InitializationException {
+        deleteTopLevelFiles(resDir, exitOnFail);
     }
 }
     }
 }
index 50bd215..807282b 100644 (file)
@@ -215,7 +215,6 @@ public class DatabaseConnection {
                invalidXMLTable.createTable();
                wordsTable.createTable();
                syncTable.createTable();
                invalidXMLTable.createTable();
                wordsTable.createTable();
                syncTable.createTable();
-               inkImagesTable.createTable();
        }
        
        public Connection getConnection() {
        }
        
        public Connection getConnection() {
index b60b02d..332c100 100644 (file)
@@ -54,7 +54,7 @@ public class NotebookTable {
                dbName = "Notebook";\r
        }\r
        // Create the table\r
                dbName = "Notebook";\r
        }\r
        // Create the table\r
-       public void createTable(boolean addDefault) {\r
+       public void createTable(boolean addDefaulte) {\r
                NSqlQuery query = new NSqlQuery(db.getConnection());\r
         logger.log(logger.HIGH, "Creating table "+dbName+"...");\r
         if (!query.exec("Create table "+dbName+" (guid varchar primary key, " +\r
                NSqlQuery query = new NSqlQuery(db.getConnection());\r
         logger.log(logger.HIGH, "Creating table "+dbName+"...");\r
         if (!query.exec("Create table "+dbName+" (guid varchar primary key, " +\r
@@ -75,9 +75,6 @@ public class NotebookTable {
         newnote.setPublished(false);\r
         newnote.setGuid("1");\r
         \r
         newnote.setPublished(false);\r
         newnote.setGuid("1");\r
         \r
-        if (!addDefault)\r
-               return;\r
-               \r
         // Setup an initial notebook\r
                SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
         query = new NSqlQuery(db.getConnection());\r
         // Setup an initial notebook\r
                SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
         query = new NSqlQuery(db.getConnection());\r
@@ -643,7 +640,7 @@ public class NotebookTable {
        public List<Pair<String,Integer>> getNotebookCounts() {\r
                List<Pair<String,Integer>> counts = new ArrayList<Pair<String,Integer>>();              \r
                NSqlQuery query = new NSqlQuery(db.getConnection());\r
        public List<Pair<String,Integer>> getNotebookCounts() {\r
                List<Pair<String,Integer>> counts = new ArrayList<Pair<String,Integer>>();              \r
                NSqlQuery query = new NSqlQuery(db.getConnection());\r
-               if (!query.exec("select notebookGuid, count(guid) from "+dbName+" where active=1 group by notebookguid;")) {\r
+               if (!query.exec("select notebookGuid, count(guid) from note where active=1 group by notebookguid;")) {\r
                        logger.log(logger.EXTREME, "NoteTags SQL getTagCounts has failed.");\r
                        logger.log(logger.MEDIUM, query.lastError());\r
                        return null;\r
                        logger.log(logger.EXTREME, "NoteTags SQL getTagCounts has failed.");\r
                        logger.log(logger.MEDIUM, query.lastError());\r
                        return null;\r
index 29a2afb..912e63a 100644 (file)
@@ -1076,12 +1076,14 @@ public class SyncRunner extends QObject implements Runnable {
                                \r
                                if (saveNeeded) \r
                                        conn.getNoteTable().noteResourceTable.updateNoteResource(r, false);\r
                                \r
                                if (saveNeeded) \r
                                        conn.getNoteTable().noteResourceTable.updateNoteResource(r, false);\r
+                               if (r.getMime().equalsIgnoreCase("application/vnd.evernote.ink"))\r
+                                       downloadInkNoteImage(r.getGuid());\r
 \r
                        }\r
                }\r
                logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteResources");\r
        }\r
 \r
                        }\r
                }\r
                logger.log(logger.EXTREME, "Leaving SyncRunner.syncRemoteResources");\r
        }\r
-       // Sync remote notebooks\r
+       // Sync remote notes\r
        private void syncRemoteNotes(List<Note> note, boolean fullSync) {\r
                logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotes");\r
                if (note != null) {\r
        private void syncRemoteNotes(List<Note> note, boolean fullSync) {\r
                logger.log(logger.EXTREME, "Entering SyncRunner.syncRemoteNotes");\r
                if (note != null) {\r
@@ -1117,6 +1119,8 @@ public class SyncRunner extends QObject implements Runnable {
                                                        for (int q=0; q<n.getResources().size() && keepRunning; q++) {\r
                                                                logger.log(logger.EXTREME, "Getting note resources.");\r
                                                                conn.getNoteTable().noteResourceTable.updateNoteResource(n.getResources().get(q), false);\r
                                                        for (int q=0; q<n.getResources().size() && keepRunning; q++) {\r
                                                                logger.log(logger.EXTREME, "Getting note resources.");\r
                                                                conn.getNoteTable().noteResourceTable.updateNoteResource(n.getResources().get(q), false);\r
+                                                               if (n.getResources().get(q).getMime().equalsIgnoreCase("application/vnd.evernote.ink"))\r
+                                                                       downloadInkNoteImage(n.getResources().get(q).getGuid());\r
                                                        }\r
                                                }\r
                                        }\r
                                                        }\r
                                                }\r
                                        }\r
index 9347616..d8baebb 100644 (file)
@@ -13,6 +13,10 @@ import com.trolltech.qt.core.QIODevice;
 import com.trolltech.qt.core.QIODevice.OpenModeFlag;\r
 import com.trolltech.qt.core.QTemporaryFile;\r
 import com.trolltech.qt.core.QUrl;\r
 import com.trolltech.qt.core.QIODevice.OpenModeFlag;\r
 import com.trolltech.qt.core.QTemporaryFile;\r
 import com.trolltech.qt.core.QUrl;\r
+import com.trolltech.qt.core.Qt.BGMode;\r
+import com.trolltech.qt.gui.QColor;\r
+import com.trolltech.qt.gui.QPainter;\r
+import com.trolltech.qt.gui.QPixmap;\r
 import com.trolltech.qt.xml.QDomAttr;\r
 import com.trolltech.qt.xml.QDomDocument;\r
 import com.trolltech.qt.xml.QDomElement;\r
 import com.trolltech.qt.xml.QDomAttr;\r
 import com.trolltech.qt.xml.QDomDocument;\r
 import com.trolltech.qt.xml.QDomElement;\r
@@ -115,6 +119,69 @@ public class NoteFormatter {
                return html.toString();\r
        }       \r
 \r
                return html.toString();\r
        }       \r
 \r
+       private void addImageHilight(String resGuid, QFile f) {\r
+               if (enSearch == null || enSearch.hilightWords == null || enSearch.hilightWords.size() == 0)\r
+                       return;\r
+               \r
+               // Get the recognition XML that tells where to hilight on the image\r
+               Resource recoResource = conn.getNoteTable().noteResourceTable.getNoteResourceRecognition(resGuid);\r
+               QByteArray recoData = new QByteArray(recoResource.getRecognition().getBody());\r
+               String xml = recoData.toString();\r
+               \r
+               // Get a painter for the image.  This is the background (the initial image).\r
+       QPixmap pix = new QPixmap(f.fileName());\r
+       QPixmap hilightedPix = new QPixmap(pix.size());\r
+       QPainter p = new QPainter(hilightedPix);\r
+       p.drawPixmap(0,0, pix);\r
+\r
+       // Create a transparent pixmap.  The only non-transparent\r
+       // piece is the hilight that will be overlayed to hilight text no the background\r
+       QPixmap overlayPix = new QPixmap(pix.size());\r
+       QPainter p2 = new QPainter(overlayPix);\r
+       p2.setBackgroundMode(BGMode.TransparentMode);\r
+       p2.setBrush(QColor.yellow);\r
+       \r
+               // Get the recognition data from the note\r
+       QDomDocument doc = new QDomDocument();\r
+       doc.setContent(xml);\r
+       \r
+       // Go through all "item" nodes\r
+               QDomNodeList anchors = doc.elementsByTagName("item");\r
+               for (int i=0; i<anchors.length(); i++) {\r
+                       QDomElement element = anchors.at(i).toElement();\r
+                       int x = new Integer(element.attribute("x"));   // x coordinate\r
+                       int y = new Integer(element.attribute("y"));   // y coordinate\r
+                       int w = new Integer(element.attribute("w"));   // width\r
+                       int h = new Integer(element.attribute("h"));   // height\r
+                       QDomNodeList children = element.childNodes();  // all children ("t" nodes).\r
+                       \r
+                       // Go through the children ("t" nodes)\r
+                       for (int j=0; j<children.length(); j++) {\r
+                       QDomElement child = children.at(j).toElement();\r
+                       if (child.nodeName().equalsIgnoreCase("t")) {\r
+                               String text = child.text();   // recognition text\r
+                               int weight = new Integer(child.attribute("w"));  // recognition weight\r
+                               if (weight >= Global.getRecognitionWeight()) {   // Are we above the maximum?\r
+                                       \r
+                                       // Check to see if this word matches something we were searching for.\r
+                                       for (int k=0; k<enSearch.hilightWords.size(); k++) {\r
+                                               if (enSearch.hilightWords.get(k).equalsIgnoreCase(text))\r
+                                                       p2.drawRect(x,y,w,h);                           \r
+                                       }\r
+                               }\r
+                       }\r
+                       }\r
+               }\r
+       p2.end();\r
+       \r
+       // Paint the hilight onto the background.\r
+       p.setOpacity(0.40);\r
+       p.drawPixmap(0,0, overlayPix);\r
+       p.end();\r
+       \r
+       // Save over the initial pixmap.\r
+       hilightedPix.save(f.fileName());\r
+       }\r
        \r
     // Modify the en-media tag into an image tag so it can be displayed.\r
     private void modifyImageTags(QDomElement docElem, QDomElement enmedia, QDomAttr hash) {\r
        \r
     // Modify the en-media tag into an image tag so it can be displayed.\r
     private void modifyImageTags(QDomElement docElem, QDomElement enmedia, QDomAttr hash) {\r
@@ -127,7 +194,7 @@ public class NoteFormatter {
        \r
        String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNoteGuid, hash.value());\r
        QFile tfile = new QFile(Global.getFileManager().getResDirPath(resGuid + type));\r
        \r
        String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNoteGuid, hash.value());\r
        QFile tfile = new QFile(Global.getFileManager().getResDirPath(resGuid + type));\r
-       if (!tfile.exists()) {\r
+//     if (!tfile.exists()) {\r
                Resource r = null;\r
                if (resGuid != null)\r
                        r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid,true);\r
                Resource r = null;\r
                if (resGuid != null)\r
                        r = conn.getNoteTable().noteResourceTable.getNoteResource(resGuid,true);\r
@@ -138,13 +205,17 @@ public class NoteFormatter {
                                QByteArray binData = new QByteArray(r.getData().getBody());\r
                                tfile.write(binData);\r
                                tfile.close();\r
                                QByteArray binData = new QByteArray(r.getData().getBody());\r
                                tfile.write(binData);\r
                                tfile.close();\r
+                               \r
+                               // If we have recognition text, outline it\r
+                               addImageHilight(r.getGuid(), tfile);\r
+                               \r
                                enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
                                enmedia.setAttribute("en-tag", "en-media");\r
                                enmedia.setNodeValue("");\r
                        enmedia.setAttribute("guid", r.getGuid());\r
                        enmedia.setTagName("img");\r
                }\r
                                enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
                                enmedia.setAttribute("en-tag", "en-media");\r
                                enmedia.setNodeValue("");\r
                        enmedia.setAttribute("guid", r.getGuid());\r
                        enmedia.setTagName("img");\r
                }\r
-       }\r
+//     }\r
        // Technically, we should do a file:// to have a proper url, but for some reason QWebPage hates\r
        // them and won't generate a thumbnail image properly if we use them.\r
 //             enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
        // Technically, we should do a file:// to have a proper url, but for some reason QWebPage hates\r
        // them and won't generate a thumbnail image properly if we use them.\r
 //             enmedia.setAttribute("src", QUrl.fromLocalFile(tfile.fileName()).toString());\r
@@ -474,5 +545,4 @@ public class NoteFormatter {
        return "attachment.png";\r
     }\r
 \r
        return "attachment.png";\r
     }\r
 \r
-\r
 }\r
 }\r