1 /******************************************************************************
2 * $Id: MessageWindowController.m 11617 2011-01-01 20:42:14Z livings124 $
4 * Copyright (c) 2006-2011 Transmission authors and contributors
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
25 #import "MessageWindowController.h"
26 #import "NSApplicationAdditions.h"
27 #import "NSStringAdditions.h"
28 #import <transmission.h>
35 #define UPDATE_SECONDS 0.75
37 @interface MessageWindowController (Private)
39 - (void) resizeColumn;
40 - (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message;
41 - (NSString *) stringForMessage: (NSDictionary *) message;
45 @implementation MessageWindowController
49 return [super initWithWindowNibName: @"MessageWindow"];
54 [[NSNotificationCenter defaultCenter] removeObserver: self];
60 [fDisplayedMessages release];
62 [fAttributes release];
69 NSWindow * window = [self window];
70 [window setFrameAutosaveName: @"MessageWindowFrame"];
71 [window setFrameUsingName: @"MessageWindowFrame"];
73 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeColumn)
74 name: @"NSTableViewColumnDidResizeNotification" object: fMessageTable];
76 [window setContentBorderThickness: NSMinY([[fMessageTable enclosingScrollView] frame]) forEdge: NSMinYEdge];
78 [[self window] setTitle: NSLocalizedString(@"Message Log", "Message window -> title")];
80 //set images and text for popup button items
81 [[fLevelButton itemAtIndex: LEVEL_ERROR] setTitle: NSLocalizedString(@"Error", "Message window -> level string")];
82 [[fLevelButton itemAtIndex: LEVEL_INFO] setTitle: NSLocalizedString(@"Info", "Message window -> level string")];
83 [[fLevelButton itemAtIndex: LEVEL_DEBUG] setTitle: NSLocalizedString(@"Debug", "Message window -> level string")];
85 const CGFloat levelButtonOldWidth = NSWidth([fLevelButton frame]);
86 [fLevelButton sizeToFit];
88 //set table column text
89 [[[fMessageTable tableColumnWithIdentifier: @"Date"] headerCell] setTitle: NSLocalizedString(@"Date",
90 "Message window -> table column")];
91 [[[fMessageTable tableColumnWithIdentifier: @"Name"] headerCell] setTitle: NSLocalizedString(@"Process",
92 "Message window -> table column")];
93 [[[fMessageTable tableColumnWithIdentifier: @"Message"] headerCell] setTitle: NSLocalizedString(@"Message",
94 "Message window -> table column")];
96 //set and size buttons
97 [fSaveButton setTitle: [NSLocalizedString(@"Save", "Message window -> save button") stringByAppendingEllipsis]];
98 [fSaveButton sizeToFit];
100 NSRect saveButtonFrame = [fSaveButton frame];
101 saveButtonFrame.size.width += 10.0;
102 saveButtonFrame.origin.x += NSWidth([fLevelButton frame]) - levelButtonOldWidth;
103 [fSaveButton setFrame: saveButtonFrame];
105 const CGFloat oldClearButtonWidth = [fClearButton frame].size.width;
107 [fClearButton setTitle: NSLocalizedString(@"Clear", "Message window -> save button")];
108 [fClearButton sizeToFit];
110 NSRect clearButtonFrame = [fClearButton frame];
111 clearButtonFrame.size.width = MAX(clearButtonFrame.size.width + 10.0, saveButtonFrame.size.width);
112 clearButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
113 [fClearButton setFrame: clearButtonFrame];
115 [[fFilterField cell] setPlaceholderString: NSLocalizedString(@"Filter", "Message window -> filter field")];
116 NSRect filterButtonFrame = [fFilterField frame];
117 filterButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
118 [fFilterField setFrame: filterButtonFrame];
120 fAttributes = [[[[[fMessageTable tableColumnWithIdentifier: @"Message"] dataCell] attributedStringValue]
121 attributesAtIndex: 0 effectiveRange: NULL] retain];
123 //select proper level in popup button
124 switch ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"])
127 [fLevelButton selectItemAtIndex: LEVEL_ERROR];
130 [fLevelButton selectItemAtIndex: LEVEL_INFO];
133 [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
136 [[NSUserDefaults standardUserDefaults] setInteger: TR_MSG_ERR forKey: @"MessageLevel"];
137 [fLevelButton selectItemAtIndex: LEVEL_ERROR];
140 fMessages = [[NSMutableArray alloc] init];
141 fDisplayedMessages = [[NSMutableArray alloc] init];
143 fLock = [[NSLock alloc] init];
146 - (void) windowDidBecomeKey: (NSNotification *) notification
150 fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
151 selector: @selector(updateLog:) userInfo: nil repeats: YES];
152 [self updateLog: nil];
156 - (void) windowWillClose: (id)sender
162 - (void) updateLog: (NSTimer *) timer
164 tr_msg_list * messages;
165 if ((messages = tr_getQueuedMessages()) == NULL)
170 static NSUInteger currentIndex = 0;
172 NSScroller * scroller = [[fMessageTable enclosingScrollView] verticalScroller];
173 const BOOL shouldScroll = currentIndex == 0 || [scroller floatValue] == 1.0 || [scroller isHidden]
174 || [scroller knobProportion] == 1.0;
176 const NSInteger maxLevel = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
177 NSString * filterString = [fFilterField stringValue];
181 for (tr_msg_list * currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
183 NSString * name = currentMessage->name != NULL ? [NSString stringWithUTF8String: currentMessage->name]
184 : [[NSProcessInfo processInfo] processName];
186 NSString * file = [[[NSString stringWithUTF8String: currentMessage->file] lastPathComponent] stringByAppendingFormat: @":%d",
187 currentMessage->line];
189 NSDictionary * message = [NSDictionary dictionaryWithObjectsAndKeys:
190 [NSString stringWithUTF8String: currentMessage->message], @"Message",
191 [NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
192 [NSNumber numberWithUnsignedInteger: currentIndex++], @"Index", //more accurate when sorting by date
193 [NSNumber numberWithInteger: currentMessage->level], @"Level",
197 [fMessages addObject: message];
199 if (currentMessage->level <= maxLevel && [self shouldIncludeMessageForFilter: filterString message: message])
201 [fDisplayedMessages addObject: message];
206 if ([fMessages count] > TR_MAX_MSG_LOG)
208 const NSUInteger oldCount = [fDisplayedMessages count];
210 NSIndexSet * removeIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fMessages count]-TR_MAX_MSG_LOG)];
211 NSArray * itemsToRemove = [fMessages objectsAtIndexes: removeIndexes];
213 [fMessages removeObjectsAtIndexes: removeIndexes];
214 [fDisplayedMessages removeObjectsInArray: itemsToRemove];
216 changed |= oldCount > [fDisplayedMessages count];
221 [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
223 [fMessageTable reloadData];
225 [fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
230 tr_freeMessageList(messages);
233 - (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
235 return [fDisplayedMessages count];
238 - (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (NSInteger) row
240 NSString * ident = [column identifier];
241 NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
243 if ([ident isEqualToString: @"Date"])
244 return [message objectForKey: @"Date"];
245 else if ([ident isEqualToString: @"Level"])
247 const NSInteger level = [[message objectForKey: @"Level"] integerValue];
251 return [NSImage imageNamed: @"RedDot.png"];
253 return [NSImage imageNamed: @"YellowDot.png"];
255 return [NSImage imageNamed: @"PurpleDot.png"];
257 NSAssert1(NO, @"Unknown message log level: %d", level);
261 else if ([ident isEqualToString: @"Name"])
262 return [message objectForKey: @"Name"];
264 return [message objectForKey: @"Message"];
267 #warning don't cut off end
268 - (CGFloat) tableView: (NSTableView *) tableView heightOfRow: (NSInteger) row
270 NSString * message = [[fDisplayedMessages objectAtIndex: row] objectForKey: @"Message"];
272 NSTableColumn * column = [tableView tableColumnWithIdentifier: @"Message"];
273 const CGFloat count = floorf([message sizeWithAttributes: fAttributes].width / [column width]);
274 return [tableView rowHeight] * (count + 1.0);
277 - (void) tableView: (NSTableView *) tableView sortDescriptorsDidChange: (NSArray *) oldDescriptors
279 [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
280 [fMessageTable reloadData];
283 - (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
284 tableColumn: (NSTableColumn *) column row: (NSInteger) row mouseLocation: (NSPoint) mouseLocation
286 NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
287 return [message objectForKey: @"File"];
290 - (void) copy: (id) sender
292 NSIndexSet * indexes = [fMessageTable selectedRowIndexes];
293 NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [indexes count]];
295 for (NSDictionary * message in [fDisplayedMessages objectsAtIndexes: indexes])
296 [messageStrings addObject: [self stringForMessage: message]];
298 NSString * messageString = [messageStrings componentsJoinedByString: @"\n"];
300 NSPasteboard * pb = [NSPasteboard generalPasteboard];
301 if ([NSApp isOnSnowLeopardOrBetter])
304 [pb writeObjects: [NSArray arrayWithObject: messageString]];
308 [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
309 [pb setString: messageString forType: NSStringPboardType];
313 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
315 SEL action = [menuItem action];
317 if (action == @selector(copy:))
318 return [fMessageTable numberOfSelectedRows] > 0;
323 - (void) changeLevel: (id) sender
326 switch ([fLevelButton indexOfSelectedItem])
338 NSAssert1(NO, @"Unknown message log level: %d", [fLevelButton indexOfSelectedItem]);
341 if ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"] == level)
346 [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
348 NSString * filterString = [fFilterField stringValue];
350 [fDisplayedMessages removeAllObjects];
351 for (NSDictionary * message in fMessages)
352 if ([[message objectForKey: @"Level"] integerValue] <= level
353 && [self shouldIncludeMessageForFilter: filterString message: message])
354 [fDisplayedMessages addObject: message];
356 [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
358 [fMessageTable reloadData];
360 if ([fDisplayedMessages count] > 0)
362 [fMessageTable deselectAll: self];
363 [fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
369 - (void) changeFilter: (id) sender
373 const NSInteger level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
374 NSString * filterString = [fFilterField stringValue];
376 [fDisplayedMessages removeAllObjects];
377 for (NSDictionary * message in fMessages)
378 if ([[message objectForKey: @"Level"] integerValue] <= level
379 && [self shouldIncludeMessageForFilter: filterString message: message])
380 [fDisplayedMessages addObject: message];
382 [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
384 [fMessageTable reloadData];
386 if ([fDisplayedMessages count] > 0)
388 [fMessageTable deselectAll: self];
389 [fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
395 - (void) clearLog: (id) sender
399 [fMessages removeAllObjects];
400 [fDisplayedMessages removeAllObjects];
401 [fMessageTable reloadData];
406 - (void) writeToFile: (id) sender
408 //make the array sorted by date
409 NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: YES] autorelease];
410 NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
411 NSArray * sortedMessages = [[fDisplayedMessages sortedArrayUsingDescriptors: descriptors] retain];
412 [descriptors release];
414 NSSavePanel * panel = [NSSavePanel savePanel];
415 [panel setRequiredFileType: @"txt"];
416 [panel setCanSelectHiddenExtension: YES];
418 [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
419 modalForWindow: [self window] modalDelegate: self
420 didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: sortedMessages];
423 - (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (NSArray *) messages
425 if (code == NSOKButton)
427 //create the text to output
428 NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [messages count]];
429 for (NSDictionary * message in messages)
430 [messageStrings addObject: [self stringForMessage: message]];
432 NSString * fileString = [messageStrings componentsJoinedByString: @"\n"];
434 if (![fileString writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
436 NSAlert * alert = [[NSAlert alloc] init];
437 [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
438 [alert setMessageText: NSLocalizedString(@"Log Could Not Be Saved", "Save log alert panel -> title")];
439 [alert setInformativeText: [NSString stringWithFormat:
440 NSLocalizedString(@"There was a problem creating the file \"%@\".",
441 "Save log alert panel -> message"), [[panel filename] lastPathComponent]]];
442 [alert setAlertStyle: NSWarningAlertStyle];
454 @implementation MessageWindowController (Private)
456 - (void) resizeColumn
458 [fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange:
459 NSMakeRange(0, [fMessageTable numberOfRows])]];
462 - (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message
464 if ([filterString isEqualToString: @""])
467 const NSStringCompareOptions searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
468 return [[message objectForKey: @"Name"] rangeOfString: filterString options: searchOptions].location != NSNotFound
469 || [[message objectForKey: @"Message"] rangeOfString: filterString options: searchOptions].location != NSNotFound;
472 - (NSString *) stringForMessage: (NSDictionary *) message
474 NSString * levelString;
475 const NSInteger level = [[message objectForKey: @"Level"] integerValue];
479 levelString = NSLocalizedString(@"Error", "Message window -> level");
482 levelString = NSLocalizedString(@"Info", "Message window -> level");
485 levelString = NSLocalizedString(@"Debug", "Message window -> level");
488 NSAssert1(NO, @"Unknown message log level: %d", level);
491 return [NSString stringWithFormat: @"%@ %@ [%@] %@: %@", [message objectForKey: @"Date"],
492 [message objectForKey: @"File"], levelString,
493 [message objectForKey: @"Name"], [message objectForKey: @"Message"], nil];