root/Trunk/AILogSizeSort.m @ 9

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

Switched to case-insensitive display-name comparison in the event of a log size tie (which most frequently happens at 0).

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