| 208 | | m_enabled = flag; |
| 209 | | } |
| 210 | | |
| 211 | | - (void)appendXmlString:(NSString *)string inbound:(BOOL)isInbound |
| 212 | | { |
| 213 | | if (m_enabled) { |
| 214 | | NSTextStorage *textStorage = [m_xmlTextView textStorage]; |
| 215 | | BOOL wasScrolledToBottom = (NSMaxY([m_xmlTextView visibleRect]) >= |
| 216 | | (NSMaxY([m_xmlTextView bounds]) - 30.0)); |
| 217 | | |
| 218 | | NSFont *font = [NSFont userFixedPitchFontOfSize:10.0]; |
| 219 | | NSString *colorKey = (isInbound ? @"ChatFriendColor" : @"ChatMyColor"); |
| 220 | | NSData *colorData = [[NSUserDefaults standardUserDefaults] dataForKey:colorKey]; |
| 221 | | NSColor *color = [NSUnarchiver unarchiveObjectWithData:colorData]; |
| 222 | | |
| 223 | | if (![string isEqualToString:@""]) { |
| 224 | | // Create the attributed string. |
| 225 | | NSAttributedString *attributedXml = [NSAttributedString attributedStringFromString:string |
| 226 | | font:font |
| 227 | | color:color]; |
| 228 | | // Append the string, with a linebreak or two. |
| 229 | | [textStorage beginEditing]; |
| 230 | | [textStorage appendAttributedString:[NSAttributedString attributedStringFromString:@"\n\n"]]; |
| 231 | | [textStorage appendAttributedString:attributedXml]; |
| 232 | | [textStorage endEditing]; |
| 233 | | } |
| 234 | | else { |
| 235 | | NSLog(@"WARNING: Console encountered empty string."); |
| 236 | | } |
| | 210 | if (m_enabled != flag) { |
| | 211 | m_enabled = flag; |
| | 212 | |
| | 213 | |
| | 214 | NSTextStorage *textStorage = [m_xmlTextView textStorage]; |
| | 215 | BOOL wasScrolledToBottom = (NSMaxY([m_xmlTextView visibleRect]) >= (NSMaxY([m_xmlTextView bounds]) - 30.0)); |
| | 216 | NSFont *font = [NSFont userFixedPitchFontOfSize:10.0]; |
| | 217 | |
| | 218 | [textStorage beginEditing]; |
| | 219 | { |
| | 220 | // Dump the contents of the recent XML stanzas buffer to the console |
| | 221 | if (m_enabled && [m_recentXMLStanzasBuffer count] > 0) { |
| | 222 | NSString *bufferDumpHeaderStr = @"\n\n<!-- ***** Dumping some saved recent XML messages: ***** -->\n"; |
| | 223 | [textStorage appendAttributedString:[NSAttributedString attributedStringFromString:bufferDumpHeaderStr |
| | 224 | font:font |
| | 225 | color:[NSColor darkGrayColor]]]; |
| | 226 | |
| | 227 | NSEnumerator *attribStrEnum = [m_recentXMLStanzasBuffer objectEnumerator]; |
| | 228 | NSAttributedString *attribString = nil; |
| | 229 | while (attribString = [attribStrEnum nextObject]) { |
| | 230 | [textStorage appendAttributedString:attribString]; |
| | 231 | } |
| | 232 | } |
| | 233 | |
| | 234 | NSString *liveDumpHeaderStr = [NSString stringWithFormat:@"\n\n<!-- ***** LIVE dump %@ at %@ ***** -->\n", |
| | 235 | (m_enabled ? @"STARTED" : @"STOPPED"), [NSDate date]]; |
| | 236 | |
| | 237 | [textStorage appendAttributedString:[NSAttributedString attributedStringFromString:liveDumpHeaderStr |
| | 238 | font:font |
| | 239 | color:[NSColor darkGrayColor]]]; |
| | 240 | } |
| | 241 | [textStorage endEditing]; |
| | 247 | |
| | 248 | if (m_enabled) { |
| | 249 | [m_recentXMLStanzasBuffer removeAllObjects]; |
| | 250 | } |
| | 251 | } |
| | 252 | } |
| | 253 | |
| | 254 | - (NSAttributedString *)attributedStringForConsoleFromXMLString:(NSString *)xmlString inbound:(BOOL)isInbound |
| | 255 | { |
| | 256 | NSMutableAttributedString *resultingAttribStr = [[NSMutableAttributedString alloc] init]; |
| | 257 | |
| | 258 | NSFont *font = [NSFont userFixedPitchFontOfSize:10.0]; |
| | 259 | NSString *colorKey = (isInbound ? @"ChatFriendColor" : @"ChatMyColor"); |
| | 260 | NSData *colorData = [[NSUserDefaults standardUserDefaults] dataForKey:colorKey]; |
| | 261 | NSColor *color = [NSUnarchiver unarchiveObjectWithData:colorData]; |
| | 262 | |
| | 263 | if ([xmlString length] > 0) { |
| | 264 | // Create the attributed strings |
| | 265 | NSString *dateTagStr = [NSString stringWithFormat:@"\n\n<!-- %@ at %@ -->\n", |
| | 266 | (isInbound ? @"Received" : @"Sent"), [NSDate date]]; |
| | 267 | NSAttributedString *attributedDateTagStr = [NSAttributedString attributedStringFromString:dateTagStr |
| | 268 | font:font |
| | 269 | color:[NSColor grayColor]]; |
| | 270 | NSAttributedString *attributedXMLStr = [NSAttributedString attributedStringFromString:xmlString |
| | 271 | font:font |
| | 272 | color:color]; |
| | 273 | |
| | 274 | [resultingAttribStr beginEditing]; |
| | 275 | [resultingAttribStr appendAttributedString:attributedDateTagStr]; |
| | 276 | [resultingAttribStr appendAttributedString:attributedXMLStr]; |
| | 277 | [resultingAttribStr endEditing]; |
| | 278 | } |
| | 279 | |
| | 280 | return [resultingAttribStr autorelease]; |
| | 281 | } |
| | 282 | |
| | 283 | #define RECENT_XML_STANZAS_BUFFER_MAX_COUNT 1000 |
| | 284 | |
| | 285 | - (void)appendXMLString:(NSString *)xmlString inbound:(BOOL)isInbound |
| | 286 | { |
| | 287 | NSAttributedString *attributedXMLStr = [self attributedStringForConsoleFromXMLString:xmlString inbound:isInbound]; |
| | 288 | |
| | 289 | if (m_enabled) { |
| | 290 | NSTextStorage *textStorage = [m_xmlTextView textStorage]; |
| | 291 | BOOL wasScrolledToBottom = (NSMaxY([m_xmlTextView visibleRect]) >= (NSMaxY([m_xmlTextView bounds]) - 30.0)); |
| | 292 | |
| | 293 | [textStorage appendAttributedString:attributedXMLStr]; |
| | 294 | |
| | 295 | // Auto-Scroll to the bottom of the content view, but only if we were already at the bottom. |
| | 296 | if (wasScrolledToBottom) { |
| | 297 | [m_xmlTextView scrollRangeToVisible:NSMakeRange([textStorage length], 0)]; |
| | 298 | } |
| | 299 | } |
| | 300 | else { |
| | 301 | // Save it on our buffer for the most recent XML stanzas |
| | 302 | [m_recentXMLStanzasBuffer addObject:attributedXMLStr]; |
| | 303 | |
| | 304 | NSUInteger newCount = [m_recentXMLStanzasBuffer count]; |
| | 305 | |
| | 306 | if (newCount > RECENT_XML_STANZAS_BUFFER_MAX_COUNT) { |
| | 307 | [m_recentXMLStanzasBuffer removeObjectsInRange:NSMakeRange(0, newCount - RECENT_XML_STANZAS_BUFFER_MAX_COUNT)]; |
| | 308 | } |