Source code

Revision control

Other Tools

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/. */
5
6
/*
7
8
This file provides the implementation for the XUL "controllers"
9
object.
10
11
*/
12
13
#include "nsString.h"
14
15
#include "nsIControllers.h"
16
#include "nsXULControllers.h"
17
#include "nsIController.h"
18
#include "mozilla/RefPtr.h"
19
20
//----------------------------------------------------------------------
21
22
nsXULControllers::nsXULControllers() : mCurControllerID(0) {}
23
24
nsXULControllers::~nsXULControllers(void) { DeleteControllers(); }
25
26
void nsXULControllers::DeleteControllers() {
27
uint32_t count = mControllers.Length();
28
for (uint32_t i = 0; i < count; i++) {
29
nsXULControllerData* controllerData = mControllers.ElementAt(i);
30
delete controllerData; // releases the nsIController
31
}
32
33
mControllers.Clear();
34
}
35
36
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULControllers)
37
38
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULControllers)
39
tmp->DeleteControllers();
40
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
41
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULControllers)
42
{
43
uint32_t i, count = tmp->mControllers.Length();
44
for (i = 0; i < count; ++i) {
45
nsXULControllerData* controllerData = tmp->mControllers[i];
46
if (controllerData) {
47
cb.NoteXPCOMChild(controllerData->mController);
48
}
49
}
50
}
51
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
52
53
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULControllers)
54
NS_INTERFACE_MAP_ENTRY(nsIControllers)
55
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllers)
56
NS_INTERFACE_MAP_END
57
58
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULControllers)
59
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULControllers)
60
61
NS_IMETHODIMP
62
nsXULControllers::GetControllerForCommand(const char* aCommand,
63
nsIController** _retval) {
64
NS_ENSURE_ARG_POINTER(_retval);
65
*_retval = nullptr;
66
67
uint32_t count = mControllers.Length();
68
for (uint32_t i = 0; i < count; i++) {
69
nsXULControllerData* controllerData = mControllers.ElementAt(i);
70
if (controllerData) {
71
nsCOMPtr<nsIController> controller;
72
controllerData->GetController(getter_AddRefs(controller));
73
if (controller) {
74
bool supportsCommand;
75
controller->SupportsCommand(aCommand, &supportsCommand);
76
if (supportsCommand) {
77
controller.forget(_retval);
78
return NS_OK;
79
}
80
}
81
}
82
}
83
84
return NS_OK;
85
}
86
87
NS_IMETHODIMP
88
nsXULControllers::InsertControllerAt(uint32_t aIndex,
89
nsIController* controller) {
90
nsXULControllerData* controllerData =
91
new nsXULControllerData(++mCurControllerID, controller);
92
#ifdef DEBUG
93
nsXULControllerData** inserted =
94
#endif
95
mControllers.InsertElementAt(aIndex, controllerData);
96
NS_ASSERTION(inserted != nullptr, "Insertion of controller failed");
97
return NS_OK;
98
}
99
100
NS_IMETHODIMP
101
nsXULControllers::RemoveControllerAt(uint32_t aIndex, nsIController** _retval) {
102
NS_ENSURE_ARG_POINTER(_retval);
103
*_retval = nullptr;
104
105
nsXULControllerData* controllerData = mControllers.SafeElementAt(aIndex);
106
if (!controllerData) return NS_ERROR_FAILURE;
107
108
mControllers.RemoveElementAt(aIndex);
109
110
controllerData->GetController(_retval);
111
delete controllerData;
112
113
return NS_OK;
114
}
115
116
NS_IMETHODIMP
117
nsXULControllers::GetControllerAt(uint32_t aIndex, nsIController** _retval) {
118
NS_ENSURE_ARG_POINTER(_retval);
119
*_retval = nullptr;
120
121
nsXULControllerData* controllerData = mControllers.SafeElementAt(aIndex);
122
if (!controllerData) return NS_ERROR_FAILURE;
123
124
return controllerData->GetController(_retval); // does the addref
125
}
126
127
NS_IMETHODIMP
128
nsXULControllers::AppendController(nsIController* controller) {
129
// This assigns controller IDs starting at 1 so we can use 0 to test if an ID
130
// was obtained
131
nsXULControllerData* controllerData =
132
new nsXULControllerData(++mCurControllerID, controller);
133
134
#ifdef DEBUG
135
nsXULControllerData** appended =
136
#endif
137
mControllers.AppendElement(controllerData);
138
NS_ASSERTION(appended != nullptr, "Appending controller failed");
139
return NS_OK;
140
}
141
142
NS_IMETHODIMP
143
nsXULControllers::RemoveController(nsIController* controller) {
144
// first get the identity pointer
145
nsCOMPtr<nsISupports> controllerSup(do_QueryInterface(controller));
146
// then find it
147
uint32_t count = mControllers.Length();
148
for (uint32_t i = 0; i < count; i++) {
149
nsXULControllerData* controllerData = mControllers.ElementAt(i);
150
if (controllerData) {
151
nsCOMPtr<nsIController> thisController;
152
controllerData->GetController(getter_AddRefs(thisController));
153
nsCOMPtr<nsISupports> thisControllerSup(
154
do_QueryInterface(thisController)); // get identity
155
if (thisControllerSup == controllerSup) {
156
mControllers.RemoveElementAt(i);
157
delete controllerData;
158
return NS_OK;
159
}
160
}
161
}
162
return NS_ERROR_FAILURE; // right thing to return if no controller found?
163
}
164
165
NS_IMETHODIMP
166
nsXULControllers::GetControllerId(nsIController* controller,
167
uint32_t* _retval) {
168
NS_ENSURE_ARG_POINTER(_retval);
169
170
uint32_t count = mControllers.Length();
171
for (uint32_t i = 0; i < count; i++) {
172
nsXULControllerData* controllerData = mControllers.ElementAt(i);
173
if (controllerData) {
174
nsCOMPtr<nsIController> thisController;
175
controllerData->GetController(getter_AddRefs(thisController));
176
if (thisController.get() == controller) {
177
*_retval = controllerData->GetControllerID();
178
return NS_OK;
179
}
180
}
181
}
182
return NS_ERROR_FAILURE; // none found
183
}
184
185
NS_IMETHODIMP
186
nsXULControllers::GetControllerById(uint32_t controllerID,
187
nsIController** _retval) {
188
NS_ENSURE_ARG_POINTER(_retval);
189
190
uint32_t count = mControllers.Length();
191
for (uint32_t i = 0; i < count; i++) {
192
nsXULControllerData* controllerData = mControllers.ElementAt(i);
193
if (controllerData && controllerData->GetControllerID() == controllerID) {
194
return controllerData->GetController(_retval);
195
}
196
}
197
return NS_ERROR_FAILURE; // none found
198
}
199
200
NS_IMETHODIMP
201
nsXULControllers::GetControllerCount(uint32_t* _retval) {
202
NS_ENSURE_ARG_POINTER(_retval);
203
*_retval = mControllers.Length();
204
return NS_OK;
205
}