source: trunk/macosx/MessageWindowController.m

Last change on this file was 14662, checked in by mikedld, 6 years ago

#6042: Remove dead code now that we build for OS X 10.7+ (patch by mattrajca)

In addition to the patch,

  • remove unused global actions menu (popover is used instead)
  • remove *Lion class macros, use NSPopover and NSDataDetector directly
  • Property svn:keywords set to Date Rev Author Id
File size: 20.3 KB
Line 
1/******************************************************************************
2 * $Id: MessageWindowController.m 14662 2016-01-06 11:05:37Z mikedld $
3 *
4 * Copyright (c) 2006-2012 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 "Controller.h"
27#import "NSApplicationAdditions.h"
28#import "NSMutableArrayAdditions.h"
29#import "NSStringAdditions.h"
30#import <log.h>
31#import <transmission.h>
32
33#define LEVEL_ERROR 0
34#define LEVEL_INFO  1
35#define LEVEL_DEBUG 2
36
37#define UPDATE_SECONDS  0.75
38
39@interface MessageWindowController (Private)
40
41- (void) resizeColumn;
42- (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message;
43- (void) updateListForFilter;
44- (NSString *) stringForMessage: (NSDictionary *) message;
45
46@end
47
48@implementation MessageWindowController
49
50- (id) init
51{
52    return [super initWithWindowNibName: @"MessageWindow"];
53}
54
55- (void) awakeFromNib
56{
57    NSWindow * window = [self window];
58    [window setFrameAutosaveName: @"MessageWindowFrame"];
59    [window setFrameUsingName: @"MessageWindowFrame"];
60    [window setRestorationClass: [self class]];
61   
62    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(resizeColumn)
63        name: NSTableViewColumnDidResizeNotification object: fMessageTable];
64   
65    [window setContentBorderThickness: NSMinY([[fMessageTable enclosingScrollView] frame]) forEdge: NSMinYEdge];
66   
67    [[self window] setTitle: NSLocalizedString(@"Message Log", "Message window -> title")];
68   
69    //set images and text for popup button items
70    [[fLevelButton itemAtIndex: LEVEL_ERROR] setTitle: NSLocalizedString(@"Error", "Message window -> level string")];
71    [[fLevelButton itemAtIndex: LEVEL_INFO] setTitle: NSLocalizedString(@"Info", "Message window -> level string")];
72    [[fLevelButton itemAtIndex: LEVEL_DEBUG] setTitle: NSLocalizedString(@"Debug", "Message window -> level string")];
73   
74    const CGFloat levelButtonOldWidth = NSWidth([fLevelButton frame]);
75    [fLevelButton sizeToFit];
76   
77    //set table column text
78    [[[fMessageTable tableColumnWithIdentifier: @"Date"] headerCell] setTitle: NSLocalizedString(@"Date",
79        "Message window -> table column")];
80    [[[fMessageTable tableColumnWithIdentifier: @"Name"] headerCell] setTitle: NSLocalizedString(@"Process",
81        "Message window -> table column")];
82    [[[fMessageTable tableColumnWithIdentifier: @"Message"] headerCell] setTitle: NSLocalizedString(@"Message",
83        "Message window -> table column")];
84   
85    //set and size buttons
86    [fSaveButton setTitle: [NSLocalizedString(@"Save", "Message window -> save button") stringByAppendingEllipsis]];
87    [fSaveButton sizeToFit];
88   
89    NSRect saveButtonFrame = [fSaveButton frame];
90    saveButtonFrame.size.width += 10.0;
91    saveButtonFrame.origin.x += NSWidth([fLevelButton frame]) - levelButtonOldWidth;
92    [fSaveButton setFrame: saveButtonFrame];
93   
94    const CGFloat oldClearButtonWidth = [fClearButton frame].size.width;
95   
96    [fClearButton setTitle: NSLocalizedString(@"Clear", "Message window -> save button")];
97    [fClearButton sizeToFit];
98   
99    NSRect clearButtonFrame = [fClearButton frame];
100    clearButtonFrame.size.width = MAX(clearButtonFrame.size.width + 10.0, saveButtonFrame.size.width);
101    clearButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
102    [fClearButton setFrame: clearButtonFrame];
103   
104    [[fFilterField cell] setPlaceholderString: NSLocalizedString(@"Filter", "Message window -> filter field")];
105    NSRect filterButtonFrame = [fFilterField frame];
106    filterButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
107    [fFilterField setFrame: filterButtonFrame];
108   
109    fAttributes = [[[[[fMessageTable tableColumnWithIdentifier: @"Message"] dataCell] attributedStringValue]
110                    attributesAtIndex: 0 effectiveRange: NULL] retain];
111   
112    //select proper level in popup button
113    switch ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"])
114    {
115        case TR_LOG_ERROR:
116            [fLevelButton selectItemAtIndex: LEVEL_ERROR];
117            break;
118        case TR_LOG_INFO:
119            [fLevelButton selectItemAtIndex: LEVEL_INFO];
120            break;
121        case TR_LOG_DEBUG:
122            [fLevelButton selectItemAtIndex: LEVEL_DEBUG];
123            break;
124        default: //safety
125            [[NSUserDefaults standardUserDefaults] setInteger: TR_LOG_ERROR forKey: @"MessageLevel"];
126            [fLevelButton selectItemAtIndex: LEVEL_ERROR];
127    }
128   
129    fMessages = [[NSMutableArray alloc] init];
130    fDisplayedMessages = [[NSMutableArray alloc] init];
131   
132    fLock = [[NSLock alloc] init];
133}
134
135- (void) dealloc
136{
137    [[NSNotificationCenter defaultCenter] removeObserver: self];
138   
139    [fTimer invalidate];
140    [fTimer release];
141    [fLock release];
142   
143    [fMessages release];
144    [fDisplayedMessages release];
145   
146    [fAttributes release];
147   
148    [super dealloc];
149}
150
151- (void) windowDidBecomeKey: (NSNotification *) notification
152{
153    if (!fTimer)
154    {
155        fTimer = [[NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self selector: @selector(updateLog:) userInfo: nil repeats: YES] retain];
156        [self updateLog: nil];
157    }
158}
159
160- (void) windowWillClose: (id)sender
161{
162    [fTimer invalidate];
163    [fTimer release];
164    fTimer = nil;
165}
166
167+ (void) restoreWindowWithIdentifier: (NSString *) identifier state: (NSCoder *) state completionHandler: (void (^)(NSWindow *, NSError *)) completionHandler
168{
169    NSAssert1([identifier isEqualToString: @"MessageWindow"], @"Trying to restore unexpected identifier %@", identifier);
170   
171    NSWindow * window = [[(Controller *)[NSApp delegate] messageWindowController] window];
172    completionHandler(window, nil);
173}
174
175- (void) window: (NSWindow *) window didDecodeRestorableState: (NSCoder *) coder
176{
177    [fTimer invalidate];
178    [fTimer release];
179    fTimer = [[NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self selector: @selector(updateLog:) userInfo: nil repeats: YES] retain];
180    [self updateLog: nil];
181}
182
183- (void) updateLog: (NSTimer *) timer
184{
185    tr_log_message * messages;
186    if ((messages = tr_logGetQueue()) == NULL)
187        return;
188   
189    [fLock lock];
190   
191    static NSUInteger currentIndex = 0;
192   
193    NSScroller * scroller = [[fMessageTable enclosingScrollView] verticalScroller];
194    const BOOL shouldScroll = currentIndex == 0 || [scroller floatValue] == 1.0 || [scroller isHidden]
195                                || [scroller knobProportion] == 1.0;
196   
197    const NSInteger maxLevel = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
198    NSString * filterString = [fFilterField stringValue];
199   
200    BOOL changed = NO;
201   
202    for (tr_log_message * currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
203    {
204        NSString * name = currentMessage->name != NULL ? [NSString stringWithUTF8String: currentMessage->name]
205                            : [[NSProcessInfo processInfo] processName];
206       
207        NSString * file = [[[NSString stringWithUTF8String: currentMessage->file] lastPathComponent] stringByAppendingFormat: @":%d",
208                            currentMessage->line];
209       
210        NSDictionary * message  = [NSDictionary dictionaryWithObjectsAndKeys:
211                                    [NSString stringWithUTF8String: currentMessage->message], @"Message",
212                                    [NSDate dateWithTimeIntervalSince1970: currentMessage->when], @"Date",
213                                    [NSNumber numberWithUnsignedInteger: currentIndex++], @"Index", //more accurate when sorting by date
214                                    [NSNumber numberWithInteger: currentMessage->level], @"Level",
215                                    name, @"Name",
216                                    file, @"File", nil];
217       
218        [fMessages addObject: message];
219       
220        if (currentMessage->level <= maxLevel && [self shouldIncludeMessageForFilter: filterString message: message])
221        {
222            [fDisplayedMessages addObject: message];
223            changed = YES;
224        }
225    }
226   
227    if ([fMessages count] > TR_LOG_MAX_QUEUE_LENGTH)
228    {
229        const NSUInteger oldCount = [fDisplayedMessages count];
230       
231        NSIndexSet * removeIndexes = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fMessages count]-TR_LOG_MAX_QUEUE_LENGTH)];
232        NSArray * itemsToRemove = [fMessages objectsAtIndexes: removeIndexes];
233       
234        [fMessages removeObjectsAtIndexes: removeIndexes];
235        [fDisplayedMessages removeObjectsInArray: itemsToRemove];
236       
237        changed |= oldCount > [fDisplayedMessages count];
238    }
239   
240    if (changed)
241    {
242        [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
243       
244        [fMessageTable reloadData];
245        if (shouldScroll)
246            [fMessageTable scrollRowToVisible: [fMessageTable numberOfRows]-1];
247    }
248   
249    [fLock unlock];
250   
251    tr_logFreeQueue (messages);
252}
253
254- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableView
255{
256    return [fDisplayedMessages count];
257}
258
259- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) column row: (NSInteger) row
260{
261    NSString * ident = [column identifier];
262    NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
263
264    if ([ident isEqualToString: @"Date"])
265        return [message objectForKey: @"Date"];
266    else if ([ident isEqualToString: @"Level"])
267    {
268        const NSInteger level = [[message objectForKey: @"Level"] integerValue];
269        switch (level)
270        {
271            case TR_LOG_ERROR:
272                return [NSImage imageNamed: @"RedDot"];
273            case TR_LOG_INFO:
274                return [NSImage imageNamed: @"YellowDot"];
275            case TR_LOG_DEBUG:
276                return [NSImage imageNamed: @"PurpleDot"];
277            default:
278                NSAssert1(NO, @"Unknown message log level: %ld", level);
279                return nil;
280        }
281    }
282    else if ([ident isEqualToString: @"Name"])
283        return [message objectForKey: @"Name"];
284    else
285        return [message objectForKey: @"Message"];
286}
287
288#warning don't cut off end
289- (CGFloat) tableView: (NSTableView *) tableView heightOfRow: (NSInteger) row
290{
291    NSString * message = [[fDisplayedMessages objectAtIndex: row] objectForKey: @"Message"];
292   
293    NSTableColumn * column = [tableView tableColumnWithIdentifier: @"Message"];
294    const CGFloat count = floorf([message sizeWithAttributes: fAttributes].width / [column width]);
295   
296    return [tableView rowHeight] * (count + 1.0);
297}
298
299- (void) tableView: (NSTableView *) tableView sortDescriptorsDidChange: (NSArray *) oldDescriptors
300{
301    [fDisplayedMessages sortUsingDescriptors: [fMessageTable sortDescriptors]];
302    [fMessageTable reloadData];
303}
304
305- (NSString *) tableView: (NSTableView *) tableView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
306                tableColumn: (NSTableColumn *) column row: (NSInteger) row mouseLocation: (NSPoint) mouseLocation
307{
308    NSDictionary * message = [fDisplayedMessages objectAtIndex: row];
309    return [message objectForKey: @"File"];
310}
311
312- (void) copy: (id) sender
313{
314    NSIndexSet * indexes = [fMessageTable selectedRowIndexes];
315    NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [indexes count]];
316   
317    for (NSDictionary * message in [fDisplayedMessages objectsAtIndexes: indexes])
318        [messageStrings addObject: [self stringForMessage: message]];
319   
320    NSString * messageString = [messageStrings componentsJoinedByString: @"\n"];
321   
322    NSPasteboard * pb = [NSPasteboard generalPasteboard];
323    [pb clearContents];
324    [pb writeObjects: [NSArray arrayWithObject: messageString]];
325}
326
327- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
328{
329    SEL action = [menuItem action];
330   
331    if (action == @selector(copy:))
332        return [fMessageTable numberOfSelectedRows] > 0;
333   
334    return YES;
335}
336
337- (void) changeLevel: (id) sender
338{
339    NSInteger level;
340    switch ([fLevelButton indexOfSelectedItem])
341    {
342        case LEVEL_ERROR:
343            level = TR_LOG_ERROR;
344            break;
345        case LEVEL_INFO:
346            level = TR_LOG_INFO;
347            break;
348        case LEVEL_DEBUG:
349            level = TR_LOG_DEBUG;
350            break;
351        default:
352            NSAssert1(NO, @"Unknown message log level: %ld", [fLevelButton indexOfSelectedItem]);
353    }
354   
355    if ([[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"] == level)
356        return;
357   
358    [[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
359   
360    [fLock lock];
361   
362    [self updateListForFilter];
363   
364    [fLock unlock];
365}
366
367- (void) changeFilter: (id) sender
368{
369    [fLock lock];
370   
371    [self updateListForFilter];
372   
373    [fLock unlock];
374}
375
376- (void) clearLog: (id) sender
377{
378    [fLock lock];
379   
380    [fMessages removeAllObjects];
381
382    [fMessageTable beginUpdates];
383    [fMessageTable removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fDisplayedMessages count])] withAnimation: NSTableViewAnimationSlideLeft];
384
385    [fDisplayedMessages removeAllObjects];
386
387    [fMessageTable endUpdates];
388   
389    [fLock unlock];
390}
391
392- (void) writeToFile: (id) sender
393{
394    NSSavePanel * panel = [NSSavePanel savePanel];
395    [panel setAllowedFileTypes: [NSArray arrayWithObject: @"txt"]];
396    [panel setCanSelectHiddenExtension: YES];
397   
398    [panel setNameFieldStringValue: NSLocalizedString(@"untitled", "Save log panel -> default file name")];
399   
400    [panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result) {
401        if (result == NSFileHandlingPanelOKButton)
402        {
403            //make the array sorted by date
404            NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey: @"Index" ascending: YES];
405            NSArray * descriptors = [[NSArray alloc] initWithObjects: descriptor, nil];
406            NSArray * sortedMessages = [fDisplayedMessages sortedArrayUsingDescriptors: descriptors];
407            [descriptors release];
408           
409            //create the text to output
410            NSMutableArray * messageStrings = [NSMutableArray arrayWithCapacity: [sortedMessages count]];
411            for (NSDictionary * message in sortedMessages)
412                [messageStrings addObject: [self stringForMessage: message]];
413           
414            NSString * fileString = [messageStrings componentsJoinedByString: @"\n"];
415           
416            if (![fileString writeToFile: [[panel URL] path] atomically: YES encoding: NSUTF8StringEncoding error: nil])
417            {
418                NSAlert * alert = [[NSAlert alloc] init];
419                [alert addButtonWithTitle: NSLocalizedString(@"OK", "Save log alert panel -> button")];
420                [alert setMessageText: NSLocalizedString(@"Log Could Not Be Saved", "Save log alert panel -> title")];
421                [alert setInformativeText: [NSString stringWithFormat:
422                                            NSLocalizedString(@"There was a problem creating the file \"%@\".",
423                                                              "Save log alert panel -> message"), [[[panel URL] path] lastPathComponent]]];
424                [alert setAlertStyle: NSWarningAlertStyle];
425               
426                [alert runModal];
427                [alert release];
428            }
429        }
430    }];
431}
432
433@end
434
435@implementation MessageWindowController (Private)
436
437- (void) resizeColumn
438{
439    [fMessageTable noteHeightOfRowsWithIndexesChanged: [NSIndexSet indexSetWithIndexesInRange:
440                    NSMakeRange(0, [fMessageTable numberOfRows])]];
441}
442
443- (BOOL) shouldIncludeMessageForFilter: (NSString *) filterString message: (NSDictionary *) message
444{
445    if ([filterString isEqualToString: @""])
446        return YES;
447   
448    const NSStringCompareOptions searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
449    return [[message objectForKey: @"Name"] rangeOfString: filterString options: searchOptions].location != NSNotFound
450            || [[message objectForKey: @"Message"] rangeOfString: filterString options: searchOptions].location != NSNotFound;
451}
452
453- (void) updateListForFilter
454{
455    const NSInteger level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
456    NSString * filterString = [fFilterField stringValue];
457   
458    NSIndexSet * indexes = [fMessages indexesOfObjectsWithOptions: NSEnumerationConcurrent passingTest: ^BOOL(id message, NSUInteger idx, BOOL * stop) {
459        return [[(NSDictionary *)message objectForKey: @"Level"] integerValue] <= level && [self shouldIncludeMessageForFilter: filterString message: message];
460    }];
461   
462    NSArray * tempMessages = [[fMessages objectsAtIndexes: indexes] sortedArrayUsingDescriptors: [fMessageTable sortDescriptors]];
463
464    [fMessageTable beginUpdates];
465   
466    //figure out which rows were added/moved
467    NSUInteger currentIndex = 0, totalCount = 0;
468    NSMutableArray * itemsToAdd = [NSMutableArray array];
469    NSMutableIndexSet * itemsToAddIndexes = [NSMutableIndexSet indexSet];
470   
471    for (NSDictionary * message in tempMessages)
472    {
473        const NSUInteger previousIndex = [fDisplayedMessages indexOfObject: message inRange: NSMakeRange(currentIndex, [fDisplayedMessages count]-currentIndex)];
474        if (previousIndex == NSNotFound)
475        {
476            [itemsToAdd addObject: message];
477            [itemsToAddIndexes addIndex: totalCount];
478        }
479        else
480        {
481            if (previousIndex != currentIndex)
482            {
483                [fDisplayedMessages moveObjectAtIndex: previousIndex toIndex: currentIndex];
484                [fMessageTable moveRowAtIndex: previousIndex toIndex: currentIndex];
485            }
486            ++currentIndex;
487        }
488       
489        ++totalCount;
490    }
491   
492    //remove trailing items - those are the unused
493    if (currentIndex < [fDisplayedMessages count])
494    {
495        const NSRange removeRange = NSMakeRange(currentIndex, [fDisplayedMessages count]-currentIndex);
496        [fDisplayedMessages removeObjectsInRange: removeRange];
497        [fMessageTable removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: removeRange] withAnimation: NSTableViewAnimationSlideDown];
498    }
499   
500    //add new items
501    [fDisplayedMessages insertObjects: itemsToAdd atIndexes: itemsToAddIndexes];
502    [fMessageTable insertRowsAtIndexes: itemsToAddIndexes withAnimation: NSTableViewAnimationSlideUp];
503
504    [fMessageTable endUpdates];
505   
506    NSAssert2([fDisplayedMessages isEqualToArray: tempMessages], @"Inconsistency between message arrays! %@ %@", fDisplayedMessages, tempMessages);
507}
508
509- (NSString *) stringForMessage: (NSDictionary *) message
510{
511    NSString * levelString;
512    const NSInteger level = [[message objectForKey: @"Level"] integerValue];
513    switch (level)
514    {
515        case TR_LOG_ERROR:
516            levelString = NSLocalizedString(@"Error", "Message window -> level");
517            break;
518        case TR_LOG_INFO:
519            levelString = NSLocalizedString(@"Info", "Message window -> level");
520            break;
521        case TR_LOG_DEBUG:
522            levelString = NSLocalizedString(@"Debug", "Message window -> level");
523            break;
524        default:
525            NSAssert1(NO, @"Unknown message log level: %ld", level);
526    }
527   
528    return [NSString stringWithFormat: @"%@ %@ [%@] %@: %@", [message objectForKey: @"Date"],
529            [message objectForKey: @"File"], levelString,
530            [message objectForKey: @"Name"], [message objectForKey: @"Message"], nil];
531}
532
533@end
Note: See TracBrowser for help on using the repository browser.