summaryrefslogtreecommitdiffstats
path: root/mailnews/base/search/src/nsMsgTraitService.js
blob: eda20bbf8b37c48a0167d2b09ffe00bd85b46e5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* 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/. */

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

// local static variables

var _lastIndex = 0;  // the first index will be one
var _traits = {};

var traitsBranch = Services.prefs.getBranch("mailnews.traits.");

function _registerTrait(aId, aIndex)
{
  var trait = {};
  trait.enabled = false;
  trait.name = "";
  trait.antiId = "";
  trait.index = aIndex;
  _traits[aId] = trait;
  return;
}

function nsMsgTraitService() {}

nsMsgTraitService.prototype =
{
  // Component setup
  classID: Components.ID("{A2E95F4F-DA72-4a41-9493-661AD353C00A}"),

  QueryInterface: XPCOMUtils.generateQI([
      Components.interfaces.nsIMsgTraitService]),

  // nsIMsgTraitService implementation

  get lastIndex()
  {
    return _lastIndex;
  },

  registerTrait: function(aId)
  {
    if (_traits[aId])
      return 0;  // meaning already registered
    _registerTrait(aId, ++_lastIndex);
    traitsBranch.setBoolPref("enabled." + _lastIndex, false);
    traitsBranch.setCharPref("id." + _lastIndex, aId);
    return _lastIndex;
  },

  unRegisterTrait: function(aId)
  {
    if (_traits[aId])
    {
      var index = _traits[aId].index;
      _traits[aId] = null;
      traitsBranch.clearUserPref("id." + index);
      traitsBranch.clearUserPref("enabled." + index);
      traitsBranch.clearUserPref("antiId." + index);
      traitsBranch.clearUserPref("name." + index);
    }
    return;
  },

  isRegistered: function(aId)
  {
    return _traits[aId] ? true : false;
  },

  setName: function(aId, aName)
  {
    traitsBranch.setCharPref("name." + _traits[aId].index, aName);
    _traits[aId].name = aName;
  },

  getName: function(aId)
  {
    return _traits[aId].name;
  },

  getIndex: function(aId)
  {
    return _traits[aId].index;
  },

  getId: function(aIndex)
  {
    for (let id in _traits)
      if (_traits[id].index == aIndex)
        return id;
    return null;
  },

  setEnabled: function(aId, aEnabled)
  {
    traitsBranch.setBoolPref("enabled." + _traits[aId].index, aEnabled);
    _traits[aId].enabled = aEnabled;
  },

  getEnabled: function(aId)
  {
    return _traits[aId].enabled;
  },

  setAntiId: function(aId, aAntiId)
  {
    traitsBranch.setCharPref("antiId." + _traits[aId].index, aAntiId);
    _traits[aId].antiId = aAntiId;
  },

  getAntiId: function(aId)
  {
    return _traits[aId].antiId;
  },

  getEnabledIndices: function(aCount, aProIndices, aAntiIndices)
  {
    let proIndices = [];
    let antiIndices = [];
    for (let id in _traits)
      if (_traits[id].enabled)
      {
        proIndices.push(_traits[id].index);
        antiIndices.push(_traits[_traits[id].antiId].index);
      }
    aCount.value = proIndices.length;
    aProIndices.value = proIndices;
    aAntiIndices.value = antiIndices;
    return;
  },

  addAlias: function addAlias(aTraitIndex, aTraitAliasIndex)
  {
    let aliasesString = "";
    try {
      aliasesString = traitsBranch.getCharPref("aliases." + aTraitIndex);
    }
    catch (e) {}
    let aliases;
    if (aliasesString.length)
      aliases = aliasesString.split(",");
    else
      aliases = [];
    if (aliases.indexOf(aTraitAliasIndex.toString()) == -1)
    {
      aliases.push(aTraitAliasIndex);
      traitsBranch.setCharPref("aliases." + aTraitIndex, aliases.join());
    }
  },

  removeAlias: function removeAlias(aTraitIndex, aTraitAliasIndex)
  {
    let aliasesString = "";
    try {
      aliasesString = traitsBranch.getCharPref("aliases." + aTraitIndex);
    }
    catch (e) {
      return;
    }
    let aliases;
    if (aliasesString.length)
      aliases = aliasesString.split(",");
    else
      aliases = [];
    let location;
    if ((location = aliases.indexOf(aTraitAliasIndex.toString())) != -1)
    {
      aliases.splice(location, 1);
      traitsBranch.setCharPref("aliases." + aTraitIndex, aliases.join());
    }
  },

  getAliases: function getAliases(aTraitIndex, aLength)
  {
    let aliasesString = "";
    try {
      aliasesString = traitsBranch.getCharPref("aliases." + aTraitIndex);
    }
    catch (e) {}

    let aliases;
    if (aliasesString.length)
      aliases = aliasesString.split(",");
    else
      aliases = [];
    aLength.value = aliases.length;
    return aliases;
  },
};

// initialization

_init();

function _init()
{
  // get existing traits
  var idBranch = Services.prefs.getBranch("mailnews.traits.id.");
  var nameBranch = Services.prefs.getBranch("mailnews.traits.name.");
  var enabledBranch = Services.prefs.getBranch("mailnews.traits.enabled.");
  var antiIdBranch = Services.prefs.getBranch("mailnews.traits.antiId.");
  _lastIndex = Services.prefs.getBranch("mailnews.traits.").getIntPref("lastIndex");
  var ids = idBranch.getChildList("");
  for (var i = 0; i < ids.length; i++)
  {
    var id = idBranch.getCharPref(ids[i]);
    var index = parseInt(ids[i]);
    _registerTrait(id, index, false);

    // Read in values, ignore errors since that usually means the
    // value does not exist
    try {
      _traits[id].name = nameBranch.getCharPref(ids[i]);
    }
    catch (e) {}

    try {
      _traits[id].enabled = enabledBranch.getBoolPref(ids[i]);
    }
    catch (e) {}

    try {
      _traits[id].antiId = antiIdBranch.getCharPref(ids[i]);
    }
    catch (e) {}

    if (_lastIndex < index)
      _lastIndex = index;
  }

  //for (traitId in _traits)
  //  dump("\nindex of " + traitId + " is " + _traits[traitId].index);
  //dump("\n");
}

var components = [nsMsgTraitService];
var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);