nsSystemTrayIconBase.cpp
Go to the documentation of this file.
1 /* vim: fileencoding=utf-8 shiftwidth=2 */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is toolkit/components/systray
16  *
17  * The Initial Developer of the Original Code is
18  * Mook <mook.moz+cvs.mozilla.org@gmail.com>.
19  * Portions created by the Initial Developer are Copyright (C) 2007
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Brad Peterson <b_peterson@yahoo.com>
24  * Daniel Glazman <daniel.glazman@disruptive-innovations.com>
25  * Matthew Gertner <matthew@allpeers.com>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40 
41 #include "nsSystemTrayIconBase.h"
42 // headers for SetImageFromURI
43 #include "nsServiceManagerUtils.h"
44 #include "nsIInterfaceRequestorUtils.h"
45 #include "imgILoader.h"
46 #include "imgIRequest.h"
47 #include "nsNetError.h" // for NS_BINDING_ABORTED
48 // needed for event dispatching
49 #include "nsIDOMNode.h"
50 #include "nsIDOMDocumentEvent.h"
51 #include "nsIDOMDocument.h"
52 #include "nsStringGlue.h"
53 #include "nsIDOMDocumentView.h"
54 #include "nsIDOMMouseEvent.h"
55 #include "nsIDOMAbstractView.h"
56 
57 // debugging
58 #if DEBUG
59 #include <cstdio>
60 #endif
61 
63  imgIDecoderObserver,
64  imgIContainerObserver)
65 
67 {
68  if (mRequest) {
69  mRequest->Cancel(NS_BINDING_ABORTED);
70  }
71 }
72 
74 {
75  nsresult rv;
76 
77  nsCOMPtr<imgILoader> imgloader =
78  do_GetService("@mozilla.org/image/loader;1", &rv);
79  NS_ENSURE_SUCCESS(rv, rv);
80 
81  if (mRequest) {
83  }
84  rv = imgloader->LoadImage(aURI, aURI, aURI, nsnull, this, nsnull,
85  nsIRequest::LOAD_BACKGROUND, nsnull, nsnull,
86  getter_AddRefs(mRequest));
87  NS_ENSURE_SUCCESS(rv, rv);
88 
89  return NS_OK;
90 }
91 
92 nsresult nsSystemTrayIconBase::SetEventTarget(nsIDOMEventTarget *aEventTarget)
93 {
94  mEventTarget = aEventTarget;
95  return NS_OK;
96 }
97 
98 void nsSystemTrayIconBase::DispatchEvent(const nsAString& aType,
99  PRUint16 aButton, PRInt32 aDetail,
100  PRBool aCtrlKey, PRBool aAltKey,
101  PRBool aShiftKey, PRBool aMetaKey)
102 {
103  nsresult rv;
104 
105  // first, create an event...
106  nsCOMPtr<nsIDOMNode> node(do_QueryInterface(mEventTarget, &rv));
107  if (NS_FAILED(rv)) return;
108 
109  nsCOMPtr<nsIDOMDocument> doc;
110  rv = node->GetOwnerDocument(getter_AddRefs(doc));
111  if (NS_FAILED(rv)) return;
112 
113  nsCOMPtr<nsIDOMDocumentEvent> docEvent(do_QueryInterface(doc, &rv));
114  if (NS_FAILED(rv)) return;
115 
116  nsCOMPtr<nsIDOMEvent> event;
117  rv = docEvent->CreateEvent(NS_LITERAL_STRING("mouseevent"),
118  getter_AddRefs(event));
119  if (NS_FAILED(rv)) return;
120 
121  nsCOMPtr<nsIDOMMouseEvent> mouseEvent(do_QueryInterface(event, &rv));
122  if (NS_FAILED(rv)) return;
123 
124  // get the view the event occurred on
125  nsCOMPtr<nsIDOMDocumentView> documentView(do_QueryInterface(doc, &rv));
126  if (NS_FAILED(rv)) return;
127 
128  nsCOMPtr<nsIDOMAbstractView> view;
129  rv = documentView->GetDefaultView(getter_AddRefs(view));
130  if (NS_FAILED(rv)) return;
131 
132  // figure out where to position the popup
133  nsPoint position = GetPopupPosition();
134 
135  // initialize the event
136  // TODO: give useful arguments here
137  rv = mouseEvent->InitMouseEvent(aType,
138  PR_FALSE, PR_TRUE, view, aDetail,
139  position.x, position.y, position.x, position.y,
140  aCtrlKey, aAltKey, aShiftKey, aMetaKey,
141  aButton, nsnull);
142  if (NS_FAILED(rv)) return;
143 
144  // and dispatch it. (yes, return value is ignored; we can't do anything)
145  PRBool result;
146  mEventTarget->DispatchEvent(event, &result);
147 }
148 
150 /* void onStartRequest (in imgIRequest aRequest); */
151 NS_IMETHODIMP nsSystemTrayIconBase::OnStartRequest(imgIRequest *aRequest)
152 {
153  return NS_OK;
154 }
155 
156 /* void onStartDecode (in imgIRequest aRequest); */
157 NS_IMETHODIMP nsSystemTrayIconBase::OnStartDecode(imgIRequest *aRequest)
158 {
159  return NS_OK;
160 }
161 
162 /* void onStartContainer (in imgIRequest aRequest, in imgIContainer aContainer); */
163 NS_IMETHODIMP nsSystemTrayIconBase::OnStartContainer(imgIRequest *aRequest, imgIContainer *aContainer)
164 {
165  return NS_OK;
166 }
167 
168 /* void onStartFrame (in imgIRequest aRequest, in unsigned long aFrame); */
169 NS_IMETHODIMP
170 nsSystemTrayService::OnStartFrame(imgIRequest *aRequest,
171  PRUint32 aFrame)
172 {
173  return NS_OK;
174 }
175 
176 /* [noscript] void onDataAvailable (in imgIRequest aRequest,
177  in boolean aCurrentFrame, [const] in nsIntRect aRect); */
178 NS_IMETHODIMP
179 nsSystemTrayService::OnDataAvailable(imgIRequest *aRequest,
180  PRBool aCurrentFrame,
181  const nsIntRect * aRect)
182 {
183  return NS_OK;
184 }
185 
186 /* void onStopFrame (in imgIRequest aRequest, in unsigned long aFrame); */
187 NS_IMETHODIMP
188 nsSystemTrayService::OnStopFrame(imgIRequest *aRequest,
189  PRUint32 aFrame)
190 {
191  return NS_ERROR_NOT_IMPLEMENTED;
192 }
193 
194 /* void onStopContainer (in imgIRequest aRequest, in imgIContainer aContainer); */
195 NS_IMETHODIMP nsSystemTrayIconBase::OnStopContainer(imgIRequest *aRequest, imgIContainer *aContainer)
196 {
197  return NS_OK;
198 }
199 
200 /* void onStopDecode (in imgIRequest aRequest, in nsresult status, in wstring statusArg); */
201 NS_IMETHODIMP nsSystemTrayIconBase::OnStopDecode(imgIRequest *aRequest, nsresult status, const PRUnichar *statusArg)
202 {
203  return NS_OK;
204 }
205 
206 /* void onStopRequest (in imgIRequest aRequest, in boolean aIsLastPart); */
207 NS_IMETHODIMP nsSystemTrayIconBase::OnStopRequest(imgIRequest *aRequest, PRBool aIsLastPart)
208 {
209  return NS_OK;
210 }
211 
213 /* [noscript] void frameChanged (in imgIContainer aContainer,
214  in nsIntRect aDirtyRect); */
215 NS_IMETHODIMP
216 nsSystemTrayService::FrameChanged(imgIContainer *aContainer,
217  nsIntRect * aDirtyRect)
218 {
219  return NS_OK;
220 }
return NS_OK
function doc() browser.contentDocument
var event
NS_IMPL_ISUPPORTS2(nsSystemTrayIconBase, imgIDecoderObserver, imgIContainerObserver) nsSystemTrayIconBase
let node
void DispatchEvent(const nsAString &aType, PRUint16 aButton, PRInt32 aDetail, PRBool aCtrlKey, PRBool aAltKey, PRBool aShiftKey, PRBool aMetaKey)
nsCOMPtr< imgIRequest > mRequest
virtual nsPoint GetPopupPosition()=0
nsCOMPtr< nsIDOMEventTarget > mEventTarget
const NS_BINDING_ABORTED
nsresult SetEventTarget(nsIDOMEventTarget *aEventTarget)
nsresult SetImageFromURI(nsIURI *aURI)