summaryrefslogtreecommitdiffstats
path: root/comm/mail/modules/MailViewManager.jsm
blob: f81221e60975d36f5c0ba7a6083b349dabf3072c (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
/* 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/. */

const EXPORTED_SYMBOLS = ["MailViewManager", "MailViewConstants"];

/**
 * Put the MailViewConstants in an object so we can export them to
 *  msgViewPickerOverlay in one blob without contaminating everyone's address
 *  space who might want to import us.
 */
var MailViewConstants = {
  // tag views have kViewTagMarker + their key as value
  kViewItemAll: 0,
  kViewItemUnread: 1,
  kViewItemTags: 2, // former labels used values 2-6
  kViewItemNotDeleted: 3,
  // not a real view! a sentinel value to pop up a dialog
  kViewItemVirtual: 7,
  // not a real view! a sentinel value to pop up a dialog
  kViewItemCustomize: 8,
  kViewItemFirstCustom: 9,

  kViewCurrent: "current-view",
  kViewCurrentTag: "current-view-tag",
  kViewTagMarker: ":",
};

/**
 * MailViews are view 'filters' implemented using search terms.  DBViewWrapper
 *  uses the SearchSpec class to combine the search terms of the mailview with
 *  those of the virtual folder (if applicable) and the quicksearch (if
 *  applicable).
 */
var MailViewManager = {
  _views: {},
  _customMailViews: Cc["@mozilla.org/messenger/mailviewlist;1"].getService(
    Ci.nsIMsgMailViewList
  ),

  /**
   * Define one of the built-in mail-views.  If you want to define your own
   *  view, you need to define a custom view using nsIMsgMailViewList.
   *
   * We define our own little view definition abstraction because some day this
   *  functionality may want to be generalized to be usable by gloda as well.
   *
   * @param aViewDef The view definition, three attributes are required:
   * - name: A string name for the view, for debugging purposes only.  This
   *         should not be localized!
   * - index: The index to assign to the view.
   * - makeTerms: A function to invoke that returns a list of search terms.
   */
  defineView(aViewDef) {
    this._views[aViewDef.index] = aViewDef;
  },

  /**
   * Wrap a custom view into our cute little view abstraction.  We do not cache
   *  these because views should not change often enough for it to matter from
   *  a performance perspective, but they will change enough to make stale
   *  caches a potential issue.
   */
  _wrapCustomView(aCustomViewIndex) {
    let mailView = this._customMailViews.getMailViewAt(aCustomViewIndex);
    return {
      name: mailView.prettyName, // since the user created it it's localized
      index: aCustomViewIndex,
      makeTerms(aSession, aData) {
        return mailView.searchTerms;
      },
    };
  },

  _findCustomViewByName(aName) {
    let count = this._customMailViews.mailViewCount;
    for (let i = 0; i < count; i++) {
      let mailView = this._customMailViews.getMailViewAt(i);
      if (mailView.mailViewName == aName) {
        return this._wrapCustomView(i);
      }
    }
    throw new Error("No custom view with name: " + aName);
  },

  /**
   * Return the view definition associated with the given view index.
   *
   * @param aViewIndex If the value is an integer it references the built-in
   *      view with the view index from MailViewConstants, or if the index
   *      is >= MailViewConstants.kViewItemFirstCustom, it is a reference to
   *      a custom view definition.  If the value is a string, it is the name
   *      of a custom view.  The string case is mainly intended for testing
   *      purposes.
   */
  getMailViewByIndex(aViewIndex) {
    if (typeof aViewIndex == "string") {
      return this._findCustomViewByName(aViewIndex);
    }
    if (aViewIndex < MailViewConstants.kViewItemFirstCustom) {
      return this._views[aViewIndex];
    }
    return this._wrapCustomView(
      aViewIndex - MailViewConstants.kViewItemFirstCustom
    );
  },
};

MailViewManager.defineView({
  name: "all mail", // debugging assistance only! not localized!
  index: MailViewConstants.kViewItemAll,
  makeTerms(aSession, aData) {
    return null;
  },
});

MailViewManager.defineView({
  name: "new mail / unread", // debugging assistance only! not localized!
  index: MailViewConstants.kViewItemUnread,
  makeTerms(aSession, aData) {
    let term = aSession.createTerm();
    let value = term.value;

    value.status = Ci.nsMsgMessageFlags.Read;
    value.attrib = Ci.nsMsgSearchAttrib.MsgStatus;
    term.value = value;
    term.attrib = Ci.nsMsgSearchAttrib.MsgStatus;
    term.op = Ci.nsMsgSearchOp.Isnt;
    term.booleanAnd = true;

    return [term];
  },
});

MailViewManager.defineView({
  name: "tags", // debugging assistance only! not localized!
  index: MailViewConstants.kViewItemTags,
  makeTerms(aSession, aKeyword) {
    let term = aSession.createTerm();
    let value = term.value;

    value.str = aKeyword;
    value.attrib = Ci.nsMsgSearchAttrib.Keywords;
    term.value = value;
    term.attrib = Ci.nsMsgSearchAttrib.Keywords;
    term.op = Ci.nsMsgSearchOp.Contains;
    term.booleanAnd = true;

    return [term];
  },
});

MailViewManager.defineView({
  name: "not deleted", // debugging assistance only! not localized!
  index: MailViewConstants.kViewItemNotDeleted,
  makeTerms(aSession, aKeyword) {
    let term = aSession.createTerm();
    let value = term.value;

    value.status = Ci.nsMsgMessageFlags.IMAPDeleted;
    value.attrib = Ci.nsMsgSearchAttrib.MsgStatus;
    term.value = value;
    term.attrib = Ci.nsMsgSearchAttrib.MsgStatus;
    term.op = Ci.nsMsgSearchOp.Isnt;
    term.booleanAnd = true;

    return [term];
  },
});