home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / editor / EditorPublishProgress.js < prev    next >
Text File  |  2003-06-08  |  12KB  |  404 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.  *   Charles Manske (cmanske@netscape.com)
  22.  */
  23.  
  24. var gInProgress = true;
  25. var gPublishData;
  26. var gPersistObj;
  27. var gTotalFileCount = 0;
  28. var gSucceededCount = 0;
  29. var gFinished = false;
  30. var gPublishingFailed = false;
  31. var gFileNotFound = false;
  32. var gStatusMessage="";
  33.  
  34. var gTimerID;
  35. var gAllowEnterKey = false;
  36.  
  37. // Publishing error codes
  38. //   These are translated from C++ error code strings like this:
  39. //   kFileNotFound = "FILE_NOT_FOUND",
  40. const kNetReset = 2152398868; // nsISocketTransportService.idl
  41. const kFileNotFound = 2152857618;
  42. const kNotConnected = 2152398860; // in netCore.h
  43. const kConnectionRefused = 2152398861; // nsISocketTransportService.idl
  44. const kNetTimeout = 2152398862; // nsISocketTransportService.idl
  45. const kNoConnectionOrTimeout = 2152398878;
  46. const kPortAccessNotAllowed = 2152398867; // netCore.h
  47. const kOffline = 2152398865; // netCore.h
  48. const kDiskFull = 2152857610;
  49. const kNoDeviceSpace = 2152857616;
  50. const kNameTooLong = 2152857617;
  51. const kAccessDenied = 2152857621;
  52.  
  53. // These are more errors that I don't think we encounter during publishing,
  54. //  so we don't have error strings yet. Let's keep them here for future reference
  55. //const kUnrecognizedPath = 2152857601;
  56. //const kUnresolvableSymlink = 2152857602;
  57. //const kUnknownType = 2152857604;
  58. //const kDestinationNotDir = 2152857605;
  59. //const kTargetDoesNotExist = 2152857606;
  60. //const kAlreadyExists = 2152857608;
  61. //const kInvalidPath = 2152857609;
  62. //const kNotDirectory = 2152857612;
  63. //const kIsDirectory = 2152857613;
  64. //const kIsLocked = 2152857614;
  65. //const kTooBig = 2152857615;
  66. //const kReadOnly = 2152857619;
  67. //const kDirectoryNotEmpty = 2152857620;
  68. //const kErrorBindingRedirected = 2152398851;
  69. //const kAlreadyConnected = 2152398859; // in netCore.h
  70. //const kInProgress = 2152398863; // netCore.h
  71. //const kNoContent = 2152398865; // netCore.h
  72. //const kUnknownProtocol = 2152398866 // netCore.h
  73. //const kFtpLogin = 2152398869; // ftpCore.h
  74. //const kFtpCWD = 2152398870; // ftpCore.h
  75. //const kFtpPasv = 2152398871; // ftpCore.h
  76. //const kFtpPwd = 2152398872; // ftpCore.h
  77.  
  78.  
  79. function Startup()
  80. {
  81.   gPublishData = window.arguments[0];
  82.   if (!gPublishData)
  83.   {
  84.     dump("No publish data!\n");
  85.     window.close();
  86.     return;
  87.   }
  88.  
  89.   gDialog.FileList           = document.getElementById("FileList");
  90.   gDialog.FinalStatusMessage = document.getElementById("FinalStatusMessage");
  91.   gDialog.StatusMessage      = document.getElementById("StatusMessage");
  92.   gDialog.KeepOpen           = document.getElementById("KeepOpen");
  93.   gDialog.Close              = document.documentElement.getButton("cancel");
  94.  
  95.   SetWindowLocation();
  96.   var title = GetDocumentTitle();
  97.   if (!title)
  98.     title = "("+GetString("untitled")+")";
  99.   window.title = GetString("PublishProgressCaption").replace(/%title%/, title);
  100.  
  101.   document.getElementById("PublishToSite").value = 
  102.     GetString("PublishToSite").replace(/%title%/, TruncateStringAtWordEnd(gPublishData.siteName, 25)); 
  103.  
  104.   // Show publishing destination URL
  105.   document.getElementById("PublishUrl").value = gPublishData.publishUrl;
  106.   
  107.   // Show subdirectories only if not empty
  108.   if (gPublishData.docDir || gPublishData.otherDir)
  109.   {
  110.     if (gPublishData.docDir)
  111.       document.getElementById("docDir").value = gPublishData.docDir;
  112.     else
  113.       document.getElementById("DocSubdir").hidden = true;
  114.       
  115.     if (gPublishData.publishOtherFiles && gPublishData.otherDir)
  116.       document.getElementById("otherDir").value = gPublishData.otherDir;
  117.     else
  118.       document.getElementById("OtherSubdir").hidden = true;
  119.   }
  120.   else
  121.     document.getElementById("Subdirectories").hidden = true;
  122.  
  123.   // Add the document to the "publish to" list as quick as possible!
  124.   SetProgressStatus(gPublishData.filename, "busy");
  125.  
  126.   if (gPublishData.publishOtherFiles)
  127.   {
  128.     // When publishing images as well, expand list to show more items
  129.     gDialog.FileList.setAttribute("rows", 5);
  130.     window.sizeToContent();
  131.   }
  132.  
  133.   // Now that dialog is initialized, we can start publishing
  134.   window.opener.StartPublishing();
  135. }
  136.  
  137. // this function is to be used when we cancel persist's saving
  138. // since not all messages will be returned to us if we cancel
  139. // this function changes status for all non-done/non-failure to failure
  140. function SetProgressStatusCancel()
  141. {
  142.   var listitems = document.getElementsByTagName("listitem");
  143.   if (!listitems)
  144.     return;
  145.  
  146.   for (var i=0; i < listitems.length; i++)
  147.   {
  148.     var attr = listitems[i].getAttribute("progress");
  149.     if (attr != "done" && attr != "failed")
  150.       listitems[i].setAttribute("progress", "failed");
  151.   }
  152. }
  153.  
  154. // Add filename to list of files to publish
  155. // or set status for file already in the list
  156. // Returns true if file was in the list
  157. function SetProgressStatus(filename, status)
  158. {
  159.   if (!filename)
  160.     return false;
  161.  
  162.   if (!status)
  163.     status = "busy";
  164.  
  165.   // Just set attribute for status icon 
  166.   // if we already have this filename 
  167.   var listitems = document.getElementsByTagName("listitem");
  168.   if (listitems)
  169.   {
  170.     for (var i=0; i < listitems.length; i++)
  171.     {
  172.       if (listitems[i].getAttribute("label") == filename)
  173.       {
  174.         listitems[i].setAttribute("progress", status);
  175.         return true;
  176.       }
  177.     }
  178.   }
  179.   // We're adding a new file item to list
  180.   gTotalFileCount++;
  181.  
  182.   var listitem = document.createElementNS(XUL_NS, "listitem");
  183.   if (listitem)
  184.   {
  185.     listitem.setAttribute("class", "listitem-iconic progressitem");
  186.     // This triggers CSS to show icon for each status state
  187.     listitem.setAttribute("progress", status);
  188.     listitem.setAttribute("label", filename);
  189.     gDialog.FileList.appendChild(listitem);
  190.   }
  191.   return false;
  192. }
  193.  
  194. function SetProgressFinished(filename, networkStatus)
  195. {
  196.   var abortPublishing = false;
  197.   if (filename)
  198.   {
  199.     var status = networkStatus ? "failed" : "done";
  200.     if (networkStatus == 0)
  201.       gSucceededCount++;
  202.  
  203.     SetProgressStatus(filename, status);
  204.   }
  205.  
  206.   if (networkStatus != 0) // Error condition
  207.   {
  208.     // We abort on all errors except if image file was not found
  209.     abortPublishing = networkStatus != kFileNotFound;
  210.  
  211.     // Mark all remaining files as "failed"
  212.     if (abortPublishing)
  213.     {
  214.       gPublishingFailed = true;
  215.       SetProgressStatusCancel();
  216.       gDialog.FinalStatusMessage.value = GetString("PublishFailed");
  217.     }
  218.  
  219.     switch (networkStatus)
  220.     {
  221.       case kFileNotFound:
  222.         gFileNotFound = true;
  223.         if (filename)
  224.           gStatusMessage = GetString("FileNotFound").replace(/%file%/, filename);
  225.         break;
  226.       case kNetReset:
  227.         // We get this when subdir doesn't exist AND
  228.         //   if filename used is same as an existing subdir 
  229.         var dir = (gPublishData.filename == filename) ? 
  230.                      gPublishData.docDir : gPublishData.otherDir;
  231.  
  232.         if (dir)
  233.         {
  234.           // This is the ambiguous case when we can't tell if subdir or filename is bad
  235.           // Remove terminal "/" from dir string and insert into message
  236.           gStatusMessage = GetString("SubdirDoesNotExist").replace(/%dir%/, dir.slice(0, dir.length-1));
  237.           gStatusMessage = gStatusMessage.replace(/%file%/, filename);
  238.  
  239.           // Remove directory from saved prefs
  240.           // XXX Note that if subdir is good, 
  241.           //     but filename = next level subdirectory name, 
  242.           //     we really shouldn't remove subdirectory, 
  243.           //     but it's impossible to differentiate this case!
  244.           RemovePublishSubdirectoryFromPrefs(gPublishData, dir);
  245.         }
  246.         else if (filename)
  247.           gStatusMessage = GetString("FilenameIsSubdir").replace(/%file%/, filename);
  248.  
  249.         break;
  250.       case kNotConnected:
  251.       case kConnectionRefused:
  252.       case kNetTimeout:
  253.       case kNoConnectionOrTimeout:
  254.       case kPortAccessNotAllowed:
  255.         gStatusMessage = GetString("ServerNotAvailable");
  256.         break;
  257.       case kOffline:
  258.         gStatusMessage = GetString("Offline");
  259.         break;
  260.       case kDiskFull:
  261.       case kNoDeviceSpace:
  262.         if (filename)
  263.           gStatusMessage = GetString("DiskFull").replace(/%file%/, filename);
  264.         break;
  265.       case kNameTooLong:
  266.         if (filename)
  267.           gStatusMessage = GetString("NameTooLong").replace(/%file%/, filename);
  268.         break;
  269.       case kAccessDenied:
  270.         if (filename)
  271.           gStatusMessage = GetString("AccessDenied").replace(/%file%/, filename);
  272.         break;
  273.       case kUnknownType:
  274.       default:
  275.         gStatusMessage = GetString("UnknownPublishError")
  276.         break;
  277.     }
  278.   }
  279.   else if (!filename)
  280.   {
  281.     gFinished = true;
  282.     gDialog.Close.setAttribute("label", GetString("Close"));
  283.     if (!gStatusMessage)
  284.       gStatusMessage = GetString(gPublishingFailed ? "UnknownPublishError" : "AllFilesPublished");
  285.  
  286.     // Now allow "Enter/Return" key to close the dialog
  287.     AllowDefaultButton();
  288.  
  289.     if (gPublishingFailed || gFileNotFound)
  290.     {
  291.       // Show "Troubleshooting" button to help solving problems
  292.       //  and key for successful / failed files
  293.       document.getElementById("failureBox").hidden = false;
  294.     }
  295.   }
  296.  
  297.   if (gStatusMessage)
  298.     SetStatusMessage(gStatusMessage);
  299. }
  300.  
  301. function CheckKeepOpen()
  302. {
  303.   if (gTimerID)
  304.   {
  305.     clearTimeout(gTimerID);
  306.     gTimerID = null;
  307.   }
  308. }
  309.  
  310. function onClose()
  311. {
  312.   if (gTimerID)
  313.   {
  314.     clearTimeout(gTimerID);
  315.     gTimerID = null;
  316.   }
  317.  
  318.   if (!gFinished && gPersistObj)
  319.   {
  320.     try {
  321.       gPersistObj.cancelSave();
  322.     } catch (e) {}
  323.   }
  324.   SaveWindowLocation();
  325.  
  326.   // Tell caller so they can cleanup and restore editability
  327.   window.opener.FinishPublishing();
  328.   return true;
  329. }
  330.  
  331. function AllowDefaultButton()
  332. {
  333.   gDialog.Close.setAttribute("default","true");
  334.   gAllowEnterKey = true;
  335. }
  336.  
  337. function doTroubleshooting()
  338. {
  339.   openHelp("comp-doc-publish-troubleshooting");
  340. }
  341.  
  342. function onEnterKey()
  343. {
  344.   if (gAllowEnterKey)
  345.     return CloseDialog();
  346.  
  347.   return false;
  348. }
  349.  
  350. function RequestCloseDialog()
  351. {
  352.   // Finish progress messages, settings buttons etc.
  353.   SetProgressFinished(null, 0);
  354.  
  355.   if (!gDialog.KeepOpen.checked)
  356.   {
  357.     // Leave window open a minimum amount of time 
  358.     gTimerID = setTimeout("CloseDialog();", 3000);
  359.   }
  360.  
  361.   // Set "completed" message if we succeeded
  362.   // (Some image files may have failed,
  363.   //  but we don't abort publishing for that)
  364.   if (!gPublishingFailed)
  365.   {
  366.     gDialog.FinalStatusMessage.value = GetString("PublishCompleted");
  367.     if (gFileNotFound && gTotalFileCount-gSucceededCount)
  368.     {
  369.       // Show number of files that failed to upload
  370.       gStatusMessage = 
  371.         (GetString("FailedFileMsg").replace(/%x%/,(gTotalFileCount-gSucceededCount)))
  372.           .replace(/%total%/,gTotalFileCount);
  373.  
  374.       SetStatusMessage(gStatusMessage);
  375.     }
  376.   }
  377. }
  378.  
  379. function SetStatusMessage(message)
  380. {
  381.   // Status message is a child of <description> element
  382.   //  so text can wrap to multiple lines if necessary
  383.   if (gDialog.StatusMessage.firstChild)
  384.   {
  385.     gDialog.StatusMessage.firstChild.data = message;
  386.   }
  387.   else
  388.   {
  389.     var textNode = document.createTextNode(message);
  390.     if (textNode)
  391.       gDialog.StatusMessage.appendChild(textNode);
  392.   }
  393.   window.sizeToContent();
  394. }
  395.  
  396. function CloseDialog()
  397. {
  398.   SaveWindowLocation();
  399.   window.opener.FinishPublishing();
  400.   try {
  401.     window.close();
  402.   } catch (e) {}
  403. }
  404.