Back to index

lightning-sunbird  0.9+nobinonly
nsStatusBarBiffManager.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Seth Spitzer <sspitzer@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 "nsStatusBarBiffManager.h"
00040 #include "nsMsgBiffManager.h"
00041 #include "nsCRT.h"
00042 #include "nsIMsgMailSession.h"
00043 #include "nsIMsgAccountManager.h"
00044 #include "nsMsgBaseCID.h"
00045 #include "nsIObserverService.h"
00046 #include "nsIWindowMediator.h"
00047 #include "nsIDOMDocument.h"
00048 #include "nsIDOMWindowInternal.h"
00049 #include "nsIDOMElement.h"
00050 #include "nsIMsgMailSession.h"
00051 #include "nsMsgFolderDataSource.h"
00052 #include "MailNewsTypes.h"
00053 #include "nsIMsgFolder.h" // TO include biffState enum. Change to bool later...
00054 #include "nsIFileChannel.h"
00055 #include "nsIPrefService.h"
00056 #include "nsIPrefBranch.h"
00057 #include "nsXPIDLString.h"
00058 #include "nsIURL.h"
00059 #include "nsNetUtil.h"
00060 #include "nsIFileURL.h"
00061 #include "nsIFile.h"
00062 
00063 // QueryInterface, AddRef, and Release
00064 //
00065 NS_IMPL_ISUPPORTS2(nsStatusBarBiffManager, nsIStatusBarBiffManager, nsIFolderListener)
00066 
00067 nsIAtom * nsStatusBarBiffManager::kBiffStateAtom = nsnull;
00068 
00069 nsStatusBarBiffManager::nsStatusBarBiffManager()
00070 : mInitialized(PR_FALSE), mCurrentBiffState(nsIMsgFolder::nsMsgBiffState_Unknown)
00071 {
00072 }
00073 
00074 nsStatusBarBiffManager::~nsStatusBarBiffManager()
00075 {
00076     NS_IF_RELEASE(kBiffStateAtom);
00077 }
00078 
00079 #define PREF_PLAY_SOUND_ON_NEW_MAIL      "mail.biff.play_sound"
00080 #define PREF_NEW_MAIL_SOUND_URL          "mail.biff.play_sound.url"
00081 #define PREF_NEW_MAIL_SOUND_TYPE         "mail.biff.play_sound.type"
00082 #define SYSTEM_SOUND_TYPE 0
00083 #define CUSTOM_SOUND_TYPE 1
00084 #define DEFAULT_SYSTEM_SOUND "_moz_mailbeep"
00085 
00086 nsresult nsStatusBarBiffManager::Init()
00087 {
00088   if (mInitialized)
00089     return NS_ERROR_ALREADY_INITIALIZED;
00090 
00091   nsresult rv;
00092 
00093   kBiffStateAtom = NS_NewAtom("BiffState");
00094 
00095   nsCOMPtr<nsIMsgMailSession> mailSession = 
00096     do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv); 
00097   if(NS_SUCCEEDED(rv))
00098     mailSession->AddFolderListener(this, nsIFolderListener::intPropertyChanged);
00099 
00100   mInitialized = PR_TRUE;
00101   return NS_OK;
00102 }
00103 
00104 nsresult nsStatusBarBiffManager::PlayBiffSound()
00105 {
00106   nsresult rv;
00107   nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00108   NS_ENSURE_SUCCESS(rv,rv);
00109   
00110   PRBool playSoundOnBiff = PR_FALSE;
00111   rv = pref->GetBoolPref(PREF_PLAY_SOUND_ON_NEW_MAIL, &playSoundOnBiff);
00112   NS_ENSURE_SUCCESS(rv,rv);
00113   
00114   if (!playSoundOnBiff)
00115     return NS_OK;
00116 
00117   // lazily create the sound instance
00118   if (!mSound)
00119     mSound = do_CreateInstance("@mozilla.org/sound;1");
00120       
00121   PRInt32 newMailSoundType = SYSTEM_SOUND_TYPE;
00122   rv = pref->GetIntPref(PREF_NEW_MAIL_SOUND_TYPE, &newMailSoundType);
00123   NS_ENSURE_SUCCESS(rv,rv);
00124 
00125   PRBool customSoundPlayed = PR_FALSE;
00126 
00127   if (newMailSoundType == CUSTOM_SOUND_TYPE) {
00128     nsXPIDLCString soundURLSpec;
00129     rv = pref->GetCharPref(PREF_NEW_MAIL_SOUND_URL, getter_Copies(soundURLSpec));
00130     if (NS_SUCCEEDED(rv) && !soundURLSpec.IsEmpty()) {
00131       if (!strncmp(soundURLSpec.get(), "file://", 7)) {
00132         nsCOMPtr<nsIURI> fileURI;
00133         rv = NS_NewURI(getter_AddRefs(fileURI), soundURLSpec);
00134         NS_ENSURE_SUCCESS(rv,rv);
00135         nsCOMPtr<nsIFileURL> soundURL = do_QueryInterface(fileURI,&rv);
00136         if (NS_SUCCEEDED(rv)) {
00137           nsCOMPtr<nsIFile> soundFile;
00138           rv = soundURL->GetFile(getter_AddRefs(soundFile));
00139           if (NS_SUCCEEDED(rv)) {
00140             PRBool soundFileExists = PR_FALSE;
00141             rv = soundFile->Exists(&soundFileExists);
00142             if (NS_SUCCEEDED(rv) && soundFileExists) {
00143               rv = mSound->Play(soundURL);
00144               if (NS_SUCCEEDED(rv))
00145                 customSoundPlayed = PR_TRUE;
00146             }
00147           }
00148         }
00149       }
00150       else {
00151         // todo, see if we can create a nsIFile using the string as a native path.
00152         // if that fails, try playing a system sound
00153         rv = mSound->PlaySystemSound(soundURLSpec.get());
00154         if (NS_SUCCEEDED(rv))
00155           customSoundPlayed = PR_TRUE;
00156       }
00157     }
00158   }    
00159   
00160   // if nothing played, play the default system sound
00161   if (!customSoundPlayed) {
00162     rv = mSound->PlaySystemSound(DEFAULT_SYSTEM_SOUND);
00163     NS_ENSURE_SUCCESS(rv,rv);
00164   }
00165   return rv;
00166 }
00167 
00168 // nsIFolderListener methods....
00169 NS_IMETHODIMP 
00170 nsStatusBarBiffManager::OnItemAdded(nsIRDFResource *parentItem, nsISupports *item)
00171 {
00172   return NS_OK;
00173 }
00174 
00175 NS_IMETHODIMP 
00176 nsStatusBarBiffManager::OnItemRemoved(nsIRDFResource *parentItem, nsISupports *item)
00177 {
00178   return NS_OK;
00179 }
00180 
00181 NS_IMETHODIMP 
00182 nsStatusBarBiffManager::OnItemPropertyChanged(nsIRDFResource *item, nsIAtom *property, const char *oldValue, const char *newValue)
00183 {
00184   return NS_OK;
00185 }
00186 
00187 NS_IMETHODIMP
00188 nsStatusBarBiffManager::OnItemIntPropertyChanged(nsIRDFResource *item, nsIAtom *property, PRInt32 oldValue, PRInt32 newValue)
00189 {
00190   if (kBiffStateAtom == property && mCurrentBiffState != newValue) {
00191     // if we got new mail, attempt to play a sound.
00192     // if we fail along the way, don't return.
00193     // we still need to update the UI.    
00194     if (newValue == nsIMsgFolder::nsMsgBiffState_NewMail) {
00195       // if we fail to play the biff sound, keep going.
00196       (void)PlayBiffSound();
00197     }
00198     mCurrentBiffState = newValue;
00199 
00200     // don't care if notification fails
00201     nsCOMPtr<nsIObserverService>
00202       observerService(do_GetService("@mozilla.org/observer-service;1"));
00203       
00204     if (observerService)
00205       observerService->NotifyObservers(this, "mail:biff-state-changed", nsnull);
00206   }
00207   return NS_OK;
00208 }
00209 
00210 NS_IMETHODIMP 
00211 nsStatusBarBiffManager::OnItemBoolPropertyChanged(nsIRDFResource *item, nsIAtom *property, PRBool oldValue, PRBool newValue)
00212 {
00213   return NS_OK;
00214 }
00215 
00216 NS_IMETHODIMP 
00217 nsStatusBarBiffManager::OnItemUnicharPropertyChanged(nsIRDFResource *item, nsIAtom *property, const PRUnichar *oldValue, const PRUnichar *newValue)
00218 {
00219   return NS_OK;
00220 }
00221 
00222 NS_IMETHODIMP 
00223 nsStatusBarBiffManager::OnItemPropertyFlagChanged(nsIMsgDBHdr *item, nsIAtom *property, PRUint32 oldFlag, PRUint32 newFlag)
00224 {
00225   return NS_OK;
00226 }
00227 
00228 NS_IMETHODIMP 
00229 nsStatusBarBiffManager::OnItemEvent(nsIMsgFolder *item, nsIAtom *event)
00230 {
00231   return NS_OK;
00232 }
00233 // nsIStatusBarBiffManager method....
00234 NS_IMETHODIMP
00235 nsStatusBarBiffManager::GetBiffState(PRInt32 *aBiffState)
00236 {
00237   NS_ENSURE_ARG_POINTER(aBiffState);
00238   *aBiffState = mCurrentBiffState;
00239   return NS_OK;
00240 }
00241