OSDN Git Service

Change generation of files/paths in res dir to use FileManager
authorNick Clarke <memorius@gmail.com>
Tue, 20 Jul 2010 01:10:43 +0000 (13:10 +1200)
committerRandy Baumgarte <randy@fbn.cx>
Fri, 23 Jul 2010 09:31:06 +0000 (05:31 -0400)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/dialog/OnlineNoteHistory.java
src/cx/fbn/nevernote/dialog/PreviewDialog.java
src/cx/fbn/nevernote/gui/BrowserWindow.java
src/cx/fbn/nevernote/gui/Thumbnailer.java

index 9bebc03..c861eb4 100644 (file)
@@ -120,6 +120,7 @@ import com.trolltech.qt.xml.QDomDocument;
 import com.trolltech.qt.xml.QDomElement;
 import com.trolltech.qt.xml.QDomNodeList;
 
+import cx.fbn.nevernote.config.FileManager;
 import cx.fbn.nevernote.config.InitializationException;
 import cx.fbn.nevernote.config.StartupConfig;
 import cx.fbn.nevernote.dialog.AccountDialog;
@@ -156,6 +157,7 @@ import cx.fbn.nevernote.threads.SyncRunner;
 import cx.fbn.nevernote.utilities.AESEncrypter;
 import cx.fbn.nevernote.utilities.ApplicationLogger;
 import cx.fbn.nevernote.utilities.FileImporter;
+import cx.fbn.nevernote.utilities.FileUtils;
 import cx.fbn.nevernote.utilities.ListManager;
 import cx.fbn.nevernote.utilities.SyncTimes;
 import cx.fbn.nevernote.xml.ExportData;
@@ -3096,7 +3098,7 @@ public class NeverNote extends QMainWindow{
        // Save a generated thumbnail
        @SuppressWarnings("unused")
        private void saveThumbnail(String guid) {
-               QFile tFile = new QFile(Global.currentDir+"res/thumbnail-"+guid+".png");
+               QFile tFile = new QFile(Global.getFileManager().getResDirPath("thumbnail-" + guid + ".png"));
                tFile.open(OpenModeFlag.ReadOnly);
                QByteArray imgBytes = tFile.readAll();
                tFile.close();
@@ -3604,7 +3606,7 @@ public class NeverNote extends QMainWindow{
        // View a thumbnail of the note
        public void thumbnailView() {
                
-               String thumbnailName = Global.currentDir+"res/thumbnail-"+currentNoteGuid+".png";
+               String thumbnailName = Global.getFileManager().getResDirPath("thumbnail-" + currentNoteGuid + ".png");
                QFile thumbnail = new QFile(thumbnailName);
                if (!thumbnail.exists()) {
                        
@@ -3806,14 +3808,13 @@ public class NeverNote extends QMainWindow{
                                if (r.getAttributes() != null && r.getAttributes().getFileName() != null && !r.getAttributes().getFileName().equals(""))
                                        fileDetails = r.getAttributes().getFileName();
                                String contextFileName;
-                               String pathPrefix = Global.currentDir;
-                               pathPrefix = Global.getDirectoryPath()+"res/";
-                               if (fileDetails != null && !fileDetails.equals("")) {
+                               FileManager fileManager = Global.getFileManager();
+                                if (fileDetails != null && !fileDetails.equals("")) {
                                        enmedia.setAttribute("href", "nnres://" +r.getGuid() +Global.attachmentNameDelimeter +fileDetails);
-                                       contextFileName = Global.currentDir +"res/" +r.getGuid() +Global.attachmentNameDelimeter +fileDetails;
+                                       contextFileName = fileManager.getResDirPath(r.getGuid() + Global.attachmentNameDelimeter + fileDetails);
                                } else { 
                                        enmedia.setAttribute("href", "nnres://" +r.getGuid() +Global.attachmentNameDelimeter +appl);
-                                       contextFileName = pathPrefix+r.getGuid() +Global.attachmentNameDelimeter +appl;
+                                       contextFileName = fileManager.getResDirPath(r.getGuid() + Global.attachmentNameDelimeter + appl);
                                }
                                contextFileName = contextFileName.replace("\\", "/");
                                enmedia.setAttribute("onContextMenu", "window.jambi.resourceContextMenu('" +contextFileName +"');");
@@ -3834,7 +3835,7 @@ public class NeverNote extends QMainWindow{
                                                fileName = res.getGuid()+Global.attachmentNameDelimeter+res.getAttributes().getFileName();
                                        else
                                                fileName = res.getGuid()+".pdf";
-                                       QFile file = new QFile(Global.getDirectoryPath() +"res/"+fileName);
+                                       QFile file = new QFile(fileManager.getResDirPath(fileName));
                                QFile.OpenMode mode = new QFile.OpenMode();
                                mode.set(QFile.OpenModeFlag.WriteOnly);
                                file.open(mode);
@@ -3879,7 +3880,8 @@ public class NeverNote extends QMainWindow{
                                // NFC TODO: should this be a 'file://' URL?
                                newText.setAttribute("src", Global.getFileManager().getImageDirPath(icon));
                                if (goodPreview) {
-                                       newText.setAttribute("src", Global.getDirectoryPath()+"res/"+filePath);
+                                       // NFC TODO: should this be a 'file://' URL?
+                                       newText.setAttribute("src", fileManager.getResDirPath(filePath));
                                        newText.setAttribute("style", "border-style:solid; border-color:green; padding:0.5mm 0.5mm 0.5mm 0.5mm;");
                                }
                                newText.setAttribute("title", fileDetails);
@@ -3932,7 +3934,7 @@ public class NeverNote extends QMainWindow{
                type="";
        
        String resGuid = conn.getNoteTable().noteResourceTable.getNoteResourceGuidByHashHex(currentNoteGuid, hash.value());
-       QFile tfile = new QFile(Global.getDirectoryPath()+"res"+File.separator +resGuid+type);
+       QFile tfile = new QFile(Global.getFileManager().getResDirPath(resGuid + type));
        if (!tfile.exists()) {
                Resource r = null;
                if (resGuid != null)
@@ -4731,10 +4733,11 @@ public class NeverNote extends QMainWindow{
        //**************************************************
        private void externalFileEdited(String fileName) throws NoSuchAlgorithmException {
                logger.log(logger.HIGH, "Entering exernalFileEdited");
-               
-               String dPath = Global.getDirectoryPath() + "res/";
-               dPath = dPath.replace('\\', '/');
+
+               // Strip URL prefix and base dir path
+               String dPath = FileUtils.toForwardSlashedPath(Global.getFileManager().getResDirPath());
                String name = fileName.replace(dPath, "");
+
                int pos = name.lastIndexOf('.');
                String guid = name;
                if (pos > -1) {
index ce58148..8a47b9c 100644 (file)
@@ -353,7 +353,7 @@ public class OnlineNoteHistory extends QDialog {
                if (r==null)\r
                        return;\r
                \r
-       QFile tfile = new QFile(Global.getDirectoryPath()+"res"+File.separator +r.getGuid()+note.getUpdateSequenceNum()+type);\r
+       QFile tfile = new QFile(Global.getFileManager().getResDirPath(r.getGuid() + note.getUpdateSequenceNum() + type));\r
        if (!tfile.exists()) {\r
                        if (r!= null && r.getData() != null && r.getData().getBody().length > 0) {\r
                                tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
index 3b7d302..a0f0135 100644 (file)
@@ -41,13 +41,13 @@ public class PreviewDialog extends QDialog {
                \r
                PreviewImage imageLabel = new PreviewImage(masterLabel);\r
                imageLabel.ID = "Number 1";\r
-        QImage image = new QImage(Global.currentDir+"res/thumbnail.png");\r
+        QImage image = new QImage(Global.getFileManager().getResDirPath("thumbnail.png"));\r
         imageLabel.setPixmap(QPixmap.fromImage(image).scaled(400, 400));\r
         subLayout.addWidget(imageLabel);\r
 \r
                PreviewImage imageLabel2 = new PreviewImage(masterLabel);\r
                imageLabel2.ID = "Number 2";\r
-               QImage image2 = new QImage(Global.currentDir+"res/thumbnail.png");\r
+               QImage image2 = new QImage(Global.getFileManager().getResDirPath("thumbnail.png"));\r
         imageLabel2.setPixmap(QPixmap.fromImage(image2).scaled(400,400));\r
         subLayout.addWidget(imageLabel2);\r
         masterLabel.setLayout(subLayout);\r
index 898d978..ca12e3a 100644 (file)
@@ -715,7 +715,7 @@ public class BrowserWindow extends QWidget {
                                        fileName = res.getGuid()+Global.attachmentNameDelimeter+res.getAttributes().getFileName();\r
                                else\r
                                        fileName = res.getGuid()+"."+type;\r
-                               QFile file = new QFile(Global.getDirectoryPath() +"res/"+fileName);\r
+                               QFile file = new QFile(Global.getFileManager().getResDirPath(fileName));\r
                        QFile.OpenMode mode = new QFile.OpenMode();\r
                        mode.set(QFile.OpenModeFlag.WriteOnly);\r
                        boolean openResult = file.open(mode);\r
@@ -1477,8 +1477,8 @@ public class BrowserWindow extends QWidget {
                String script_end = new String("');");\r
 \r
                long now = new Date().getTime();\r
-               String path = Global.getDirectoryPath() + "res/"\r
-                               + (new Long(now).toString()) + ".jpg";\r
+               String path = Global.getFileManager().getResDirPath(\r
+                               (new Long(now).toString()) + ".jpg");\r
 \r
                // This block is just a hack to make sure we wait at least 1ms so we\r
                // don't\r
@@ -1576,14 +1576,13 @@ public class BrowserWindow extends QWidget {
                        buffer = new StringBuffer(100);\r
                        \r
                        // Open the file & write the data\r
-                       String fileName = Global.getDirectoryPath()+"res/"+newRes.getGuid();\r
+                       String fileName = Global.getFileManager().getResDirPath(newRes.getGuid());\r
                        QFile tfile = new QFile(fileName);\r
                        tfile.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
                        tfile.write(newRes.getData().getBody());\r
                        tfile.close();\r
-                       fileName = fileName.replace('\\', '/');\r
                        buffer.append(script_start_image);\r
-                       buffer.append("<img src=\"" +fileName);\r
+                       buffer.append("<img src=\"" + FileUtils.toForwardSlashedPath(fileName));\r
 //                     if (mimeType.equalsIgnoreCase("image/jpg"))\r
 //                             mimeType = "image/jpeg";\r
                        buffer.append("\" en-tag=\"en-media\" type=\"" + mimeType +"\""\r
@@ -1638,7 +1637,7 @@ public class BrowserWindow extends QWidget {
                                                newRes.getAttributes().getFileName();\r
                                else\r
                                        fileName = newRes.getGuid()+".pdf";\r
-                               QFile file = new QFile(Global.getDirectoryPath() +"res/"+fileName);\r
+                               QFile file = new QFile(Global.getFileManager().getResDirPath(fileName));\r
                        QFile.OpenMode mode = new QFile.OpenMode();\r
                        mode.set(QFile.OpenModeFlag.WriteOnly);\r
                        file.open(mode);\r
@@ -1650,7 +1649,7 @@ public class BrowserWindow extends QWidget {
                        file.close();\r
 \r
                                PDFPreview pdfPreview = new PDFPreview();\r
-                               if (pdfPreview.setupPreview(Global.currentDir+"res/"+fileName, "pdf",0)) {\r
+                               if (pdfPreview.setupPreview(Global.getFileManager().getResDirPath(fileName), "pdf",0)) {\r
                                        // NFC TODO: should this be a 'file://' url like the ones above?\r
                                        imageURL = file.fileName() + ".png";\r
                                }\r
@@ -1658,7 +1657,9 @@ public class BrowserWindow extends QWidget {
                                                \r
                        logger.log(logger.EXTREME, "Generating link tags");\r
                        buffer.append("<a en-tag=\"en-media\" guid=\"" +newRes.getGuid()+"\" ");\r
-                       buffer.append(" onContextMenu=\"window.jambi.imageContextMenu(&apos;" +Global.getDirectoryPath() +"res/"+fileName +"&apos;);\" ");\r
+                       buffer.append(" onContextMenu=\"window.jambi.imageContextMenu(&apos;")\r
+                             .append(Global.getFileManager().getResDirPath(fileName))\r
+                             .append("&apos;);\" ");\r
                        buffer.append("type=\"" + mimeType + "\" href=\"nnres://" + fileName +"\" hash=\""+Global.byteArrayToHexString(newRes.getData().getBodyHash()) +"\" >");\r
                        buffer.append("<img src=\"" + imageURL + "\" title=\"" +newRes.getAttributes().getFileName());\r
                        buffer.append("\"></img>");\r
@@ -1863,7 +1864,11 @@ public class BrowserWindow extends QWidget {
                } else {\r
                        guid = name;\r
                }\r
-               guid = guid.replace("nnres://", "").replace(Global.currentDir.replace("\\","/")+"res/", "");\r
+\r
+               // Strip URL prefix and base dir\r
+               guid = guid.replace("nnres://", "")\r
+                       .replace(FileUtils.toForwardSlashedPath(Global.getFileManager().getResDirPath()), "");\r
+\r
                pos = guid.lastIndexOf('.');\r
                if (pos > 0)\r
                        guid = guid.substring(0,pos);\r
@@ -1887,6 +1892,7 @@ public class BrowserWindow extends QWidget {
        // * User chose to save an attachment. Pares out the request *\r
        // * into a guid & file. Save the result. --- DONE FROM downloadAttachment now!!!!!   \r
        // ************************************************************\r
+       // NFC TODO: unused? remove\r
        public void downloadImage(QNetworkRequest request) {\r
                QFileDialog fd = new QFileDialog(this);\r
                fd.setFileMode(FileMode.AnyFile);\r
@@ -1895,10 +1901,12 @@ public class BrowserWindow extends QWidget {
                fd.setAcceptMode(AcceptMode.AcceptSave);\r
                fd.setDirectory(System.getProperty("user.home"));\r
                String name = request.url().toString();\r
+\r
+               // Strip URL prefix and base dir path\r
                name = name.replace("nnres://", "");\r
-               String dPath = Global.getDirectoryPath() + "res/";\r
-               dPath = dPath.replace('\\', '/');\r
+               String dPath = FileUtils.toForwardSlashedPath(Global.getFileManager().getResDirPath());\r
                name = name.replace(dPath, "");\r
+\r
                int pos = name.lastIndexOf('.');\r
                String guid = name;\r
                if (pos > -1) {\r
@@ -2223,7 +2231,7 @@ public class BrowserWindow extends QWidget {
                        \r
                        r.setGuid(randint);\r
                        conn.getNoteTable().noteResourceTable.saveNoteResource(r, true);\r
-                               QFile f = new QFile(Global.getDirectoryPath() +"res/" +newFile);\r
+                               QFile f = new QFile(Global.getFileManager().getResDirPath(newFile));\r
                                QByteArray bin = new QByteArray(r.getData().getBody());\r
                                f.open(QFile.OpenModeFlag.WriteOnly);\r
                                f.write(bin);\r
@@ -2236,7 +2244,8 @@ public class BrowserWindow extends QWidget {
                                String source; \r
                                if (locTag.startsWith("src")) {\r
                                         source = newSegment.substring(startSrcPos+locTag.length(),endSrcPos);\r
-                                       newSegment = newSegment.replace(source, Global.getDirectoryPath().replace("\\", "/")+"res/"+newFile);\r
+                                       newSegment = newSegment.replace(source,\r
+                                               FileUtils.toForwardSlashedPath(Global.getFileManager().getResDirPath(newFile)));\r
                                } else {\r
                                        source = newSegment.substring(startSrcPos+locTag.length(),endSrcPos);\r
                                        newSegment = newSegment.replace(source, newFile);\r
index 9cba4b5..7585ce6 100644 (file)
@@ -34,7 +34,7 @@ public class Thumbnailer extends QObject {
     }\r
     \r
     public void setContent(String content) {\r
-       QFile file = new QFile(Global.currentDir+"res/thumbnail-"+guid+".html");\r
+        QFile file = new QFile(Global.getFileManager().getResDirPath("thumbnail-" + guid + ".html"));\r
        file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly));\r
        file.write(new QByteArray(content));\r
        file.close(); \r
@@ -63,7 +63,7 @@ public class Thumbnailer extends QObject {
         page.mainFrame().render(painter);             //<<<< THIS CAN LOCKUP if height too big!!!!\r
         painter.end();\r
         \r
-        image.save(Global.currentDir+"res/thumbnail-"+guid+".png");\r
+        image.save(Global.getFileManager().getResDirPath("thumbnail-" + guid + ".png"));\r
         finished.emit(guid);\r
     }\r
 }\r