Bug 378527 - Localize nsIAlertsService. r=cbarrett,r=mano. Also Fixes Bug 380142 - Growl doesn't support click notification

This commit is contained in:
sdwilsh@shawnwilsher.com 2007-05-15 13:25:53 -07:00
Родитель 0f180a5981
Коммит 697fed38af
7 изменённых файлов: 120 добавлений и 42 удалений

Просмотреть файл

@ -51,6 +51,7 @@ REQUIRES = \
string \
necko \
xulapp \
intl \
$(NULL)
CMMSRCS = \

Просмотреть файл

@ -39,8 +39,9 @@
#import <Cocoa/Cocoa.h>
// XXX should this be localized? Bug 378527
#define NOTIFICATION_NAME @"Application Notice"
#define GROWL_STRING_BUNDLE_LOCATION \
"chrome://alerts/locale/notificationNames.properties"
#define OBSERVER_KEY @"ALERT_OBSERVER"
#define COOKIE_KEY @"ALERT_COOKIE"
@ -48,23 +49,27 @@
{
@private
PRUint32 mKey;
NSMutableDictionary* mDict;
NSMutableDictionary *mDict;
NSMutableArray *mNames;
NSMutableArray *mEnabled;
}
/**
* Dispatches a notification to Growl
*
* @param aName The notification name to dispatch
* @param aTitle The title of the notification
* @param aText The body of the notification
* @param aImage The image data, or [NSData data] if no image
* @param aKey The observer key to use as a lookup (or 0 if no observer)
* @param aCookie The string to be used as a cookie if there is an observer
*/
+ (void) notifyWithTitle:(const nsAString&)aTitle
description:(const nsAString&)aText
iconData:(NSData*)aImage
key:(PRUint32)aKey
cookie:(const nsAString&)aCookie;
+ (void) notifyWithName:(const nsAString&)aName
title:(const nsAString&)aTitle
description:(const nsAString&)aText
iconData:(NSData*)aImage
key:(PRUint32)aKey
cookie:(const nsAString&)aCookie;
/**
* Adds an nsIObserver that we can query later for dispatching obsevers.

Просмотреть файл

@ -42,6 +42,7 @@
#include "nsCOMPtr.h"
#include "nsServiceManagerUtils.h"
#include "nsIXULAppInfo.h"
#include "nsIStringBundle.h"
@implementation mozGrowlDelegate
@ -50,6 +51,38 @@
if ((self = [super init])) {
mKey = 0;
mDict = [[NSMutableDictionary dictionaryWithCapacity: 8] retain];
mNames = [[NSMutableArray alloc] init];
mEnabled = [[NSMutableArray alloc] init];
nsresult rv;
nsCOMPtr<nsIStringBundleService> bundleService =
do_GetService("@mozilla.org/intl/stringbundle;1", &rv);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIStringBundle> bundle;
rv = bundleService->CreateBundle(GROWL_STRING_BUNDLE_LOCATION,
getter_AddRefs(bundle));
if (NS_SUCCEEDED(rv)) {
nsString text;
rv = bundle->GetStringFromName(NS_LITERAL_STRING("general").get(),
getter_Copies(text));
if (NS_SUCCEEDED(rv)) {
NSString *s = [NSString stringWithCharacters: text.BeginReading()
length: text.Length()];
[mNames addObject: s];
[mEnabled addObject: s];
return self;
}
}
}
// Fallback
[mNames addObject: @"General Notification"];
[mEnabled addObject: @"General Notification"];
}
return self;
@ -59,15 +92,21 @@
{
[mDict release];
[mNames release];
[mEnabled release];
[super dealloc];
}
+ (void) notifyWithTitle:(const nsAString&)aTitle
description:(const nsAString&)aText
iconData:(NSData*)aImage
key:(PRUint32)aKey
cookie:(const nsAString&)aCookie
+ (void) notifyWithName:(const nsAString&)aName
title:(const nsAString&)aTitle
description:(const nsAString&)aText
iconData:(NSData*)aImage
key:(PRUint32)aKey
cookie:(const nsAString&)aCookie
{
NS_ASSERTION(aName.Length(), "No name specified for the alert!");
NSDictionary* clickContext = nil;
if (aKey) {
clickContext = [NSDictionary dictionaryWithObjectsAndKeys:
@ -85,7 +124,8 @@
length: aTitle.Length()]
description: [NSString stringWithCharacters: aText.BeginReading()
length: aText.Length()]
notificationName: NOTIFICATION_NAME
notificationName: [NSString stringWithCharacters: aName.BeginReading()
length: aName.Length()]
iconData: aImage
priority: 0
isSticky: NO
@ -105,10 +145,8 @@
- (NSDictionary *) registrationDictionaryForGrowl
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects: NOTIFICATION_NAME, nil],
GROWL_NOTIFICATIONS_ALL,
[NSArray arrayWithObjects: NOTIFICATION_NAME, nil],
GROWL_NOTIFICATIONS_DEFAULT,
mNames, GROWL_NOTIFICATIONS_ALL,
mEnabled, GROWL_NOTIFICATIONS_DEFAULT,
nil];
}

Просмотреть файл

@ -45,7 +45,8 @@
class nsAlertsImageLoadListener : public nsIStreamLoaderObserver
{
public:
nsAlertsImageLoadListener(const nsAString& aAlertTitle,
nsAlertsImageLoadListener(const nsAString &aName,
const nsAString& aAlertTitle,
const nsAString& aAlertText,
PRBool aAlertClickable,
const nsAString& aAlertCookie,
@ -54,6 +55,7 @@ public:
NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMLOADEROBSERVER
private:
nsString mName;
nsString mAlertTitle;
nsString mAlertText;
PRBool mAlertClickable;

Просмотреть файл

@ -45,12 +45,13 @@
NS_IMPL_ISUPPORTS1(nsAlertsImageLoadListener, nsIStreamLoaderObserver)
nsAlertsImageLoadListener::nsAlertsImageLoadListener(const nsAString& aAlertTitle,
nsAlertsImageLoadListener::nsAlertsImageLoadListener(const nsAString &aName,
const nsAString& aAlertTitle,
const nsAString& aAlertText,
PRBool aAlertClickable,
const nsAString& aAlertCookie,
PRUint32 aAlertListenerKey) :
mAlertTitle(aAlertTitle), mAlertText(aAlertText),
mName(aName), mAlertTitle(aAlertTitle), mAlertText(aAlertText),
mAlertClickable(aAlertClickable), mAlertCookie(aAlertCookie),
mAlertListenerKey(aAlertListenerKey)
{
@ -82,13 +83,14 @@ nsAlertsImageLoadListener::OnStreamComplete(nsIStreamLoader* aLoader,
}
#endif
[mozGrowlDelegate notifyWithTitle: mAlertTitle
description: mAlertText
iconData: NS_FAILED(aStatus) ? [NSData data] :
[NSData dataWithBytes: aResult
length: aLength]
key: mAlertListenerKey
cookie: mAlertCookie];
[mozGrowlDelegate notifyWithName: mName
title: mAlertTitle
description: mAlertText
iconData: NS_FAILED(aStatus) ? [NSData data] :
[NSData dataWithBytes: aResult
length: aLength]
key: mAlertListenerKey
cookie: mAlertCookie];
return NS_OK;
}

Просмотреть файл

@ -42,6 +42,8 @@
#include "nsNetUtil.h"
#include "nsCRT.h"
#include "nsCOMPtr.h"
#include "nsIStringBundle.h"
#include "nsIObserverService.h"
#import "mozGrowlDelegate.h"
#import "GrowlApplicationBridge.h"
@ -76,9 +78,12 @@ nsAlertsService::Init()
if ([GrowlApplicationBridge isGrowlInstalled] == NO)
return NS_ERROR_SERVICE_NOT_AVAILABLE;
mDelegate = new GrowlDelegateWrapper();
nsresult rv;
nsCOMPtr<nsIObserverService> os =
do_GetService("@mozilla.org/observer-service;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
return os->AddObserver(this, "final-ui-startup", PR_FALSE);
}
nsAlertsService::nsAlertsService() : mDelegate(nsnull) {}
@ -97,7 +102,6 @@ nsAlertsService::ShowAlertNotification(const nsAString& aImageUrl,
const nsAString& aAlertCookie,
nsIObserver* aAlertListener)
{
NS_ASSERTION(mDelegate->delegate == [GrowlApplicationBridge growlDelegate],
"Growl Delegate was not registered properly.");
@ -105,22 +109,41 @@ nsAlertsService::ShowAlertNotification(const nsAString& aImageUrl,
if (aAlertListener)
ind = [mDelegate->delegate addObserver: aAlertListener];
nsresult rv;
nsCOMPtr<nsIStringBundleService> bundleService =
do_GetService("@mozilla.org/intl/stringbundle;1", &rv);
// We don't want to fail just yet if we can't get the alert name
nsString name = NS_LITERAL_STRING("General Notification");
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIStringBundle> bundle;
rv = bundleService->CreateBundle(GROWL_STRING_BUNDLE_LOCATION,
getter_AddRefs(bundle));
if (NS_SUCCEEDED(rv)) {
rv = bundle->GetStringFromName(NS_LITERAL_STRING("general").get(),
getter_Copies(name));
if (NS_FAILED(rv))
name = NS_LITERAL_STRING("General Notification");
}
}
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aImageUrl);
rv = NS_NewURI(getter_AddRefs(uri), aImageUrl);
if (NS_FAILED(rv)) {
// image uri failed to resolve, so dispatch to growl with no image
[mozGrowlDelegate notifyWithTitle: aAlertTitle
description: aAlertText
iconData: [NSData data]
key: ind
cookie: aAlertCookie];
[mozGrowlDelegate notifyWithName: name
title: aAlertTitle
description: aAlertText
iconData: [NSData data]
key: ind
cookie: aAlertCookie];
return NS_OK;
}
nsCOMPtr<nsAlertsImageLoadListener> listener =
new nsAlertsImageLoadListener(aAlertTitle, aAlertText, aAlertClickable,
aAlertCookie, ind);
new nsAlertsImageLoadListener(name, aAlertTitle, aAlertText,
aAlertClickable, aAlertCookie, ind);
nsCOMPtr<nsIStreamLoader> loader;
rv = NS_NewStreamLoader(getter_AddRefs(loader), uri, listener);
@ -133,8 +156,15 @@ NS_IMETHODIMP
nsAlertsService::Observe(nsISupports* aSubject, const char* aTopic,
const PRUnichar* aData)
{
if (nsCRT::strcmp(aTopic, "app-startup") == 0) // registers with Growl here
if (nsCRT::strcmp(aTopic, "final-ui-startup") == 0) {
NS_ASSERTION([GrowlApplicationBridge growlDelegate] == nil,
"We already registered with Growl!");
mDelegate = new GrowlDelegateWrapper();
// registers with Growl
[GrowlApplicationBridge setGrowlDelegate: mDelegate->delegate];
}
return NS_OK;
}

Просмотреть файл

@ -61,8 +61,8 @@ nsAlertsServiceRegister(nsIComponentManager* aCompMgr,
char* prev = nsnull;
rv = catman->AddCategoryEntry("app-startup", "nsAlertsService",
NS_ALERTSERVICE_CONTRACTID, PR_TRUE, PR_TRUE,
&prev);
"service," NS_ALERTSERVICE_CONTRACTID, PR_TRUE,
PR_TRUE, &prev);
if (prev)
nsMemory::Free(prev);