William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AILoggerPlugin.h"
#import "AILogToGroup.h"
#import "AIChatLog.h"
#import "AILogViewerWindowController.h"
#import <AIUtilities/AIFileManagerAdditions.h>
@interface AILogToGroup ()
- (NSDictionary *)logDict;
@end
@implementation AILogToGroup
//A group of logs to an specific user
- (id)initWithPath:(NSString *)inPath from:(NSString *)inFrom to:(NSString *)inTo serviceClass:(NSString *)inServiceClass
{
if ((self = [super init]))
{
NSParameterAssert(inPath != nil);
relativePath = [inPath copy];
from = [inFrom copy];
to = [inTo copy];
serviceClass = [handleSpecialCasesForUIDAndServiceClass(to, inServiceClass) retain];
logDict = nil;
partialLogDict = nil;
defaultManager = [[NSFileManager defaultManager] retain];
}
return self;
}
//Dealloc
- (void)dealloc
{
[relativePath release];
[to release];
[from release];
[serviceClass release];
[logDict release];
[partialLogDict release];
[defaultManager release];
[super dealloc];
}
- (NSString *)from
{
return from;
}
- (NSString *)to
{
return to;
}
- (NSString *)relativePath
{
return relativePath;
}
- (NSString *)serviceClass
{
return serviceClass;
}
//Returns an enumerator for all of our logs
- (NSEnumerator *)logEnumerator
{
return [[self logDict] objectEnumerator];
}
- (NSInteger)logCount
{
return [[self logDict] count];
}
- (NSDictionary *)logDict
{
@synchronized(self) {
if (!logDict) {
NSString *logBasePath, *fullPath;
//
logDict = [[NSMutableDictionary alloc] init];
//Retrieve any logs we've already loaded
if (partialLogDict) {
[logDict addEntriesFromDictionary:partialLogDict];
[partialLogDict release]; partialLogDict = nil;
}
logBasePath = [AILoggerPlugin logBasePath];
fullPath = [logBasePath stringByAppendingPathComponent:relativePath];
for (NSString *fileName in [defaultManager contentsOfDirectoryAtPath:fullPath error:NULL]) {
if (![fileName hasPrefix:@"."]) {
NSString *relativeLogPath = [relativePath stringByAppendingPathComponent:fileName];
if (![logDict objectForKey:relativeLogPath]) {
AIChatLog *theLog;
theLog = [[AIChatLog alloc] initWithPath:relativeLogPath
from:from
to:to
serviceClass:serviceClass];
if (theLog) {
[logDict setObject:theLog
forKey:relativeLogPath];
} else {
AILog(@"Class %@: Couldn't make for %@ %@ %@ %@",NSStringFromClass([AIChatLog class]),relativeLogPath,from,to,serviceClass);
}
[theLog release];
}
}
}
}
}
return logDict;
}
/*!
* @brief Get an AIChatLog within this AILogToGroup
*
* @param inPath A _relative_ path of the form SERVICE.ACCOUNT_NAME/TO_NAME/LogName.Extension
*
* @result The AIChatLog, from the cache if possible
*/
- (AIChatLog *)logAtPath:(NSString *)inPath
{
AIChatLog *theLog;
@synchronized(self) {
if (logDict) {
//Use the full dictionary if we have it
theLog = [logDict objectForKey:inPath];
} else {
//Otherwise, use the partialLog dictionary, adding to it if necessary
if (!partialLogDict) partialLogDict = [[NSMutableDictionary alloc] init];
if (!(theLog = [partialLogDict objectForKey:inPath])) {
theLog = [[AIChatLog alloc] initWithPath:inPath
from:from
to:to
serviceClass:serviceClass];
[partialLogDict setObject:theLog
forKey:inPath];
[theLog release];
}
if (!theLog) AILog(@"%@ couldn't find %@ in its partialLogDict",self,inPath);
}
}
return theLog;
}
/*!
* @brief Trash an AIChatLog within this AILogToGroup
*
* @param aLog The AIChatLog to move to the trash
*
* @result YES if the AIChatLog was successfully trashed
*/
- (BOOL)trashLog:(AIChatLog *)aLog
{
NSString *logPath = [[AILoggerPlugin logBasePath] stringByAppendingPathComponent:[aLog relativePath]];
BOOL success;
success = [[NSFileManager defaultManager] trashFileAtPath:logPath];
//Remove from our dictionaries so we don't reference the removed log
[logDict removeObjectForKey:[aLog relativePath]];
[partialLogDict removeObjectForKey:[aLog relativePath]];
return success;
}
/*!
* @brief Partial isEqual implementation
*
* 'Partial' in the sense that it doesn't actually test equality. If two AILogToGroup objects are for the same service/contact pair,
* they are considered equal by this function. They may (and probably do) have different source accounts and therefore different
* contained logs.
*
* This is useful because all To groups for a service/contact pair are presented as a single To group in the Contacts source list.
*/
- (BOOL)isEqual:(id)inObject
{
return ([inObject isMemberOfClass:[self class]] &&
([[(AILogToGroup *)inObject to] isEqualToString:[self to]] &&
[[(AILogToGroup *)inObject serviceClass] isEqualToString:[self serviceClass]]));
}
- (NSUInteger)hash
{
return [[self to] hash];
}
@end
|