source: trunk/macosx/MessageWindowController.m @ 9844

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

happy new year!

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