source: trunk/macosx/MessageWindowController.m @ 4316

Last change on this file since 4316 was 4316, checked in by livings124, 15 years ago

only update the message window when it's visible

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