source: trunk/macosx/MessageWindowController.m @ 813

Last change on this file since 813 was 813, checked in by livings124, 16 years ago

Empty (and display) buffer right away when changing message level, and update the log a little more often.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.0 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 813 2006-08-22 01:08:44Z livings124 $
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.35
33
34@interface MessageWindowController (Private)
35
36MessageWindowController * selfReference; //I'm not sure why "self" can't be used directly
37
38@end
39
40@implementation MessageWindowController
41
42- (id) initWithWindowNibName: (NSString *) name
43{
44    if ((self = [super initWithWindowNibName: name]))
45    {
46        selfReference = self;
47       
48        fLock = [[NSLock alloc] init];
49        fBufferArray = [[NSMutableArray alloc] init];
50       
51        fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
52                    selector: @selector(updateLog:) userInfo: nil repeats: YES];
53       
54        [[self window] update]; //make sure nib is loaded right away
55    }
56    return self;
57}
58
59- (void) dealloc
60{
61    [fTimer invalidate];
62   
63    tr_setMessageFunction(NULL);
64   
65    [fLock release];
66    [fBufferArray release];
67   
68    [super dealloc];
69}
70
71- (void) awakeFromNib
72{
73    int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
74    if (level == TR_MSG_ERR)
75        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
76    else if (level == TR_MSG_INF)
77        [fLevelButton selectItemAtIndex: LEVEL_INFO];
78    else if (level == TR_MSG_DBG)
79        [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
80    else
81    {
82        level = TR_MSG_ERR;
83        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
84        [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
85    }
86   
87    tr_setMessageLevel(level);
88    tr_setMessageFunction(addMessage);
89}
90
91void addMessage(int level, const char * message)
92{
93    [selfReference addMessage: message level: level];
94}
95
96- (void) addMessage: (const char *) message level: (int) level
97{
98    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
99
100    NSString * levelString;
101   
102    if (level == TR_MSG_ERR)
103        levelString = @"ERR";
104    else if (level == TR_MSG_INF)
105        levelString = @"INF";
106    else if (level == TR_MSG_DBG)
107        levelString = @"DBG";
108    else
109        levelString = @"???";
110   
111    NSAttributedString * messageString = [[[NSAttributedString alloc] initWithString:
112                            [NSString stringWithFormat: @"%@: %s\n", levelString, message]] autorelease];
113   
114    [fLock lock];
115    [fBufferArray addObject: messageString];
116    [fLock unlock];
117   
118    [pool release];
119}
120
121- (void) updateLog: (NSTimer *) timer
122{
123    if ([fBufferArray count] == 0)
124        return;
125   
126    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
127    BOOL shouldScroll = NO;
128    NSScroller * scroller = [fScrollView verticalScroller];
129    if ([scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0)
130        shouldScroll = YES;
131   
132    [fLock lock];
133   
134    NSEnumerator * enumerator = [fBufferArray objectEnumerator];
135    NSAttributedString * messageString;
136    while ((messageString = [enumerator nextObject]))
137        [[fTextView textStorage] appendAttributedString: messageString];
138    [fBufferArray removeAllObjects];
139   
140    [fLock unlock];
141   
142    if (shouldScroll)
143        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
144}
145
146- (void) changeLevel: (id) sender
147{
148    int selection = [fLevelButton indexOfSelectedItem], level;
149    if (selection == LEVEL_INFO)
150        level = TR_MSG_INF;
151    else if (selection == LEVEL_DEBUG)
152        level = TR_MSG_DBG;
153    else
154        level = TR_MSG_ERR;
155   
156    [self updateLog: nil];
157   
158    tr_setMessageLevel(level);
159    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
160}
161
162- (void) clearLog: (id) sender
163{
164    [fLock lock];
165    [fTextView setString: @""];
166    [fLock unlock];
167}
168
169@end
Note: See TracBrowser for help on using the repository browser.