source: trunk/macosx/MessageWindowController.m @ 6959

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

update some floats to CGFloats; append "f" to some float constants

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