source: trunk/macosx/MessageWindowController.m @ 3036

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

use images for error levels...it needs better images

  • Property svn:keywords set to Date Rev Author Id
File size: 8.4 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 3036 2007-09-10 20:05:24Z 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    2500
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    [[self window] center];
70   
71    fErrorImage = [NSImage imageNamed: @"RedDot.tiff"];
72    fInfoImage = [NSImage imageNamed: @"YellowDot.tiff"];
73    fDebugImage = [NSImage imageNamed: @"GreenDot.tiff"];
74   
75    //set images to popup button items
76    [[fLevelButton itemAtIndex: LEVEL_ERROR] setImage: fErrorImage];
77    [[fLevelButton itemAtIndex: LEVEL_INFO] setImage: fInfoImage];
78    [[fLevelButton itemAtIndex: LEVEL_DEBUG] setImage: fDebugImage];
79   
80    //select proper level in popup button
81    int level = tr_getMessageLevel();
82    if (level == TR_MSG_ERR)
83        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
84    else if (level == TR_MSG_INF)
85        [fLevelButton selectItemAtIndex: LEVEL_INFO];
86    else if (level == TR_MSG_DBG)
87        [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
88    else
89    {
90        level = TR_MSG_ERR;
91        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
92        [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
93    }
94   
95    [self setDebugWarningHidden: level != TR_MSG_DBG];
96}
97
98- (void) updateLog: (NSTimer *) timer
99{
100    tr_msg_list_t * messages, * currentMessage;
101    if ((messages = tr_getQueuedMessages()) == NULL)
102        return;
103   
104    for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
105        [fMessages addObject: [NSDictionary dictionaryWithObjectsAndKeys:
106                                [NSString stringWithUTF8String: currentMessage->message], @"Message",
107                                [NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
108                                [NSNumber numberWithInt: currentMessage->level], @"Level", nil]];
109   
110    #warning still needed?
111    int total = [fMessages count];
112    if (total > MAX_MESSAGES)
113        [fMessages removeObjectsInRange: NSMakeRange(0, total-MAX_MESSAGES)];
114   
115    [fMessageView reloadData];
116   
117    tr_freeMessageList(messages);
118}
119
120- (int) numberOfRowsInTableView: (NSTableView *) tableView
121{
122    return [fMessages count];
123}
124
125- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row
126{
127    NSString * ident = [column identifier];
128    NSDictionary * message = [fMessages objectAtIndex: row];
129
130    if ([ident isEqualToString: @"Date"])
131        return [message objectForKey: @"Date"];
132    else if ([ident isEqualToString: @"Level"])
133    {
134        int level = [[message objectForKey: @"Level"] intValue];
135        if (level == TR_MSG_ERR)
136            return fErrorImage;
137        else if (level == TR_MSG_INF)
138            return fInfoImage;
139        else if (level == TR_MSG_DBG)
140            return fDebugImage;
141        else
142            return nil;
143    }
144    else
145        return [message objectForKey: @"Message"];
146}
147
148- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
149                tableColumn: (NSTableColumn *) column row: (int) row mouseLocation: (NSPoint) mouseLocation
150{
151    return [self stringForMessage: [fMessages objectAtIndex: row]];
152}
153
154- (void) changeLevel: (id) sender
155{
156    [self updateLog: nil];
157   
158    int selection = [fLevelButton indexOfSelectedItem], level;
159    if (selection == LEVEL_INFO)
160        level = TR_MSG_INF;
161    else if (selection == LEVEL_DEBUG)
162        level = TR_MSG_DBG;
163    else
164        level = TR_MSG_ERR;
165   
166    [self setDebugWarningHidden: level != TR_MSG_DBG];
167   
168    tr_setMessageLevel(level);
169    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
170}
171
172- (void) clearLog: (id) sender
173{
174    [fMessages removeAllObjects];
175    [fMessageView reloadData];
176}
177
178- (void) writeToFile: (id) sender
179{
180    //create the text to output
181    NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [fMessages count]];
182   
183    NSEnumerator * enumerator = [fMessages objectEnumerator];
184    NSDictionary * message;
185    while ((message = [enumerator nextObject]))
186        [messageStrings addObject: [self stringForMessage: message]];
187   
188    NSString * fileString = [[messageStrings componentsJoinedByString: @"\n"] retain];
189   
190    NSSavePanel * panel = [NSSavePanel savePanel];
191    [panel setRequiredFileType: @"txt"];
192    [panel setCanSelectHiddenExtension: YES];
193   
194    [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
195            modalForWindow: [self window] modalDelegate: self
196            didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: fileString];
197}
198
199- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
200{
201    if (code == NSOKButton)
202    {
203        if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
204        {
205            NSAlert * alert = [[NSAlert alloc] init];
206            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
207            [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Log Could Not Be Saved",
208                                    "Save log alert panel -> title")]];
209            [alert setInformativeText: [NSString stringWithFormat:
210                    NSLocalizedString(@"There was a problem creating the file \"%@\".",
211                                        "Save log alert panel -> message"), [[panel filename] lastPathComponent]]];
212            [alert setAlertStyle: NSWarningAlertStyle];
213           
214            [alert runModal];
215            [alert release];
216        }
217    }
218   
219    [string release];
220}
221
222@end
223
224@implementation MessageWindowController (Private)
225
226- (NSString *) stringForMessage: (NSDictionary *) message
227{
228    int level = [[message objectForKey: @"Level"] intValue];
229    NSString * levelString;
230    if (level == TR_MSG_ERR)
231        levelString = @"Error";
232    else if (level == TR_MSG_INF)
233        levelString = @"Info";
234    else if (level == TR_MSG_DBG)
235        levelString = @"Debug";
236    else
237        levelString = @"???";
238   
239    return [NSString stringWithFormat: @"%@ %@ %@", [message objectForKey: @"Date"], levelString, [message objectForKey: @"Message"]];
240}
241
242- (void) setDebugWarningHidden: (BOOL) hide
243{
244    [fDebugWarningField setHidden: hide];
245    [fDebugWarningIcon setHidden: hide];
246}
247
248@end
Note: See TracBrowser for help on using the repository browser.