1 /* vim:set st=2 sts=2 ts=2 et cin: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is a cache for services in a category.
17 * The Initial Developer of the Original Code is
18 * Christian Biesinger <cbiesinger@web.de>.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsIObserverService.h"
39 #include "nsISupportsPrimitives.h"
41 #include "nsXPCOMCID.h"
43 #include "nsCategoryCache.h"
45 nsCategoryObserver::nsCategoryObserver(const char* aCategory
,
46 nsCategoryListener
* aListener
)
47 : mListener(nsnull
), mCategory(aCategory
)
54 mListener
= aListener
;
56 // First, enumerate the currently existing entries
57 nsCOMPtr
<nsICategoryManager
> catMan
=
58 do_GetService(NS_CATEGORYMANAGER_CONTRACTID
);
62 nsCOMPtr
<nsISimpleEnumerator
> enumerator
;
63 nsresult rv
= catMan
->EnumerateCategory(aCategory
,
64 getter_AddRefs(enumerator
));
68 nsCOMPtr
<nsISupports
> entry
;
69 while (NS_SUCCEEDED(enumerator
->GetNext(getter_AddRefs(entry
)))) {
70 nsCOMPtr
<nsISupportsCString
> entryName
= do_QueryInterface(entry
, &rv
);
72 if (NS_SUCCEEDED(rv
)) {
73 nsCAutoString categoryEntry
;
74 rv
= entryName
->GetData(categoryEntry
);
77 catMan
->GetCategoryEntry(aCategory
,
79 getter_Copies(entryValue
));
81 if (NS_SUCCEEDED(rv
)) {
82 mHash
.Put(categoryEntry
, entryValue
);
83 mListener
->EntryAdded(entryValue
);
88 // Now, listen for changes
89 nsCOMPtr
<nsIObserverService
> serv
=
90 do_GetService(NS_OBSERVERSERVICE_CONTRACTID
);
94 serv
->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, PR_FALSE
);
96 serv
->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID
, PR_FALSE
);
97 serv
->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID
, PR_FALSE
);
98 serv
->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID
, PR_FALSE
);
101 nsCategoryObserver::~nsCategoryObserver() {
104 NS_IMPL_ISUPPORTS1(nsCategoryObserver
, nsIObserver
)
107 nsCategoryObserver::ListenerDied() {
110 nsCOMPtr
<nsIObserverService
> serv
=
111 do_GetService(NS_OBSERVERSERVICE_CONTRACTID
);
115 serv
->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
);
117 serv
->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID
);
118 serv
->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID
);
119 serv
->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID
);
123 nsCategoryObserver::Observe(nsISupports
* aSubject
, const char* aTopic
,
124 const PRUnichar
* aData
) {
128 if (strcmp(aTopic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
) == 0) {
130 mListener
->CategoryCleared();
135 !nsDependentString(aData
).Equals(NS_ConvertASCIItoUTF16(mCategory
)))
139 nsCOMPtr
<nsISupportsCString
> strWrapper(do_QueryInterface(aSubject
));
141 strWrapper
->GetData(str
);
143 if (strcmp(aTopic
, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID
) == 0) {
144 nsCOMPtr
<nsICategoryManager
> catMan
=
145 do_GetService(NS_CATEGORYMANAGER_CONTRACTID
);
149 nsCString entryValue
;
150 catMan
->GetCategoryEntry(mCategory
.get(),
152 getter_Copies(entryValue
));
154 mHash
.Put(str
, entryValue
);
155 mListener
->EntryAdded(entryValue
);
156 } else if (strcmp(aTopic
, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID
) == 0) {
158 if (mHash
.Get(str
, &val
)) {
160 mListener
->EntryRemoved(val
);
162 } else if (strcmp(aTopic
, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID
) == 0) {
164 mListener
->CategoryCleared();