source: trunk/macosx/MessageWindowController.m @ 829

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

Limit message log to 1000 lines.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.2 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 829 2006-08-24 01:55:09Z 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#define MAX_LINES       1000
34
35@implementation MessageWindowController
36
37- (id) initWithWindowNibName: (NSString *) name
38{
39    if ((self = [super initWithWindowNibName: name]))
40    {
41        NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
42        [paragraph setHeadIndent: 20.0];
43       
44        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
45                        [NSFont fontWithName: @"Monaco" size: 10], NSFontAttributeName,
46                        paragraph, NSParagraphStyleAttributeName, nil];
47        [paragraph release];
48       
49        fLines = 0;
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    [fAttributes release];
63   
64    [super dealloc];
65}
66
67- (void) awakeFromNib
68{
69    int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
70    if (level == TR_MSG_ERR)
71        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
72    else if (level == TR_MSG_INF)
73        [fLevelButton selectItemAtIndex: LEVEL_INFO];
74    else if (level == TR_MSG_DBG)
75        [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
76    else
77    {
78        level = TR_MSG_ERR;
79        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
80        [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
81    }
82   
83    tr_setMessageLevel(level);
84    tr_setMessageQueuing(1);
85}
86
87- (void) updateLog: (NSTimer *) timer
88{
89    tr_msg_list_t * messages, * currentMessage;
90    if (!(messages = tr_getQueuedMessages()))
91        return;
92   
93    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
94    NSScroller * scroller = [fScrollView verticalScroller];
95    BOOL shouldScroll = [scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0;
96   
97    NSString * levelString, * dateString, * messageString;
98    for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
99    {
100        int level = currentMessage->level;
101        if (level == TR_MSG_ERR)
102            levelString = @"ERR";
103        else if (level == TR_MSG_INF)
104            levelString = @"INF";
105        else if (level == TR_MSG_DBG)
106            levelString = @"DBG";
107        else
108            levelString = @"???";
109       
110        dateString = [[NSDate dateWithTimeIntervalSince1970: currentMessage->when]
111                            descriptionWithCalendarFormat: @"%1m/%d %H:%M:%S" timeZone: nil locale: nil];
112        messageString = [NSString stringWithFormat: @"%s%@ %@ %s",
113                            fLines > 0 ? "\n" : "", dateString, levelString, currentMessage->message];
114       
115        //remove the first line if at max number of lines
116        if (fLines == MAX_LINES)
117        {
118            NSString * text = [fTextView string];
119            unsigned int loc = [text rangeOfString: @"\n"].location;
120            if (loc != NSNotFound)
121                [fTextView setString: [[text substringFromIndex: loc + 1] stringByAppendingString: messageString]];
122        }
123        else
124        {
125            [[fTextView textStorage] appendAttributedString: [[[NSAttributedString alloc] initWithString:
126                                        messageString attributes: fAttributes] autorelease]];
127            fLines++;
128        }
129    }
130   
131    tr_freeMessageList(messages);
132   
133    if (shouldScroll)
134        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
135}
136
137- (void) changeLevel: (id) sender
138{
139    int selection = [fLevelButton indexOfSelectedItem], level;
140    if (selection == LEVEL_INFO)
141        level = TR_MSG_INF;
142    else if (selection == LEVEL_DEBUG)
143        level = TR_MSG_DBG;
144    else
145        level = TR_MSG_ERR;
146   
147    [self updateLog: nil];
148   
149    tr_setMessageLevel(level);
150    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
151}
152
153- (void) clearLog: (id) sender
154{
155    [fTextView setString: @""];
156    fLines = 0;
157}
158
159- (void) writeToFile: (id) sender
160{
161    NSString * string = [[fTextView string] retain];
162   
163    NSSavePanel * panel = [NSSavePanel savePanel];
164    [panel setRequiredFileType: @"txt"];
165    [panel setCanSelectHiddenExtension: YES];
166   
167    [panel beginSheetForDirectory: nil file: @"untitled" modalForWindow: [self window] modalDelegate: self
168        didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: string];
169}
170
171- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
172{
173    if (code == NSOKButton)
174        [string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil];
175   
176    [string release];
177}
178
179@end
Note: See TracBrowser for help on using the repository browser.