1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#import <Foundation/Foundation.h>
#import <DBusKit/DBusKit.h>
#import "AlarmBackend.h"
#import "Alarm.h"
#import "Element.h"
@protocol Notifications
- (NSArray *)GetCapabilities;
- (NSNumber *)Notify:(NSString *)appname :(uint)replaceid :(NSString *)appicon :(NSString *)summary :(NSString *)body :(NSArray *)actions :(NSDictionary *)hints :(int)expires;
@end
@interface DBusBackend : AlarmBackend
@end
static NSString * const DBUS_BUS = @"org.freedesktop.Notifications";
static NSString * const DBUS_PATH = @"/org/freedesktop/Notifications";
@implementation DBusBackend
+ (NSString *)backendName
{
return @"DBus desktop notification";
}
- (enum icalproperty_action)backendType
{
return ICAL_ACTION_DISPLAY;
}
- (id)init
{
NSConnection *c;
id <NSObject,Notifications> remote;
NSArray *caps;
self = [super init];
if (self) {
NS_DURING
{
c = [NSConnection connectionWithReceivePort:[DKPort port] sendPort:AUTORELEASE([[DKPort alloc] initWithRemote:DBUS_BUS])];
if (!c) {
NSLog(@"Unable to create a connection to %@", DBUS_BUS);
DESTROY(self);
}
remote = (id <NSObject,Notifications>)[c proxyAtPath:DBUS_PATH];
if (!remote) {
NSLog(@"Unable to create a proxy for %@", DBUS_PATH);
DESTROY(self);
}
caps = [remote GetCapabilities];
if (!caps) {
NSLog(@"No response to GetCapabilities method");
DESTROY(self);
}
[c invalidate];
}
NS_HANDLER
{
NSLog(@"%@", [localException description]);
NSLog(@"Exception during DBus backend setup, backend disabled");
DESTROY(self);
}
NS_ENDHANDLER
}
return self;
}
- (void)display:(Alarm *)alarm
{
NSConnection *c;
id <NSObject,Notifications> remote;
Element *el = [alarm element];
NSString *desc;
c = [NSConnection connectionWithReceivePort:[DKPort port] sendPort:[[DKPort alloc] initWithRemote:DBUS_BUS]];
remote = (id <NSObject,Notifications>)[c proxyAtPath:DBUS_PATH];
if ([el text])
desc = [NSString stringWithFormat:@"%@\n\n%@ : %@", [[[el nextActivationDate] calendarDate] descriptionWithCalendarFormat:[[NSUserDefaults standardUserDefaults] objectForKey:NSShortTimeDateFormatString]], [el summary], [[el text] string]];
else
desc = [NSString stringWithFormat:@"%@\n\n%@", [[[el nextActivationDate] calendarDate] descriptionWithCalendarFormat:[[NSUserDefaults standardUserDefaults] objectForKey:NSShortTimeDateFormatString]], [el summary]];
[remote Notify:@"SimpleAgenda"
:0
:@""
:_(@"SimpleAgenda Reminder !")
:desc
:[NSArray array]
:[NSDictionary dictionary]
:-1];
[c invalidate];
}
@end
|