source: trunk/macosx/MessageWindowController.m @ 823

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

Log window now indents none-first lines.

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