source: trunk/macosx/MessageWindowController.m @ 4315

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

set up message log in tr_initFull

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