source: trunk/macosx/MessageWindowController.m @ 1125

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

warning if move folder doesn't exist, and a few releases

  • Property svn:keywords set to Date Rev Author Id
File size: 7.4 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 1094 2006-11-16 22:03:51Z 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.6
33#define MAX_LINES       2500
34
35@interface MessageWindowController (Private)
36
37- (void) setDebugWarningHidden: (BOOL) hide;
38
39@end
40
41@implementation MessageWindowController
42
43- (id) initWithWindowNibName: (NSString *) name
44{
45    if ((self = [super initWithWindowNibName: name]))
46    {
47        NSMutableParagraphStyle * paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
48        [paragraph setHeadIndent: 20.0];
49       
50        fAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
51                        [NSFont fontWithName: @"Monaco" size: 10], NSFontAttributeName,
52                        paragraph, NSParagraphStyleAttributeName, nil];
53        [paragraph release];
54       
55        fLines = 0;
56       
57        fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
58                    selector: @selector(updateLog:) userInfo: nil repeats: YES];
59       
60        [[self window] update]; //make sure nib is loaded right away
61    }
62    return self;
63}
64
65- (void) dealloc
66{
67    [fTimer invalidate];
68    [fAttributes release];
69   
70    [super dealloc];
71}
72
73- (void) awakeFromNib
74{
75    [[self window] center];
76   
77    int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
78    if (level == TR_MSG_ERR)
79        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
80    else if (level == TR_MSG_INF)
81        [fLevelButton selectItemAtIndex: LEVEL_INFO];
82    else if (level == TR_MSG_DBG)
83        [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
84    else
85    {
86        level = TR_MSG_ERR;
87        [fLevelButton selectItemAtIndex: LEVEL_ERROR];
88        [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
89    }
90   
91    [self setDebugWarningHidden: level != TR_MSG_DBG];
92   
93    tr_setMessageLevel(level);
94    tr_setMessageQueuing(1);
95}
96
97- (void) updateLog: (NSTimer *) timer
98{
99    tr_msg_list_t * messages, * currentMessage;
100    if ((messages = tr_getQueuedMessages()) == NULL)
101        return;
102   
103    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
104    NSScroller * scroller = [fScrollView verticalScroller];
105    BOOL shouldScroll = [scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0;
106   
107    NSString * levelString, * dateString, * messageString;
108    for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
109    {
110        int level = currentMessage->level;
111        if (level == TR_MSG_ERR)
112            levelString = @"ERR";
113        else if (level == TR_MSG_INF)
114            levelString = @"INF";
115        else if (level == TR_MSG_DBG)
116            levelString = @"DBG";
117        else
118            levelString = @"???";
119       
120        dateString = [[NSDate dateWithTimeIntervalSince1970: currentMessage->when]
121                            descriptionWithCalendarFormat: @"%1m/%d %H:%M:%S" timeZone: nil locale: nil];
122        messageString = [NSString stringWithFormat: @"%s%@ %@ %s",
123                            fLines > 0 ? "\n" : "", dateString, levelString, currentMessage->message];
124       
125        //remove the first line if at max number of lines
126        if (fLines == MAX_LINES)
127        {
128            unsigned int loc = [[fTextView string] rangeOfString: @"\n"].location;
129            if (loc != NSNotFound)
130                [[fTextView textStorage] deleteCharactersInRange: NSMakeRange(0, loc + 1)];
131        }
132        else
133            fLines++;
134       
135        [[fTextView textStorage] appendAttributedString: [[[NSAttributedString alloc] initWithString:
136                                        messageString attributes: fAttributes] autorelease]];
137    }
138   
139    tr_freeMessageList(messages);
140   
141    if (shouldScroll)
142        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
143}
144
145- (void) changeLevel: (id) sender
146{
147    [self updateLog: nil];
148   
149    int selection = [fLevelButton indexOfSelectedItem], level;
150    if (selection == LEVEL_INFO)
151        level = TR_MSG_INF;
152    else if (selection == LEVEL_DEBUG)
153        level = TR_MSG_DBG;
154    else
155        level = TR_MSG_ERR;
156   
157    [self setDebugWarningHidden: level != TR_MSG_DBG];
158   
159    tr_setMessageLevel(level);
160    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
161}
162
163- (void) clearLog: (id) sender
164{
165    [fTextView setString: @""];
166    fLines = 0;
167}
168
169- (void) writeToFile: (id) sender
170{
171    NSString * string = [[fTextView string] retain];
172   
173    NSSavePanel * panel = [NSSavePanel savePanel];
174    [panel setRequiredFileType: @"txt"];
175    [panel setCanSelectHiddenExtension: YES];
176   
177    [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name")
178            modalForWindow: [self window] modalDelegate: self
179            didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: string];
180}
181
182- (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string
183{
184    if (code == NSOKButton)
185    {
186        if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil])
187        {
188            NSAlert * alert = [[NSAlert alloc] init];
189            [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
190            [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Log Could Not Be Saved",
191                                    "Save log alert panel -> title")]];
192            [alert setInformativeText: [NSString stringWithFormat:
193                    NSLocalizedString(@"There was a problem creating the file \"%@\".",
194                                        "Save log alert panel -> message"), [[panel filename] lastPathComponent]]];
195            [alert setAlertStyle: NSWarningAlertStyle];
196           
197            [alert runModal];
198            [alert release];
199        }
200    }
201   
202    [string release];
203}
204
205- (void) setDebugWarningHidden: (BOOL) hide
206{
207    [fDebugWarningField setHidden: hide];
208    [fDebugWarningIcon setHidden: hide];
209}
210
211@end
Note: See TracBrowser for help on using the repository browser.