nsSystemTrayServiceGTK.cpp
Go to the documentation of this file.
1 /* vim: set 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 "nsSystemTrayServiceGTK.h"
42 #include "nsIURI.h"
43 #include "nsIDOMDocument.h"
44 #include "nsIDOMWindow.h"
45 #include "nsIDOMXULElement.h"
46 #include "nsStringGlue.h"
47 #include "nsIDOMEventTarget.h"
48 
51 
53 {
54  mIconDataMap.Init();
55 }
56 
57 nsSystemTrayService::~nsSystemTrayService()
58 {
59  // no need to clean up the icons, because the hashtable will delete the
60  // wrappers, which will remove the icons for us
61 }
62 
63 /* void showIcon (in AString iconId, in nsIURI imageURI, in nsIDOMWindow window, in nsIBaseWindow baseWindow); */
64 NS_IMETHODIMP nsSystemTrayService::ShowIcon(const nsAString & aIconId,
65  nsIURI *aImageURI,
66  nsIDOMWindow *aDOMWindow)
67 {
68  nsresult rv;
69  nsCOMPtr<nsSystemTrayIconGTK> icon;
70  if (mIconDataMap.Get(aIconId, getter_AddRefs(icon))) {
71  // we are updating an old icon
72  rv = icon->SetImageFromURI(aImageURI);
73  // XXX Mook: hack until I figure out what to do with missing images
74  if (NS_FAILED(rv)) {
75  return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
76  }
77  return NS_OK;
78  }
79 
80  // first make sure that we can find the element associated with the
81  // <trayicon> element
82  nsCOMPtr<nsIDOMDocument> document;
83  rv = aDOMWindow->GetDocument(getter_AddRefs(document));
84  NS_ENSURE_SUCCESS(rv, rv);
85 
86  nsCOMPtr<nsIDOMElement> iconElement;
87  rv = document->GetElementById(aIconId, getter_AddRefs(iconElement));
88  NS_ENSURE_SUCCESS(rv, rv);
89 
90  nsCOMPtr<nsIDOMEventTarget> eventTarget =
91  do_QueryInterface(iconElement, &rv);
92  NS_ENSURE_SUCCESS(rv, rv);
93 
94  // okay, make a new icon and tell it about the event target
95  icon = new nsSystemTrayIconGTK();
96  if (!icon || !icon->mIcon) {
97  return NS_ERROR_OUT_OF_MEMORY;
98  }
99 
100  rv = icon->SetEventTarget(eventTarget);
101  NS_ENSURE_SUCCESS(rv, rv);
102 
103  mIconDataMap.Put(aIconId, icon);
104  rv = icon->SetImageFromURI(aImageURI);
105  if (NS_FAILED(rv)) {
106  // XXX Mook: hack until I figure out what to do with missing images
107  return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
108  }
109  return NS_OK;
110 }
111 
112 /* void hideIcon (in AString iconId); */
113 NS_IMETHODIMP nsSystemTrayService::HideIcon(const nsAString & aIconId)
114 {
115  if (!mIconDataMap.Get(aIconId, nsnull)) {
116  // we don't know about this icon in the first place
117  return NS_ERROR_INVALID_ARG;
118  }
119  // destructing the icon removes it for us
120  mIconDataMap.Remove(aIconId);
121  return NS_OK;
122 }
123 
124 /* void setTitle (in AString iconId, in AString title); */
125 NS_IMETHODIMP nsSystemTrayService::SetTitle(const nsAString & aIconId, const nsAString & aTitle)
126 {
127  nsCOMPtr<nsSystemTrayIconGTK> icon;
128  mIconDataMap.Get(aIconId, getter_AddRefs(icon));
129  if (!icon) {
130  return NS_ERROR_INVALID_ARG;
131  }
132 
133  // XXX Mook: OOM
134  gtk_status_icon_set_tooltip(icon->mIcon, NS_ConvertUTF16toUTF8(aTitle).get());
135  return NS_OK;
136 }
return NS_OK
NS_IMPL_ISUPPORTS1(nsSystemTrayService, nsISystemTrayService) nsSystemTrayService
const nsIDOMWindow
nsInterfaceHashtable< nsStringHashKey, nsSystemTrayIconGTK > mIconDataMap