Back to index

lightning-sunbird  0.9+nobinonly
nsMsgFilterDelegateFactory.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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alec Flett <alecf@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 "nsMsgFilterDelegateFactory.h"
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsXPIDLString.h"
00043 #include "nsTextFormatter.h"
00044 
00045 #include "nsIRDFService.h"
00046 #include "nsIRDFResource.h"
00047 #include "nsRDFCID.h"
00048 
00049 #include "nsIMsgFolder.h"
00050 #include "nsIMsgFilter.h"
00051 #include "nsIMsgFilterList.h"
00052 
00053 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
00054 
00055 // unicode "%s" format string
00056 static const PRUnichar unicodeFormatter[] = {
00057     (PRUnichar)'%',
00058     (PRUnichar)'s',
00059     (PRUnichar)0,
00060 };
00061 
00062 
00063 NS_IMPL_ISUPPORTS1(nsMsgFilterDelegateFactory, nsIRDFDelegateFactory)
00064 
00065 nsMsgFilterDelegateFactory::nsMsgFilterDelegateFactory()
00066 {
00067   /* member initializers and constructor code */
00068 }
00069 
00070 nsMsgFilterDelegateFactory::~nsMsgFilterDelegateFactory()
00071 {
00072   /* destructor code */
00073 }
00074 
00075 /* void CreateDelegate (in nsIRDFResource aOuter, in string aKey, in nsIIDRef aIID, [iid_is (aIID), retval] out nsQIResult aResult); */
00076 NS_IMETHODIMP nsMsgFilterDelegateFactory::CreateDelegate(nsIRDFResource *aOuter, const char *aKey, const nsIID & aIID, void * *aResult)
00077 {
00078     nsresult rv;
00079     *aResult = nsnull;
00080     
00081     // if it's a folder, then we return a filter list..
00082     // otherwise make sure it's in the form
00083     // mailbox://userid@server/foldername;filterName=foo
00084     
00085 
00086     const char* uri;
00087     aOuter->GetValueConst(&uri);
00088 
00089     // if it has ';filter' then it's a filter, otherwise we'll assume
00090     // that it's a folder.
00091     nsCAutoString uriStr(uri);
00092 
00093     nsCOMPtr<nsISupports> resultSupports;
00094     
00095     if (uriStr.Find("filterName=") != -1) {
00096         nsCOMPtr<nsIMsgFilter> filter;
00097         rv = getFilterDelegate(aOuter, getter_AddRefs(filter));
00098         if (NS_SUCCEEDED(rv))
00099             resultSupports = filter;
00100         
00101     }
00102     else {
00103         // probably a folder, get the filter list
00104         nsCOMPtr<nsIMsgFilterList> filterList;
00105         rv = getFilterListDelegate(aOuter, getter_AddRefs(filterList));
00106         if (NS_SUCCEEDED(rv))
00107             resultSupports = filterList;
00108         
00109     }
00110 
00111     if (resultSupports)
00112         return resultSupports->QueryInterface(aIID, aResult);
00113 
00114     return NS_ERROR_FAILURE;
00115 }
00116 
00117 nsresult
00118 nsMsgFilterDelegateFactory::getFilterListDelegate(nsIRDFResource *aOuter,
00119                                                   nsIMsgFilterList **aResult)
00120 {
00121     nsresult rv;
00122     
00123     nsCOMPtr<nsIMsgFolder> folder = do_QueryInterface(aOuter, &rv);
00124     NS_ENSURE_SUCCESS(rv, rv);
00125     
00126     nsCOMPtr<nsIMsgFilterList> filterList;
00127     rv = folder->GetFilterList(nsnull, getter_AddRefs(filterList));
00128     NS_ENSURE_SUCCESS(rv, rv);
00129     
00130     *aResult = filterList;
00131     NS_IF_ADDREF(*aResult);
00132     return NS_OK;
00133 }
00134 
00135 nsresult
00136 nsMsgFilterDelegateFactory::getFilterDelegate(nsIRDFResource *aOuter,
00137                                               nsIMsgFilter **aResult)
00138 {
00139     nsresult rv;
00140     // now try to find ";filterName="
00141     nsXPIDLCString uri;
00142     rv = aOuter->GetValue(getter_Copies(uri));
00143     if (NS_FAILED(rv)) return rv;
00144     
00145     PRInt32 separatorPosition = 0;
00146     const char *filterTag = uri;
00147     while (filterTag && *filterTag != ';') {
00148         separatorPosition++;
00149         filterTag++;
00150     }
00151     
00152     // if no ;, and it's not a folder,
00153     // I don't know what the heck it is.
00154     if (!filterTag)
00155         return NS_ERROR_FAILURE;
00156 
00157     const char *filterName = getFilterName(filterTag);
00158 
00159     nsCOMPtr<nsIMsgFilterList> filterList;
00160     rv = getFilterList(uri, separatorPosition, getter_AddRefs(filterList));
00161 
00162         // now that we have the filter list and index, retrieve the filter.
00163 
00164     // XXX convert from UTF8
00165     nsAutoString filterString;
00166     PRUnichar *unicodeString =
00167         nsTextFormatter::smprintf(unicodeFormatter, filterName);
00168     NS_ENSURE_TRUE(unicodeString, NS_ERROR_OUT_OF_MEMORY);
00169     
00170     filterString.Assign(unicodeString);
00171 
00172     nsTextFormatter::smprintf_free(unicodeString);
00173     
00174     nsCOMPtr<nsIMsgFilter> filter;
00175     rv = filterList->GetFilterNamed(filterString.get(), getter_AddRefs(filter));
00176     if (NS_FAILED(rv)) return rv;
00177 
00178     *aResult = filter;
00179     NS_IF_ADDREF(*aResult);
00180     
00181     return NS_OK;
00182 }
00183     
00184 
00185 const char *
00186 nsMsgFilterDelegateFactory::getFilterName(const char *filterTag)
00187 {
00188     
00189     if (nsCRT::strncmp(filterTag, MSGFILTER_TAG, MSGFILTER_TAG_LENGTH) != 0)
00190         return nsnull;
00191     
00192     const char *filterNameStr = filterTag + MSGFILTER_TAG_LENGTH;
00193 
00194     return filterNameStr;
00195 }
00196 
00197 nsresult
00198 nsMsgFilterDelegateFactory::getFilterList(const char *aUri,
00199                                           PRInt32 aTagPosition,
00200                                           nsIMsgFilterList** aResult)
00201 {
00202     nsresult rv;
00203     // now we actually need the filter list, so we truncate the string
00204     // and use getdelegate to get the filter delegate
00205     nsCAutoString folderUri((const char*)aUri);
00206     folderUri.Truncate(aTagPosition);
00207     
00208     // convert URI to resource
00209     nsCOMPtr<nsIRDFService> rdf(do_GetService(kRDFServiceCID, &rv));
00210     NS_ENSURE_SUCCESS(rv, rv);
00211 
00212     nsCOMPtr<nsIRDFResource> filterListResource;
00213     rdf->GetResource(folderUri, getter_AddRefs(filterListResource));
00214     NS_ENSURE_SUCCESS(rv, rv);
00215 
00216     return filterListResource->GetDelegate("filter",
00217                                            NS_GET_IID(nsIMsgFilterList),
00218                                            (void **)aResult);
00219 }
00220