source: trunk/macosx/MessageWindowController.m @ 9294

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

trivial cleanup to the message window: set the pulldown's images in the xib, use NSInteger instead of int (same for unsigned), and don't bother storing the dot images as instance variables

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