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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | /*
* 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 "AIChatLog.h"
#import "AILoginController.h"
#import "AILogViewerWindowController.h"
#import "AILoggerPlugin.h"
#import "AICalendarDate.h"
#import <AIUtilities/NSCalendarDate+ISO8601Parsing.h>
@implementation AIChatLog
static NSCalendarDate *dateFromFileName(NSString *fileName);
- (id)initWithPath:(NSString *)inPath from:(NSString *)inFrom to:(NSString *)inTo serviceClass:(NSString *)inServiceClass
{
if ((self = [super init])) {
relativePath = [inPath retain];
from = [inFrom retain];
to = [inTo retain];
serviceClass = [inServiceClass retain];
rankingPercentage = 0;
}
return self;
}
- (id)initWithPath:(NSString *)inPath
{
NSString *parentPath = [inPath stringByDeletingLastPathComponent];
NSString *toUID = [parentPath lastPathComponent];
NSString *serviceAndFromUID = [[parentPath stringByDeletingLastPathComponent] lastPathComponent];
NSString *myServiceClass, *fromUID;
//Determine the service and fromUID - should be SERVICE.ACCOUNT_NAME
//Check against count to guard in case of old, malformed or otherwise odd folders & whatnot sitting in log base
NSArray *serviceAndFromUIDArray = [serviceAndFromUID componentsSeparatedByString:@"."];
if ([serviceAndFromUIDArray count] >= 2) {
myServiceClass = handleSpecialCasesForUIDAndServiceClass(toUID, [serviceAndFromUIDArray objectAtIndex:0]);
//Use substringFromIndex so we include the rest of the string in the case of a UID with a . in it
fromUID = [serviceAndFromUID substringFromIndex:([serviceClass length] + 1)]; //One off for the '.'
} else {
//Fallback: blank non-nil serviceClass; folderName as the fromUID
myServiceClass = @"";
fromUID = serviceAndFromUID;
}
return [self initWithPath:inPath
from:fromUID
to:toUID
serviceClass:myServiceClass];
}
- (void)dealloc
{
[relativePath release];
[from release];
[to release];
[serviceClass release];
[date release];
[super dealloc];
}
- (NSString *)relativePath{
return relativePath;
}
- (NSString *)from{
return from;
}
- (NSString *)to{
return to;
}
- (NSString *)serviceClass{
return serviceClass;
}
- (NSCalendarDate *)date{
//Determine the date of this log lazily
if (!date) {
date = [dateFromFileName([relativePath lastPathComponent]) retain];
if (!date) {
//Sometimes the filename doesn't have a date (e.g., “jdoe ((null)).chatlog”). In such cases, if it's a chatlog, parse it and get the date from the first element that has one.
//We don't do this first because NSXMLParser uses +[NSData dataWithContentsOfURL:], which is painful for large log files.
if ([[relativePath pathExtension] isEqualToString:@"chatlog"]) {
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[AILoggerPlugin logBasePath] stringByAppendingPathComponent:relativePath]]];
[parser setDelegate:self];
[parser parse];
[parser release];
}
}
}
return date;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
//Stop at the first element with a date.
NSString *dateString = nil;
if ((dateString = [attributeDict objectForKey:@"time"])) {
date = [[NSCalendarDate calendarDateWithString:dateString strictly:YES] retain];
if (date)
[parser abortParsing];
}
}
- (CGFloat)rankingPercentage
{
return rankingPercentage;
}
- (void)setRankingPercentage:(CGFloat)inRankingPercentage
{
rankingPercentage = inRankingPercentage;
}
- (void)setRankingValueOnArbitraryScale:(CGFloat)inRankingValue
{
rankingValue = inRankingValue;
}
- (CGFloat)rankingValueOnArbitraryScale
{
return rankingValue;
}
- (BOOL)isFromSameDayAsDate:(NSCalendarDate *)inDate
{
return [[self date] dayOfCommonEra] == [inDate dayOfCommonEra];
}
#pragma mark Sort Selectors
//Sort by To, then Date
- (NSComparisonResult)compareTo:(AIChatLog *)inLog
{
NSComparisonResult result = [to caseInsensitiveCompare:[inLog to]];
if (result == NSOrderedSame) {
NSTimeInterval interval = [date timeIntervalSinceDate:[inLog date]];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
}
}
return result;
}
- (NSComparisonResult)compareToReverse:(AIChatLog *)inLog
{
NSComparisonResult result = [[inLog to] caseInsensitiveCompare:to];
if (result == NSOrderedSame) {
NSTimeInterval interval = [date timeIntervalSinceDate:[inLog date]];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
}
}
return result;
}
//Sort by From, then Date
- (NSComparisonResult)compareFrom:(AIChatLog *)inLog
{
NSComparisonResult result = [from caseInsensitiveCompare:[inLog from]];
if (result == NSOrderedSame) {
NSTimeInterval interval = [date timeIntervalSinceDate:[inLog date]];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
}
}
return result;
}
- (NSComparisonResult)compareFromReverse:(AIChatLog *)inLog
{
NSComparisonResult result = [[inLog from] caseInsensitiveCompare:from];
if (result == NSOrderedSame) {
NSTimeInterval interval = [date timeIntervalSinceDate:[inLog date]];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
}
}
return result;
}
//Sort by From, then Date
- (NSComparisonResult)compareService:(AIChatLog *)inLog
{
NSComparisonResult result = [serviceClass caseInsensitiveCompare:inLog.serviceClass];
if (result == NSOrderedSame) {
NSTimeInterval interval = [date timeIntervalSinceDate:inLog.date];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
}
}
return result;
}
- (NSComparisonResult)compareServiceReverse:(AIChatLog *)inLog
{
NSComparisonResult result = [inLog.serviceClass caseInsensitiveCompare:serviceClass];
if (result == NSOrderedSame) {
NSTimeInterval interval = [date timeIntervalSinceDate:inLog.date];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
}
}
return result;
}
//Sort by Date, then To
- (NSComparisonResult)compareDate:(AIChatLog *)inLog
{
NSComparisonResult result;
NSTimeInterval interval = [[self date] timeIntervalSinceDate:[inLog date]];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
} else {
result = [to caseInsensitiveCompare:[inLog to]];
}
return result;
}
- (NSComparisonResult)compareDateReverse:(AIChatLog *)inLog
{
NSComparisonResult result;
NSTimeInterval interval = [[inLog date] timeIntervalSinceDate:[self date]];
if (interval < 0) {
result = NSOrderedAscending;
} else if (interval > 0) {
result = NSOrderedDescending;
} else {
result = [[inLog to] caseInsensitiveCompare:to];
}
return result;
}
-(NSComparisonResult)compareRank:(AIChatLog *)inLog
{
NSComparisonResult result;
CGFloat otherRankingPercentage = [inLog rankingPercentage];
if (rankingPercentage > otherRankingPercentage) {
result = NSOrderedDescending;
} else if (rankingPercentage < otherRankingPercentage) {
result = NSOrderedAscending;
} else {
result = [to caseInsensitiveCompare:[inLog to]];
}
return result;
}
-(NSComparisonResult)compareRankReverse:(AIChatLog *)inLog
{
NSComparisonResult result;
CGFloat otherRankingPercentage = [inLog rankingPercentage];
if (rankingPercentage > otherRankingPercentage) {
result = NSOrderedAscending;
} else if (rankingPercentage < otherRankingPercentage) {
result = NSOrderedDescending;
} else {
result = [[inLog to] caseInsensitiveCompare:to];
}
return result;
}
#pragma mark Date utilities
//Scan an Adium date string, supahfast C style
static BOOL scandate(const char *sample,
unsigned long *outyear, unsigned long *outmonth, unsigned long *outdate,
BOOL *outHasTime, unsigned long *outhour, unsigned long *outminute, unsigned long *outsecond,
long *outtimezone)
{
BOOL success = YES;
unsigned long component;
//Read a date, followed by a '('.
//First, find the '('.
while (*sample != '(') {
if (!*sample) {
success = NO;
goto fail;
} else {
++sample;
}
}
//current character is a '(' now, so skip over it.
++sample; //start with the next character
/*get the year*/ {
while (*sample && (*sample < '0' || *sample > '9')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
component = strtoul(sample, (char **)&sample, 10);
if (outyear) *outyear = component;
}
/*get the month*/ {
while (*sample && (*sample < '0' || *sample > '9')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
component = strtoul(sample, (char **)&sample, 10);
if (outmonth) *outmonth = component;
}
/*get the date*/ {
while (*sample && (*sample < '0' || *sample > '9')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
component = strtoul(sample, (char **)&sample, 10);
if (outdate) *outdate = component;
}
if (*sample == 'T') {
++sample; //start with the next character
if (outHasTime) *outHasTime = YES;
/*get the hour*/ {
while (*sample && (*sample < '0' || *sample > '9')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
component = strtoul(sample, (char **)&sample, 10);
if (outhour) *outhour = component;
}
/*get the minute*/ {
while (*sample && (*sample < '0' || *sample > '9')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
component = strtoul(sample, (char **)&sample, 10);
if (outminute) *outminute = component;
}
/*get the second*/ {
while (*sample && (*sample < '0' || *sample > '9')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
component = strtoul(sample, (char **)&sample, 10);
if (outsecond) *outsecond = component;
}
/*get the time zone*/ {
while (*sample && ((*sample < '0' || *sample > '9') && *sample != '-' && *sample != '+')) ++sample;
if (!*sample) {
success = NO;
goto fail;
}
long timezone_sign = 1;
if(*sample == '+') {
++sample;
} else if(*sample == '-') {
timezone_sign = -1;
++sample;
} else if (*sample) {
//There's something here, but it's not a time zone. Bail.
success = NO;
goto fail;
}
long timezone_hr = 0;
if (*sample >= '0' || *sample <= '9') {
timezone_hr += *(sample++) - '0';
}
if (*sample >= '0' || *sample <= '9') {
timezone_hr *= 10;
timezone_hr += *(sample++) - '0';
}
long timezone_min = 0;
if (*sample >= '0' || *sample <= '9') {
timezone_min += *(sample++) - '0';
}
if (*sample >= '0' || *sample <= '9') {
timezone_min *= 10;
timezone_min += *(sample++) - '0';
}
if (outtimezone) *outtimezone = (timezone_hr * 60 + timezone_min) * timezone_sign;
}
}
fail:
return success;
}
//Given an Adium log file name, return an NSCalendarDate with year, month, and day specified
static NSCalendarDate *dateFromFileName(NSString *fileName)
{
unsigned long year = 0;
unsigned long month = 0;
unsigned long day = 0;
unsigned long hour = 0;
unsigned long minute = 0;
unsigned long second = 0;
long timezone = NSNotFound;
BOOL hasTime = NO;
if (scandate([fileName UTF8String], &year, &month, &day, &hasTime, &hour, &minute, &second, &timezone)) {
if (year && month && day) {
AICalendarDate *calendarDate;
calendarDate = [AICalendarDate dateWithYear:year
month:month
day:day
hour:hour
minute:minute
second:second
timeZone:((timezone == NSNotFound) ? nil : [NSTimeZone timeZoneForSecondsFromGMT:(timezone * 60)])];
[calendarDate setGranularity:(hasTime ? AISecondGranularity : AIDayGranularity)];
return calendarDate;
}
}
return nil;
}
@end
|