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
|
/* 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 file contains helper methods for dealing with nsIMsgFolders.
*/
this.EXPORTED_SYMBOLS = ["getFolderProperties", "getSpecialFolderString",
"getFolderFromUri", "allAccountsSorted",
"getMostRecentFolders", "folderNameCompare"];
Components.utils.import("resource:///modules/mailServices.js");
Components.utils.import("resource:///modules/iteratorUtils.jsm");
/**
* Returns a string representation of a folder's "special" type.
*
* @param aFolder the nsIMsgFolder whose special type should be returned
*/
function getSpecialFolderString(aFolder) {
const nsMsgFolderFlags = Components.interfaces.nsMsgFolderFlags;
let flags = aFolder.flags;
if (flags & nsMsgFolderFlags.Inbox)
return "Inbox";
if (flags & nsMsgFolderFlags.Trash)
return "Trash";
if (flags & nsMsgFolderFlags.Queue)
return "Outbox";
if (flags & nsMsgFolderFlags.SentMail)
return "Sent";
if (flags & nsMsgFolderFlags.Drafts)
return "Drafts";
if (flags & nsMsgFolderFlags.Templates)
return "Templates";
if (flags & nsMsgFolderFlags.Junk)
return "Junk";
if (flags & nsMsgFolderFlags.Archive)
return "Archive";
if (flags & nsMsgFolderFlags.Virtual)
return "Virtual";
return "none";
}
/**
* This function is meant to be used with trees. It returns the property list
* for all of the common properties that css styling is based off of.
*
* @param nsIMsgFolder aFolder the folder whose properties should be returned
* as a string
* @param bool aOpen true if the folder is open/expanded
*
* @return A string of the property names, delimited by space.
*/
function getFolderProperties(aFolder, aOpen) {
const nsIMsgFolder = Components.interfaces.nsIMsgFolder;
let properties = [];
properties.push("folderNameCol");
properties.push("serverType-" + aFolder.server.type);
// set the SpecialFolder attribute
properties.push("specialFolder-" + getSpecialFolderString(aFolder));
// Now set the biffState
switch (aFolder.biffState) {
case nsIMsgFolder.nsMsgBiffState_NewMail:
properties.push("biffState-NewMail");
break;
case nsIMsgFolder.nsMsgBiffState_NoMail:
properties.push("biffState-NoMail");
break;
default:
properties.push("biffState-UnknownMail");
}
properties.push("isSecure-" + aFolder.server.isSecure);
// A folder has new messages, or a closed folder or any subfolder has new messages.
if (aFolder.hasNewMessages ||
(!aOpen && aFolder.hasSubFolders && aFolder.hasFolderOrSubfolderNewMessages))
properties.push("newMessages-true");
if (aFolder.isServer) {
properties.push("isServer-true");
}
else
{
// We only set this if we're not a server
let shallowUnread = aFolder.getNumUnread(false);
if (shallowUnread > 0) {
properties.push("hasUnreadMessages-true");
}
else
{
// Make sure that shallowUnread isn't negative
shallowUnread = 0;
}
let deepUnread = aFolder.getNumUnread(true);
if (deepUnread - shallowUnread > 0)
properties.push("subfoldersHaveUnreadMessages-true");
}
properties.push("noSelect-" + aFolder.noSelect);
properties.push("imapShared-" + aFolder.imapShared);
return properties.join(" ");
}
/**
* Returns a folder for a particular uri
*
* @param aUri the rdf uri of the folder to return
*/
function getFolderFromUri(aUri) {
const Cc = Components.classes;
const Ci = Components.interfaces;
return Cc["@mozilla.org/mail/folder-lookup;1"].
getService(Ci.nsIFolderLookupService).getFolderById(aUri);
}
/**
* Returns the sort order value based on the server type to be used for sorting.
* The servers (accounts) go in the following order:
* (0) default account, (1) other mail accounts, (2) Local Folders,
* (3) IM accounts, (4) RSS, (5) News, (9) others (no server)
* This ordering is encoded in the .sortOrder property of each server type.
*
* @param aServer the server object to be tested
*/
function getServerSortOrder(aServer) {
// If there is no server sort this object to the end.
if (!aServer)
return 999999999;
// Otherwise get the server sort order from the Account manager.
return MailServices.accounts.getSortOrder(aServer);
}
/**
* Compares the passed in accounts according to their precedence.
*/
function compareAccounts(aAccount1, aAccount2) {
return getServerSortOrder(aAccount1.incomingServer)
- getServerSortOrder(aAccount2.incomingServer);
}
/**
* Returns a list of accounts sorted by server type.
*
* @param aExcludeIMAccounts Remove IM accounts from the list?
*/
function allAccountsSorted(aExcludeIMAccounts) {
// Get the account list, and add the proper items.
let accountList = toArray(fixIterator(MailServices.accounts.accounts,
Components.interfaces.nsIMsgAccount));
// This is a HACK to work around bug 41133. If we have one of the
// dummy "news" accounts there, that account won't have an
// incomingServer attached to it, and everything will blow up.
accountList = accountList.filter(function hasServer(a) {
return a.incomingServer;
});
// Remove IM servers.
if (aExcludeIMAccounts) {
accountList = accountList.filter(function(a) {
return a.incomingServer.type != "im";
});
}
return accountList.sort(compareAccounts);
}
/**
* Returns the most recently used/modified folders from the passed in list.
*
* @param aFolderList The array of nsIMsgFolders to search for recent folders.
* @param aMaxHits How many folders to return.
* @param aTimeProperty Which folder time property to use.
* Use "MRMTime" for most recently modified time.
* Use "MRUTime" for most recently used time.
*/
function getMostRecentFolders(aFolderList, aMaxHits, aTimeProperty) {
let recentFolders = [];
/**
* This sub-function will add a folder to the recentFolders array if it
* is among the aMaxHits most recent. If we exceed aMaxHits folders,
* it will pop the oldest folder, ensuring that we end up with the
* right number.
*
* @param aFolder The folder to check for recency.
*/
let oldestTime = 0;
function addIfRecent(aFolder) {
let time = 0;
try {
time = Number(aFolder.getStringProperty(aTimeProperty)) || 0;
} catch(e) {}
if (time <= oldestTime)
return;
if (recentFolders.length == aMaxHits) {
recentFolders.sort(function sort_folders_by_time(a, b) {
return a.time < b.time; });
recentFolders.pop();
oldestTime = recentFolders[recentFolders.length - 1].time;
}
recentFolders.push({ folder: aFolder, time: time });
}
for (let folder of aFolderList) {
addIfRecent(folder);
}
return recentFolders.map(function (f) { return f.folder; });
}
/**
* A locale dependent comparison function to produce a case-insensitive sort order
* used to sort folder names.
* Returns positive number if aString1 > aString2, negative number if aString1 > aString2,
* otherwise 0.
*
* @param aString1 first string to compare
* @param aString2 second string to compare
*/
function folderNameCompare(aString1, aString2) {
// TODO: improve this as described in bug 992651.
return aString1.toLocaleLowerCase()
.localeCompare(aString2.toLocaleLowerCase());
}
|