Bug 1436491 - Move category related functions into calCategoryUtils.jsm - manual changes. r=MakeMyDay
authorPhilipp Kewisch <mozilla@kewis.ch>
Wed, 07 Feb 2018 20:34:21 +0100
changeset 31101 3de68de8bf72c03fefe20521619c0a78c07b07a5
parent 31100 afe12c1075598ecdaf16e2d8d17393e496b7ae8b
child 31102 79de47fa45edc4bb9dc0de474dc6bdb8e7cf2d6f
push id383
push userclokep@gmail.com
push dateMon, 07 May 2018 21:52:48 +0000
reviewersMakeMyDay
bugs1436491
Bug 1436491 - Move category related functions into calCategoryUtils.jsm - manual changes. r=MakeMyDay MozReview-Commit-ID: 2qKXZttBfo5
calendar/base/content/preferences/categories.js
calendar/base/modules/calCategoryUtils.jsm
calendar/base/modules/calUtils.jsm
calendar/base/modules/calUtilsCompat.jsm
calendar/base/modules/moz.build
calendar/base/src/calUtils.js
calendar/providers/gdata/modules/calUtilsShim.jsm
--- a/calendar/base/content/preferences/categories.js
+++ b/calendar/base/content/preferences/categories.js
@@ -48,23 +48,16 @@ var gCategoriesPane = {
         // If no categories are configured load a default set from properties file
         if (!categories) {
             categories = cal.setupDefaultCategories();
             document.getElementById("calendar.categories.names").value = categories;
         }
 
         gCategoryList = cal.categoriesStringToArray(categories);
 
-        // When categories is empty, split returns an array containing one empty
-        // string, rather than an empty array. This results in an empty listbox
-        // child with no corresponding category.
-        if (gCategoryList.length == 1 && !gCategoryList[0].length) {
-            gCategoryList.pop();
-        }
-
         this.updateCategoryList();
 
         this.mCategoryDialog = "chrome://calendar/content/preferences/editCategory.xul";
 
         // Workaround for Bug 1151440 - the HTML color picker won't work
         // in linux when opened from modal dialog
         this.mWinProp = "centerscreen, chrome, resizable=no";
         if (AppConstants.platform != "linux") {
new file mode 100644
--- /dev/null
+++ b/calendar/base/modules/calCategoryUtils.jsm
@@ -0,0 +1,90 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+ChromeUtils.import("resource://gre/modules/Preferences.jsm");
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "cal", "resource://calendar/modules/calUtils.jsm", "cal");
+
+this.EXPORTED_SYMBOLS = ["calcategory"]; /* exported calcategory */
+
+var calcategory = {
+    /**
+     * Sets up the default categories from the localized string
+     *
+     * @return      The default set of categories as a comma separated string.
+     */
+    setupDefaultCategories: function() {
+        // First, set up the category names
+        let categories = cal.calGetString("categories", "categories2");
+        Preferences.set("calendar.categories.names", categories);
+
+        // Now, initialize the category default colors
+        let categoryArray = calcategory.stringToArray(categories);
+        for (let category of categoryArray) {
+            let prefName = cal.view.formatStringForCSSRule(category);
+            Preferences.set("calendar.category.color." + prefName,
+                            cal.view.hashColor(category));
+        }
+
+        // Return the list of categories for further processing
+        return categories;
+    },
+
+    /**
+     * Get array of category names from preferences or locale default,
+     * unescaping any commas in each category name.
+     *
+     * @return                      array of category names
+     */
+    fromPrefs: function() {
+        let categories = Preferences.get("calendar.categories.names", null);
+
+        // If no categories are configured load a default set from properties file
+        if (!categories) {
+            categories = calcategory.setupDefaultCategories();
+        }
+        return calcategory.stringToArray(categories);
+    },
+
+    /**
+     * Convert categories string to list of category names.
+     *
+     * Stored categories may include escaped commas within a name. Split
+     * categories string at commas, but not at escaped commas (\,). Afterward,
+     * replace escaped commas (\,) with commas (,) in each name.
+     *
+     * @param aCategoriesPrefValue  string from "calendar.categories.names" pref,
+     *                                which may contain escaped commas (\,) in names.
+     * @return                      list of category names
+     */
+    stringToArray: function(aCategories) {
+        if (!aCategories) {
+            return [];
+        }
+        // \u001A is the unicode "SUBSTITUTE" character
+        let categories = aCategories.replace(/\\,/g, "\u001A").split(",")
+                                    .map(name => name.replace(/\u001A/g, ","));
+        if (categories.length == 1 && categories[0] == "") {
+            // Split will return an array with an empty element when splitting an
+            // empty string, correct this.
+            categories.pop();
+        }
+        return categories;
+    },
+
+    /**
+     * Convert array of category names to string.
+     *
+     * Category names may contain commas (,). Escape commas (\,) in each, then
+     * join them in comma separated string for storage.
+     *
+     * @param aSortedCategoriesArray    sorted array of category names, may
+     *                                    contain unescaped commas, which will
+     *                                    be escaped in combined string.
+     */
+    arrayToString: function(aSortedCategoriesArray) {
+        return aSortedCategoriesArray.map(cat => cat.replace(/,/g, "\\,")).join(",");
+    }
+};
--- a/calendar/base/modules/calUtils.jsm
+++ b/calendar/base/modules/calUtils.jsm
@@ -750,16 +750,17 @@ var cal = {
      */
     registerForShutdownCleanup: shutdownCleanup
 };
 
 // Sub-modules for calUtils
 XPCOMUtils.defineLazyModuleGetter(cal, "data", "resource://calendar/modules/calDataUtils.jsm", "caldata");
 XPCOMUtils.defineLazyModuleGetter(cal, "dtz", "resource://calendar/modules/calDateTimeUtils.jsm", "caldtz");
 XPCOMUtils.defineLazyModuleGetter(cal, "acl", "resource://calendar/modules/calACLUtils.jsm", "calacl");
+XPCOMUtils.defineLazyModuleGetter(cal, "category", "resource://calendar/modules/calCategoryUtils.jsm", "calcategory");
 XPCOMUtils.defineLazyModuleGetter(cal, "item", "resource://calendar/modules/calItemUtils.jsm", "calitem");
 XPCOMUtils.defineLazyModuleGetter(cal, "view", "resource://calendar/modules/calViewUtils.jsm", "calview");
 XPCOMUtils.defineLazyModuleGetter(cal, "window", "resource://calendar/modules/calWindowUtils.jsm", "calwindow");
 
 /**
  * Returns a function that provides access to the given service.
  *
  * @param cid           The contract id to create
--- a/calendar/base/modules/calUtilsCompat.jsm
+++ b/calendar/base/modules/calUtilsCompat.jsm
@@ -18,16 +18,22 @@ this.EXPORTED_SYMBOLS = ["injectCalUtils
  */
 var migrations = {
     acl: {
         isCalendarWritable: "isCalendarWritable",
         userCanAddItemsToCalendar: "userCanAddItemsToCalendar",
         userCanDeleteItemsFromCalendar: "userCanDeleteItemsFromCalendar",
         userCanModifyItem: "userCanModifyItem"
     },
+    category: {
+        setupDefaultCategories: "setupDefaultCategories",
+        getPrefCategoriesArray: "fromPrefs",
+        categoriesStringToArray: "stringToArray",
+        categoriesArrayToString: "arrayToString"
+    },
     data: {
         binarySearch: "binarySearch",
         binaryInsertNode: "binaryInsertNode",
         binaryInsert: "binaryInsert",
         compareObjects: "compareObjects"
     },
     dtz: {
         now: "now",
--- a/calendar/base/modules/moz.build
+++ b/calendar/base/modules/moz.build
@@ -3,16 +3,17 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXTRA_JS_MODULES += [
     'calACLUtils.jsm',
     'calAlarmUtils.jsm',
     'calAsyncUtils.jsm',
     'calAuthUtils.jsm',
+    'calCategoryUtils.jsm',
     'calDataUtils.jsm',
     'calDateTimeUtils.jsm',
     'calExtract.jsm',
     'calHashedArray.jsm',
     'calItemUtils.jsm',
     'calIteratorUtils.jsm',
     'calItipUtils.jsm',
     'calPrintUtils.jsm',
--- a/calendar/base/src/calUtils.js
+++ b/calendar/base/src/calUtils.js
@@ -3,22 +3,19 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* This file contains commonly used functions in a centralized place so that
  * various components (and other js scopes) don't need to replicate them. Note
  * that loading this file twice in the same scope will throw errors.
  */
 
 /* exported getCalendarDirectory, attendeeMatchesAddresses,
- *          calRadioGroupSelectItem, getPrefCategoriesArray,
- *          setPrefCategoriesFromArray, calTryWrappedJSObject, LOG, WARN,
- *          ERROR, showError, getContrastingTextColor, sendMailTo,
- *          applyAttributeToMenuChildren, isPropertyValueSame,
- *          getParentNodeOrThis, getParentNodeOrThisByAttribute,
- *          calIterateEmailIdentities, getCompositeCalendar
+ *          calTryWrappedJSObject, compareObjects, LOG, WARN, ERROR, showError,
+ *          sendMailTo, applyAttributeToMenuChildren, isPropertyValueSame,
+ *          calIterateEmailIdentities, calGetString
  */
 
 ChromeUtils.import("resource:///modules/mailServices.js");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/Preferences.jsm");
 ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 
@@ -74,101 +71,16 @@ function attendeeMatchesAddresses(anAtte
     return false;
 }
 
 /**
  * Other functions
  */
 
 /**
- * Get array of category names from preferences or locale default,
- * unescaping any commas in each category name.
- * @return array of category names
- */
-function getPrefCategoriesArray() {
-    let categories = Preferences.get("calendar.categories.names", null);
-
-    // If no categories are configured load a default set from properties file
-    if (!categories) {
-        categories = setupDefaultCategories();
-    }
-    return categoriesStringToArray(categories);
-}
-
-/**
- * Sets up the default categories from the localized string
- *
- * @return      The default set of categories as a comma separated string.
- */
-function setupDefaultCategories() {
-    // First, set up the category names
-    let categories = calGetString("categories", "categories2");
-    Preferences.set("calendar.categories.names", categories);
-
-    // Now, initialize the category default colors
-    let categoryArray = categoriesStringToArray(categories);
-    for (let category of categoryArray) {
-        let prefName = cal.view.formatStringForCSSRule(category);
-        Preferences.set("calendar.category.color." + prefName,
-                        cal.view.hashColor(category));
-    }
-
-    // Return the list of categories for further processing
-    return categories;
-}
-
-/**
- * Convert categories string to list of category names.
- *
- * Stored categories may include escaped commas within a name.
- * Split categories string at commas, but not at escaped commas (\,).
- * Afterward, replace escaped commas (\,) with commas (,) in each name.
- * @param aCategoriesPrefValue string from "calendar.categories.names" pref,
- * which may contain escaped commas (\,) in names.
- * @return list of category names
- */
-function categoriesStringToArray(aCategories) {
-    if (!aCategories) {
-        return [];
-    }
-    // \u001A is the unicode "SUBSTITUTE" character
-    function revertCommas(name) { return name.replace(/\u001A/g, ","); }
-    let categories = aCategories.replace(/\\,/g, "\u001A").split(",").map(revertCommas);
-    if (categories.length == 1 && categories[0] == "") {
-        // Split will return an array with an empty element when splitting an
-        // empty string, correct this.
-        categories.pop();
-    }
-    return categories;
-}
-
-/**
- * Set categories preference, escaping any commas in category names.
- * @param aCategoriesArray array of category names,
- * may contain unescaped commas which will be escaped in combined pref.
- */
-function setPrefCategoriesFromArray(aCategoriesArray) {
-    Preferences.set("calendar.categories.names",
-                     categoriesArrayToString(aCategoriesList));
-}
-
-/**
- * Convert array of category names to string.
- *
- * Category names may contain commas (,).  Escape commas (\,) in each,
- * then join them in comma separated string for storage.
- * @param aSortedCategoriesArray sorted array of category names,
- * may contain unescaped commas, which will be escaped in combined string.
- */
-function categoriesArrayToString(aSortedCategoriesArray) {
-    function escapeComma(category) { return category.replace(/,/g, "\\,"); }
-    return aSortedCategoriesArray.map(escapeComma).join(",");
-}
-
-/**
  * Gets the value of a string in a .properties file from the calendar bundle
  *
  * @param aBundleName  the name of the properties file.  It is assumed that the
  *                     file lives in chrome://calendar/locale/
  * @param aStringName  the name of the string within the properties file
  * @param aParams      optional array of parameters to format the string
  * @param aComponent   optional stringbundle component name
  */
--- a/calendar/providers/gdata/modules/calUtilsShim.jsm
+++ b/calendar/providers/gdata/modules/calUtilsShim.jsm
@@ -67,8 +67,15 @@ if (!cal.view || !cal.view.hashColor) {
 
 if (typeof cal.window == "undefined") {
     cal.window = {
         getCalendarWindow: function() {
             return cal.getCalendarWindow();
         }
     };
 }
+
+if (typeof cal.category == "undefined") {
+    cal.category = {
+        stringToArray: function(aStr) { return cal.categoriesStringToArray(aStr); },
+        arrayToString: function(aArr) { return cal.categoriesArrayToString(aArr); }
+    };
+}