if (!awaitingHttpResponse)
return;
awaitingHttpResponse = false;
QUrl replyUrl = reply.url();
QByteArray image = reply.readAll();
reply.close();
logger.log(logger.EXTREME, "New image size: " +image.size());
Resource newRes = null;
QFile tfile;
String path;
if (latexGuid == null) {
logger.log(logger.EXTREME, "Creating temporary gif");
path = Global.getFileManager().getResDirPath("latex-temp.gif");
tfile = new QFile(path);
tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));
logger.log(logger.EXTREME, "File Open: " +tfile.errorString());
tfile.write(image);
logger.log(logger.EXTREME, "Bytes writtes: "+tfile.size());
tfile.close();
logger.log(logger.EXTREME, "Creating resource");
int sequence = 0;
if (currentNote.getResources() != null || currentNote.getResources().size() > 0)
sequence = currentNote.getResources().size();
newRes = createResource(path,sequence ,"image/gif", false);
QImage pix = new QImage();
pix.loadFromData(image);
newRes.setHeight(new Integer(pix.height()).shortValue());
newRes.setWidth(new Integer(pix.width()).shortValue());
logger.log(logger.EXTREME, "Renaming temporary file to " +newRes.getGuid()+".gif");
path = Global.getFileManager().getResDirPath(newRes.getGuid()+".gif");
tfile.rename(path);
} else {
newRes = conn.getNoteTable().noteResourceTable.getNoteResource(latexGuid, false);
path = Global.getFileManager().getResDirPath(newRes.getGuid()+".gif");
tfile = new QFile(path);
tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));
tfile.write(image);
tfile.close();
newRes.getData().setBody(image.toByteArray());
// Calculate the new hash value
MessageDigest md;
logger.log(logger.EXTREME, "Generating MD5");
try {
md = MessageDigest.getInstance("MD5");
md.update(image.toByteArray());
byte[] hash = md.digest();
newRes.getData().setBodyHash(hash);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
QImage pix = new QImage();
pix.loadFromData(image);
newRes.setHeight(new Integer(pix.height()).shortValue());
newRes.setWidth(new Integer(pix.width()).shortValue());
conn.getNoteTable().noteResourceTable.updateNoteResource(newRes, true);
}
logger.log(logger.EXTREME, "Setting source: " +replyUrl.toString());
newRes.getAttributes().setSourceURL(replyUrl.toString());
conn.getNoteTable().noteResourceTable.updateNoteSourceUrl(newRes.getGuid(), replyUrl.toString(), true);
for(int i=0; i<currentNote.getResourcesSize(); i++) {
if (currentNote.getResources().get(i).getGuid().equals(newRes.getGuid())) {
currentNote.getResources().remove(i);
i=currentNote.getResourcesSize();
}
}
currentNote.getResources().add(newRes);
// do the actual insert into the note. We only do this on new formulas.
if (latexGuid == null) {
StringBuffer buffer = new StringBuffer(100);
String formula = replyUrl.toString().toLowerCase().replace("http://latex.codecogs.com/gif.latex?", "");
buffer.append("<a href=\"latex://"+path.replace("\\", "/")+"\" title=\""+formula+"\""
+"><img src=\"");
buffer.append(path.replace("\\", "/"));
buffer.append("\" en-tag=\"en-latex\" type=\"image/gif\""
+" hash=\""+Global.byteArrayToHexString(newRes.getData().getBodyHash()) +"\""
+" guid=\"" +newRes.getGuid() +"\""
+ " /></a>");
String script_start = new String("document.execCommand('insertHTML', false, '");
String script_end = new String("');");
browser.page().mainFrame().evaluateJavaScript(
script_start + buffer + script_end);
} else {
HtmlTagModifier modifier = new HtmlTagModifier(getContent());
modifier.modifyLatexTagHash(newRes);
String newContent = modifier.getHtml();
setContent(new QByteArray(newContent));
}
logger.log(logger.EXTREME, "New HTML set\n" +browser.page().currentFrame().toHtml());
QWebSettings.setMaximumPagesInCache(0);
QWebSettings.setObjectCacheCapacities(0, 0, 0);