source: trunk/macosx/MessageWindowController.m @ 820

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

Get rid of blank line at bottom of message log.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.0 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 820 2006-08-22 03:11:24Z 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@implementation MessageWindowController
35
36- (id) initWithWindowNibName: (NSString *) name
37{
38    if ((self = [super initWithWindowNibName: name]))
39    {
40        fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
41                    selector: @selector(updateLog:) userInfo: nil repeats: YES];
42       
43        [[self window] update]; //make sure nib is loaded right away
44    }
45    return self;
46}
47
48- (void) dealloc
49{
50    [fTimer invalidate];
51    [super dealloc];
52}
53
54- (void) awakeFromNib
55{
56    int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
57    if (level == TR_MSG_ERR)
58        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
59    else if (level == TR_MSG_INF)
60        [fLevelButton selectItemAtIndex: LEVEL_INFO];
61    else if (level == TR_MSG_DBG)
62        [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
63    else
64    {
65        level = TR_MSG_ERR;
66        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
67        [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
68    }
69   
70    tr_setMessageLevel(level);
71    tr_setMessageQueuing(1);
72}
73
74- (void) updateLog: (NSTimer *) timer
75{
76    tr_msg_list_t * messages, * currentMessage;
77    if (!(messages = tr_getQueuedMessages()))
78        return;
79   
80    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
81    NSScroller * scroller = [fScrollView verticalScroller];
82    BOOL shouldScroll = [scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0;
83   
84    NSAttributedString * messageString;
85    NSString * levelString;
86    NSCalendarDate * dateString;
87    for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
88    {
89        //new line if text view is not empty
90        if (currentMessage != messages || ![[fTextView string] isEqualToString: @""])
91            [[fTextView textStorage] appendAttributedString: [[[NSAttributedString alloc]
92                                                                initWithString: @"\n"] autorelease]];
93       
94        int level = currentMessage->level;
95        if (level == TR_MSG_ERR)
96            levelString = @"ERR";
97        else if (level == TR_MSG_INF)
98            levelString = @"INF";
99        else if (level == TR_MSG_DBG)
100            levelString = @"DBG";
101        else
102            levelString = @"???";
103       
104        dateString = [[NSDate dateWithTimeIntervalSince1970: currentMessage->when]
105                            dateWithCalendarFormat: @"%Y-%m-%d %H:%M:%S" timeZone: nil];
106        messageString = [[[NSAttributedString alloc] initWithString: [NSString stringWithFormat: @"%@ %@ %s",
107                            dateString, levelString, currentMessage->message]] autorelease];
108       
109        [[fTextView textStorage] appendAttributedString: messageString];
110    }
111   
112    tr_freeMessageList(messages);
113   
114    [fTextView setFont: [NSFont fontWithName: @"Monaco" size: 10]]; //find a way to set this permanently
115   
116    if (shouldScroll)
117        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
118}
119
120- (void) changeLevel: (id) sender
121{
122    int selection = [fLevelButton indexOfSelectedItem], level;
123    if (selection == LEVEL_INFO)
124        level = TR_MSG_INF;
125    else if (selection == LEVEL_DEBUG)
126        level = TR_MSG_DBG;
127    else
128        level = TR_MSG_ERR;
129   
130    [self updateLog: nil];
131   
132    tr_setMessageLevel(level);
133    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
134}
135
136- (void) clearLog: (id) sender
137{
138    [fTextView setString: @""];
139}
140
141@end
Note: See TracBrowser for help on using the repository browser.