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/AIPreferenceControllerProtocol.h> |
---|
20 | #import <Adium/AIListObject.h> |
---|
21 | #import <Adium/AIMetaContact.h> |
---|
22 | |
---|
23 | @implementation AILogSizeSort |
---|
24 | |
---|
25 | /*! |
---|
26 | * @brief Did become active first time |
---|
27 | * |
---|
28 | * Called only once; gives the sort controller an opportunity to set defaults and load preferences lazily. |
---|
29 | */ |
---|
30 | - (void)didBecomeActiveFirstTime |
---|
31 | { |
---|
32 | NSEnumerator *groupEnumerator = [[[[adium contactController] contactList] listContacts] objectEnumerator]; |
---|
33 | |
---|
34 | id group, contact; |
---|
35 | |
---|
36 | while(group = [groupEnumerator nextObject]) |
---|
37 | { |
---|
38 | AILog(@"%@", group); |
---|
39 | |
---|
40 | NSEnumerator *contactEnumerator = [[group listContacts] objectEnumerator]; |
---|
41 | |
---|
42 | while(contact = [contactEnumerator nextObject]) |
---|
43 | { |
---|
44 | AILog(@"\t%@: %lld", contact, [AILogSizeSort getContactLogSize:contact]); |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | /*! |
---|
50 | * @brief Non-localized identifier |
---|
51 | */ |
---|
52 | - (NSString *)identifier{ |
---|
53 | return @"Log size"; |
---|
54 | } |
---|
55 | |
---|
56 | /*! |
---|
57 | * @brief Localized display name |
---|
58 | */ |
---|
59 | - (NSString *)displayName{ |
---|
60 | return AILocalizedString(@"Sort Contacts by Log Size",nil); |
---|
61 | } |
---|
62 | |
---|
63 | /*! |
---|
64 | * @brief Properties which, when changed, should trigger a resort |
---|
65 | */ |
---|
66 | - (NSSet *)statusKeysRequiringResort{ |
---|
67 | return nil; |
---|
68 | } |
---|
69 | |
---|
70 | /*! |
---|
71 | * @brief Attribute keys which, when changed, should trigger a resort |
---|
72 | */ |
---|
73 | - (NSSet *)attributeKeysRequiringResort{ |
---|
74 | return nil; |
---|
75 | } |
---|
76 | |
---|
77 | #pragma mark Configuration |
---|
78 | /*! |
---|
79 | * @brief Window title when configuring the sort |
---|
80 | * |
---|
81 | * Subclasses should provide a title for configuring the sort only if configuration is possible. |
---|
82 | * @result Localized title. If nil, the menu item will be disabled. |
---|
83 | */ |
---|
84 | - (NSString *)configureSortWindowTitle{ |
---|
85 | return AILocalizedString(@"Configure Sort by Log Size",nil); |
---|
86 | } |
---|
87 | |
---|
88 | /*! |
---|
89 | * @brief Nib name for configuration |
---|
90 | */ |
---|
91 | - (NSString *)configureNibName{ |
---|
92 | return @"LogSizeSortConfiguration"; |
---|
93 | } |
---|
94 | |
---|
95 | /*! |
---|
96 | * @brief View did load |
---|
97 | */ |
---|
98 | - (void)viewDidLoad{ |
---|
99 | } |
---|
100 | |
---|
101 | /*! |
---|
102 | * @brief Preference changed |
---|
103 | * |
---|
104 | * Sort controllers should live update as preferences change. |
---|
105 | */ |
---|
106 | - (IBAction)changePreference:(id)sender |
---|
107 | { |
---|
108 | } |
---|
109 | |
---|
110 | #pragma mark Sorting |
---|
111 | /*! |
---|
112 | * @brief Alphabetical sort |
---|
113 | */ |
---|
114 | int logSizeSort(id objectA, id objectB, BOOL groups) |
---|
115 | { |
---|
116 | // Not real excited about doing this with an implicit definition, but seems to be |
---|
117 | // the only option for now. |
---|
118 | //AILog([objectA formattedUID]); |
---|
119 | return NSOrderedAscending; |
---|
120 | } |
---|
121 | |
---|
122 | /*! |
---|
123 | * @brief Sort function |
---|
124 | */ |
---|
125 | - (sortfunc)sortFunction{ |
---|
126 | return &logSizeSort; |
---|
127 | } |
---|
128 | |
---|
129 | +(unsigned long long)getContactLogSize:(AIListContact *)listObject |
---|
130 | { |
---|
131 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
---|
132 | |
---|
133 | if([listObject isMemberOfClass:[AIMetaContact class]]) |
---|
134 | { |
---|
135 | unsigned long long size = 0; |
---|
136 | |
---|
137 | NSEnumerator *contactEnumerator = [[listObject listContacts] objectEnumerator]; |
---|
138 | |
---|
139 | id contact; |
---|
140 | |
---|
141 | while(contact = [contactEnumerator nextObject]) |
---|
142 | { |
---|
143 | size += [AILogSizeSort getContactLogSize:contact]; |
---|
144 | } |
---|
145 | |
---|
146 | return size; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | NSString *path = [[AILoggerPlugin logBasePath] stringByAppendingPathComponent:[AILoggerPlugin relativePathForLogWithObject:[listObject UID] onAccount: [listObject account]]]; |
---|
151 | |
---|
152 | NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path]; |
---|
153 | NSString *file; |
---|
154 | |
---|
155 | unsigned long long size = 0; |
---|
156 | |
---|
157 | while(file = [dirEnum nextObject]) |
---|
158 | { |
---|
159 | NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:[path stringByAppendingPathComponent:file] traverseLink:YES]; |
---|
160 | |
---|
161 | if (fileAttributes != nil) |
---|
162 | { |
---|
163 | NSNumber *fileSize; |
---|
164 | if(fileSize = [fileAttributes objectForKey:NSFileSize]) |
---|
165 | { |
---|
166 | size += [fileSize unsignedLongLongValue]; |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | return size; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | @end |
---|