Changeset 5528

Show
Ignore:
Timestamp:
05/22/08 15:41:30 (4 months ago)
Author:
morris
Message:

Store news in a list of WCNewsPost so that we can reload the textual representation at midnight and get correct relative dates

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • WiredClient/trunk/WCNews.h

    r4799 r5528  
    2828 
    2929@interface WCNews : WCConnectionController { 
    30         IBOutlet WITextView                             *_newsTextView; 
     30        IBOutlet WITextView                                    *_newsTextView; 
    3131 
    32         IBOutlet NSPanel                                *_postPanel; 
    33         IBOutlet NSTextView                             *_postTextView; 
     32        IBOutlet NSPanel                                       *_postPanel; 
     33        IBOutlet NSTextView                                    *_postTextView; 
    3434 
    35         IBOutlet NSButton                               *_postButton; 
    36         IBOutlet NSButton                               *_clearButton; 
     35        IBOutlet NSButton                                      *_postButton; 
     36        IBOutlet NSButton                                      *_clearButton; 
    3737 
    38         IBOutlet NSButton                               *_reloadButton; 
    39         IBOutlet NSProgressIndicator    *_progressIndicator; 
     38        IBOutlet NSButton                                      *_reloadButton; 
     39        IBOutlet NSProgressIndicator           *_progressIndicator; 
    4040 
    41         NSMutableAttributedString              *_news; 
    42         WITextFilter                                    *_newsFilter; 
    43         WIDateFormatter                                 *_dateFormatter; 
    44         NSUInteger                                              _unread; 
     41        NSMutableArray                                         *_posts; 
     42        WITextFilter                                           *_newsFilter; 
     43        WIDateFormatter                                        *_dateFormatter; 
     44        NSUInteger                                                     _unread; 
    4545         
    46         NSString                                                *_hiddenNews; 
     46        NSString                                                       *_hiddenNews; 
    4747} 
    4848 
    4949 
    50 #define WCNewsDidAddPost                        @"WCNewsDidAddPost" 
    51 #define WCNewsDidReadPost                       @"WCNewsDidReadPost" 
     50#define WCNewsDidAddPost                               @"WCNewsDidAddPost" 
     51#define WCNewsDidReadPost                              @"WCNewsDidReadPost" 
    5252 
    5353 
  • WiredClient/trunk/WCNews.m

    r5249 r5528  
    2828 
    2929#import "WCAccount.h" 
     30#import "WCApplicationController.h" 
    3031#import "WCNews.h" 
     32#import "WCNewsPost.h" 
    3133#import "WCPreferences.h" 
    3234 
     
    4042- (void)_readAllPosts; 
    4143 
    42 - (NSAttributedString *)_postWithNick:(NSString *)nick date:(NSDate *)date message:(NSString *)message; 
     44- (NSAttributedString *)_attributedStringForPost:(WCNewsPost *)post; 
     45- (NSAttributedString *)_attributedStringForPosts; 
    4346 
    4447@end 
     
    5255                                                         connection:connection]; 
    5356 
    54         _news = [[NSMutableAttributedString alloc] init]; 
     57        _posts = [[NSMutableArray alloc] init]; 
    5558        _newsFilter = [[WITextFilter alloc] initWithSelectors:@selector(filterURLs:), @selector(filterWiredSmilies:), 0]; 
    5659 
     
    6265                           name:WCPreferencesDidChange]; 
    6366 
     67        [[NSNotificationCenter defaultCenter] 
     68                addObserver:self 
     69                   selector:@selector(dateDidChange:) 
     70                           name:WCDateDidChange]; 
     71         
    6472        [[self connection] addObserver:self 
    6573                                                  selector:@selector(newsShouldAddNews:) 
     
    118126 
    119127- (void)_reloadNews { 
    120         [[_news mutableString] setString:@""]; 
     128        [_posts removeAllObjects]; 
    121129        [_newsTextView setString:@""]; 
    122130        [_newsTextView setNeedsDisplay:YES]; 
     
    140148#pragma mark - 
    141149 
    142 - (NSAttributedString *)_postWithNick:(NSString *)nick date:(NSDate *)date message:(NSString *)message
    143         NSMutableAttributedString       *post
     150- (NSAttributedString *)_attributedStringForPost:(WCNewsPost *)post
     151        NSMutableAttributedString       *attributedString
    144152        NSAttributedString                      *header, *entry; 
    145153        NSDictionary                            *attributes; 
     
    153161                NULL]; 
    154162        string = [NSSWF:NSLS(@"From %@ (%@):\n", @"News header (nick, time)"), 
    155                 nick
    156                 [_dateFormatter stringFromDate:date]]; 
     163                [post userNick]
     164                [_dateFormatter stringFromDate:[post date]]]; 
    157165        header = [NSAttributedString attributedStringWithString:string attributes:attributes]; 
    158166         
     
    163171                        NSForegroundColorAttributeName, 
    164172                NULL]; 
    165         entry = [NSAttributedString attributedStringWithString:message attributes:attributes]; 
    166          
    167         post = [NSMutableAttributedString attributedString]; 
    168         [post appendAttributedString:header]; 
    169         [post appendAttributedString:entry]; 
    170          
    171         return post; 
     173        entry = [NSAttributedString attributedStringWithString:[post message] attributes:attributes]; 
     174         
     175        attributedString = [NSMutableAttributedString attributedString]; 
     176        [attributedString appendAttributedString:header]; 
     177        [attributedString appendAttributedString:entry]; 
     178         
     179        return [attributedString attributedStringByApplyingFilter:_newsFilter]; 
     180
     181 
     182 
     183 
     184- (NSAttributedString *)_attributedStringForPosts { 
     185        NSMutableAttributedString       *attributedString; 
     186        NSUInteger                                      i, count; 
     187         
     188        attributedString = [NSMutableAttributedString attributedString]; 
     189        count = [_posts count]; 
     190         
     191        for(i = 0; i < count; i++) { 
     192                [attributedString appendAttributedString:[self _attributedStringForPost:[_posts objectAtIndex:i]]]; 
     193                 
     194                if(i != count - 1) 
     195                        [[attributedString mutableString] appendString:@"\n\n"]; 
     196        } 
     197         
     198        return attributedString; 
    172199} 
    173200 
     
    184211 
    185212- (void)dealloc { 
    186         [_news release]; 
     213        [_posts release]; 
    187214        [_newsFilter release]; 
    188215        [_dateFormatter release]; 
     
    301328 
    302329- (void)newsShouldAddNews:(NSNotification *)notification { 
    303         NSArray                                 *fields; 
    304         NSString                                *nick, *date, *message; 
    305         NSAttributedString              *post; 
    306  
    307         fields = [[notification userInfo] objectForKey:WCArgumentsKey]; 
    308          
    309         if([fields count] < 3) 
    310                 return; 
    311          
    312         nick    = [fields safeObjectAtIndex:0]; 
    313         date    = [fields safeObjectAtIndex:1]; 
    314         message = [fields safeObjectAtIndex:2]; 
    315  
    316         post = [self _postWithNick:nick date:[NSDate dateWithISO8601String:date] message:message]; 
    317         [_news appendAttributedString:post]; 
    318         [[_news mutableString] appendString:@"\n\n"]; 
     330        WCNewsPost                      *post; 
     331         
     332        post = [WCNewsPost newsPostWithArguments:[[notification userInfo] objectForKey:WCArgumentsKey]]; 
     333         
     334        [_posts addObject:post]; 
    319335} 
    320336 
     
    322338 
    323339- (void)newsShouldCompleteNews:(NSNotification *)notification { 
    324         NSRange         range; 
    325  
    326         while([[_news mutableString] hasSuffix:@"\n"]) { 
    327                 range = NSMakeRange([[_news mutableString] length] - 1, 1); 
    328                 [[_news mutableString] deleteCharactersInRange:range]; 
    329         } 
    330  
    331         [[_newsTextView textStorage] setAttributedString:[_news attributedStringByApplyingFilter:_newsFilter]]; 
     340        [[_newsTextView textStorage] setAttributedString:[self _attributedStringForPosts]]; 
     341 
    332342        [_progressIndicator stopAnimation:self]; 
    333343} 
     
    336346 
    337347- (void)newsShouldAddNewNews:(NSNotification *)notification { 
    338         NSArray                                 *fields; 
    339         NSString                                *nick, *date, *message; 
    340         NSAttributedString              *post; 
    341  
    342         fields  = [[notification userInfo] objectForKey:WCArgumentsKey]; 
    343         nick    = [fields safeObjectAtIndex:0]; 
    344         date    = [fields safeObjectAtIndex:1]; 
    345         message = [fields safeObjectAtIndex:2]; 
    346  
    347         post = [self _postWithNick:nick date:[NSDate dateWithISO8601String:date] message:message]; 
    348         [[_news mutableString] insertString:@"\n\n" atIndex:0]; 
    349         [_news insertAttributedString:[post attributedStringByApplyingFilter:_newsFilter] atIndex:0]; 
    350         [[_newsTextView textStorage] setAttributedString:_news]; 
     348        NSAttributedString              *string; 
     349        WCNewsPost                              *post; 
     350         
     351        post = [WCNewsPost newsPostWithArguments:[[notification userInfo] objectForKey:WCArgumentsKey]]; 
     352         
     353        if([_posts count] == 0) 
     354                [_posts addObject:post]; 
     355        else 
     356                [_posts insertObject:post atIndex:0]; 
     357         
     358        string = [self _attributedStringForPost:post]; 
     359         
     360        if([_posts count] > 0) 
     361                [[[_newsTextView textStorage] mutableString] insertString:@"\n\n" atIndex:0]; 
     362         
     363        [[_newsTextView textStorage] insertAttributedString:string atIndex:0]; 
    351364         
    352365        if(![[self window] isVisible]) { 
     
    356369        } 
    357370 
    358         [[self connection] triggerEvent:WCEventsNewsPosted info1:nick info2:message]; 
     371        [[self connection] triggerEvent:WCEventsNewsPosted info1:[post userNick] info2:[post message]]; 
    359372} 
    360373 
     
    363376- (void)preferencesDidChange:(NSNotification *)notification { 
    364377        [self _update]; 
     378} 
     379 
     380 
     381 
     382- (void)dateDidChange:(NSNotification *)notification { 
     383        [[_newsTextView textStorage] setAttributedString:[self _attributedStringForPosts]]; 
    365384} 
    366385 
  • WiredClient/trunk/WiredClient.xcodeproj/project.pbxproj

    r5503 r5528  
    8080                77E4287E0DD1DAD900703987 /* libssl.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 77E4287D0DD1DAD700703987 /* libssl.dylib */; }; 
    8181                77E428820DD1DAE500703987 /* libcrypto.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 77E428810DD1DAE500703987 /* libcrypto.dylib */; }; 
     82                77E49CB30DE59F3300703987 /* WCNewsPost.m in Sources */ = {isa = PBXBuildFile; fileRef = 77E49CB20DE59F3300703987 /* WCNewsPost.m */; }; 
    8283                77E555BC075346E4009A7557 /* GreenDrop.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 77E555BB075346E4009A7557 /* GreenDrop.tiff */; }; 
    8384                77E555C5075346F3009A7557 /* YellowDrop.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 77E555C4075346F3009A7557 /* YellowDrop.tiff */; }; 
     
    328329                77E4287D0DD1DAD700703987 /* libssl.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libssl.dylib; path = /usr/lib/libssl.dylib; sourceTree = "<absolute>"; }; 
    329330                77E428810DD1DAE500703987 /* libcrypto.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libcrypto.dylib; path = /usr/lib/libcrypto.dylib; sourceTree = "<absolute>"; }; 
     331                77E49CB10DE59F3300703987 /* WCNewsPost.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WCNewsPost.h; sourceTree = "<group>"; }; 
     332                77E49CB20DE59F3300703987 /* WCNewsPost.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WCNewsPost.m; sourceTree = "<group>"; }; 
    330333                77E555BB075346E4009A7557 /* GreenDrop.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = GreenDrop.tiff; sourceTree = "<group>"; }; 
    331334                77E555C4075346F3009A7557 /* YellowDrop.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = YellowDrop.tiff; sourceTree = "<group>"; }; 
     
    818821                                A5DC7EDA057AAF2900736BBF /* WCNews.m */, 
    819822                                A5DC7ED9057AAF2900736BBF /* WCNews.h */, 
     823                                77E49CB20DE59F3300703987 /* WCNewsPost.m */, 
     824                                77E49CB10DE59F3300703987 /* WCNewsPost.h */, 
    820825                        ); 
    821826                        name = News; 
     
    14771482                                778E8616096D27B500258FE6 /* WCMessagesWindow.m in Sources */, 
    14781483                                A5DC7EEA057AAF2900736BBF /* WCNews.m in Sources */, 
     1484                                77E49CB30DE59F3300703987 /* WCNewsPost.m in Sources */, 
    14791485                                A5DC7ECA057AAEAD00736BBF /* WCPreferences.m in Sources */, 
    14801486                                A5DC7F05057AAF6C00736BBF /* WCPreview.m in Sources */,