Core/Source/GrowlNotificationTicket.m
author boredzo
Tue Jul 15 12:25:11 2008 +0000 (2008-07-15)
changeset 4147 a4a2e33ce568
parent 3512 184e24eedcfa
child 4489 9c0b9f927d0e
permissions -rw-r--r--
Release the sound object. This was a leak that the clang static analyzer found, IIRC.
     1 //
     2 //  GrowlNotificationTicket.m
     3 //  Growl
     4 //
     5 //  Created by Karl Adam on 01.10.05.
     6 //  Copyright 2005-2006 matrixPointer. All rights reserved.
     7 //
     8 
     9 #import "GrowlNotificationTicket.h"
    10 #import "GrowlApplicationTicket.h"
    11 #import "GrowlPluginController.h"
    12 #import "GrowlDisplayPlugin.h"
    13 #include "CFDictionaryAdditions.h"
    14 #include "CFMutableDictionaryAdditions.h"
    15 
    16 @implementation GrowlNotificationTicket
    17 
    18 + (GrowlNotificationTicket *) notificationWithName:(NSString *)theName {
    19 	return [[[GrowlNotificationTicket alloc] initWithName:theName] autorelease];
    20 }
    21 
    22 + (GrowlNotificationTicket *) notificationWithDictionary:(NSDictionary *)dict {
    23 	return [[[GrowlNotificationTicket alloc] initWithDictionary:dict] autorelease];
    24 }
    25 
    26 - (GrowlNotificationTicket *) initWithDictionary:(NSDictionary *)dict {
    27 	NSString *inName = getObjectForKey(dict, @"Name");
    28 
    29 	NSString *inHumanReadableName = getObjectForKey(dict, @"HumanReadableName");
    30 
    31 	NSString *inNotificationDescription = getObjectForKey(dict, @"NotificationDescription");
    32 
    33 	id value = getObjectForKey(dict, @"Priority");
    34 	enum GrowlPriority inPriority = value ? [value intValue] : GrowlPriorityUnset;
    35 
    36 	BOOL inEnabled = getBooleanForKey(dict, @"Enabled");
    37 
    38 	int  inSticky  = getIntegerForKey(dict, @"Sticky");
    39 	inSticky = (inSticky >= 0 ? (inSticky > 0 ? NSOnState : NSOffState) : NSMixedState);
    40 
    41 	NSString *inDisplay = [dict objectForKey:@"Display"];
    42 	NSString *inSound = [dict objectForKey:@"Sound"];
    43 
    44 	return [self initWithName:inName
    45 			humanReadableName:inHumanReadableName
    46 	  notificationDescription:inNotificationDescription
    47 					 priority:inPriority
    48 					  enabled:inEnabled
    49 					   sticky:inSticky
    50 			displayPluginName:inDisplay
    51 						sound:inSound];
    52 }
    53 
    54 - (GrowlNotificationTicket *) initWithName:(NSString *)theName {
    55 	return [self initWithName:theName
    56 			humanReadableName:nil
    57 	  notificationDescription:nil
    58 					 priority:GrowlPriorityUnset
    59 					  enabled:YES
    60 					   sticky:NSMixedState
    61 			displayPluginName:nil
    62 						sound:nil];
    63 }
    64 
    65 - (GrowlNotificationTicket *) initWithName:(NSString *)inName
    66 						 humanReadableName:(NSString *)inHumanReadableName
    67 				   notificationDescription:(NSString *)inNotificationDescription
    68 								  priority:(enum GrowlPriority)inPriority
    69 								   enabled:(BOOL)inEnabled
    70 									sticky:(int)inSticky
    71 						 displayPluginName:(NSString *)display
    72 									 sound:(NSString *)inSound
    73 {
    74 	if ((self = [self init])) {
    75 		name					= [inName retain];
    76 		humanReadableName		= [inHumanReadableName retain];
    77 		notificationDescription = [inNotificationDescription retain];
    78 		priority				= inPriority;
    79 		enabled					= inEnabled;
    80 		sticky					= inSticky;
    81 		displayPluginName		= [display copy];
    82 		sound					= [inSound retain];
    83 	}
    84 	return self;
    85 }
    86 
    87 - (void) dealloc {
    88 	[name release];
    89 	[humanReadableName release];
    90 	[displayPluginName release];
    91 	[notificationDescription release];
    92 	[sound release];
    93 
    94 	[super dealloc];
    95 }
    96 
    97 #pragma mark -
    98 
    99 - (NSDictionary *) dictionaryRepresentation {
   100 	NSNumber    *enabledValue = [[NSNumber alloc] initWithBool:enabled];
   101 	NSNumber     *stickyValue = [[NSNumber alloc] initWithInt:sticky];
   102 	NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   103 		name,         @"Name",
   104 		enabledValue, @"Enabled",
   105 		stickyValue,  @"Sticky",
   106 		nil];
   107 	[enabledValue release];
   108 	[stickyValue  release];
   109 	if (priority != GrowlPriorityUnset)
   110 		setIntegerForKey(dict, @"Priority", priority);
   111 	if (displayPluginName)
   112 		setObjectForKey(dict, @"Display", displayPluginName);
   113 	if (notificationDescription)
   114 		setObjectForKey(dict, @"NotificationDescription", notificationDescription);
   115 	if (humanReadableName)
   116 		setObjectForKey(dict, @"HumanReadableName", humanReadableName);
   117 	if (sound)
   118 		setObjectForKey(dict, @"Sound", sound);
   119 
   120 	return dict;
   121 }
   122 
   123 - (NSString *) description {
   124 	return [NSString stringWithFormat:@"<%@ %p %@>", [self class], self, [[self dictionaryRepresentation] description]];
   125 }
   126 
   127 - (BOOL) isEqualToNotification:(GrowlNotificationTicket *) other {
   128 	return [[self name] isEqualToString:[other name]];
   129 }
   130 #define GENERIC_EQUALITY_METHOD(other) {                                                                      \
   131 	return ([other isKindOfClass:[GrowlNotificationTicket class]] && [self isEqualToNotification:other]); \
   132 }
   133 //NSObject's way
   134 - (BOOL) isEqualTo:(id) other GENERIC_EQUALITY_METHOD(other)
   135 //Object's way
   136 - (BOOL) isEqual:(id) other GENERIC_EQUALITY_METHOD(other)
   137 #undef GENERIC_EQUALITY_METHOD
   138 
   139 #pragma mark -
   140 
   141 - (NSString *) name {
   142 	return [[name retain] autorelease];
   143 }
   144 
   145 - (NSString *) humanReadableName {
   146 	return (humanReadableName ? [[humanReadableName retain] autorelease] : [self name]);
   147 }
   148 
   149 - (void) setHumanReadableName:(NSString *)inHumanReadableName {
   150 	if (humanReadableName != inHumanReadableName) {
   151 		[humanReadableName release];
   152 		humanReadableName = [inHumanReadableName retain];
   153 	}
   154 }
   155 
   156 - (NSString *) notificationDescription {
   157 	return notificationDescription;
   158 }
   159 
   160 - (void) setNotificationDescription:(NSString *)inNotificationDescription {
   161 	if (notificationDescription != inNotificationDescription) {
   162 		[notificationDescription release];
   163 		notificationDescription = [inNotificationDescription retain];
   164 	}
   165 }
   166 
   167 - (enum GrowlPriority) priority {
   168 	return priority;
   169 }
   170 - (void) setPriority:(enum GrowlPriority)newPriority {
   171 	priority = newPriority;
   172 	[ticket synchronize];
   173 }
   174 
   175 - (BOOL) enabled {
   176 	return enabled;
   177 }
   178 - (void) setEnabled:(BOOL)flag {
   179 	enabled = flag;
   180 	[ticket setUseDefaults:NO];
   181 	[ticket synchronize];
   182 }
   183 
   184 - (GrowlApplicationTicket *) ticket {
   185 	return ticket;
   186 }
   187 - (void) setTicket:(GrowlApplicationTicket *)newTicket {
   188 	ticket = newTicket;
   189 }
   190 
   191 // With sticky, 1 is on, 0 is off, -1 means use what's passed
   192 // This corresponds to NSOnState, NSOffState, and NSMixedState
   193 - (int) sticky {
   194 	return sticky;
   195 }
   196 - (void) setSticky:(int)value {
   197 	sticky = value;
   198 	[ticket synchronize];
   199 }
   200 
   201 - (NSString *) displayPluginName {
   202 	return displayPluginName;
   203 }
   204 - (void) setDisplayPluginName:(NSString *)pluginName {
   205 	[displayPluginName release];
   206 	displayPluginName = [pluginName copy];
   207 	displayPlugin = nil;
   208 	[ticket synchronize];
   209 }
   210 
   211 - (GrowlDisplayPlugin *) displayPlugin {
   212 	if (!displayPlugin && displayPluginName)
   213 		displayPlugin = (GrowlDisplayPlugin *)[[[GrowlPluginController sharedController] displayPluginDictionaryWithName:displayPluginName author:nil version:nil type:nil] pluginInstance];
   214 	return displayPlugin;
   215 }
   216 
   217 - (NSComparisonResult) humanReadableNameCompare:(GrowlNotificationTicket *)inTicket {
   218 	return [[self humanReadableName] caseInsensitiveCompare:[inTicket humanReadableName]];
   219 }
   220 
   221 - (NSString *) sound {
   222 	return sound;
   223 }
   224 - (void) setSound:(NSString *)value {
   225 	if (value != sound) {
   226 		[sound release];
   227 		sound = [value retain];
   228 		[ticket synchronize];
   229 	}
   230 }
   231 
   232 @end