home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip_2003-11_cd1.bin / software / dave / dqsd.exe / tools.js < prev    next >
Text File  |  2002-10-23  |  6KB  |  268 lines

  1. var DQSD_BROWSER_WINDOW_NAME = "DQSDBrowserWindow";
  2.  
  3. // IE 5.0 and earlier don't have splice, push
  4. if (!Array.prototype.splice)
  5. {
  6.   function array_splice(ind,cnt)
  7.   {
  8.     removeArray = this.slice(ind,ind+cnt);
  9.     endArray = this.slice(ind+cnt);
  10.     this.length = ind;
  11.     for (var i=2; i < arguments.length; i++)
  12.       this[this.length] = arguments[i];
  13.     for (var i=0; i < endArray.length; i++)
  14.       this[this.length] = endArray[i];
  15.     return removeArray;
  16.   }
  17.   Array.prototype.splice = array_splice;
  18. }
  19.  
  20. if (!Array.prototype.push)
  21. {
  22.   function array_push()
  23.   {
  24.     for (var i=0; i<arguments.length; i++)
  25.       this[this.length] = arguments[i];
  26.     return this.length;
  27.   }
  28.   Array.prototype.push = array_push;
  29. }
  30.  
  31. // Tools that rely on DQSDTools.dll
  32.  
  33. function ensureLauncher()
  34. {
  35.   if (!triedToCreateLauncher)
  36.   {
  37.     triedToCreateLauncher = true;
  38.  
  39.     try
  40.     {
  41.       DQSDLauncher = new ActiveXObject("DQSDTools.Launcher");
  42.     }
  43.     catch (e)
  44.     {
  45.       alert('Unable to instantiate DQSDTools.Launcher:\n' + e.description);
  46.       DQSDLauncher = null;
  47.     }
  48.   }
  49.   return (DQSDLauncher != null);
  50. }
  51.  
  52. // read file
  53. function readFile(filename)
  54. {
  55.   if (ensureLauncher())
  56.   {
  57.     return DQSDLauncher.ReadFile(filename);
  58.   }
  59.   else
  60.     throw "Unable to read file " + filename;
  61. }
  62.  
  63. // write file
  64. function writeFile(filename, contents)
  65. {
  66.   if (ensureLauncher())
  67.   {
  68.     DQSDLauncher.WriteFile(filename, contents);
  69.   }
  70.   else
  71.     throw "Unable to write file " + filename;
  72. }
  73.  
  74. // Here's a quick hack to append to an existing file, but
  75. // is very slow because it rewrites the whole file.  If
  76. // this is needed for anything but debugging, the component
  77. // should be modified to have something like an AppendFile 
  78. // method.
  79. function appendFile(filename, contents)
  80. {
  81.   if (ensureLauncher())
  82.   {
  83.     var curr = '';
  84.     try
  85.     {
  86.       curr = DQSDLauncher.ReadFile(filename);
  87.     }
  88.     catch (e) {}
  89.     DQSDLauncher.WriteFile(filename, curr + contents);
  90.   }
  91.   else
  92.     throw "Unable to append to file " + filename;
  93. }
  94.  
  95. // get files
  96. function getFiles(directory)
  97. {
  98.   if (ensureLauncher())
  99.   {
  100.     try
  101.     {
  102.       return DQSDLauncher.GetFiles(directory);
  103.     }
  104.     catch (e)
  105.     {
  106.       return "";
  107.     }
  108.   }
  109.   else
  110.     throw "Unable to get files from directory " + directory;
  111. }
  112.  
  113. function getFolders(directory)
  114. {
  115.   if (ensureLauncher())
  116.   {
  117.     try
  118.     {
  119.       return DQSDLauncher.GetFolders(directory);
  120.     }
  121.     catch (e)
  122.     {
  123.       return getFiles(directory);
  124.     }
  125.   }
  126.   else
  127.     throw "Unable to get folders from directory " + directory;
  128. }
  129.  
  130.  
  131. // read tab-delimited, or pipe (|) delimited file
  132. function readTabDelimitedFile(filename)
  133. {
  134.   var fileText = "";
  135.   try {fileText = readFile(filename);} catch (e) {}
  136.   fileText = fileText.replace(/\r\n/g,"\n");
  137.   var fileLines = fileText.split("\n");
  138.   var fileTable = [];
  139.   for (var i = 0; i < fileLines.length; i++)
  140.   {
  141.     if (!fileLines[i].match(/^\s*($|\/\/)/))  // ignore comments (//) and blank lines
  142.     {
  143.       //must prepend a space or the split will complain on the gg search
  144.       if (fileLines[i].match(/^[\|\t]/))
  145.       {
  146.         fileLines[i]=" "+fileLines[i];
  147.       }
  148.       //split on the pipe symbol or the tab character 
  149.       fileTable.push(fileLines[i].split(/[\|\t]/));
  150.     }
  151.   }
  152.   return fileTable;
  153. }
  154.  
  155. function isInternalSearch( fn )
  156. {
  157.   return (fn.substr(0,INTERNAL_FUNC_PREFIX.length) == INTERNAL_FUNC_PREFIX)
  158. }
  159.  
  160. function getSearchAliases( search )
  161. {
  162.   return isInternalSearch(search.aliases[0]) ? search.aliases.slice(1).join(', ') : search.aliases.join(', ');
  163. }
  164.  
  165. function getFirstAlias( search )
  166. {
  167.   var alias = '';
  168.   
  169.   // If it's an internal search, then the first name will be the internal name (_dqsd_internal_fn_??)
  170.   // so return the second alias which should always be there, _unless_ this is a shortcut (mapping to 
  171.   // an internal function) defined aliases.txt that was overridden by a shortcut in localaliases.txt.
  172.   if ( isInternalSearch(search.aliases[0]) )
  173.   {
  174.     if ( search.aliases.length > 1 )
  175.     {
  176.       alias = search.aliases[1];
  177.     }
  178.     else
  179.     {
  180.       alias = ''; // could return the internal name here, but that would be weird.
  181.     }
  182.   }
  183.   else
  184.   {
  185.     alias = search.aliases[0];
  186.   }
  187.   return alias;
  188. }
  189.  
  190. // This method just is just a convenience for instantiating the same
  191. // same MSXML DOM each time. This method is now changed
  192. // to probe for older versions of MSXML, in case the newest
  193. // version is not installed on the machine.
  194. function getMSXMLDOMDocumentInstance()
  195. {
  196.   try
  197.   {
  198.      return new ActiveXObject("Msxml2.DOMDocument");
  199.   }
  200.   catch(e)
  201.   {
  202.      return new ActiveXObject("Msxml.DOMDocument");
  203.   }
  204. }
  205.  
  206. function getDefaultLanguage()
  207. {
  208.   language=navigator.userLanguage.substr(0,2);
  209. }
  210.  
  211. function initGlobals()
  212. {
  213.   DQSDLauncher = null;
  214.   triedToCreateLauncher = false;
  215.  
  216.   firsterror = null;
  217.   // window.onerror=handleerror;
  218.  
  219.   var uaMatch = navigator.userAgent.toLowerCase().match("msie\\s+(\\d+\\.\\d*)");
  220.   var ie_version = (uaMatch ? parseFloat(uaMatch[1]) : 0.0);
  221.   dopopup = (ie_version >= 5.5);
  222.  
  223.   useExternalBrowser = (launchmode > 0);
  224.   if (useExternalBrowser)
  225.   {
  226.     if (ensureLauncher())
  227.     {
  228.       var browserPath = DQSDLauncher.pathDefaultBrowser
  229.       if (launchmode != 0 && browserPath.toLowerCase().indexOf("iexplore.exe") > 0)
  230.         useExternalBrowser = false;
  231.     }
  232.     else // Gracefully ignore problems creating the control.
  233.     {
  234.       useExternalBrowser = false;
  235.     }
  236.   }
  237.  
  238.   reuseBrowserWindowModeOverride = reuseBrowserWindowMode;
  239.   searchPrefix = '';
  240.  
  241.   init = true;
  242.   selfdrag = false;
  243.  
  244.   bant = null;
  245.  
  246.   // eliminate Go button if window is narrow
  247.   gowidth = -1;
  248.   textheight = -1;
  249.  
  250.   keyboardHook = null;
  251. }
  252.  
  253. // catch errors
  254. function handleerror(msg, url, lineno)
  255. {
  256.   if (!firsterror)
  257.   {
  258.      firsterror = msg;
  259.      alert("Error (line " + lineno + "):\n" + msg);
  260.   }
  261.   return true;
  262. }
  263.  
  264. function qualifiedalert(s)
  265. {
  266.   alertmode && alert(s);
  267. }
  268.