Back to index

lightning-sunbird  0.9+nobinonly
nsSmtpDataSource.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 "nsXPIDLString.h"
00040 #include "nsEnumeratorUtils.h"
00041 #include "nsRDFCID.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsCRT.h"
00044 
00045 #include "nsISmtpService.h"
00046 #include "nsISmtpServer.h"
00047 #include "nsSmtpDataSource.h"
00048 #include "nsMsgCompCID.h"
00049 
00050 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
00051 
00052 #define NC_RDF_SMTPSERVERS "NC:smtpservers"
00053 #define NC_RDF_ISDEFAULT        NC_NAMESPACE_URI "IsDefaultServer"
00054 #define NC_RDF_ISSESSIONDEFAULT NC_NAMESPACE_URI "IsSessionDefaultServer"
00055 
00056 nsrefcnt nsSmtpDataSource::gRefCount = 0;
00057 nsCOMPtr<nsIRDFResource> nsSmtpDataSource::kNC_Child;
00058 nsCOMPtr<nsIRDFResource> nsSmtpDataSource::kNC_Name;
00059 nsCOMPtr<nsIRDFResource> nsSmtpDataSource::kNC_Key;
00060 nsCOMPtr<nsIRDFResource> nsSmtpDataSource::kNC_SmtpServers;
00061 nsCOMPtr<nsIRDFResource> nsSmtpDataSource::kNC_IsDefaultServer;
00062 nsCOMPtr<nsIRDFResource> nsSmtpDataSource::kNC_IsSessionDefaultServer;
00063 
00064 nsCOMPtr<nsIRDFLiteral> nsSmtpDataSource::kTrueLiteral;
00065 
00066 nsCOMPtr<nsISupportsArray> nsSmtpDataSource::mServerArcsOut;
00067 nsCOMPtr<nsISupportsArray> nsSmtpDataSource::mServerRootArcsOut;
00068 
00069 NS_IMPL_ISUPPORTS1(nsSmtpDataSource, nsIRDFDataSource)
00070 
00071 nsSmtpDataSource::nsSmtpDataSource()
00072 {
00073     gRefCount++;
00074     if (gRefCount == 1)
00075         initGlobalObjects();
00076 }
00077 
00078 nsSmtpDataSource::~nsSmtpDataSource()
00079 {
00080   
00081 }
00082 
00083 nsresult
00084 nsSmtpDataSource::initGlobalObjects()
00085 {
00086     nsresult rv;
00087     nsCOMPtr<nsIRDFService> rdf = do_GetService(kRDFServiceCID, &rv);
00088     if (NS_FAILED(rv)) return rv;
00089 
00090     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_CHILD), getter_AddRefs(kNC_Child));
00091     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_NAME), getter_AddRefs(kNC_Name));
00092     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_KEY), getter_AddRefs(kNC_Key));
00093     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_SMTPSERVERS),
00094                      getter_AddRefs(kNC_SmtpServers));
00095     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_ISDEFAULT),
00096                      getter_AddRefs(kNC_IsDefaultServer));
00097     rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_ISSESSIONDEFAULT),
00098                      getter_AddRefs(kNC_IsSessionDefaultServer));
00099 
00100     nsAutoString trueStr(NS_LITERAL_STRING("true"));
00101     rdf->GetLiteral(trueStr.get(), getter_AddRefs(kTrueLiteral));
00102 
00103     // now create cached arrays for each type we support
00104     rv = NS_NewISupportsArray(getter_AddRefs(mServerArcsOut));
00105     if (NS_FAILED(rv)) return rv;
00106     mServerArcsOut->AppendElement(kNC_Name);
00107     mServerArcsOut->AppendElement(kNC_Key);
00108     mServerArcsOut->AppendElement(kNC_IsDefaultServer);
00109     mServerArcsOut->AppendElement(kNC_IsSessionDefaultServer);
00110 
00111     rv = NS_NewISupportsArray(getter_AddRefs(mServerRootArcsOut));
00112     mServerRootArcsOut->AppendElement(kNC_Child);
00113     mServerRootArcsOut->AppendElement(kNC_SmtpServers);
00114     
00115     return NS_OK;
00116 }
00117 
00118 /* readonly attribute string URI; */
00119 NS_IMETHODIMP nsSmtpDataSource::GetURI(char * *aURI)
00120 {
00121     *aURI = nsCRT::strdup("NC:smtpservers");
00122     return NS_OK;
00123 }
00124 
00125 /* nsIRDFResource GetSource (in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00126 NS_IMETHODIMP nsSmtpDataSource::GetSource(nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue, nsIRDFResource **aResult)
00127 {
00128     return NS_ERROR_NOT_IMPLEMENTED;
00129 }
00130 
00131 /* nsISimpleEnumerator GetSources (in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00132 NS_IMETHODIMP nsSmtpDataSource::GetSources(nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue, nsISimpleEnumerator **aResult)
00133 {
00134     return NS_ERROR_NOT_IMPLEMENTED;
00135 }
00136 
00137 /* nsIRDFNode GetTarget (in nsIRDFResource aSource, in nsIRDFResource aProperty, in boolean aTruthValue); */
00138 NS_IMETHODIMP 
00139 nsSmtpDataSource::GetTarget(nsIRDFResource *aSource,
00140                             nsIRDFResource *aProperty, 
00141                             PRBool aTruthValue, nsIRDFNode **aResult)
00142 {
00143     nsresult rv;
00144     nsXPIDLCString str;
00145         
00146     *aResult = nsnull;
00147     
00148     nsCOMPtr<nsISmtpServer> smtpServer;
00149     rv = aSource->GetDelegate("smtpserver", NS_GET_IID(nsISmtpServer),
00150                               (void **)getter_AddRefs(smtpServer));
00151     
00152     if (NS_FAILED(rv))
00153         return NS_RDF_NO_VALUE;
00154 
00155     if (aProperty == kNC_Name.get() ||
00156         aProperty == kNC_Key.get()) {
00157     
00158         if (aProperty == kNC_Name.get()) {
00159             smtpServer->GetDisplayname(getter_Copies(str));
00160         } else if (aProperty == kNC_Key.get()) {
00161             smtpServer->GetKey(getter_Copies(str));
00162         }
00163         
00164         nsCOMPtr<nsIRDFService> rdf =
00165             do_GetService(kRDFServiceCID, &rv);
00166         NS_ENSURE_SUCCESS(rv, rv);
00167         
00168         nsAutoString unicodeString;
00169         unicodeString.AssignWithConversion((const char*)str);
00170         
00171         nsCOMPtr<nsIRDFLiteral> literalResult;
00172         rv = rdf->GetLiteral(unicodeString.get(), getter_AddRefs(literalResult));
00173         NS_ENSURE_SUCCESS(rv, rv);
00174         
00175         *aResult = literalResult;
00176         NS_ADDREF(*aResult);
00177     }
00178 
00179     else if (aProperty == kNC_IsDefaultServer.get() ||
00180              aProperty == kNC_IsSessionDefaultServer.get()) {
00181 
00182         nsCOMPtr<nsISmtpService> smtpService =
00183             do_GetService(NS_SMTPSERVICE_CONTRACTID, &rv);
00184 
00185         NS_ENSURE_SUCCESS(rv, rv);
00186         
00187         PRBool truthValue = PR_FALSE;
00188         nsCOMPtr<nsISmtpServer> testServer;
00189         
00190         if (aProperty == kNC_IsDefaultServer.get()) {
00191             printf("Checking for default..");
00192             smtpService->GetDefaultServer(getter_AddRefs(testServer));
00193         }
00194 
00195         else if (aProperty == kNC_IsSessionDefaultServer.get()) {
00196             printf("checking for session default..");
00197             smtpService->GetSessionDefaultServer(getter_AddRefs(testServer));
00198         }
00199         
00200         if (testServer.get() == smtpServer.get())
00201             truthValue = PR_TRUE;
00202 
00203         printf("%s\n",  truthValue ? "TRUE" : "FALSE");
00204         if (truthValue) {
00205             *aResult = kTrueLiteral;
00206             NS_ADDREF(*aResult);
00207         }
00208     }
00209 
00210     else {
00211         printf("smtpDatasource: Unknown property\n");
00212     }
00213 
00214     return NS_OK;
00215 }
00216 
00217 /* nsISimpleEnumerator GetTargets (in nsIRDFResource aSource, in nsIRDFResource aProperty, in boolean aTruthValue); */
00218 NS_IMETHODIMP nsSmtpDataSource::GetTargets(nsIRDFResource *aSource, nsIRDFResource *aProperty, PRBool aTruthValue, nsISimpleEnumerator **aResult)
00219 {
00220     nsresult rv = NS_OK;
00221 
00222     
00223     if (aSource == kNC_SmtpServers.get() &&
00224         aProperty == kNC_Child.get()) {
00225 
00226         nsCOMPtr<nsISupportsArray> arcs;
00227         GetSmtpServerTargets(getter_AddRefs(arcs));
00228         
00229         // enumerate the smtp servers
00230         rv = NS_NewArrayEnumerator(aResult, arcs);
00231         NS_ENSURE_SUCCESS(rv, rv);
00232     } else {
00233         rv = NS_NewEmptyEnumerator(aResult);
00234     }
00235 
00236     return NS_OK;
00237 }
00238 
00239 nsresult
00240 nsSmtpDataSource::GetSmtpServerTargets(nsISupportsArray **aResultArray)
00241 {
00242     nsresult rv;
00243     nsCOMPtr<nsISmtpService> smtpService =
00244         do_GetService(NS_SMTPSERVICE_CONTRACTID, &rv);
00245     NS_ENSURE_SUCCESS(rv, rv);
00246 
00247     nsCOMPtr<nsIRDFService> rdf =
00248         do_GetService(kRDFServiceCID, &rv);
00249     
00250     nsCOMPtr<nsISupportsArray> smtpServers;
00251     rv = smtpService->GetSmtpServers(getter_AddRefs(smtpServers));
00252     NS_ENSURE_SUCCESS(rv, rv);
00253     
00254     nsCOMPtr<nsISupportsArray> smtpServerResources;
00255     rv = NS_NewISupportsArray(getter_AddRefs(smtpServerResources));
00256     
00257     PRUint32 count;
00258     rv = smtpServers->Count(&count);
00259     NS_ENSURE_SUCCESS(rv, rv);
00260 
00261     PRUint32 i;
00262     for (i=0; i<count; i++) {
00263         nsCOMPtr<nsISmtpServer> smtpServer;
00264         rv = smtpServers->QueryElementAt(i, NS_GET_IID(nsISmtpServer),
00265                                     (void **)getter_AddRefs(smtpServer));
00266         if (NS_FAILED(rv)) continue;
00267         
00268         nsXPIDLCString smtpServerUri;
00269         rv = smtpServer->GetServerURI(getter_Copies(smtpServerUri));
00270         if (NS_FAILED(rv)) continue;
00271 
00272         nsCOMPtr<nsIRDFResource> smtpServerResource;
00273         rv = rdf->GetResource(smtpServerUri, getter_AddRefs(smtpServerResource));
00274         if (NS_FAILED(rv)) continue;
00275 
00276         rv = smtpServerResources->AppendElement(smtpServerResource);
00277     }
00278 
00279     *aResultArray = smtpServerResources;
00280     NS_ADDREF(*aResultArray);
00281 
00282     return NS_OK;
00283 }
00284 
00285 /* void Assert (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00286 NS_IMETHODIMP nsSmtpDataSource::Assert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue)
00287 {
00288     return NS_ERROR_NOT_IMPLEMENTED;
00289 }
00290 
00291 /* void Unassert (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget); */
00292 NS_IMETHODIMP nsSmtpDataSource::Unassert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget)
00293 {
00294     return NS_ERROR_NOT_IMPLEMENTED;
00295 }
00296 
00297 /* void Change (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aOldTarget, in nsIRDFNode aNewTarget); */
00298 NS_IMETHODIMP nsSmtpDataSource::Change(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aOldTarget, nsIRDFNode *aNewTarget)
00299 {
00300     return NS_ERROR_NOT_IMPLEMENTED;
00301 }
00302 
00303 /* void Move (in nsIRDFResource aOldSource, in nsIRDFResource aNewSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget); */
00304 NS_IMETHODIMP nsSmtpDataSource::Move(nsIRDFResource *aOldSource, nsIRDFResource *aNewSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget)
00305 {
00306     return NS_ERROR_NOT_IMPLEMENTED;
00307 }
00308 
00309 /* boolean HasAssertion (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00310 NS_IMETHODIMP nsSmtpDataSource::HasAssertion(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue, PRBool *aResult)
00311 {
00312     return NS_ERROR_NOT_IMPLEMENTED;
00313 }
00314 
00315 /* void AddObserver (in nsIRDFObserver aObserver); */
00316 NS_IMETHODIMP nsSmtpDataSource::AddObserver(nsIRDFObserver *aObserver)
00317 {
00318     NS_PRECONDITION(aObserver != nsnull, "null ptr");
00319     if (! aObserver)
00320         return NS_ERROR_NULL_POINTER;
00321  
00322     if (! mObservers)
00323     {
00324         nsresult rv;
00325         rv = NS_NewISupportsArray(getter_AddRefs(mObservers));
00326         if (NS_FAILED(rv)) return rv;
00327     }
00328     mObservers->AppendElement(aObserver);
00329     return NS_OK;
00330 }
00331 
00332 /* void RemoveObserver (in nsIRDFObserver aObserver); */
00333 NS_IMETHODIMP nsSmtpDataSource::RemoveObserver(nsIRDFObserver *aObserver)
00334 {
00335     NS_PRECONDITION(aObserver != nsnull, "null ptr");
00336     if (! aObserver)
00337         return NS_ERROR_NULL_POINTER;
00338 
00339     if (! mObservers)
00340         return NS_OK;
00341 
00342     mObservers->RemoveElement(aObserver);
00343     return NS_OK;
00344 }
00345 
00346 NS_IMETHODIMP 
00347 nsSmtpDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, PRBool *result)
00348 {
00349   *result = PR_FALSE;
00350   return NS_OK;
00351 }
00352 
00353 NS_IMETHODIMP 
00354 nsSmtpDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, PRBool *result)
00355 {
00356   nsresult rv;
00357   if (aSource == kNC_SmtpServers.get()) {
00358       *result = mServerRootArcsOut->IndexOf(aArc) != -1;
00359   }
00360   else {
00361       nsCOMPtr<nsISmtpServer> smtpServer;
00362       rv = aSource->GetDelegate("smtpserver", NS_GET_IID(nsISmtpServer),
00363                                 (void **)getter_AddRefs(smtpServer));
00364       if (NS_SUCCEEDED(rv)) {
00365           *result = mServerArcsOut->IndexOf(aArc) != -1;
00366       }
00367       else {
00368           *result = PR_FALSE;
00369       }
00370   }
00371   return NS_OK;
00372 }
00373 
00374 /* nsISimpleEnumerator ArcLabelsIn (in nsIRDFNode aNode); */
00375 NS_IMETHODIMP nsSmtpDataSource::ArcLabelsIn(nsIRDFNode *aNode, nsISimpleEnumerator **aResult)
00376 {
00377     return NS_ERROR_NOT_IMPLEMENTED;
00378 }
00379 
00380 /* nsISimpleEnumerator ArcLabelsOut (in nsIRDFResource aSource); */
00381 NS_IMETHODIMP nsSmtpDataSource::ArcLabelsOut(nsIRDFResource *aSource, nsISimpleEnumerator **aResult)
00382 {
00383     nsresult rv;
00384 
00385     if (aSource == kNC_SmtpServers.get()) {
00386         rv = NS_NewArrayEnumerator(aResult, mServerRootArcsOut);
00387     } else {
00388     
00389         nsCOMPtr<nsISmtpServer> smtpServer;
00390         rv = aSource->GetDelegate("smtpserver", NS_GET_IID(nsISmtpServer),
00391                                   (void **)getter_AddRefs(smtpServer));
00392         if (NS_SUCCEEDED(rv)) {
00393             rv = NS_NewArrayEnumerator(aResult, mServerArcsOut);
00394         } 
00395     }
00396 
00397     if (!*aResult)
00398         rv = NS_NewEmptyEnumerator(aResult);
00399     return rv;
00400 }
00401 
00402 /* nsISimpleEnumerator GetAllResources (); */
00403 NS_IMETHODIMP nsSmtpDataSource::GetAllResources(nsISimpleEnumerator **aResult)
00404 {
00405     return NS_ERROR_NOT_IMPLEMENTED;
00406 }
00407 
00408 /* boolean IsCommandEnabled (in nsISupportsArray aSources, in nsIRDFResource aCommand, in nsISupportsArray aArguments); */
00409 NS_IMETHODIMP nsSmtpDataSource::IsCommandEnabled(nsISupportsArray *aSources, nsIRDFResource *aCommand, nsISupportsArray *aArguments, PRBool *aResult)
00410 {
00411     return NS_ERROR_NOT_IMPLEMENTED;
00412 }
00413 
00414 /* void DoCommand (in nsISupportsArray aSources, in nsIRDFResource aCommand, in nsISupportsArray aArguments); */
00415 NS_IMETHODIMP nsSmtpDataSource::DoCommand(nsISupportsArray *aSources, nsIRDFResource *aCommand, nsISupportsArray *aArguments)
00416 {
00417     return NS_ERROR_NOT_IMPLEMENTED;
00418 }
00419 
00420 /* nsISimpleEnumerator GetAllCmds (in nsIRDFResource aSource); */
00421 NS_IMETHODIMP nsSmtpDataSource::GetAllCmds(nsIRDFResource *aSource, nsISimpleEnumerator **aResult)
00422 {
00423     return NS_ERROR_NOT_IMPLEMENTED;
00424 }
00425 
00426 /* void beginUpdateBatch (); */
00427 NS_IMETHODIMP nsSmtpDataSource::BeginUpdateBatch()
00428 {
00429     return NS_OK;
00430 }
00431 
00432 /* void endUpdateBatch (); */
00433 NS_IMETHODIMP nsSmtpDataSource::EndUpdateBatch()
00434 {
00435     return NS_OK;
00436 }