Kiosk Project logo

MozillaCookbook

KioskWiki | RecentChanges | Preferences

Mozilla Cookbook

A collection of tips, tricks and recipies for mozilla application development. If you have any tips that could be added here please contact jim

Basic development environment setup

:-* Usefull Preferences:

To get dump() to work you need to have user_pref("browser.dom.window.dump.enabled", "true"); in your user preferences.

To have firefox or thunderbird to use a specific external application for a given protocol set the preference 'network.protocol-handler.app.PROTCOLNAME' ie:

     network.protocol-handler.app.mailto, /path/to/mail/progam
     network.protocol-handler.app.http, /path/to/browser/progam
you can even use this to specify an external handler for non-existant protocols like 'jim:'

Usefull program environment discovery utilities

:-* To get the profile directory and other program paths and filenames
   Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties)
   .get("ProfD", Components.interfaces.nsIFile).path
The 'ProfD' can be subsituted with values defined in http://lxr.mozilla.org/mozilla1.8/source/xpcom/io/nsAppDirectoryServiceDefs.h to get other usefull values.

:-* To read environment variables

   var userEnvironment = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
     //in newer moz versions there is nsIEnvironment 
     // @mozilla.org/process/environment;1 getService
     //that I think replaces process.getEnvironment()
     var userName = userEnvironment.getEnvironment("USER");

     var userEnvironment = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment);
     var userName = userEnvironment.get("USER");

In the example I am just getting the env var USER but the value of any env var can be obtained. Use the set(EnVarName, NewValue) method to assign a new value to an envar.

Read and write files - /FileHandling

:-* To write a file
  // *** copied from an example(note) at xulplanet
  
  // sFilePath and sFileContent are just strings
  function writeFile( sFilePath, sFileContent )
  {
   try
   {
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.QueryInterface(Components.interfaces.nsIFile);
    file.initWithPath( sFilePath );
    if( file.exists() == true ) file.remove( false );
    var strm =  Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
    strm.QueryInterface(Components.interfaces.nsIOutputStream);
    strm.QueryInterface(Components.interfaces.nsISeekableStream);
    //strm.init( file, 0x04 | 0x08, 420, 0 );
    strm.init( file, 0x04 | 0x08, 0666, 0 );
    strm.write( sFileContent, sFileContent.length );
    strm.flush();
    strm.close();
   }
   catch(ex)
   {
    window.alert(ex.message);
  }
 } 
:-* To read a file
 try { 
  var localFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  var inputFile = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  var readInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);

  localFile.initWithPath("/var/tmp/list.txt");
  //localFile.initWithPath(listPath);

  var tmp;
  inputFile.init(localFile, 0x01, 444, tmp); //file, open, mode, behavior
  readInputStream.init(inputFile);
  var fileStream = readInputStream.read(-1); //fileStream now contains the file contents

  inputFile.close();
  readInputStream.close();
 } catch (e) {
    //do something with the error
 } 

other usefull tidbits

:-* You can use nsIHttpAuthManager?.idl clearAll() to dump the httpAuth cache: @mozilla.org/network/http-auth-manager;1

:-* Use nsICookieManager?.idl removeAll() to dump all cookies: @mozilla.org/cookiemanager;1


KioskWiki | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited August 4, 2009 1:55 pm (diff)
Search: