Back to index

lightning-sunbird  0.9+nobinonly
nsMsgMailViewList.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott MacGregor <mscott@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMsgMailViewList.h"
00040 #include "nsReadableUtils.h"
00041 #include "nsISupportsArray.h"
00042 #include "nsIFileChannel.h"
00043 #include "nsIMsgFilterService.h"
00044 #include "nsIFileSpec.h"
00045 #include "nsIMsgMailSession.h"
00046 #include "nsMsgBaseCID.h"
00047 #include "nsAppDirectoryServiceDefs.h"
00048 
00049 #define kDefaultViewPeopleIKnow "People I Know"
00050 #define kDefaultViewRecent "Recent Mail"
00051 #define kDefaultViewFiveDays "Last 5 Days"
00052 #define kDefaultViewNotJunk "Not Junk"
00053 #define kDefaultViewHasAttachments "Has Attachments"
00054  
00055 nsMsgMailView::nsMsgMailView()
00056 {
00057     NS_NewISupportsArray(getter_AddRefs(mViewSearchTerms));
00058 }
00059 
00060 NS_IMPL_ADDREF(nsMsgMailView)
00061 NS_IMPL_RELEASE(nsMsgMailView)
00062 NS_IMPL_QUERY_INTERFACE1(nsMsgMailView, nsIMsgMailView)
00063 
00064 nsMsgMailView::~nsMsgMailView()
00065 {
00066     if (mViewSearchTerms)
00067         mViewSearchTerms->Clear();
00068 }
00069 
00070 NS_IMETHODIMP nsMsgMailView::GetMailViewName(PRUnichar ** aMailViewName)
00071 {
00072     *aMailViewName = ToNewUnicode(mName); 
00073     return NS_OK;
00074 }
00075 
00076 NS_IMETHODIMP nsMsgMailView::SetMailViewName(const PRUnichar * aMailViewName)
00077 {
00078     mName = aMailViewName;
00079     return NS_OK;
00080 }
00081 
00082 NS_IMETHODIMP nsMsgMailView::GetPrettyName(PRUnichar ** aMailViewName)
00083 {
00084     nsresult rv = NS_OK;
00085     if (!mBundle)
00086     {
00087         nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00088         NS_ENSURE_SUCCESS(rv, rv);
00089         bundleService->CreateBundle("chrome://messenger/locale/mailviews.properties",
00090                                     getter_AddRefs(mBundle));
00091     }
00092 
00093     NS_ENSURE_TRUE(mBundle, NS_ERROR_FAILURE);
00094 
00095     // see if mName has an associated pretty name inside our string bundle and if so, use that as the pretty name
00096     // otherwise just return mName
00097     if (mName.EqualsLiteral(kDefaultViewPeopleIKnow))
00098         rv = mBundle->GetStringFromName(NS_LITERAL_STRING("mailViewPeopleIKnow").get(), aMailViewName);    
00099     else if (mName.EqualsLiteral(kDefaultViewRecent))
00100         rv = mBundle->GetStringFromName(NS_LITERAL_STRING("mailViewRecentMail").get(), aMailViewName);  
00101     else if (mName.EqualsLiteral(kDefaultViewFiveDays))
00102         rv = mBundle->GetStringFromName(NS_LITERAL_STRING("mailViewLastFiveDays").get(), aMailViewName);  
00103     else if (mName.EqualsLiteral(kDefaultViewNotJunk))
00104         rv = mBundle->GetStringFromName(NS_LITERAL_STRING("mailViewNotJunk").get(), aMailViewName); 
00105     else if (mName.EqualsLiteral(kDefaultViewHasAttachments))
00106         rv = mBundle->GetStringFromName(NS_LITERAL_STRING("mailViewHasAttachments").get(), aMailViewName); 
00107     else
00108         *aMailViewName = ToNewUnicode(mName); 
00109 
00110     return rv;
00111 }
00112 
00113 NS_IMETHODIMP nsMsgMailView::GetSearchTerms(nsISupportsArray ** aSearchTerms)
00114 {
00115     NS_ENSURE_ARG_POINTER(aSearchTerms);
00116     NS_IF_ADDREF(*aSearchTerms = mViewSearchTerms);
00117     return NS_OK;
00118 }
00119 
00120 NS_IMETHODIMP nsMsgMailView::SetSearchTerms(nsISupportsArray * aSearchTerms)
00121 {
00122     mViewSearchTerms = aSearchTerms;
00123     return NS_OK;
00124 }
00125 
00126 NS_IMETHODIMP nsMsgMailView::AppendTerm(nsIMsgSearchTerm * aTerm)
00127 {
00128     NS_ENSURE_TRUE(aTerm, NS_ERROR_NULL_POINTER);
00129     
00130     return mViewSearchTerms->AppendElement(NS_STATIC_CAST(nsISupports*,aTerm));
00131 }
00132 
00133 NS_IMETHODIMP nsMsgMailView::CreateTerm(nsIMsgSearchTerm **aResult)
00134 {
00135     NS_ENSURE_ARG_POINTER(aResult);
00136     nsCOMPtr<nsIMsgSearchTerm> searchTerm = do_CreateInstance("@mozilla.org/messenger/searchTerm;1");
00137     NS_IF_ADDREF(*aResult = searchTerm);
00138     return NS_OK;
00139 }
00140 
00142 // nsMsgMailViewList implementation
00144 nsMsgMailViewList::nsMsgMailViewList()
00145 {
00146     LoadMailViews();
00147 }
00148 
00149 NS_IMPL_ADDREF(nsMsgMailViewList)
00150 NS_IMPL_RELEASE(nsMsgMailViewList)
00151 NS_IMPL_QUERY_INTERFACE1(nsMsgMailViewList, nsIMsgMailViewList)
00152 
00153 nsMsgMailViewList::~nsMsgMailViewList()
00154 {
00155 
00156 }
00157 
00158 NS_IMETHODIMP nsMsgMailViewList::GetMailViewCount(PRUint32 * aCount)
00159 {
00160     if (m_mailViews)
00161        m_mailViews->Count(aCount);
00162     else
00163         *aCount = 0;
00164     return NS_OK;
00165 }
00166 
00167 NS_IMETHODIMP nsMsgMailViewList::GetMailViewAt(PRUint32 aMailViewIndex, nsIMsgMailView ** aMailView)
00168 {
00169     NS_ENSURE_ARG_POINTER(aMailView);
00170     NS_ENSURE_TRUE(m_mailViews, NS_ERROR_FAILURE);
00171     
00172     PRUint32 mailViewCount;
00173     m_mailViews->Count(&mailViewCount);
00174     NS_ENSURE_TRUE(mailViewCount >= aMailViewIndex, NS_ERROR_FAILURE);
00175 
00176     return m_mailViews->QueryElementAt(aMailViewIndex, NS_GET_IID(nsIMsgMailView),
00177                                       (void **)aMailView);
00178 }
00179 
00180 NS_IMETHODIMP nsMsgMailViewList::AddMailView(nsIMsgMailView * aMailView)
00181 {
00182     NS_ENSURE_ARG_POINTER(aMailView);
00183     NS_ENSURE_TRUE(m_mailViews, NS_ERROR_FAILURE);
00184 
00185     m_mailViews->AppendElement(NS_STATIC_CAST(nsISupports*, aMailView));
00186     return NS_OK;
00187 }
00188 
00189 NS_IMETHODIMP nsMsgMailViewList::RemoveMailView(nsIMsgMailView * aMailView)
00190 {
00191     m_mailViews->RemoveElement(NS_STATIC_CAST(nsISupports*, aMailView));
00192     return NS_OK;
00193 }
00194 
00195 NS_IMETHODIMP nsMsgMailViewList::CreateMailView(nsIMsgMailView ** aMailView)
00196 {
00197     NS_ENSURE_ARG_POINTER(aMailView);
00198 
00199     nsMsgMailView * mailView = new nsMsgMailView;
00200     NS_ENSURE_TRUE(mailView, NS_ERROR_OUT_OF_MEMORY);
00201 
00202     NS_IF_ADDREF(*aMailView = mailView);    
00203     return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP nsMsgMailViewList::Save()
00207 {
00208     // brute force...remove all the old filters in our filter list, then we'll re-add our current
00209     // list
00210     nsCOMPtr<nsIMsgFilter> msgFilter;
00211     PRUint32 numFilters;
00212     mFilterList->GetFilterCount(&numFilters);
00213     while (numFilters)
00214     {
00215         mFilterList->RemoveFilterAt(numFilters - 1);
00216         numFilters--;
00217     }
00218 
00219     // now convert our mail view list into a filter list and save it
00220     ConvertMailViewListToFilterList();
00221 
00222     // now save the filters to our file
00223     return mFilterList->SaveToDefaultFile();
00224 }
00225 
00226 nsresult nsMsgMailViewList::ConvertMailViewListToFilterList()
00227 {
00228   PRUint32 mailViewCount = 0;
00229   m_mailViews->Count(&mailViewCount);
00230   nsCOMPtr<nsIMsgMailView> mailView;
00231   nsCOMPtr<nsIMsgFilter> newMailFilter;
00232   nsXPIDLString mailViewName;
00233   for (PRUint32 index = 0; index < mailViewCount; index++)
00234   {
00235       GetMailViewAt(index, getter_AddRefs(mailView));
00236       if (!mailView)
00237           continue;
00238       mailView->GetMailViewName(getter_Copies(mailViewName));
00239       mFilterList->CreateFilter(mailViewName, getter_AddRefs(newMailFilter));
00240       if (!newMailFilter)
00241           continue;
00242 
00243       nsCOMPtr<nsISupportsArray> searchTerms;
00244       mailView->GetSearchTerms(getter_AddRefs(searchTerms));
00245       newMailFilter->SetSearchTerms(searchTerms);
00246       mFilterList->InsertFilterAt(index, newMailFilter);
00247   }
00248 
00249   return NS_OK;
00250 }
00251 
00252 nsresult nsMsgMailViewList::LoadMailViews()
00253 {
00254     nsCOMPtr<nsIFile> file;
00255     nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
00256     NS_ENSURE_SUCCESS(rv, rv);
00257 
00258     rv = file->AppendNative(nsDependentCString("mailViews.dat"));
00259 
00260     // if the file doesn't exist, we should try to get it from the defaults directory and copy it over
00261     PRBool exists = PR_FALSE;
00262     file->Exists(&exists);
00263     if (!exists)
00264     {
00265         nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
00266         NS_ENSURE_SUCCESS(rv, rv);
00267         nsCOMPtr<nsIFile> defaultMessagesFile;
00268         rv = mailSession->GetDataFilesDir("messenger", getter_AddRefs(defaultMessagesFile));
00269         rv = defaultMessagesFile->AppendNative(nsDependentCString("mailViews.dat"));
00270 
00271         nsCOMPtr<nsIFileSpec> defaultMailViewSpec;
00272         rv = NS_NewFileSpecFromIFile(defaultMessagesFile, getter_AddRefs(defaultMailViewSpec));
00273         
00274         // get the profile directory
00275         rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(defaultMessagesFile));
00276         
00277         // convert to spec
00278         nsCOMPtr<nsIFileSpec> profileDirSpec;
00279         rv = NS_NewFileSpecFromIFile(defaultMessagesFile, getter_AddRefs(profileDirSpec));
00280         // now copy the file over to the profile directory
00281         defaultMailViewSpec->CopyToDir(profileDirSpec);
00282     }
00283 
00284     nsCOMPtr<nsIFileSpec> mailViewSpec;
00285     rv = NS_NewFileSpecFromIFile(file, getter_AddRefs(mailViewSpec));
00286     if (NS_FAILED(rv)) return rv;
00287 
00288     // this is kind of a hack but I think it will be an effective hack. The filter service already knows how to 
00289     // take a nsIFileSpec and parse the contents into filters which are very similar to mail views. Intead of
00290     // re-writing all of that dirty parsing code, let's just re-use it then convert the results into a data strcuture
00291     // we wish to give to our consumers. 
00292       
00293     nsCOMPtr<nsIMsgFilterService> filterService = do_GetService(NS_MSGFILTERSERVICE_CONTRACTID, &rv);
00294     nsCOMPtr<nsIMsgFilterList> mfilterList;
00295       
00296     rv = filterService->OpenFilterList(mailViewSpec, NULL, NULL, getter_AddRefs(mFilterList));
00297     NS_ENSURE_SUCCESS(rv, rv);
00298 
00299     // now convert the filter list into our mail view objects, stripping out just the info we need
00300     ConvertFilterListToMailView(mFilterList, getter_AddRefs(m_mailViews));
00301     return rv;
00302 }
00303 
00304 nsresult nsMsgMailViewList::ConvertFilterListToMailView(nsIMsgFilterList * aFilterList, nsISupportsArray ** aMailViewList)
00305 {
00306     nsresult rv = NS_OK;
00307     NS_ENSURE_ARG_POINTER(aFilterList);
00308     NS_ENSURE_ARG_POINTER(aMailViewList);
00309 
00310     nsCOMPtr<nsISupportsArray> mailViewList;
00311     NS_NewISupportsArray(getter_AddRefs(mailViewList));
00312 
00313     // iterate over each filter in the list
00314     nsCOMPtr<nsIMsgFilter> msgFilter;
00315     PRUint32 numFilters;
00316     aFilterList->GetFilterCount(&numFilters);
00317     for (PRUint32 index = 0; index < numFilters; index++)
00318     {
00319         aFilterList->GetFilterAt(index, getter_AddRefs(msgFilter));
00320         if (!msgFilter)
00321             continue;
00322         // create a new nsIMsgMailView for this item
00323         nsCOMPtr<nsIMsgMailView> newMailView; 
00324         rv = CreateMailView(getter_AddRefs(newMailView));
00325         NS_ENSURE_SUCCESS(rv, rv);
00326         nsXPIDLString filterName;
00327         msgFilter->GetFilterName(getter_Copies(filterName));
00328         newMailView->SetMailViewName(filterName);
00329 
00330         nsCOMPtr<nsISupportsArray> filterSearchTerms;
00331         msgFilter->GetSearchTerms(getter_AddRefs(filterSearchTerms));
00332         newMailView->SetSearchTerms(filterSearchTerms);
00333 
00334         // now append this new mail view to our global list view
00335         mailViewList->AppendElement(NS_STATIC_CAST(nsISupports*, newMailView));
00336     }
00337 
00338     NS_IF_ADDREF(*aMailViewList = mailViewList);
00339 
00340     return rv;
00341 }