1 | /****************************************************************************** |
---|
2 | * $Id: MessageWindowController.m 3035 2007-09-10 19:28:15Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2007 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_MESSAGES 2500 |
---|
34 | |
---|
35 | @interface MessageWindowController (Private) |
---|
36 | |
---|
37 | - (NSString *) stringForMessage: (NSDictionary *) message; |
---|
38 | - (void) setDebugWarningHidden: (BOOL) hide; |
---|
39 | |
---|
40 | @end |
---|
41 | |
---|
42 | @implementation MessageWindowController |
---|
43 | |
---|
44 | - (id) initWithWindowNibName: (NSString *) name |
---|
45 | { |
---|
46 | if ((self = [super initWithWindowNibName: name])) |
---|
47 | { |
---|
48 | fMessages = [[NSMutableArray alloc] init]; |
---|
49 | |
---|
50 | fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self |
---|
51 | selector: @selector(updateLog:) userInfo: nil repeats: YES]; |
---|
52 | |
---|
53 | int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"]; |
---|
54 | if (level == TR_MSG_ERR) |
---|
55 | [fLevelButton selectItemAtIndex: LEVEL_ERROR]; |
---|
56 | else if (level == TR_MSG_INF) |
---|
57 | [fLevelButton selectItemAtIndex: LEVEL_INFO]; |
---|
58 | else if (level == TR_MSG_DBG) |
---|
59 | [fLevelButton selectItemAtIndex: LEVEL_DEBUG]; |
---|
60 | else |
---|
61 | { |
---|
62 | level = TR_MSG_ERR; |
---|
63 | [fLevelButton selectItemAtIndex: LEVEL_ERROR]; |
---|
64 | [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"]; |
---|
65 | } |
---|
66 | |
---|
67 | tr_setMessageLevel(level); |
---|
68 | tr_setMessageQueuing(1); |
---|
69 | } |
---|
70 | return self; |
---|
71 | } |
---|
72 | |
---|
73 | - (void) dealloc |
---|
74 | { |
---|
75 | [fTimer invalidate]; |
---|
76 | [fMessages release]; |
---|
77 | |
---|
78 | [super dealloc]; |
---|
79 | } |
---|
80 | |
---|
81 | - (void) awakeFromNib |
---|
82 | { |
---|
83 | [[self window] center]; |
---|
84 | |
---|
85 | int level = tr_getMessageLevel(); |
---|
86 | if (level == TR_MSG_ERR) |
---|
87 | [fLevelButton selectItemAtIndex: LEVEL_ERROR]; |
---|
88 | else if (level == TR_MSG_INF) |
---|
89 | [fLevelButton selectItemAtIndex: LEVEL_INFO]; |
---|
90 | else if (level == TR_MSG_DBG) |
---|
91 | [fLevelButton selectItemAtIndex: LEVEL_DEBUG]; |
---|
92 | else |
---|
93 | { |
---|
94 | level = TR_MSG_ERR; |
---|
95 | [fLevelButton selectItemAtIndex: LEVEL_ERROR]; |
---|
96 | [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"]; |
---|
97 | } |
---|
98 | |
---|
99 | [self setDebugWarningHidden: level != TR_MSG_DBG]; |
---|
100 | } |
---|
101 | |
---|
102 | - (void) updateLog: (NSTimer *) timer |
---|
103 | { |
---|
104 | tr_msg_list_t * messages, * currentMessage; |
---|
105 | if ((messages = tr_getQueuedMessages()) == NULL) |
---|
106 | return; |
---|
107 | |
---|
108 | NSString * levelString; |
---|
109 | for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next) |
---|
110 | { |
---|
111 | int level = currentMessage->level; |
---|
112 | if (level == TR_MSG_ERR) |
---|
113 | levelString = @"Error"; |
---|
114 | else if (level == TR_MSG_INF) |
---|
115 | levelString = @"Info"; |
---|
116 | else if (level == TR_MSG_DBG) |
---|
117 | levelString = @"Debug"; |
---|
118 | else |
---|
119 | levelString = @"???"; |
---|
120 | |
---|
121 | //remove the first line if at max number of lines |
---|
122 | /*if (fLines == MAX_LINES) |
---|
123 | { |
---|
124 | unsigned int loc = [[fTextView string] rangeOfString: @"\n"].location; |
---|
125 | if (loc != NSNotFound) |
---|
126 | [[fTextView textStorage] deleteCharactersInRange: NSMakeRange(0, loc + 1)]; |
---|
127 | }*/ |
---|
128 | |
---|
129 | [fMessages addObject: [NSDictionary dictionaryWithObjectsAndKeys: |
---|
130 | [NSString stringWithUTF8String: currentMessage->message], @"Message", |
---|
131 | [NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date", |
---|
132 | levelString, @"Level", nil]]; |
---|
133 | } |
---|
134 | |
---|
135 | #warning still needed? |
---|
136 | int total = [fMessages count]; |
---|
137 | if (total > MAX_MESSAGES) |
---|
138 | [fMessages removeObjectsInRange: NSMakeRange(0, total-MAX_MESSAGES)]; |
---|
139 | |
---|
140 | [fMessageView reloadData]; |
---|
141 | |
---|
142 | tr_freeMessageList(messages); |
---|
143 | } |
---|
144 | |
---|
145 | - (int) numberOfRowsInTableView: (NSTableView *) tableView |
---|
146 | { |
---|
147 | return [fMessages count]; |
---|
148 | } |
---|
149 | |
---|
150 | - (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (int) row |
---|
151 | { |
---|
152 | NSString * ident = [column identifier]; |
---|
153 | NSDictionary * message = [fMessages objectAtIndex: row]; |
---|
154 | |
---|
155 | if ([ident isEqualToString: @"Date"]) |
---|
156 | return [message objectForKey: @"Date"]; |
---|
157 | else if ([ident isEqualToString: @"Level"]) |
---|
158 | return [message objectForKey: @"Level"]; |
---|
159 | else |
---|
160 | return [message objectForKey: @"Message"]; |
---|
161 | } |
---|
162 | |
---|
163 | - (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect |
---|
164 | tableColumn: (NSTableColumn *) column row: (int) row mouseLocation: (NSPoint) mouseLocation |
---|
165 | { |
---|
166 | return [self stringForMessage: [fMessages objectAtIndex: row]]; |
---|
167 | } |
---|
168 | |
---|
169 | - (void) changeLevel: (id) sender |
---|
170 | { |
---|
171 | [self updateLog: nil]; |
---|
172 | |
---|
173 | int selection = [fLevelButton indexOfSelectedItem], level; |
---|
174 | if (selection == LEVEL_INFO) |
---|
175 | level = TR_MSG_INF; |
---|
176 | else if (selection == LEVEL_DEBUG) |
---|
177 | level = TR_MSG_DBG; |
---|
178 | else |
---|
179 | level = TR_MSG_ERR; |
---|
180 | |
---|
181 | [self setDebugWarningHidden: level != TR_MSG_DBG]; |
---|
182 | |
---|
183 | tr_setMessageLevel(level); |
---|
184 | [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"]; |
---|
185 | } |
---|
186 | |
---|
187 | - (void) clearLog: (id) sender |
---|
188 | { |
---|
189 | [fMessages removeAllObjects]; |
---|
190 | [fMessageView reloadData]; |
---|
191 | } |
---|
192 | |
---|
193 | - (void) writeToFile: (id) sender |
---|
194 | { |
---|
195 | //create the text to output |
---|
196 | NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [fMessages count]]; |
---|
197 | |
---|
198 | NSEnumerator * enumerator = [fMessages objectEnumerator]; |
---|
199 | NSDictionary * message; |
---|
200 | while ((message = [enumerator nextObject])) |
---|
201 | [messageStrings addObject: [self stringForMessage: message]]; |
---|
202 | |
---|
203 | NSString * fileString = [[messageStrings componentsJoinedByString: @"\n"] retain]; |
---|
204 | |
---|
205 | NSSavePanel * panel = [NSSavePanel savePanel]; |
---|
206 | [panel setRequiredFileType: @"txt"]; |
---|
207 | [panel setCanSelectHiddenExtension: YES]; |
---|
208 | |
---|
209 | [panel beginSheetForDirectory: nil file: NSLocalizedString(@"untitled", "Save log panel -> default file name") |
---|
210 | modalForWindow: [self window] modalDelegate: self |
---|
211 | didEndSelector: @selector(writeToFileSheetClosed:returnCode:contextInfo:) contextInfo: fileString]; |
---|
212 | } |
---|
213 | |
---|
214 | - (void) writeToFileSheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSString *) string |
---|
215 | { |
---|
216 | if (code == NSOKButton) |
---|
217 | { |
---|
218 | if (![string writeToFile: [panel filename] atomically: YES encoding: NSUTF8StringEncoding error: nil]) |
---|
219 | { |
---|
220 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
221 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")]; |
---|
222 | [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Log Could Not Be Saved", |
---|
223 | "Save log alert panel -> title")]]; |
---|
224 | [alert setInformativeText: [NSString stringWithFormat: |
---|
225 | NSLocalizedString(@"There was a problem creating the file \"%@\".", |
---|
226 | "Save log alert panel -> message"), [[panel filename] lastPathComponent]]]; |
---|
227 | [alert setAlertStyle: NSWarningAlertStyle]; |
---|
228 | |
---|
229 | [alert runModal]; |
---|
230 | [alert release]; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | [string release]; |
---|
235 | } |
---|
236 | |
---|
237 | @end |
---|
238 | |
---|
239 | @implementation MessageWindowController (Private) |
---|
240 | |
---|
241 | - (NSString *) stringForMessage: (NSDictionary *) message |
---|
242 | { |
---|
243 | return [NSString stringWithFormat: @"%@ %@ %@", [message objectForKey: @"Date"], |
---|
244 | [message objectForKey: @"Level"], [message objectForKey: @"Message"]]; |
---|
245 | } |
---|
246 | |
---|
247 | - (void) setDebugWarningHidden: (BOOL) hide |
---|
248 | { |
---|
249 | [fDebugWarningField setHidden: hide]; |
---|
250 | [fDebugWarningIcon setHidden: hide]; |
---|
251 | } |
---|
252 | |
---|
253 | @end |
---|