source: trunk/macosx/MessageWindowController.m @ 824

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

Add Find to edit menu, since find can be useful for debugging.

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