Back to index

lightning-sunbird  0.9+nobinonly
nsMsgRDFDataSource.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsMsgRDFDataSource.h"
00039 #include "nsRDFCID.h"
00040 #include "rdf.h"
00041 #include "plstr.h"
00042 #include "nsXPIDLString.h"
00043 #include "nsMsgRDFUtils.h"
00044 #include "nsEnumeratorUtils.h"
00045 #include "nsIObserverService.h"
00046 
00047 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
00048 
00049 nsMsgRDFDataSource::nsMsgRDFDataSource():
00050     m_shuttingDown(PR_FALSE),
00051     mInitialized(PR_FALSE),
00052     mRDFService(nsnull)
00053 {
00054     // do one-time initialization here
00055     
00056     NS_NewISupportsArray(getter_AddRefs(mObservers));
00057 }
00058 
00059 nsMsgRDFDataSource::~nsMsgRDFDataSource()
00060 {
00061     // final shutdown happens here
00062     NS_ASSERTION(!mInitialized, "Object going away without cleanup, possibly dangerous!");
00063     if (mInitialized) Cleanup();
00064 }
00065 
00066 /* initialization happens here - object is constructed,
00067    but possibly partially shut down
00068 */
00069 nsresult
00070 nsMsgRDFDataSource::Init()
00071 {
00072     nsresult rv=NS_OK;
00073 
00074     if (mInitialized)
00075         return NS_ERROR_ALREADY_INITIALIZED;
00076     
00077     /* Add an observer to XPCOM shutdown */
00078     nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1",
00079                                                      &rv);
00080     if (NS_FAILED(rv)) return rv;
00081     rv = obs->AddObserver(NS_STATIC_CAST(nsIObserver*, this), NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_TRUE);
00082     if (NS_FAILED(rv)) return rv;
00083 
00084     /* Get and keep the rdf service. Will be released by the observer */
00085     getRDFService();
00086     
00087     mInitialized=PR_TRUE;
00088     return rv;
00089 }
00090 
00091 // clean yourself up - undo anything you did in Init()
00092 void nsMsgRDFDataSource::Cleanup()
00093 {
00094     nsresult rv;
00095     mRDFService = nsnull;
00096     
00097     // release ourselves from the observer service
00098     nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1",
00099                                                      &rv);
00100     if (NS_SUCCEEDED(rv)) {
00101         rv = obs->RemoveObserver(NS_STATIC_CAST(nsIObserver*, this),
00102                                  NS_XPCOM_SHUTDOWN_OBSERVER_ID);
00103     }
00104     
00105     // release the window
00106        mWindow = nsnull;
00107 
00108     mInitialized = PR_FALSE;
00109 }
00110 
00111 NS_IMPL_ADDREF(nsMsgRDFDataSource)
00112 NS_IMPL_RELEASE(nsMsgRDFDataSource)
00113 
00114 NS_INTERFACE_MAP_BEGIN(nsMsgRDFDataSource)
00115        NS_INTERFACE_MAP_ENTRY(nsIRDFDataSource)
00116        NS_INTERFACE_MAP_ENTRY(nsIObserver)
00117        NS_INTERFACE_MAP_ENTRY(nsIMsgRDFDataSource)
00118        NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00119        NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIRDFDataSource)
00120 NS_INTERFACE_MAP_END
00121 
00122 
00123 /* readonly attribute string URI; */
00124 NS_IMETHODIMP
00125 nsMsgRDFDataSource::GetURI(char * *aURI)
00126 {
00127     NS_NOTREACHED("should be implemented by a subclass");
00128     return NS_ERROR_UNEXPECTED;
00129 }
00130 
00131 
00132 /* nsIRDFResource GetSource (in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00133 NS_IMETHODIMP
00134 nsMsgRDFDataSource::GetSource(nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue, nsIRDFResource **_retval)
00135 {
00136     return NS_RDF_NO_VALUE;
00137 }
00138 
00139 
00140 /* nsISimpleEnumerator GetSources (in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00141 NS_IMETHODIMP
00142 nsMsgRDFDataSource::GetSources(nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue, nsISimpleEnumerator **_retval)
00143 {
00144     return NS_RDF_NO_VALUE;
00145 }
00146 
00147 
00148 /* nsIRDFNode GetTarget (in nsIRDFResource aSource, in nsIRDFResource aProperty, in boolean aTruthValue); */
00149 NS_IMETHODIMP
00150 nsMsgRDFDataSource::GetTarget(nsIRDFResource *aSource, nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **_retval)
00151 {
00152     return NS_RDF_NO_VALUE;
00153 }
00154 
00155 
00156 /* nsISimpleEnumerator GetTargets (in nsIRDFResource aSource, in nsIRDFResource aProperty, in boolean aTruthValue); */
00157 NS_IMETHODIMP
00158 nsMsgRDFDataSource::GetTargets(nsIRDFResource *aSource, nsIRDFResource *aProperty, PRBool aTruthValue, nsISimpleEnumerator **_retval)
00159 {
00160     return NS_RDF_NO_VALUE;
00161 }
00162 
00163 
00164 /* void Assert (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00165 NS_IMETHODIMP
00166 nsMsgRDFDataSource::Assert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue)
00167 {
00168     return NS_RDF_NO_VALUE;
00169 }
00170 
00171 
00172 /* void Unassert (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget); */
00173 NS_IMETHODIMP
00174 nsMsgRDFDataSource::Unassert(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget)
00175 {
00176     return NS_RDF_NO_VALUE;
00177 }
00178 
00179 
00180 NS_IMETHODIMP
00181 nsMsgRDFDataSource::Change(nsIRDFResource *aSource,
00182                            nsIRDFResource *aProperty,
00183                            nsIRDFNode *aOldTarget,
00184                            nsIRDFNode *aNewTarget)
00185 {
00186     return NS_RDF_NO_VALUE;
00187 }
00188 
00189 NS_IMETHODIMP
00190 nsMsgRDFDataSource::Move(nsIRDFResource *aOldSource,
00191                          nsIRDFResource *aNewSource,
00192                          nsIRDFResource *aProperty,
00193                          nsIRDFNode *aTarget)
00194 {
00195     return NS_RDF_NO_VALUE;
00196 }
00197 
00198 
00199 /* boolean HasAssertion (in nsIRDFResource aSource, in nsIRDFResource aProperty, in nsIRDFNode aTarget, in boolean aTruthValue); */
00200 NS_IMETHODIMP
00201 nsMsgRDFDataSource::HasAssertion(nsIRDFResource *aSource, nsIRDFResource *aProperty, nsIRDFNode *aTarget, PRBool aTruthValue, PRBool *_retval)
00202 {
00203     *_retval = PR_FALSE;
00204     return NS_OK;
00205 }
00206 
00207 
00208 /* void AddObserver (in nsIRDFObserver aObserver); */
00209 NS_IMETHODIMP
00210 nsMsgRDFDataSource::AddObserver(nsIRDFObserver *aObserver)
00211 {
00212     // make sure we're initialized
00213     if (!mInitialized)
00214         Init();
00215     
00216     NS_ASSERTION(mObservers->IndexOf(aObserver) == -1,
00217                  "better not already be observing this");
00218 
00219     mObservers->AppendElement(aObserver);
00220     return NS_OK;
00221 }
00222 
00223 
00224 /* void RemoveObserver (in nsIRDFObserver aObserver); */
00225 NS_IMETHODIMP
00226 nsMsgRDFDataSource::RemoveObserver(nsIRDFObserver *aObserver)
00227 {
00228   if (! mObservers)
00229     return NS_OK;
00230   mObservers->RemoveElement(aObserver);
00231 
00232   // when we hit 0 observers, then it's probably time
00233   // to go away - clean ourselves up now
00234   PRUint32 count;
00235   mObservers->Count(&count);
00236   if (count == 0)
00237       Cleanup();
00238 
00239   return NS_OK;
00240 }
00241 
00242 
00243 NS_IMETHODIMP 
00244 nsMsgRDFDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, PRBool *result)
00245 {
00246   *result = PR_FALSE;
00247   return NS_OK;
00248 }
00249 
00250 NS_IMETHODIMP 
00251 nsMsgRDFDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, PRBool *result)
00252 {
00253   *result = PR_FALSE;
00254   return NS_OK;
00255 }
00256 
00257 /* nsISimpleEnumerator ArcLabelsIn (in nsIRDFNode aNode); */
00258 NS_IMETHODIMP
00259 nsMsgRDFDataSource::ArcLabelsIn(nsIRDFNode *aNode, nsISimpleEnumerator **_retval)
00260 {
00261  //return empty enumerator
00262   nsCOMPtr<nsISupportsArray> arcs;
00263 
00264   nsresult rv = NS_NewISupportsArray(getter_AddRefs(arcs));
00265   if(NS_FAILED(rv))
00266          return rv;
00267 
00268   return NS_NewArrayEnumerator(_retval, arcs);
00269 }
00270 
00271 
00272 /* nsISimpleEnumerator ArcLabelsOut (in nsIRDFResource aSource); */
00273 NS_IMETHODIMP
00274 nsMsgRDFDataSource::ArcLabelsOut(nsIRDFResource *aSource, nsISimpleEnumerator **_retval)
00275 {
00276     return NS_RDF_NO_VALUE;
00277 }
00278 
00279 
00280 /* nsISimpleEnumerator GetAllResources (); */
00281 NS_IMETHODIMP
00282 nsMsgRDFDataSource::GetAllResources(nsISimpleEnumerator **_retval)
00283 {
00284     return NS_RDF_NO_VALUE;
00285 }
00286 
00287 
00288 /* nsISimpleEnumerator GetAllCmds (in nsIRDFResource aSource); */
00289 NS_IMETHODIMP
00290 nsMsgRDFDataSource::GetAllCmds(nsIRDFResource *aSource, nsISimpleEnumerator **_retval)
00291 {
00292     return NS_RDF_NO_VALUE;
00293 }
00294 
00295 
00296 /* boolean IsCommandEnabled (in nsISupportsArray aSources, in nsIRDFResource aCommand, in nsISupportsArray aArguments); */
00297 NS_IMETHODIMP
00298 nsMsgRDFDataSource::IsCommandEnabled(nsISupportsArray *aSources, nsIRDFResource *aCommand, nsISupportsArray *aArguments, PRBool *_retval)
00299 {
00300     return NS_RDF_NO_VALUE;
00301 }
00302 
00303 
00304 /* void DoCommand (in nsISupportsArray aSources, in nsIRDFResource aCommand, in nsISupportsArray aArguments); */
00305 NS_IMETHODIMP
00306 nsMsgRDFDataSource::DoCommand(nsISupportsArray *aSources, nsIRDFResource *aCommand, nsISupportsArray *aArguments)
00307 {
00308     return NS_RDF_NO_VALUE;
00309 }
00310 
00311 /* void BeginUpdateBatch (); */
00312 NS_IMETHODIMP
00313 nsMsgRDFDataSource::BeginUpdateBatch()
00314 {
00315     return NS_OK;
00316 }
00317 
00318 /* void EndUpdateBatch (); */
00319 NS_IMETHODIMP
00320 nsMsgRDFDataSource::EndUpdateBatch()
00321 {
00322     return NS_OK;
00323 }
00324 
00325 
00326 /* XPCOM Shutdown observer */
00327 NS_IMETHODIMP
00328 nsMsgRDFDataSource::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData )
00329 {
00330   if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
00331          m_shuttingDown = PR_TRUE;
00332          Cleanup();
00333   }
00334        return NS_OK;
00335 }
00336 
00337 
00338 NS_IMETHODIMP nsMsgRDFDataSource::GetWindow(nsIMsgWindow * *aWindow)
00339 {
00340        if(!aWindow)
00341               return NS_ERROR_NULL_POINTER;
00342 
00343        *aWindow = mWindow;
00344        NS_IF_ADDREF(*aWindow);
00345        return NS_OK;
00346 }
00347 
00348 NS_IMETHODIMP nsMsgRDFDataSource::SetWindow(nsIMsgWindow * aWindow)
00349 {
00350        mWindow = aWindow;
00351        return NS_OK;
00352 }
00353 
00354 
00355 nsIRDFService *
00356 nsMsgRDFDataSource::getRDFService()
00357 {
00358     if (!mRDFService && !m_shuttingDown) {
00359         nsresult rv;
00360         mRDFService = do_GetService(kRDFServiceCID, &rv);
00361         if (NS_FAILED(rv)) return nsnull;
00362     }
00363     
00364     return mRDFService;
00365 }
00366 
00367 nsresult nsMsgRDFDataSource::NotifyPropertyChanged(nsIRDFResource *resource,
00368                                                    nsIRDFResource *propertyResource,
00369                                                    nsIRDFNode *newNode,
00370                                                    nsIRDFNode *oldNode /* = nsnull */)
00371 {
00372 
00373   NotifyObservers(resource, propertyResource, newNode, oldNode, PR_FALSE, PR_TRUE);
00374   return NS_OK;
00375 
00376 }
00377 
00378 nsresult nsMsgRDFDataSource::NotifyObservers(nsIRDFResource *subject,
00379                                                 nsIRDFResource *property,
00380                                                 nsIRDFNode *newObject,
00381                                                 nsIRDFNode *oldObject,
00382                                                 PRBool assert, PRBool change)
00383 {
00384     NS_ASSERTION(!(change && assert),
00385                  "Can't change and assert at the same time!\n");
00386     
00387        if(mObservers)
00388        {
00389               nsMsgRDFNotification note = { this, subject, property, newObject, oldObject };
00390               if(change)
00391                      mObservers->EnumerateForwards(changeEnumFunc, &note);
00392               else if (assert)
00393                      mObservers->EnumerateForwards(assertEnumFunc, &note);
00394               else
00395                      mObservers->EnumerateForwards(unassertEnumFunc, &note);
00396   }
00397        return NS_OK;
00398 }
00399 
00400 PRBool
00401 nsMsgRDFDataSource::assertEnumFunc(nsISupports *aElement, void *aData)
00402 {
00403   nsMsgRDFNotification *note = (nsMsgRDFNotification *)aData;
00404   nsIRDFObserver* observer = (nsIRDFObserver *)aElement;
00405   
00406   observer->OnAssert(note->datasource,
00407                      note->subject,
00408                      note->property,
00409                      note->newObject);
00410   return PR_TRUE;
00411 }
00412 
00413 PRBool
00414 nsMsgRDFDataSource::unassertEnumFunc(nsISupports *aElement, void *aData)
00415 {
00416   nsMsgRDFNotification* note = (nsMsgRDFNotification *)aData;
00417   nsIRDFObserver* observer = (nsIRDFObserver *)aElement;
00418 
00419   observer->OnUnassert(note->datasource,
00420                        note->subject,
00421                        note->property,
00422                        note->newObject);
00423   return PR_TRUE;
00424 }
00425 
00426 PRBool
00427 nsMsgRDFDataSource::changeEnumFunc(nsISupports *aElement, void *aData)
00428 {
00429   nsMsgRDFNotification* note = (nsMsgRDFNotification *)aData;
00430   nsIRDFObserver* observer = (nsIRDFObserver *)aElement;
00431 
00432   observer->OnChange(note->datasource,
00433                      note->subject,
00434                      note->property,
00435                      note->oldObject, note->newObject);
00436   return PR_TRUE;
00437 }
00438 nsresult 
00439 nsMsgRDFDataSource::GetTransactionManager(nsISupportsArray *aSources, nsITransactionManager **aTransactionManager)
00440 {
00441   if(!aTransactionManager)
00442     return NS_ERROR_NULL_POINTER;
00443   
00444   *aTransactionManager = nsnull;
00445   nsresult rv = NS_OK;
00446   
00447   nsCOMPtr<nsITransactionManager> transactionManager;
00448   
00449   PRUint32 cnt;
00450   
00451   rv = aSources->Count(&cnt);
00452   if (NS_FAILED(rv)) return rv;
00453   
00454   if (cnt > 0)
00455   {
00456     transactionManager = do_QueryElementAt(aSources, 0, &rv);
00457     if (NS_SUCCEEDED(rv) && transactionManager)
00458     {
00459       aSources->RemoveElementAt(0);
00460       NS_IF_ADDREF(*aTransactionManager = transactionManager);
00461     }
00462   }
00463   
00464   return NS_OK;      
00465 }
00466