1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 17:47:37 +00:00
transmission/macosx/MessageWindowController.m

392 lines
15 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* $Id$
*
2008-01-02 16:55:05 +00:00
* Copyright (c) 2006-2008 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"
#import "NSStringAdditions.h"
#import "NSApplicationAdditions.h"
2007-09-16 01:02:06 +00:00
#import <transmission.h>
#define LEVEL_ERROR 0
#define LEVEL_INFO 1
#define LEVEL_DEBUG 2
#define UPDATE_SECONDS 0.6
#define MAX_MESSAGES 4000
@interface MessageWindowController (Private)
- (void) resizeColumn;
2007-09-16 01:02:06 +00:00
- (NSString *) stringForMessage: (NSDictionary *) message;
@end
@implementation MessageWindowController
- (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) dealloc
{
[fTimer invalidate];
[fMessages release];
[fAttributes release];
2007-09-16 01:02:06 +00:00
[super dealloc];
}
- (void) awakeFromNib
{
NSWindow * window = [self window];
[window setFrameAutosaveName: @"MessageWindowFrame"];
[window setFrameUsingName: @"MessageWindowFrame"];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeColumn)
name: @"NSTableViewColumnDidResizeNotification" object: fMessageTable];
if ([NSApp isOnLeopardOrBetter])
[window setContentBorderThickness: [[fMessageTable enclosingScrollView] frame].origin.y forEdge: NSMinYEdge];
//initially sort peer table by date
2007-09-16 01:02:06 +00:00
if ([[fMessageTable sortDescriptors] count] == 0)
[fMessageTable setSortDescriptors: [NSArray arrayWithObject: [[fMessageTable tableColumnWithIdentifier: @"Date"]
sortDescriptorPrototype]]];
2007-10-28 03:29:20 +00:00
fErrorImage = [NSImage imageNamed: @"RedDot.png"];
fInfoImage = [NSImage imageNamed: @"YellowDot.png"];
2007-09-16 01:02:06 +00:00
fDebugImage = [NSImage imageNamed: @"PurpleDot.png"];
2008-03-15 20:18:44 +00:00
[[self window] setTitle: NSLocalizedString(@"Message Log", "Message window -> title")];
//set images and text for popup button items
2007-09-16 01:02:06 +00:00
[[fLevelButton itemAtIndex: LEVEL_ERROR] setImage: fErrorImage];
[[fLevelButton itemAtIndex: LEVEL_ERROR] setTitle: NSLocalizedString(@"Error", "Message window -> level string")];
2007-09-16 01:02:06 +00:00
[[fLevelButton itemAtIndex: LEVEL_INFO] setImage: fInfoImage];
[[fLevelButton itemAtIndex: LEVEL_INFO] setTitle: NSLocalizedString(@"Info", "Message window -> level string")];
2007-09-16 01:02:06 +00:00
[[fLevelButton itemAtIndex: LEVEL_DEBUG] setImage: fDebugImage];
[[fLevelButton itemAtIndex: LEVEL_DEBUG] setTitle: NSLocalizedString(@"Debug", "Message window -> level string")];
//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;
[fSaveButton setFrame: saveButtonFrame];
float oldClearButtonWidth = [fClearButton frame].size.width;
[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);
clearButtonFrame.origin.x -= clearButtonFrame.size.width - oldClearButtonWidth;
[fClearButton setFrame: clearButtonFrame];
2007-09-16 01:02:06 +00:00
//select proper level in popup button
switch (tr_getMessageLevel())
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];
}
2007-12-24 16:18:09 +00:00
fMessages = [[NSMutableArray alloc] init];
fIndex = 0;
}
- (void) windowDidBecomeKey: (NSNotification *) notification
{
if (!fTimer)
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
selector: @selector(updateLog:) userInfo: nil repeats: YES];
2007-12-24 16:18:09 +00:00
[self updateLog: nil];
2007-09-16 01:02:06 +00:00
}
- (void) windowWillClose: (id)sender
{
[fTimer invalidate];
fTimer = nil;
}
2007-09-16 01:02:06 +00:00
- (void) updateLog: (NSTimer *) timer
{
2007-09-20 20:24:33 +00:00
tr_msg_list * messages, * currentMessage;
2007-09-16 01:02:06 +00:00
if ((messages = tr_getQueuedMessages()) == NULL)
return;
for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
{
2008-03-16 15:26:53 +00:00
NSString * name = currentMessage->name != NULL ? [NSString stringWithUTF8String: currentMessage->name]
: [[NSProcessInfo processInfo] processName];
2008-03-16 15:26:53 +00:00
NSDictionary * message = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithUTF8String: currentMessage->message], @"Message",
[NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
[NSNumber numberWithUnsignedInt: fIndex], @"Index", //more accurate when sorting by date
2008-03-16 15:26:53 +00:00
[NSNumber numberWithInt: currentMessage->level], @"Level",
name, @"Name",
[NSString stringWithUTF8String: currentMessage->file], @"File",
[NSNumber numberWithInt: currentMessage->line], @"Line", nil];
[fMessages addObject: message];
fIndex++;
}
2007-09-16 01:02:06 +00:00
tr_freeMessageList(messages);
NSScroller * scroller = [[fMessageTable enclosingScrollView] verticalScroller];
BOOL shouldScroll = [scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0;
2007-09-16 01:02:06 +00:00
int total = [fMessages count];
if (total > MAX_MESSAGES)
{
//remove the oldest
NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: YES] autorelease];
[fMessages sortUsingDescriptors: [NSArray arrayWithObject: descriptor]];
2007-09-16 01:02:06 +00:00
[fMessages removeObjectsInRange: NSMakeRange(0, total-MAX_MESSAGES)];
[fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, MAX_MESSAGES)]];
total = MAX_MESSAGES;
2007-09-16 01:02:06 +00:00
}
[fMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
[fMessageTable reloadData];
if (shouldScroll)
[fMessageTable scrollRowToVisible: total-1];
2007-09-16 01:02:06 +00:00
}
- (int) numberOfRowsInTableView: (NSTableView *) tableView
{
return [fMessages count];
}
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
{
NSString * ident = [column identifier];
NSDictionary * message = [fMessages objectAtIndex: row];
if ([ident isEqualToString: @"Date"])
return [message objectForKey: @"Date"];
else if ([ident isEqualToString: @"Level"])
{
2007-10-01 21:52:31 +00:00
switch ([[message objectForKey: @"Level"] intValue])
{
case TR_MSG_ERR:
return fErrorImage;
case TR_MSG_INF:
return fInfoImage;
case TR_MSG_DBG:
return fDebugImage;
default:
return nil;
}
2007-09-16 01:02:06 +00:00
}
else if ([ident isEqualToString: @"Name"])
return [message objectForKey: @"Name"];
2007-09-16 01:02:06 +00:00
else
return [message objectForKey: @"Message"];
}
#warning don't cut off end
- (float) tableView: (NSTableView *) tableView heightOfRow: (int) row
{
NSTableColumn * column = [tableView tableColumnWithIdentifier: @"Message"];
2007-11-21 20:22:04 +00:00
if (!fAttributes)
fAttributes = [[[[column dataCell] attributedStringValue] attributesAtIndex: 0 effectiveRange: NULL] retain];
2007-11-21 20:22:04 +00:00
int count = [[[fMessages objectAtIndex: row] objectForKey: @"Message"] sizeWithAttributes: fAttributes].width / [column width];
return [tableView rowHeight] * (float)(count+1);
}
2007-09-16 01:02:06 +00:00
- (void) tableView: (NSTableView *) tableView sortDescriptorsDidChange: (NSArray *) oldDescriptors
{
[fMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
[fMessageTable reloadData];
}
- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
tableColumn: (NSTableColumn *) column row: (int) row mouseLocation: (NSPoint) mouseLocation
{
NSDictionary * message = [fMessages objectAtIndex: row];
return [NSString stringWithFormat: @"%@:%@", [[message objectForKey: @"File"] lastPathComponent], [message objectForKey: @"Line"]];
}
2007-09-16 01:02:06 +00:00
- (void) copy: (id) sender
{
NSPasteboard * pb = [NSPasteboard generalPasteboard];
[pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: self];
NSIndexSet * indexes = [fMessageTable selectedRowIndexes];
NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [indexes count]];
2007-10-12 11:33:26 +00:00
NSEnumerator * enumerator = [[fMessages objectsAtIndexes: indexes] objectEnumerator];
NSDictionary * message;
while ((message = [enumerator nextObject]))
[messageStrings addObject: [self stringForMessage: message]];
2007-09-16 01:02:06 +00:00
[pb setString: [messageStrings componentsJoinedByString: @"\n"] forType: NSStringPboardType];
}
- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
{
SEL action = [menuItem action];
if (action == @selector(copy:))
return [fMessageTable numberOfSelectedRows] > 0;
return YES;
}
- (void) changeLevel: (id) sender
{
[self updateLog: nil];
2007-10-01 21:52:31 +00:00
int level;
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;
}
2007-09-16 01:02:06 +00:00
tr_setMessageLevel(level);
[[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
}
- (void) clearLog: (id) sender
{
[fMessages removeAllObjects];
[fMessageTable reloadData];
}
- (void) writeToFile: (id) sender
{
//make the array sorted by date
NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: YES] autorelease];
2007-09-16 01:02:06 +00:00
NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
NSArray * sortedMessages = [fMessages sortedArrayUsingDescriptors: descriptors];
[descriptors release];
//create the text to output
NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [fMessages count]];
NSEnumerator * enumerator = [sortedMessages objectEnumerator];
NSDictionary * message;
while ((message = [enumerator nextObject]))
[messageStrings addObject: [self stringForMessage: message]];
NSString * fileString = [[messageStrings componentsJoinedByString: @"\n"] retain];
NSSavePanel * panel = [NSSavePanel savePanel];
[panel setRequiredFileType: @"txt"];
[panel setCanSelectHiddenExtension: YES];
[panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
modalForWindow: [self window] modalDelegate: self
didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: fileString];
}
- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
{
if (code == NSOKButton)
{
if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
[alert setMessageText: [NSString stringWithFormat: 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 filename] lastPathComponent]]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert runModal];
[alert release];
}
}
[string release];
}
@end
@implementation MessageWindowController (Private)
- (void) resizeColumn
{
[fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0, [fMessageTable numberOfRows])]];
}
2007-09-16 01:02:06 +00:00
- (NSString *) stringForMessage: (NSDictionary *) message
{
2007-10-01 21:52:31 +00:00
NSString * level;
switch ([[message objectForKey: @"Level"] intValue])
{
case TR_MSG_ERR:
level = NSLocalizedString(@"Error", "Message window -> level");
2007-10-01 21:52:31 +00:00
break;
case TR_MSG_INF:
level = NSLocalizedString(@"Info", "Message window -> level");
2007-10-01 21:52:31 +00:00
break;
case TR_MSG_DBG:
level = NSLocalizedString(@"Debug", "Message window -> level");
2007-10-01 21:52:31 +00:00
break;
default:
level = @"";
}
2007-09-16 01:02:06 +00:00
return [NSString stringWithFormat: @"%@ %@:%@ [%@] %@: %@", [message objectForKey: @"Date"],
[[message objectForKey: @"File"] lastPathComponent], [message objectForKey: @"Line"], level,
[message objectForKey: @"Name"], [message objectForKey: @"Message"], nil];
2007-09-16 01:02:06 +00:00
}
@end