William Bowling is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

wbowling / adium (fork of adium / adium)

Fork of Adium for patches/improvements

Clone this repository (size: 338.7 MB): HTTPS / SSH
hg clone https://bitbucket.org/wbowling/adium
hg clone ssh://hg@bitbucket.org/wbowling/adium

adium / Source / NEHGrowlPlugin.m

  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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* 
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "NEHGrowlPlugin.h"
#import "CBGrowlAlertDetailPane.h"
#import <Adium/AIChatControllerProtocol.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIContentControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import <Adium/AIContactAlertsControllerProtocol.h>
#import <Adium/AIStatusControllerProtocol.h>
#import <Adium/AIAccount.h>
#import <Adium/AIChat.h>
#import <Adium/AIContentObject.h>
#import <Adium/AIListContact.h>
#import <Adium/AIListObject.h>
#import <Adium/AIServiceIcons.h>
#import <Adium/AIStatus.h>
#import <Adium/ESFileTransfer.h>
#import <AIUtilities/AIApplicationAdditions.h>
#import <AIUtilities/AIImageAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <AIUtilities/AIMutableStringAdditions.h>
#import <AIUtilities/AIObjectAdditions.h>
#import <Growl-WithInstaller/Growl.h>

//#define GROWL_DEBUG 1

#define GROWL_ALERT                                                     AILocalizedString(@"Display a Growl notification",nil)
#define GROWL_STICKY_ALERT                                      AILocalizedString(@"Display a sticky Growl notification",nil)

#define GROWL_INSTALLATION_WINDOW_TITLE         AILocalizedString(@"Growl Installation Recommended", "Growl installation window title")
#define GROWL_UPDATE_WINDOW_TITLE                       AILocalizedString(@"Growl Update Available", "Growl update window title")

#define GROWL_INSTALLATION_EXPLANATION          AILocalizedString(@"Adium uses the Growl notification system to provide a configurable interface to display status changes, incoming messages and more.\n\nIt is strongly recommended that you allow Adium to automatically install Growl; no download is required.","Growl installation explanation")
#define GROWL_UPDATE_EXPLANATION                        AILocalizedString(@"Adium uses the Growl notification system to provide a configurable interface to display status changes, incoming messages and more.\n\nThis release of Adium includes an updated version of Growl. It is strongly recommended that you allow Adium to automatically update Growl; no download is required.","Growl update explanation")

#define GROWL_TEXT_SIZE 11

#define GROWL_EVENT_ALERT_IDENTIFIER            @"Growl"

#define KEY_FILE_TRANSFER_ID    @"fileTransferUniqueID"
#define KEY_CHAT_ID                             @"uniqueChatID"
#define KEY_LIST_OBJECT_ID              @"internalObjectID"

@interface NEHGrowlPlugin ()
- (NSAttributedString *)_growlInformationForUpdate:(BOOL)isUpdate;
- (NSString *)eventQueueKeyForEventID:(NSString *)eventID
                                                           inChat:(AIChat *)chat;

- (void)postSingleEventID:(NSString *)eventID
                        forListObject:(AIListObject *)listObject
                          withDetails:(NSDictionary *)details
                                 userInfo:(id)userInfo;

- (void)postMultipleEventID:(NSString *)eventID
                                         sticky:(BOOL)sticky
                                   priority:(NSInteger)priority
                          forListObject:(AIListObject *)listObject
                                        forChat:(AIChat *)chat
                                  withCount:(NSUInteger)count;
@end
 
/*!
 * @class NEHGrowlPlugin
 * @brief Implements Growl functionality in Adium
 *
 * This class manages the Growl event type, and controls the display of Growl notifications that Adium generates.
 */
@implementation NEHGrowlPlugin

/*!
 * @brief Initialize the Growl plugin
 *
 * Waits for Adium to finish launching before we perform further actions so all events are registered.
 */
- (void)installPlugin
{
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                                   selector:@selector(adiumFinishedLaunching:)
                                                                           name:AIApplicationDidFinishLoadingNotification
                                                                         object:nil];
        
        queuedEvents = [[NSMutableDictionary alloc] init];
}

- (void)dealloc
{
        [queuedEvents release]; queuedEvents = nil;
        
        [super dealloc];
}

/*!
 * @brief Adium finished launching
 *
 * Delays one more run loop so any events which are registered on this notification are guaranteed to be complete
 * regardless of the order in which the observers are called.
 */
- (void)adiumFinishedLaunching:(NSNotification *)notification
{
        [self performSelector:@selector(beginGrowling)
                           withObject:nil
                           afterDelay:0];

        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                                  name:AIApplicationDidFinishLoadingNotification
                                                                                object:nil];
}

/*!
 * @brief Begin accepting Growl events
 */
- (void)beginGrowling
{
        [GrowlApplicationBridge setGrowlDelegate:self];

        //Install our contact alert
        [adium.contactAlertsController registerActionID:GROWL_EVENT_ALERT_IDENTIFIER withHandler:self];
        
#ifdef GROWL_DEBUG
        [GrowlApplicationBridge notifyWithTitle:@"We have found a witch."
                                                                description:@"May we burn her?"
                                                   notificationName:CONTENT_MESSAGE_RECEIVED
                                                                   iconData:nil
                                                                   priority:0
                                                                   isSticky:YES
                                                           clickContext:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                   @"AIM.tekjew", @"internalObjectID",
                                                                   CONTENT_MESSAGE_RECEIVED, @"eventID",
                                                                   nil]];
#endif
}

#pragma mark AIActionHandler
/*!
 * @brief Returns a short description of Growl events
 */
- (NSString *)shortDescriptionForActionID:(NSString *)actionID
{
        return GROWL_ALERT;
}

/*!
 * @brief Returns a long description of Growl events
 *
 * The long description reflects the "sticky"-ness of the notification.
 */
- (NSString *)longDescriptionForActionID:(NSString *)actionID withDetails:(NSDictionary *)details
{
        if ([[details objectForKey:KEY_GROWL_ALERT_STICKY] boolValue]) {
                return GROWL_STICKY_ALERT;
        } else {
                return GROWL_ALERT;
        }
}

/*!
 * @brief Returns the image associated with the Growl event
 */
- (NSImage *)imageForActionID:(NSString *)actionID
{
        return [NSImage imageNamed:@"GrowlAlert" forClass:[self class]];
}

/*!
 * @brief Post a notification for Growl for display
 *
 * This method is called when by Adium when a Growl alert is activated. It passes this information on to Growl, which displays a notificaion.
 *
 * @param actionID The Action ID being performed, in this case the Growl plugin's Action ID.
 * @param listObject The list object the event is related to
 * @param details A dictionary containing additional information about the event
 * @param eventID The ID of the event (e.g. new message, contact went away, etc)
 * @param userInfo Any additional information
 */
- (BOOL)performActionID:(NSString *)actionID forListObject:(AIListObject *)listObject withDetails:(NSDictionary *)details triggeringEventID:(NSString *)eventID userInfo:(id)userInfo
{
        // Don't show growl notifications if we're silencing growl.
        if ([adium.statusController.activeStatusState silencesGrowl]) {
                return NO;
        }
        
        // Get the chat if it's appropriate.
        AIChat *chat = nil;
        
        if ([userInfo respondsToSelector:@selector(objectForKey:)]) {
                chat = [userInfo objectForKey:@"AIChat"];
                AIContentObject *contentObject = [userInfo objectForKey:@"AIContentObject"];
                if (contentObject.source) {
                        listObject = contentObject.source;
                }
        }
        
        // Add this event to the queue.
        NSString *queueKey = [self eventQueueKeyForEventID:eventID inChat:chat];
        
        NSMutableArray *events = [queuedEvents objectForKey:queueKey];
        
        if (!events)
                events = [NSMutableArray array];
        
        NSMutableDictionary *eventDetails = [NSMutableDictionary dictionary];
        
        if (listObject)
                [eventDetails setValue:listObject forKey:@"AIListObject"];
        
        if (userInfo)
                [eventDetails setValue:userInfo forKey:@"UserInfo"];
        
        if (details)
                [eventDetails setValue:details forKey:@"Details"];
        
        [events addObject:eventDetails];
        
        [queuedEvents setValue:events forKey:queueKey];
        
        // wtb cancelPreviousPerformRequestsWithTarget:selector:object:object:
        // chat may be nil
        NSDictionary *queueCall = [NSDictionary dictionaryWithObjectsAndKeys:eventID, @"EventID", chat, @"AIChat", nil];
        
        // Trigger the queue to be cleared in GROWL_QUEUE_WAIT seconds.
        [NSObject cancelPreviousPerformRequestsWithTarget:self
                                                                                         selector:@selector(clearQueue:)
                                                                                           object:queueCall];
        
        [self performSelector:@selector(clearQueue:)
                           withObject:queueCall
                           afterDelay:GROWL_QUEUE_WAIT];
        
        // If the queue has <GROWL_QUEUE_POST_COUNT entries already, post this one immediately.
        if (events.count < GROWL_QUEUE_POST_COUNT) {
                [self postSingleEventID:eventID
                                  forListObject:listObject
                                        withDetails:details
                                           userInfo:userInfo];
        }

        return YES;
}

/*!
 * @brief Returns our details pane, an instance of <tt>CBGrowlAlertDetailPane</tt>
 */
- (AIModularPane *)detailsPaneForActionID:(NSString *)actionID
{
    return [CBGrowlAlertDetailPane actionDetailsPane];
}

/*!
 * @brief Allow multiple actions?
 *
 * This action should not be performed multiple times for the same triggering event.
 */
- (BOOL)allowMultipleActionsWithID:(NSString *)actionID
{
        return NO;
}

#pragma mark Event Queue
- (NSString *)eventQueueKeyForEventID:(NSString *)eventID
                                                           inChat:(AIChat *)chat
{
        if (chat) {
                return [NSString stringWithFormat:@"%@-%@", eventID, chat.internalObjectID];
        } else {
                return eventID;
        }
}

- (void)clearQueue:(NSDictionary *)callDict
{
        // Grab our actual arguments.
        NSString *eventID = [callDict objectForKey:@"EventID"];
        AIChat *chat = [callDict objectForKey:@"AIChat"];
        
        // Look for events.
        NSString *queueKey = [self eventQueueKeyForEventID:eventID inChat:chat];
        NSMutableArray *events = [queuedEvents objectForKey:queueKey];
        
        // If for some reason we don't have any events, bail.
        if (!events.count) {
                AILogWithSignature(@"Called to clear queue with no events. EventID: %@ chat: %@", eventID, chat);
                return;
        }
        
        // Remove the first GROWL_QUEUE_POST_COUNT entries, since we've already posted about them.
        NSRange removeRange = NSMakeRange(0,
                                                                          (events.count > GROWL_QUEUE_POST_COUNT ? GROWL_QUEUE_POST_COUNT : events.count));

        [events removeObjectsInRange:removeRange];
        
        if (events.count == 1) {
                // Seeing "1 message" is just silly!
                
                NSDictionary *event = [events objectAtIndex:0];
                
                [self postSingleEventID:eventID
                                  forListObject:[event objectForKey:@"AIListObject"]
                                        withDetails:[event objectForKey:@"Details"]
                                           userInfo:[event objectForKey:@"UserInfo"]];
                
        } else if(events.count) {               
                // We have a bunch of events; let's combine them.
                AIListObject *overallListObject = nil;

                // If all events are from the same listObject, let's use that one in the message.
                NSArray *listObjects = [events valueForKeyPath:@"@distinctUnionOfObjects.AIListObject"];
                
                if (listObjects.count == 1) {
                        overallListObject = [listObjects objectAtIndex:0];
                }
                
                AILog(@"Posting multiple event - %@ %@ %@ %d", eventID, overallListObject, chat, events.count);
                
                // Use any random event for sticky.
                NSDictionary *anyEventDetails = [[events objectAtIndex:0] objectForKey:@"Details"];
                
                BOOL sticky = [[anyEventDetails objectForKey:KEY_GROWL_ALERT_STICKY] boolValue];
                NSInteger priority = [[anyEventDetails objectForKey:KEY_GROWL_PRIORITY] integerValue];
                
                // Post the events combined. Use any random event to see if sticky.
                [self postMultipleEventID:eventID
                                                   sticky:sticky
                                                 priority:priority
                                        forListObject:overallListObject
                                                  forChat:chat
                                                withCount:events.count];
        }
        
        // Clear our queue; we're done.
        [queuedEvents setValue:nil forKey:queueKey];
}

- (void)postSingleEventID:(NSString *)eventID
                        forListObject:(AIListObject *)listObject
                          withDetails:(NSDictionary *)details
                                 userInfo:(id)userInfo
{
        NSString                        *title, *description;
        AIChat                          *chat = nil;
        NSData                          *iconData = nil;
        NSMutableDictionary     *clickContext = [NSMutableDictionary dictionary];
        NSString                        *identifier = nil;

        //For a message event, listObject should become whoever sent the message
        if ([adium.contactAlertsController isMessageEvent:eventID] &&
                [userInfo respondsToSelector:@selector(objectForKey:)] &&
                [userInfo objectForKey:@"AIContentObject"]) {
                AIContentObject *contentObject = [userInfo objectForKey:@"AIContentObject"];
                AIListObject    *source = [contentObject source];
                chat = [userInfo objectForKey:@"AIChat"];
                
                if (source) listObject = source;
        }
        
        [clickContext setObject:eventID
                                         forKey:@"eventID"];
        
        if (listObject) {
                if ([listObject isKindOfClass:[AIListContact class]]) {
                        //Use the parent
                        listObject = [(AIListContact *)listObject parentContact];
                        title = [listObject longDisplayName];
                } else {
                        title = listObject.displayName;
                }
                
                iconData = [listObject userIconData];
                
                if (!iconData) {
                        iconData = [[AIServiceIcons serviceIconForObject:listObject
                                                                                                                type:AIServiceIconLarge
                                                                                                   direction:AIIconNormal] TIFFRepresentation];
                }
                
                if (chat) {
                        [clickContext setObject:chat.uniqueChatID
                                                         forKey:KEY_CHAT_ID];
                        
                } else {
                        if ([userInfo isKindOfClass:[ESFileTransfer class]] &&
                                [eventID isEqualToString:FILE_TRANSFER_COMPLETE]) {
                                [clickContext setObject:[(ESFileTransfer *)userInfo uniqueID]
                                                                 forKey:KEY_FILE_TRANSFER_ID];
                                
                        } else {
                                [clickContext setObject:listObject.internalObjectID
                                                                 forKey:KEY_LIST_OBJECT_ID];
                        }
                }
                
        } else {
                if (chat) {
                        title = chat.displayName;
                        
                        [clickContext setObject:chat.uniqueChatID
                                                         forKey:KEY_CHAT_ID];
                        
                        //If we have no listObject or we have a name, we are a group chat and
                        //should use the account's service icon
                        iconData = [[AIServiceIcons serviceIconForObject:chat.account
                                                                                                                type:AIServiceIconLarge
                                                                                                   direction:AIIconNormal] TIFFRepresentation];
                        
                } else {
                        title = @"Adium";
                }
        }
        
        description = [adium.contactAlertsController naturalLanguageDescriptionForEventID:eventID
                                   listObject:listObject
                                   userInfo:userInfo
                                   includeSubject:NO];
        
        if (([eventID isEqualToString:CONTACT_STATUS_ONLINE_YES] ||
                 [eventID isEqualToString:CONTACT_STATUS_ONLINE_NO] ||
                 [eventID isEqualToString:CONTACT_STATUS_AWAY_YES] ||
                 [eventID isEqualToString:CONTACT_SEEN_ONLINE_YES] ||
                 [eventID isEqualToString:CONTACT_SEEN_ONLINE_NO]) && 
                [(AIListContact *)listObject contactListStatusMessage]) {
                NSString *statusMessage = [[adium.contentController filterAttributedString:[(AIListContact *)listObject contactListStatusMessage]
                                                                        usingFilterType:AIFilterContactList
                                                                        direction:AIFilterIncoming
                                                                        context:listObject] string];
                statusMessage = [[[statusMessage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy] autorelease];
                
                /* If the message contains line breaks, start it on a new line */
                description = [NSString stringWithFormat:@"%@:%@%@",
                                           description,
                                           (([statusMessage rangeOfLineBreakCharacter].location != NSNotFound) ? @"\n" : @" "),
                                           statusMessage];
        }
        
        if (listObject && [adium.contactAlertsController isContactStatusEvent:eventID]) {
                identifier = listObject.internalObjectID;
        }
        
        NSAssert5((title || description),
                          @"Growl notify error: EventID %@, listObject %@, userInfo %@\nGave Title \"%@\" description \"%@\"",
                          eventID,
                          listObject,
                          userInfo,
                          title,
                          description);
        
        AILog(@"Posting Growl notification: Event ID: %@, listObject: %@, chat: %@, description: %@",
                  eventID, listObject, chat, description);
        
        [GrowlApplicationBridge notifyWithTitle:title
                                                                description:description
                                                   notificationName:eventID
                                                                   iconData:iconData
                                                                   priority:[[details objectForKey:KEY_GROWL_PRIORITY] integerValue]
                                                                   isSticky:[[details objectForKey:KEY_GROWL_ALERT_STICKY] boolValue]
                                                           clickContext:clickContext
                                                                 identifier:identifier];
}

- (void)postMultipleEventID:(NSString *)eventID
                                         sticky:(BOOL)sticky
                                   priority:(NSInteger)priority
                          forListObject:(AIListObject *)listObject
                                        forChat:(AIChat *)chat
                                  withCount:(NSUInteger)count
{
        NSString                        *title, *description;
        NSData                          *iconData = nil;
        NSMutableDictionary     *clickContext = [NSMutableDictionary dictionary];
        NSString                        *identifier = nil;
        
        [clickContext setObject:eventID
                                         forKey:@"eventID"];
        
        if (listObject) {
                if ([listObject isKindOfClass:[AIListContact class]]) {
                        //Use the parent
                        listObject = [(AIListContact *)listObject parentContact];
                        title = [listObject longDisplayName];
                } else {
                        title = listObject.displayName;
                }
                
                iconData = [listObject userIconData];
                
                if (!iconData) {
                        iconData = [[AIServiceIcons serviceIconForObject:listObject
                                                                                                                type:AIServiceIconLarge
                                                                                                   direction:AIIconNormal] TIFFRepresentation];
                }
                
                if (chat) {
                        [clickContext setObject:chat.uniqueChatID
                                                         forKey:KEY_CHAT_ID];
                        
                } else {
                        [clickContext setObject:listObject.internalObjectID
                                                         forKey:KEY_LIST_OBJECT_ID];
                }
                
        } else {
                if (chat) {
                        title = chat.displayName;
                        
                        [clickContext setObject:chat.uniqueChatID
                                                         forKey:KEY_CHAT_ID];
                        
                        //If we have no listObject or we have a name, we are a group chat and
                        //should use the account's service icon
                        iconData = [[AIServiceIcons serviceIconForObject:chat.account
                                                                                                                type:AIServiceIconLarge
                                                                                                   direction:AIIconNormal] TIFFRepresentation];
                        
                } else {
                        title = @"Adium";
                }
        }
        
        description = [adium.contactAlertsController descriptionForCombinedEventID:eventID
                                   forListObject:listObject
                                   forChat:chat
                                   withCount:count];
        
        if (listObject && [adium.contactAlertsController isContactStatusEvent:eventID]) {
                identifier = listObject.internalObjectID;
        }
        
        NSAssert5((title || description),
                          @"Growl notify error: EventID %@, listObject %@, chat %@\nGave Title \"%@\" description \"%@\"",
                          eventID,
                          listObject,
                          chat,
                          title,
                          description);
        
        AILog(@"Posting combined Growl notification: Event ID: %@, listObject: %@, chat: %@, description: %@",
                  eventID, listObject, chat, description);
        
        [GrowlApplicationBridge notifyWithTitle:title
                                                                description:description
                                                   notificationName:eventID
                                                                   iconData:iconData
                                                                   priority:priority
                                                                   isSticky:sticky
                                                           clickContext:clickContext
                                                                 identifier:identifier];
}

#pragma mark Growl

/*!
 * @brief Returns the application name Growl will use
 */
- (NSString *)applicationNameForGrowl
{
        return @"Adium";
}

/*!
 * @brief Registration information for Growl
 *
 * Returns information that Growl needs, like which notifications we will post and our application name.
 */
- (NSDictionary *)registrationDictionaryForGrowl
{
        id <AIContactAlertsController> contactAlertsController = adium.contactAlertsController;
        NSArray                                         *allNotes = [contactAlertsController allEventIDs];
        NSMutableDictionary                     *humanReadableNames = [NSMutableDictionary dictionary];
        NSMutableDictionary                     *descriptions = [NSMutableDictionary dictionary];
        NSString                                        *eventID;
        
        for (eventID in allNotes) {
                [humanReadableNames setObject:[contactAlertsController globalShortDescriptionForEventID:eventID]
                                                           forKey:eventID];
                
                [descriptions setObject:[contactAlertsController longDescriptionForEventID:eventID 
                                                                                                                                         forListObject:nil]
                                                 forKey:eventID];               
        }

        NSDictionary    *growlReg = [NSDictionary dictionaryWithObjectsAndKeys:
                allNotes, GROWL_NOTIFICATIONS_ALL,
                allNotes, GROWL_NOTIFICATIONS_DEFAULT,
                humanReadableNames, GROWL_NOTIFICATIONS_HUMAN_READABLE_NAMES,
                descriptions, GROWL_NOTIFICATIONS_DESCRIPTIONS,
                nil];

        return growlReg;
}

/*!
 * @brief Called when Growl is ready
 *
 * Currently, this is just used for debugging Growl.
 */
- (void)growlIsReady
{
#ifdef GROWL_DEBUG
        AILog(@"Growl is go for launch.");
#endif
}

/*!
 * @brief Called when a Growl notification is clicked
 *
 * When a Growl notificaion is clicked, this method is called, allowing us to take action (e.g. open a new window, make
 * a conversation active, etc).
 *
 * @param clickContext A dictionary that was passed to Growl when we installed the notification.
 */
- (void)growlNotificationWasClicked:(NSDictionary *)clickContext
{
        NSString                *internalObjectID, *uniqueChatID;
        AIListObject    *listObject;
        AIChat                  *chat = nil;
                
        if ((internalObjectID = [clickContext objectForKey:KEY_LIST_OBJECT_ID])) {
                if ((listObject = [adium.contactController existingListObjectWithUniqueID:internalObjectID]) &&
                        ([listObject isKindOfClass:[AIListContact class]])) {
                        
                        //First look for an existing chat to avoid changing anything
                        if (!(chat = [adium.chatController existingChatWithContact:(AIListContact *)listObject])) {
                                //If we don't find one, create one
                                chat = [adium.chatController openChatWithContact:(AIListContact *)listObject
                                                                                                onPreferredAccount:YES];
                        }
                }

        } else if ((uniqueChatID = [clickContext objectForKey:KEY_CHAT_ID])) {
                chat = [adium.chatController existingChatWithUniqueChatID:uniqueChatID];
                
                //If we didn't find a chat, it may have closed since the notification was posted.
                //If we have an appropriate existing list object, we can create a new chat.
                if ((!chat) &&
                        (listObject = [adium.contactController existingListObjectWithUniqueID:uniqueChatID]) &&
                        ([listObject isKindOfClass:[AIListContact class]])) {
                
                        //If the uniqueChatID led us to an existing contact, create a chat with it
                        chat = [adium.chatController openChatWithContact:(AIListContact *)listObject
                                                                                        onPreferredAccount:YES];
                }       
        }

        NSString *fileTransferID;
        if ((fileTransferID = [clickContext objectForKey:KEY_FILE_TRANSFER_ID])) {
                //If a file transfer notification is clicked, reveal the file
                [[ESFileTransfer existingFileTransferWithID:fileTransferID] reveal];
        }

        if (chat) {
                //Make the chat active
                [adium.interfaceController setActiveChat:chat];
        }

        //Make Adium active (needed if, for example, our notification was clicked with another app active)
        [NSApp activateIgnoringOtherApps:YES];  
}

/*!
 * @brief The title of the window shown if Growl needs to be installed
 */
- (NSString *)growlInstallationWindowTitle
{
        return GROWL_INSTALLATION_WINDOW_TITLE; 
}

/*!
 * @brief The title of the window shown if Growl needs to be updated
 */
- (NSString *)growlUpdateWindowTitle
{
        return GROWL_UPDATE_WINDOW_TITLE;
}

/*!
 * @brief The body of the window shown if Growl needs to be installed
 *
 * This method calls _growlInformationForUpdate.
 */
- (NSAttributedString *)growlInstallationInformation
{
        return [self _growlInformationForUpdate:NO];
}

/*!
 * @brief The body of the window shown if Growl needs to be update
 *
 * This method calls _growlInformationForUpdate.
 */
- (NSAttributedString *)growlUpdateInformation
{
        return [self _growlInformationForUpdate:YES];
}

/*!
 * @brief Returns the body text for the window displayed when Growl needs to be installed or updated
 *
 * @param isUpdate YES generates the message for the update window, NO likewise for the install window.
 */
- (NSAttributedString *)_growlInformationForUpdate:(BOOL)isUpdate
{
        NSMutableAttributedString       *growlInfo;
        
        //Start with the window title, centered and bold
        NSMutableParagraphStyle *centeredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
        [centeredStyle setAlignment:NSCenterTextAlignment];
        
        growlInfo = [[NSMutableAttributedString alloc] initWithString:(isUpdate ? GROWL_UPDATE_WINDOW_TITLE : GROWL_INSTALLATION_WINDOW_TITLE)
                                                                                                           attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                                                   centeredStyle,NSParagraphStyleAttributeName,
                                                                                                                   [NSFont boldSystemFontOfSize:GROWL_TEXT_SIZE], NSFontAttributeName,
                                                                                                                   nil]];
        //Skip a line
        [[growlInfo mutableString] appendString:@"\n\n"];
        
        //Now provide a default explanation
        NSAttributedString *defaultExplanation;
        defaultExplanation = [[[NSAttributedString alloc] initWithString:(isUpdate ? GROWL_UPDATE_EXPLANATION : GROWL_INSTALLATION_EXPLANATION)
                                                                                                                  attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                                                          [NSFont systemFontOfSize:GROWL_TEXT_SIZE], NSFontAttributeName,
                                                                                                                          nil]] autorelease];
        
        [growlInfo appendAttributedString:defaultExplanation];
        
        return growlInfo;
}

@end