source: trunk/macosx/MessageWindowController.m @ 819

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

Re-add time to message log.

  • Property svn:keywords set to Date Rev Author Id
File size: 4.6 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 819 2006-08-22 02:56: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
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        int level = currentMessage->level;
90        if (level == TR_MSG_ERR)
91            levelString = @"ERR";
92        else if (level == TR_MSG_INF)
93            levelString = @"INF";
94        else if (level == TR_MSG_DBG)
95            levelString = @"DBG";
96        else
97            levelString = @"???";
98       
99        dateString = [[NSDate dateWithTimeIntervalSince1970: currentMessage->when]
100                            dateWithCalendarFormat: @"%Y-%m-%d %H:%M:%S" timeZone: nil];
101        messageString = [[[NSAttributedString alloc] initWithString: [NSString stringWithFormat: @"%@ %@ %s\n",
102                            dateString, levelString, currentMessage->message]] autorelease];
103       
104        [[fTextView textStorage] appendAttributedString: messageString];
105    }
106   
107    tr_freeMessageList(messages);
108   
109    [fTextView setFont: [NSFont fontWithName: @"Monaco" size: 10]]; //find a way to set this permanently
110   
111    if (shouldScroll)
112        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
113}
114
115- (void) changeLevel: (id) sender
116{
117    int selection = [fLevelButton indexOfSelectedItem], level;
118    if (selection == LEVEL_INFO)
119        level = TR_MSG_INF;
120    else if (selection == LEVEL_DEBUG)
121        level = TR_MSG_DBG;
122    else
123        level = TR_MSG_ERR;
124   
125    [self updateLog: nil];
126   
127    tr_setMessageLevel(level);
128    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
129}
130
131- (void) clearLog: (id) sender
132{
133    [fTextView setString: @""];
134}
135
136@end
Note: See TracBrowser for help on using the repository browser.