source: trunk/macosx/MessageWindowController.m @ 3378

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

random cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 10.6 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 3378 2007-10-12 11:33:26Z 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) init
45{
46    if ((self = [super initWithWindowNibName: @"MessageWindow"]))
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    NSEnumerator * enumerator = [[fMessages objectsAtIndexes: indexes] objectEnumerator];
189    NSDictionary * message;
190    while ((message = [enumerator nextObject]))
191        [messageStrings addObject: [self stringForMessage: message]];
192   
193    [pb setString: [messageStrings componentsJoinedByString: @"\n"] forType: NSStringPboardType];
194}
195
196- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
197{
198    SEL action = [menuItem action];
199   
200    if (action == @selector(copy:))
201        return [fMessageTable numberOfSelectedRows] > 0;
202   
203    return YES;
204}
205
206- (void) changeLevel: (id) sender
207{
208    [self updateLog: nil];
209   
210    int level;
211    switch ([fLevelButton indexOfSelectedItem])
212    {
213        case LEVEL_ERROR:
214            level = TR_MSG_ERR;
215            break;
216        case LEVEL_INFO:
217            level = TR_MSG_INF;
218            break;
219        case LEVEL_DEBUG:
220            level = TR_MSG_DBG;
221            break;
222    }
223   
224    [self setDebugWarningHidden: level != TR_MSG_DBG];
225   
226    tr_setMessageLevel(level);
227    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
228}
229
230- (void) clearLog: (id) sender
231{
232    [fMessages removeAllObjects];
233    [fMessageTable reloadData];
234}
235
236- (void) writeToFile: (id) sender
237{
238    //make the array sorted by date
239    NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey: @"Date" ascending: YES] autorelease];
240    NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
241    NSArray * sortedMessages = [fMessages sortedArrayUsingDescriptors: descriptors];
242    [descriptors release];
243   
244    //create the text to output
245    NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [fMessages count]];
246    NSEnumerator * enumerator = [sortedMessages objectEnumerator];
247    NSDictionary * message;
248    while ((message = [enumerator nextObject]))
249        [messageStrings addObject: [self stringForMessage: message]];
250   
251    NSString * fileString = [[messageStrings componentsJoinedByString: @"\n"] retain];
252   
253    NSSavePanel * panel = [NSSavePanel savePanel];
254    [panel setRequiredFileType: @"txt"];
255    [panel setCanSelectHiddenExtension: YES];
256   
257    [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
258            modalForWindow: [self window] modalDelegate: self
259            didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: fileString];
260}
261
262- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
263{
264    if (code == NSOKButton)
265    {
266        if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
267        {
268            NSAlert * alert = [[NSAlert alloc] init];
269            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
270            [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Log Could Not Be Saved",
271                                    "Save log alert panel -> title")]];
272            [alert setInformativeText: [NSString stringWithFormat:
273                    NSLocalizedString(@"There was a problem creating the file \"%@\".",
274                                        "Save log alert panel -> message"), [[panel filename] lastPathComponent]]];
275            [alert setAlertStyle: NSWarningAlertStyle];
276           
277            [alert runModal];
278            [alert release];
279        }
280    }
281   
282    [string release];
283}
284
285@end
286
287@implementation MessageWindowController (Private)
288
289- (NSString *) stringForMessage: (NSDictionary *) message
290{
291    NSString * level;
292    switch ([[message objectForKey: @"Level"] intValue])
293    {
294        case TR_MSG_ERR:
295            level = @"Error";
296            break;
297        case TR_MSG_INF:
298            level = @"Info";
299            break;
300        case TR_MSG_DBG:
301            level = @"Debug";
302            break;
303        default:
304            level = @"";
305    }
306   
307    return [NSString stringWithFormat: @"%@ [%@] %@", [message objectForKey: @"Date"], level, [message objectForKey: @"Message"]];
308}
309
310- (void) setDebugWarningHidden: (BOOL) hide
311{
312    [fDebugWarningField setHidden: hide];
313    [fDebugWarningIcon setHidden: hide];
314}
315
316@end
Note: See TracBrowser for help on using the repository browser.