Show
Ignore:
Timestamp:
18-12-2007 13:49:29 (5 years ago)
Author:
jppavao
Message:

Added a badge to the app dock icon that displays the number of unread offline messages. Also added a dock menu. References #24 and #76.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lilypad/Sources/LPMessageCenter.m

    r95 r166  
    9595 
    9696 
     97#pragma mark - 
     98 
     99 
     100@interface LPMessageCenter (Private) 
     101+ (NSManagedObjectModel *)p_managedObjectModelWithVersionNr:(unsigned int)version; 
     102+ (void)p_migrateOfflineMessagesFromManagedObjectContext:(NSManagedObjectContext *)sourceContext toContext:(NSManagedObjectContext *)targetContext; 
     103+ (void)p_migrateSapoNotificationsFromManagedObjectContext:(NSManagedObjectContext *)sourceContext toContext:(NSManagedObjectContext *)targetContext; 
     104+ (BOOL)p_shouldMigrateFromXMLFilePath:(NSString *)xmlFilePath toSQLiteFilePath:(NSString *)sqliteFilePath; 
     105+ (NSString *)p_migratePersistentStoreWithURL:(NSURL *)storeURL storeType:(NSString *)storeType fromVersion:(int)storeVersionNr; 
     106- (NSPersistentStoreCoordinator *)p_persistentStoreCoordinator; 
     107 
     108- (void)p_updateUnreadOfflineMessagesCountFromManagedObjectsContextEmittingKVONotification:(BOOL)emitNotification; 
     109- (void)p_setUnreadOfflineMessagesCount:(int)count emitKVONotification:(BOOL)emitNotification; 
     110 
     111- (void)p_managedObjectContextObjectsDidChange:(NSNotification *)notif; 
     112@end 
     113 
     114 
     115#pragma mark - 
     116 
     117 
    97118@implementation LPMessageCenter 
    98119 
     
    102123                m_presenceSubscriptionsByJID = [[NSMutableDictionary alloc] init]; 
    103124                m_presenceSubscriptions = [[NSMutableArray alloc] init]; 
     125                 
     126                // Mark the offline messages count as uninitialized 
     127                m_unreadOfflineMessagesCount = -1; 
    104128        } 
    105129        return self; 
     
    108132- (void)dealloc 
    109133{ 
     134        [[NSNotificationCenter defaultCenter] removeObserver:self]; 
     135         
    110136        [m_presenceSubscriptionsByJID release]; 
    111137        [m_presenceSubscriptions release]; 
     
    399425                        [m_managedObjectContext setPersistentStoreCoordinator: coordinator]; 
    400426                } 
     427                 
     428                [[NSNotificationCenter defaultCenter] addObserver:self 
     429                                                                                                 selector:@selector(p_managedObjectContextObjectsDidChange:) 
     430                                                                                                         name:NSManagedObjectContextObjectsDidChangeNotification 
     431                                                                                                   object:m_managedObjectContext]; 
    401432        } 
    402433         
     
    475506 
    476507 
     508- (void)p_updateUnreadOfflineMessagesCountFromManagedObjectsContextEmittingKVONotification:(BOOL)emitNotification 
     509{ 
     510        NSManagedObjectContext  *context = [self managedObjectContext]; 
     511        NSEntityDescription             *msgEntity = [NSEntityDescription entityForName:@"LPOfflineMessage" inManagedObjectContext:context]; 
     512         
     513        NSFetchRequest  *fetchRequest = [[NSFetchRequest alloc] init]; 
     514        NSError                 *error; 
     515         
     516        // No predicate, fetch them all. 
     517        [fetchRequest setEntity:msgEntity]; 
     518        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"unread == YES"]]; 
     519         
     520        NSArray *result = [context executeFetchRequest:fetchRequest error:&error]; 
     521        [fetchRequest release]; 
     522         
     523         
     524        [self p_setUnreadOfflineMessagesCount:[result count] emitKVONotification:emitNotification]; 
     525} 
     526 
     527 
     528- (int)unreadOfflineMessagesCount 
     529{ 
     530        // Is the unread messages count initialized yet? 
     531        if (m_unreadOfflineMessagesCount < 0) 
     532                [self p_updateUnreadOfflineMessagesCountFromManagedObjectsContextEmittingKVONotification:NO]; 
     533         
     534        return m_unreadOfflineMessagesCount; 
     535} 
     536 
     537- (void)p_setUnreadOfflineMessagesCount:(int)count emitKVONotification:(BOOL)emitNotification 
     538{ 
     539        if (count != m_unreadOfflineMessagesCount) { 
     540                if (emitNotification) 
     541                        [self willChangeValueForKey:@"unreadOfflineMessagesCount"]; 
     542                 
     543                m_unreadOfflineMessagesCount = count; 
     544                 
     545                if (emitNotification) 
     546                        [self didChangeValueForKey:@"unreadOfflineMessagesCount"]; 
     547        } 
     548} 
     549 
     550 
    477551- (void)addReceivedOfflineMessageFromJID:(NSString *)fromJID account:(LPAccount *)account nick:(NSString *)nick timestamp:(NSString *)timestamp subject:(NSString *)subject plainTextVariant:(NSString *)plainTextVariant XHTMLVariant:(NSString *)xhtmlVariant URLs:(NSArray *)urls 
    478552{ 
     
    508582 
    509583 
     584#pragma mark - 
     585#pragma mark NSManagedObjectContext Notifications 
     586 
     587- (void)p_managedObjectContextObjectsDidChange:(NSNotification *)notif 
     588{ 
     589        [self p_updateUnreadOfflineMessagesCountFromManagedObjectsContextEmittingKVONotification:YES]; 
     590} 
     591 
     592 
    510593@end