source: trunk/macosx/MessageWindowController.m @ 9617

Last change on this file since 9617 was 9617, checked in by livings124, 13 years ago

#2624 Message Log's level filter should act as an actual filter, and all messages should be stored

  • Property svn:keywords set to Date Rev Author Id
File size: 15.1 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 9617 2009-11-28 20:39:31Z livings124 $
3 *
4 * Copyright (c) 2006-2009 Transmission authors and contributors
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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 *****************************************************************************/
24
25#import "MessageWindowController.h"
26#import "NSApplicationAdditions.h"
27#import "NSStringAdditions.h"
28#import <transmission.h>
29
30#define LEVEL_ERROR 0
31#define LEVEL_INFO  1
32#define LEVEL_DEBUG 2
33
34#define UPDATE_SECONDS  0.75
35
36@interface MessageWindowController (Private)
37
38- (void) resizeColumn;
39- (NSString *) stringForMessage: (NSDictionary *) message;
40
41@end
42
43@implementation MessageWindowController
44
45- (id) init
46{
47    return [super initWithWindowNibName: @"MessageWindow"];
48}
49
50- (void) dealloc
51{
52    [fTimer invalidate];
53    [fMessages release];
54    [fDisplayedMessages release];
55   
56    [fAttributes release];
57   
58    [super dealloc];
59}
60
61- (void) awakeFromNib
62{
63    NSWindow * window = [self window];
64    [window setFrameAutosaveName: @"MessageWindowFrame"];
65    [window setFrameUsingName: @"MessageWindowFrame"];
66   
67    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeColumn)
68        name: @"NSTableViewColumnDidResizeNotification" object: fMessageTable];
69   
70    [window setContentBorderThickness: NSMinY([[fMessageTable enclosingScrollView] frame]) forEdge: NSMinYEdge];
71   
72    //initially sort peer table by date
73    if ([[fMessageTable sortDescriptors] count] == 0)
74        [fMessageTable setSortDescriptors: [NSArray arrayWithObject: [[fMessageTable tableColumnWithIdentifier: @"Date"]
75                                            sortDescriptorPrototype]]];
76   
77    [[self window] setTitle: NSLocalizedString(@"Message Log", "Message window -> title")];
78   
79    //set images and text for popup button items
80    [[fLevelButton itemAtIndex: LEVEL_ERROR] setTitle: NSLocalizedString(@"Error", "Message window -> level string")];
81    [[fLevelButton itemAtIndex: LEVEL_INFO] setTitle: NSLocalizedString(@"Info", "Message window -> level string")];
82    [[fLevelButton itemAtIndex: LEVEL_DEBUG] setTitle: NSLocalizedString(@"Debug", "Message window -> level string")];
83   
84    //set table column text
85    [[[fMessageTable tableColumnWithIdentifier: @"Date"] headerCell] setTitle: NSLocalizedString(@"Date",
86        "Message window -> table column")];
87    [[[fMessageTable tableColumnWithIdentifier: @"Name"] headerCell] setTitle: NSLocalizedString(@"Process",
88        "Message window -> table column")];
89    [[[fMessageTable tableColumnWithIdentifier: @"Message"] headerCell] setTitle: NSLocalizedString(@"Message",
90        "Message window -> table column")];
91   
92    //set and size buttons
93    [fSaveButton setTitle: [NSLocalizedString(@"Save", "Message window -> save button") stringByAppendingEllipsis]];
94    [fSaveButton sizeToFit];
95   
96    NSRect saveButtonFrame = [fSaveButton frame];
97    saveButtonFrame.size.width += 10.0;
98    [fSaveButton setFrame: saveButtonFrame];
99   
100    const CGFloat oldClearButtonWidth = [fClearButton frame].size.width;
101   
102    [fClearButton setTitle: NSLocalizedString(@"Clear", "Message window -> save button")];
103    [fClearButton sizeToFit];
104   
105    NSRect clearButtonFrame = [fClearButton frame];
106    clearButtonFrame.size.width = MAX(clearButtonFrame.size.width + 10.0, saveButtonFrame.size.width);
107    clearButtonFrame.origin.x -= clearButtonFrame.size.width - oldClearButtonWidth;
108    [fClearButton setFrame: clearButtonFrame];
109   
110    //select proper level in popup button
111    switch ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"])
112    {
113        case TR_MSG_ERR:
114            [fLevelButton selectItemAtIndex: LEVEL_ERROR];
115            break;
116        case TR_MSG_INF:
117            [fLevelButton selectItemAtIndex: LEVEL_INFO];
118            break;
119        case TR_MSG_DBG:
120            [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
121            break;
122    }
123   
124    fMessages = [[NSMutableArray alloc] init];
125    fDisplayedMessages = [[NSMutableArray alloc] init];
126}
127
128- (void) windowDidBecomeKey: (NSNotification *) notification
129{
130    if (!fTimer)
131        fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
132                    selector: @selector(updateLog:) userInfo: nil repeats: YES];
133    [self updateLog: nil];
134}
135
136- (void) windowWillClose: (id)sender
137{
138    [fTimer invalidate];
139    fTimer = nil;
140}
141
142- (void) updateLog: (NSTimer *) timer
143{
144    tr_msg_list * messages;
145    if ((messages = tr_getQueuedMessages()) == NULL)
146        return;
147   
148    static NSUInteger currentIndex = 0;
149   
150    NSScroller * scroller = [[fMessageTable enclosingScrollView] verticalScroller];
151    const BOOL shouldScroll = currentIndex == 0 || [scroller floatValue] == 1.0 || [scroller isHidden]
152                                || [scroller knobProportion] == 1.0;
153   
154    const NSInteger maxLevel = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
155    BOOL changed = NO;
156   
157    for (tr_msg_list * currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
158    {
159        NSString * name = currentMessage->name != NULL ? [NSString stringWithUTF8String: currentMessage->name]
160                            : [[NSProcessInfo processInfo] processName];
161       
162        NSString * file = [[[NSString stringWithUTF8String: currentMessage->file] lastPathComponent] stringByAppendingFormat: @":%d",
163                            currentMessage->line];
164       
165        NSDictionary * message  = [NSDictionary dictionaryWithObjectsAndKeys:
166                                    [NSString stringWithUTF8String: currentMessage->message], @"Message",
167                                    [NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
168                                    [NSNumber numberWithUnsignedInteger: currentIndex++], @"Index", //more accurate when sorting by date
169                                    [NSNumber numberWithInteger: currentMessage->level], @"Level",
170                                    name, @"Name",
171                                    file, @"File", nil];
172       
173        [fMessages addObject: message];
174       
175        if (currentMessage->level <= maxLevel)
176        {
177            [fDisplayedMessages addObject: message];
178            changed = YES;
179        }
180    }
181   
182    tr_freeMessageList(messages);
183   
184    if (changed)
185    {
186        [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
187       
188        [fMessageTable reloadData];
189        if (shouldScroll)
190            [fMessageTable scrollRowToVisible: [fDisplayedMessages count]-1];
191    }
192}
193
194- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
195{
196    return [fDisplayedMessages count];
197}
198
199- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (NSInteger) row
200{
201    NSString * ident = [column identifier];
202    NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
203
204    if ([ident isEqualToString: @"Date"])
205        return [message objectForKey: @"Date"];
206    else if ([ident isEqualToString: @"Level"])
207    {
208        const NSInteger level = [[message objectForKey: @"Level"] integerValue];
209        switch (level)
210        {
211            case TR_MSG_ERR:
212                return [NSImage imageNamed: @"RedDot.png"];
213            case TR_MSG_INF:
214                return [NSImage imageNamed: @"YellowDot.png"];
215            case TR_MSG_DBG:
216                return [NSImage imageNamed: @"PurpleDot.png"];
217            default:
218                NSAssert1(NO, @"Unknown message log level: %d", level);
219                return nil;
220        }
221    }
222    else if ([ident isEqualToString: @"Name"])
223        return [message objectForKey: @"Name"];
224    else
225        return [message objectForKey: @"Message"];
226}
227
228#warning don't cut off end
229- (CGFloat) tableView: (NSTableView *) tableView heightOfRow: (NSInteger) row
230{
231    NSTableColumn * column = [tableView tableColumnWithIdentifier: @"Message"];
232   
233    if (!fAttributes)
234        fAttributes = [[[[column dataCell] attributedStringValue] attributesAtIndex: 0 effectiveRange: NULL] retain];
235   
236    const CGFloat count = floorf([[[fDisplayedMessages objectAtIndex: row] objectForKey: @"Message"] sizeWithAttributes: fAttributes].width
237                                / [column width]);
238    return [tableView rowHeight] * (count + 1.0);
239}
240
241- (void) tableView: (NSTableView *) tableView sortDescriptorsDidChange: (NSArray *) oldDescriptors
242{
243    [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
244    [fMessageTable reloadData];
245}
246
247- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
248                tableColumn: (NSTableColumn *) column row: (NSInteger) row mouseLocation: (NSPoint) mouseLocation
249{
250    NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
251    return [message objectForKey: @"File"];
252}
253
254- (void) copy: (id) sender
255{
256    NSIndexSet * indexes = [fMessageTable selectedRowIndexes];
257    NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [indexes count]];
258   
259    for (NSDictionary * message in [fDisplayedMessages objectsAtIndexes: indexes])
260        [messageStrings addObject: [self stringForMessage: message]];
261   
262    NSString * messageString = [messageStrings componentsJoinedByString: @"\n"];
263   
264    NSPasteboard * pb = [NSPasteboard generalPasteboard];
265    if ([NSApp isOnSnowLeopardOrBetter])
266    {
267        [pb clearContents];
268        [pb writeObjects: [NSArray arrayWithObject: messageString]];
269    }
270    else
271    {
272        [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
273        [pb setString: messageString forType: NSStringPboardType];
274    }
275}
276
277- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
278{
279    SEL action = [menuItem action];
280   
281    if (action == @selector(copy:))
282        return [fMessageTable numberOfSelectedRows] > 0;
283   
284    return YES;
285}
286
287- (void) changeLevel: (id) sender
288{
289    NSInteger level;
290    switch ([fLevelButton indexOfSelectedItem])
291    {
292        case LEVEL_ERROR:
293            level = TR_MSG_ERR;
294            break;
295        case LEVEL_INFO:
296            level = TR_MSG_INF;
297            break;
298        case LEVEL_DEBUG:
299            level = TR_MSG_DBG;
300            break;
301    }
302   
303    if ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"] == level)
304        return;
305   
306    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
307   
308    if (level == TR_MSG_DBG) //all messages at this level
309        [fDisplayedMessages setArray: fMessages];
310    else
311    {
312        [fDisplayedMessages removeAllObjects];
313        for (NSDictionary * message in fMessages)
314            if ([[message objectForKey: @"Level"] integerValue] <= level)
315                [fDisplayedMessages addObject: message];
316    }
317   
318    [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
319   
320    [fMessageTable deselectAll: nil];
321    [fMessageTable reloadData];
322}
323
324- (void) clearLog: (id) sender
325{
326    [fMessages removeAllObjects];
327    [fDisplayedMessages removeAllObjects];
328    [fMessageTable reloadData];
329}
330
331- (void) writeToFile: (id) sender
332{
333    //make the array sorted by date
334    NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: YES] autorelease];
335    NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
336    NSArray * sortedMessages = [[fDisplayedMessages sortedArrayUsingDescriptors: descriptors] retain];
337    [descriptors release];
338   
339    NSSavePanel * panel = [NSSavePanel savePanel];
340    [panel setRequiredFileType: @"txt"];
341    [panel setCanSelectHiddenExtension: YES];
342   
343    [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
344            modalForWindow: [self window] modalDelegate: self
345            didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: sortedMessages];
346}
347
348- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (NSArray *) messages
349{
350    if (code == NSOKButton)
351    {
352        //create the text to output
353        NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [messages count]];
354        for (NSDictionary * message in messages)
355            [messageStrings addObject: [self stringForMessage: message]];
356   
357        NSString * fileString = [messageStrings componentsJoinedByString: @"\n"];
358       
359        if (![fileString writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
360        {
361            NSAlert * alert = [[NSAlert alloc] init];
362            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
363            [alert setMessageText: NSLocalizedString(@"Log Could Not Be Saved", "Save log alert panel -> title")];
364            [alert setInformativeText: [NSString stringWithFormat:
365                    NSLocalizedString(@"There was a problem creating the file \"%@\".",
366                    "Save log alert panel -> message"), [[panel filename] lastPathComponent]]];
367            [alert setAlertStyle: NSWarningAlertStyle];
368           
369            [alert runModal];
370            [alert release];
371        }
372    }
373   
374    [messages release];
375}
376
377@end
378
379@implementation MessageWindowController (Private)
380
381- (void) resizeColumn
382{
383    [fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange:
384                    NSMakeRange(0, [fMessageTable numberOfRows])]];
385}
386
387- (NSString *) stringForMessage: (NSDictionary *) message
388{
389    NSString * level;
390    switch ([[message objectForKey: @"Level"] integerValue])
391    {
392        case TR_MSG_ERR:
393            level = NSLocalizedString(@"Error", "Message window -> level");
394            break;
395        case TR_MSG_INF:
396            level = NSLocalizedString(@"Info", "Message window -> level");
397            break;
398        case TR_MSG_DBG:
399            level = NSLocalizedString(@"Debug", "Message window -> level");
400            break;
401        default:
402            level = @"";
403    }
404   
405    return [NSString stringWithFormat: @"%@ %@ [%@] %@: %@", [message objectForKey: @"Date"],
406            [message objectForKey: @"File"], level,
407            [message objectForKey: @"Name"], [message objectForKey: @"Message"], nil];
408}
409
410@end
Note: See TracBrowser for help on using the repository browser.