home *** CD-ROM | disk | FTP | other *** search
- // Copyright (C) 1996, 1997 Netscape Communications Corporation, All rights reserved.
-
- // --- Preference objects ---
- function preference(default_value) {
- this.default_value = default_value;
- this.locked = false;
- this.changed = false; // Admin Kit extra
- this.is_leaf = true;
- };
-
- function pref_node () {
- this.is_leaf = false;
- };
-
- // --- Preference initialization functions ---
- // -- Pass a string and value to a pref function, e.g.
- // user_pref("browser.startup.homepage", "abc");
- // -- It parses the string and defines branches of the prefs
- // object as necessary.
- // -- Note assumptions:
- // Root of the tree is prefs.
- // Every branch is a pref_node and leaves are preference objects.
- //
- function next_field(str, index)
- {
- var i = str.indexOf('.', index);
- if (i == -1)
- i = str.length;
- return str.substring(index, i)
- }
-
- function init_prefnode(prefix, name)
- {
- if (typeof(prefix[name]) == "undefined") {
- prefix[name] = new pref_node();
- }
- }
-
- function init_prefleaf(prefix, name)
- {
- if (typeof(prefix[name]) == "undefined")
- prefix[name] = new preference(null);
- }
-
- // creates the full path to prefname under prefs and
- // returns the the preference object named by prefname
- function init_preftree(prefname)
- {
- var index = 0;
- var prefix = prefs;
- while (index < prefname.length)
- {
- field = next_field(prefname, index);
- index = index + field.length + 1;
-
- if (index >= prefname.length) {
- init_prefleaf(prefix, field);
- }
- else {
- init_prefnode(prefix, field);
- }
- prefix = prefix[field];
- }
- return prefix;
- }
-
- // all.js will be calling this
- function pref(prefname, value)
- {
- var pref = init_preftree(prefname);
- pref.default_value = value;
- pref.changed = false;
- }
-
- // configURL files generated by the config editor (or otherwise)
- // will be calling the following two functions
- function default_pref(prefname, value)
- {
- var pref = init_preftree(prefname);
-
- pref.default_value = value;
- pref.changed = true;
- return pref;
- }
-
- function defaultPref(prefname, value)
- {
- return (default_pref(prefname, value));
- }
-
- function lock_pref(prefname, value)
- {
- // Set default pref and lock it
- var pref = init_preftree(prefname);
- pref.default_value = value;
- pref.locked = true;
- pref.changed = true;
- }
-
- function lockPref(prefname, value)
- {
- lock_pref(prefname, value);
- }
-
- // the config editor itself will be calling the following two functions
- // to maintain it's own run-time preference data structure which the
- // user will be interacting with.
- function set_pref(prefname, value, locked)
- {
- var pref = init_preftree(prefname);
-
- if ((pref.default_value != value) || (pref.locked != locked))
- pref.changed = true;
- pref.default_value = value;
- pref.locked = locked;
- }
-
- // the config editor itself will be calling the following function
- // to maintain wipe out pref setting and turn of the "changed" flag.
- function clear_pref(prefname)
- {
- var pref = init_preftree(prefname);
-
- pref.changed = false;
- pref.default_value = null;
- pref.locked = null;
- }
-
- // returns null if the pref doesn't exist
- function get_pref(prefname)
- {
- var index = 0;
- var prefix = prefs;
- while (index < prefname.length)
- {
- field = next_field(prefname, index);
- index = index + field.length + 1;
- if (typeof(prefix[field]) == "undefined")
- return null;
- prefix = prefix[field];
- }
- return prefix;
- }
-
- // This is like the func in prefapi, to escape \ and "
- function str_escape(str)
- {
- var outstr = "";
- var next = 0;
- while (next < str.length) {
- var c1 = str.indexOf('\\', next);
- var c2 = str.indexOf('\"', next);
- if (c1 == -1 && c2 == -1) {
- outstr += str.substring(next, str.length);
- next = str.length;
- }
- else {
- var start = next;
- if (c1 == -1)
- next = c2;
- else if (c2 == -1)
- next = c1;
- else
- next = Math.min(c1, c2);
- outstr += str.substring(start, next) + "\\" + str.charAt(next);
- next++;
- }
- }
- return outstr;
- }
-
- // does a depth-first traversal of a preference structure and
- // returns the JS for a configURL file that will set all the same
- // settings that have been changed by the user in this preference structure.
- function save_uservals(obj, obj_name)
- {
- var result = "";
-
- if (obj==null) return result;
-
- // if it's a leaf,a pref, print it out
- if (obj.is_leaf == true)
- {
- if ((obj.default_value != null) && (obj.changed == true))
- {
- if (obj.locked)
- result += "lockPref(\"" + obj_name + "\", ";
- else
- result += "defaultPref(\"" + obj_name + "\", ";
-
- if (typeof(obj.default_value) == "string")
- result += "\"" + str_escape(obj.default_value) + "\");" + top.EOL;
- else
- // number or boolean
- result += obj.default_value + ");" + top.EOL;
- }
- }
- else
- // if it's a node traverse all it's children
- {
- for (var key in obj)
- {
- if (key != "is_leaf")
- {
- if (obj_name.length == 0)
- result += save_uservals(obj[key], key);
- else
- result += save_uservals(obj[key], obj_name + "." + key);
- }
- }
- }
- return result;
- }
-
- // -- These functions are called from within prefapi.c
- // -- Returns true iff the pref's value has changed.
- // (Set creates the pref if necessary; Get assumes it already exists)
- function SetUserPref(prefname, value)
- {
- var pref = init_preftree(prefname);
-
- if (pref.user_value != value && !pref.locked) {
- if (pref.default_value == value)
- pref.user_value = null;
- else
- pref.user_value = value;
- return true;
- }
- return false;
- }
-
- function GetUserPref(obj)
- {
- if (obj.locked || obj.user_value == null) {
- return obj.default_value;
- }
- else {
- return obj.user_value;
- }
- }
-
- function SetDefaultUserPref(prefname, value)
- {
- var pref = init_preftree(prefname);
-
- if (pref.default_value != value) {
- pref.default_value = value;
- return true;
- }
- return false;
- }
-
-
- function mime_type()
- {}
-
- // ----------------------------------------------------------------------
-
- // --- Config objects ---
- function configsetting (value) {
- this.value = value;
- this.is_leaf = true;
- this.changed = false;
- };
-
- function config_array() {
- this.is_leaf = false;
- };
-
- // --- Config initialization functions ---
- // -- Pass a string and value to a config function, e.g.
- // config("directory.button1.label", "abc");
- // -- It parses the string and defines branches of the configs
- // object as necessary.
- // -- Note assumptions:
- // Root of the tree is configs.
- // Every branch is a config_array and leaves are configsetting objects.
-
- function init_configarray(prefix, name)
- {
- if (typeof(prefix[name]) == "undefined") {
- prefix[name] = new config_array(prefix);
- }
- }
-
- function init_config(prefix, name)
- {
- if (typeof(prefix[name]) == "undefined")
- prefix[name] = new configsetting(null);
- }
-
- // returns the config object named by configname
- function init_configtree(configname)
- {
- var index = 0;
- var prefix = configs;
- while (index < configname.length)
- {
- field = next_field(configname, index);
- index = index + field.length + 1;
-
- if (index >= configname.length) {
- init_config(prefix, field);
- }
- else {
- init_configarray(prefix, field);
- }
- prefix = prefix[field];
- }
- return prefix;
- }
-
- // This function will be called by defaults.js to load the factory defaults
- function default_config(configname, value)
- {
- var config = init_configtree(configname);
-
- config.value = value;
- config.changed = false;
- return config;
- }
-
- // This function will be called by configuration files that we open
- function config(configname, value)
- {
- var config = init_configtree(configname);
-
- config.value = value;
- config.changed = true;
- return config;
- }
-
-
- function set_config(configname, value)
- {
- var config = init_configtree(configname);
-
- if (config.value != value)
- {
- config.value = value;
- config.changed = true;
- }
- return config;
- }
-
-
- // Admin Kit: writes default values instead.
-
- function save_configs(obj, obj_name) {
- var result = "";
-
- if (obj==null) return result;
-
- // if it's a leaf, print it out
- if (obj.is_leaf == true)
- {
- if ((obj.value != null) && (obj.changed == true))
- {
- result += "config(\"" + obj_name + "\", ";
-
- if (typeof(obj.value) == "string")
- result += "\"" + str_escape(obj.value) + "\");"+top.EOL;
- else
- // number or boolean
- result += obj.value + ");"+top.EOL;
- }
- }
- else
- // if it's a node traverse all it's children
- {
- for (var key in obj)
- {
- if (key != "is_leaf")
- {
- if (obj_name.length == 0)
- result += save_configs(obj[key], key);
- else
- result += save_configs(obj[key], obj_name + "." + key);
- }
- }
- }
- return result;
- }
-
-