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 | /*
* 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 <Adium/AIContentControllerProtocol.h>
#import "DCMessageContextDisplayPlugin.h"
#import "DCMessageContextDisplayPreferences.h"
#import <AIUtilities/AIDictionaryAdditions.h>
#import <Adium/AIChat.h>
#import <Adium/AIContentContext.h>
#import <Adium/AIService.h>
//#import "SMSQLiteLoggerPlugin.h"
//#import "AICoreComponentLoader.h"
//Old school
#import <Adium/AIListContact.h>
#import <AIUtilities/AIAttributedStringAdditions.h>
#import <Adium/AIAccountControllerProtocol.h>
//omg crawsslinkz
#import "AILoggerPlugin.h"
//LMX
#import <LMX/LMXParser.h>
#import <Adium/AIXMLElement.h>
#import <AIUtilities/AIStringAdditions.h>
#import "unistd.h"
#import <AIUtilities/NSCalendarDate+ISO8601Parsing.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIHTMLDecoder.h>
#define RESTORED_CHAT_CONTEXT_LINE_NUMBER 50
/**
* @class DCMessageContextDisplayPlugin
* @brief Component to display in-window message history
*
* The amount of history, and criteria of when to display history, are determined in the Advanced->Message History preferences.
*/
@interface DCMessageContextDisplayPlugin ()
- (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime;
- (void)old_preferencesChangedForGroup:(NSString *)group key:(NSString *)key
object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime;
- (BOOL)contextShouldBeDisplayed:(NSCalendarDate *)inDate;
- (NSArray *)contextForChat:(AIChat *)chat;
@end
@implementation DCMessageContextDisplayPlugin
/**
* @brief Install
*/
- (void)installPlugin
{
isObserving = NO;
//Setup our preferences
[adium.preferenceController registerDefaults:[NSDictionary dictionaryNamed:CONTEXT_DISPLAY_DEFAULTS
forClass:[self class]]
forGroup:PREF_GROUP_CONTEXT_DISPLAY];
//Obtain the default preferences and use them - Adium 1.1 experiment to see if people use these prefs
[self old_preferencesChangedForGroup:PREF_GROUP_CONTEXT_DISPLAY
key:nil
object:nil
preferenceDict:[NSDictionary dictionaryNamed:CONTEXT_DISPLAY_DEFAULTS
forClass:[self class]]
firstTime:YES];
//Observe preference changes for whether or not to display message history
[adium.preferenceController registerPreferenceObserver:self forGroup:PREF_GROUP_CONTEXT_DISPLAY];
}
/**
* @brief Uninstall
*/
- (void)uninstallPlugin
{
[adium.preferenceController unregisterPreferenceObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
{
if (!object) {
shouldDisplay = [[prefDict objectForKey:KEY_DISPLAY_CONTEXT] boolValue];
linesToDisplay = [[prefDict objectForKey:KEY_DISPLAY_LINES] integerValue];
if (shouldDisplay && linesToDisplay > 0 && !isObserving) {
//Observe new message windows only if we aren't already observing them
isObserving = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(addContextDisplayToWindow:)
name:Chat_DidOpen
object:nil];
} else if (isObserving && (!shouldDisplay || linesToDisplay <= 0)) {
//Remove observer
isObserving = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self name:Chat_DidOpen object:nil];
}
}
}
/**
* @brief Preferences for when to display history changed
*
* Only change our preferences in response to global preference notifications; specific objects use this group as well.
*/
- (void)old_preferencesChangedForGroup:(NSString *)group key:(NSString *)key
object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
{
if (!object) {
haveTalkedDays = [[prefDict objectForKey:KEY_HAVE_TALKED_DAYS] integerValue];
haveNotTalkedDays = [[prefDict objectForKey:KEY_HAVE_NOT_TALKED_DAYS] integerValue];
displayMode = [[prefDict objectForKey:KEY_DISPLAY_MODE] integerValue];
haveTalkedUnits = [[prefDict objectForKey:KEY_HAVE_TALKED_UNITS] integerValue];
haveNotTalkedUnits = [[prefDict objectForKey:KEY_HAVE_NOT_TALKED_UNITS] integerValue];
}
}
/**
* @brief Retrieve and display in-window message history
*
* Called in response to the Chat_DidOpen notification
*/
- (void)addContextDisplayToWindow:(NSNotification *)notification
{
AIChat *chat = (AIChat *)[notification object];
NSArray *context = [self contextForChat:chat];
if (context && [context count] > 0 && shouldDisplay) {
//Check if the history fits the date restrictions
//The most recent message is what determines whether we have "chatted in the last X days", "not chatted in the last X days", etc.
NSCalendarDate *mostRecentMessage = [[(AIContentContext *)[context lastObject] date] dateWithCalendarFormat:nil timeZone:nil];
if ([self contextShouldBeDisplayed:mostRecentMessage]) {
AIContentContext *contextMessage;
for(contextMessage in context) {
/* Don't display immediately, so the message view can aggregate multiple message history items.
* As required, we post Content_ChatDidFinishAddingUntrackedContent when finished adding. */
[contextMessage setDisplayContentImmediately:NO];
[adium.contentController displayContentObject:contextMessage
usingContentFilters:YES
immediately:YES];
}
//We finished adding untracked content
[[NSNotificationCenter defaultCenter] postNotificationName:Content_ChatDidFinishAddingUntrackedContent
object:chat];
}
}
}
/**
* @brief Does a specified date match our criteria for display?
*
* The date passed should be the date of the _most recent_ stored message history item
*
* @result YES if the mesage history should be displayed
*/
- (BOOL)contextShouldBeDisplayed:(NSCalendarDate *)inDate
{
BOOL dateIsGood = YES;
NSInteger thresholdDays = 0;
NSInteger thresholdHours = 0;
if (displayMode != MODE_ALWAYS) {
if (displayMode == MODE_HAVE_TALKED) {
if (haveTalkedUnits == UNIT_DAYS)
thresholdDays = haveTalkedDays;
else if (haveTalkedUnits == UNIT_HOURS)
thresholdHours = haveTalkedDays;
} else if (displayMode == MODE_HAVE_NOT_TALKED) {
if ( haveTalkedUnits == UNIT_DAYS )
thresholdDays = haveNotTalkedDays;
else if (haveTalkedUnits == UNIT_HOURS)
thresholdHours = haveNotTalkedDays;
}
// Take the most recent message's date, add our limits to it
// See if the new date is earlier or later than today's date
NSCalendarDate *newDate = [inDate dateByAddingYears:0 months:0 days:thresholdDays hours:thresholdHours minutes:0 seconds:0];
NSComparisonResult comparison = [newDate compare:[NSDate date]];
if (((displayMode == MODE_HAVE_TALKED) && (comparison == NSOrderedAscending)) ||
((displayMode == MODE_HAVE_NOT_TALKED) && (comparison == NSOrderedDescending)) ) {
dateIsGood = NO;
}
}
return dateIsGood;
}
static NSInteger linesLeftToFind = 0;
/*!
* @brief Retrieve the message history for a particular chat
*
* Asks AILoggerPlugin for the path to the right file, and then uses LMX to parse that file backwards.
*/
- (NSArray *)contextForChat:(AIChat *)chat
{
//If there's no log there, there's no message history. Bail out.
NSArray *logPaths = [AILoggerPlugin sortedArrayOfLogFilesForChat:chat];
if(!logPaths) return nil;
AIHTMLDecoder *decoder = [AIHTMLDecoder decoder];
NSString *logObjectUID = chat.name;
if (!logObjectUID) logObjectUID = chat.listObject.UID;
logObjectUID = [logObjectUID safeFilenameString];
NSString *baseLogPath = [[AILoggerPlugin logBasePath] stringByAppendingPathComponent:
[AILoggerPlugin relativePathForLogWithObject:logObjectUID onAccount:chat.account]];
if ([chat boolValueForProperty:@"Restored Chat"] && linesToDisplay < RESTORED_CHAT_CONTEXT_LINE_NUMBER) {
linesLeftToFind = RESTORED_CHAT_CONTEXT_LINE_NUMBER;
} else {
linesLeftToFind = linesToDisplay;
}
//Initialize a place to store found messages
NSMutableArray *outerFoundContentContexts = [NSMutableArray arrayWithCapacity:linesLeftToFind];
//Iterate over the elements of the log path array.
NSEnumerator *pathsEnumerator = [logPaths objectEnumerator];
NSString *logPath = nil;
while (linesLeftToFind > 0 && (logPath = [pathsEnumerator nextObject])) {
//If it's not a .chatlog, ignore it.
if (![logPath hasSuffix:@".chatlog"])
continue;
//Stick the base path on to the beginning
logPath = [baseLogPath stringByAppendingPathComponent:logPath];
//By default, the xmlFilePath is the chat log file/bundle... if we find that the chatlog is a bundle, we'll use the xml file inside.
NSString *xmlFilePath = logPath;
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:logPath isDirectory:&isDir]) {
/* If we have a chatLog bundle, we want to get the text content for the xml file inside */
NSString *baseURL;
if (isDir) {
baseURL = logPath;
xmlFilePath = [logPath stringByAppendingPathComponent:
[[[logPath lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:@"xml"]];
} else {
baseURL = nil;
}
[decoder setBaseURL:baseURL];
}
//Initialize the found messages array and element stack for us-as-delegate
foundMessages = [NSMutableArray arrayWithCapacity:linesLeftToFind];
elementStack = [NSMutableArray array];
//Create the parser and set ourselves as the delegate
LMXParser *parser = [LMXParser parser];
[parser setDelegate:self];
//Set up info needed by elementStarted to create content objects.
NSMutableDictionary *contextInfo = nil;
{
//Get the service name from the path name
NSString *serviceName = [[[[[logPath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] lastPathComponent] componentsSeparatedByString:@"."] objectAtIndex:0U];
AIListObject *account = chat.account;
NSString *accountID = [NSString stringWithFormat:@"%@.%@", account.service.serviceID, account.UID];
contextInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
serviceName, @"Service name",
account, @"Account",
accountID, @"Account ID",
chat, @"Chat",
decoder, @"AIHTMLDecoder",
nil];
[parser setContextInfo:(void *)contextInfo];
}
//Open up the file we need to read from, and seek to the end (this is a *backwards* parser, after all :)
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:xmlFilePath];
[file seekToEndOfFile];
//Set up some more doohickeys and then start the parse loop
NSInteger readSize = 4 * getpagesize(); //Read 4 pages at a time.
NSMutableData *chunk = [NSMutableData dataWithLength:readSize];
NSInteger fd = [file fileDescriptor];
char *buf = [chunk mutableBytes];
off_t offset = [file offsetInFile];
enum LMXParseResult result = LMXParsedIncomplete;
parsingAutoreleasePool = [[NSAutoreleasePool alloc] init];
do {
//Calculate the new offset
offset = (offset <= readSize) ? 0 : offset - readSize;
//Seek to it and read greedily until we hit readSize or run out of file.
NSInteger idx = 0;
for (ssize_t amountRead = 0; idx < readSize; idx += amountRead) {
amountRead = pread(fd, buf + idx, readSize, offset + idx);
if (amountRead <= 0) break;
}
offset -= idx;
//Parse
result = [parser parseChunk:chunk];
//Continue to parse as long as we need more elements, we have data to read, and LMX doesn't think we're done.
} while ([foundMessages count] < linesLeftToFind && offset > 0 && result != LMXParsedCompletely);
//Pop our autorelease pool.
[parsingAutoreleasePool release]; parsingAutoreleasePool = nil;
//Be a good citizen and close the file
[file closeFile];
//Add our locals to the outer array; we're probably looping again.
[outerFoundContentContexts replaceObjectsInRange:NSMakeRange(0, 0) withObjectsFromArray:foundMessages];
linesLeftToFind -= [outerFoundContentContexts count];
}
return outerFoundContentContexts;
}
#pragma mark LMX delegate
- (void)parser:(LMXParser *)parser elementEnded:(NSString *)elementName
{
if ([elementName isEqualToString:@"message"]) {
[elementStack insertObject:[AIXMLElement elementWithName:elementName] atIndex:0U];
}
else if ([elementStack count]) {
AIXMLElement *element = [AIXMLElement elementWithName:elementName];
[(AIXMLElement *)[elementStack objectAtIndex:0U] insertObject:element atIndex:0U];
[elementStack insertObject:element atIndex:0U];
}
}
- (void)parser:(LMXParser *)parser foundCharacters:(NSString *)string
{
if ([elementStack count])
[(AIXMLElement *)[elementStack objectAtIndex:0U] insertObject:string atIndex:0U];
}
- (void)parser:(LMXParser *)parser elementStarted:(NSString *)elementName attributes:(NSDictionary *)attributes
{
if ([elementStack count]) {
AIXMLElement *element = [elementStack objectAtIndex:0U];
if (attributes) {
[element setAttributeNames:[attributes allKeys] values:[attributes allValues]];
}
NSMutableDictionary *contextInfo = [parser contextInfo];
if ([elementName isEqualToString:@"message"]) {
//A message element has started!
//This means that we have all of this message now, and therefore can create a single content object from the AIXMLElement tree and then throw away that tree.
//This saves memory when a message element contains many elements (since each one is represented by an AIXMLElement sub-tree in the AIXMLElement tree, as opposed to a simple NSAttributeRun in the NSAttributedString of the content object).
NSString *serviceName = [contextInfo objectForKey:@"Service name"];
AIListObject *account = [contextInfo objectForKey:@"Account"];
NSString *accountID = [contextInfo objectForKey:@"Account ID"];
AIChat *chat = [contextInfo objectForKey:@"Chat"];
//Set up some doohickers.
NSDictionary *attributes = [element attributes];
NSString *timeString = [attributes objectForKey:@"time"];
//Create the context object
if (timeString) {
NSCalendarDate *time = [NSCalendarDate calendarDateWithString:timeString];
NSString *autoreplyAttribute = [attributes objectForKey:@"auto"];
NSString *sender = [NSString stringWithFormat:@"%@.%@", serviceName, [attributes objectForKey:@"sender"]];
BOOL sentByMe = ([sender isEqualToString:accountID]);
/*don't fade the messages if they're within the last 5 minutes
*since that will be resuming a conversation, not starting a new one.
*Why the class trickery? Less code duplication, clearer what is actually different between the two cases.
*/
Class messageClass = (-[time timeIntervalSinceNow] > 300.0) ? [AIContentContext class] : [AIContentMessage class];
AIListContact *listContact = nil;
if (chat.isGroupChat) {
listContact = [chat.account contactWithUID:[attributes objectForKey:@"sender"]];
} else {
listContact = chat.listObject;
}
AIContentMessage *message = [messageClass messageInChat:chat
withSource:(sentByMe ? account : listContact)
destination:(sentByMe ? (chat.isGroupChat ? nil : chat.listObject) : account)
date:time
message:[[contextInfo objectForKey:@"AIHTMLDecoder"] decodeHTML:[element contentsAsXMLString]]
autoreply:(autoreplyAttribute && [autoreplyAttribute caseInsensitiveCompare:@"true"] == NSOrderedSame)];
//Don't log this object
[message setPostProcessContent:NO];
[message setTrackContent:NO];
//Add it to the array (in front, since we're working backwards, and we want the array in forward order)
[foundMessages insertObject:message atIndex:0];
} else {
NSLog(@"Null message context display time for %@",element);
}
}
[elementStack removeObjectAtIndex:0U];
if ([foundMessages count] == linesLeftToFind) {
if ([elementStack count]) [elementStack removeAllObjects];
[parser abortParsing];
} else {
//We're still looking for more messages in this file.
//Pop the current autorelease pool and start a new one.
//This frees the most recent tree of autoreleased AIXMLElements.
[parsingAutoreleasePool release];
parsingAutoreleasePool = [[NSAutoreleasePool alloc] init];
}
}
}
@end
|