|
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 #include "nsString.h" |
|
7 #include "nsIComponentManager.h" |
|
8 #include "nsBaseCommandController.h" |
|
9 |
|
10 #include "nsString.h" |
|
11 #include "nsWeakPtr.h" |
|
12 |
|
13 NS_IMPL_ADDREF(nsBaseCommandController) |
|
14 NS_IMPL_RELEASE(nsBaseCommandController) |
|
15 |
|
16 NS_INTERFACE_MAP_BEGIN(nsBaseCommandController) |
|
17 NS_INTERFACE_MAP_ENTRY(nsIController) |
|
18 NS_INTERFACE_MAP_ENTRY(nsICommandController) |
|
19 NS_INTERFACE_MAP_ENTRY(nsIControllerContext) |
|
20 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) |
|
21 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext) |
|
22 NS_INTERFACE_MAP_END |
|
23 |
|
24 nsBaseCommandController::nsBaseCommandController() |
|
25 : mCommandContextRawPtr(nullptr) |
|
26 { |
|
27 } |
|
28 |
|
29 nsBaseCommandController::~nsBaseCommandController() |
|
30 { |
|
31 } |
|
32 |
|
33 NS_IMETHODIMP |
|
34 nsBaseCommandController::Init(nsIControllerCommandTable *aCommandTable) |
|
35 { |
|
36 nsresult rv = NS_OK; |
|
37 |
|
38 if (aCommandTable) |
|
39 mCommandTable = aCommandTable; // owning addref |
|
40 else |
|
41 mCommandTable = do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv); |
|
42 |
|
43 return rv; |
|
44 } |
|
45 |
|
46 NS_IMETHODIMP |
|
47 nsBaseCommandController::SetCommandContext(nsISupports *aCommandContext) |
|
48 { |
|
49 mCommandContextWeakPtr = nullptr; |
|
50 mCommandContextRawPtr = nullptr; |
|
51 |
|
52 if (aCommandContext) { |
|
53 nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext); |
|
54 if (weak) { |
|
55 nsresult rv = |
|
56 weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr)); |
|
57 NS_ENSURE_SUCCESS(rv, rv); |
|
58 } |
|
59 else { |
|
60 mCommandContextRawPtr = aCommandContext; |
|
61 } |
|
62 } |
|
63 |
|
64 return NS_OK; |
|
65 } |
|
66 |
|
67 NS_IMETHODIMP |
|
68 nsBaseCommandController::GetInterface(const nsIID & aIID, void * *result) |
|
69 { |
|
70 NS_ENSURE_ARG_POINTER(result); |
|
71 |
|
72 if (NS_SUCCEEDED(QueryInterface(aIID, result))) |
|
73 return NS_OK; |
|
74 |
|
75 if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) |
|
76 { |
|
77 if (mCommandTable) |
|
78 return mCommandTable->QueryInterface(aIID, result); |
|
79 return NS_ERROR_NOT_INITIALIZED; |
|
80 } |
|
81 |
|
82 return NS_NOINTERFACE; |
|
83 } |
|
84 |
|
85 |
|
86 |
|
87 /* ======================================================================= |
|
88 * nsIController |
|
89 * ======================================================================= */ |
|
90 |
|
91 NS_IMETHODIMP |
|
92 nsBaseCommandController::IsCommandEnabled(const char *aCommand, |
|
93 bool *aResult) |
|
94 { |
|
95 NS_ENSURE_ARG_POINTER(aCommand); |
|
96 NS_ENSURE_ARG_POINTER(aResult); |
|
97 NS_ENSURE_STATE(mCommandTable); |
|
98 |
|
99 nsISupports* context = mCommandContextRawPtr; |
|
100 nsCOMPtr<nsISupports> weak; |
|
101 if (!context) { |
|
102 weak = do_QueryReferent(mCommandContextWeakPtr); |
|
103 context = weak; |
|
104 } |
|
105 return mCommandTable->IsCommandEnabled(aCommand, context, aResult); |
|
106 } |
|
107 |
|
108 NS_IMETHODIMP |
|
109 nsBaseCommandController::SupportsCommand(const char *aCommand, bool *aResult) |
|
110 { |
|
111 NS_ENSURE_ARG_POINTER(aCommand); |
|
112 NS_ENSURE_ARG_POINTER(aResult); |
|
113 NS_ENSURE_STATE(mCommandTable); |
|
114 |
|
115 nsISupports* context = mCommandContextRawPtr; |
|
116 nsCOMPtr<nsISupports> weak; |
|
117 if (!context) { |
|
118 weak = do_QueryReferent(mCommandContextWeakPtr); |
|
119 context = weak; |
|
120 } |
|
121 return mCommandTable->SupportsCommand(aCommand, context, aResult); |
|
122 } |
|
123 |
|
124 NS_IMETHODIMP |
|
125 nsBaseCommandController::DoCommand(const char *aCommand) |
|
126 { |
|
127 NS_ENSURE_ARG_POINTER(aCommand); |
|
128 NS_ENSURE_STATE(mCommandTable); |
|
129 |
|
130 nsISupports* context = mCommandContextRawPtr; |
|
131 nsCOMPtr<nsISupports> weak; |
|
132 if (!context) { |
|
133 weak = do_QueryReferent(mCommandContextWeakPtr); |
|
134 context = weak; |
|
135 } |
|
136 return mCommandTable->DoCommand(aCommand, context); |
|
137 } |
|
138 |
|
139 NS_IMETHODIMP |
|
140 nsBaseCommandController::DoCommandWithParams(const char *aCommand, |
|
141 nsICommandParams *aParams) |
|
142 { |
|
143 NS_ENSURE_ARG_POINTER(aCommand); |
|
144 NS_ENSURE_STATE(mCommandTable); |
|
145 |
|
146 nsISupports* context = mCommandContextRawPtr; |
|
147 nsCOMPtr<nsISupports> weak; |
|
148 if (!context) { |
|
149 weak = do_QueryReferent(mCommandContextWeakPtr); |
|
150 context = weak; |
|
151 } |
|
152 return mCommandTable->DoCommandParams(aCommand, aParams, context); |
|
153 } |
|
154 |
|
155 NS_IMETHODIMP |
|
156 nsBaseCommandController::GetCommandStateWithParams(const char *aCommand, |
|
157 nsICommandParams *aParams) |
|
158 { |
|
159 NS_ENSURE_ARG_POINTER(aCommand); |
|
160 NS_ENSURE_STATE(mCommandTable); |
|
161 |
|
162 nsISupports* context = mCommandContextRawPtr; |
|
163 nsCOMPtr<nsISupports> weak; |
|
164 if (!context) { |
|
165 weak = do_QueryReferent(mCommandContextWeakPtr); |
|
166 context = weak; |
|
167 } |
|
168 return mCommandTable->GetCommandState(aCommand, aParams, context); |
|
169 } |
|
170 |
|
171 NS_IMETHODIMP |
|
172 nsBaseCommandController::OnEvent(const char * aEventName) |
|
173 { |
|
174 NS_ENSURE_ARG_POINTER(aEventName); |
|
175 return NS_OK; |
|
176 } |