OSDN Git Service

Correct LaTeX images. Upgrade to 0.99
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / BrowserWindow.java
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