summaryrefslogtreecommitdiffstats
path: root/macosx/HBQueueInfoViewController.m
blob: 83659c2c53195fd6c39990a4123882750879a3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*  HBQueueDetailsViewController.m $

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBQueueInfoViewController.h"
#import "HBQueue.h"

@interface HBQueueInfoViewController ()

@property (nonatomic, weak) IBOutlet NSView *statisticsHeader;
@property (nonatomic, weak) IBOutlet NSTextField *statisticsLabel;
@property (nonatomic, weak) IBOutlet NSTextField *summaryLabel;
@property (nonatomic, weak) IBOutlet NSScrollView *scrollView;
@property (nonatomic, weak) IBOutlet NSBox *divider;

@property (nonatomic, weak) id<HBQueueDetailsViewControllerDelegate> delegate;

@property (nonatomic) BOOL canReset;

@end

@implementation HBQueueInfoViewController

- (instancetype)initWithDelegate:(id<HBQueueDetailsViewControllerDelegate>)delegate
{
    self = [super init];
    if (self)
    {
        _delegate = delegate;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateLabels];
    [self setUpObservers];
}

- (void)setUpObservers
{
    // It would be easier to just KVO the item state property,
    // But we can't because the item is a NSProxy.
    NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter;

    [center addObserverForName:HBQueueDidStartItemNotification
                                              object:nil
                                               queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
                           {
                               HBQueueItem *startedItem = note.userInfo[HBQueueItemNotificationItemKey];

                               if (startedItem == self.item)
                               {
                                   [self updateLabels];
                                   [self updateReset];
                               }
                           }];

    [center addObserverForName:HBQueueDidCompleteItemNotification
                        object:nil
                         queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
     {
         HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey];

         if (completedItem == self.item)
         {
             [self updateLabels];
             [self updateReset];
         }
     }];

    [center addObserverForName:HBQueueDidChangeItemNotification
                        object:nil
                         queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
     {
         [self updateLabels];
         [self updateReset];
     }];

}

- (void)updateReset
{
    self.canReset = self.item && (self.item.state != HBQueueItemStateWorking && self.item.state != HBQueueItemStateReady);
}

- (void)updateLabels
{
    if (self.item)
    {
        self.statisticsLabel.hidden = self.item.startedDate == nil;
        self.statisticsHeader.hidden = self.item.startedDate == nil;
        self.summaryLabel.hidden = NO;

        self.statisticsLabel.attributedStringValue = self.item.attributedStatistics;
        self.summaryLabel.attributedStringValue = self.item.attributedDescription;

        [self.scrollView flashScrollers];
    }
    else
    {
        self.statisticsHeader.hidden = YES;
        self.statisticsLabel.hidden = YES;
        self.summaryLabel.hidden = YES;
    }
}

- (void)viewDidLayout
{
    [super viewDidLayout];
    self.divider.hidden = self.scrollView.frame.size.height > self.scrollView.contentView.documentView.frame.size.height;
}

- (void)setItem:(HBQueueItem *)item
{
    _item = item;
    [self updateLabels];
    [self updateReset];
}

- (IBAction)editItem:(id)sender
{
    [self.delegate detailsViewEditItem:self.item];
}

- (IBAction)resetItem:(id)sender
{
    [self.delegate detailsViewResetItem:self.item];
}

@end