Back to index

lightning-sunbird  0.9+nobinonly
nsAlertsService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott MacGregor <mscott@netscape.com>
00024  *   Jens Bannmann <jens.b@web.de>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsAlertsService.h"
00041 #include "nsISupportsArray.h"
00042 #include "nsXPCOM.h"
00043 #include "nsISupportsPrimitives.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsIDOMWindowInternal.h"
00046 #include "nsIWindowWatcher.h"
00047 #include "nsDependentString.h"
00048 #include "nsWidgetsCID.h"
00049 #include "nsILookAndFeel.h"
00050 
00051 static NS_DEFINE_CID(kLookAndFeelCID, NS_LOOKANDFEEL_CID);
00052 
00053 #define ALERT_CHROME_URL "chrome://global/content/alerts/alert.xul"
00054 
00055 NS_IMPL_THREADSAFE_ADDREF(nsAlertsService)
00056 NS_IMPL_THREADSAFE_RELEASE(nsAlertsService)
00057 
00058 NS_INTERFACE_MAP_BEGIN(nsAlertsService)
00059    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAlertsService)
00060    NS_INTERFACE_MAP_ENTRY(nsIAlertsService)
00061 NS_INTERFACE_MAP_END_THREADSAFE
00062 
00063 nsAlertsService::nsAlertsService()
00064 {
00065 }
00066 
00067 nsAlertsService::~nsAlertsService()
00068 {}
00069 
00070 NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle, 
00071                                                      const nsAString & aAlertText, PRBool aAlertTextClickable,
00072                                                      const nsAString & aAlertCookie,
00073                                                      nsIObserver * aAlertListener)
00074 {
00075   nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00076   nsCOMPtr<nsIDOMWindow> newWindow;
00077   nsresult rv;
00078 
00079   nsCOMPtr<nsISupportsArray> argsArray;
00080   rv = NS_NewISupportsArray(getter_AddRefs(argsArray));
00081   NS_ENSURE_SUCCESS(rv, rv);
00082 
00083   // create scriptable versions of our strings that we can store in our nsISupportsArray....
00084   nsCOMPtr<nsISupportsString> scriptableImageUrl (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00085   NS_ENSURE_TRUE(scriptableImageUrl, NS_ERROR_FAILURE);
00086 
00087   scriptableImageUrl->SetData(aImageUrl);
00088   rv = argsArray->AppendElement(scriptableImageUrl);
00089   NS_ENSURE_SUCCESS(rv, rv);
00090 
00091   nsCOMPtr<nsISupportsString> scriptableAlertTitle (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00092   NS_ENSURE_TRUE(scriptableAlertTitle, NS_ERROR_FAILURE);
00093 
00094   scriptableAlertTitle->SetData(aAlertTitle);
00095   rv = argsArray->AppendElement(scriptableAlertTitle);
00096   NS_ENSURE_SUCCESS(rv, rv);
00097 
00098   nsCOMPtr<nsISupportsString> scriptableAlertText (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00099   NS_ENSURE_TRUE(scriptableAlertText, NS_ERROR_FAILURE);
00100 
00101   scriptableAlertText->SetData(aAlertText);
00102   rv = argsArray->AppendElement(scriptableAlertText);
00103   NS_ENSURE_SUCCESS(rv, rv);
00104 
00105   nsCOMPtr<nsISupportsPRBool> scriptableIsClickable (do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID));
00106   NS_ENSURE_TRUE(scriptableIsClickable, NS_ERROR_FAILURE);
00107 
00108   scriptableIsClickable->SetData(aAlertTextClickable);
00109   rv = argsArray->AppendElement(scriptableIsClickable);
00110   NS_ENSURE_SUCCESS(rv, rv);
00111 
00112   nsCOMPtr<nsISupportsString> scriptableAlertCookie (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00113   NS_ENSURE_TRUE(scriptableAlertCookie, NS_ERROR_FAILURE);
00114 
00115   scriptableAlertCookie->SetData(aAlertCookie);
00116   rv = argsArray->AppendElement(scriptableAlertCookie);
00117   NS_ENSURE_SUCCESS(rv, rv);
00118 
00119   nsCOMPtr<nsISupportsPRInt32> scriptableOrigin (do_CreateInstance(NS_SUPPORTS_PRINT32_CONTRACTID));
00120   NS_ENSURE_TRUE(scriptableOrigin, NS_ERROR_FAILURE);
00121   nsCOMPtr<nsILookAndFeel> lookAndFeel = do_GetService("@mozilla.org/widget/lookandfeel;1");
00122   if (lookAndFeel)
00123   {
00124     PRInt32 origin;
00125     lookAndFeel->GetMetric(nsILookAndFeel::eMetric_AlertNotificationOrigin,
00126                            origin);
00127     scriptableOrigin->SetData(origin);
00128   }
00129   rv = argsArray->AppendElement(scriptableOrigin);
00130   NS_ENSURE_SUCCESS(rv, rv);
00131 
00132   if (aAlertListener)
00133   {
00134     nsCOMPtr<nsISupportsInterfacePointer> ifptr = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
00135     NS_ENSURE_SUCCESS(rv, rv);
00136 
00137     nsCOMPtr<nsISupports> iSupports (do_QueryInterface(aAlertListener));
00138     ifptr->SetData(iSupports);
00139     ifptr->SetDataIID(&NS_GET_IID(nsIObserver));
00140     rv = argsArray->AppendElement(ifptr);
00141     NS_ENSURE_SUCCESS(rv, rv);
00142   }
00143 
00144   rv = wwatch->OpenWindow(0, ALERT_CHROME_URL, "_blank",
00145                  "chrome,dialog=yes,titlebar=no,popup=yes", argsArray,
00146                  getter_AddRefs(newWindow));
00147   return rv;
00148 }