home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / components / nsFilePicker.js < prev    next >
Text File  |  2001-02-14  |  13KB  |  400 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  * 
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  * 
  13.  * The Original Code is mozilla.org code.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 2000 Netscape Communications Corporation.  All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): Stuart Parmenter <pavlov@netscape.com>
  21.  */
  22.  
  23. /*
  24.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  25.  * If you must perform some initialization, and it could possibly fail (even
  26.  * due to an out-of-memory condition), you should use an Init method, which
  27.  * can convey failure appropriately (thrown exception in JS,
  28.  * NS_FAILED(nsresult) return in C++).
  29.  *
  30.  * In JS, you can actually cheat, because a thrown exception will cause the
  31.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  32.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  33.  * for portability reasons -- and even when you're building completely
  34.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  35.  */
  36.  
  37.  
  38. const DEBUG = true; /* set to false to suppress debug messages */
  39. const FILEPICKER_CONTRACTID     = "@mozilla.org/filepicker;1";
  40. const FILEPICKER_CID        = Components.ID("{54ae32f8-1dd2-11b2-a209-df7c505370f8}");
  41.  
  42. const APPSHELL_SERV_CONTRACTID  = "@mozilla.org/appshell/appShellService;1";
  43. const nsIAppShellService    = Components.interfaces.nsIAppShellService;
  44. const nsILocalFile          = Components.interfaces.nsILocalFile;
  45. const nsIFileURL            = Components.interfaces.nsIFileURL;
  46. const nsISupports           = Components.interfaces.nsISupports;
  47. const nsIFactory            = Components.interfaces.nsIFactory;
  48. const nsIFilePicker         = Components.interfaces.nsIFilePicker;
  49. const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor
  50. const nsIDOMWindow          = Components.interfaces.nsIDOMWindow;
  51.  
  52. const bundle                = srGetStrBundle("chrome://global/locale/filepicker.properties");
  53. var   lastDirectory         = "/";
  54.  
  55. function nsFilePicker()
  56. {
  57.   /* attributes */
  58.   this.mDefaultString = "";
  59.   this.mDisplayDirectory = lastDirectory;
  60.   this.mFilterTitles = new Array();
  61.   this.mFilters = new Array();
  62. }
  63.  
  64. nsFilePicker.prototype = {
  65.  
  66.   /* attribute nsILocalFile displayDirectory; */
  67.   set displayDirectory(a) { this.mDisplayDirectory = a; },
  68.   get displayDirectory()  { return this.mDisplayDirectory; },
  69.  
  70.   /* readonly attribute nsILocalFile file; */
  71.   set file(a) { throw "readonly property"; },
  72.   get file()  { return this.mFile; },
  73.  
  74.   /* readonly attribute nsIFileURL fileURL; */
  75.   set fileURL(a) { throw "readonly property"; },
  76.   get fileURL()  { 
  77.     if (this.mFile) {
  78.       var url = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(nsIFileURL);
  79.       url.file = this.mFile;
  80.       return url;
  81.     }
  82.   },
  83.  
  84.   /* attribute wstring defaultString; */
  85.   set defaultString(a) { this.mDefaultString = a; },
  86.   get defaultString()  { return this.mDefaultString; },
  87.  
  88.   /* methods */
  89.   init: function(parent, title, mode) {
  90.     this.mParentWindow = parent;
  91.     this.mTitle = title;
  92.     this.mMode = mode;
  93.   },
  94.  
  95.   appendFilters: function(filterMask) {
  96.     if (filterMask & nsIFilePicker.filterHTML) {
  97.       this.appendFilter(bundle.GetStringFromName("htmlTitle"),
  98.                    bundle.GetStringFromName("htmlFilter"));
  99.     }
  100.     if (filterMask & nsIFilePicker.filterText) {
  101.       this.appendFilter(bundle.GetStringFromName("textTitle"),
  102.                    bundle.GetStringFromName("textFilter"));
  103.     }
  104.     if (filterMask & nsIFilePicker.filterImages) {
  105.       this.appendFilter(bundle.GetStringFromName("imageTitle"),
  106.                    bundle.GetStringFromName("imageFilter"));
  107.     }
  108.     if (filterMask & nsIFilePicker.filterXML) {
  109.       this.appendFilter(bundle.GetStringFromName("xmlTitle"),
  110.                    bundle.GetStringFromName("xmlFilter"));
  111.     }
  112.     if (filterMask & nsIFilePicker.filterXUL) {
  113.       this.appendFilter(bundle.GetStringFromName("xulTitle"),
  114.                    bundle.GetStringFromName("xulFilter"));
  115.     }
  116.     if (filterMask & nsIFilePicker.filterAll) {
  117.       this.appendFilter(bundle.GetStringFromName("allTitle"),
  118.                    bundle.GetStringFromName("allFilter"));
  119.     }
  120.   },
  121.  
  122.   appendFilter: function(title, extentions) {
  123.     this.mFilterTitles.push(title);
  124.     this.mFilters.push(extentions);
  125.   },
  126.  
  127.   QueryInterface: function(iid) {
  128.     if (!iid.equals(nsIFilePicker) &&
  129.         !iid.equals(nsISupports))
  130.         throw Components.results.NS_ERROR_NO_INTERFACE;
  131.     return this;
  132.   },
  133.  
  134.   show: function() {
  135.     var o = new Object();
  136.     o.title = this.mTitle;
  137.     o.mode = this.mMode;
  138.     o.displayDirectory = this.mDisplayDirectory;
  139.     o.defaultString = this.mDefaultString;
  140.     o.filters = new Object();
  141.     o.filters.titles = this.mFilterTitles;
  142.     o.filters.types = this.mFilters;
  143.     o.retvals = new Object();
  144.  
  145.     dump("@mozilla.org/js/xpc/ID;1OMWindow id value = " + nsIDOMWindow.id + "\n");
  146.  
  147.     var parent;
  148.     try {
  149.       if (this.mParentWindow) {
  150.         parent = this.mParentWindow;
  151.       } else if (typeof(window) == "object" && window != null) {
  152.         parent = window;
  153.       } else {
  154.         try {
  155.           var appShellService = Components.classes[APPSHELL_SERV_CONTRACTID].getService(nsIAppShellService);
  156.           parent = appShellService.getHiddenDOMWindow();
  157.         } catch(ex) {
  158.           dump("Can't get parent.  xpconnect hates me so we can't get one from the appShellService.\n");
  159.           dump(ex + "\n");
  160.         }
  161.       }
  162.     } catch(ex) { dump("fuck\n"); }
  163.  
  164.     try {
  165.       parent.openDialog("chrome://global/content/filepicker.xul",
  166.                         "",
  167.                         "chrome,modal,titlebar,resizeable=yes,dependent=yes",
  168.                         o);
  169.       this.mFile = o.retvals.file;
  170.       lastDirectory = o.retvals.directory;
  171.       return o.retvals.buttonStatus;
  172.     } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); }
  173.   }
  174. }
  175.  
  176. if (DEBUG)
  177.     debug = function (s) { dump("-*- filepicker: " + s + "\n"); }
  178. else
  179.     debug = function (s) {}
  180.  
  181. /* module foo */
  182.  
  183. var filePickerModule = new Object();
  184.  
  185. filePickerModule.registerSelf =
  186. function (compMgr, fileSpec, location, type)
  187. {
  188.     debug("registering (all right -- a JavaScript module!)");
  189.     compMgr.registerComponentWithType(FILEPICKER_CID, "FilePicker JS Component",
  190.                                       FILEPICKER_CONTRACTID, fileSpec, location,
  191.                                       true, true, type);
  192. }
  193.  
  194. filePickerModule.getClassObject =
  195. function (compMgr, cid, iid) {
  196.     if (!cid.equals(FILEPICKER_CID))
  197.         throw Components.results.NS_ERROR_NO_INTERFACE;
  198.     
  199.     if (!iid.equals(Components.interfaces.nsIFactory))
  200.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  201.     
  202.     return filePickerFactory;
  203. }
  204.  
  205. filePickerModule.canUnload =
  206. function(compMgr)
  207. {
  208.     debug("Unloading component.");
  209.     return true;
  210. }
  211.     
  212. /* factory object */
  213. filePickerFactory = new Object();
  214.  
  215. filePickerFactory.createInstance =
  216. function (outer, iid) {
  217.     debug("CI: " + iid);
  218.     debug("IID:" + nsIFilePicker);
  219.     if (outer != null)
  220.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  221.  
  222.     return (new nsFilePicker()).QueryInterface(iid);
  223. }
  224.  
  225. /* entrypoint */
  226. function NSGetModule(compMgr, fileSpec) {
  227.     return filePickerModule;
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. /* crap from strres.js that I want to use for string bundles since I can't include another .js file.... */
  236.  
  237. var strBundleService = null;
  238.  
  239. function srGetAppLocale()
  240. {
  241.   var localeService = null;
  242.   var applicationLocale = null;
  243.  
  244.   localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"].createInstance();
  245.   if (!localeService) {
  246.     dump("\n--** localeService createInstance 1 failed **--\n");
  247.     return null;
  248.   }
  249.  
  250.   localeService = localeService.QueryInterface(Components.interfaces.nsILocaleService);
  251.   if (!localeService) {
  252.     dump("\n--** localeService createInstance 2 failed **--\n");
  253.     return null;
  254.   }
  255.   applicationLocale = localeService.GetApplicationLocale();
  256.   if (!applicationLocale) {
  257.     dump("\n--** localeService.GetApplicationLocale failed **--\n");
  258.   }
  259.   return applicationLocale;
  260. }
  261.  
  262. function srGetStrBundleWithLocale(path, locale)
  263. {
  264.   var strBundle = null;
  265.  
  266.   strBundleService =
  267.     Components.classes["@mozilla.org/intl/stringbundle;1"].createInstance(); 
  268.  
  269.   if (!strBundleService) {
  270.     dump("\n--** strBundleService createInstance 1 failed **--\n");
  271.     return null;
  272.   }
  273.  
  274.   strBundleService = 
  275.       strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  276.  
  277.   if (!strBundleService) {
  278.     dump("\n--** strBundleService createInstance 2 failed **--\n");
  279.     return null;
  280.   }    
  281.  
  282.   strBundle = strBundleService.CreateBundle(path, locale); 
  283.   if (!strBundle) {
  284.     dump("\n--** strBundle createInstance failed **--\n");
  285.   }
  286.   return strBundle;
  287. }
  288.  
  289. function srGetStrBundle(path)
  290. {
  291.   var appLocale = srGetAppLocale();
  292.   return srGetStrBundleWithLocale(path, appLocale);
  293. }
  294.  
  295.  
  296. function localeSwitching(winType, baseDirectory, providerName)
  297. {
  298.   dump("\n ** Enter localeSwitching() ** \n");
  299.   dump("\n ** winType=" +  winType + " ** \n");
  300.   dump("\n ** baseDirectory=" +  baseDirectory + " ** \n");
  301.   dump("\n ** providerName=" +  providerName + " ** \n");
  302.  
  303.   //
  304.   var rdf;
  305.   if(document.rdf) {
  306.     rdf = document.rdf;
  307.     dump("\n ** rdf = document.rdf ** \n");
  308.   }
  309.   else if(Components) {
  310.     var isupports = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService();
  311.     rdf = isupports.QueryInterface(Components.interfaces.nsIRDFService);
  312.     dump("\n ** rdf = Components... ** \n");
  313.   }
  314.   else {
  315.     dump("can't find nuthin: no document.rdf, no Components. \n");
  316.   }
  317.   //
  318.  
  319.   var ds = rdf.GetDataSource("rdf:chrome");
  320.  
  321.   // For M4 builds, use this line instead.
  322.   // var ds = rdf.GetDataSource("resource:/chrome/registry.rdf");
  323.   var srcURL = "chrome://";
  324.   srcURL += winType + "/locale/";
  325.   dump("\n** srcURL=" + srcURL + " **\n");
  326.   var sourceNode = rdf.GetResource(srcURL);
  327.   var baseArc = rdf.GetResource("http://chrome.mozilla.org/rdf#base");
  328.   var nameArc = rdf.GetResource("http://chrome.mozilla.org/rdf#name");
  329.                       
  330.   // Get the old targets
  331.   var oldBaseTarget = ds.GetTarget(sourceNode, baseArc, true);
  332.   dump("\n** oldBaseTarget=" + oldBaseTarget + "**\n");
  333.   var oldNameTarget = ds.GetTarget(sourceNode, nameArc, true);
  334.   dump("\n** oldNameTarget=" + oldNameTarget + "**\n");
  335.  
  336.   // Get the new targets 
  337.   // file:/u/tao/gila/mozilla-org/html/projects/intl/chrome/
  338.   // da-DK
  339.   if (baseDirectory == "") {
  340.     baseDirectory =  "resource:/chrome/";
  341.   }
  342.  
  343.   var finalBase = baseDirectory;
  344.   if (baseDirectory != "") {
  345.     finalBase += winType + "/locale/" + providerName + "/";
  346.   }
  347.   dump("\n** finalBase=" + finalBase + "**\n");
  348.  
  349.   var newBaseTarget = rdf.GetLiteral(finalBase);
  350.   var newNameTarget = rdf.GetLiteral(providerName);
  351.   
  352.   // Unassert the old relationships
  353.   if (baseDirectory != "") {
  354.       ds.Unassert(sourceNode, baseArc, oldBaseTarget);
  355.   }
  356.  
  357.   ds.Unassert(sourceNode, nameArc, oldNameTarget);
  358.   
  359.   // Assert the new relationships (note that we want a reassert rather than
  360.   // an unassert followed by an assert, once reassert is implemented)
  361.   if (baseDirectory != "") {
  362.       ds.Assert(sourceNode, baseArc, newBaseTarget, true);
  363.   }
  364.   ds.Assert(sourceNode, nameArc, newNameTarget, true);
  365.   
  366.   // Flush the modified data source to disk
  367.   // (Note: crashes in M4 builds, so don't use Flush() until fix checked in)
  368.   ds.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Flush();
  369.  
  370.   // Open up a new window to see your new chrome, since changes aren't yet dynamically
  371.   // applied to the current window
  372.  
  373.   // BrowserOpenWindow('chrome://addressbook/content');
  374.   dump("\n ** Leave localeSwitching() ** \n");
  375. }
  376.  
  377. function localeTo(baseDirectory, localeName)
  378. {
  379.   dump("\n ** Enter localeTo() ** \n");
  380.  
  381.   localeSwitching("addressbook", baseDirectory, localeName); 
  382.   localeSwitching("bookmarks", baseDirectory, localeName); 
  383.   localeSwitching("directory", baseDirectory, localeName); 
  384.   localeSwitching("editor", baseDirectory, localeName); 
  385.   localeSwitching("global", baseDirectory, localeName); 
  386.   localeSwitching("history", baseDirectory, localeName); 
  387.   localeSwitching("messenger", baseDirectory, localeName); 
  388.   localeSwitching("messengercompose", baseDirectory, localeName); 
  389.   localeSwitching("navigator", baseDirectory, localeName); 
  390.   localeSwitching("pref", baseDirectory, localeName); 
  391.   localeSwitching("profile", baseDirectory, localeName); 
  392.   localeSwitching("regviewer", baseDirectory, localeName); 
  393.   localeSwitching("related", baseDirectory, localeName); 
  394.   localeSwitching("sidebar", baseDirectory, localeName); 
  395.   localeSwitching("wallet", baseDirectory, localeName); 
  396.   localeSwitching("xpinstall", baseDirectory, localeName); 
  397.   
  398.   dump("\n ** Leave localeTo() ** \n");
  399. }
  400.