source: trunk/macosx/MessageWindowController.m @ 841

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

Only update peer table when it's visible

  • Property svn:keywords set to Date Rev Author Id
File size: 6.7 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 841 2006-09-03 16:16:35Z 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 = [[NSParagraphStyle 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()) == NULL)
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 textStorage] deleteCharactersInRange: NSMakeRange(0, loc + 1)];
122        }
123        else
124            fLines++;
125       
126        [[fTextView textStorage] appendAttributedString: [[[NSAttributedString alloc] initWithString:
127                                        messageString attributes: fAttributes] autorelease]];
128    }
129   
130    tr_freeMessageList(messages);
131   
132    if (shouldScroll)
133        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
134}
135
136- (void) changeLevel: (id) sender
137{
138    [self updateLog: nil];
139   
140    int selection = [fLevelButton indexOfSelectedItem], level;
141    if (selection == LEVEL_INFO)
142        level = TR_MSG_INF;
143    else if (selection == LEVEL_DEBUG)
144        level = TR_MSG_DBG;
145    else
146        level = TR_MSG_ERR;
147   
148    tr_setMessageLevel(level);
149    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
150}
151
152- (void) clearLog: (id) sender
153{
154    [fTextView setString: @""];
155    fLines = 0;
156}
157
158- (void) writeToFile: (id) sender
159{
160    NSString * string = [[fTextView string] retain];
161   
162    NSSavePanel * panel = [NSSavePanel savePanel];
163    [panel setRequiredFileType: @"txt"];
164    [panel setCanSelectHiddenExtension: YES];
165   
166    [panel beginSheetForDirectory: nil file: @"untitled" modalForWindow: [self window] modalDelegate: self
167        didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: string];
168}
169
170- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
171{
172    if (code == NSOKButton)
173    {
174        if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
175        {
176            NSAlert * alert = [[NSAlert alloc] init];
177            [alert addButtonWithTitle: @"OK"];
178            [alert setMessageText: [NSString stringWithFormat: @"Log Could Not Be Saved"]];
179            [alert setInformativeText: [NSString stringWithFormat:
180                    @"There was a problem creating the file \"%@\".", [[panel filename] lastPathComponent]]];
181            [alert setAlertStyle: NSWarningAlertStyle];
182           
183            [alert runModal];
184        }
185    }
186   
187    [string release];
188}
189
190@end
Note: See TracBrowser for help on using the repository browser.