summaryrefslogtreecommitdiffstats
path: root/macosx/HBAutoNamer.m
blob: 2fced6239a3bfe5cad6484ff63c994a128dac190 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*  HBAutoNamer.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 "HBAutoNamer.h"

#import "HBJob.h"
#import "HBUtilities.h"

static void *HBAutoNamerPrefsContext = &HBAutoNamerPrefsContext;
static void *HBAutoNamerContext = &HBAutoNamerContext;

@interface HBAutoNamer ()

@property (nonatomic) HBJob *job;
@property (nonatomic) NSArray<NSString *> *format;

@end

@implementation HBAutoNamer

- (instancetype)initWithJob:(HBJob *)job
{
    self = [super init];
    if (self)
    {
        _job = job;
        _format = [[NSUserDefaults standardUserDefaults] objectForKey:@"HBAutoNamingFormat"];
        [self addFormatObservers];
        [self addJobObservers];
        [self addPrefsObservers];
    }
    return self;
}

- (void)dealloc
{
    [self removeFormatObservers];
    [self removeJobObservers];
    [self removePrefsObservers];
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == HBAutoNamerContext)
    {
        [self updateFileName];
    }
    else if (context == HBAutoNamerPrefsContext)
    {
        [self removeJobObservers];
        self.format = [[NSUserDefaults standardUserDefaults] objectForKey:@"HBAutoNamingFormat"];
        [self addJobObservers];
        [self updateFileName];
        [self updateFileExtension];
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)addPrefsObservers
{
    NSUserDefaultsController *ud = [NSUserDefaultsController sharedUserDefaultsController];
    [ud addObserver:self forKeyPath:@"values.HBAutoNamingFormat" options:0 context:HBAutoNamerPrefsContext];
    [ud addObserver:self forKeyPath:@"values.HBAutoNamingRemoveUnderscore" options:0 context:HBAutoNamerPrefsContext];
    [ud addObserver:self forKeyPath:@"values.HBAutoNamingRemovePunctuation" options:0 context:HBAutoNamerPrefsContext];
    [ud addObserver:self forKeyPath:@"values.HBAutoNamingTitleCase" options:0 context:HBAutoNamerPrefsContext];
}

- (void)removePrefsObservers
{
    NSUserDefaultsController *ud = [NSUserDefaultsController sharedUserDefaultsController];
    [ud removeObserver:self forKeyPath:@"values.HBAutoNamingFormat" context:HBAutoNamerPrefsContext];
    [ud removeObserver:self forKeyPath:@"values.HBAutoNamingRemoveUnderscore" context:HBAutoNamerPrefsContext];
    [ud removeObserver:self forKeyPath:@"values.HBAutoNamingRemovePunctuation" context:HBAutoNamerPrefsContext];
    [ud removeObserver:self forKeyPath:@"values.HBAutoNamingTitleCase" context:HBAutoNamerPrefsContext];

}

#pragma mark - File extension

- (void)addFormatObservers
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFileExtension:) name:HBAudioEncoderChangedNotification object:self.job.audio];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFileExtension:) name:HBChaptersChangedNotification object:self.job];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFileExtension:) name:HBRangeChangedNotification object:self.job.range];
}

- (void)removeFormatObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:HBAudioEncoderChangedNotification object:self.job.audio];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:HBChaptersChangedNotification object:self.job];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:HBRangeChangedNotification object:_job.range];
}

- (void)updateFileExtension:(NSNotification *)notification
{
    NSUndoManager *undo = self.job.undo;

    if (self.job && !(undo.isUndoing || undo.isRedoing))
    {
        NSString *extension = [HBUtilities automaticExtForJob:self.job];
        if (![extension isEqualTo:self.job.outputFileName.pathExtension])
        {
            self.job.outputFileName = [[self.job.outputFileName stringByDeletingPathExtension] stringByAppendingPathExtension:extension];
        }
    }
}

- (void)updateFileExtension
{
    [self updateFileExtension:nil];
}

#pragma mark - File name

- (void)addJobObservers
{
    for (NSString *formatKey in self.format)
    {
        if ([formatKey isEqualToString:@"{Chapters}"])
        {
            [self.job addObserver:self forKeyPath:@"range.chapterStart" options:0 context:HBAutoNamerContext];
            [self.job addObserver:self forKeyPath:@"range.chapterStop" options:0 context:HBAutoNamerContext];
        }
        else if ([formatKey isEqualToString:@"{Quality/Bitrate}"])
        {
            [self.job addObserver:self forKeyPath:@"video.qualityType" options:0 context:HBAutoNamerContext];
            [self.job addObserver:self forKeyPath:@"video.avgBitrate" options:0 context:HBAutoNamerContext];
            [self.job addObserver:self forKeyPath:@"video.quality" options:0 context:HBAutoNamerContext];
        }
    }
}

- (void)removeJobObservers
{
    for (NSString *formatKey in self.format)
    {
        if ([formatKey isEqualToString:@"{Chapters}"])
        {
            [self.job removeObserver:self forKeyPath:@"range.chapterStart" context:HBAutoNamerContext];
            [self.job removeObserver:self forKeyPath:@"range.chapterStop" context:HBAutoNamerContext];
        }
        else if ([formatKey isEqualToString:@"{Quality/Bitrate}"])
        {
            [self.job removeObserver:self forKeyPath:@"video.qualityType" context:HBAutoNamerContext];
            [self.job removeObserver:self forKeyPath:@"video.avgBitrate" context:HBAutoNamerContext];
            [self.job removeObserver:self forKeyPath:@"video.quality" context:HBAutoNamerContext];
        }
    }
}

- (void)updateFileName
{
    NSUndoManager *undo = self.job.undo;

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultAutoNaming"] && self.job && !(undo.isUndoing || undo.isRedoing))
    {
        // Generate a new file name
        NSString *fileName = [HBUtilities automaticNameForJob:self.job];

        // Swap the old one with the new one
        self.job.outputFileName = [NSString stringWithFormat:@"%@.%@", fileName, self.job.outputFileName.pathExtension];
    }
}

@end