Native use of growl on mac.

Allows use of custom icons in notifications (eg. album covers issue #17 ).

Objective-C++ is nasty.
This commit is contained in:
John Maguire 2010-02-24 21:44:14 +00:00
parent 27fe713113
commit 1c6eead38e
6 changed files with 160 additions and 22 deletions

View File

@ -1,6 +1,7 @@
#ifndef OSD_H
#define OSD_H
#include <QImage>
#include <QObject>
#include <QSystemTrayIcon>
@ -37,15 +38,19 @@ class OSD : public QObject {
private:
void ShowMessage(const QString& summary,
const QString& message = QString::null,
const QString& icon = QString::null);
const QString& message = QString(),
const QString& icon = QString());
// These are implemented in the OS-specific files
void Init();
bool CanShowNativeMessages() const;
void ShowMessageNative(const QString& summary,
const QString& message = QString::null,
const QString& icon = QString::null);
const QString& message = QString(),
const QString& icon = QString());
void ShowMessageNative(const QString& summary,
const QString& message,
const QImage& image);
private:
QSystemTrayIcon* tray_icon_;
@ -55,6 +60,11 @@ class OSD : public QObject {
#ifdef Q_WS_X11
NotifyNotification* notification_;
#endif
#ifdef Q_OS_DARWIN
class GrowlNotificationWrapper;
GrowlNotificationWrapper* wrapper_;
#endif // Q_OS_DARWIN
};
#endif // OSD_H

View File

@ -1,16 +0,0 @@
#include "osd.h"
#include <QtDebug>
void OSD::Init() {
}
bool OSD::CanShowNativeMessages() const {
return true;
}
void OSD::ShowMessageNative(const QString& summary, const QString& message,
const QString& icon) {
// This should use growl
tray_icon_->showMessage(summary, message, QSystemTrayIcon::NoIcon, timeout_);
}

130
src/osd_mac.mm Normal file
View File

@ -0,0 +1,130 @@
#include "osd.h"
#include <QBuffer>
#include <QByteArray>
#include <QFile>
#include <QtDebug>
#import <GrowlApplicationBridge.h>
@interface GrowlInterface :NSObject <GrowlApplicationBridgeDelegate> {
}
-(void) SendGrowlAlert:(NSString*)message title:(NSString*)title image:(NSData*)image;
-(void) ClickCallback; // Called when user clicks on notification.
@end
@implementation GrowlInterface
-(id) init {
if ((self = [super init])) {
[GrowlApplicationBridge setGrowlDelegate:self];
}
return self;
}
-(void) dealloc {
[super dealloc];
}
-(NSDictionary*) registrationDictionaryForGrowl {
NSArray* array = [NSArray arrayWithObjects:@"next_track", nil]; // Valid notification names.
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1],
@"TicketVersion",
array,
@"AllNotifications",
array,
@"DefaultNotifications",
nil];
return dict;
}
-(void) growlNotificationWasClicked:(id)clickContext {
if (clickContext) {
[self ClickCallback];
}
return;
}
-(void) SendGrowlAlert:(NSString*)message title:(NSString*)title image:(NSData*)image {
[GrowlApplicationBridge notifyWithTitle:title
description:message
notificationName:@"next_track"
iconData:image
priority:0
isSticky:NO
clickContext:@"click_callback"]; // String sent to our callback.
}
-(void) ClickCallback {
qDebug() << "Growl notification clicked!";
return;
}
@end
class OSD::GrowlNotificationWrapper {
public:
GrowlNotificationWrapper() {
pool_ = [[NSAutoreleasePool alloc] init];
growl_interface_ = [[GrowlInterface alloc] init];
}
~GrowlNotificationWrapper() {
[growl_interface_ release];
[pool_ release];
}
void ShowMessage(const QString& summary,
const QString& message,
const QImage& image) {
NSString* mac_message = [[NSString alloc] initWithUTF8String:message.toUtf8().data()];
NSString* mac_summary = [[NSString alloc] initWithUTF8String:summary.toUtf8().data()];
NSData* image_data = nil;
// Growl expects raw TIFF data.
// This is nasty but it keeps the API nice.
if (!image.isNull()) {
QByteArray tiff_data;
QBuffer tiff(&tiff_data);
image.save(&tiff, "TIFF");
image_data = [NSData dataWithBytes:tiff_data.constData() length:tiff_data.size()];
}
[growl_interface_ SendGrowlAlert:mac_message
title:mac_summary
image:image_data];
[image_data release];
[mac_message release];
[mac_summary release];
}
private:
GrowlInterface* growl_interface_;
NSAutoreleasePool* pool_;
};
void OSD::Init() {
wrapper_ = new GrowlNotificationWrapper;
}
bool OSD::CanShowNativeMessages() const {
return true;
}
void OSD::ShowMessageNative(const QString& summary, const QString& message,
const QString& icon) {
Q_UNUSED(icon);
wrapper_->ShowMessage(summary, message, QImage());
}
void OSD::ShowMessageNative(const QString& summary,
const QString& message,
const QImage& image) {
wrapper_->ShowMessage(summary, message, image);
}

View File

@ -11,5 +11,10 @@ bool OSD::CanShowNativeMessages() const {
void OSD::ShowMessageNative(const QString&, const QString&,
const QString&) {
qWarning() << __PRETTY_FUNCTION__ << ": NOT IMPLEMENTED";
}
void OSD::ShowMessageNative(const QString& summary, const QString& message,
const QImage& image) {
qWarning() << __PRETTY_FUNCTION__ << ": NOT IMPLEMENTED";
}

View File

@ -45,3 +45,8 @@ void OSD::ShowMessageNative(const QString& summary, const QString& message,
g_error_free(error);
}
}
void OSD::ShowMessageNative(const QString& summary, const QString& message,
const QImage& image) {
qWarning() << __PRETTY_FUNCTION__ << ": NOT IMPLEMENTED";
}

View File

@ -154,7 +154,11 @@ QMAKE_CXXFLAGS += -Wall -Werror=non-virtual-dtor -Woverloaded-virtual
-ltag \
-lxine \
-framework \
Carbon
Carbon \
-framework \
Growl \
-framework \
Cocoa
}
!mac {
QMAKE_CXXFLAGS += $$system(taglib-config --cflags)
@ -175,7 +179,7 @@ unix:!macx:!fedora-win32-cross {
LIBS += $$system(pkg-config --libs libnotify)
}
}
macx:SOURCES += osd_mac.cpp
macx:SOURCES += osd_mac.mm
win32|fedora-win32-cross:SOURCES += osd_win.cpp
# QXT