2007-09-16 01:02:06 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2006-2012 Transmission authors and contributors
|
2007-09-16 01:02:06 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "MessageWindowController.h"
|
2012-05-27 22:31:58 +00:00
|
|
|
#import "Controller.h"
|
2009-10-21 13:01:14 +00:00
|
|
|
#import "NSApplicationAdditions.h"
|
2011-07-21 02:36:27 +00:00
|
|
|
#import "NSMutableArrayAdditions.h"
|
2008-03-15 20:17:19 +00:00
|
|
|
#import "NSStringAdditions.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import <transmission.h>
|
2009-12-02 00:27:07 +00:00
|
|
|
#import <utils.h>
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
#define LEVEL_ERROR 0
|
|
|
|
#define LEVEL_INFO 1
|
|
|
|
#define LEVEL_DEBUG 2
|
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
#define UPDATE_SECONDS 0.75
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
@interface MessageWindowController (Private)
|
|
|
|
|
2007-10-28 03:10:11 +00:00
|
|
|
- (void) resizeColumn;
|
2010-02-02 01:13:15 +00:00
|
|
|
- (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message;
|
2011-07-21 02:36:27 +00:00
|
|
|
- (void) updateListForFilter;
|
2007-09-16 01:02:06 +00:00
|
|
|
- (NSString *) stringForMessage: (NSDictionary *) message;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MessageWindowController
|
|
|
|
|
2007-10-07 12:24:26 +00:00
|
|
|
- (id) init
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2007-12-24 16:18:09 +00:00
|
|
|
return [super initWithWindowNibName: @"MessageWindow"];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
NSWindow * window = [self window];
|
|
|
|
[window setFrameAutosaveName: @"MessageWindowFrame"];
|
|
|
|
[window setFrameUsingName: @"MessageWindowFrame"];
|
|
|
|
|
2012-06-18 01:33:27 +00:00
|
|
|
if ([NSApp isOnLionOrBetter])
|
|
|
|
[window setRestorationClass: [self class]];
|
2012-05-27 22:31:58 +00:00
|
|
|
|
2007-10-28 03:10:11 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeColumn)
|
2012-07-24 00:20:04 +00:00
|
|
|
name: NSTableViewColumnDidResizeNotification object: fMessageTable];
|
2007-10-28 03:10:11 +00:00
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
[window setContentBorderThickness: NSMinY([[fMessageTable enclosingScrollView] frame]) forEdge: NSMinYEdge];
|
2008-03-24 22:42:30 +00:00
|
|
|
|
2008-03-15 20:18:44 +00:00
|
|
|
[[self window] setTitle: NSLocalizedString(@"Message Log", "Message window -> title")];
|
|
|
|
|
2008-03-15 20:17:19 +00:00
|
|
|
//set images and text for popup button items
|
|
|
|
[[fLevelButton itemAtIndex: LEVEL_ERROR] setTitle: NSLocalizedString(@"Error", "Message window -> level string")];
|
|
|
|
[[fLevelButton itemAtIndex: LEVEL_INFO] setTitle: NSLocalizedString(@"Info", "Message window -> level string")];
|
|
|
|
[[fLevelButton itemAtIndex: LEVEL_DEBUG] setTitle: NSLocalizedString(@"Debug", "Message window -> level string")];
|
|
|
|
|
2009-12-13 21:56:51 +00:00
|
|
|
const CGFloat levelButtonOldWidth = NSWidth([fLevelButton frame]);
|
|
|
|
[fLevelButton sizeToFit];
|
|
|
|
|
2008-03-15 20:17:19 +00:00
|
|
|
//set table column text
|
|
|
|
[[[fMessageTable tableColumnWithIdentifier: @"Date"] headerCell] setTitle: NSLocalizedString(@"Date",
|
|
|
|
"Message window -> table column")];
|
|
|
|
[[[fMessageTable tableColumnWithIdentifier: @"Name"] headerCell] setTitle: NSLocalizedString(@"Process",
|
|
|
|
"Message window -> table column")];
|
|
|
|
[[[fMessageTable tableColumnWithIdentifier: @"Message"] headerCell] setTitle: NSLocalizedString(@"Message",
|
|
|
|
"Message window -> table column")];
|
|
|
|
|
|
|
|
//set and size buttons
|
|
|
|
[fSaveButton setTitle: [NSLocalizedString(@"Save", "Message window -> save button") stringByAppendingEllipsis]];
|
|
|
|
[fSaveButton sizeToFit];
|
|
|
|
|
|
|
|
NSRect saveButtonFrame = [fSaveButton frame];
|
|
|
|
saveButtonFrame.size.width += 10.0;
|
2009-12-13 21:56:51 +00:00
|
|
|
saveButtonFrame.origin.x += NSWidth([fLevelButton frame]) - levelButtonOldWidth;
|
2008-03-15 20:17:19 +00:00
|
|
|
[fSaveButton setFrame: saveButtonFrame];
|
|
|
|
|
2009-10-19 03:10:29 +00:00
|
|
|
const CGFloat oldClearButtonWidth = [fClearButton frame].size.width;
|
2008-03-15 20:17:19 +00:00
|
|
|
|
|
|
|
[fClearButton setTitle: NSLocalizedString(@"Clear", "Message window -> save button")];
|
|
|
|
[fClearButton sizeToFit];
|
|
|
|
|
|
|
|
NSRect clearButtonFrame = [fClearButton frame];
|
|
|
|
clearButtonFrame.size.width = MAX(clearButtonFrame.size.width + 10.0, saveButtonFrame.size.width);
|
2010-02-02 01:13:15 +00:00
|
|
|
clearButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
|
2008-03-15 20:17:19 +00:00
|
|
|
[fClearButton setFrame: clearButtonFrame];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2010-02-02 01:13:15 +00:00
|
|
|
[[fFilterField cell] setPlaceholderString: NSLocalizedString(@"Filter", "Message window -> filter field")];
|
|
|
|
NSRect filterButtonFrame = [fFilterField frame];
|
|
|
|
filterButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
|
|
|
|
[fFilterField setFrame: filterButtonFrame];
|
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
fAttributes = [[[[[fMessageTable tableColumnWithIdentifier: @"Message"] dataCell] attributedStringValue]
|
|
|
|
attributesAtIndex: 0 effectiveRange: NULL] retain];
|
2009-12-10 05:16:30 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//select proper level in popup button
|
2009-11-28 20:39:31 +00:00
|
|
|
switch ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"])
|
2007-10-11 12:06:46 +00:00
|
|
|
{
|
|
|
|
case TR_MSG_ERR:
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_ERROR];
|
|
|
|
break;
|
|
|
|
case TR_MSG_INF:
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_INFO];
|
|
|
|
break;
|
|
|
|
case TR_MSG_DBG:
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_DEBUG];
|
2009-11-28 20:39:31 +00:00
|
|
|
break;
|
2009-11-29 02:17:11 +00:00
|
|
|
default: //safety
|
|
|
|
[[NSUserDefaults standardUserDefaults] setInteger: TR_MSG_ERR forKey: @"MessageLevel"];
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_ERROR];
|
2007-10-11 12:06:46 +00:00
|
|
|
}
|
2007-12-24 16:18:09 +00:00
|
|
|
|
2007-12-24 16:56:43 +00:00
|
|
|
fMessages = [[NSMutableArray alloc] init];
|
2009-11-28 20:39:31 +00:00
|
|
|
fDisplayedMessages = [[NSMutableArray alloc] init];
|
2009-11-28 21:01:27 +00:00
|
|
|
|
|
|
|
fLock = [[NSLock alloc] init];
|
2007-12-24 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver: self];
|
|
|
|
|
|
|
|
[fTimer invalidate];
|
2012-09-10 02:37:29 +00:00
|
|
|
[fTimer release];
|
2012-03-13 02:52:11 +00:00
|
|
|
[fLock release];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[fMessages release];
|
|
|
|
[fDisplayedMessages release];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[fAttributes release];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
2012-03-13 02:52:11 +00:00
|
|
|
[super dealloc];
|
2011-07-21 02:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-12-24 16:56:43 +00:00
|
|
|
- (void) windowDidBecomeKey: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
if (!fTimer)
|
2009-12-11 04:01:47 +00:00
|
|
|
{
|
2012-09-10 02:37:29 +00:00
|
|
|
fTimer = [[NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self selector: @selector(updateLog:) userInfo: nil repeats: YES] retain];
|
2009-12-11 04:01:47 +00:00
|
|
|
[self updateLog: nil];
|
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2007-12-24 16:56:43 +00:00
|
|
|
- (void) windowWillClose: (id)sender
|
|
|
|
{
|
|
|
|
[fTimer invalidate];
|
2012-09-10 02:37:29 +00:00
|
|
|
[fTimer release];
|
2007-12-24 16:56:43 +00:00
|
|
|
fTimer = nil;
|
|
|
|
}
|
|
|
|
|
2012-05-27 22:31:58 +00:00
|
|
|
+ (void) restoreWindowWithIdentifier: (NSString *) identifier state: (NSCoder *) state completionHandler: (void (^)(NSWindow *, NSError *)) completionHandler
|
|
|
|
{
|
|
|
|
NSAssert1([identifier isEqualToString: @"MessageWindow"], @"Trying to restore unexpected identifier %@", identifier);
|
|
|
|
|
|
|
|
NSWindow * window = [[(Controller *)[NSApp delegate] messageWindowController] window];
|
|
|
|
completionHandler(window, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) window: (NSWindow *) window didDecodeRestorableState: (NSCoder *) coder
|
|
|
|
{
|
|
|
|
[fTimer invalidate];
|
2012-09-10 02:37:29 +00:00
|
|
|
[fTimer release];
|
|
|
|
fTimer = [[NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self selector: @selector(updateLog:) userInfo: nil repeats: YES] retain];
|
2012-05-27 22:31:58 +00:00
|
|
|
[self updateLog: nil];
|
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (void) updateLog: (NSTimer *) timer
|
|
|
|
{
|
2008-10-12 21:38:13 +00:00
|
|
|
tr_msg_list * messages;
|
2007-09-16 01:02:06 +00:00
|
|
|
if ((messages = tr_getQueuedMessages()) == NULL)
|
|
|
|
return;
|
|
|
|
|
2009-11-28 21:01:27 +00:00
|
|
|
[fLock lock];
|
|
|
|
|
2009-12-10 13:06:04 +00:00
|
|
|
static NSUInteger currentIndex = 0;
|
2009-12-10 04:49:57 +00:00
|
|
|
|
2009-10-19 02:44:04 +00:00
|
|
|
NSScroller * scroller = [[fMessageTable enclosingScrollView] verticalScroller];
|
2009-12-10 05:25:12 +00:00
|
|
|
const BOOL shouldScroll = currentIndex == 0 || [scroller floatValue] == 1.0 || [scroller isHidden]
|
2009-10-19 02:44:04 +00:00
|
|
|
|| [scroller knobProportion] == 1.0;
|
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
const NSInteger maxLevel = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
|
2010-02-02 01:13:15 +00:00
|
|
|
NSString * filterString = [fFilterField stringValue];
|
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
BOOL changed = NO;
|
|
|
|
|
2008-10-12 21:38:13 +00:00
|
|
|
for (tr_msg_list * currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
|
2007-12-08 19:59:44 +00:00
|
|
|
{
|
2008-03-16 15:26:53 +00:00
|
|
|
NSString * name = currentMessage->name != NULL ? [NSString stringWithUTF8String: currentMessage->name]
|
|
|
|
: [[NSProcessInfo processInfo] processName];
|
2007-12-08 19:59:44 +00:00
|
|
|
|
2009-11-27 20:47:03 +00:00
|
|
|
NSString * file = [[[NSString stringWithUTF8String: currentMessage->file] lastPathComponent] stringByAppendingFormat: @":%d",
|
2009-11-22 03:40:19 +00:00
|
|
|
currentMessage->line];
|
|
|
|
|
2008-03-16 15:26:53 +00:00
|
|
|
NSDictionary * message = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSString stringWithUTF8String: currentMessage->message], @"Message",
|
|
|
|
[NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
|
2009-12-10 05:25:12 +00:00
|
|
|
[NSNumber numberWithUnsignedInteger: currentIndex++], @"Index", //more accurate when sorting by date
|
2009-10-11 16:25:07 +00:00
|
|
|
[NSNumber numberWithInteger: currentMessage->level], @"Level",
|
2008-03-16 15:26:53 +00:00
|
|
|
name, @"Name",
|
2009-11-22 03:40:19 +00:00
|
|
|
file, @"File", nil];
|
|
|
|
|
2007-12-08 19:59:44 +00:00
|
|
|
[fMessages addObject: message];
|
2009-11-28 20:39:31 +00:00
|
|
|
|
2010-02-02 01:13:15 +00:00
|
|
|
if (currentMessage->level <= maxLevel && [self shouldIncludeMessageForFilter: filterString message: message])
|
2009-11-28 20:39:31 +00:00
|
|
|
{
|
|
|
|
[fDisplayedMessages addObject: message];
|
|
|
|
changed = YES;
|
|
|
|
}
|
2007-12-08 19:59:44 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-12-10 05:25:12 +00:00
|
|
|
if ([fMessages count] > TR_MAX_MSG_LOG)
|
2009-11-28 20:39:31 +00:00
|
|
|
{
|
2010-02-20 14:29:33 +00:00
|
|
|
const NSUInteger oldCount = [fDisplayedMessages count];
|
|
|
|
|
2009-12-10 05:25:12 +00:00
|
|
|
NSIndexSet * removeIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fMessages count]-TR_MAX_MSG_LOG)];
|
2009-12-02 01:35:48 +00:00
|
|
|
NSArray * itemsToRemove = [fMessages objectsAtIndexes: removeIndexes];
|
2009-12-02 00:27:07 +00:00
|
|
|
|
2009-12-02 01:35:48 +00:00
|
|
|
[fMessages removeObjectsAtIndexes: removeIndexes];
|
|
|
|
[fDisplayedMessages removeObjectsInArray: itemsToRemove];
|
2010-02-20 14:29:33 +00:00
|
|
|
|
|
|
|
changed |= oldCount > [fDisplayedMessages count];
|
2009-12-02 01:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
{
|
2009-11-28 20:39:31 +00:00
|
|
|
[fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
|
|
|
|
|
|
|
|
[fMessageTable reloadData];
|
|
|
|
if (shouldScroll)
|
2009-11-29 02:17:11 +00:00
|
|
|
[fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
|
2009-11-28 20:39:31 +00:00
|
|
|
}
|
2009-11-28 21:01:27 +00:00
|
|
|
|
|
|
|
[fLock unlock];
|
2009-11-28 21:44:27 +00:00
|
|
|
|
|
|
|
tr_freeMessageList(messages);
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2008-10-25 21:34:30 +00:00
|
|
|
- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2009-11-28 20:39:31 +00:00
|
|
|
return [fDisplayedMessages count];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2008-10-25 21:34:30 +00:00
|
|
|
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (NSInteger) row
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
NSString * ident = [column identifier];
|
2009-11-28 20:39:31 +00:00
|
|
|
NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
if ([ident isEqualToString: @"Date"])
|
|
|
|
return [message objectForKey: @"Date"];
|
|
|
|
else if ([ident isEqualToString: @"Level"])
|
|
|
|
{
|
2009-10-11 16:25:07 +00:00
|
|
|
const NSInteger level = [[message objectForKey: @"Level"] integerValue];
|
|
|
|
switch (level)
|
2007-10-01 21:52:31 +00:00
|
|
|
{
|
|
|
|
case TR_MSG_ERR:
|
2012-06-10 02:35:58 +00:00
|
|
|
return [NSImage imageNamed: @"RedDot"];
|
2007-10-01 21:52:31 +00:00
|
|
|
case TR_MSG_INF:
|
2012-06-10 02:35:58 +00:00
|
|
|
return [NSImage imageNamed: @"YellowDot"];
|
2007-10-01 21:52:31 +00:00
|
|
|
case TR_MSG_DBG:
|
2012-06-10 02:35:58 +00:00
|
|
|
return [NSImage imageNamed: @"PurpleDot"];
|
2007-10-01 21:52:31 +00:00
|
|
|
default:
|
2012-08-13 00:52:04 +00:00
|
|
|
NSAssert1(NO, @"Unknown message log level: %ld", level);
|
2007-10-01 21:52:31 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2008-03-13 10:49:48 +00:00
|
|
|
else if ([ident isEqualToString: @"Name"])
|
|
|
|
return [message objectForKey: @"Name"];
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
|
|
|
return [message objectForKey: @"Message"];
|
|
|
|
}
|
|
|
|
|
2007-11-18 04:27:55 +00:00
|
|
|
#warning don't cut off end
|
2008-11-02 14:04:57 +00:00
|
|
|
- (CGFloat) tableView: (NSTableView *) tableView heightOfRow: (NSInteger) row
|
2007-10-28 03:10:11 +00:00
|
|
|
{
|
2009-11-29 02:17:11 +00:00
|
|
|
NSString * message = [[fDisplayedMessages objectAtIndex: row] objectForKey: @"Message"];
|
2009-12-10 05:16:30 +00:00
|
|
|
|
|
|
|
NSTableColumn * column = [tableView tableColumnWithIdentifier: @"Message"];
|
2009-11-29 02:17:11 +00:00
|
|
|
const CGFloat count = floorf([message sizeWithAttributes: fAttributes].width / [column width]);
|
2012-09-06 03:21:03 +00:00
|
|
|
|
2012-09-06 03:25:52 +00:00
|
|
|
return [tableView rowHeight] * (count + 1.0);
|
2007-10-28 03:10:11 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (void) tableView: (NSTableView *) tableView sortDescriptorsDidChange: (NSArray *) oldDescriptors
|
|
|
|
{
|
2009-11-28 20:39:31 +00:00
|
|
|
[fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
|
2007-09-16 01:02:06 +00:00
|
|
|
[fMessageTable reloadData];
|
|
|
|
}
|
|
|
|
|
2007-12-08 20:42:34 +00:00
|
|
|
- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
|
2008-10-25 21:34:30 +00:00
|
|
|
tableColumn: (NSTableColumn *) column row: (NSInteger) row mouseLocation: (NSPoint) mouseLocation
|
2007-12-08 20:42:34 +00:00
|
|
|
{
|
2009-11-28 20:39:31 +00:00
|
|
|
NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
|
2009-11-22 03:40:19 +00:00
|
|
|
return [message objectForKey: @"File"];
|
2007-12-08 20:42:34 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (void) copy: (id) sender
|
|
|
|
{
|
|
|
|
NSIndexSet * indexes = [fMessageTable selectedRowIndexes];
|
|
|
|
NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [indexes count]];
|
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
for (NSDictionary * message in [fDisplayedMessages objectsAtIndexes: indexes])
|
2007-10-12 11:33:26 +00:00
|
|
|
[messageStrings addObject: [self stringForMessage: message]];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-10-19 03:14:49 +00:00
|
|
|
NSString * messageString = [messageStrings componentsJoinedByString: @"\n"];
|
|
|
|
|
|
|
|
NSPasteboard * pb = [NSPasteboard generalPasteboard];
|
2011-10-06 00:30:40 +00:00
|
|
|
[pb clearContents];
|
|
|
|
[pb writeObjects: [NSArray arrayWithObject: messageString]];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
|
|
|
|
{
|
|
|
|
SEL action = [menuItem action];
|
|
|
|
|
|
|
|
if (action == @selector(copy:))
|
|
|
|
return [fMessageTable numberOfSelectedRows] > 0;
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) changeLevel: (id) sender
|
|
|
|
{
|
2009-10-11 16:25:07 +00:00
|
|
|
NSInteger level;
|
2007-10-01 21:52:31 +00:00
|
|
|
switch ([fLevelButton indexOfSelectedItem])
|
|
|
|
{
|
|
|
|
case LEVEL_ERROR:
|
|
|
|
level = TR_MSG_ERR;
|
|
|
|
break;
|
|
|
|
case LEVEL_INFO:
|
|
|
|
level = TR_MSG_INF;
|
|
|
|
break;
|
|
|
|
case LEVEL_DEBUG:
|
|
|
|
level = TR_MSG_DBG;
|
|
|
|
break;
|
2009-11-29 02:17:11 +00:00
|
|
|
default:
|
2012-08-13 00:52:04 +00:00
|
|
|
NSAssert1(NO, @"Unknown message log level: %ld", [fLevelButton indexOfSelectedItem]);
|
2007-10-01 21:52:31 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
if ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"] == level)
|
|
|
|
return;
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
|
2009-11-28 20:39:31 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
[fLock lock];
|
2010-02-02 01:13:15 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
[self updateListForFilter];
|
2009-11-28 20:39:31 +00:00
|
|
|
|
2010-02-02 01:13:15 +00:00
|
|
|
[fLock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) changeFilter: (id) sender
|
|
|
|
{
|
|
|
|
[fLock lock];
|
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
[self updateListForFilter];
|
2009-11-28 21:44:27 +00:00
|
|
|
|
2009-11-28 21:01:27 +00:00
|
|
|
[fLock unlock];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) clearLog: (id) sender
|
|
|
|
{
|
2009-12-07 03:53:52 +00:00
|
|
|
[fLock lock];
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
[fMessages removeAllObjects];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
|
|
|
const BOOL onLion = [NSApp isOnLionOrBetter];
|
|
|
|
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable beginUpdates];
|
|
|
|
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedMessages count])] withAnimation: NSTableViewAnimationSlideLeft];
|
2009-11-28 20:39:31 +00:00
|
|
|
[fDisplayedMessages removeAllObjects];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable endUpdates];
|
|
|
|
else
|
|
|
|
[fMessageTable reloadData];
|
2009-12-07 03:53:52 +00:00
|
|
|
|
|
|
|
[fLock unlock];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) writeToFile: (id) sender
|
|
|
|
{
|
|
|
|
NSSavePanel * panel = [NSSavePanel savePanel];
|
2011-10-03 01:38:55 +00:00
|
|
|
[panel setAllowedFileTypes: [NSArray arrayWithObject: @"txt"]];
|
2007-09-16 01:02:06 +00:00
|
|
|
[panel setCanSelectHiddenExtension: YES];
|
|
|
|
|
2011-12-11 22:31:01 +00:00
|
|
|
[panel setNameFieldStringValue: NSLocalizedString(@"untitled", "Save log panel -> default file name")];
|
2009-10-25 21:30:19 +00:00
|
|
|
|
2011-12-11 22:31:01 +00:00
|
|
|
[panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result) {
|
|
|
|
if (result == NSFileHandlingPanelOKButton)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2011-12-11 22:31:01 +00:00
|
|
|
//make the array sorted by date
|
|
|
|
NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey: @"Index" ascending: YES];
|
|
|
|
NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
|
|
|
|
NSArray * sortedMessages = [fDisplayedMessages sortedArrayUsingDescriptors: descriptors];
|
2012-03-13 02:52:11 +00:00
|
|
|
[descriptors release];
|
2011-12-11 22:31:01 +00:00
|
|
|
|
|
|
|
//create the text to output
|
|
|
|
NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [sortedMessages count]];
|
|
|
|
for (NSDictionary * message in sortedMessages)
|
|
|
|
[messageStrings addObject: [self stringForMessage: message]];
|
|
|
|
|
|
|
|
NSString * fileString = [messageStrings componentsJoinedByString: @"\n"];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2011-12-11 22:31:01 +00:00
|
|
|
if (![fileString writeToFile: [[panel URL] path] atomically: YES encoding: NSUTF8StringEncoding error: nil])
|
|
|
|
{
|
|
|
|
NSAlert * alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
|
|
|
|
[alert setMessageText: NSLocalizedString(@"Log Could Not Be Saved", "Save log alert panel -> title")];
|
|
|
|
[alert setInformativeText: [NSString stringWithFormat:
|
|
|
|
NSLocalizedString(@"There was a problem creating the file \"%@\".",
|
|
|
|
"Save log alert panel -> message"), [[[panel URL] path] lastPathComponent]]];
|
|
|
|
[alert setAlertStyle: NSWarningAlertStyle];
|
|
|
|
|
|
|
|
[alert runModal];
|
2012-03-13 02:52:11 +00:00
|
|
|
[alert release];
|
2011-12-11 22:31:01 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2011-12-11 22:31:01 +00:00
|
|
|
}];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MessageWindowController (Private)
|
|
|
|
|
2007-10-28 03:10:11 +00:00
|
|
|
- (void) resizeColumn
|
|
|
|
{
|
|
|
|
[fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange:
|
|
|
|
NSMakeRange(0, [fMessageTable numberOfRows])]];
|
|
|
|
}
|
|
|
|
|
2010-02-02 01:13:15 +00:00
|
|
|
- (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message
|
|
|
|
{
|
|
|
|
if ([filterString isEqualToString: @""])
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
const NSStringCompareOptions searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
|
|
|
|
return [[message objectForKey: @"Name"] rangeOfString: filterString options: searchOptions].location != NSNotFound
|
|
|
|
|| [[message objectForKey: @"Message"] rangeOfString: filterString options: searchOptions].location != NSNotFound;
|
|
|
|
}
|
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
- (void) updateListForFilter
|
|
|
|
{
|
|
|
|
const NSInteger level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
|
|
|
|
NSString * filterString = [fFilterField stringValue];
|
|
|
|
|
2012-01-08 17:05:12 +00:00
|
|
|
NSIndexSet * indexes = [fMessages indexesOfObjectsWithOptions: NSEnumerationConcurrent passingTest: ^BOOL(id message, NSUInteger idx, BOOL * stop) {
|
|
|
|
return [[(NSDictionary *)message objectForKey: @"Level"] integerValue] <= level && [self shouldIncludeMessageForFilter: filterString message: message];
|
|
|
|
}];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
2012-01-08 17:05:12 +00:00
|
|
|
NSArray * tempMessages = [[fMessages objectsAtIndexes: indexes] sortedArrayUsingDescriptors: [fMessageTable sortDescriptors]];
|
2011-07-21 02:36:27 +00:00
|
|
|
|
|
|
|
const BOOL onLion = [NSApp isOnLionOrBetter];
|
|
|
|
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable beginUpdates];
|
|
|
|
|
|
|
|
//figure out which rows were added/moved
|
|
|
|
NSUInteger currentIndex = 0, totalCount = 0;
|
|
|
|
NSMutableArray * itemsToAdd = [NSMutableArray array];
|
|
|
|
NSMutableIndexSet * itemsToAddIndexes = [NSMutableIndexSet indexSet];
|
|
|
|
|
|
|
|
for (NSDictionary * message in tempMessages)
|
|
|
|
{
|
|
|
|
const NSUInteger previousIndex = [fDisplayedMessages indexOfObject: message inRange: NSMakeRange(currentIndex, [fDisplayedMessages count]-currentIndex)];
|
|
|
|
if (previousIndex == NSNotFound)
|
|
|
|
{
|
|
|
|
[itemsToAdd addObject: message];
|
|
|
|
[itemsToAddIndexes addIndex: totalCount];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (previousIndex != currentIndex)
|
|
|
|
{
|
|
|
|
[fDisplayedMessages moveObjectAtIndex: previousIndex toIndex: currentIndex];
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable moveRowAtIndex: previousIndex toIndex: currentIndex];
|
|
|
|
}
|
|
|
|
++currentIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
++totalCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove trailing items - those are the unused
|
|
|
|
if (currentIndex < [fDisplayedMessages count])
|
|
|
|
{
|
|
|
|
const NSRange removeRange = NSMakeRange(currentIndex, [fDisplayedMessages count]-currentIndex);
|
|
|
|
[fDisplayedMessages removeObjectsInRange: removeRange];
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: removeRange] withAnimation: NSTableViewAnimationSlideDown];
|
|
|
|
}
|
|
|
|
|
|
|
|
//add new items
|
|
|
|
[fDisplayedMessages insertObjects: itemsToAdd atIndexes: itemsToAddIndexes];
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable insertRowsAtIndexes: itemsToAddIndexes withAnimation: NSTableViewAnimationSlideUp];
|
|
|
|
|
|
|
|
if (onLion)
|
|
|
|
[fMessageTable endUpdates];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[fMessageTable reloadData];
|
|
|
|
|
|
|
|
if ([fDisplayedMessages count] > 0)
|
|
|
|
[fMessageTable deselectAll: self];
|
|
|
|
}
|
|
|
|
|
2011-07-21 22:12:34 +00:00
|
|
|
NSAssert2([fDisplayedMessages isEqualToArray: tempMessages], @"Inconsistency between message arrays! %@ %@", fDisplayedMessages, tempMessages);
|
2011-07-21 02:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (NSString *) stringForMessage: (NSDictionary *) message
|
|
|
|
{
|
2009-11-29 02:17:11 +00:00
|
|
|
NSString * levelString;
|
|
|
|
const NSInteger level = [[message objectForKey: @"Level"] integerValue];
|
|
|
|
switch (level)
|
2007-10-01 21:52:31 +00:00
|
|
|
{
|
|
|
|
case TR_MSG_ERR:
|
2009-11-29 02:17:11 +00:00
|
|
|
levelString = NSLocalizedString(@"Error", "Message window -> level");
|
2007-10-01 21:52:31 +00:00
|
|
|
break;
|
|
|
|
case TR_MSG_INF:
|
2009-11-29 02:17:11 +00:00
|
|
|
levelString = NSLocalizedString(@"Info", "Message window -> level");
|
2007-10-01 21:52:31 +00:00
|
|
|
break;
|
|
|
|
case TR_MSG_DBG:
|
2009-11-29 02:17:11 +00:00
|
|
|
levelString = NSLocalizedString(@"Debug", "Message window -> level");
|
2007-10-01 21:52:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-08-13 00:52:04 +00:00
|
|
|
NSAssert1(NO, @"Unknown message log level: %ld", level);
|
2007-10-01 21:52:31 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-11-22 03:40:19 +00:00
|
|
|
return [NSString stringWithFormat: @"%@ %@ [%@] %@: %@", [message objectForKey: @"Date"],
|
2009-11-29 02:17:11 +00:00
|
|
|
[message objectForKey: @"File"], levelString,
|
2008-03-16 15:44:10 +00:00
|
|
|
[message objectForKey: @"Name"], [message objectForKey: @"Message"], nil];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|