OSDN Git Service

Correct LaTeX images. Upgrade to 0.99
authorRandy Baumgarte <randy@fbn.cx>
Sat, 30 Apr 2011 11:56:28 +0000 (07:56 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Sat, 30 Apr 2011 12:18:19 +0000 (08:18 -0400)
src/cx/fbn/nevernote/Global.java
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/gui/BrowserWindow.java
src/cx/fbn/nevernote/sql/NoteTable.java
src/cx/fbn/nevernote/xml/HtmlTagModifier.java [new file with mode: 0644]

index a508472..daab703 100644 (file)
@@ -56,8 +56,8 @@ import cx.fbn.nevernote.utilities.ApplicationLogger;
 import cx.fbn.nevernote.utilities.Pair;\r
 \r
 public class Global {\r
-       public static String version = "0.98";\r
-       public static String[] validVersions = {"0.98", "0.97", "0.96"};\r
+       public static String version = "0.99";\r
+       public static String[] validVersions = {"0.99", "0.98", "0.97", "0.96"};\r
     public static String username = ""; \r
     public static String password = "";     \r
     \r
index 96b53c6..e18ba7b 100644 (file)
@@ -4279,7 +4279,10 @@ public class NeverNote extends QMainWindow{
                        js.replace("<!DOCTYPE en-note SYSTEM 'http://xml.evernote.com/pub/enml.dtd'>", "");
                        js.replace("<!DOCTYPE en-note SYSTEM 'http://xml.evernote.com/pub/enml2.dtd'>", "");
                        js.replace("<?xml version='1.0' encoding='UTF-8'?>", "");
-                       browser.getBrowser().setContent(js);
+//             if (Global.enableHTMLEntitiesFix) {
+//                     browser.getBrowser().setContent(new QByteArray(StringEscapeUtils.unescapeHtml(js.toString())));
+//             } else
+                       browser.getBrowser().setContent(js);
                        noteCache.put(currentNoteGuid, js.toString());
 
                        if (formatter.resourceError)
@@ -4602,7 +4605,7 @@ public class NeverNote extends QMainWindow{
        newNote.setUpdateSequenceNum(0);
        newNote.setGuid(randint);
        newNote.setNotebookGuid(notebook);
-       newNote.setTitle("");
+       newNote.setTitle("Untitled Note");
        newNote.setContent(noteString.toString());
        newNote.setDeleted(0);
        newNote.setCreated(System.currentTimeMillis());
@@ -4644,6 +4647,7 @@ public class NeverNote extends QMainWindow{
        // If the window is hidden, then we want to popup this in an external window & 
        if (!isVisible())
                listDoubleClick();
+       waitCursor(false);
        logger.log(logger.HIGH, "Leaving NeverNote.addNote");
     }
     // Restore a note from the trash;
index d9b6d76..f47dd85 100644 (file)
@@ -130,6 +130,7 @@ import cx.fbn.nevernote.sql.DatabaseConnection;
 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
 import cx.fbn.nevernote.utilities.FileUtils;\r
 import cx.fbn.nevernote.utilities.Pair;\r
+import cx.fbn.nevernote.xml.HtmlTagModifier;\r
 \r
 public class BrowserWindow extends QWidget {\r
 \r
@@ -1473,7 +1474,14 @@ public class BrowserWindow extends QWidget {
                        logger.log(logger.EXTREME, "Bytes writtes: "+tfile.size());\r
                        tfile.close();\r
                        logger.log(logger.EXTREME, "Creating resource");\r
-                       newRes = createResource(path,0,"image/gif", false);\r
+                       int sequence = 0;\r
+                       if (currentNote.getResources() != null || currentNote.getResources().size() > 0)\r
+                               sequence = currentNote.getResources().size();\r
+                       newRes = createResource(path,sequence ,"image/gif", false);\r
+                       QImage pix = new QImage();\r
+                       pix.loadFromData(image);\r
+                       newRes.setHeight(new Integer(pix.height()).shortValue());\r
+                       newRes.setWidth(new Integer(pix.width()).shortValue());\r
                        logger.log(logger.EXTREME, "Renaming temporary file to " +newRes.getGuid()+".gif");\r
                        path = Global.getFileManager().getResDirPath(newRes.getGuid()+".gif");\r
                        tfile.rename(path);\r
@@ -1485,6 +1493,22 @@ public class BrowserWindow extends QWidget {
                        tfile.write(image);\r
                        tfile.close();\r
                        newRes.getData().setBody(image.toByteArray());\r
+                       // Calculate the new hash value\r
+               MessageDigest md;\r
+\r
+               logger.log(logger.EXTREME, "Generating MD5");\r
+               try {\r
+                               md = MessageDigest.getInstance("MD5");\r
+                       md.update(image.toByteArray());\r
+                       byte[] hash = md.digest();\r
+                       newRes.getData().setBodyHash(hash);\r
+                       } catch (NoSuchAlgorithmException e) {\r
+                               e.printStackTrace();\r
+                       }\r
+                       QImage pix = new QImage();\r
+                       pix.loadFromData(image);\r
+                       newRes.setHeight(new Integer(pix.height()).shortValue());\r
+                       newRes.setWidth(new Integer(pix.width()).shortValue());\r
                        conn.getNoteTable().noteResourceTable.updateNoteResource(newRes, true);\r
                }\r
 \r
@@ -1501,8 +1525,7 @@ public class BrowserWindow extends QWidget {
                currentNote.getResources().add(newRes);\r
                \r
 \r
-               // do the actual insert into the note.  We only do this on new formulas.  Existing ones we\r
-               // just write out the file (which is aleady done) and reload.\r
+               // do the actual insert into the note.  We only do this on new formulas.  \r
                if (latexGuid == null) {\r
                        StringBuffer buffer = new StringBuffer(100);\r
                        String formula = replyUrl.toString().toLowerCase().replace("http://latex.codecogs.com/gif.latex?", "");\r
@@ -1518,6 +1541,11 @@ public class BrowserWindow extends QWidget {
                        String script_end = new String("');");\r
                        browser.page().mainFrame().evaluateJavaScript(\r
                                        script_start + buffer + script_end);\r
+               } else {\r
+                       HtmlTagModifier modifier = new HtmlTagModifier(getContent());\r
+                       modifier.modifyLatexTagHash(newRes);\r
+                       String newContent = modifier.getHtml();\r
+                       browser.setContent(new QByteArray(newContent));\r
                }\r
 \r
                logger.log(logger.EXTREME, "New HTML set\n" +browser.page().currentFrame().toHtml());\r
@@ -1530,7 +1558,6 @@ public class BrowserWindow extends QWidget {
 //             resourceSignal.contentChanged.emit(path);\r
                unblockTime = -1;\r
        unblockApplication.emit();\r
-\r
                return;\r
                \r
        }\r
index c25d125..5ccfb8f 100644 (file)
@@ -68,8 +68,8 @@ public class NoteTable {
        }\r
        // Create the table\r
        public void createTable() {\r
-               getQueryWithContent = new NSqlQuery(db.getConnection());\r
-               getQueryWithoutContent = new NSqlQuery(db.getConnection());\r
+               //getQueryWithContent = new NSqlQuery(db.getConnection());\r
+               //getQueryWithoutContent = new NSqlQuery(db.getConnection());\r
                NSqlQuery query = new NSqlQuery(db.getConnection());\r
         logger.log(logger.HIGH, "Creating table Note...");\r
         if (!query.exec("Create table Note (guid varchar primary key, " +\r
@@ -164,45 +164,49 @@ public class NoteTable {
                        for (int i=0; i<n.getTagGuids().size(); i++) \r
                                noteTagsTable.saveNoteTag(n.getGuid(), n.getTagGuids().get(i));\r
                }\r
-               \r
                logger.log(logger.EXTREME, "Leaving addNote");\r
        } \r
        // Setup queries for get to save time later\r
        private void prepareQueries() {\r
-               if (getQueryWithContent != null)\r
-                       return;\r
-               getQueryWithContent = new NSqlQuery(db.getConnection());\r
-               getQueryWithoutContent = new NSqlQuery(db.getConnection());\r
-               getAllQueryWithoutContent = new NSqlQuery(db.getConnection());\r
-               \r
-               if (!getQueryWithContent.prepare("Select "\r
-                               +"guid, updateSequenceNumber, title, "\r
-                               +"created, updated, deleted, active, notebookGuid, "\r
-                               +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
-                               +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "\r
-                               +"content, contentHash, contentLength"\r
-                               +" from Note where guid=:guid and isExpunged=false")) {\r
-                                       logger.log(logger.EXTREME, "Note SQL select prepare with content has failed.");\r
-                                       logger.log(logger.MEDIUM, getQueryWithContent.lastError());\r
+               if (getQueryWithContent == null) {\r
+                       getQueryWithContent = new NSqlQuery(db.getConnection());\r
+                       if (!getQueryWithContent.prepare("Select "\r
+                                       +"guid, updateSequenceNumber, title, "\r
+                                       +"created, updated, deleted, active, notebookGuid, "\r
+                                       +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
+                                       +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "\r
+                                       +"content, contentHash, contentLength"\r
+                                       +" from Note where guid=:guid and isExpunged=false")) {\r
+                                               logger.log(logger.EXTREME, "Note SQL select prepare with content has failed.");\r
+                                               logger.log(logger.MEDIUM, getQueryWithContent.lastError());\r
+                       }\r
                }\r
                \r
-               if (!getQueryWithoutContent.prepare("Select "\r
-                               +"guid, updateSequenceNumber, title, "\r
-                               +"created, updated, deleted, active, notebookGuid, "\r
-                               +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
-                               +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "\r
-                               +" from Note where guid=:guid and isExpunged=false")) {\r
-                                       logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");\r
-                                       logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());\r
+               if (getQueryWithoutContent == null) {\r
+                       getQueryWithoutContent = new NSqlQuery(db.getConnection());\r
+                       if (!getQueryWithoutContent.prepare("Select "\r
+                                       +"guid, updateSequenceNumber, title, "\r
+                                       +"created, updated, deleted, active, notebookGuid, "\r
+                                       +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
+                                       +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "\r
+                                       +" from Note where guid=:guid and isExpunged=false")) {\r
+                                               logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");\r
+                                               logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());\r
+                       }\r
                }\r
-               if (!getAllQueryWithoutContent.prepare("Select "\r
+                       \r
+               if (getAllQueryWithoutContent == null) {\r
+                       getAllQueryWithoutContent = new NSqlQuery(db.getConnection());\r
+               \r
+                       if (!getAllQueryWithoutContent.prepare("Select "\r
                                +"guid, updateSequenceNumber, title, "\r
                                +"created, updated, deleted, active, notebookGuid, "\r
                                +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "\r
                                +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication "\r
                                +" from Note where isExpunged = false")) {\r
-                                       logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");\r
+                               logger.log(logger.EXTREME, "Note SQL select prepare without content has failed.");\r
                                        logger.log(logger.MEDIUM, getQueryWithoutContent.lastError());\r
+                       }\r
                }\r
        }\r
 \r
@@ -311,9 +315,17 @@ public class NoteTable {
                        codec = QTextCodec.codecForName("UTF-8");\r
                String unicode =  codec.fromUnicode(query.valueString(16)).toString();\r
 \r
-               if (Global.enableHTMLEntitiesFix)\r
-                       unicode = codec.fromUnicode(StringEscapeUtils.unescapeXml(query.valueString(16).toString())).toString();\r
-               \r
+               // This is a hack.  Basically I need to convert HTML Entities to "normal" text, but if I\r
+               // convert the &lt; character to < it will mess up the XML parsing.  So, to get around this\r
+               // I am "bit stuffing" the &lt; to &&lt; so StringEscapeUtils doesn't unescape it.  After\r
+               // I'm done I convert it back.\r
+               if (Global.enableHTMLEntitiesFix && unicode.indexOf("&#") > 0) {\r
+                       unicode = query.valueString(16);\r
+                       unicode = unicode.replace("&lt;", "&_lt;");\r
+                       unicode = codec.fromUnicode(StringEscapeUtils.unescapeHtml(unicode)).toString();\r
+                       unicode = unicode.replace("&_lt;", "&lt;");\r
+               }\r
+                       \r
                n.setContent(unicode);\r
 //                     n.setContent(query.valueString(16).toString());\r
                        \r
@@ -921,7 +933,9 @@ public class NoteTable {
        public List<Note> getAllNotes() {\r
                List<Note> notes = new ArrayList<Note>();\r
                prepareQueries();\r
-               boolean check;                                  \r
+               boolean check;  \r
+               if (getAllQueryWithoutContent == null) \r
+                       prepareQueries();\r
         NSqlQuery query = getAllQueryWithoutContent;\r
                check = query.exec();\r
                if (!check)\r
@@ -1388,4 +1402,9 @@ public class NoteTable {
                        position = n.getContent().indexOf("<en-media", position+1);\r
                }\r
        }\r
+\r
 }      \r
+\r
+\r
+\r
+\r
diff --git a/src/cx/fbn/nevernote/xml/HtmlTagModifier.java b/src/cx/fbn/nevernote/xml/HtmlTagModifier.java
new file mode 100644 (file)
index 0000000..8eb8bd3
--- /dev/null
@@ -0,0 +1,95 @@
+/*\r
+ * This file is part of NeverNote \r
+ * Copyright 2009 Randy Baumgarte\r
+ * \r
+ * This file may be licensed under the terms of of the\r
+ * GNU General Public License Version 2 (the ``GPL'').\r
+ *\r
+ * Software distributed under the License is distributed\r
+ * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
+ * express or implied. See the GPL for the specific language\r
+ * governing rights and limitations.\r
+ *\r
+ * You should have received a copy of the GPL along with this\r
+ * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
+ * or write to the Free Software Foundation, Inc.,\r
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
+ *\r
+*/\r
+\r
+\r
+package cx.fbn.nevernote.xml;\r
+\r
+import com.evernote.edam.type.Resource;\r
+\r
+import cx.fbn.nevernote.Global;\r
+\r
+public class HtmlTagModifier {\r
+       StringBuffer html;\r
+\r
+       public HtmlTagModifier() {\r
+               html = null;\r
+       }\r
+       \r
+       public HtmlTagModifier(String data) {\r
+               html = new StringBuffer(data);\r
+       }\r
+       \r
+       public void setHtml(String data) {\r
+               html = new StringBuffer(data);\r
+       }\r
+       \r
+       public String getHtml() {\r
+               return html.toString();\r
+       }\r
+       \r
+       public void modifyLatexTagHash(Resource res) {\r
+               \r
+               int position = 0;\r
+               for (; position<html.length();) {\r
+                       position = html.indexOf("<img", position);\r
+                       if (position > 0) {\r
+                               if (matchesGuid(position, res.getGuid())) {\r
+                                       replaceValue(position, "height", new Integer(res.getHeight()).toString());\r
+                                       replaceValue(position, "width", new Integer(res.getWidth()).toString());\r
+                                       replaceValue(position, "hash", Global.byteArrayToHexString(res.getData().getBodyHash()));\r
+                                       return;\r
+                               } \r
+                       }\r
+                       position = position+1;\r
+               }\r
+               return;\r
+       }\r
+       \r
+       \r
+       private boolean matchesGuid(int position, String guid) {\r
+               int endPosition = html.indexOf(">", position);\r
+               if (endPosition < 0)\r
+                       return false;\r
+               \r
+               int guidPos = html.indexOf(guid, position);\r
+               if (guidPos > endPosition) \r
+                       return false;\r
+               else\r
+                       return true;\r
+       }\r
+       \r
+       public void replaceValue(int position, String attribute, String newValue) {\r
+               int endPosition = html.indexOf(">", position);\r
+               if (endPosition < 0)\r
+                       return;\r
+               \r
+               int attributeStart = html.indexOf(attribute, position);\r
+               if (attributeStart < 0)\r
+                       return;\r
+               \r
+               int attributeEnd = html.indexOf(" ", attributeStart);\r
+               if (attributeEnd < 0 || endPosition < attributeEnd) \r
+                       attributeEnd = endPosition-1;\r
+                               \r
+               attributeStart = attributeStart+2+attribute.length();\r
+               html = html.delete(attributeStart, endPosition-1);\r
+               html = html.insert(attributeStart, newValue);\r
+               \r
+       }\r
+}\r