Changed to add/remove observer in nsDBFolderInfo instead of doing it from the module,

also changed to use XPCOM_SHUTDOWN to release the observer at shutdown,
bug 74772, sr=alecf,bienvenu.
This commit is contained in:
nhotta%netscape.com 2001-04-26 18:48:45 +00:00
parent a25724a18e
commit 413210e80d
3 changed files with 50 additions and 53 deletions

View File

@ -91,17 +91,12 @@ nsresult nsMsgDBModule::Initialize()
return NS_OK; return NS_OK;
mInitialized = PR_TRUE; mInitialized = PR_TRUE;
nsDBFolderInfo::AddPrefObserver();
return NS_OK; return NS_OK;
} }
// Shutdown this module, releasing all of the module resources // Shutdown this module, releasing all of the module resources
void nsMsgDBModule::Shutdown() void nsMsgDBModule::Shutdown()
{ {
nsDBFolderInfo::RemovePrefObserver();
nsMsgDatabase::CleanupCache(); nsMsgDatabase::CleanupCache();
// Release the factory objects // Release the factory objects
mMailDBFactory = null_nsCOMPtr(); mMailDBFactory = null_nsCOMPtr();

View File

@ -81,8 +81,6 @@ public:
nsresult GetUint32PropertyWithToken(mdb_token aProperty, PRUint32 &propertyValue, PRUint32 defaultValue = 0); nsresult GetUint32PropertyWithToken(mdb_token aProperty, PRUint32 &propertyValue, PRUint32 defaultValue = 0);
nsresult GetInt32PropertyWithToken(mdb_token aProperty, PRInt32 &propertyValue, PRInt32 defaultValue = 0); nsresult GetInt32PropertyWithToken(mdb_token aProperty, PRInt32 &propertyValue, PRInt32 defaultValue = 0);
static void AddPrefObserver();
static void RemovePrefObserver();
nsMsgKeyArray m_lateredKeys; // list of latered messages nsMsgKeyArray m_lateredKeys; // list of latered messages

View File

@ -27,6 +27,7 @@
#include "nsMsgFolderFlags.h" #include "nsMsgFolderFlags.h"
#include "nsIPref.h" #include "nsIPref.h"
#include "nsIObserver.h" #include "nsIObserver.h"
#include "nsIObserverService.h"
static const char *kDBFolderInfoScope = "ns:msg:db:row:scope:dbfolderinfo:all"; static const char *kDBFolderInfoScope = "ns:msg:db:row:scope:dbfolderinfo:all";
static const char *kDBFolderInfoTableKind = "ns:msg:db:table:kind:dbfolderinfo"; static const char *kDBFolderInfoTableKind = "ns:msg:db:table:kind:dbfolderinfo";
@ -59,6 +60,8 @@ static const char * kLocaleColumnName = "locale";
static nsString gDefaultCharacterSet; static nsString gDefaultCharacterSet;
static PRBool gDefaultCharacterOverride; static PRBool gDefaultCharacterOverride;
static nsIObserver *gFolderCharsetObserver = nsnull; static nsIObserver *gFolderCharsetObserver = nsnull;
static PRBool gInitializeObserver = PR_FALSE;
static PRBool gReleaseObserver = PR_FALSE;
// observer for charset related preference notification // observer for charset related preference notification
class nsFolderCharsetObserver : public nsIObserver { class nsFolderCharsetObserver : public nsIObserver {
@ -100,6 +103,12 @@ NS_IMETHODIMP nsFolderCharsetObserver::Observe(nsISupports *aSubject, const PRUn
rv = prefs->GetBoolPref(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, &gDefaultCharacterOverride); rv = prefs->GetBoolPref(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, &gDefaultCharacterOverride);
} }
} }
else if (aTopicString.Equals(NS_LITERAL_STRING(NS_XPCOM_SHUTDOWN_OBSERVER_ID)))
{
rv = prefs->RemoveObserver(kMAILNEWS_VIEW_DEFAULT_CHARSET, this);
rv = prefs->RemoveObserver(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, this);
gReleaseObserver = PR_TRUE; // set true to release observer
}
return rv; return rv;
} }
@ -157,6 +166,42 @@ nsDBFolderInfo::nsDBFolderInfo(nsMsgDatabase *mdb)
m_mdbTokensInitialized = PR_FALSE; m_mdbTokensInitialized = PR_FALSE;
if (!gInitializeObserver)
{
gInitializeObserver = PR_TRUE;
nsresult rv;
nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
PRUnichar *prefCharset = nsnull;
rv = prefs->GetLocalizedUnicharPref(kMAILNEWS_VIEW_DEFAULT_CHARSET, &prefCharset);
if (NS_SUCCEEDED(rv))
{
gDefaultCharacterSet.Assign(prefCharset);
PR_Free(prefCharset);
}
rv = prefs->GetBoolPref(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, &gDefaultCharacterOverride);
gFolderCharsetObserver = new nsFolderCharsetObserver();
NS_ASSERTION(gFolderCharsetObserver, "failed to create observer");
// register prefs callbacks
if (gFolderCharsetObserver)
{
NS_ADDREF(gFolderCharsetObserver);
rv = prefs->AddObserver(kMAILNEWS_VIEW_DEFAULT_CHARSET, gFolderCharsetObserver);
rv = prefs->AddObserver(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, gFolderCharsetObserver);
// also register for shutdown
nsCOMPtr<nsIObserverService> observerService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
rv = observerService->AddObserver(gFolderCharsetObserver, NS_LITERAL_STRING(NS_XPCOM_SHUTDOWN_OBSERVER_ID).get());
}
}
}
}
m_mdb = mdb; m_mdb = mdb;
if (mdb) if (mdb)
{ {
@ -179,6 +224,11 @@ nsDBFolderInfo::nsDBFolderInfo(nsMsgDatabase *mdb)
nsDBFolderInfo::~nsDBFolderInfo() nsDBFolderInfo::~nsDBFolderInfo()
{ {
if (gReleaseObserver)
{
NS_IF_RELEASE(gFolderCharsetObserver);
}
if (m_mdb) if (m_mdb)
{ {
if (m_mdbTable) if (m_mdbTable)
@ -875,49 +925,3 @@ NS_IMETHODIMP nsDBFolderInfo::InitFromTransferInfo(nsIDBFolderInfo *transferInfo
return NS_OK; return NS_OK;
} }
void nsDBFolderInfo::AddPrefObserver()
{
NS_ASSERTION(!gFolderCharsetObserver, "should only be called once");
nsresult rv;
nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
// initialize the globals
PRUnichar *prefCharset = nsnull;
rv = prefs->GetLocalizedUnicharPref(kMAILNEWS_VIEW_DEFAULT_CHARSET, &prefCharset);
if (NS_SUCCEEDED(rv))
{
gDefaultCharacterSet.Assign(prefCharset);
PR_Free(prefCharset);
}
rv = prefs->GetBoolPref(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, &gDefaultCharacterOverride);
// create an observer
gFolderCharsetObserver = new nsFolderCharsetObserver();
NS_ASSERTION(gFolderCharsetObserver, "failed to create observer");
// register prefs callbacks
if (gFolderCharsetObserver)
{
NS_ADDREF(gFolderCharsetObserver);
rv = prefs->AddObserver(kMAILNEWS_VIEW_DEFAULT_CHARSET, gFolderCharsetObserver);
rv = prefs->AddObserver(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, gFolderCharsetObserver);
}
}
}
void nsDBFolderInfo::RemovePrefObserver()
{
nsresult rv;
nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
if (gFolderCharsetObserver)
{
rv = prefs->RemoveObserver(kMAILNEWS_VIEW_DEFAULT_CHARSET, gFolderCharsetObserver);
rv = prefs->RemoveObserver(kMAILNEWS_DEFAULT_CHARSET_OVERRIDE, gFolderCharsetObserver);
NS_RELEASE(gFolderCharsetObserver);
}
}
}