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  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsAlertsService.h"
00040 #include "nsISupportsArray.h"
00041 #include "nsXPCOM.h"
00042 #include "nsISupportsPrimitives.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsIDOMWindowInternal.h"
00045 #include "nsIWindowWatcher.h"
00046 #include "nsDependentString.h"
00047 
00048 #define ALERT_CHROME_URL "chrome://communicator/content/alerts/alert.xul"
00049 
00050 NS_IMPL_THREADSAFE_ADDREF(nsAlertsService)
00051 NS_IMPL_THREADSAFE_RELEASE(nsAlertsService)
00052 
00053 NS_INTERFACE_MAP_BEGIN(nsAlertsService)
00054    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAlertsService)
00055    NS_INTERFACE_MAP_ENTRY(nsIAlertsService)
00056 NS_INTERFACE_MAP_END_THREADSAFE
00057 
00058 nsAlertsService::nsAlertsService()
00059 {
00060 }
00061 
00062 nsAlertsService::~nsAlertsService()
00063 {}
00064 
00065 NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle, 
00066                                                      const nsAString & aAlertText, PRBool aAlertTextClickable,
00067                                                      const nsAString & aAlertCookie,
00068                                                      nsIObserver * aAlertListener)
00069 {
00070   nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00071   nsCOMPtr<nsIDOMWindow> newWindow;
00072   nsresult rv;
00073 
00074   nsCOMPtr<nsISupportsArray> argsArray;
00075   rv = NS_NewISupportsArray(getter_AddRefs(argsArray));
00076   NS_ENSURE_SUCCESS(rv, rv);
00077 
00078   // create scriptable versions of our strings that we can store in our nsISupportsArray....
00079   nsCOMPtr<nsISupportsString> scriptableImageUrl (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00080   NS_ENSURE_TRUE(scriptableImageUrl, NS_ERROR_FAILURE);
00081 
00082   scriptableImageUrl->SetData(aImageUrl);
00083   argsArray->AppendElement(scriptableImageUrl);
00084 
00085   nsCOMPtr<nsISupportsString> scriptableAlertTitle (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00086   NS_ENSURE_TRUE(scriptableAlertTitle, NS_ERROR_FAILURE);
00087 
00088   scriptableAlertTitle->SetData(aAlertTitle);
00089   argsArray->AppendElement(scriptableAlertTitle);
00090 
00091   nsCOMPtr<nsISupportsString> scriptableAlertText (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00092   NS_ENSURE_TRUE(scriptableAlertText, NS_ERROR_FAILURE);
00093 
00094   scriptableAlertText->SetData(aAlertText);
00095   argsArray->AppendElement(scriptableAlertText);
00096 
00097   nsCOMPtr<nsISupportsPRBool> scriptableIsClickable (do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID));
00098   NS_ENSURE_TRUE(scriptableIsClickable, NS_ERROR_FAILURE);
00099 
00100   scriptableIsClickable->SetData(aAlertTextClickable);
00101   argsArray->AppendElement(scriptableIsClickable);
00102 
00103   nsCOMPtr<nsISupportsString> scriptableAlertCookie (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
00104   NS_ENSURE_TRUE(scriptableAlertCookie, NS_ERROR_FAILURE);
00105 
00106   scriptableAlertCookie->SetData(aAlertCookie);
00107   argsArray->AppendElement(scriptableAlertCookie);
00108 
00109   if (aAlertListener)
00110   {
00111     nsCOMPtr<nsISupportsInterfacePointer> ifptr = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
00112     NS_ENSURE_SUCCESS(rv, rv);
00113 
00114     nsCOMPtr<nsISupports> iSupports (do_QueryInterface(aAlertListener));
00115     ifptr->SetData(iSupports);
00116     ifptr->SetDataIID(&NS_GET_IID(nsIObserver));
00117     argsArray->AppendElement(ifptr);
00118   }
00119   
00120   rv = wwatch->OpenWindow(0, ALERT_CHROME_URL, "_blank",
00121                  "chrome,dialog=yes,titlebar=no,popup=yes", argsArray,
00122                  getter_AddRefs(newWindow));
00123   return rv;
00124 }