home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / chrome / mail.jar / content / editor / EdLinkChecker.js < prev    next >
Encoding:
JavaScript  |  2003-11-09  |  6.7 KB  |  222 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  *   Akkana Peck (akkana@netscape.com)
  22.  *   Charles Manxke (cmanske@netscape.com)
  23.  */
  24.  
  25. // Variables used across all the links being checked:
  26. var gNumLinksToCheck = 0;     // The number of nsILinkCheckers
  27. var gLinksBeingChecked = [];  // Array of nsIURICheckers
  28. var gURIRefObjects = [];      // Array of nsIURIRefObjects
  29. var gNumLinksCalledBack = 0;
  30. var gStartedAllChecks = false;
  31. var gLinkCheckTimerID = 0;
  32.  
  33. // Implement nsIRequestObserver:
  34. var gRequestObserver =
  35. {
  36.   // urichecker requires that we have an OnStartRequest even tho it's a nop.
  37.   onStartRequest: function(request, ctxt) { },
  38.  
  39.   // onStopRequest is where we really handle the status.
  40.   onStopRequest: function(request, ctxt, status)
  41.   {
  42.     var linkChecker = request.QueryInterface(Components.interfaces.nsIURIChecker);
  43.     if (linkChecker)
  44.     {
  45.       gNumLinksCalledBack++;
  46.       linkChecker.status = status;
  47.       for (var i = 0; i < gNumLinksCalledBack; i++)
  48.       {
  49.         if (linkChecker == gLinksBeingChecked[i])
  50.           gLinksBeingChecked[i].status = status;
  51.       }
  52.  
  53.       if (gStartedAllChecks && gNumLinksCalledBack >= gNumLinksToCheck)
  54.       {
  55.         clearTimeout(gLinkCheckTimerID);
  56.         LinkCheckTimeOut();
  57.       }
  58.     }
  59.   }
  60. }
  61.  
  62. function Startup()
  63. {
  64.   var editor = GetCurrentEditor();
  65.   if (!editor)
  66.   {
  67.     window.close();
  68.     return;
  69.   }
  70.  
  71.   // Get all objects that refer to other locations
  72.   var objects;
  73.   try {
  74.     objects = editor.getLinkedObjects();
  75.   } catch (e) {}
  76.  
  77.   if (!objects || objects.Count() == 0)
  78.   {
  79.     AlertWithTitle(GetString("Alert"), GetString("NoLinksToCheck"));
  80.     window.close();
  81.     return;
  82.   }
  83.  
  84.   gDialog.LinksList = document.getElementById("LinksList");
  85.   gDialog.Close     = document.documentElement.getButton("cancel");
  86.  
  87.   // Set window location relative to parent window (based on persisted attributes)
  88.   SetWindowLocation();
  89.  
  90.  
  91.   // Loop over the nodes that have links:
  92.   for (var i = 0; i < objects.Count(); i++)
  93.   {
  94.     var refobj = objects.GetElementAt(gNumLinksToCheck).QueryInterface(Components.interfaces.nsIURIRefObject);
  95.     // Loop over the links in this node:
  96.     if (refobj)
  97.     {
  98.       try {
  99.         var uri;
  100.         while ((uri = refobj.GetNextURI()))
  101.         {
  102.           // Use the real class in netlib:
  103.           // Note that there may be more than one link per refobj
  104.           gURIRefObjects[gNumLinksToCheck] = refobj;
  105.  
  106.           // Make a new nsIURIChecker
  107.           gLinksBeingChecked[gNumLinksToCheck]
  108.             = Components.classes["@mozilla.org/network/urichecker;1"]
  109.                 .createInstance()
  110.                   .QueryInterface(Components.interfaces.nsIURIChecker);
  111.           // XXX uri creation needs to be localized
  112.           gLinksBeingChecked[gNumLinksToCheck].init(GetIOService().newURI(uri, null, null));
  113.           gLinksBeingChecked[gNumLinksToCheck].asyncCheck(gRequestObserver, null);
  114.  
  115.           // Add item  
  116.           var linkChecker = gLinksBeingChecked[gNumLinksToCheck].QueryInterface(Components.interfaces.nsIURIChecker);
  117.           SetItemStatus(linkChecker.name, "busy");
  118. dump(" *** Linkcount = "+gNumLinksToCheck+"\n");
  119.           gNumLinksToCheck++;
  120.  
  121.         };
  122.       } catch (e) { dump (" *** EXCEPTION\n");}
  123.     }
  124.   }
  125.   // Done with the loop, now we can be prepared for the finish:
  126.   gStartedAllChecks = true;
  127.  
  128.   // Start timer to limit how long we wait for link checking
  129.   gLinkCheckTimerID = setTimeout("LinkCheckTimeOut()", 5000);
  130.   window.sizeToContent();
  131. }
  132.  
  133. function LinkCheckTimeOut()
  134. {
  135.   // We might have gotten here via a late timeout
  136.   if (gNumLinksToCheck <= 0)
  137.     return;
  138.   gLinkCheckTimerID = 0;
  139.  
  140.   gNumLinksToCheck = 0;
  141.   gStartedAllChecks = false;
  142.   for (var i=0; i < gLinksBeingChecked.length; i++)
  143.   {
  144.     var linkChecker = gLinksBeingChecked[i].QueryInterface(Components.interfaces.nsIURIChecker);
  145.     // nsIURIChecker status values:
  146.     // NS_BINDING_SUCCEEDED     link is valid
  147.     // NS_BINDING_FAILED        link is invalid (gave an error)
  148.     // NS_BINDING_ABORTED       timed out, or cancelled
  149.     switch (linkChecker.status)
  150.     {
  151.       case 0:           // NS_BINDING_SUCCEEDED
  152.         SetItemStatus(linkChecker.name, "done");
  153.         break;
  154.       case 0x804b0001:  // NS_BINDING_FAILED
  155.         dump(">> " + linkChecker.name + " is broken\n");
  156.       case 0x804b0002:   // NS_BINDING_ABORTED
  157. //        dump(">> " + linkChecker.name + " timed out\n");
  158.       default:
  159. //        dump(">> " + linkChecker.name + " not checked\n");
  160.         SetItemStatus(linkChecker.name, "failed");
  161.         break;
  162.     }
  163.   }
  164.   gDialog.Close.setAttribute("label", GetString("Close"));
  165. }
  166.  
  167. // Add url to list of links to check
  168. // or set status for file already in the list
  169. // Returns true if url was in the list
  170. function SetItemStatus(url, status)
  171. {
  172.   if (!url)
  173.     return false;
  174.  
  175.   if (!status)
  176.     status = "busy";
  177.  
  178.   // Just set attribute for status icon 
  179.   // if we already have this url 
  180.   var listitems = document.getElementsByTagName("listitem");
  181.   if (listitems)
  182.   {
  183.     for (var i=0; i < listitems.length; i++)
  184.     {
  185.       if (listitems[i].getAttribute("label") == url)
  186.       {
  187.         listitems[i].setAttribute("progress", status);
  188.         return true;
  189.       }
  190.     }
  191.   }
  192.  
  193.   // We're adding a new item to list
  194.   var listitem = document.createElementNS(XUL_NS, "listitem");
  195.   if (listitem)
  196.   {
  197.     listitem.setAttribute("class", "listitem-iconic progressitem");
  198.     // This triggers CSS to show icon for each status state
  199.     listitem.setAttribute("progress", status);
  200.     listitem.setAttribute("label", url);
  201.     gDialog.LinksList.appendChild(listitem);
  202.   }
  203.   return false;
  204. }
  205.  
  206. function onAccept()
  207. {
  208.   SaveWindowLocation();
  209.   return true; // do close the window
  210. }
  211.  
  212. function onCancelLinkChecker()
  213. {
  214.   if (gLinkCheckTimerID)
  215.     clearTimeout(gLinkCheckTimerID);
  216.  
  217. /*
  218.   LinkCheckTimeOut();
  219. */
  220.   return onCancel();
  221. }
  222.