source: trunk/macosx/MessageWindowController.m @ 9620

Last change on this file since 9620 was 9620, checked in by livings124, 12 years ago

(hopefully) fix a potential crash in the message log window

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