1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 This file provides the implementation for the XUL "controllers"
15 #include "nsIControllers.h"
16 #include "nsXULControllers.h"
17 #include "nsIController.h"
18 #include "mozilla/RefPtr.h"
20 //----------------------------------------------------------------------
22 nsXULControllerData::nsXULControllerData(uint32_t inControllerID
,
23 nsIController
* inController
)
24 : mControllerID(inControllerID
), mController(inController
) {}
26 nsresult
nsXULControllerData::GetController(nsIController
** outController
) {
27 NS_IF_ADDREF(*outController
= mController
);
31 nsXULControllers::nsXULControllers() : mCurControllerID(0) {}
33 nsXULControllers::~nsXULControllers(void) { DeleteControllers(); }
35 void nsXULControllers::DeleteControllers() {
36 uint32_t count
= mControllers
.Length();
37 for (uint32_t i
= 0; i
< count
; i
++) {
38 nsXULControllerData
* controllerData
= mControllers
.ElementAt(i
);
39 delete controllerData
; // releases the nsIController
45 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULControllers
)
47 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULControllers
)
48 tmp
->DeleteControllers();
49 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
50 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULControllers
)
52 uint32_t i
, count
= tmp
->mControllers
.Length();
53 for (i
= 0; i
< count
; ++i
) {
54 nsXULControllerData
* controllerData
= tmp
->mControllers
[i
];
56 cb
.NoteXPCOMChild(controllerData
->mController
);
60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
62 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULControllers
)
63 NS_INTERFACE_MAP_ENTRY(nsIControllers
)
64 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIControllers
)
67 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULControllers
)
68 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULControllers
)
71 nsXULControllers::GetControllerForCommand(const char* aCommand
,
72 nsIController
** _retval
) {
73 NS_ENSURE_ARG_POINTER(_retval
);
76 uint32_t count
= mControllers
.Length();
77 for (uint32_t i
= 0; i
< count
; i
++) {
78 nsXULControllerData
* controllerData
= mControllers
.ElementAt(i
);
80 nsCOMPtr
<nsIController
> controller
;
81 controllerData
->GetController(getter_AddRefs(controller
));
84 controller
->SupportsCommand(aCommand
, &supportsCommand
);
85 if (supportsCommand
) {
86 controller
.forget(_retval
);
97 nsXULControllers::InsertControllerAt(uint32_t aIndex
,
98 nsIController
* controller
) {
99 nsXULControllerData
* controllerData
=
100 new nsXULControllerData(++mCurControllerID
, controller
);
102 nsXULControllerData
** inserted
=
104 mControllers
.InsertElementAt(aIndex
, controllerData
);
105 NS_ASSERTION(inserted
!= nullptr, "Insertion of controller failed");
110 nsXULControllers::RemoveControllerAt(uint32_t aIndex
, nsIController
** _retval
) {
111 NS_ENSURE_ARG_POINTER(_retval
);
114 nsXULControllerData
* controllerData
= mControllers
.SafeElementAt(aIndex
);
115 if (!controllerData
) return NS_ERROR_FAILURE
;
117 mControllers
.RemoveElementAt(aIndex
);
119 controllerData
->GetController(_retval
);
120 delete controllerData
;
126 nsXULControllers::GetControllerAt(uint32_t aIndex
, nsIController
** _retval
) {
127 NS_ENSURE_ARG_POINTER(_retval
);
130 nsXULControllerData
* controllerData
= mControllers
.SafeElementAt(aIndex
);
131 if (!controllerData
) return NS_ERROR_FAILURE
;
133 return controllerData
->GetController(_retval
); // does the addref
137 nsXULControllers::AppendController(nsIController
* controller
) {
138 // This assigns controller IDs starting at 1 so we can use 0 to test if an ID
140 nsXULControllerData
* controllerData
=
141 new nsXULControllerData(++mCurControllerID
, controller
);
144 nsXULControllerData
** appended
=
146 mControllers
.AppendElement(controllerData
);
147 NS_ASSERTION(appended
!= nullptr, "Appending controller failed");
152 nsXULControllers::RemoveController(nsIController
* controller
) {
153 // first get the identity pointer
154 nsCOMPtr
<nsISupports
> controllerSup(do_QueryInterface(controller
));
156 uint32_t count
= mControllers
.Length();
157 for (uint32_t i
= 0; i
< count
; i
++) {
158 nsXULControllerData
* controllerData
= mControllers
.ElementAt(i
);
159 if (controllerData
) {
160 nsCOMPtr
<nsIController
> thisController
;
161 controllerData
->GetController(getter_AddRefs(thisController
));
162 nsCOMPtr
<nsISupports
> thisControllerSup(
163 do_QueryInterface(thisController
)); // get identity
164 if (thisControllerSup
== controllerSup
) {
165 mControllers
.RemoveElementAt(i
);
166 delete controllerData
;
171 return NS_ERROR_FAILURE
; // right thing to return if no controller found?
175 nsXULControllers::GetControllerId(nsIController
* controller
,
177 NS_ENSURE_ARG_POINTER(_retval
);
179 uint32_t count
= mControllers
.Length();
180 for (uint32_t i
= 0; i
< count
; i
++) {
181 nsXULControllerData
* controllerData
= mControllers
.ElementAt(i
);
182 if (controllerData
) {
183 nsCOMPtr
<nsIController
> thisController
;
184 controllerData
->GetController(getter_AddRefs(thisController
));
185 if (thisController
.get() == controller
) {
186 *_retval
= controllerData
->GetControllerID();
191 return NS_ERROR_FAILURE
; // none found
195 nsXULControllers::GetControllerById(uint32_t controllerID
,
196 nsIController
** _retval
) {
197 NS_ENSURE_ARG_POINTER(_retval
);
199 uint32_t count
= mControllers
.Length();
200 for (uint32_t i
= 0; i
< count
; i
++) {
201 nsXULControllerData
* controllerData
= mControllers
.ElementAt(i
);
202 if (controllerData
&& controllerData
->GetControllerID() == controllerID
) {
203 return controllerData
->GetController(_retval
);
206 return NS_ERROR_FAILURE
; // none found
210 nsXULControllers::GetControllerCount(uint32_t* _retval
) {
211 NS_ENSURE_ARG_POINTER(_retval
);
212 *_retval
= mControllers
.Length();