source: trunk/macosx/MessageWindowController.m @ 822

Last change on this file since 822 was 822, checked in by livings124, 16 years ago

simpler date format

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