home *** CD-ROM | disk | FTP | other *** search
/ PC User 2004 November / PCU1104CD1.iso / software / security / files / ieview.xpi / chrome / ieview.jar / content / ieviewOverlay.js < prev    next >
Encoding:
JavaScript  |  2004-06-11  |  9.0 KB  |  357 lines

  1. /* 
  2.  *
  3.  * Copyright (c) 2003  Paul Roub <paul@roub.net>
  4.  *
  5.  * $Header: /cvs/ieview/ieview/content/ieviewOverlay.js,v 1.1 2004/06/11 04:20:05 roub Exp $
  6.  *
  7.  * Portions based on GPLed code by
  8.  *     Ted Mielczarek
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Lesser General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2.1 of the License, or (at your option) any later version.
  14.  * 
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.  * Lesser General Public License for more details.
  19.  * 
  20.  * You should have received a copy of the GNU Lesser General Public
  21.  * License along with this library; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23.  *
  24.  * *****
  25.  *
  26.  * Notes:
  27.  *
  28.  *    Since we can't know ahead of time where IE will be installed, we Kiboze the
  29.  *    Start Menu tree.  If the shortcut has been renamed (i.e. it is no longer
  30.  *    titled "Internet Explorer"), we're out of luck.  The only option at that
  31.  *    point would be to open and dereference every symlink we find, and see if the
  32.  *    target leaf name is "iexplore.exe".  Not doing that at the moment, since
  33.  *    it just seems insane.
  34.  */
  35.  
  36. var ieviewMenuItems = new Array("ieview-do-view", "ieview-do-viewlink");
  37. var userPrograms = "Progs";
  38. var allUserPrograms = "CmPrgs";
  39. var applicationData = "AppData";
  40.  
  41. var gIeViewBundle;
  42.  
  43. function ieviewContext() {
  44.   if(gContextMenu) {
  45.     for(var i=0; i<ieviewMenuItems.length; i++) {
  46.       var menuitem = document.getElementById(ieviewMenuItems[i]);
  47.       if(menuitem && (i == 0))
  48.         menuitem.hidden = (gContextMenu.isTextSelected || gContextMenu.onLink || gContextMenu.onImage || gContextMenu.onTextInput );
  49.       else if (menuitem)
  50.       {
  51.          menuitem.hidden = ! gContextMenu.onLink;
  52.       }
  53.     }
  54.   }
  55. }
  56.  
  57. function ieView() {
  58.   if(gContextMenu) {
  59.      var href = gBrowser.currentURI.spec;
  60.  
  61.      ieViewLaunch("Internet Explorer.lnk", href);
  62.   }
  63. }
  64.  
  65.  
  66. function ieViewLink() {
  67.   if(gContextMenu) {
  68.       var href = gContextMenu.linkURL();
  69.  
  70.       ieViewLaunch("Internet Explorer.lnk", href);
  71.   }
  72. }
  73.  
  74.  
  75.  
  76. // attempt to grab the real path of a predefined directory
  77. //
  78. function tryDir(dsp, key)
  79. {
  80.    try
  81.    {
  82.       var nif = dsp.get(key, Components.interfaces.nsIFile);
  83.       return(nif.path);
  84.    }
  85.    catch (ar)
  86.    {
  87.       return("");
  88.    }
  89. }
  90.  
  91.  
  92. function searchPath(path, fname)
  93. {
  94.    var result = null;
  95.  
  96.    try
  97.    {
  98.       var f = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  99.  
  100.       f.initWithPath(path);
  101.  
  102.       if (f.exists() && f.isDirectory())
  103.       {
  104.          var entries = f.directoryEntries;
  105.  
  106.          while (entries.hasMoreElements())
  107.          {
  108.             var ent = entries.getNext().QueryInterface(Components.interfaces.nsIFile);
  109.  
  110.             if (ent.isDirectory())
  111.             {
  112.                result = searchPath(ent.path, fname);
  113.  
  114.                if (result)
  115.                {
  116.                   break;
  117.                }
  118.             }
  119.             else if (ent.isSymlink())
  120.             {
  121.                if (ent.leafName.toLowerCase() == fname.toLowerCase())
  122.                {
  123.                   result = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  124.                   result.followLinks = true;
  125.                   result.initWithPath(ent.path);
  126.  
  127.                   if (result.target == "")
  128.                   {
  129.                      result = null;
  130.                   }
  131.                   else
  132.                   {
  133.                      break;
  134.                   }
  135.                }
  136.             }
  137.             else if (ent.isFile())
  138.             {
  139.                if (ent.leafName.toLowerCase() == fname.toLowerCase())
  140.                {
  141.                   result = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  142.                   result.initWithPath(ent.path);
  143.                   break;
  144.                }
  145.             }
  146.          }
  147.       }
  148.    }
  149.    catch (ar)
  150.    {
  151.       return(null);
  152.    }
  153.  
  154.    return( result );
  155. }
  156.  
  157.  
  158.  
  159. function ieViewLaunch (path,argumentstext)
  160. {
  161.     var cantMessage = "can't find";
  162.  
  163.     try
  164.     {
  165.         cantMessage = gIeViewBundle.getString("ieview.cantFindExplorer");
  166.     }
  167.     catch(e)
  168.     {
  169.         alert(e);
  170.     }
  171.  
  172.   var prefservice = Components.classes["@mozilla.org/preferences-service;1"].
  173.                 getService(Components.interfaces.nsIPrefService);   
  174.  
  175.   var prefs = prefservice.getBranch("");
  176.  
  177.    try{
  178.       if(path=="") return false;
  179.  
  180.       var ieloc = null;
  181.  
  182.       if (prefs.getPrefType("ieview.ieapp") == prefs.PREF_STRING)
  183.       {
  184.         ieloc = prefs.getCharPref("ieview.ieapp");
  185.       }
  186.  
  187.       var   natTarget = null;
  188.       var   usePath = null;
  189.  
  190.       if (ieloc != null)
  191.       {
  192.          natTarget = ieloc;
  193.       }
  194.       else
  195.       {
  196.          var dsprops = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
  197.  
  198.          usePath = tryDir(dsprops, userPrograms);    // try user-specific program menu first
  199.  
  200.          var file = null;
  201.  
  202.          if (usePath != "")
  203.          {
  204.             file = searchPath(usePath, path);
  205.          }
  206.  
  207.          if (! file)
  208.          {
  209.             usePath = tryDir(dsprops, allUserPrograms);   // no joy?  try "all users" program menu
  210.  
  211.             if (usePath != "")
  212.             {
  213.                file = searchPath(usePath, path);
  214.             }
  215.          }
  216.  
  217.          if (! file)
  218.          {
  219.             usePath = tryDir(dsprops, applicationData);   // last resort, check the "quick start" bar
  220.  
  221.             if (usePath != "")
  222.             {
  223.                var   quickPath = "\\microsoft\\internet explorer\\quick launch";
  224.  
  225.                usePath = usePath + quickPath;
  226.  
  227.                file = searchPath(usePath, path);
  228.  
  229.                if (! file)    // check alternate QuickLaunch bar title
  230.                {
  231.                   var launchLink = "Launch Internet Explorer Browser.lnk";
  232.  
  233.                   file = searchPath(usePath, launchLink);
  234.                }
  235.             }
  236.          }
  237.  
  238.          // last ditch -- find the windows directory
  239.          // assume that the main Program Files directory is on the same drive
  240.          // look in there, under Program Files\Internet Explorer, for iexplore.exe
  241.          //
  242.          if (! file)
  243.          {
  244.             var usePath = tryDir(dsprops, "WinD");
  245.  
  246.             if ((usePath != "") && (usePath.charAt(1) == ":"))
  247.             {
  248.                usePath = usePath.substr(0, 2) + "\\program files\\internet explorer";
  249.  
  250.                file = searchPath(usePath, "iexplore.exe");
  251.             }
  252.          }
  253.  
  254.          if ((! file) || (! file.exists()))
  255.          {
  256.             alert(cantMessage);
  257.             return false;
  258.          }
  259.  
  260.          natTarget = file.target;
  261.       }
  262.  
  263.       var targetFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  264.  
  265.       targetFile.initWithPath(natTarget);
  266.  
  267.       if (! targetFile.exists())
  268.       {
  269.          alert(cantMessage);
  270.          return(false);
  271.       }
  272.  
  273.       usePath = targetFile.target;
  274.  
  275.       var process = Components.classes['@mozilla.org/process/util;1'].getService(Components.interfaces.nsIProcess);
  276.       process.init(targetFile);
  277.       var arguments= [] ;
  278.  
  279.       arguments.push(argumentstext);
  280.  
  281.       process.run(false, arguments, arguments.length, {});
  282.       return true;
  283.  
  284.    }catch(e){ 
  285.       alert(e);return false;
  286.    }
  287.  
  288.    return false;        // avoid JavaScript Error.
  289. }
  290.  
  291.  
  292. function ieviewInit() {
  293.   var menu = document.getElementById("contentAreaContextMenu");
  294.   menu.addEventListener("popupshowing",ieviewContext,false);
  295.  
  296.   gIeViewBundle = document.getElementById("bundle_ieview");
  297.  
  298.   if (! gIeViewBundle)
  299.   {
  300.       alert("no bundle");
  301.   }
  302. }
  303.  
  304. function setIeviewOptions()
  305. {
  306.     if (document.getElementById('ieloc').value)
  307.     {
  308.        var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  309.                                .getService(Components.interfaces.nsIPrefService);
  310.  
  311.        var prefs = prefService.getBranch("");
  312.  
  313.          prefs.setCharPref("ieview.ieapp", document.getElementById('ieloc').value);
  314.     }
  315.  
  316.     window.close();
  317. }
  318.  
  319. function pickIe()
  320. {
  321.     var picker = Components.classes["@mozilla.org/filepicker;1"].getService(Components.interfaces.nsIFilePicker);
  322.  
  323.     picker.init(window, "Choose Browser", 0);
  324.     picker.appendFilters(64);
  325.     
  326.     if (picker.show() == 0)
  327.     {
  328.         document.getElementById('ieloc').value = picker.file.target;
  329.     }
  330. }
  331.  
  332.  
  333. function initPath()
  334. {
  335.     var prefservice = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);   
  336.     
  337.     var prefs = prefservice.getBranch("");
  338.  
  339.     var ieloc = null;
  340.  
  341.     if (prefs.getPrefType("ieview.ieapp") == prefs.PREF_STRING)
  342.     {
  343.       ieloc = prefs.getCharPref("ieview.ieapp");
  344.     }
  345.  
  346.     if ((ieloc != null) && (ieloc.length > 0)) 
  347.     {
  348.         document.getElementById('ieloc').value = ieloc;
  349.     }
  350.  
  351.     prefs = null;
  352. }
  353.  
  354. // do the init on load
  355. window.addEventListener("load", ieviewInit, false); 
  356.  
  357.