home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------------------------------
- // Options dialog module.
- //-------------------------------------------------------------------------------------------
-
- const Cc = Components.classes;
- const Ci = Components.interfaces;
-
- var EXPORTED_SYMBOLS = ["samfind_modoptionsdialog"];
-
- Components.utils.import("resource://samfind/samfind_modutils.jsm");
-
- var samfind_modoptionsdialog =
- {
- _io_service : null,
- _favicon_service : null,
-
- _init : function()
- {
- samfind_modoptionsdialog._io_service = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
- samfind_modoptionsdialog._favicon_service = Cc["@mozilla.org/browser/favicon-service;1"].getService(Ci.nsIFaviconService);
- },
-
- //-------------------------------------------------------------------------------------------
- //
- //-------------------------------------------------------------------------------------------
-
- updateTopicIconPreview : function(textbox)
- {
- var img = textbox.ownerDocument.getElementById("validate-image");
- img.onerror = samfind_modoptionsdialog.topicImageOnError;
- img.onload = samfind_modoptionsdialog.topicImageOnLoad;
- img.src = samfind_modutils.trim(textbox.value);
- },
-
- openTopicFP : function(win)
- {
- var nsIFilePicker = Ci.nsIFilePicker;
- var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
- fp.init(win, "samfind | Select a topic icon", nsIFilePicker.modeOpen);
- fp.appendFilters(nsIFilePicker.filterImages);
- var res = fp.show();
- if (res == nsIFilePicker.returnOK)
- {
- var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
- var url = ios.newFileURI(fp.file);
- var img = win.document.getElementById("validate-image");
- img.onerror = samfind_modoptionsdialog.topicImageOnError;
- img.onload = samfind_modoptionsdialog.topicImageOnLoad;
- img.src = url.spec;
- }
- fp = null;
- },
-
- topicImageOnError : function(event)
- {
- event.target.ownerDocument.getElementById("topic-icon-preview").src = "";
- },
-
- topicImageOnLoad : function(event)
- {
- var image = event.target;
- var document = image.ownerDocument;
- var textbox = document.getElementById("topic-icon");
- if (image.width <= 32 && image.height <= 32)
- {
- textbox.value = image.src;
- document.getElementById("topic-icon-preview").src = image.src;
- }
- else
- {
- var old_value = textbox.value;
- textbox.value = "Please upload images with maximum size 32x32 pixels";
- textbox.style.color = "red";
- document.defaultView.setTimeout(function()
- {
- if (textbox.value == "Please upload images with maximum size 32x32 pixels")
- {
- textbox.value = old_value;
- }
- textbox.style.color = "black";
- textbox.focus();
- },
- 2000);
- }
- return false;
- },
-
- //-------------------------------------------------------------------------------------------
- //
- //-------------------------------------------------------------------------------------------
-
- setWebsiteIcon : function(textbox, caller)
- {
- samfind_modoptionsdialog.getFavicon(textbox.value, caller);
- },
-
- updateWebsiteIconPreview : function(textbox)
- {
- var img = textbox.ownerDocument.getElementById("validate-image");
- img.onerror = samfind_modoptionsdialog.websiteImageOnError;
- img.onload = samfind_modoptionsdialog.websiteImageOnLoad;
- img.src = samfind_modutils.trim(textbox.value);
- },
-
- openWebsiteFP : function(win)
- {
- var nsIFilePicker = Ci.nsIFilePicker;
- var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
- fp.init(win, "samfind | Select a website icon", nsIFilePicker.modeOpen);
- fp.appendFilters(nsIFilePicker.filterImages);
- var res = fp.show();
- if (res == nsIFilePicker.returnOK)
- {
- var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
- var url = ios.newFileURI(fp.file);
- var img = win.document.getElementById("validate-image");
- img.onerror = samfind_modoptionsdialog.websiteImageOnError;
- img.onload = samfind_modoptionsdialog.websiteImageOnLoad;
- img.src = url.spec;
- }
- fp = null;
- },
-
- websiteImageOnError : function(event)
- {
- var document = event.target.ownerDocument;
- document.getElementById("website-icon-preview-1").src = "chrome://mozapps/skin/places/defaultFavicon.png";
- document.getElementById("website-icon-preview-2").src = "chrome://mozapps/skin/places/defaultFavicon.png";
- },
-
- websiteImageOnLoad : function(event)
- {
- var image = event.target;
- var document = image.ownerDocument;
- var textbox = document.getElementById("website-icon");
- if (image.width <= 32 && image.height <= 32)
- {
- textbox.value = image.src;
- document.getElementById("website-icon-preview-1").src = image.src;
- document.getElementById("website-icon-preview-2").src = image.src;
- }
- else
- {
- var old_value = textbox.value;
- textbox.value = "Please upload images with maximum size 32x32 pixels";
- textbox.style.color = "red";
- document.defaultView.setTimeout(function()
- {
- if (textbox.value == "Please upload images with maximum size 32x32 pixels")
- {
- textbox.value = old_value;
- }
- textbox.style.color = "black";
- textbox.focus();
- },
- 2000);
- }
- return false;
- },
-
- //-------------------------------------------------------------------------------------------
- //
- //-------------------------------------------------------------------------------------------
-
- getFavicon : function(url, caller)
- {
- var favicon = null;
- var url_pre_path = null;
- try
- {
- var url_object = samfind_modoptionsdialog._io_service.newURI(url, null, null);
- url_pre_path = url_object.prePath;
- url_object = null;
- favicon = samfind_modoptionsdialog._favicon_service.getFaviconImageForPage(url_object);
- if (favicon && favicon.spec && favicon.spec.length)
- {
- favicon = favicon.spec;
- if (favicon.indexOf("moz-anno:favicon:") == 0)
- {
- favicon = favicon.substring("moz-anno:favicon:".length);
- }
- }
- }
- catch (e)
- {}
- if (favicon != null && favicon != "chrome://mozapps/skin/places/defaultFavicon.png")
- {
- caller._website_icon_preview_1.src = favicon;
- caller._website_icon.value = favicon;
- caller._website_icon_preview_2.src = favicon;
- }
- else if (url_pre_path != null)
- {
- if (url_pre_path.lastIndexOf("/") != url_pre_path.length - 1)
- {
- url_pre_path += "/";
- }
- url_pre_path += "favicon.ico";
- caller._website_icon.value = url_pre_path;
- var img = caller._website_icon.ownerDocument.getElementById("validate-image");
- img.onerror = samfind_modoptionsdialog.websiteImageOnError;
- img.onload = samfind_modoptionsdialog.websiteImageOnLoad;
- img.src = url_pre_path;
- }
- else
- {
- caller._website_icon_preview_1.src = "chrome://mozapps/skin/places/defaultFavicon.png";
- caller._website_icon.value = "chrome://mozapps/skin/places/defaultFavicon.png";
- caller._website_icon_preview_2.src = "chrome://mozapps/skin/places/defaultFavicon.png";
- }
- },
-
- showWebsiteMore : function(target)
- {
- var win = target.ownerDocument.defaultView;
- win.resizeBy(0, +102);
- target.removeAttribute("onclick");
- target.setAttribute("onclick", "samfind_modoptionsdialog.hideWebsiteMore(this);");
- target.setAttribute("opened", true);
- target.blur();
- win.document.getElementById("website-icon-preview-1").hidden = true;
- target.parentNode.nextSibling.hidden = false;
- target.parentNode.nextSibling.nextSibling.hidden = false;
- target.parentNode.nextSibling.nextSibling.nextSibling.hidden = false;
- target.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.hidden = false;
- },
-
- hideWebsiteMore : function(target)
- {
- target.removeAttribute("onclick");
- target.setAttribute("onclick", "samfind_modoptionsdialog.showWebsiteMore(this);");
- target.removeAttribute("opened");
- target.blur();
- var win = target.ownerDocument.defaultView;
- win.document.getElementById("website-icon-preview-1").hidden = false;
- target.parentNode.nextSibling.hidden = true;
- target.parentNode.nextSibling.nextSibling.hidden = true;
- target.parentNode.nextSibling.nextSibling.nextSibling.hidden = true;
- target.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.hidden = true;
- win.resizeBy(0, -102);
- },
-
- importCurrentWebsite : function(caller)
- {
- if (!caller._website_name)
- {
- return;
- }
- //
- var win = samfind_modutils.getWindow();
- var doc = win.content.document;
- caller._website_name.value = doc.title;
- var homeurl = win.content.location.href;
- caller._website_homeurl.value = homeurl;
- var rss_found = false;
- var favicon_found = false;
- try
- {
- var links = doc.getElementsByTagName("link");
- for (var i=0; i<links.length; ++i)
- {
- var link = links[i];
- if (link.hasAttribute("rel") == false)
- {
- continue;
- }
- var rel = link.getAttribute("rel").toLowerCase();
- if (rel == "alternate")
- {
- if (!rss_found)
- {
- var type = link.getAttribute("type");
- if (type == "application/rss+xml" || type == "application/atom+xml")
- {
- var href = link.getAttribute("href");
- if (href != null)
- {
- if (href.indexOf("http") == -1)
- {
- var base_uri = doc.baseURIObject;
- if (href.charAt(0) == '/')
- {
- href = base_uri.prePath + href;
- }
- else
- {
- var spec = base_uri.spec;
- href = spec.substring(0, spec.lastIndexOf("/") + 1) + href;
- }
- }
- if (href.indexOf("http") == 0)
- {
- caller._website_rssurl.value = href;
- rss_found = true;
- }
- }
- }
- }
- }
- else if (rel == "icon" || rel == "shortcut icon")
- {
- var href = link.getAttribute("href");
- if (href.indexOf("http") == -1)
- {
- var base_uri = doc.baseURIObject;
- if (href.charAt(0) == '/')
- {
- href = base_uri.prePath + href;
- }
- else
- {
- var spec = base_uri.spec;
- href = spec.substring(0, spec.lastIndexOf("/") + 1) + href;
- }
- }
- caller._website_icon_preview_1.src = href;
- caller._website_icon.value = href;
- caller._website_icon_preview_2.src = href;
- favicon_found = true;
- }
- }
- }
- catch (e)
- {}
- //
- if (!favicon_found)
- {
- samfind_modoptionsdialog.getFavicon(homeurl, caller);
- }
- },
-
- //-------------------------------------------------------------------------------------------
- //
- //-------------------------------------------------------------------------------------------
-
- showIconPicker : function(win, target)
- {
- var params = {out : null};
- win.openDialog("chrome://samfind/content/options/icon_picker.xul",
- "samfind-icon-picker",
- "chrome,modal,resizable=no,titlebar=no",
- params);
- if (params.out)
- {
- if (target == "topic")
- {
- win.document.getElementById("topic-icon").value = params.out;
- win.document.getElementById("topic-icon-preview").src = params.out;
- }
- else
- {
- win.document.getElementById("website-icon-preview-1").src = params.out;
- win.document.getElementById("website-icon").value = params.out;
- win.document.getElementById("website-icon-preview-2").src = params.out;
- }
- }
- },
-
- //-------------------------------------------------------------------------------------------
- //
- //-------------------------------------------------------------------------------------------
-
- checkRequired : function(win)
- {
- var textboxes = win.document.documentElement.getElementsByAttribute("required", "true");
- for (var i=0; i<textboxes.length; ++i)
- {
- if (samfind_modoptionsdialog._checkRequired(textboxes[i], win) == false)
- {
- return false;
- }
- }
- return true;
- },
-
- _checkRequired : function(textbox, win)
- {
- var value = samfind_modutils.trim(textbox.value);
- if (!value.length)
- {
- textbox.value = "Required";
- textbox.style.color = "red";
- win.setTimeout(function()
- {
- if (textbox.value == "Required")
- {
- textbox.value = "";
- }
- textbox.style.color = "black";
- textbox.focus();
- },
- 1000);
- return false;
- }
- return true;
- },
-
- checkURL : function(win)
- {
- var textboxes = win.document.documentElement.getElementsByAttribute("isurl", "true");
- for (var i=0; i<textboxes.length; ++i)
- {
- if (samfind_modoptionsdialog._checkURL(textboxes[i], win) == false)
- {
- return false;
- }
- }
- return true;
- },
-
- _checkURL : function(textbox, win)
- {
- var value = samfind_modutils.trim(textbox.value);
- if (value.length)
- {
- if (value.indexOf("javascript:") == 0)
- {
- return true;
- }
- var regexp = /(chrome|file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
- if (regexp.test(value) == false)
- {
- textbox.value = "Invalid URL";
- textbox.style.color = "red";
- win.setTimeout(function()
- {
- if (textbox.value == "Invalid URL")
- {
- textbox.value = value;
- }
- textbox.style.color = "black";
- textbox.focus();
- },
- 1000);
- return false;
- }
- }
- return true;
- },
-
- checkSearchURL : function(win)
- {
- var textboxes = win.document.documentElement.getElementsByAttribute("issearchurl", "true");
- for (var i=0; i<textboxes.length; ++i)
- {
- if (samfind_modoptionsdialog._checkSearchURL(textboxes[i], win) == false)
- {
- return false;
- }
- }
- return true;
- },
-
- _checkSearchURL : function(textbox, win)
- {
- var value = samfind_modutils.trim(textbox.value);
- if (value.length)
- {
- value = value.replace("query1", "QUERY1");
- if (value.indexOf("QUERY1") == -1)
- {
- textbox.value = "Replace search parameter with QUERY1";
- textbox.style.color = "red";
- win.setTimeout(function()
- {
- if (textbox.value == "Replace search parameter with QUERY1")
- {
- textbox.value = value;
- }
- textbox.style.color = "black";
- textbox.focus();
- },
- 2000);
- return false;
- }
- textbox.value = value;
- }
- return true;
- }
- };
-
- /**
- * Constructor.
- */
- (function() { this._init(); }).apply(samfind_modoptionsdialog);