| 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 | } |
---|
| 33 | |
---|
| 34 | /*! |
---|
| 35 | * @brief Non-localized identifier |
---|
| 36 | */ |
---|
| 37 | - (NSString *)identifier{ |
---|
| 38 | return @"Log size"; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /*! |
---|
| 42 | * @brief Localized display name |
---|
| 43 | */ |
---|
| 44 | - (NSString *)displayName{ |
---|
| 45 | return AILocalizedString(@"Sort Contacts by Log Size",nil); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /*! |
---|
| 49 | * @brief Properties which, when changed, should trigger a resort |
---|
| 50 | */ |
---|
| 51 | - (NSSet *)statusKeysRequiringResort{ |
---|
| 52 | return nil; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /*! |
---|
| 56 | * @brief Attribute keys which, when changed, should trigger a resort |
---|
| 57 | */ |
---|
| 58 | - (NSSet *)attributeKeysRequiringResort{ |
---|
| 59 | return nil; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | #pragma mark Configuration |
---|
| 63 | /*! |
---|
| 64 | * @brief Window title when configuring the sort |
---|
| 65 | * |
---|
| 66 | * Subclasses should provide a title for configuring the sort only if configuration is possible. |
---|
| 67 | * @result Localized title. If nil, the menu item will be disabled. |
---|
| 68 | */ |
---|
| 69 | - (NSString *)configureSortWindowTitle{ |
---|
| 70 | return AILocalizedString(@"Configure Sort by Log Size",nil); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /*! |
---|
| 74 | * @brief Nib name for configuration |
---|
| 75 | */ |
---|
| 76 | - (NSString *)configureNibName{ |
---|
| 77 | return @"LogSizeSortConfiguration"; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /*! |
---|
| 81 | * @brief View did load |
---|
| 82 | */ |
---|
| 83 | - (void)viewDidLoad{ |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /*! |
---|
| 87 | * @brief Preference changed |
---|
| 88 | * |
---|
| 89 | * Sort controllers should live update as preferences change. |
---|
| 90 | */ |
---|
| 91 | - (IBAction)changePreference:(id)sender |
---|
| 92 | { |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | #pragma mark Sorting |
---|
| 96 | /*! |
---|
| 97 | * @brief Alphabetical sort |
---|
| 98 | */ |
---|
| 99 | int logSizeSort(id objectA, id objectB, BOOL groups) |
---|
| 100 | { |
---|
| 101 | if(groups) |
---|
| 102 | { |
---|
| 103 | // Keep groups in manual order (borrowed from ESStatusSort) |
---|
| 104 | if ([objectA orderIndex] > [objectB orderIndex]) |
---|
| 105 | { |
---|
| 106 | return NSOrderedDescending; |
---|
| 107 | } |
---|
| 108 | else |
---|
| 109 | { |
---|
| 110 | return NSOrderedAscending; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | NSNumber *sizeA = [NSNumber numberWithUnsignedLongLong:[AILogSizeSort getContactLogSize:objectA]]; |
---|
| 116 | NSNumber *sizeB = [NSNumber numberWithUnsignedLongLong:[AILogSizeSort getContactLogSize:objectB]]; |
---|
| 117 | |
---|
| 118 | if([sizeB compare:sizeA] == NSOrderedSame) |
---|
| 119 | { |
---|
| 120 | return [[objectA displayName] compare:[objectB displayName]]; |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | return [sizeB compare:sizeA]; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /*! |
---|
| 129 | * @brief Sort function |
---|
| 130 | */ |
---|
| 131 | - (sortfunc)sortFunction{ |
---|
| 132 | return &logSizeSort; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | +(unsigned long long)getContactLogSize:(AIListContact *)listObject |
---|
| 136 | { |
---|
| 137 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
---|
| 138 | |
---|
| 139 | if([listObject isMemberOfClass:[AIMetaContact class]]) |
---|
| 140 | { |
---|
| 141 | unsigned long long size = 0; |
---|
| 142 | |
---|
| 143 | NSEnumerator *contactEnumerator = [[listObject listContacts] objectEnumerator]; |
---|
| 144 | |
---|
| 145 | id contact; |
---|
| 146 | |
---|
| 147 | while(contact = [contactEnumerator nextObject]) |
---|
| 148 | { |
---|
| 149 | size += [AILogSizeSort getContactLogSize:contact]; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | return size; |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
| 156 | NSString *path = [[AILoggerPlugin logBasePath] stringByAppendingPathComponent:[AILoggerPlugin relativePathForLogWithObject:[listObject UID] onAccount: [listObject account]]]; |
---|
| 157 | |
---|
| 158 | NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path]; |
---|
| 159 | NSString *file; |
---|
| 160 | |
---|
| 161 | unsigned long long size = 0; |
---|
| 162 | |
---|
| 163 | while(file = [dirEnum nextObject]) |
---|
| 164 | { |
---|
| 165 | NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:[path stringByAppendingPathComponent:file] traverseLink:YES]; |
---|
| 166 | |
---|
| 167 | if (fileAttributes != nil) |
---|
| 168 | { |
---|
| 169 | NSNumber *fileSize; |
---|
| 170 | if(fileSize = [fileAttributes objectForKey:NSFileSize]) |
---|
| 171 | { |
---|
| 172 | size += [fileSize unsignedLongLongValue]; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | return size; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | @end |
---|