summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/nsMsgTagService.cpp
blob: 83ced5fdeba622dfaa3e7f4c435562ed5a153beb (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#include "msgCore.h"
#include "nsMsgTagService.h"
#include "nsIPrefService.h"
#include "nsISupportsPrimitives.h"
#include "nsMsgI18N.h"
#include "nsIPrefLocalizedString.h"
#include "nsMsgDBView.h"  // for labels migration
#include "nsQuickSort.h"
#include "nsMsgUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsMemory.h"

#define STRLEN(s) (sizeof(s) - 1)

#define TAG_PREF_VERSION "version"
#define TAG_PREF_SUFFIX_TAG ".tag"
#define TAG_PREF_SUFFIX_COLOR ".color"
#define TAG_PREF_SUFFIX_ORDINAL ".ordinal"

static bool gMigratingKeys = false;

// Comparator to set sort order in GetAllTags().
struct CompareMsgTags {
 private:
  int cmp(RefPtr<nsIMsgTag> element1, RefPtr<nsIMsgTag> element2) const {
    // Sort nsMsgTag objects by ascending order, using their ordinal or key.
    // The "smallest" value will be first in the sorted array,
    // thus being the most important element.

    // Only use the key if the ordinal is not defined or empty.
    nsAutoCString value1, value2;
    element1->GetOrdinal(value1);
    if (value1.IsEmpty()) element1->GetKey(value1);
    element2->GetOrdinal(value2);
    if (value2.IsEmpty()) element2->GetKey(value2);

    return strcmp(value1.get(), value2.get());
  }

 public:
  bool Equals(RefPtr<nsIMsgTag> element1, RefPtr<nsIMsgTag> element2) const {
    return cmp(element1, element2) == 0;
  }
  bool LessThan(RefPtr<nsIMsgTag> element1, RefPtr<nsIMsgTag> element2) const {
    return cmp(element1, element2) < 0;
  }
};

//
//  nsMsgTag
//
NS_IMPL_ISUPPORTS(nsMsgTag, nsIMsgTag)

nsMsgTag::nsMsgTag(const nsACString& aKey, const nsAString& aTag,
                   const nsACString& aColor, const nsACString& aOrdinal)
    : mTag(aTag), mKey(aKey), mColor(aColor), mOrdinal(aOrdinal) {}

nsMsgTag::~nsMsgTag() {}

/* readonly attribute ACString key; */
NS_IMETHODIMP nsMsgTag::GetKey(nsACString& aKey) {
  aKey = mKey;
  return NS_OK;
}

/* readonly attribute AString tag; */
NS_IMETHODIMP nsMsgTag::GetTag(nsAString& aTag) {
  aTag = mTag;
  return NS_OK;
}

/* readonly attribute ACString color; */
NS_IMETHODIMP nsMsgTag::GetColor(nsACString& aColor) {
  aColor = mColor;
  return NS_OK;
}

/* readonly attribute ACString ordinal; */
NS_IMETHODIMP nsMsgTag::GetOrdinal(nsACString& aOrdinal) {
  aOrdinal = mOrdinal;
  return NS_OK;
}

//
//  nsMsgTagService
//
NS_IMPL_ISUPPORTS(nsMsgTagService, nsIMsgTagService)

nsMsgTagService::nsMsgTagService() {
  m_tagPrefBranch = nullptr;
  nsCOMPtr<nsIPrefService> prefService(
      do_GetService(NS_PREFSERVICE_CONTRACTID));
  if (prefService)
    prefService->GetBranch("mailnews.tags.", getter_AddRefs(m_tagPrefBranch));
  SetupLabelTags();
  RefreshKeyCache();
}

nsMsgTagService::~nsMsgTagService() {} /* destructor code */

/* wstring getTagForKey (in string key); */
NS_IMETHODIMP nsMsgTagService::GetTagForKey(const nsACString& key,
                                            nsAString& _retval) {
  nsAutoCString prefName(key);
  if (!gMigratingKeys) ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_TAG);
  return GetUnicharPref(prefName.get(), _retval);
}

/* void setTagForKey (in string key); */
NS_IMETHODIMP nsMsgTagService::SetTagForKey(const nsACString& key,
                                            const nsAString& tag) {
  nsAutoCString prefName(key);
  ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_TAG);
  return SetUnicharPref(prefName.get(), tag);
}

/* void getKeyForTag (in wstring tag); */
NS_IMETHODIMP nsMsgTagService::GetKeyForTag(const nsAString& aTag,
                                            nsACString& aKey) {
  nsTArray<nsCString> prefList;
  nsresult rv = m_tagPrefBranch->GetChildList("", prefList);
  NS_ENSURE_SUCCESS(rv, rv);
  // traverse the list, and look for a pref with the desired tag value.
  // XXXbz is there a good reason to reverse the list here, or did the
  // old code do it just to be clever and save some characters in the
  // for loop header?
  for (auto& prefName : mozilla::Reversed(prefList)) {
    // We are returned the tag prefs in the form "<key>.<tag_data_type>", but
    // since we only want the tags, just check that the string ends with "tag".
    if (StringEndsWith(prefName, nsLiteralCString(TAG_PREF_SUFFIX_TAG))) {
      nsAutoString curTag;
      GetUnicharPref(prefName.get(), curTag);
      if (aTag.Equals(curTag)) {
        aKey = Substring(prefName, 0,
                         prefName.Length() - STRLEN(TAG_PREF_SUFFIX_TAG));
        break;
      }
    }
  }
  ToLowerCase(aKey);
  return NS_OK;
}

/* ACString getTopKey (in ACString keylist); */
NS_IMETHODIMP nsMsgTagService::GetTopKey(const nsACString& keyList,
                                         nsACString& _retval) {
  _retval.Truncate();
  // find the most important key
  nsTArray<nsCString> keyArray;
  ParseString(keyList, ' ', keyArray);
  uint32_t keyCount = keyArray.Length();
  nsCString *topKey = nullptr, *key, topOrdinal, ordinal;
  for (uint32_t i = 0; i < keyCount; ++i) {
    key = &keyArray[i];
    if (key->IsEmpty()) continue;

    // ignore unknown keywords
    nsAutoString tagValue;
    nsresult rv = GetTagForKey(*key, tagValue);
    if (NS_FAILED(rv) || tagValue.IsEmpty()) continue;

    // new top key, judged by ordinal order?
    rv = GetOrdinalForKey(*key, ordinal);
    if (NS_FAILED(rv) || ordinal.IsEmpty()) ordinal = *key;
    if ((ordinal < topOrdinal) || topOrdinal.IsEmpty()) {
      topOrdinal = ordinal;
      topKey = key;  // copy actual result key only once - later
    }
  }
  // return the most important key - if any
  if (topKey) _retval = *topKey;
  return NS_OK;
}

/* void addTagForKey (in string key, in wstring tag, in string color, in string
 * ordinal); */
NS_IMETHODIMP nsMsgTagService::AddTagForKey(const nsACString& key,
                                            const nsAString& tag,
                                            const nsACString& color,
                                            const nsACString& ordinal) {
  nsAutoCString prefName(key);
  ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_TAG);
  nsresult rv = SetUnicharPref(prefName.get(), tag);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = SetColorForKey(key, color);
  NS_ENSURE_SUCCESS(rv, rv);
  rv = RefreshKeyCache();
  NS_ENSURE_SUCCESS(rv, rv);
  return SetOrdinalForKey(key, ordinal);
}

/* void addTag (in wstring tag, in long color); */
NS_IMETHODIMP nsMsgTagService::AddTag(const nsAString& tag,
                                      const nsACString& color,
                                      const nsACString& ordinal) {
  // figure out key from tag. Apply transformation stripping out
  // illegal characters like <SP> and then convert to imap mod utf7.
  // Then, check if we have a tag with that key yet, and if so,
  // make it unique by appending A, AA, etc.
  // Should we use an iterator?
  nsAutoString transformedTag(tag);
  transformedTag.ReplaceChar(u" ()/{%*<>\\\"", u'_');
  nsAutoCString key;
  CopyUTF16toMUTF7(transformedTag, key);
  // We have an imap server that converts keys to upper case so we're going
  // to normalize all keys to lower case (upper case looks ugly in prefs.js)
  ToLowerCase(key);
  nsAutoCString prefName(key);
  while (true) {
    nsAutoString tagValue;
    nsresult rv = GetTagForKey(prefName, tagValue);
    if (NS_FAILED(rv) || tagValue.IsEmpty() || tagValue.Equals(tag))
      return AddTagForKey(prefName, tag, color, ordinal);
    prefName.Append('A');
  }
  NS_ASSERTION(false, "can't get here");
  return NS_ERROR_FAILURE;
}

/* long getColorForKey (in string key); */
NS_IMETHODIMP nsMsgTagService::GetColorForKey(const nsACString& key,
                                              nsACString& _retval) {
  nsAutoCString prefName(key);
  if (!gMigratingKeys) ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_COLOR);
  nsCString color;
  nsresult rv = m_tagPrefBranch->GetCharPref(prefName.get(), color);
  if (NS_SUCCEEDED(rv)) _retval = color;
  return NS_OK;
}

/* long getSelectorForKey (in ACString key, out AString selector); */
NS_IMETHODIMP nsMsgTagService::GetSelectorForKey(const nsACString& key,
                                                 nsAString& _retval) {
  // Our keys are the result of MUTF-7 encoding. For CSS selectors we need
  // to reduce this to 0-9A-Za-z_ with a leading alpha character.
  // We encode non-alphanumeric characters using _ as an escape character
  // and start with a leading T in all cases. This way users defining tags
  // "selected" or "focus" don't collide with inbuilt "selected" or "focus".

  // Calculate length of selector string.
  const char* in = key.BeginReading();
  size_t outLen = 1;
  while (*in) {
    if (('0' <= *in && *in <= '9') || ('A' <= *in && *in <= 'Z') ||
        ('a' <= *in && *in <= 'z')) {
      outLen++;
    } else {
      outLen += 3;
    }
    in++;
  }

  // Now fill selector string.
  _retval.SetCapacity(outLen);
  _retval.Assign('T');
  in = key.BeginReading();
  while (*in) {
    if (('0' <= *in && *in <= '9') || ('A' <= *in && *in <= 'Z') ||
        ('a' <= *in && *in <= 'z')) {
      _retval.Append(*in);
    } else {
      _retval.AppendPrintf("_%02x", *in);
    }
    in++;
  }

  return NS_OK;
}

/* void setColorForKey (in ACString key, in ACString color); */
NS_IMETHODIMP nsMsgTagService::SetColorForKey(const nsACString& key,
                                              const nsACString& color) {
  nsAutoCString prefName(key);
  ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_COLOR);
  if (color.IsEmpty()) {
    m_tagPrefBranch->ClearUserPref(prefName.get());
    return NS_OK;
  }
  return m_tagPrefBranch->SetCharPref(prefName.get(), color);
}

/* ACString getOrdinalForKey (in ACString key); */
NS_IMETHODIMP nsMsgTagService::GetOrdinalForKey(const nsACString& key,
                                                nsACString& _retval) {
  nsAutoCString prefName(key);
  if (!gMigratingKeys) ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_ORDINAL);
  nsCString ordinal;
  nsresult rv = m_tagPrefBranch->GetCharPref(prefName.get(), ordinal);
  _retval = ordinal;
  return rv;
}

/* void setOrdinalForKey (in ACString key, in ACString ordinal); */
NS_IMETHODIMP nsMsgTagService::SetOrdinalForKey(const nsACString& key,
                                                const nsACString& ordinal) {
  nsAutoCString prefName(key);
  ToLowerCase(prefName);
  prefName.AppendLiteral(TAG_PREF_SUFFIX_ORDINAL);
  if (ordinal.IsEmpty()) {
    m_tagPrefBranch->ClearUserPref(prefName.get());
    return NS_OK;
  }
  return m_tagPrefBranch->SetCharPref(prefName.get(), ordinal);
}

/* void deleteTag (in wstring tag); */
NS_IMETHODIMP nsMsgTagService::DeleteKey(const nsACString& key) {
  // clear the associated prefs
  nsAutoCString prefName(key);
  if (!gMigratingKeys) ToLowerCase(prefName);
  prefName.Append('.');

  nsTArray<nsCString> prefNames;
  nsresult rv = m_tagPrefBranch->GetChildList(prefName.get(), prefNames);
  NS_ENSURE_SUCCESS(rv, rv);

  for (auto& prefName : prefNames) {
    m_tagPrefBranch->ClearUserPref(prefName.get());
  }

  return RefreshKeyCache();
}

/* Array<nsIMsgTag> getAllTags(); */
NS_IMETHODIMP nsMsgTagService::GetAllTags(
    nsTArray<RefPtr<nsIMsgTag>>& aTagArray) {
  aTagArray.Clear();

  // get the actual tag definitions
  nsresult rv;
  nsTArray<nsCString> prefList;
  rv = m_tagPrefBranch->GetChildList("", prefList);
  NS_ENSURE_SUCCESS(rv, rv);
  // sort them by key for ease of processing
  prefList.Sort();

  nsString tag;
  nsCString lastKey, color, ordinal;
  for (auto& pref : mozilla::Reversed(prefList)) {
    // extract just the key from <key>.<info=tag|color|ordinal>
    int32_t dotLoc = pref.RFindChar('.');
    if (dotLoc != kNotFound) {
      auto& key = Substring(pref, 0, dotLoc);
      if (key != lastKey) {
        if (!key.IsEmpty()) {
          // .tag MUST exist (but may be empty)
          rv = GetTagForKey(key, tag);
          if (NS_SUCCEEDED(rv)) {
            // .color MAY exist
            color.Truncate();
            GetColorForKey(key, color);
            // .ordinal MAY exist
            rv = GetOrdinalForKey(key, ordinal);
            if (NS_FAILED(rv)) ordinal.Truncate();
            // store the tag info in our array
            aTagArray.AppendElement(new nsMsgTag(key, tag, color, ordinal));
          }
        }
        lastKey = key;
      }
    }
  }

  // sort the non-null entries by ordinal
  aTagArray.Sort(CompareMsgTags());
  return NS_OK;
}

nsresult nsMsgTagService::SetUnicharPref(const char* prefName,
                                         const nsAString& val) {
  nsresult rv = NS_OK;
  if (!val.IsEmpty()) {
    rv = m_tagPrefBranch->SetStringPref(prefName, NS_ConvertUTF16toUTF8(val));
  } else {
    m_tagPrefBranch->ClearUserPref(prefName);
  }
  return rv;
}

nsresult nsMsgTagService::GetUnicharPref(const char* prefName,
                                         nsAString& prefValue) {
  nsCString valueUtf8;
  nsresult rv =
      m_tagPrefBranch->GetStringPref(prefName, EmptyCString(), 0, valueUtf8);
  CopyUTF8toUTF16(valueUtf8, prefValue);
  return rv;
}

nsresult nsMsgTagService::SetupLabelTags() {
  nsCString prefString;

  int32_t prefVersion = 0;
  nsresult rv = m_tagPrefBranch->GetIntPref(TAG_PREF_VERSION, &prefVersion);
  if (NS_SUCCEEDED(rv) && prefVersion > 1) {
    return rv;
  }
  nsCOMPtr<nsIPrefBranch> prefRoot(do_GetService(NS_PREFSERVICE_CONTRACTID));
  nsCOMPtr<nsIPrefLocalizedString> pls;
  nsString ucsval;
  nsAutoCString labelKey("$label1");
  for (int32_t i = 0; i < 5;) {
    prefString.AssignLiteral("mailnews.labels.description.");
    prefString.AppendInt(i + 1);
    rv = prefRoot->GetComplexValue(prefString.get(),
                                   NS_GET_IID(nsIPrefLocalizedString),
                                   getter_AddRefs(pls));
    NS_ENSURE_SUCCESS(rv, rv);
    pls->ToString(getter_Copies(ucsval));

    prefString.AssignLiteral("mailnews.labels.color.");
    prefString.AppendInt(i + 1);
    nsCString csval;
    rv = prefRoot->GetCharPref(prefString.get(), csval);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = AddTagForKey(labelKey, ucsval, csval, EmptyCString());
    NS_ENSURE_SUCCESS(rv, rv);
    labelKey.SetCharAt(++i + '1', 6);
  }
  m_tagPrefBranch->SetIntPref(TAG_PREF_VERSION, 2);
  return rv;
}

NS_IMETHODIMP nsMsgTagService::IsValidKey(const nsACString& aKey,
                                          bool* aResult) {
  NS_ENSURE_ARG_POINTER(aResult);
  *aResult = m_keys.Contains(aKey);
  return NS_OK;
}

// refresh the local tag key array m_keys from preferences
nsresult nsMsgTagService::RefreshKeyCache() {
  nsTArray<RefPtr<nsIMsgTag>> tagArray;
  nsresult rv = GetAllTags(tagArray);
  NS_ENSURE_SUCCESS(rv, rv);
  m_keys.Clear();

  uint32_t numTags = tagArray.Length();
  m_keys.SetCapacity(numTags);
  for (uint32_t tagIndex = 0; tagIndex < numTags; tagIndex++) {
    nsAutoCString key;
    tagArray[tagIndex]->GetKey(key);
    m_keys.InsertElementAt(tagIndex, key);
  }
  return rv;
}