Back to index

lightning-sunbird  0.9+nobinonly
nsChromeUIDataSource.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  *   Original Author: David W. Hyatt (hyatt@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 "nsCOMPtr.h"
00040 #include "nsChromeUIDataSource.h"
00041 #include "nsIRDFDataSource.h"
00042 #include "nsIRDFObserver.h"
00043 #include "nsIRDFRemoteDataSource.h"
00044 #include "nsCRT.h"
00045 #include "rdf.h"
00046 #include "nsIServiceManager.h"
00047 #include "nsIRDFService.h"
00048 #include "nsRDFCID.h"
00049 #include "nsIRDFResource.h"
00050 #include "nsIRDFDataSource.h"
00051 #include "nsIRDFContainer.h"
00052 #include "nsHashtable.h"
00053 #include "nsString.h"
00054 #include "nsXPIDLString.h"
00055 #include "nsISimpleEnumerator.h"
00056 #include "nsNetUtil.h"
00057 #include "nsISupportsArray.h"
00058 #include "nsIIOService.h"
00059 
00061 
00062 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
00063 
00064 nsChromeUIDataSource::nsChromeUIDataSource(nsIRDFDataSource* aComposite)
00065 {
00066   mComposite = aComposite;
00067   mComposite->AddObserver(this);
00068 
00069   nsresult rv;
00070   rv = CallGetService(kRDFServiceCID, &mRDFService);
00071   NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get RDF service");
00072 
00073   mRDFService->RegisterDataSource(this, PR_TRUE);
00074 }
00075 
00076 nsChromeUIDataSource::~nsChromeUIDataSource()
00077 {
00078   mRDFService->UnregisterDataSource(this);
00079 
00080   NS_IF_RELEASE(mRDFService);
00081 }
00082 
00083 // we require a special implementation of Release, which knows about
00084 // a circular strong reference
00085 NS_IMPL_ADDREF(nsChromeUIDataSource)
00086 NS_IMPL_QUERY_INTERFACE2(nsChromeUIDataSource, nsIRDFDataSource, nsIRDFObserver)
00087 
00088 NS_IMETHODIMP_(nsrefcnt)
00089 nsChromeUIDataSource::Release()
00090 {
00091   NS_PRECONDITION(PRInt32(mRefCnt) > 0, "duplicate release");
00092   --mRefCnt;
00093   NS_LOG_RELEASE(this, mRefCnt, "nsChromeUIDataSource");
00094 
00095   // delete if the last reference is our strong circular reference
00096   if (mComposite && PRInt32(mRefCnt) == 1) {
00097     mComposite->RemoveObserver(this);
00098     return 0;
00099   }
00100   else if (mRefCnt == 0) {
00101     delete this;
00102     return 0;
00103   }
00104   return mRefCnt;
00105 }
00106 
00107 
00108 //----------------------------------------------------------------------
00109 //
00110 // nsIRDFDataSource interface
00111 //
00112 
00113 NS_IMETHODIMP
00114 nsChromeUIDataSource::GetURI(char** aURI)
00115 {
00116   *aURI = nsCRT::strdup("rdf:chrome");
00117        if (! *aURI)
00118               return NS_ERROR_OUT_OF_MEMORY;
00119 
00120        return NS_OK;
00121 }
00122 
00123 NS_IMETHODIMP
00124 nsChromeUIDataSource::GetSource(nsIRDFResource* property,
00125                                 nsIRDFNode* target,
00126                                 PRBool tv,
00127                                 nsIRDFResource** source)
00128 {
00129   return mComposite->GetSource(property, target, tv, source);
00130 }
00131 
00132 NS_IMETHODIMP
00133 nsChromeUIDataSource::GetSources(nsIRDFResource* aProperty,
00134                                     nsIRDFNode* aTarget,
00135                                     PRBool aTruthValue,
00136                                     nsISimpleEnumerator** aResult)
00137 {
00138   return mComposite->GetSources(aProperty, aTarget, aTruthValue, aResult);
00139 }
00140 
00141 NS_IMETHODIMP
00142 nsChromeUIDataSource::GetTarget(nsIRDFResource* aSource,
00143                                    nsIRDFResource* aProperty,
00144                                    PRBool aTruthValue,
00145                                    nsIRDFNode** aResult)
00146 {
00147   return mComposite->GetTarget(aSource, aProperty, aTruthValue, aResult);
00148 }
00149 
00150 NS_IMETHODIMP
00151 nsChromeUIDataSource::GetTargets(nsIRDFResource* aSource,
00152                                  nsIRDFResource* aProperty,
00153                                  PRBool aTruthValue,
00154                                  nsISimpleEnumerator** aResult)
00155 {
00156   return mComposite->GetTargets(aSource, aProperty, aTruthValue, aResult);
00157 }
00158 
00159 NS_IMETHODIMP
00160 nsChromeUIDataSource::Assert(nsIRDFResource* aSource, 
00161                              nsIRDFResource* aProperty, 
00162                              nsIRDFNode* aTarget,
00163                              PRBool aTruthValue)
00164 {
00165   return mComposite->Assert(aSource, aProperty, aTarget, aTruthValue);
00166 }
00167 
00168 NS_IMETHODIMP
00169 nsChromeUIDataSource::Unassert(nsIRDFResource* aSource,
00170                                   nsIRDFResource* aProperty,
00171                                   nsIRDFNode* aTarget)
00172 {
00173   return mComposite->Unassert(aSource, aProperty, aTarget);
00174 }
00175 
00176 NS_IMETHODIMP
00177 nsChromeUIDataSource::Change(nsIRDFResource* aSource,
00178                                 nsIRDFResource* aProperty,
00179                                 nsIRDFNode* aOldTarget,
00180                                 nsIRDFNode* aNewTarget)
00181 {
00182   return mComposite->Change(aSource, aProperty, aOldTarget, aNewTarget);
00183 }
00184 
00185 NS_IMETHODIMP
00186 nsChromeUIDataSource::Move(nsIRDFResource* aOldSource,
00187                               nsIRDFResource* aNewSource,
00188                               nsIRDFResource* aProperty,
00189                               nsIRDFNode* aTarget)
00190 {
00191   return mComposite->Move(aOldSource, aNewSource, aProperty, aTarget);
00192 }
00193 
00194 
00195 NS_IMETHODIMP
00196 nsChromeUIDataSource::HasAssertion(nsIRDFResource* aSource,
00197                                       nsIRDFResource* aProperty,
00198                                       nsIRDFNode* aTarget,
00199                                       PRBool aTruthValue,
00200                                       PRBool* aResult)
00201 {
00202   return mComposite->HasAssertion(aSource, aProperty, aTarget, aTruthValue, aResult);
00203 }
00204 
00205 NS_IMETHODIMP
00206 nsChromeUIDataSource::AddObserver(nsIRDFObserver* aObserver)
00207 {
00208   NS_PRECONDITION(aObserver != nsnull, "null ptr");
00209   if (! aObserver)
00210       return NS_ERROR_NULL_POINTER;
00211 
00212   // XXX ensure uniqueness?
00213 
00214   mObservers.AppendObject(aObserver);
00215   return NS_OK;
00216 }
00217 
00218 NS_IMETHODIMP
00219 nsChromeUIDataSource::RemoveObserver(nsIRDFObserver* aObserver)
00220 {
00221   NS_PRECONDITION(aObserver != nsnull, "null ptr");
00222   if (! aObserver)
00223       return NS_ERROR_NULL_POINTER;
00224 
00225   mObservers.RemoveObject(aObserver);
00226   return NS_OK;
00227 }
00228 
00229 NS_IMETHODIMP 
00230 nsChromeUIDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, PRBool *result)
00231 {
00232   return mComposite->HasArcIn(aNode, aArc, result);
00233 }
00234 
00235 NS_IMETHODIMP 
00236 nsChromeUIDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, PRBool *result)
00237 {
00238   return mComposite->HasArcOut(aSource, aArc, result);
00239 }
00240 
00241 NS_IMETHODIMP
00242 nsChromeUIDataSource::ArcLabelsIn(nsIRDFNode* aTarget, nsISimpleEnumerator** aResult)
00243 {
00244   return mComposite->ArcLabelsIn(aTarget, aResult);
00245 }
00246 
00247 NS_IMETHODIMP
00248 nsChromeUIDataSource::ArcLabelsOut(nsIRDFResource* aSource,
00249                                       nsISimpleEnumerator** aResult)
00250 {
00251   return mComposite->ArcLabelsOut(aSource, aResult);
00252 }
00253 
00254 NS_IMETHODIMP
00255 nsChromeUIDataSource::GetAllResources(nsISimpleEnumerator** aResult)
00256 {
00257   return mComposite->GetAllResources(aResult);
00258 }
00259 
00260 NS_IMETHODIMP
00261 nsChromeUIDataSource::GetAllCmds(nsIRDFResource* source,
00262                                         nsISimpleEnumerator/*<nsIRDFResource>*/** result)
00263 {
00264        return mComposite->GetAllCmds(source, result);
00265 }
00266 
00267 NS_IMETHODIMP
00268 nsChromeUIDataSource::IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources,
00269                                           nsIRDFResource*   aCommand,
00270                                           nsISupportsArray/*<nsIRDFResource>*/* aArguments,
00271                                           PRBool* aResult)
00272 {
00273   return mComposite->IsCommandEnabled(aSources, aCommand, aArguments, aResult);
00274 }
00275 
00276 NS_IMETHODIMP
00277 nsChromeUIDataSource::DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources,
00278                                    nsIRDFResource*   aCommand,
00279                                    nsISupportsArray/*<nsIRDFResource>*/* aArguments)
00280 {
00281   return mComposite->DoCommand(aSources, aCommand, aArguments);
00282 }
00283 
00284 NS_IMETHODIMP
00285 nsChromeUIDataSource::BeginUpdateBatch() {
00286   return mComposite->BeginUpdateBatch();
00287 }
00288 
00289 NS_IMETHODIMP
00290 nsChromeUIDataSource::EndUpdateBatch() {
00291   return mComposite->EndUpdateBatch();
00292 }
00293                                                                                
00295 
00296 NS_IMETHODIMP
00297 nsChromeUIDataSource::OnAssert(nsIRDFDataSource* aDataSource,
00298                                nsIRDFResource* aSource,
00299                                nsIRDFResource* aProperty,
00300                                nsIRDFNode* aTarget)
00301 {
00302   PRInt32 count = mObservers.Count();
00303 
00304   for (PRInt32 i = count - 1; i >= 0; --i) {
00305     mObservers[i]->OnAssert(this, aSource, aProperty, aTarget);
00306   }
00307   return NS_OK;
00308 }
00309 
00310 NS_IMETHODIMP
00311 nsChromeUIDataSource::OnUnassert(nsIRDFDataSource* aDataSource,
00312                                  nsIRDFResource* aSource,
00313                                  nsIRDFResource* aProperty,
00314                                  nsIRDFNode* aTarget)
00315 {
00316   PRInt32 count = mObservers.Count();
00317   for (PRInt32 i = count - 1; i >= 0; --i) {
00318     mObservers[i]->OnUnassert(aDataSource, aSource, aProperty, aTarget);
00319   }
00320   return NS_OK;
00321 }
00322 
00323 
00324 NS_IMETHODIMP
00325 nsChromeUIDataSource::OnChange(nsIRDFDataSource* aDataSource,
00326                                nsIRDFResource* aSource,
00327                                nsIRDFResource* aProperty,
00328                                nsIRDFNode* aOldTarget,
00329                                nsIRDFNode* aNewTarget)
00330 {
00331   PRInt32 count = mObservers.Count();
00332 
00333   for (PRInt32 i = count - 1; i >= 0; --i) {
00334     mObservers[i]->OnChange(aDataSource, aSource, aProperty, aOldTarget, aNewTarget);
00335   }
00336   return NS_OK;
00337 }
00338 
00339 
00340 NS_IMETHODIMP
00341 nsChromeUIDataSource::OnMove(nsIRDFDataSource* aDataSource,
00342                              nsIRDFResource* aOldSource,
00343                              nsIRDFResource* aNewSource,
00344                              nsIRDFResource* aProperty,
00345                              nsIRDFNode* aTarget)
00346 {
00347   PRInt32 count = mObservers.Count();
00348 
00349   for (PRInt32 i = count - 1; i >= 0; --i) {
00350     mObservers[i]->OnMove(aDataSource, aOldSource, aNewSource, aProperty, aTarget);
00351   }
00352   return NS_OK;
00353 }
00354 
00355 NS_IMETHODIMP
00356 nsChromeUIDataSource::OnBeginUpdateBatch(nsIRDFDataSource* aDataSource)
00357 {
00358   PRInt32 count = mObservers.Count();
00359 
00360   for (PRInt32 i = count - 1; i >= 0; --i) {
00361     mObservers[i]->OnBeginUpdateBatch(aDataSource);
00362   }
00363   return NS_OK;
00364 }
00365 
00366 NS_IMETHODIMP
00367 nsChromeUIDataSource::OnEndUpdateBatch(nsIRDFDataSource* aDataSource)
00368 {
00369   PRInt32 count = mObservers.Count();
00370 
00371   for (PRInt32 i = count - 1; i >= 0; --i) {
00372     mObservers[i]->OnEndUpdateBatch(aDataSource);
00373   }
00374   return NS_OK;
00375 }
00376 
00378 nsresult
00379 NS_NewChromeUIDataSource(nsIRDFDataSource* aComposite, nsIRDFDataSource** aResult)
00380 {
00381   NS_PRECONDITION(aResult != nsnull, "null ptr");
00382   if (! aResult)
00383       return NS_ERROR_NULL_POINTER;
00384 
00385   nsChromeUIDataSource* ChromeUIDataSource = new nsChromeUIDataSource(aComposite);
00386   if (ChromeUIDataSource == nsnull)
00387       return NS_ERROR_OUT_OF_MEMORY;
00388   NS_ADDREF(*aResult = ChromeUIDataSource);
00389   return NS_OK;
00390 }