home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 August / PCpro_2005_08.ISO / files / firefox / ScrapBook.xpi / chrome / scrapbook.jar / content / calculate.js < prev    next >
Encoding:
JavaScript  |  2005-06-04  |  6.3 KB  |  200 lines

  1. /**************************************************
  2. // calculate.js
  3. // Implementation file for calculate.xul
  4. // 
  5. // Description:
  6. // Author: Gomita
  7. // Contributors: 
  8. // 
  9. // Version: 
  10. // License: see LICENSE.txt
  11. **************************************************/
  12.  
  13.  
  14.  
  15. var SBwizard;
  16. var SBstring;
  17. var SBlist;
  18.  
  19. const NS_XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
  20.  
  21.  
  22.  
  23. function SB_initList()
  24. {
  25.     SBstring = document.getElementById("ScrapBookString");
  26.     SBlist   = document.getElementById("ScrapBookCalcList");
  27.     SBwizard = document.getElementById("ScrapBookCalcWizard");
  28.     SBwizard.getButton("cancel").hidden = true;
  29.     SBwizard.getButton("back").disabled = false;
  30.     SBwizard.getButton("back").label = SBstring.getString("START_CALCULATION");
  31.     SBwizard.getButton("back").addEventListener("click", function() { SBcalculate.exec(); }, false);
  32.     if ( document.location.href.match(/\?reload$/) ) SBcalculate.exec();
  33. }
  34.  
  35.  
  36. var SBcalculate = {
  37.  
  38.     IDList : [],
  39.     ID2Size : {},
  40.     ResIDs : {},
  41.     DirIDs : {},
  42.     DirList : [],
  43.     index : 0,
  44.     total : 0,
  45.     totalSize : 0,
  46.  
  47.     exec : function()
  48.     {
  49.         if ( SBlist.childNodes.length > 2 ) window.location.href = "chrome://scrapbook/content/calculate.xul?reload";
  50.  
  51.         this.IDList = [];
  52.         this.ID2Size = {};
  53.         this.ResIDs = {};
  54.         this.DirIDs = {};
  55.         this.DirList = [];
  56.         this.index = 0;
  57.         this.total = 0;
  58.         this.totalSize = 0;
  59.         SBRDF.init();
  60.         var allRes = SBRDF.data.GetAllResources();
  61.         while ( allRes.hasMoreElements() )
  62.         {
  63.             var myRes = allRes.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  64.             if ( SBRDF.getProperty("type", myRes) == "folder" ) continue;
  65.             if ( myRes.Value != "urn:scrapbook:root" && myRes.Value != "urn:scrapbook:search" )
  66.             {
  67.                 this.ResIDs[SBRDF.getProperty("id", myRes)] = true;
  68.                 this.total++;
  69.             }
  70.         }
  71.         var dataDir = SBcommon.getScrapBookDir().clone();
  72.         dataDir.append("data");
  73.         var dataDirEnum = dataDir.directoryEntries;
  74.         while ( dataDirEnum.hasMoreElements() )
  75.         {
  76.             var myDir = dataDirEnum.getNext().QueryInterface(Components.interfaces.nsIFile);
  77.             this.DirList.push(myDir);
  78.             document.getElementById("ScrapBookMessage").value = SBstring.getString("INITIALIZING") + "... (" + ++this.index + ")";
  79.         }
  80.         this.asyncProcess(this.DirList[this.index = 0]);
  81.     },
  82.  
  83.     asyncProcess : function(aDir)
  84.     {
  85.         var myID = aDir.leafName;
  86.         var mySize = SB_getTotalFileSize(myID)[0];
  87.         this.IDList.push(myID);
  88.         this.ID2Size[myID] = mySize;
  89.         this.DirIDs[myID] = true;
  90.         this.totalSize += mySize;
  91.         this.index++;
  92.         document.getElementById("ScrapBookMessage").value  = SBstring.getString("CALCULATING") + "... (" + this.index + "/" + this.total + ")";
  93.         document.getElementById("ScrapBookProgress").value = Math.round( this.index / this.total * 100 );
  94.         if ( this.index < this.DirList.length ) {
  95.             setTimeout(function() { SBcalculate.asyncProcess(SBcalculate.DirList[SBcalculate.index]); }, 0);
  96.         } else {
  97.             this.finish();
  98.         }
  99.     },
  100.  
  101.     finish : function()
  102.     {
  103.         this.IDList.sort( function(a, b){ return(SBcalculate.ID2Size[b] - SBcalculate.ID2Size[a]); });
  104.  
  105.         for ( var i = 0; i < this.IDList.length; i++ )
  106.         {
  107.             var mySize = this.ID2Size[this.IDList[i]];
  108.             var myRes   = SBservice.RDF.GetResource("urn:scrapbook:item" + this.IDList[i]);
  109.             var myTitle = SBRDF.getProperty("title", myRes);
  110.             var myImage = SBRDF.getProperty("icon",  myRes);
  111.             var myType  = SBRDF.getProperty("type",  myRes);
  112.             if ( !myTitle ) myTitle = this.IDList[i] + " (" + SBstring.getString("INVALID") + ")";
  113.             if ( !myImage ) myImage = SBcommon.getDefaultIcon(myType);
  114.             var listItem  = document.createElement("listitem");
  115.             var listCell1 = document.createElement("listcell");
  116.             var listCell2 = document.createElement("listcell");
  117.             listItem.setAttribute("id", this.IDList[i]);
  118.             listItem.setAttribute("type", myType);
  119.             listCell1.setAttribute("class", "listcell-iconic");
  120.             listCell1.setAttribute("image", myImage);
  121.             listCell1.setAttribute("label", myTitle);
  122.             listCell2.setAttribute("class", "text-right");
  123.             listCell2.setAttribute("label", SB_formatFileSize([mySize, false], null));
  124.             if ( !this.ResIDs[this.IDList[i]] )
  125.             {
  126.                 listCell1.setAttribute("style", "color:#FF0000;font-weight:bold;");
  127.                 listCell2.setAttribute("style", "color:#FF0000;font-weight:bold;");
  128.                 listItem.setAttribute("invalid", "true");
  129.             }
  130.             listItem.appendChild(listCell1);
  131.             listItem.appendChild(listCell2);
  132.             SBlist.appendChild(listItem);
  133.         }
  134.  
  135.         document.getElementById("ScrapBookMessage").value  = "";
  136.         document.getElementById("ScrapBookProgress").value = "0";
  137.  
  138.         var LackIDs  = [];
  139.         for ( var aID in this.ResIDs )
  140.         {
  141.             if ( !this.DirIDs[aID] ) LackIDs.push(aID);
  142.         }
  143.  
  144.         var SurpIDs = [];
  145.         for ( var aID in this.DirIDs )
  146.         {
  147.             if ( !this.ResIDs[aID] ) SurpIDs.push(aID);
  148.         }
  149.  
  150.         document.getElementById("ScrapBookTotalSize").value  = SB_formatFileSize([this.totalSize, this.DirList.length], "ITEMS_COUNT");
  151.  
  152.         var msg = ( SurpIDs.length == 0 ) ? SBstring.getString("DIAGNOSIS_OK") : SBstring.getFormattedString("DIAGNOSIS_NG", [SurpIDs.length]);
  153.         if ( LackIDs.length > 0 ) msg += " (" + LackIDs + ")";
  154.         document.getElementById("ScrapBookDiagnosis").value = msg;
  155.  
  156.     },
  157.  
  158. };
  159.  
  160.  
  161.  
  162. function SB_createPopupMenuList(aEvent)
  163. {
  164.     var isValid = ( SBlist.currentItem.getAttribute("invalid") != "true");
  165.     document.getElementById("ScrapBookTreePopupD").setAttribute("disabled", isValid);
  166.     document.getElementById("ScrapBookTreePopupP").setAttribute("disabled", !isValid);
  167. }
  168.  
  169.  
  170. function SB_openList(aEvent, tabbed)
  171. {
  172.     if ( aEvent.originalTarget.localName != "listitem" && aEvent.originalTarget.localName != "menuitem" ) return;
  173.     var myID   = SBlist.currentItem.id;
  174.     var myType = SBlist.currentItem.getAttribute("type");
  175.     SBcommon.loadURL(SBcommon.getURL(myID, myType), tabbed);
  176. }
  177.  
  178.  
  179. function SB_transmitList(flag)
  180. {
  181.     var myID = SBlist.currentItem.id;
  182.     if ( !myID ) return;
  183.     switch ( flag )
  184.     {
  185.         case "P" : window.openDialog("chrome://scrapbook/content/property.xul", "", "modal,centerscreen,chrome" ,myID); break;
  186.         case "L" : SBcommon.launchDirectory(SBcommon.getContentDir(myID));
  187.         default  : break;
  188.     }
  189. }
  190.  
  191.  
  192. function SB_deleteList()
  193. {
  194.     var myID = SBlist.currentItem.id;
  195.     if ( myID.length == 14 ) SBcommon.removeDirSafety( SBcommon.getContentDir(myID) );
  196.     SBlist.removeItemAt(SBlist.selectedIndex + 2);
  197. }
  198.  
  199.  
  200.