home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / browser.jar / content / browser / pref / pref-themes.js < prev    next >
Text File  |  2002-11-28  |  9KB  |  291 lines

  1.  
  2. var gShowDescription = true;
  3. var gData;
  4. const kPrefSvcContractID = "@mozilla.org/preferences;1";
  5. const kPrefSvcIID = Components.interfaces.nsIPref;
  6. const kPrefSvc = Components.classes[kPrefSvcContractID].getService(kPrefSvcIID);
  7.  
  8. try {
  9.   var chromeRegistry = Components.classes["@mozilla.org/chrome/chrome-registry;1"].getService();
  10.   if (chromeRegistry)
  11.     chromeRegistry = chromeRegistry.QueryInterface(Components.interfaces.nsIXULChromeRegistry);
  12. }
  13. catch(e) {}
  14.  
  15. function Startup()
  16. {
  17.   gData = parent.hPrefWindow.wsm.dataManager.pageData["chrome://browser/content/pref/pref-themes.xul"];
  18.   var list = document.getElementById( "skinsList" );
  19.   if ("loaded" in gData && "themeIndex" in gData) {
  20.     list.selectedIndex = gData.themeIndex;    
  21.     return;
  22.   }
  23.   gData.loaded = true;
  24.   parent.hPrefWindow.registerOKCallbackFunc(applyTheme);
  25.  
  26.   var theme = null;
  27.   try {
  28.     theme = kPrefSvc.getComplexValue("general.skins.selectedSkin",
  29.                                      Components.interfaces.nsISupportsString).data;
  30.   } catch (e) {
  31.   }
  32.   var matches;
  33.   for (var i = 0; i < list.childNodes.length; ++i) {
  34.     var child = list.childNodes[i];
  35.     var name = child.getAttribute("name");
  36.     if (!theme)
  37.       matches = chromeRegistry.isSkinSelectedForPackage(name, "browser", true);
  38.     else
  39.       matches = name == theme;
  40.     if (matches) {
  41.       list.selectItem(child);
  42.       break;
  43.     }      
  44.   }
  45.  
  46.   var navbundle = document.getElementById("bundle_navigator");
  47.   var showSkinsDescription = navbundle.getString("showskinsdescription");
  48.   if (showSkinsDescription == "false")
  49.   {
  50.     gShowDescription = false;
  51.     var description = document.getElementById("description");
  52.     while (description.hasChildNodes())
  53.       description.removeChild(description.firstChild);
  54.   }
  55.  
  56.   var extList = document.getElementById("extList");
  57.   for (var i = 0; i < extList.childNodes.length; ++i) {
  58.     if (extList.childNodes[i].getAttribute("name")) {
  59.       extList.selectItem(extList.childNodes[i]);
  60.       break;
  61.     }
  62.   }
  63. }
  64.  
  65. function applyTheme()
  66. {
  67.   var data = parent.hPrefWindow.wsm.dataManager.pageData["chrome://browser/content/pref/pref-themes.xul"];
  68.   if (data.name == null)
  69.     return;
  70.  
  71.   const kPrefSvcContractID = "@mozilla.org/preferences;1";
  72.   const kPrefSvcIID = Components.interfaces.nsIPref;
  73.   const kPrefSvc = Components.classes[kPrefSvcContractID].getService(kPrefSvcIID);
  74.  
  75.   var theme = null;
  76.   try {
  77.     theme = kPrefSvc.getComplexValue("general.skins.selectedSkin",
  78.                                      Components.interfaces.nsISupportsString).data;
  79.   } catch (e) {
  80.   }
  81.  
  82.   if (theme == data.name) return;
  83.  
  84.   try {
  85.     var reg = Components.classes["@mozilla.org/chrome/chrome-registry;1"].getService();
  86.     if (reg)
  87.       reg = reg.QueryInterface(Components.interfaces.nsIXULChromeRegistry);
  88.   }
  89.   catch(e) {}
  90.  
  91.   var inUse = reg.isSkinSelected(data.name, true);
  92.   if (!theme && inUse == Components.interfaces.nsIChromeRegistry.FULL) return;
  93.  
  94.   var chromeRegistry = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
  95.     .getService(Components.interfaces.nsIXULChromeRegistry);
  96.  
  97.   var oldTheme = false;
  98.   try {
  99.     oldTheme = !chromeRegistry.checkThemeVersion(data.name);
  100.   }
  101.   catch(e) {
  102.   }
  103.  
  104.  
  105.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  106.   if (oldTheme) {
  107.     var title = gNavigatorBundle.getString("oldthemetitle");
  108.     var message = gNavigatorBundle.getString("oldTheme");
  109.  
  110.     message = message.replace(/%theme_name%/, themeName.getAttribute("displayName"));
  111.     message = message.replace(/%brand%/g, gBrandBundle.getString("brandShortName"));
  112.  
  113.     if (promptService.confirm(window, title, message)){
  114.       var inUse = chromeRegistry.isSkinSelected(data.name, true);
  115.  
  116.       chromeRegistry.uninstallSkin(data.name, true);
  117.  
  118.       var str = Components.classes["@mozilla.org/supports-string;1"]
  119.                           .createInstance(Components.interfaces.nsISupportsString);
  120.  
  121.       str.data = true;
  122.       pref.setComplexValue("general.skins.removelist." + data.name,
  123.                            Components.interfaces.nsISupportsString, str);
  124.       
  125.       if (inUse)
  126.         chromeRegistry.refreshSkins();
  127.     }
  128.  
  129.     return;
  130.   }
  131.  
  132. var str = Components.classes["@mozilla.org/supports-string;1"]
  133.                     .createInstance(Components.interfaces.nsISupportsString);
  134. str.data = data.name;
  135. kPrefSvc.setComplexValue("general.skins.selectedSkin", Components.interfaces.nsISupportsString, str);
  136.  
  137.  
  138. chromeRegistry.selectSkin(data.name, true);                                        
  139. chromeRegistry.refreshSkins();
  140. }
  141.  
  142.  
  143. function uninstallSkin()
  144. {
  145.   var list = document.getElementById("skinsList");
  146.   var selectedSkinItem = list.selectedItems[0];
  147.   var skinName = selectedSkinItem.getAttribute("name");
  148.   var inUse = chromeRegistry.isSkinSelected(skinName, true);
  149.   chromeRegistry.uninstallSkin(skinName, true);
  150.   if (inUse)
  151.     chromeRegistry.refreshSkins();
  152.   list.selectedIndex = 0;
  153. }
  154.  
  155. function themeSelect()
  156. {
  157.   var list = document.getElementById("skinsList");
  158.  
  159.   if (!list)
  160.     return;
  161.  
  162.   var prefbundle = document.getElementById("bundle_prefutilities");
  163.  
  164.   var selectedItem = list.selectedItems.length ? list.selectedItems[0] : null;
  165.   if (selectedItem && selectedItem.getAttribute("skin") == "true") {
  166.     var themeName = selectedItem.getAttribute("displayName");
  167.     var skinName = selectedItem.getAttribute("name");
  168.     gData.name = skinName;
  169.     gData.themeIndex = list.selectedIndex;
  170.  
  171.     var oldTheme;
  172.     try {
  173.       oldTheme = !chromeRegistry.checkThemeVersion(skinName);
  174.     }
  175.     catch(e) {
  176.       oldTheme = false;
  177.     }
  178.  
  179.     var nameField = document.getElementById("displayName");
  180.     var author = document.getElementById("author");
  181.     var image = document.getElementById("previewImage");
  182.     var descText = document.createTextNode(selectedItem.getAttribute("description"));
  183.     var description = document.getElementById("description");
  184.     var uninstallButton = document.getElementById("uninstallSkin");
  185.     var uninstallLabel = prefbundle.getString("uninstallThemePrefix");
  186.  
  187.     while (description.hasChildNodes())
  188.       description.removeChild(description.firstChild);
  189.  
  190.     nameField.setAttribute("value", themeName);
  191.     author.setAttribute("value", selectedItem.getAttribute("author"));
  192.     image.setAttribute("src", selectedItem.getAttribute("image"));
  193.  
  194.     // XXX - this sucks and should only be temporary.
  195.     var selectedSkin = "";
  196.     try {
  197.       selectedSkin = kPrefSvc.CopyCharPref("general.skins.selectedSkin");
  198.     }
  199.     catch (e) {
  200.     }
  201.     if (!oldTheme) {    
  202.       if( gShowDescription ) 
  203.         description.appendChild(descText);
  204.  
  205.       var locType = selectedItem.getAttribute("loctype");
  206.       uninstallButton.disabled = (selectedSkin == skinName) || (locType == "install");
  207.       
  208.       uninstallLabel = uninstallLabel.replace(/%theme_name%/, themeName);
  209.       uninstallButton.label = uninstallLabel;
  210.     }
  211.     else {
  212.       var brandbundle = document.getElementById("bundle_brand");
  213.  
  214.       uninstallLabel = uninstallLabel.replace(/%theme_name%/, themeName);
  215.       uninstallButton.label = uninstallLabel;
  216.  
  217.       uninstallButton.disabled = selectedSkin == skinName;
  218.  
  219.       var newText = prefbundle.getString("oldTheme");
  220.       newText = newText.replace(/%theme_name%/, themeName);
  221.       
  222.       newText = newText.replace(/%brand%/g, brandbundle.getString("brandShortName"));
  223.  
  224.       if( gShowDescription )  {
  225.         descText = document.createTextNode(newText);
  226.         description.appendChild(descText);
  227.       }
  228.     }
  229.   }
  230.   else {
  231.     uninstallButton.disabled = true;
  232.     gData.name = null;
  233.   }
  234. }
  235.  
  236.  
  237. function extensionSelect()
  238. {
  239.   var list = document.getElementById("extList");
  240.  
  241.   if (!list)
  242.     return;
  243.  
  244.   var selectedItem = list.selectedItems.length ? list.selectedItems[0] : null;
  245.   if (selectedItem) {
  246.     var extName = selectedItem.getAttribute("displayName");
  247.     var nameField = document.getElementById("extDisplayName");
  248.     var author = document.getElementById("extAuthor");
  249.     var descText = document.createTextNode(selectedItem.getAttribute("description"));
  250.     var description = document.getElementById("extDescription");
  251.     var uninstallButton = document.getElementById("uninstallExtension");
  252.     
  253.     while (description.hasChildNodes())
  254.       description.removeChild(description.firstChild);
  255.  
  256.     nameField.setAttribute("value", extName);
  257.     author.setAttribute("value", selectedItem.getAttribute("author"));
  258.     
  259.     description.appendChild(descText);
  260.  
  261.     updateDisableExtButton(selectedItem);
  262.   }
  263. }
  264.  
  265. function toggleExtension()
  266. {
  267.   var list = document.getElementById("extList");
  268.  
  269.   if (!list)
  270.     return;
  271.  
  272.   var selectedItem = list.selectedItems.length ? list.selectedItems[0] : null;
  273.   if (selectedItem) {
  274.     var disabled = (selectedItem.getAttribute("disabledState") == "true");
  275.     chromeRegistry.setAllowOverlaysForPackage(selectedItem.getAttribute("name"), disabled);
  276.     updateDisableExtButton(selectedItem);
  277.   }   
  278. }
  279.  
  280. function updateDisableExtButton(item)
  281. {
  282.   var disableButton = document.getElementById("disableExtension");
  283.   if (disableButton.disabled)
  284.     disableButton.disabled = false;
  285.  
  286.   if (item.getAttribute("disabledState") == "true")
  287.     disableButton.setAttribute("label", "Enable Extension"); // XXXdwh localize
  288.   else
  289.     disableButton.setAttribute("label", "Disable Extension"); // XXXdwh localize
  290. }
  291.