source: trunk/macosx/MessageWindowController.m @ 814

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

Larger default message window, font matching default Console look, and show date (in same format as Console).

  • Property svn:keywords set to Date Rev Author Id
File size: 5.2 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 814 2006-08-22 01:59:46Z 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", [[NSDate date] dateWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"
113                                timeZone: nil], levelString, message]] autorelease];
114   
115    [fLock lock];
116    [fBufferArray addObject: messageString];
117    [fLock unlock];
118   
119    [pool release];
120}
121
122- (void) updateLog: (NSTimer *) timer
123{
124    if ([fBufferArray count] == 0)
125        return;
126   
127    [fLock lock];
128   
129    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
130    BOOL shouldScroll = NO;
131    NSScroller * scroller = [fScrollView verticalScroller];
132    if ([scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0)
133        shouldScroll = YES;
134   
135    NSEnumerator * enumerator = [fBufferArray objectEnumerator];
136    NSAttributedString * messageString;
137    while ((messageString = [enumerator nextObject]))
138        [[fTextView textStorage] appendAttributedString: messageString];
139    [fBufferArray removeAllObjects];
140   
141    [fTextView setFont: [NSFont fontWithName: @"Monaco" size: 10]]; //find a way to set this permanently
142   
143    if (shouldScroll)
144        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
145   
146    [fLock unlock];
147}
148
149- (void) changeLevel: (id) sender
150{
151    int selection = [fLevelButton indexOfSelectedItem], level;
152    if (selection == LEVEL_INFO)
153        level = TR_MSG_INF;
154    else if (selection == LEVEL_DEBUG)
155        level = TR_MSG_DBG;
156    else
157        level = TR_MSG_ERR;
158   
159    [self updateLog: nil];
160   
161    tr_setMessageLevel(level);
162    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
163}
164
165- (void) clearLog: (id) sender
166{
167    [fLock lock];
168    [fTextView setString: @""];
169    [fLock unlock];
170}
171
172@end
Note: See TracBrowser for help on using the repository browser.