// * User chose to save an attachment. Pares out the request *
// * into a guid & file. Save the result. *
// ************************************************************
public void downloadAttachment(QNetworkRequest request) {
String guid;
QFileDialog fd = new QFileDialog(this);
fd.setFileMode(FileMode.AnyFile);
fd.setConfirmOverwrite(true);
fd.setWindowTitle(tr("Save File"));
fd.setAcceptMode(AcceptMode.AcceptSave);
fd.setDirectory(System.getProperty("user.home"));
String name = request.url().toString();
int pos = name.lastIndexOf(Global.attachmentNameDelimeter);
if (pos > -1) {
guid = name.substring(0, pos).replace("nnres://", "");
name = name.substring(pos +Global.attachmentNameDelimeter.length());
fd.selectFile(name);
pos = name.lastIndexOf('.');
if (pos > -1) {
String mimeType = "(*." + name.substring(pos + 1)
+ ");; All Files (*)";
fd.setFilter(tr(mimeType));
}
} else {
guid = name;
}
// Strip URL prefix and base dir
guid = guid.replace("nnres://", "")
.replace(FileUtils.toForwardSlashedPath(Global.getFileManager().getResDirPath()), "");
guid = guid.replace("file://", "").replace("/", "")
.replace(FileUtils.toForwardSlashedPath(Global.getFileManager().getResDirPath()), "");
pos = guid.lastIndexOf('.');
if (pos > 0)
guid = guid.substring(0,pos);
if (fd.exec() != 0 && fd.selectedFiles().size() > 0) {
name = name.replace('\\', '/');
Resource resBinary = conn.getNoteTable().noteResourceTable.getNoteResource(guid, true);
QFile saveFile = new QFile(fd.selectedFiles().get(0));
QFile.OpenMode mode = new QFile.OpenMode();
mode.set(QFile.OpenModeFlag.WriteOnly);
saveFile.open(mode);
QDataStream saveOut = new QDataStream(saveFile);
QByteArray binData = new QByteArray(resBinary.getData().getBody());