summaryrefslogtreecommitdiffstats
path: root/mailnews/extensions/mailviews/src/nsMsgMailViewList.cpp
blob: 8e5e04a3f3a9b71f6feba29310744e225e244815 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "nsMsgMailViewList.h"
// Disable deprecation warnings generated by nsISupportsArray and associated
// classes.
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning (disable : 4996)
#endif
#include "nsISupportsArray.h"
#include "nsIFileChannel.h"
#include "nsIMsgFilterService.h"
#include "nsIMsgMailSession.h"
#include "nsIMsgSearchTerm.h"
#include "nsMsgBaseCID.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsIFile.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/Services.h"
#include "nsIMsgFilter.h"

#define kDefaultViewPeopleIKnow "People I Know"
#define kDefaultViewRecent "Recent Mail"
#define kDefaultViewFiveDays "Last 5 Days"
#define kDefaultViewNotJunk "Not Junk"
#define kDefaultViewHasAttachments "Has Attachments"
 
nsMsgMailView::nsMsgMailView()
{
    mViewSearchTerms = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID);
}

NS_IMPL_ADDREF(nsMsgMailView)
NS_IMPL_RELEASE(nsMsgMailView)
NS_IMPL_QUERY_INTERFACE(nsMsgMailView, nsIMsgMailView)

nsMsgMailView::~nsMsgMailView()
{
    if (mViewSearchTerms)
        mViewSearchTerms->Clear();
}

NS_IMETHODIMP nsMsgMailView::GetMailViewName(char16_t ** aMailViewName)
{
    NS_ENSURE_ARG_POINTER(aMailViewName);

    *aMailViewName = ToNewUnicode(mName);
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailView::SetMailViewName(const char16_t * aMailViewName)
{
    mName = aMailViewName;
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailView::GetPrettyName(char16_t ** aMailViewName)
{
    NS_ENSURE_ARG_POINTER(aMailViewName);

    nsresult rv = NS_OK;
    if (!mBundle)
    {
        nsCOMPtr<nsIStringBundleService> bundleService =
          mozilla::services::GetStringBundleService();
        NS_ENSURE_TRUE(bundleService, NS_ERROR_UNEXPECTED);
        bundleService->CreateBundle("chrome://messenger/locale/mailviews.properties",
                                    getter_AddRefs(mBundle));
    }

    NS_ENSURE_TRUE(mBundle, NS_ERROR_FAILURE);

    // see if mName has an associated pretty name inside our string bundle and if so, use that as the pretty name
    // otherwise just return mName
    if (mName.EqualsLiteral(kDefaultViewPeopleIKnow))
        rv = mBundle->GetStringFromName(u"mailViewPeopleIKnow", aMailViewName);
    else if (mName.EqualsLiteral(kDefaultViewRecent))
        rv = mBundle->GetStringFromName(u"mailViewRecentMail", aMailViewName);
    else if (mName.EqualsLiteral(kDefaultViewFiveDays))
        rv = mBundle->GetStringFromName(u"mailViewLastFiveDays", aMailViewName);
    else if (mName.EqualsLiteral(kDefaultViewNotJunk))
        rv = mBundle->GetStringFromName(u"mailViewNotJunk", aMailViewName);
    else if (mName.EqualsLiteral(kDefaultViewHasAttachments))
        rv = mBundle->GetStringFromName(u"mailViewHasAttachments", aMailViewName);
    else
        *aMailViewName = ToNewUnicode(mName);

    return rv;
}

NS_IMETHODIMP nsMsgMailView::GetSearchTerms(nsISupportsArray ** aSearchTerms)
{
    NS_ENSURE_ARG_POINTER(aSearchTerms);
    NS_IF_ADDREF(*aSearchTerms = mViewSearchTerms);
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailView::SetSearchTerms(nsISupportsArray * aSearchTerms)
{
    mViewSearchTerms = aSearchTerms;
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailView::AppendTerm(nsIMsgSearchTerm * aTerm)
{
    NS_ENSURE_TRUE(aTerm, NS_ERROR_NULL_POINTER);
    
    return mViewSearchTerms->AppendElement(static_cast<nsISupports*>(aTerm));
}

NS_IMETHODIMP nsMsgMailView::CreateTerm(nsIMsgSearchTerm **aResult)
{
    NS_ENSURE_ARG_POINTER(aResult);
    nsCOMPtr<nsIMsgSearchTerm> searchTerm = do_CreateInstance("@mozilla.org/messenger/searchTerm;1");
    NS_IF_ADDREF(*aResult = searchTerm);
    return NS_OK;
}

/////////////////////////////////////////////////////////////////////////////
// nsMsgMailViewList implementation
/////////////////////////////////////////////////////////////////////////////
nsMsgMailViewList::nsMsgMailViewList()
{
    LoadMailViews();
}

NS_IMPL_ADDREF(nsMsgMailViewList)
NS_IMPL_RELEASE(nsMsgMailViewList)
NS_IMPL_QUERY_INTERFACE(nsMsgMailViewList, nsIMsgMailViewList)

nsMsgMailViewList::~nsMsgMailViewList()
{

}

NS_IMETHODIMP nsMsgMailViewList::GetMailViewCount(uint32_t * aCount)
{
    NS_ENSURE_ARG_POINTER(aCount);

    *aCount = m_mailViews.Length();
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailViewList::GetMailViewAt(uint32_t aMailViewIndex, nsIMsgMailView ** aMailView)
{
    NS_ENSURE_ARG_POINTER(aMailView);

    uint32_t mailViewCount = m_mailViews.Length();

    NS_ENSURE_ARG(mailViewCount > aMailViewIndex);

    NS_IF_ADDREF(*aMailView = m_mailViews[aMailViewIndex]);
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailViewList::AddMailView(nsIMsgMailView * aMailView)
{
    NS_ENSURE_ARG_POINTER(aMailView);

    m_mailViews.AppendElement(aMailView);
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailViewList::RemoveMailView(nsIMsgMailView * aMailView)
{
    NS_ENSURE_ARG_POINTER(aMailView);

    m_mailViews.RemoveElement(aMailView);
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailViewList::CreateMailView(nsIMsgMailView ** aMailView)
{
    NS_ENSURE_ARG_POINTER(aMailView);

    nsMsgMailView * mailView = new nsMsgMailView;
    NS_ENSURE_TRUE(mailView, NS_ERROR_OUT_OF_MEMORY);

    NS_IF_ADDREF(*aMailView = mailView);
    return NS_OK;
}

NS_IMETHODIMP nsMsgMailViewList::Save()
{
    // brute force...remove all the old filters in our filter list, then we'll re-add our current
    // list
    nsCOMPtr<nsIMsgFilter> msgFilter;
    uint32_t numFilters = 0;
    if (mFilterList)
      mFilterList->GetFilterCount(&numFilters);
    while (numFilters)
    {
        mFilterList->RemoveFilterAt(numFilters - 1);
        numFilters--;
    }

    // now convert our mail view list into a filter list and save it
    ConvertMailViewListToFilterList();

    // now save the filters to our file
    return mFilterList ? mFilterList->SaveToDefaultFile() : NS_ERROR_FAILURE;
}

nsresult nsMsgMailViewList::ConvertMailViewListToFilterList()
{
  uint32_t mailViewCount = m_mailViews.Length();
  nsCOMPtr<nsIMsgMailView> mailView;
  nsCOMPtr<nsIMsgFilter> newMailFilter;
  nsString mailViewName;
  for (uint32_t index = 0; index < mailViewCount; index++)
  {
      GetMailViewAt(index, getter_AddRefs(mailView));
      if (!mailView)
          continue;
      mailView->GetMailViewName(getter_Copies(mailViewName));
      mFilterList->CreateFilter(mailViewName, getter_AddRefs(newMailFilter));
      if (!newMailFilter)
          continue;

      nsCOMPtr<nsISupportsArray> searchTerms;
      mailView->GetSearchTerms(getter_AddRefs(searchTerms));
      newMailFilter->SetSearchTerms(searchTerms);
      mFilterList->InsertFilterAt(index, newMailFilter);
  }

  return NS_OK;
}

nsresult nsMsgMailViewList::LoadMailViews()
{
    nsCOMPtr<nsIFile> file;
    nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = file->AppendNative(nsDependentCString("mailViews.dat"));

    // if the file doesn't exist, we should try to get it from the defaults directory and copy it over
    bool exists = false;
    file->Exists(&exists);
    if (!exists)
    {
        nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
        NS_ENSURE_SUCCESS(rv, rv);
        nsCOMPtr<nsIFile> defaultMessagesFile;
        nsCOMPtr<nsIFile> profileDir;
        rv = mailSession->GetDataFilesDir("messenger", getter_AddRefs(defaultMessagesFile));
        rv = defaultMessagesFile->AppendNative(nsDependentCString("mailViews.dat"));

         // get the profile directory
        rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
        
        // now copy the file over to the profile directory
        defaultMessagesFile->CopyToNative(profileDir, EmptyCString());
    }
    // this is kind of a hack but I think it will be an effective hack. The filter service already knows how to 
    // take a nsIFile and parse the contents into filters which are very similar to mail views. Intead of
    // re-writing all of that dirty parsing code, let's just re-use it then convert the results into a data strcuture
    // we wish to give to our consumers. 
      
    nsCOMPtr<nsIMsgFilterService> filterService = do_GetService(NS_MSGFILTERSERVICE_CONTRACTID, &rv);
    nsCOMPtr<nsIMsgFilterList> mfilterList;
      
    rv = filterService->OpenFilterList(file, nullptr, nullptr, getter_AddRefs(mFilterList));
    NS_ENSURE_SUCCESS(rv, rv);

    return ConvertFilterListToMailViews();
}
/**
 * Converts the filter list into our mail view objects,
 * stripping out just the info we need.
 */
nsresult nsMsgMailViewList::ConvertFilterListToMailViews()
{
    nsresult rv = NS_OK;
    m_mailViews.Clear();

    // iterate over each filter in the list
    uint32_t numFilters = 0;
    mFilterList->GetFilterCount(&numFilters);
    for (uint32_t index = 0; index < numFilters; index++)
    {
        nsCOMPtr<nsIMsgFilter> msgFilter;
        rv = mFilterList->GetFilterAt(index, getter_AddRefs(msgFilter));
        if (NS_FAILED(rv) || !msgFilter)
            continue;

        // create a new nsIMsgMailView for this item
        nsCOMPtr<nsIMsgMailView> newMailView;
        rv = CreateMailView(getter_AddRefs(newMailView));
        NS_ENSURE_SUCCESS(rv, rv);

        nsString filterName;
        msgFilter->GetFilterName(filterName);
        newMailView->SetMailViewName(filterName.get());

        nsCOMPtr<nsISupportsArray> filterSearchTerms;
        rv = msgFilter->GetSearchTerms(getter_AddRefs(filterSearchTerms));
        NS_ENSURE_SUCCESS(rv, rv);
        rv = newMailView->SetSearchTerms(filterSearchTerms);
        NS_ENSURE_SUCCESS(rv, rv);

        // now append this new mail view to our global list view
        m_mailViews.AppendElement(newMailView);
    }

    return rv;
}