summaryrefslogtreecommitdiffstats
path: root/mailnews/db/gloda/modules/noun_tag.js
blob: 292bdad9bbd2ee2bc4b20ed1b0fbd42dd5c2501a (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
/* 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/. */

this.EXPORTED_SYMBOLS = ['TagNoun'];

Components.utils.import("resource:///modules/mailServices.js");

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cr = Components.results;
var Cu = Components.utils;

Cu.import("resource:///modules/gloda/gloda.js");

/**
 * @namespace Tag noun provider.
 */
var TagNoun = {
  name: "tag",
  clazz: Ci.nsIMsgTag,
  usesParameter: true,
  allowsArbitraryAttrs: false,
  idAttr: "key",
  _msgTagService: null,
  _tagMap: null,
  _tagList: null,

  _init: function () {
    this._msgTagService = MailServices.tags;
    this._updateTagMap();
  },

  getAllTags: function gloda_noun_tag_getAllTags() {
    if (this._tagList == null)
      this._updateTagMap();
    return this._tagList;
  },

  _updateTagMap: function gloda_noun_tag_updateTagMap() {
    this._tagMap = {};
    let tagArray = this._tagList = this._msgTagService.getAllTags({});
    for (let iTag = 0; iTag < tagArray.length; iTag++) {
      let tag = tagArray[iTag];
      this._tagMap[tag.key] = tag;
    }
  },

  comparator: function gloda_noun_tag_comparator(a, b) {
    if (a == null) {
      if (b == null)
        return 0;
      else
        return 1;
    }
    else if (b == null) {
      return -1;
    }
    return a.tag.localeCompare(b.tag);
  },
  userVisibleString: function gloda_noun_tag_userVisibleString(aTag) {
    return aTag.tag;
  },

  // we cannot be an attribute value

  toParamAndValue: function gloda_noun_tag_toParamAndValue(aTag) {
    return [aTag.key, null];
  },
  toJSON: function gloda_noun_tag_toJSON(aTag) {
    return aTag.key;
  },
  fromJSON: function gloda_noun_tag_fromJSON(aTagKey, aIgnored) {
    let tag = this._tagMap.hasOwnProperty(aTagKey) ? this._tagMap[aTagKey]
                : undefined;
    // you will note that if a tag is removed, we are unable to aggressively
    //  deal with this.  we are okay with this, but it would be nice to be able
    //  to listen to the message tag service to know when we should rebuild.
    if ((tag === undefined) && this._msgTagService.isValidKey(aTagKey)) {
      this._updateTagMap();
      tag = this._tagMap[aTagKey];
    }
    // we intentionally are returning undefined if the tag doesn't exist
    return tag;
  },
  /**
   * Convenience helper to turn a tag key into a tag name.
   */
  getTag: function gloda_noun_tag_getTag(aTagKey) {
    return this.fromJSON(aTagKey);
  }
};

TagNoun._init();
Gloda.defineNoun(TagNoun, Gloda.NOUN_TAG);