Jump to content

User:PerfektesChaos/js/localEdit/Firefox/chrome/content/fileIO.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/// chrome://wikilocaledit/content/fileIO.js
/// General file I/O interface
//  Used by:
//     * content/wikilocaledit.xul
/// 2012-12-12 PerfektesChaos@de.wikipedia
/// Fingerprint: #0#0#
/// <nowiki>
/*jshint curly:true, eqeqeq:true, newcap:false,
         strict:true, undef:true, white:false                          */
/*global window                                                        */
/*jslint white: true                                                   */
/*globals window: true                                                 */



( function ( w, AppObj ) {
   "use strict";
   var FILEIO, IMPORT, First;
   AppObj.fileIO  =  { };
   FILEIO         =  AppObj.fileIO;
   IMPORT         =  w.Components.utils[ "import" ];
   IMPORT( "resource://gre/modules/FileUtils.jsm" );
   IMPORT( "resource://gre/modules/NetUtil.jsm" );
   /*
      Public:
      * FILEIO.feed()
      * FILEIO.find()
      * FILEIO.flush()
   */



   First  =  function () {
      // Initialize file IO components, (re-)open local file
      // Postcondition:
      //    Returns new opened file handle
      // Uses:
      //    w
      //    FILEIO
      //    >< .fileIO.chrome
      //     < .fileIO.converter
      //     < .fileIO.fileLocal
      //     < .fileIO.filePicker
      //     < .fileIO.nsIFilePicker
      // 2012-11-30 PerfektesChaos@de.wikipedia
      var cif  =  w.Components.interfaces;
      FILEIO.fileLocal  =  null;
      if ( ! FILEIO.chrome ) {
         FILEIO.converter      =  w.Components
                                  .classes[ "@mozilla.org/intl/scriptableunicodeconverter" ]
                                  .createInstance( cif.nsIScriptableUnicodeConverter );
         FILEIO.nsIFilePicker  =  cif.nsIFilePicker;
         FILEIO.filePicker     =  w.Components
                                  .classes[ "@mozilla.org/filepicker;1" ]
                                  .createInstance( cif.nsIFilePicker );
         FILEIO.chrome         =  true;
      }
      FILEIO.fileLocal  =  w.Components
                                  .classes[ "@mozilla.org/file/local;1" ]
                                  .createInstance( cif.nsILocalFile );
      return  FILEIO.fileLocal;
   };   // First()



   FILEIO.feed  =  function ( appoint, access, after, ansi ) {
      // Retrieve file content for certain file path
      // Precondition:
      //    appoint  -- full file path and name
      //    access   -- document of user web page
      //    after    -- callback function, called when reading completed
      //                called with  Array, or false
      //                             [0]  path (appoint)
      //                             [1]  true: write protected
      //                             [2]  content, or false
      //                             [3]  document
      //    ansi     -- input file is not utf-8
      // Postcondition:
      //    Returns true if failed
      // Uses:
      //    this
      //    w
      //    First()
      // 2012-11-24 PerfektesChaos@de.wikipedia
      var file,
          lock  =  false,
          r     =  true;
      file  =  First();
      file.initWithPath( appoint );
      if ( w.NetUtil ) {
         w.NetUtil.asyncFetch( file,
                               function( inputStream, status ) {
              var data,
                  live,
                  opt   =  null;

              if ( w.Components.isSuccessCode( status ) ) {
                 if ( ! ansi ) {
                    opt  =  { "charset": "UTF-8" };
                 }
                 live  =  inputStream.available();
                 data  =  w.NetUtil.readInputStreamToString( inputStream,
                                                             live,
                                                             opt );

              } else {
                 data  =  false;
                 lock  =  true;
              }
              after( [ appoint, lock, data, access ] );
                                                             }
                            );
         r  =  false;
      }
      return r;
   };   // .fileIO.feed()



   FILEIO.find  =  function ( adult, apply, assign, add, about, allow ) {
      // Retrieve path to file or directory
      // Precondition:
      //    adult   -- string with parent directory, or empty
      //    apply   -- false: directory;  object: search pattern Array
      //               object modeOpen       0  Load or save a file
      //                      [0]  title for dialog
      //                      [1]  extension pattern
      //               false  modeGetFolder  2  Select a folder/directory
      //    assign  -- suggestion for file name
      //    add     -- default extension, or false
      //    about   -- UI window title
      //    allow   -- save a non-existing file,  modeSave 1
      // Postcondition:
      //    Returns string with full path, or false
      // Uses:
      //    this
      //    w
      //    >  .fileIO.nsIFilePicker
      //    >  .fileIO.fileLocal
      //    >< .fileIO.filePicker
      //    First()
      // 2012-12-12 PerfektesChaos@de.wikipedia
      var dir,
          get,
          mode  =  "modeOpen",
          nod,
          r     =  false;
      First();
      get  =  this.filePicker;
      if ( get ) {
         if ( adult ) {
            try {
               dir  =  this.fileLocal;
               dir.initWithPath( adult );
               if ( dir ) {
                  get.displayDirectory  =  dir;
               }
            } catch ( e ) {
            }
         }
         if ( apply ) {
            if ( typeof apply  ===  "object" ) {
               get.appendFilter( apply[ 0 ],  apply[ 1 ] );
            }
            mode  =  ( allow ? "modeSave" : "modeOpen" );
         } else {
            get.appendFilter( "", "" );
            mode  =  "modeGetFolder";
         }
         get.defaultString     =  ( assign ? assign : "" );
         get.defaultExtension  =  ( add ? add : "" );
         get.init( w,
                   ( about ? about : "Wiki LocalEdit" ),
                   FILEIO.nsIFilePicker[ mode ] );
         nod  =  get.show();   // deprecated since Gecko 17
             //  get.open();   // requires Gecko 17
         if ( nod === FILEIO.nsIFilePicker.returnOK  ||
              nod === FILEIO.nsIFilePicker.returnReplace ) {
            r  =  get.file.path;
         }
      }
      return r;
   };   // .fileIO.find()



   FILEIO.flush  =  function ( apply, appoint, access, after, arglist ) {
      // Write asynchrously to local file
      // Precondition:
      //    apply    -- wikitext
      //    appoint  -- full file path and name
      //    access   -- document of user web page
      //    after    -- callback function, called when writing completed
      //                called with status code and arglist
      //    arglist  -- additianal parameters for after
      // Postcondition:
      //    Returns false if successful started
      // Uses:
      //    this
      //    w
      //    >  .fileIO.converter
      //    First()
      // 2012-11-30 PerfektesChaos@de.wikipedia
      var file,
          istream,
          ostream,
          r        =  true;
      file  =  First();
      file.initWithPath( appoint );
      if ( w.FileUtils && w.NetUtil ) {
         if ( file.exists() ) {
            if ( ! file.isWritable() ) {
               r  =  "fileNotWritable";
            }
         }
         if ( r === true ) {
            this.converter.charset  =  "UTF-8";
            istream  =  this.converter.convertToInputStream( apply );
            ostream  =  w.FileUtils.openSafeFileOutputStream( file );
            w.NetUtil.asyncCopy( istream,
                                 ostream,
                                 function (status) {
                                                  after(status, arglist);
                                                   }
                               );
            r  =  false;
         }
      }
      return r;
   };   // .fileIO.flush()



} ( window, window.wikiLocalEdit ) );



/// EOF </nowiki>   content/fileIO.js