Changeset 19

Show
Ignore:
Timestamp:
10/05/08 14:37:33 (14 years ago)
Author:
jon
Message:

Added dirty log detection/handling. Did some more rearranging, too.

Location:
Trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • Trunk/AILogSizeSort.h

    r18 r19  
    3030} 
    3131 
     32// Cache operations 
     33-(void)createCacheEntryIfNil:(AIListContact *)listContact; 
     34-(void)removeCacheEntry:(AIListContact *)listContact; 
     35 
     36// Event handlers 
     37-(void)contentObjectAdded:(NSNotification *)notification; 
     38 
     39// Log operations 
    3240-(unsigned long long)getCachedLogSize:(AIListContact *)listContact; 
    3341+(unsigned long long)getContactLogSize:(AIListContact *)listContact; 
  • Trunk/AILogSizeSort.m

    r18 r19  
    2828#import <AIUtilities/AIStringUtilities.h> 
    2929 
     30#import <Adium/AIAdiumProtocol.h> 
     31#import <Adium/AIContentControllerProtocol.h> 
    3032#import <Adium/AIContactControllerProtocol.h> 
    3133#import <Adium/AIListObject.h> 
    3234#import <Adium/AIMetaContact.h> 
    3335 
    34 #import <Adium/ESDebugAILog.h> 
     36#import <Adium/AIChat.h> 
     37#import <Adium/AIContentObject.h> 
     38#import <Adium/AIContentMessage.h> 
    3539 
    3640@implementation AILogSizeSort 
    3741 
     42#pragma mark AISortController obligations 
     43 
    3844/*! 
    3945 * @brief Did become active first time 
     
    4349- (void)didBecomeActiveFirstTime 
    4450{ 
    45         AILog(@"Sort by log size controller became active for first time."); 
    4651        logSizeCache = [[NSMutableDictionary alloc] init]; 
    47         AILog(@"%@", logSizeCache); 
     52         
     53        // Listen for content addition notifications 
     54        [[adium notificationCenter] addObserver:self  
     55                                                                   selector:@selector(contentObjectAdded:)  
     56                                                                           name:Content_ContentObjectAdded  
     57                                                                         object:nil]; 
    4858} 
    4959 
     
    5969 */ 
    6070- (NSString *)displayName{ 
    61     return AILocalizedString(@"Sort Contacts by Log Size",nil); 
     71    return AILocalizedString(@"Sort Contacts by Log Size", nil); 
    6272} 
    6373 
     
    7787 
    7888#pragma mark Configuration 
     89 
    7990/*! 
    8091 * @brief Window title when configuring the sort 
     
    117128} 
    118129 
     130#pragma mark Cache operations 
     131 
     132/*! 
     133 * @brief Creates and populates a new cache entry for a contact if needed 
     134 */ 
     135-(void)createCacheEntryIfNil:(AIListContact *)listContact 
     136{ 
     137        // Check for a match on the account name; create a new sub-dictionary if needed 
     138        if([logSizeCache valueForKey:[[listContact account] explicitFormattedUID]] == nil) 
     139        { 
     140                [logSizeCache setValue:[[NSMutableDictionary alloc] init] forKey:[[listContact account] explicitFormattedUID]]; 
     141        } 
     142         
     143        NSMutableDictionary *accountDictionary = [logSizeCache valueForKey:[[listContact account] explicitFormattedUID]]; 
     144         
     145        // If we don't already have a valid log size cached for this contact, create one 
     146        if([accountDictionary valueForKey:[listContact UID]] == nil) 
     147        { 
     148                [accountDictionary setValue:[NSNumber numberWithUnsignedLongLong:[AILogSizeSort getContactLogSize:listContact]] forKey: [listContact UID]]; 
     149        } 
     150} 
     151 
     152/*! 
     153 * @brief Invalidates a cached log size for a list contact 
     154 */ 
     155-(void)removeCacheEntry:(AIListContact *)listContact 
     156{ 
     157        if([listContact isMemberOfClass:[AIMetaContact class]]) 
     158        { 
     159                // Recurse!  Invalidate each sub-contact's cache entry. 
     160                id contact; 
     161                 
     162                NSEnumerator *contactEnumerator = [[(AIMetaContact *)listContact listContacts] objectEnumerator]; 
     163                 
     164                while(contact = [contactEnumerator nextObject]) 
     165                { 
     166                        [self removeCacheEntry:contact]; 
     167                } 
     168        } 
     169        else 
     170        { 
     171                // Bail out if we don't know about the group this contact is in (there's nothing for us to do 
     172                // anyway). 
     173                if([logSizeCache valueForKey:[[listContact account] explicitFormattedUID]] == nil) { return; } 
     174                 
     175                // Remove the cache entry for the dirty account. 
     176                [(NSMutableDictionary *)[logSizeCache valueForKey:[[listContact account] explicitFormattedUID]] setValue:nil forKey:[listContact UID]]; 
     177        } 
     178} 
     179 
     180#pragma mark Event handlers 
     181 
     182/*! 
     183 * @brief Handles content send/receive events 
     184 * 
     185 * Handles content send/receive events.  For one-on-one chats, the cached log size for a contact is 
     186 * invalidated (forcing a recalculation on the next sorting cycle). 
     187 */ 
     188-(void)contentObjectAdded:(NSNotification *)notification 
     189{ 
     190        AIChat *chat = [notification object]; 
     191         
     192        if(![chat isGroupChat]) 
     193        { 
     194                [self removeCacheEntry:[chat listObject]]; 
     195        } 
     196} 
     197 
     198#pragma mark Log operations 
     199 
     200/*! 
     201 * @brief Returns the cached log size for a list contact 
     202 */ 
    119203-(unsigned long long)getCachedLogSize:(AIListContact *)listContact 
    120204{ 
    121         AILogWithSignature(@"Getting cached log size for %@/%@.", [[listContact account] explicitFormattedUID], [listContact UID]); 
    122          
    123         if([logSizeCache valueForKey:[[listContact account] explicitFormattedUID]] == nil) 
    124         { 
    125                 [logSizeCache setValue:[[NSMutableDictionary alloc] init] forKey:[[listContact account] explicitFormattedUID]]; 
    126         } 
     205        [self createCacheEntryIfNil:listContact]; 
    127206         
    128207        NSMutableDictionary *accountDictionary = [logSizeCache valueForKey:[[listContact account] explicitFormattedUID]]; 
    129          
    130         if([accountDictionary valueForKey:[listContact UID]] == nil) 
    131         { 
    132                 AILogWithSignature(@"\tNo cache hit."); 
    133                 [accountDictionary setValue:[NSNumber numberWithUnsignedLongLong:[AILogSizeSort getContactLogSize:listContact]] forKey: [listContact UID]]; 
    134         } 
    135          
    136         AILogWithSignature(@"\tLog file size: %@", [accountDictionary valueForKey:[listContact UID]]); 
    137208        return [[accountDictionary valueForKey:[listContact UID]] unsignedLongLongValue]; 
    138209} 
     
    197268 
    198269#pragma mark Sorting 
     270 
    199271/*! 
    200272 * @brief Sort by log size 
     
    222294        unsigned long long sizeB = 0; 
    223295         
     296        // Attempt to use cached log sizes if possible, but fall back to dumber methods if something 
     297        // unforeseen happens. 
    224298        if([sortController isMemberOfClass:[AILogSizeSort class]]) 
    225299        {