summaryrefslogtreecommitdiffstats
path: root/macosx/HBQueueDockTileController.m
blob: 8e50633341fc4c58cf7dc96c6972b742b21fd657 (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
//
//  HBQueueDockTileController.m
//  HandBrake
//
//  Created by Damiano Galassi on 09/04/2020.
//  Copyright © 2020 HandBrake. All rights reserved.
//

#import "HBQueueDockTileController.h"

#import "HBDockTile.h"
#import "HBQueue.h"

#define dockTileUpdateFrequency 0.1f

@interface HBQueueDockTileController ()

@property (nonatomic, readonly) HBQueue *queue;
@property (nonatomic, readonly) HBDockTile *dockTile;
@property (nonatomic) double progress;

@property (nonatomic) id observerToken;

@end

@implementation HBQueueDockTileController

- (instancetype)initWithQueue:(HBQueue *)queue dockTile:(id)dockTile image:(NSImage *)image
{
    self = [super init];
    if (self)
    {
        _queue = queue;
        _dockTile = [[HBDockTile alloc] initWithDockTile:dockTile image:image];

        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidStartItemNotification
                                                        object:_queue queue:NSOperationQueue.mainQueue
                                                    usingBlock:^(NSNotification * _Nonnull note) { [self setUpObservers]; }];

        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteItemNotification
                                                        object:_queue queue:NSOperationQueue.mainQueue
                                                    usingBlock:^(NSNotification * _Nonnull note) { [self setUpObservers]; }];

        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidChangeStateNotification
                                                        object:_queue queue:NSOperationQueue.mainQueue
                                                    usingBlock:^(NSNotification * _Nonnull note) { [self setUpObservers]; }];

        [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteNotification
                                                        object:_queue queue:NSOperationQueue.mainQueue
                                                    usingBlock:^(NSNotification * _Nonnull note) { self.dockTile.stringValue = @""; }];
    }
    return self;
}

- (void)setUpObservers
{
    [self removeObservers];

    if (self->_queue.workingItemsCount > 1)
    {
        [self setUpForMultipleWorkers];
    }
    else if (self->_queue.workingItemsCount == 1)
    {
        [self setUpForSingleWorker];
    }
}

- (void)setUpForMultipleWorkers
{
    self.dockTile.stringValue = [NSString stringWithFormat:@"%lu - %lu", self.queue.workingItemsCount, self.queue.pendingItemsCount];
    self.progress = 0;
}

- (void)setUpForSingleWorker
{
    self.progress = 0;
    HBQueueJobItem *firstWorkingItem = nil;
    for (HBQueueJobItem *item in self.queue.items)
    {
        if (item.state == HBQueueItemStateWorking)
        {
            firstWorkingItem = item;
            break;
        }
    }

    if (firstWorkingItem)
    {
        HBQueueWorker *worker = [self.queue workerForItem:firstWorkingItem];

        if (worker)
        {

            self.observerToken = [NSNotificationCenter.defaultCenter addObserverForName:HBQueueWorkerProgressNotification
                                                                                 object:worker queue:NSOperationQueue.mainQueue
                                                                             usingBlock:^(NSNotification * _Nonnull note) {
                double progress = [note.userInfo[HBQueueWorkerProgressNotificationPercentKey] doubleValue];

                if (self.progress < 100.0 * progress)
                {
                    double hours = [note.userInfo[HBQueueWorkerProgressNotificationHoursKey] doubleValue];
                    double minutes = [note.userInfo[HBQueueWorkerProgressNotificationMinutesKey] doubleValue];
                    double seconds = [note.userInfo[HBQueueWorkerProgressNotificationSecondsKey] doubleValue];

                    [self.dockTile setProgress:progress hours:hours minutes:minutes seconds:seconds];
                    self.progress += dockTileUpdateFrequency;
                }
            }];
        }
    }
}

- (void)removeObservers
{
    if (self.observerToken)
    {
        [NSNotificationCenter.defaultCenter removeObserver:self.observerToken];
        self.observerToken = nil;
    }
}

@end