source: trunk/macosx/MessageWindowController.m @ 3608

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

allow messages to take multiple rows in message

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