Back to index

lightning-sunbird  0.9+nobinonly
nsCategoryCache.cpp
Go to the documentation of this file.
00001 /* vim:set st=2 sts=2 ts=2 et cin: */
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 a cache for services in a category.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Christian Biesinger <cbiesinger@web.de>.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
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 the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 "nsIObserverService.h"
00039 #include "nsISupportsPrimitives.h"
00040 
00041 #include "nsXPCOMCID.h"
00042 #include "nsObserverService.h"
00043 
00044 #include "nsCategoryCache.h"
00045 
00046 nsCategoryObserver::nsCategoryObserver(const char* aCategory,
00047                                        nsCategoryListener* aListener)
00048   : mListener(nsnull), mCategory(aCategory)
00049 {
00050   if (!mHash.Init()) {
00051     // OOM
00052     return;
00053   }
00054 
00055   mListener = aListener;
00056 
00057   // First, enumerate the currently existing entries
00058   nsCOMPtr<nsICategoryManager> catMan =
00059     do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
00060   if (!catMan)
00061     return;
00062 
00063   nsCOMPtr<nsISimpleEnumerator> enumerator;
00064   nsresult rv = catMan->EnumerateCategory(aCategory,
00065                                           getter_AddRefs(enumerator));
00066   if (NS_FAILED(rv))
00067     return;
00068 
00069   nsCOMPtr<nsISupports> entry;
00070   while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
00071     nsCOMPtr<nsISupportsCString> entryName = do_QueryInterface(entry, &rv);
00072 
00073     if (NS_SUCCEEDED(rv)) {
00074       nsCAutoString categoryEntry;
00075       rv = entryName->GetData(categoryEntry);
00076 
00077       nsXPIDLCString entryValue;
00078       catMan->GetCategoryEntry(aCategory,
00079                                categoryEntry.get(),
00080                                getter_Copies(entryValue));
00081 
00082       if (NS_SUCCEEDED(rv)) {
00083         mHash.Put(categoryEntry, entryValue);
00084         mListener->EntryAdded(entryValue);
00085       }
00086     }
00087   }
00088 
00089   // Now, listen for changes
00090   nsCOMPtr<nsIObserverService> serv =
00091     do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
00092   if (!serv)
00093     return;
00094 
00095   serv->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
00096 
00097   serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, PR_FALSE);
00098   serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, PR_FALSE);
00099   serv->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID, PR_FALSE);
00100 }
00101 
00102 nsCategoryObserver::~nsCategoryObserver() {
00103 }
00104 
00105 NS_IMPL_ISUPPORTS1(nsCategoryObserver, nsIObserver)
00106 
00107 void
00108 nsCategoryObserver::ListenerDied() {
00109   mListener = nsnull;
00110 
00111   nsCOMPtr<nsIObserverService> serv =
00112     do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
00113   if (!serv)
00114     return;
00115 
00116   serv->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
00117 
00118   serv->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID);
00119   serv->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID);
00120   serv->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID);
00121 }
00122 
00123 NS_IMETHODIMP
00124 nsCategoryObserver::Observe(nsISupports* aSubject, const char* aTopic,
00125                             const PRUnichar* aData) {
00126   if (!mListener)
00127     return NS_OK;
00128 
00129   if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
00130     mHash.Clear();
00131     mListener->CategoryCleared();
00132     return NS_OK;
00133   }
00134 
00135   if (!aData || !nsDependentString(aData).EqualsASCII(mCategory.get()))
00136     return NS_OK;
00137 
00138   nsCAutoString str;
00139   nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject));
00140   if (strWrapper)
00141     strWrapper->GetData(str);
00142 
00143   if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID) == 0) {
00144     nsCOMPtr<nsICategoryManager> catMan =
00145       do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
00146     if (!catMan)
00147       return NS_OK;
00148 
00149     nsXPIDLCString entryValue;
00150     catMan->GetCategoryEntry(mCategory.get(),
00151                              str.get(),
00152                              getter_Copies(entryValue));
00153 
00154     mHash.Put(str, entryValue);
00155     mListener->EntryAdded(entryValue);
00156   } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) {
00157     nsCAutoString val;
00158     if (mHash.Get(str, &val)) {
00159       mHash.Remove(str);
00160       mListener->EntryRemoved(val);
00161     }
00162   } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) {
00163     mHash.Clear();
00164     mListener->CategoryCleared();
00165   }
00166   return NS_OK;
00167 }