source: trunk/macosx/MessageWindowController.m @ 920

Last change on this file since 920 was 920, checked in by joshe, 15 years ago

Merge nat-traversal branch to trunk.

  • Property svn:keywords set to Date Rev Author Id
File size: 7.0 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 920 2006-09-25 18:37:45Z joshe $
3 *
4 * Copyright (c) 2006 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_LINES       2500
34
35@interface MessageWindowController (Private)
36
37- (void) setDebugWarningHidden: (BOOL) hide;
38
39@end
40
41@implementation MessageWindowController
42
43- (id) initWithWindowNibName: (NSString *) name
44{
45    if ((self = [super initWithWindowNibName: name]))
46    {
47        NSMutableParagraphStyle * paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
48        [paragraph setHeadIndent: 20.0];
49       
50        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
51                        [NSFont fontWithName: @"Monaco" size: 10], NSFontAttributeName,
52                        paragraph, NSParagraphStyleAttributeName, nil];
53        [paragraph release];
54       
55        fLines = 0;
56       
57        fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
58                    selector: @selector(updateLog:) userInfo: nil repeats: YES];
59       
60        [[self window] update]; //make sure nib is loaded right away
61    }
62    return self;
63}
64
65- (void) dealloc
66{
67    [fTimer invalidate];
68    [fAttributes release];
69   
70    [super dealloc];
71}
72
73- (void) awakeFromNib
74{
75    int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
76    if (level == TR_MSG_ERR)
77        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
78    else if (level == TR_MSG_INF)
79        [fLevelButton selectItemAtIndex: LEVEL_INFO];
80    else if (level == TR_MSG_DBG)
81        [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
82    else
83    {
84        level = TR_MSG_ERR;
85        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
86        [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
87    }
88   
89    [self setDebugWarningHidden: level != TR_MSG_DBG];
90   
91    tr_setMessageLevel(level);
92    tr_setMessageQueuing(1);
93}
94
95- (void) updateLog: (NSTimer *) timer
96{
97    tr_msg_list_t * messages, * currentMessage;
98    if ((messages = tr_getQueuedMessages()) == NULL)
99        return;
100   
101    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
102    NSScroller * scroller = [fScrollView verticalScroller];
103    BOOL shouldScroll = [scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0;
104   
105    NSString * levelString, * dateString, * messageString;
106    for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
107    {
108        int level = currentMessage->level;
109        if (level == TR_MSG_ERR)
110            levelString = @"ERR";
111        else if (level == TR_MSG_INF)
112            levelString = @"INF";
113        else if (level == TR_MSG_DBG)
114            levelString = @"DBG";
115        else
116            levelString = @"???";
117       
118        dateString = [[NSDate dateWithTimeIntervalSince1970: currentMessage->when]
119                            descriptionWithCalendarFormat: @"%1m/%d %H:%M:%S" timeZone: nil locale: nil];
120        messageString = [NSString stringWithFormat: @"%s%@ %@ %s",
121                            fLines > 0 ? "\n" : "", dateString, levelString, currentMessage->message];
122       
123        //remove the first line if at max number of lines
124        if (fLines == MAX_LINES)
125        {
126            unsigned int loc = [[fTextView string] rangeOfString: @"\n"].location;
127            if (loc != NSNotFound)
128                [[fTextView textStorage] deleteCharactersInRange: NSMakeRange(0, loc + 1)];
129        }
130        else
131            fLines++;
132       
133        [[fTextView textStorage] appendAttributedString: [[[NSAttributedString alloc] initWithString:
134                                        messageString attributes: fAttributes] autorelease]];
135    }
136   
137    tr_freeMessageList(messages);
138   
139    if (shouldScroll)
140        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
141}
142
143- (void) changeLevel: (id) sender
144{
145    [self updateLog: nil];
146   
147    int selection = [fLevelButton indexOfSelectedItem], level;
148    if (selection == LEVEL_INFO)
149        level = TR_MSG_INF;
150    else if (selection == LEVEL_DEBUG)
151        level = TR_MSG_DBG;
152    else
153        level = TR_MSG_ERR;
154   
155    [self setDebugWarningHidden: level != TR_MSG_DBG];
156   
157    tr_setMessageLevel(level);
158    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
159}
160
161- (void) clearLog: (id) sender
162{
163    [fTextView setString: @""];
164    fLines = 0;
165}
166
167- (void) writeToFile: (id) sender
168{
169    NSString * string = [[fTextView string] retain];
170   
171    NSSavePanel * panel = [NSSavePanel savePanel];
172    [panel setRequiredFileType: @"txt"];
173    [panel setCanSelectHiddenExtension: YES];
174   
175    [panel beginSheetForDirectory: nil file: @"untitled" modalForWindow: [self window] modalDelegate: self
176        didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: string];
177}
178
179- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
180{
181    if (code == NSOKButton)
182    {
183        if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
184        {
185            NSAlert * alert = [[NSAlert alloc] init];
186            [alert addButtonWithTitle: @"OK"];
187            [alert setMessageText: [NSString stringWithFormat: @"Log Could Not Be Saved"]];
188            [alert setInformativeText: [NSString stringWithFormat:
189                    @"There was a problem creating the file \"%@\".", [[panel filename] lastPathComponent]]];
190            [alert setAlertStyle: NSWarningAlertStyle];
191           
192            [alert runModal];
193        }
194    }
195   
196    [string release];
197}
198
199- (void) setDebugWarningHidden: (BOOL) hide
200{
201    [fDebugWarningField setHidden: hide];
202    [fDebugWarningIcon setHidden: hide];
203}
204
205@end
Note: See TracBrowser for help on using the repository browser.