root/Trunk/AILogSizeSort.m @ 15

Revision 15, 4.3 KB (checked in by jon, 16 years ago)

Simplified the comparison routine to just use primitive numbers instead of NSNumbers. Changed the description string to use more Adium-y terminology.

Line 
1//
2//  AILogSizeSort.m
3//  SortByLogSizePlugin
4//
5//  Created by Jon Chambers on 9/28/08.
6//  Copyright 2008 Jon Chambers. All rights reserved.
7//
8
9#import "AILogSizeSort.h"
10#import "AILoggerPlugin.h"
11
12#import <Adium/AISharedAdium.h>
13
14#import <AIUtilities/AITigerCompatibility.h>
15#import <AIUtilities/AIStringUtilities.h>
16
17#import <Adium/AIContactControllerProtocol.h>
18#import <Adium/AIListObject.h>
19#import <Adium/AIMetaContact.h>
20
21@implementation AILogSizeSort
22
23/*!
24 * @brief Did become active first time
25 *
26 * Called only once; gives the sort controller an opportunity to set defaults and load preferences lazily.
27 */
28- (void)didBecomeActiveFirstTime
29{
30}
31
32/*!
33 * @brief Non-localized identifier
34 */
35- (NSString *)identifier{
36    return @"Log size";
37}
38
39/*!
40 * @brief Localized display name
41 */
42- (NSString *)displayName{
43    return AILocalizedString(@"Sort Contacts by Log Size",nil);
44}
45
46/*!
47 * @brief Properties which, when changed, should trigger a resort
48 */
49- (NSSet *)statusKeysRequiringResort{
50        return nil;
51}
52
53/*!
54 * @brief Attribute keys which, when changed, should trigger a resort
55 */
56- (NSSet *)attributeKeysRequiringResort{
57        return nil;
58}
59
60#pragma mark Configuration
61/*!
62 * @brief Window title when configuring the sort
63 *
64 * Subclasses should provide a title for configuring the sort only if configuration is possible.
65 * @result Localized title. If nil, the menu item will be disabled.
66 */
67- (NSString *)configureSortWindowTitle{
68        return nil;
69}
70
71/*!
72 * @brief Nib name for configuration
73 */
74- (NSString *)configureNibName{
75        return nil;
76}
77
78/*!
79 * @brief View did load
80 */
81- (void)viewDidLoad{
82}
83
84/*!
85 * @brief Preference changed
86 *
87 * Sort controllers should live update as preferences change.
88 */
89- (IBAction)changePreference:(id)sender
90{
91}
92
93/*!
94 * Allow users to manually sort groups.
95 */
96-(BOOL)canSortManually
97{
98        return YES;
99}
100
101/**
102 * Returns the total aggregate log size for a contact.  For meta-contacts, the
103 * total log file size of all sub-contacts is returned.  If no log exists or if
104 * something else goes wrong, 0 is returned.
105 *
106 * @param listObject an AIListContact for which to retrieve a total log file size
107 * @return the total log file size in bytes or 0 if an error occurred
108 */
109+(unsigned long long)getContactLogSize:(AIListContact *)listObject
110{
111        NSFileManager *fileManager = [NSFileManager defaultManager];
112       
113        if([listObject isMemberOfClass:[AIMetaContact class]])
114        {
115                // Recurse through all sub-contacts
116               
117                id contact;
118                unsigned long long size = 0;
119               
120                NSEnumerator *contactEnumerator = [[listObject listContacts] objectEnumerator];
121
122                while(contact = [contactEnumerator nextObject])
123                {
124                        size += [AILogSizeSort getContactLogSize:contact];
125                }
126               
127                return size;
128        }
129        else
130        {
131                // Find the path to the directory containing the log files for this contact
132                NSString *path = [[AILoggerPlugin logBasePath] stringByAppendingPathComponent:[AILoggerPlugin relativePathForLogWithObject:[listObject UID] onAccount: [listObject account]]];
133               
134                // Grab an enumerator for all log files for this contact
135                NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
136                NSString *file;
137               
138                unsigned long long size = 0;
139               
140                while(file = [dirEnum nextObject])
141                {
142                        NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:[path stringByAppendingPathComponent:file] traverseLink:YES];
143                       
144                        if (fileAttributes != nil)
145                        {
146                                NSNumber *fileSize;
147                                if(fileSize = [fileAttributes objectForKey:NSFileSize])
148                                {
149                                        size += [fileSize unsignedLongLongValue];
150                                }
151                        }
152                }
153               
154                return size;
155        }
156}
157
158#pragma mark Sorting
159/*!
160 * @brief Alphabetical sort
161 */
162int logSizeSort(id objectA, id objectB, BOOL groups)
163{
164        if(groups)
165        {
166                // Keep groups in manual order (borrowed from ESStatusSort)
167                if ([objectA orderIndex] > [objectB orderIndex])
168                {
169                        return NSOrderedDescending;
170                }
171                else
172                {
173                        return NSOrderedAscending;
174                }
175        }
176       
177        unsigned long long sizeA = [AILogSizeSort getContactLogSize:objectA];
178        unsigned long long sizeB = [AILogSizeSort getContactLogSize:objectB];
179       
180        if(sizeB == sizeA)
181        {
182                // Fall back to basic alphabetical sorting in the event of a tie.
183                return [[objectA displayName] caseInsensitiveCompare:[objectB displayName]];
184        }
185        else if(sizeA > sizeB)
186        {
187                // There's a clear winner; run with it.
188                return NSOrderedAscending;
189        }
190        else
191        {
192                return NSOrderedDescending;
193        }
194}
195
196/*!
197 * @brief Sort function
198 */
199- (sortfunc)sortFunction{
200        return &logSizeSort;
201}
202@end
Note: See TracBrowser for help on using the browser.