[Pkg-mozext-commits] [itsalltext] 110/459: Okay, this refactoring adds in file extensions to my code. It changes the way file cleanup happens and some other changes.

David Prévot taffit at moszumanska.debian.org
Tue Feb 24 23:26:12 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository itsalltext.

commit 0dfc2784d6ce30bd0923c8a03537c09f4f4bcb7f
Author: Christian Höltje <docwhat at gerf.org>
Date:   Sun Feb 4 23:12:48 2007 -0500

    Okay, this refactoring adds in file extensions to my code.  It changes
    the way file cleanup happens and some other changes.
---
 Makefile                      |   6 +--
 chrome/content/cacheobj.js    | 112 +++++++++++++++++++++++++-----------------
 chrome/content/itsalltext.js  |  25 +++++-----
 chrome/content/preferences.js |   5 +-
 4 files changed, 85 insertions(+), 63 deletions(-)

diff --git a/Makefile b/Makefile
index 9d2e9f0..c3b4ad3 100644
--- a/Makefile
+++ b/Makefile
@@ -99,6 +99,6 @@ clean:
 realclean: clean
 	$(Q)rm -rf ../itsalltext*.xpi docs
 
-## @todo [low] Build the extension by copying into a build directory.
-## @todo [low] Minimize built JavaScript.
-## @todo [low] Put contents into a .jar during build phase.
+## @todo [5] Build the extension by copying into a build directory.
+## @todo [5] Minimize built JavaScript.
+## @todo [5] Put contents into a .jar during build phase.
diff --git a/chrome/content/cacheobj.js b/chrome/content/cacheobj.js
index 58d421a..6e0eb46 100644
--- a/chrome/content/cacheobj.js
+++ b/chrome/content/cacheobj.js
@@ -29,6 +29,7 @@ function CacheObj(node) {
     that.uid = that.hashString([ doc.location.toString(),
                                  Math.random(),
                                  that.node_id ].join(':'));
+    // @todo [security] Add a serial to the uid hash.
 
     node.setAttribute(ItsAllText.MYSTRING+'_UID', that.uid);
     ItsAllText.tracker[that.uid] = that;
@@ -36,28 +37,26 @@ function CacheObj(node) {
     /* Figure out where we will store the file.  While the filename can
      * change, the directory that the file is stored in should not!
      */
-    ItsAllText.debug('narf',doc, doc.location);
+    var host = window.escape(doc.location.hostname);
+    var hash = that.hashString([ doc.location.protocol,
+                                 doc.location.port,
+                                 doc.location.search,
+                                 doc.location.pathname,
+                                 doc.location.hash,
+                                 that.node_id ].join(':'));
+    that.base_filename = [host, hash.slice(0,10)].join('.');
+    /* The current extension.
+     * @type String
+     */
+    that.extension = null;
 
-    var hostname = doc.location.toString;
-    /* Since the hash is supposed to be equally distributed, it shouldn't
-     * matter how we slice it.  However, this does make it less unique.
+    /* Stores an nsILocalFile pointing to the current filename.
+     * @type nsILocalFile
      */
-    that.filename = that.hashString([ doc.location.toString(),
-                                      that.node_id ].join(':'));
-    that.filename = that.filename.slice(0,15);
-     
-    var editdir = ItsAllText.getEditDir();
+    that.file = null;
 
-    /* Get a file */
-    that.file = Components.classes["@mozilla.org/file/local;1"].
-        createInstance(Components.interfaces.nsILocalFile);
-    that.file.initWithFile(editdir);
-    that.file.append(that.filename);
-
-    /* Remove any existing files */
-    if (that.file.exists()) {
-        that.file.remove(false);
-    }
+    /* Set the default extension and create the nsIFile object. */
+    that.setExtension('.txt');
 
     /**
      * A callback for when the textarea/textbox or button has 
@@ -67,6 +66,7 @@ function CacheObj(node) {
     that.mouseover = function(event) {
         var style = that.button.style;
         style.opacity = '0.7';
+        ItsAllText.refreshTextarea(that.node);
     };
 
     /**
@@ -80,6 +80,24 @@ function CacheObj(node) {
     };
 }
 
+/**
+ * Set the extension for the file to ext.
+ * @param {String} ext The extension.  Must include the dot.  Example: .txt
+ */
+CacheObj.prototype.setExtension = function(ext) {
+    if (ext == this.extension) {
+        return; /* It's already set.  No problem. */
+    }
+
+    /* Create the nsIFile object */
+    var file = Components.classes["@mozilla.org/file/local;1"].
+        createInstance(Components.interfaces.nsILocalFile);
+    file.initWithFile(ItsAllText.getEditDir());
+    file.append([this.base_filename,ext].join(''));
+
+    this.extension = ext;
+    this.file = file;
+};
 
 /**
  * Returns a unique identifier for the node, within the document.
@@ -119,46 +137,48 @@ CacheObj.prototype.toString = function() {
  * Write out the contents of the node.
  */
 CacheObj.prototype.write = function() {
-    try {
-        var foStream = Components.
-            classes["@mozilla.org/network/file-output-stream;1"].
-            createInstance(Components.interfaces.nsIFileOutputStream);
+    var foStream = Components.
+        classes["@mozilla.org/network/file-output-stream;1"].
+        createInstance(Components.interfaces.nsIFileOutputStream);
              
-        /* write, create, truncate */
-        foStream.init(this.file, 0x02 | 0x08 | 0x20, 
-                      parseInt('0600',8), 0); 
+    /* write, create, truncate */
+    foStream.init(this.file, 0x02 | 0x08 | 0x20, 
+                  parseInt('0600',8), 0); 
              
-        /* We convert to charset */
-        var conv = Components.
-            classes["@mozilla.org/intl/scriptableunicodeconverter"].
-            createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
-        conv.charset = ItsAllText.getCharset();
+    /* We convert to charset */
+    var conv = Components.
+        classes["@mozilla.org/intl/scriptableunicodeconverter"].
+        createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
+    conv.charset = ItsAllText.getCharset();
              
-        var text = conv.ConvertFromUnicode(this.node.value);
-        foStream.write(text, text.length);
-        foStream.close();
+    var text = conv.ConvertFromUnicode(this.node.value);
+    foStream.write(text, text.length);
+    foStream.close();
              
-        /* Reset Timestamp and filesize, to prevent a spurious refresh */
-        this.timestamp = this.file.lastModifiedTime;
-        this.size      = this.file.fileSize;
+    /* Reset Timestamp and filesize, to prevent a spurious refresh */
+    this.timestamp = this.file.lastModifiedTime;
+    this.size      = this.file.fileSize;
+
+    /* Register the file to be deleted on app exit. */
+    Components.classes["@mozilla.org/uriloader/external-helper-app-service;1"].
+        getService(Components.interfaces.nsPIExternalAppLauncher).
+        deleteTemporaryFileOnExit(this.file);
              
-        return this.file.path;
-    } catch(e) {
-        ItsAllText.debug('write',this.file.path,e);
-        return null;
-    }
+    return this.file.path;
 };
      
-// @todo [idea] Pass in the line number to the editor, arbitrary command?
-// @todo [high] On edit, let user pick the file extension.
-// @todo [idea] allow the user to pick an alternative editor?
+// @todo [9] IDEA: Pass in the line number to the editor, arbitrary command?
+// @todo [1] On edit, let user pick the file extension.
+// @todo [9] IDEA: Allow the user to pick an alternative editor?
 /**
  * Edit a textarea as a file.
  * @param {String} extension The extension of the file to edit.
  * @param {boolean} retried This is used internally.
  */
 CacheObj.prototype.edit = function(extension, retried) {
-    extension = (typeof(extension) == 'undefined'? null : extension);
+    if (typeof(extension) == 'string') {
+        this.setExtension(extension);
+    }
     if (typeof(retried) == 'undefined') { retried = false; }
     var filename = this.write();
     this.initial_color = this.node.style.backgroundColor;
diff --git a/chrome/content/itsalltext.js b/chrome/content/itsalltext.js
index e2989c0..8c822cd 100644
--- a/chrome/content/itsalltext.js
+++ b/chrome/content/itsalltext.js
@@ -17,7 +17,7 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-// @todo [idea] dropdown list for charsets (utf-8, western-iso, default)?
+// @todo [9] IDEA: dropdown list for charsets (utf-8, western-iso, default)?
 
 var ItsAllText = function() {
     /**
@@ -114,6 +114,7 @@ var ItsAllText = function() {
      * Cleans out the edit directory, deleting all files.
      */
     that.cleanEditDir = function() {
+        var last_week = Date.now() - (1000*60*60*24*7);
         var fobj = that.getEditDir();
         //return dir.directoryEntries;
         // file is the given directory (nsIFile)
@@ -121,14 +122,17 @@ var ItsAllText = function() {
         while (entries.hasMoreElements()) {
             var entry = entries.getNext();
             entry.QueryInterface(Components.interfaces.nsIFile);
-            try{
-                entry.remove(false);
-            } catch(e) {
-                that.debug('unable to remove',entry,'error:',e);
+            if(entry.lastModifiedTime < last_week) {
+                try{
+                    entry.remove(false);
+                } catch(e) {
+                    that.debug('unable to remove',entry,'because:',e);
+                }
             }
         }
     };
-    /* Clean the edit directory right now, on startup. */
+
+    /* Clean the edit directory whenever we create a new window. */
     that.cleanEditDir();
 
     /* Load the various bits needed to make this work. */
@@ -242,7 +246,7 @@ var ItsAllText = function() {
         return that.preferences.debug;
     };
 
-    // @todo [med] Profiling and optimization.
+    // @todo [3] Profiling and optimization.
     
     /**
      * Returns a cache object
@@ -292,16 +296,15 @@ var ItsAllText = function() {
         if (!is_chrome) { cobj.addGumDrop(); }
     };
 
-    // @todo [med] If the textarea is focused, we should refresh it.
-    // @todo [low] When the editor quits, we should refresh the textarea.
-    // @todo [idea] support for input elements as well?
+    // @todo [5] Refresh textarea on editor quit.
+    // @todo [9] IDEA: support for input elements as well?
 
     /**
      * Refresh Document.
      * @param {Object} doc The document to refresh.
      */
     that.refreshDocument = function(doc) {
-        // @todo [high] Confirm that we find textareas inside iframes and frames.
+        // @todo [1] Confirm that we find textareas inside iframes and frames.
         if(!doc.location) { return; } // it's being cached, but not shown.
         var is_chrome = (doc.location.protocol == 'chrome:');
         var nodes = doc.getElementsByTagName('textarea');
diff --git a/chrome/content/preferences.js b/chrome/content/preferences.js
index d88aba7..c21d4a8 100644
--- a/chrome/content/preferences.js
+++ b/chrome/content/preferences.js
@@ -1,6 +1,5 @@
-// @todo [low] Use the EDITOR environment variable for default editor.
-// @todo [med] Try getting the editor from view_source.editor.path if not set.
-// @todo [med] Redo the preferences dialog to look better.
+// @todo [6] Better strategy for getting the default editor: EDITOR env variable or view_source.editor.path
+// @todo [3] Preference dialog redesign
 /**
  * Open a filepicker to select the value of the editor.
  */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/itsalltext.git



More information about the Pkg-mozext-commits mailing list