source: trunk/macosx/MessageWindowController.m @ 9408

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

we should use the size of the array that we're actually using

  • Property svn:keywords set to Date Rev Author Id
File size: 14.8 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 9408 2009-10-25 21:31:01Z 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.6
35#define MAX_MESSAGES    8000
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    [fMessages 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: [[fMessageTable enclosingScrollView] frame].origin.y 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 (tr_getMessageLevel())
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    }
122   
123    fMessages = [[NSMutableArray alloc] init];
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    static NSUInteger currentIndex = 0;
147   
148    NSScroller * scroller = [[fMessageTable enclosingScrollView] verticalScroller];
149    const BOOL shouldScroll = currentIndex == 0 || [scroller floatValue] == 1.0 || [scroller isHidden]
150                                || [scroller knobProportion] == 1.0;
151   
152    for (tr_msg_list * currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
153    {
154        NSString * name = currentMessage->name != NULL ? [NSString stringWithUTF8String: currentMessage->name]
155                            : [[NSProcessInfo processInfo] processName];
156       
157        NSDictionary * message  = [NSDictionary dictionaryWithObjectsAndKeys:
158                                    [NSString stringWithUTF8String: currentMessage->message], @"Message",
159                                    [NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
160                                    [NSNumber numberWithUnsignedInteger: currentIndex++], @"Index", //more accurate when sorting by date
161                                    [NSNumber numberWithInteger: currentMessage->level], @"Level",
162                                    name, @"Name",
163                                    [NSString stringWithUTF8String: currentMessage->file], @"File",
164                                    [NSNumber numberWithInteger: currentMessage->line], @"Line", nil];
165                               
166        [fMessages addObject: message];
167    }
168   
169    tr_freeMessageList(messages);
170   
171    NSUInteger total = [fMessages count];
172    if (total > MAX_MESSAGES)
173    {
174        //remove the oldest - move oldest to end for (assumedly) most efficient removal
175        NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: NO] autorelease];
176        [fMessages sortUsingDescriptors: [NSArray arrayWithObject: descriptor]];
177       
178        [fMessages removeObjectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(MAX_MESSAGES, total-MAX_MESSAGES)]];
179       
180        [fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, MAX_MESSAGES)]];
181        total = MAX_MESSAGES;
182    }
183   
184    [fMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
185   
186    [fMessageTable reloadData];
187    if (shouldScroll)
188        [fMessageTable scrollRowToVisible: total-1];
189}
190
191- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
192{
193    return [fMessages count];
194}
195
196- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (NSInteger) row
197{
198    NSString * ident = [column identifier];
199    NSDictionary * message = [fMessages objectAtIndex: row];
200
201    if ([ident isEqualToString: @"Date"])
202        return [message objectForKey: @"Date"];
203    else if ([ident isEqualToString: @"Level"])
204    {
205        const NSInteger level = [[message objectForKey: @"Level"] integerValue];
206        switch (level)
207        {
208            case TR_MSG_ERR:
209                return [NSImage imageNamed: @"RedDot.png"];
210            case TR_MSG_INF:
211                return [NSImage imageNamed: @"YellowDot.png"];
212            case TR_MSG_DBG:
213                return [NSImage imageNamed: @"PurpleDot.png"];
214            default:
215                NSAssert1(NO, @"Unknown message log level: %d", level);
216                return nil;
217        }
218    }
219    else if ([ident isEqualToString: @"Name"])
220        return [message objectForKey: @"Name"];
221    else
222        return [message objectForKey: @"Message"];
223}
224
225#warning don't cut off end
226- (CGFloat) tableView: (NSTableView *) tableView heightOfRow: (NSInteger) row
227{
228    NSTableColumn * column = [tableView tableColumnWithIdentifier: @"Message"];
229   
230    if (!fAttributes)
231        fAttributes = [[[[column dataCell] attributedStringValue] attributesAtIndex: 0 effectiveRange: NULL] retain];
232   
233    const CGFloat count = floorf([[[fMessages objectAtIndex: row] objectForKey: @"Message"] sizeWithAttributes: fAttributes].width
234                                / [column width]);
235    return [tableView rowHeight] * (count + 1.0);
236}
237
238- (void) tableView: (NSTableView *) tableView sortDescriptorsDidChange: (NSArray *) oldDescriptors
239{
240    [fMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
241    [fMessageTable reloadData];
242}
243
244- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
245                tableColumn: (NSTableColumn *) column row: (NSInteger) row mouseLocation: (NSPoint) mouseLocation
246{
247    NSDictionary * message = [fMessages objectAtIndex: row];
248    return [NSString stringWithFormat: @"%@:%@", [[message objectForKey: @"File"] lastPathComponent], [message objectForKey: @"Line"]];
249}
250
251- (void) copy: (id) sender
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    NSString * messageString = [messageStrings componentsJoinedByString: @"\n"];
260   
261    NSPasteboard * pb = [NSPasteboard generalPasteboard];
262    if ([NSApp isOnSnowLeopardOrBetter])
263    {
264        [pb clearContents];
265        [pb writeObjects: [NSArray arrayWithObject: messageString]];
266    }
267    else
268    {
269        [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
270        [pb setString: messageString forType: NSStringPboardType];
271    }
272}
273
274- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
275{
276    SEL action = [menuItem action];
277   
278    if (action == @selector(copy:))
279        return [fMessageTable numberOfSelectedRows] > 0;
280   
281    return YES;
282}
283
284- (void) changeLevel: (id) sender
285{
286    [self updateLog: nil];
287   
288    NSInteger level;
289    switch ([fLevelButton indexOfSelectedItem])
290    {
291        case LEVEL_ERROR:
292            level = TR_MSG_ERR;
293            break;
294        case LEVEL_INFO:
295            level = TR_MSG_INF;
296            break;
297        case LEVEL_DEBUG:
298            level = TR_MSG_DBG;
299            break;
300    }
301   
302    tr_setMessageLevel(level);
303    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
304}
305
306- (void) clearLog: (id) sender
307{
308    [fMessages removeAllObjects];
309    [fMessageTable reloadData];
310}
311
312- (void) writeToFile: (id) sender
313{
314    //make the array sorted by date
315    NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Index" ascending: YES] autorelease];
316    NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
317    NSArray * sortedMessages = [[fMessages sortedArrayUsingDescriptors: descriptors] retain];
318    [descriptors release];
319   
320    NSSavePanel * panel = [NSSavePanel savePanel];
321    [panel setRequiredFileType: @"txt"];
322    [panel setCanSelectHiddenExtension: YES];
323   
324    [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
325            modalForWindow: [self window] modalDelegate: self
326            didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: sortedMessages];
327}
328
329- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (NSInteger) code contextInfo: (NSArray *) messages
330{
331    if (code == NSOKButton)
332    {
333        //create the text to output
334        NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [messages count]];
335        for (NSDictionary * message in messages)
336            [messageStrings addObject: [self stringForMessage: message]];
337   
338        NSString * fileString = [messageStrings componentsJoinedByString: @"\n"];
339       
340        if (![fileString writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
341        {
342            NSAlert * alert = [[NSAlert alloc] init];
343            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
344            [alert setMessageText: NSLocalizedString(@"Log Could Not Be Saved", "Save log alert panel -> title")];
345            [alert setInformativeText: [NSString stringWithFormat:
346                    NSLocalizedString(@"There was a problem creating the file \"%@\".",
347                    "Save log alert panel -> message"), [[panel filename] lastPathComponent]]];
348            [alert setAlertStyle: NSWarningAlertStyle];
349           
350            [alert runModal];
351            [alert release];
352        }
353    }
354   
355    [messages release];
356}
357
358@end
359
360@implementation MessageWindowController (Private)
361
362- (void) resizeColumn
363{
364    [fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange:
365                    NSMakeRange(0, [fMessageTable numberOfRows])]];
366}
367
368- (NSString *) stringForMessage: (NSDictionary *) message
369{
370    NSString * level;
371    switch ([[message objectForKey: @"Level"] integerValue])
372    {
373        case TR_MSG_ERR:
374            level = NSLocalizedString(@"Error", "Message window -> level");
375            break;
376        case TR_MSG_INF:
377            level = NSLocalizedString(@"Info", "Message window -> level");
378            break;
379        case TR_MSG_DBG:
380            level = NSLocalizedString(@"Debug", "Message window -> level");
381            break;
382        default:
383            level = @"";
384    }
385   
386    return [NSString stringWithFormat: @"%@ %@:%@ [%@] %@: %@", [message objectForKey: @"Date"],
387            [[message objectForKey: @"File"] lastPathComponent], [message objectForKey: @"Line"], level,
388            [message objectForKey: @"Name"], [message objectForKey: @"Message"], nil];
389}
390
391@end
Note: See TracBrowser for help on using the repository browser.