root/Trunk/AILogSizeSort.m @ 8

Revision 8, 4.2 KB (checked in by jon, 16 years ago)

Removed superfluous menu items and added some documentation.

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 * Returns the total aggregate log size for a contact.  For meta-contacts, the
96 * total log file size of all sub-contacts is returned.  If no log exists or if
97 * something else goes wrong, 0 is returned.
98 *
99 * @param listObject an AIListContact for which to retrieve a total log file size
100 * @return the total log file size in bytes or 0 if an error occurred
101 */
102+(unsigned long long)getContactLogSize:(AIListContact *)listObject
103{
104        NSFileManager *fileManager = [NSFileManager defaultManager];
105       
106        if([listObject isMemberOfClass:[AIMetaContact class]])
107        {
108                // Recurse through all sub-contacts
109               
110                id contact;
111                unsigned long long size = 0;
112               
113                NSEnumerator *contactEnumerator = [[listObject listContacts] objectEnumerator];
114
115                while(contact = [contactEnumerator nextObject])
116                {
117                        size += [AILogSizeSort getContactLogSize:contact];
118                }
119               
120                return size;
121        }
122        else
123        {
124                // Find the path to the directory containing the log files for this contact
125                NSString *path = [[AILoggerPlugin logBasePath] stringByAppendingPathComponent:[AILoggerPlugin relativePathForLogWithObject:[listObject UID] onAccount: [listObject account]]];
126               
127                // Grab an enumerator for all log files for this contact
128                NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
129                NSString *file;
130               
131                unsigned long long size = 0;
132               
133                while(file = [dirEnum nextObject])
134                {
135                        NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:[path stringByAppendingPathComponent:file] traverseLink:YES];
136                       
137                        if (fileAttributes != nil)
138                        {
139                                NSNumber *fileSize;
140                                if(fileSize = [fileAttributes objectForKey:NSFileSize])
141                                {
142                                        size += [fileSize unsignedLongLongValue];
143                                }
144                        }
145                }
146               
147                return size;
148        }
149}
150
151#pragma mark Sorting
152/*!
153 * @brief Alphabetical sort
154 */
155int logSizeSort(id objectA, id objectB, BOOL groups)
156{
157        if(groups)
158        {
159                // Keep groups in manual order (borrowed from ESStatusSort)
160                if ([objectA orderIndex] > [objectB orderIndex])
161                {
162                        return NSOrderedDescending;
163                }
164                else
165                {
166                        return NSOrderedAscending;
167                }
168        }
169       
170       
171        NSNumber *sizeA = [NSNumber numberWithUnsignedLongLong:[AILogSizeSort getContactLogSize:objectA]];
172        NSNumber *sizeB = [NSNumber numberWithUnsignedLongLong:[AILogSizeSort getContactLogSize:objectB]];
173       
174        if([sizeB compare:sizeA] == NSOrderedSame)
175        {
176                return [[objectA displayName] compare:[objectB displayName]];
177        }
178        else
179        {
180                return [sizeB compare:sizeA];
181        }
182}
183
184/*!
185 * @brief Sort function
186 */
187- (sortfunc)sortFunction{
188        return &logSizeSort;
189}
190@end
Note: See TracBrowser for help on using the browser.