Back to index

lightning-sunbird  0.9+nobinonly
nsMsgFilterDataSource.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 Communicator client 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 "nsMsgFilterDataSource.h"
00040 #include "nsMsgRDFUtils.h"
00041 #include "nsEnumeratorUtils.h"
00042 
00043 #include "nsIMsgFilter.h"
00044 #include "nsIMsgFilterList.h"
00045 
00046 #include "nsXPIDLString.h"
00047 #include "nsReadableUtils.h"
00048 
00049 #define NC_RDF_ENABLED NC_NAMESPACE_URI "Enabled"
00050 
00051 nsrefcnt nsMsgFilterDataSource::mGlobalRefCount = 0;
00052 nsCOMPtr<nsIRDFResource> nsMsgFilterDataSource::kNC_Child;
00053 nsCOMPtr<nsIRDFResource> nsMsgFilterDataSource::kNC_Name;
00054 nsCOMPtr<nsIRDFResource> nsMsgFilterDataSource::kNC_Enabled;
00055 
00056 nsCOMPtr<nsIRDFLiteral> nsMsgFilterDataSource::kTrueLiteral;
00057 
00058 nsCOMPtr<nsISupportsArray> nsMsgFilterDataSource::mFilterListArcsOut;
00059 nsCOMPtr<nsISupportsArray> nsMsgFilterDataSource::mFilterArcsOut;
00060 
00061 
00062 
00063 nsMsgFilterDataSource::nsMsgFilterDataSource()
00064 {
00065     if (mGlobalRefCount++ == 0)
00066         initGlobalObjects(getRDFService());
00067 }
00068 
00069 nsMsgFilterDataSource::~nsMsgFilterDataSource()
00070 {
00071     mGlobalRefCount--;
00072     if (mGlobalRefCount == 0)
00073         cleanupGlobalObjects();
00074 }
00075 
00076 nsresult
00077 nsMsgFilterDataSource::cleanupGlobalObjects()
00078 {
00079     mFilterListArcsOut = nsnull;
00080     mFilterArcsOut = nsnull;
00081     kNC_Child = nsnull;
00082     kNC_Name = nsnull;
00083     kNC_Enabled = nsnull;
00084     kTrueLiteral = nsnull;
00085     return NS_OK;
00086 }
00087 
00088 nsresult
00089 nsMsgFilterDataSource::initGlobalObjects(nsIRDFService *rdf)
00090 {
00091     rdf->GetLiteral(NS_LITERAL_STRING("true").get(),
00092                     getter_AddRefs(kTrueLiteral));
00093     
00094     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_CHILD), getter_AddRefs(kNC_Child));
00095     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_NAME), getter_AddRefs(kNC_Name));
00096     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_ENABLED), getter_AddRefs(kNC_Enabled));
00097 
00098     NS_NewISupportsArray(getter_AddRefs(mFilterListArcsOut));
00099     mFilterListArcsOut->AppendElement(kNC_Child);
00100   
00101     NS_NewISupportsArray(getter_AddRefs(mFilterArcsOut));
00102     mFilterArcsOut->AppendElement(kNC_Name);
00103     mFilterArcsOut->AppendElement(kNC_Enabled);
00104   
00105     return NS_OK;
00106 }
00107 
00108 NS_IMETHODIMP
00109 nsMsgFilterDataSource::GetTargets(nsIRDFResource *aSource,
00110                                   nsIRDFResource *aProperty,
00111                                   PRBool aTruthValue,
00112                                   nsISimpleEnumerator **aResult)
00113 {
00114     nsresult rv;
00115     
00116     nsCOMPtr<nsISupportsArray> resourceList;
00117     rv = NS_NewISupportsArray(getter_AddRefs(resourceList));
00118     NS_ENSURE_SUCCESS(rv, rv);
00119 
00120     nsCOMPtr<nsISupports> filterDelegate;
00121     
00122     // first see if it's a filter list
00123     rv = aSource->GetDelegate("filter", NS_GET_IID(nsISupports),
00124                              (void **)getter_AddRefs(filterDelegate));
00125 
00126     if (NS_FAILED(rv)) {
00127         // no filter delegate
00128         nsSingletonEnumerator *cursor = new nsSingletonEnumerator(nsnull);
00129         NS_ENSURE_TRUE(cursor, NS_ERROR_OUT_OF_MEMORY);
00130         
00131         *aResult = cursor;
00132         NS_ADDREF(*aResult);
00133         return NS_OK;
00134     }
00135         
00136 
00137     //
00138     // nsIMsgFilterList
00139     //
00140     nsCOMPtr<nsIMsgFilterList> filterList =
00141         do_QueryInterface(filterDelegate, &rv);
00142     
00143     if (NS_SUCCEEDED(rv)) {
00144         rv = getFilterListTargets(filterList, aSource, aProperty,
00145                                   aTruthValue, resourceList);
00146     }
00147     else {
00148 
00149         //
00150         // nsIMsgFilter
00151         //
00152         nsCOMPtr<nsIMsgFilter> filter =
00153             do_QueryInterface(filterDelegate, &rv);
00154 
00155         if (NS_SUCCEEDED(rv)) {
00156             // filters do not have multiple targets, right?
00157             // do we have to call GetTarget and return the result?
00158         }
00159 
00160         else {
00161             NS_WARNING("ArcLabelsOut(): unknown filter delegate!\n");
00162         }
00163     }
00164     
00165     nsArrayEnumerator *cursor = new nsArrayEnumerator(resourceList);
00166     NS_ENSURE_TRUE(cursor, NS_ERROR_OUT_OF_MEMORY);
00167     
00168     *aResult = cursor;
00169     NS_ADDREF(*aResult);
00170     return NS_OK;
00171 }
00172 
00173 NS_IMETHODIMP
00174 nsMsgFilterDataSource::GetTarget(nsIRDFResource *aSource,
00175                                  nsIRDFResource *aProperty,
00176                                  PRBool aTruthValue,
00177                                  nsIRDFNode **aResult)
00178 {
00179     nsresult rv;
00180     *aResult = nsnull;
00181 
00182     
00183     nsCOMPtr<nsISupports> filterDelegate;
00184     aSource->GetDelegate("filter", NS_GET_IID(nsISupports),
00185                          (void **)getter_AddRefs(filterDelegate));
00186 
00187     //
00188     // nsIMsgFilterList
00189     //
00190     nsCOMPtr<nsIMsgFilterList> filterList =
00191         do_QueryInterface(filterDelegate, &rv);
00192 
00193     if (NS_SUCCEEDED(rv))
00194         rv = getFilterListTarget(filterList, aProperty, aTruthValue, aResult);
00195     else {
00196 
00197         //
00198         // nsIMsgFilter
00199         //
00200         nsCOMPtr<nsIMsgFilter> filter =
00201             do_QueryInterface(filterDelegate, &rv);
00202         if (NS_SUCCEEDED(rv))
00203             rv = getFilterTarget(filter, aProperty, aTruthValue, aResult);
00204         
00205         else {
00206             NS_WARNING("GetTarget(): unknown filter delegate!\n");
00207         }
00208     }
00209 
00210     if (*aResult)
00211         return NS_OK;
00212 
00213     return NS_RDF_NO_VALUE;
00214 }
00215 
00216 
00217 NS_IMETHODIMP
00218 nsMsgFilterDataSource::ArcLabelsOut(nsIRDFResource *aSource,
00219                                     nsISimpleEnumerator **aResult)
00220 {
00221     nsresult rv;
00222     nsCOMPtr<nsISupportsArray> arcs;
00223 
00224     nsCOMPtr<nsISupports> filterDelegate;
00225     
00226     rv = aSource->GetDelegate("filter", NS_GET_IID(nsISupports),
00227                               (void **)getter_AddRefs(filterDelegate));
00228     
00229     if (NS_FAILED(rv)) return NS_RDF_NO_VALUE;
00230 
00231     //
00232     // nsIMsgFilterList
00233     //
00234     nsCOMPtr<nsIMsgFilterList> filterList =
00235         do_QueryInterface(filterDelegate, &rv);
00236     
00237     if (NS_SUCCEEDED(rv)) {
00238         arcs = mFilterListArcsOut;
00239     
00240     } else {
00241 
00242         //
00243         // nsIMsgFilter
00244         //
00245         nsCOMPtr<nsIMsgFilter> filter =
00246             do_QueryInterface(filterDelegate, &rv);
00247         
00248         if (NS_SUCCEEDED(rv))
00249             arcs = mFilterArcsOut;
00250         
00251         else {
00252             NS_WARNING("GetTargets(): unknown filter delegate!\n");
00253 
00254         }
00255     }
00256 
00257     if (!arcs) {
00258         *aResult = nsnull;
00259         return NS_RDF_NO_VALUE;
00260     }
00261 
00262     nsArrayEnumerator* enumerator =
00263         new nsArrayEnumerator(arcs);
00264     NS_ENSURE_TRUE(enumerator, NS_ERROR_OUT_OF_MEMORY);
00265   
00266     *aResult = enumerator;
00267     NS_ADDREF(*aResult);
00268 
00269     return NS_OK;
00270 }
00271 
00272 // takes a base resource, like mailbox://username@host/folder and returns
00273 // the array that corresponds to all the filters.
00274 // it does this quickly because it knows the sub-filters will be in the form
00275 //
00276 // mailbox://username@host/folder;filterName=abc
00277 // mailbox://username@host/folder;filterName=cde
00278 // mailbox://username@host/folder;filterName=efg
00279 // mailbox://username@host/folder;filterName=ghi
00280 //
00281 // and so forth
00282 nsresult
00283 nsMsgFilterDataSource::getFilterListTargets(nsIMsgFilterList *aFilterList,
00284                                             nsIRDFResource *aSource,
00285                                             nsIRDFResource *aProperty,
00286                                             PRBool aTruthValue,
00287                                             nsISupportsArray *aResult)
00288 {
00289     nsresult rv;
00290 
00291     // get the URI of the source resource, say mailbox://username@host/folder
00292     // from there we'll append ";filter", and then enumerate the
00293     // filters to get all the resources like
00294     // mailbox://username@host/folder;filterName=abcd
00295 
00296     // a better way to do this might be to ask the filter
00297     // what folder it's in. Then we wouldn't need aSource
00298     const char* filterListUri;
00299     aSource->GetValueConst(&filterListUri);
00300 
00301     nsCAutoString filterUri(filterListUri);
00302     filterUri.Append(";filterName=");
00303 
00304     // we'll use the length of this base string to truncate the string later
00305     PRInt32 baseFilterUriLength = filterUri.Length();
00306 
00307     // now start looping through the filters
00308     // we're not actually touching any filters here, just creating
00309     // resources that correspond to them.
00310     PRUint32 filterCount;
00311     rv = aFilterList->GetFilterCount(&filterCount);
00312     if (NS_FAILED(rv)) return rv;
00313 
00314     PRUint32 i;
00315     for (i=0; i<filterCount; i++) {
00316         nsCOMPtr<nsIMsgFilter> filter;
00317         rv = aFilterList->GetFilterAt(i, getter_AddRefs(filter));
00318         if (NS_FAILED(rv)) continue;
00319 
00320         PRBool isTemporary;
00321         rv = filter->GetTemporary(&isTemporary);
00322         NS_ASSERTION(NS_SUCCEEDED(rv), "GetTemporary() failed");
00323         if (isTemporary)
00324           continue;
00325 
00326         nsXPIDLString filterName;
00327         rv = filter->GetFilterName(getter_Copies(filterName));
00328         if (NS_FAILED(rv)) return rv;
00329 
00330         nsAutoString filterString(filterName);
00331 
00332         char *utf8Name = ToNewUTF8String(filterString);
00333         filterUri.Append(utf8Name);
00334         Recycle(utf8Name);
00335         
00336         nsCOMPtr<nsIRDFResource> filterResource;
00337         rv = getRDFService()->GetResource(filterUri,
00338                               getter_AddRefs(filterResource));
00339         if (NS_SUCCEEDED(rv))
00340             aResult->AppendElement(filterResource);
00341 
00342         // now reduce the URI back to the base uri
00343         filterUri.Truncate(baseFilterUriLength);
00344     }
00345     
00346     return NS_OK;
00347 }
00348 
00349 nsresult
00350 nsMsgFilterDataSource::getFilterListTarget(nsIMsgFilterList *aFilterList,
00351                                            nsIRDFResource *aProperty,
00352                                            PRBool aTruthValue,
00353                                            nsIRDFNode **aResult)
00354 {
00355 
00356     // here we probably need to answer to the #child property
00357     // so that RDF realizes this is a container
00358     return NS_ERROR_NOT_IMPLEMENTED;
00359 }
00360 
00361 
00362 nsresult
00363 nsMsgFilterDataSource::getFilterTarget(nsIMsgFilter *aFilter,
00364                                        nsIRDFResource *aProperty,
00365                                        PRBool aTruthValue,
00366                                        nsIRDFNode **aResult)
00367 {
00368 
00369     if (aProperty == kNC_Name.get()) {
00370         nsXPIDLString filterName;
00371         aFilter->GetFilterName(getter_Copies(filterName));
00372         return createNode((const PRUnichar*)filterName, aResult,
00373                           getRDFService());
00374         
00375     } else if (aProperty == kNC_Enabled.get()) {
00376         PRBool enabled;
00377         aFilter->GetEnabled(&enabled);
00378         if (enabled) {
00379             *aResult = kTrueLiteral;
00380             NS_ADDREF(*aResult);
00381             return NS_OK;
00382         }
00383     }
00384     
00385     return NS_RDF_NO_VALUE;
00386 }
00387 
00388