home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9866 / 9866.xpi / modules / samfind_modoptionsdialog.jsm < prev    next >
Encoding:
Text File  |  2009-11-13  |  14.1 KB  |  485 lines

  1. //-------------------------------------------------------------------------------------------
  2. // Options dialog module.
  3. //-------------------------------------------------------------------------------------------
  4.  
  5. const Cc = Components.classes;
  6. const Ci = Components.interfaces;
  7.  
  8. var EXPORTED_SYMBOLS = ["samfind_modoptionsdialog"];
  9.  
  10. Components.utils.import("resource://samfind/samfind_modutils.jsm");
  11.  
  12. var samfind_modoptionsdialog =
  13. {
  14.     _io_service : null,
  15.     _favicon_service : null,
  16.  
  17.     _init : function()
  18.     {
  19.         samfind_modoptionsdialog._io_service = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  20.         samfind_modoptionsdialog._favicon_service = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
  21.     },
  22.  
  23.     //-------------------------------------------------------------------------------------------
  24.     //
  25.     //-------------------------------------------------------------------------------------------
  26.  
  27.     updateTopicIconPreview : function(textbox)
  28.     {
  29.         var img = textbox.ownerDocument.getElementById("validate-image");
  30.         img.onerror = samfind_modoptionsdialog.topicImageOnError;
  31.         img.onload = samfind_modoptionsdialog.topicImageOnLoad;
  32.         img.src = samfind_modutils.trim(textbox.value);
  33.     },
  34.  
  35.     openTopicFP : function(win)
  36.     {
  37.         var nsIFilePicker = Ci.nsIFilePicker;
  38.         var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  39.         fp.init(win, "samfind | Select a topic icon", nsIFilePicker.modeOpen);
  40.         fp.appendFilters(nsIFilePicker.filterImages);
  41.         var res = fp.show();
  42.         if (res == nsIFilePicker.returnOK)
  43.         {
  44.             var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  45.             var url = ios.newFileURI(fp.file);
  46.             var img = win.document.getElementById("validate-image");
  47.             img.onerror = samfind_modoptionsdialog.topicImageOnError;
  48.             img.onload = samfind_modoptionsdialog.topicImageOnLoad;
  49.             img.src = url.spec;
  50.         }
  51.         fp = null;
  52.     },
  53.  
  54.     topicImageOnError : function(event)
  55.     {
  56.         event.target.ownerDocument.getElementById("topic-icon-preview").src = "";
  57.     },
  58.  
  59.     topicImageOnLoad : function(event)
  60.     {
  61.         var image = event.target;
  62.         var document = image.ownerDocument;
  63.         var textbox = document.getElementById("topic-icon");
  64.         if (image.width <= 32 && image.height <= 32)
  65.         {
  66.             textbox.value = image.src;
  67.             document.getElementById("topic-icon-preview").src = image.src;
  68.         }
  69.         else
  70.         {
  71.             var old_value = textbox.value;
  72.              textbox.value = "Please upload images with maximum size 32x32 pixels";
  73.             textbox.style.color = "red";
  74.             document.defaultView.setTimeout(function()
  75.                 {
  76.                     if (textbox.value == "Please upload images with maximum size 32x32 pixels")
  77.                     {
  78.                         textbox.value = old_value;
  79.                     }
  80.                     textbox.style.color = "black";
  81.                     textbox.focus();
  82.                 },
  83.                 2000);
  84.         }
  85.         return false;
  86.     },
  87.  
  88.     //-------------------------------------------------------------------------------------------
  89.     //
  90.     //-------------------------------------------------------------------------------------------
  91.  
  92.     setWebsiteIcon : function(textbox, caller)
  93.     {
  94.         samfind_modoptionsdialog.getFavicon(textbox.value, caller);
  95.     },
  96.  
  97.     updateWebsiteIconPreview : function(textbox)
  98.     {
  99.         var img = textbox.ownerDocument.getElementById("validate-image");
  100.         img.onerror = samfind_modoptionsdialog.websiteImageOnError;
  101.         img.onload = samfind_modoptionsdialog.websiteImageOnLoad;
  102.         img.src = samfind_modutils.trim(textbox.value);
  103.     },
  104.  
  105.     openWebsiteFP : function(win)
  106.     {
  107.         var nsIFilePicker = Ci.nsIFilePicker;
  108.         var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  109.         fp.init(win, "samfind | Select a website icon", nsIFilePicker.modeOpen);
  110.         fp.appendFilters(nsIFilePicker.filterImages);
  111.         var res = fp.show();
  112.         if (res == nsIFilePicker.returnOK)
  113.         {
  114.             var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  115.             var url = ios.newFileURI(fp.file);
  116.             var img = win.document.getElementById("validate-image");
  117.             img.onerror = samfind_modoptionsdialog.websiteImageOnError;
  118.             img.onload = samfind_modoptionsdialog.websiteImageOnLoad;
  119.             img.src = url.spec;
  120.         }
  121.         fp = null;
  122.     },
  123.  
  124.     websiteImageOnError : function(event)
  125.     {
  126.         var document = event.target.ownerDocument;
  127.         document.getElementById("website-icon-preview-1").src = "chrome://mozapps/skin/places/defaultFavicon.png";
  128.         document.getElementById("website-icon-preview-2").src = "chrome://mozapps/skin/places/defaultFavicon.png";    
  129.     },
  130.  
  131.     websiteImageOnLoad : function(event)
  132.     {
  133.         var image = event.target;
  134.         var document = image.ownerDocument;
  135.         var textbox = document.getElementById("website-icon");
  136.         if (image.width <= 32 && image.height <= 32)
  137.         {
  138.             textbox.value = image.src;
  139.             document.getElementById("website-icon-preview-1").src = image.src;
  140.             document.getElementById("website-icon-preview-2").src = image.src;
  141.         }
  142.         else
  143.         {
  144.             var old_value = textbox.value;
  145.              textbox.value = "Please upload images with maximum size 32x32 pixels";
  146.             textbox.style.color = "red";
  147.             document.defaultView.setTimeout(function()
  148.                 {
  149.                     if (textbox.value == "Please upload images with maximum size 32x32 pixels")
  150.                     {
  151.                         textbox.value = old_value;
  152.                     }
  153.                     textbox.style.color = "black";
  154.                     textbox.focus();
  155.                 },
  156.                 2000);
  157.         }
  158.         return false;
  159.     },
  160.  
  161.     //-------------------------------------------------------------------------------------------
  162.     //
  163.     //-------------------------------------------------------------------------------------------
  164.  
  165.     getFavicon : function(url, caller)
  166.     {
  167.         var favicon = null;
  168.         var url_pre_path = null;
  169.         try
  170.         {
  171.             var url_object = samfind_modoptionsdialog._io_service.newURI(url, null, null);
  172.             url_pre_path = url_object.prePath;
  173.             url_object = null;
  174.             favicon = samfind_modoptionsdialog._favicon_service.getFaviconImageForPage(url_object);
  175.             if (favicon && favicon.spec && favicon.spec.length)
  176.             {
  177.                 favicon = favicon.spec;
  178.                 if (favicon.indexOf("moz-anno:favicon:") == 0)
  179.                 {
  180.                     favicon = favicon.substring("moz-anno:favicon:".length);
  181.                 }
  182.             }
  183.         }
  184.         catch (e)
  185.         {}
  186.         if (favicon != null && favicon != "chrome://mozapps/skin/places/defaultFavicon.png")
  187.         {
  188.             caller._website_icon_preview_1.src = favicon;
  189.             caller._website_icon.value = favicon;
  190.             caller._website_icon_preview_2.src = favicon;
  191.         }
  192.         else if (url_pre_path != null)
  193.         {
  194.             if (url_pre_path.lastIndexOf("/") != url_pre_path.length - 1)
  195.             {
  196.                 url_pre_path += "/";    
  197.             }
  198.             url_pre_path += "favicon.ico";
  199.             caller._website_icon.value = url_pre_path;
  200.             var img = caller._website_icon.ownerDocument.getElementById("validate-image");
  201.             img.onerror = samfind_modoptionsdialog.websiteImageOnError;
  202.             img.onload = samfind_modoptionsdialog.websiteImageOnLoad;
  203.             img.src = url_pre_path;
  204.         }
  205.         else
  206.         {
  207.             caller._website_icon_preview_1.src = "chrome://mozapps/skin/places/defaultFavicon.png";    
  208.             caller._website_icon.value = "chrome://mozapps/skin/places/defaultFavicon.png";
  209.             caller._website_icon_preview_2.src = "chrome://mozapps/skin/places/defaultFavicon.png";
  210.         }
  211.     },
  212.  
  213.     showWebsiteMore : function(target)
  214.     {
  215.         var win = target.ownerDocument.defaultView;
  216.         win.resizeBy(0, +102);
  217.         target.removeAttribute("onclick");
  218.         target.setAttribute("onclick", "samfind_modoptionsdialog.hideWebsiteMore(this);");
  219.         target.setAttribute("opened", true);
  220.         target.blur();
  221.         win.document.getElementById("website-icon-preview-1").hidden = true;
  222.         target.parentNode.nextSibling.hidden = false;
  223.         target.parentNode.nextSibling.nextSibling.hidden = false;
  224.         target.parentNode.nextSibling.nextSibling.nextSibling.hidden = false;
  225.         target.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.hidden = false;
  226.     },
  227.  
  228.     hideWebsiteMore : function(target)
  229.     {
  230.         target.removeAttribute("onclick");
  231.         target.setAttribute("onclick", "samfind_modoptionsdialog.showWebsiteMore(this);");
  232.         target.removeAttribute("opened");
  233.         target.blur();
  234.         var win = target.ownerDocument.defaultView;
  235.         win.document.getElementById("website-icon-preview-1").hidden = false;
  236.         target.parentNode.nextSibling.hidden = true;
  237.         target.parentNode.nextSibling.nextSibling.hidden = true;
  238.         target.parentNode.nextSibling.nextSibling.nextSibling.hidden = true;
  239.         target.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.hidden = true;
  240.         win.resizeBy(0, -102);
  241.     },
  242.  
  243.     importCurrentWebsite : function(caller)
  244.     {
  245.         if (!caller._website_name)
  246.         {
  247.             return;    
  248.         }
  249.         //
  250.         var win = samfind_modutils.getWindow();
  251.         var doc = win.content.document;
  252.         caller._website_name.value = doc.title;
  253.         var homeurl = win.content.location.href;
  254.         caller._website_homeurl.value = homeurl;
  255.         var rss_found = false;
  256.         var favicon_found = false;
  257.         try
  258.         {
  259.             var links = doc.getElementsByTagName("link");
  260.             for (var i=0; i<links.length; ++i)
  261.             {
  262.                 var link = links[i];
  263.                 if (link.hasAttribute("rel") == false)
  264.                 {
  265.                     continue;    
  266.                 }
  267.                 var rel = link.getAttribute("rel").toLowerCase();
  268.                 if (rel == "alternate")
  269.                 {
  270.                     if (!rss_found)
  271.                     {
  272.                         var type = link.getAttribute("type");
  273.                         if (type == "application/rss+xml" || type == "application/atom+xml")
  274.                         {
  275.                             var href = link.getAttribute("href");
  276.                             if (href != null)
  277.                             {
  278.                                 if (href.indexOf("http") == -1)
  279.                                 {
  280.                                     var base_uri = doc.baseURIObject;
  281.                                     if (href.charAt(0) == '/')
  282.                                     {
  283.                                         href = base_uri.prePath + href;
  284.                                     }
  285.                                     else
  286.                                     {
  287.                                         var spec =  base_uri.spec;
  288.                                         href = spec.substring(0, spec.lastIndexOf("/") + 1) + href;
  289.                                     }
  290.                                 }
  291.                                 if (href.indexOf("http") == 0)
  292.                                 {
  293.                                     caller._website_rssurl.value = href;
  294.                                     rss_found = true;
  295.                                 }
  296.                             }
  297.                         }
  298.                     }
  299.                 }
  300.                 else if (rel == "icon" || rel == "shortcut icon")
  301.                 {
  302.                     var href = link.getAttribute("href");
  303.                     if (href.indexOf("http") == -1)
  304.                     {
  305.                         var base_uri = doc.baseURIObject;
  306.                         if (href.charAt(0) == '/')
  307.                         {
  308.                             href = base_uri.prePath + href;
  309.                         }
  310.                         else
  311.                         {
  312.                             var spec =  base_uri.spec;
  313.                             href = spec.substring(0, spec.lastIndexOf("/") + 1) + href;
  314.                         }
  315.                     }
  316.                     caller._website_icon_preview_1.src = href;
  317.                     caller._website_icon.value = href;
  318.                     caller._website_icon_preview_2.src = href;
  319.                     favicon_found = true;
  320.                 }
  321.             }
  322.         }
  323.         catch (e)
  324.         {}
  325.         //
  326.         if (!favicon_found)
  327.         {
  328.             samfind_modoptionsdialog.getFavicon(homeurl, caller);
  329.         }
  330.     },
  331.  
  332.     //-------------------------------------------------------------------------------------------
  333.     //
  334.     //-------------------------------------------------------------------------------------------
  335.  
  336.     showIconPicker : function(win, target)
  337.     {
  338.         var params = {out : null};
  339.         win.openDialog("chrome://samfind/content/options/icon_picker.xul",
  340.                        "samfind-icon-picker",
  341.                        "chrome,modal,resizable=no,titlebar=no",
  342.                        params);
  343.         if (params.out)
  344.         {
  345.             if (target == "topic")
  346.             {
  347.                 win.document.getElementById("topic-icon").value = params.out;
  348.                 win.document.getElementById("topic-icon-preview").src = params.out;
  349.             }
  350.             else
  351.             {
  352.                 win.document.getElementById("website-icon-preview-1").src = params.out;
  353.                 win.document.getElementById("website-icon").value = params.out;
  354.                 win.document.getElementById("website-icon-preview-2").src = params.out;
  355.             }
  356.         }
  357.     },
  358.  
  359.     //-------------------------------------------------------------------------------------------
  360.     //
  361.     //-------------------------------------------------------------------------------------------
  362.  
  363.     checkRequired : function(win)
  364.     {
  365.         var textboxes = win.document.documentElement.getElementsByAttribute("required", "true");
  366.         for (var i=0; i<textboxes.length; ++i)
  367.         {
  368.             if (samfind_modoptionsdialog._checkRequired(textboxes[i], win) == false)
  369.             {
  370.                 return false;    
  371.             }
  372.         }
  373.         return true;
  374.     },
  375.  
  376.     _checkRequired : function(textbox, win)
  377.     {
  378.         var value = samfind_modutils.trim(textbox.value);
  379.         if (!value.length)
  380.         {
  381.             textbox.value = "Required";
  382.             textbox.style.color = "red";
  383.             win.setTimeout(function()
  384.                 {
  385.                     if (textbox.value == "Required")
  386.                     {
  387.                         textbox.value = "";
  388.                     }
  389.                     textbox.style.color = "black";
  390.                     textbox.focus();
  391.                 },
  392.                 1000);
  393.             return false;
  394.         }
  395.         return true;
  396.     },
  397.  
  398.     checkURL : function(win)
  399.     {
  400.         var textboxes = win.document.documentElement.getElementsByAttribute("isurl", "true");
  401.         for (var i=0; i<textboxes.length; ++i)
  402.         {
  403.             if (samfind_modoptionsdialog._checkURL(textboxes[i], win) == false)
  404.             {
  405.                 return false;    
  406.             }
  407.         }
  408.         return true;
  409.     },
  410.  
  411.     _checkURL : function(textbox, win)
  412.     {
  413.         var value = samfind_modutils.trim(textbox.value);
  414.         if (value.length)
  415.         {
  416.             if (value.indexOf("javascript:") == 0)
  417.             {
  418.                 return true;    
  419.             }
  420.             var regexp = /(chrome|file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  421.             if (regexp.test(value) == false)
  422.             {
  423.                 textbox.value = "Invalid URL";
  424.                 textbox.style.color = "red";
  425.                 win.setTimeout(function()
  426.                     {
  427.                         if (textbox.value == "Invalid URL")
  428.                         {
  429.                             textbox.value = value;
  430.                         }
  431.                         textbox.style.color = "black";
  432.                         textbox.focus();
  433.                     },
  434.                     1000);
  435.                 return false;
  436.             }
  437.         }
  438.         return true;
  439.     },
  440.  
  441.     checkSearchURL : function(win)
  442.     {
  443.         var textboxes = win.document.documentElement.getElementsByAttribute("issearchurl", "true");
  444.         for (var i=0; i<textboxes.length; ++i)
  445.         {
  446.             if (samfind_modoptionsdialog._checkSearchURL(textboxes[i], win) == false)
  447.             {
  448.                 return false;    
  449.             }
  450.         }
  451.         return true;
  452.     },
  453.  
  454.     _checkSearchURL : function(textbox, win)
  455.     {
  456.         var value = samfind_modutils.trim(textbox.value);
  457.         if (value.length)
  458.         {
  459.             value = value.replace("query1", "QUERY1");
  460.             if (value.indexOf("QUERY1") == -1)
  461.             {
  462.                 textbox.value = "Replace search parameter with QUERY1";
  463.                 textbox.style.color = "red";
  464.                 win.setTimeout(function()
  465.                     {
  466.                         if (textbox.value == "Replace search parameter with QUERY1")
  467.                         {
  468.                             textbox.value = value;
  469.                         }
  470.                         textbox.style.color = "black";
  471.                         textbox.focus();
  472.                     },
  473.                     2000);
  474.                 return false;
  475.             }
  476.             textbox.value = value;
  477.         }
  478.         return true;
  479.     }
  480. };
  481.  
  482. /**
  483.  * Constructor.
  484.  */
  485. (function() { this._init(); }).apply(samfind_modoptionsdialog);