source: trunk/macosx/MessageWindowController.m @ 3264

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

"switch-ify" some code

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