| Index: lib/utils.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/lib/utils.js |
| @@ -0,0 +1,82 @@ |
| +var Utils = exports.Utils = { |
| + systemPrincipal: null, |
| + getString: function(id) |
| + { |
| + return id; |
| + }, |
| + runAsync: function(callback, thisPtr) |
| + { |
| + var params = Array.prototype.slice.call(arguments, 2); |
| + window.setTimeout(function() |
| + { |
| + callback.apply(thisPtr, params); |
| + }, 0); |
| + }, |
| + get appLocale() |
| + { |
| + var locale = chrome.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); |
| + this.__defineGetter__("appLocale", function() {return locale}); |
| + return this.appLocale; |
| + }, |
| + generateChecksum: function(lines) |
| + { |
| + // We cannot calculate MD5 checksums yet :-( |
| + return null; |
| + }, |
| + makeURI: function(url) |
| + { |
| + return Services.io.newURI(url); |
| + }, |
| + |
| + checkLocalePrefixMatch: function(prefixes) |
| + { |
| + if (!prefixes) |
| + return null; |
| + |
| + var list = prefixes.split(","); |
| + for (var i = 0; i < list.length; i++) |
| + if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) |
| + return list[i]; |
| + |
| + return null; |
| + }, |
| + |
| + chooseFilterSubscription: function(subscriptions) |
| + { |
| + var selectedItem = null; |
| + var selectedPrefix = null; |
| + var matchCount = 0; |
| + for (var i = 0; i < subscriptions.length; i++) |
| + { |
| + var subscription = subscriptions[i]; |
| + if (!selectedItem) |
| + selectedItem = subscription; |
| + |
| + var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.getAttribute("prefixes")); |
| + if (prefix) |
| + { |
| + if (!selectedPrefix || selectedPrefix.length < prefix.length) |
| + { |
| + selectedItem = subscription; |
| + selectedPrefix = prefix; |
| + matchCount = 1; |
| + } |
| + else if (selectedPrefix && selectedPrefix.length == prefix.length) |
| + { |
| + matchCount++; |
| + |
| + // If multiple items have a matching prefix of the same length: |
| + // Select one of the items randomly, probability should be the same |
| + // for all items. So we replace the previous match here with |
| + // probability 1/N (N being the number of matches). |
| + if (Math.random() * matchCount < 1) |
| + { |
| + selectedItem = subscription; |
| + selectedPrefix = prefix; |
| + } |
| + } |
| + } |
| + } |
| + return selectedItem; |
| + } |
| +}; |