From a052152ebb5b1844ca76ca65b1fed359307e8ad4 Mon Sep 17 00:00:00 2001
From: ritsuka <damiog@gmail.com>
Date: Mon, 22 Dec 2014 07:55:39 +0000
Subject: [PATCH] MacGui: implement NSCoding protocol in HBJob.

git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6638 b64f7644-9d1e-0410-96f1-a4d463321fa5
---
 macosx/HBAudioDefaults.h                   |  2 +-
 macosx/HBAudioDefaults.m                   | 43 +++++++++++++++
 macosx/HBFilters.h                         |  2 +-
 macosx/HBFilters.m                         | 61 +++++++++++++++++++++
 macosx/HBJob.m                             | 53 +++++++++++++++++-
 macosx/HBPicture.h                         |  4 +-
 macosx/HBPicture.m                         | 54 +++++++++++++++++-
 macosx/HBSubtitlesDefaults.h               |  2 +-
 macosx/HBSubtitlesDefaults.m               | 29 ++++++++++
 macosx/HBVideo.h                           |  4 +-
 macosx/HBVideo.m                           | 64 ++++++++++++++++++++++
 macosx/HandBrake.xcodeproj/project.pbxproj |  2 +
 macosx/NSCodingMacro.h                     | 23 ++++++++
 13 files changed, 332 insertions(+), 11 deletions(-)
 create mode 100644 macosx/NSCodingMacro.h

diff --git a/macosx/HBAudioDefaults.h b/macosx/HBAudioDefaults.h
index aeb271770..a2e30a318 100644
--- a/macosx/HBAudioDefaults.h
+++ b/macosx/HBAudioDefaults.h
@@ -16,7 +16,7 @@ typedef NS_ENUM(NSUInteger, HBAudioTrackSelectionBehavior) {
  *  HBAudioSettings
  *  Stores the audio defaults settings.
  */
-@interface HBAudioDefaults : NSObject
+@interface HBAudioDefaults : NSObject <NSCoding>
 
 @property (nonatomic, readwrite) HBAudioTrackSelectionBehavior trackSelectionBehavior;
 @property (nonatomic, readwrite, retain) NSMutableArray *trackSelectionLanguages;
diff --git a/macosx/HBAudioDefaults.m b/macosx/HBAudioDefaults.m
index 58b355573..3628b5564 100644
--- a/macosx/HBAudioDefaults.m
+++ b/macosx/HBAudioDefaults.m
@@ -6,6 +6,7 @@
 
 #import "HBAudioDefaults.h"
 #import "HBAudioTrackPreset.h"
+#import "NSCodingMacro.h"
 #import "hb.h"
 #import "lang.h"
 
@@ -240,4 +241,46 @@
     self.container = container;
 }
 
+#pragma mark - NSCoding
+
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+    [coder encodeInt:1 forKey:@"HBAudioDefaultsVersion"];
+
+    encodeInteger(_trackSelectionBehavior);
+    encodeObject(_trackSelectionLanguages);
+
+    encodeObject(_tracksArray);
+
+    encodeBool(_allowAACPassthru);
+    encodeBool(_allowAC3Passthru);
+    encodeBool(_allowDTSHDPassthru);
+    encodeBool(_allowDTSPassthru);
+    encodeBool(_allowMP3Passthru);
+
+    encodeInt(_encoderFallback);
+    encodeBool(_secondaryEncoderMode);
+}
+
+- (id)initWithCoder:(NSCoder *)decoder
+{
+    self = [super init];
+
+    decodeInteger(_trackSelectionBehavior);
+    decodeObject(_trackSelectionLanguages);
+
+    decodeObject(_tracksArray);
+
+    decodeBool(_allowAACPassthru);
+    decodeBool(_allowAC3Passthru);
+    decodeBool(_allowDTSHDPassthru);
+    decodeBool(_allowDTSPassthru);
+    decodeBool(_allowMP3Passthru);
+
+    decodeInt(_encoderFallback);
+    decodeBool(_secondaryEncoderMode);
+
+    return self;
+}
+
 @end
diff --git a/macosx/HBFilters.h b/macosx/HBFilters.h
index f0a5fbbe9..ae0603ea0 100644
--- a/macosx/HBFilters.h
+++ b/macosx/HBFilters.h
@@ -11,7 +11,7 @@ extern NSString * const HBFiltersChangedNotification;
 /**
  *  Filters settings.
  */
-@interface HBFilters : NSObject
+@interface HBFilters : NSObject <NSCoding>
 
 - (void)prepareFiltersForPreset:(NSMutableDictionary *)preset;
 - (void)applySettingsFromPreset:(NSDictionary *)preset;
diff --git a/macosx/HBFilters.m b/macosx/HBFilters.m
index 4d3583daf..1d9c5893b 100644
--- a/macosx/HBFilters.m
+++ b/macosx/HBFilters.m
@@ -5,6 +5,7 @@
  It may be used under the terms of the GNU General Public License. */
 
 #import "HBFilters.h"
+#import "NSCodingMacro.h"
 
 NSString * const HBFiltersChangedNotification = @"HBFiltersChangedNotification";
 
@@ -257,6 +258,66 @@ static NSDictionary *_nlmeansTunesDict;
     [self postChangedNotification];
 }
 
+#pragma mark - NSCoding
+
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+    [coder encodeInt:1 forKey:@"HBFiltersVersion"];
+
+    encodeInteger(_detelecine);
+    encodeObject(_detelecineCustomString);
+
+    encodeInteger(_deinterlace);
+    encodeObject(_deinterlaceCustomString);
+
+    encodeInteger(_decomb);
+    encodeObject(_decombCustomString);
+
+    encodeInteger(_detelecine);
+    encodeObject(_detelecineCustomString);
+
+    encodeObject(_denoise);
+    encodeObject(_denoisePreset);
+    encodeObject(_denoiseTune);
+    encodeObject(_denoiseCustomString);
+
+    encodeInteger(_deblock);
+    encodeBool(_grayscale);
+
+    encodeBool(_useDecomb);
+}
+
+- (id)initWithCoder:(NSCoder *)decoder
+{
+    self = [super init];
+
+    decodeInteger(_detelecine);
+    decodeObject(_detelecineCustomString);
+
+    decodeInteger(_deinterlace);
+    decodeObject(_deinterlaceCustomString);
+
+    decodeInteger(_decomb);
+    decodeObject(_decombCustomString);
+
+    decodeInteger(_detelecine);
+    decodeObject(_detelecineCustomString);
+
+    decodeObject(_denoise);
+    decodeObject(_denoisePreset);
+    decodeObject(_denoiseTune);
+    decodeObject(_denoiseCustomString);
+
+    decodeInteger(_deblock);
+    decodeBool(_grayscale);
+
+    decodeBool(_useDecomb);
+
+    _notificationsEnabled = YES;
+
+    return self;
+}
+
 #pragma mark - Presets and queue
 
 - (void)prepareFiltersForPreset:(NSMutableDictionary *)preset
diff --git a/macosx/HBJob.m b/macosx/HBJob.m
index 744952401..5e334bae2 100644
--- a/macosx/HBJob.m
+++ b/macosx/HBJob.m
@@ -11,7 +11,7 @@
 #import "HBAudioController.h"
 #import "HBSubtitlesController.h"
 
-#include "lang.h"
+#import "NSCodingMacro.h"
 
 @implementation HBJob
 
@@ -71,6 +71,8 @@
  */
 - (hb_job_t *)hb_job
 {
+    NSAssert(self.title, @"HBJob: calling hb_job without a valid title loaded");
+
     hb_title_t *title = self.title.hb_title;
     hb_job_t *job = hb_job_init(title);
 
@@ -574,11 +576,58 @@
 
 - (void)encodeWithCoder:(NSCoder *)coder
 {
+    [coder encodeInt:1 forKey:@"HBVideoVersion"];
+
+    encodeInt(_state);
+
+    encodeObject(_fileURL);
+    encodeObject(_destURL);
+
+    encodeInt(_fileFormat);
+    encodeBool(_mp4HttpOptimize);
+    encodeBool(_mp4iPodCompatible);
+
+    encodeObject(_video);
+    encodeObject(_picture);
+    encodeObject(_filters);
+
+    encodeObject(_audioTracks);
+    encodeObject(_subtitlesTracks);
+
+    encodeBool(_chaptersEnabled);
+    encodeObject(_chapterNames);
+
+    encodeObject(_audioDefaults);
+    encodeObject(_subtitlesDefaults);
 }
 
 - (id)initWithCoder:(NSCoder *)decoder
 {
-    return nil;
+    self = [super init];
+
+    decodeInt(_state);
+
+    decodeObject(_fileURL);
+    decodeObject(_destURL);
+
+    decodeInt(_fileFormat);
+    decodeBool(_mp4HttpOptimize);
+    decodeBool(_mp4iPodCompatible);
+
+    decodeObject(_video);
+    decodeObject(_picture);
+    decodeObject(_filters);
+
+    decodeObject(_audioTracks);
+    decodeObject(_subtitlesTracks);
+
+    decodeBool(_chaptersEnabled);
+    decodeObject(_chapterNames);
+
+    decodeObject(_audioDefaults);
+    decodeObject(_subtitlesDefaults);
+
+    return self;
 }
 
 #pragma mark - NSCopying
diff --git a/macosx/HBPicture.h b/macosx/HBPicture.h
index ee35f75eb..25394767f 100644
--- a/macosx/HBPicture.h
+++ b/macosx/HBPicture.h
@@ -13,7 +13,7 @@ extern NSString * const HBPictureChangedNotification;
 /**
  * HBPicture
  */
-@interface HBPicture : NSObject
+@interface HBPicture : NSObject <NSCoding>
 
 - (instancetype)initWithTitle:(HBTitle *)title;
 
@@ -63,7 +63,7 @@ extern NSString * const HBPictureChangedNotification;
 @property (nonatomic, readonly, getter=isKeepDisplayAspect) BOOL keepDisplayAspectEditable;
 @property (nonatomic, readonly, getter=isCustomAnamorphicEnabled) BOOL customAnamorphicEnabled;
 
-@property (nonatomic, readonly) HBTitle *title;
+@property (nonatomic, readwrite, assign) HBTitle *title;
 
 
 @end
diff --git a/macosx/HBPicture.m b/macosx/HBPicture.m
index beecd8d81..63318757c 100644
--- a/macosx/HBPicture.m
+++ b/macosx/HBPicture.m
@@ -7,6 +7,8 @@
 #import "HBPicture.h"
 #import "HBTitle.h"
 
+#import "NSCodingMacro.h"
+
 #include "hb.h"
 
 NSString * const HBPictureChangedNotification = @"HBPictureChangedNotification";
@@ -42,7 +44,7 @@ NSString * const HBPictureChangedNotification = @"HBPictureChangedNotification";
     self = [self init];
     if (self)
     {
-        _title = [title retain];
+        _title = title;
         _width = title.hb_title->geometry.width;
         _height = title.hb_title->geometry.height;
 
@@ -479,6 +481,56 @@ NSString * const HBPictureChangedNotification = @"HBPictureChangedNotification";
     [self postChangedNotification];
 }
 
+#pragma mark - NSCoding
+
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+    [coder encodeInt:1 forKey:@"HBPictureVersion"];
+
+    encodeInt(_width);
+    encodeInt(_height);
+
+    encodeInt(_keepDisplayAspect);
+    encodeInt(_anamorphicMode);
+    encodeInt(_modulus);
+
+    encodeInt(_displayWidth);
+    encodeInt(_parWidth);
+    encodeInt(_parHeight);
+
+    encodeBool(_autocrop);
+    encodeInt(_cropTop);
+    encodeInt(_cropBottom);
+    encodeInt(_cropLeft);
+    encodeInt(_cropRight);
+}
+
+- (id)initWithCoder:(NSCoder *)decoder
+{
+    self = [super init];
+
+    decodeInt(_width);
+    decodeInt(_height);
+
+    decodeInt(_keepDisplayAspect);
+    decodeInt(_anamorphicMode);
+    decodeInt(_modulus);
+
+    decodeInt(_displayWidth);
+    decodeInt(_parWidth);
+    decodeInt(_parHeight);
+
+    decodeBool(_autocrop);
+    decodeInt(_cropTop);
+    decodeInt(_cropBottom);
+    decodeInt(_cropLeft);
+    decodeInt(_cropRight);
+    
+    return self;
+}
+
+#pragma mark - Presets/Queue
+
 - (void)preparePictureForPreset:(NSMutableDictionary *)preset
 {
     preset[@"PictureKeepRatio"] = @(self.keepDisplayAspect);
diff --git a/macosx/HBSubtitlesDefaults.h b/macosx/HBSubtitlesDefaults.h
index f345e6c86..b09da8127 100644
--- a/macosx/HBSubtitlesDefaults.h
+++ b/macosx/HBSubtitlesDefaults.h
@@ -12,7 +12,7 @@ typedef NS_ENUM(NSUInteger, HBSubtitleTrackSelectionBehavior) {
     HBSubtitleTrackSelectionBehaviorAll,
 };
 
-@interface HBSubtitlesDefaults : NSObject
+@interface HBSubtitlesDefaults : NSObject <NSCoding>
 
 @property (nonatomic, readwrite) HBSubtitleTrackSelectionBehavior trackSelectionBehavior;
 @property (nonatomic, readwrite, retain) NSMutableArray *trackSelectionLanguages;
diff --git a/macosx/HBSubtitlesDefaults.m b/macosx/HBSubtitlesDefaults.m
index 544123de1..efcd49c8d 100644
--- a/macosx/HBSubtitlesDefaults.m
+++ b/macosx/HBSubtitlesDefaults.m
@@ -5,6 +5,7 @@
  It may be used under the terms of the GNU General Public License. */
 
 #import "HBSubtitlesDefaults.h"
+#import "NSCodingMacro.h"
 
 @implementation HBSubtitlesDefaults
 
@@ -59,6 +60,34 @@
     preset[@"SubtitleAddForeignAudioSubtitle"] = @(self.addForeignAudioSubtitle);
 }
 
+#pragma mark - NSCoding
+
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+    [coder encodeInt:1 forKey:@"HBAudioDefaultsVersion"];
+
+    encodeInteger(_trackSelectionBehavior);
+    encodeObject(_trackSelectionLanguages);
+
+    encodeBool(_addForeignAudioSearch);
+    encodeBool(_addForeignAudioSubtitle);
+    encodeBool(_addCC);
+}
+
+- (id)initWithCoder:(NSCoder *)decoder
+{
+    self = [super init];
+
+    decodeInteger(_trackSelectionBehavior);
+    decodeObject(_trackSelectionLanguages);
+
+    decodeBool(_addForeignAudioSearch);
+    decodeBool(_addForeignAudioSubtitle);
+    decodeBool(_addCC);
+
+    return self;
+}
+
 - (void)dealloc
 {
     [_trackSelectionLanguages release];
diff --git a/macosx/HBVideo.h b/macosx/HBVideo.h
index 72208ae8a..837aaaeb2 100644
--- a/macosx/HBVideo.h
+++ b/macosx/HBVideo.h
@@ -5,12 +5,11 @@
  It may be used under the terms of the GNU General Public License. */
 
 #import <Foundation/Foundation.h>
-#include "hb.h"
 
 /**
  *  HBVideo
  */
-@interface HBVideo : NSObject
+@interface HBVideo : NSObject <NSCoding>
 
 - (void)applySettingsFromPreset:(NSDictionary *)preset;
 - (void)prepareVideoForPreset:(NSMutableDictionary *)preset;
@@ -27,7 +26,6 @@
 @property (nonatomic, readwrite) int frameRate;
 @property (nonatomic, readwrite) int frameRateMode;
 
-@property (nonatomic, readwrite) BOOL fastFirstPass;
 @property (nonatomic, readwrite) BOOL twoPass;
 @property (nonatomic, readwrite) BOOL turboTwoPass;
 
diff --git a/macosx/HBVideo.m b/macosx/HBVideo.m
index 48c5a8a15..73a3e8c7e 100644
--- a/macosx/HBVideo.m
+++ b/macosx/HBVideo.m
@@ -5,6 +5,8 @@
  It may be used under the terms of the GNU General Public License. */
 
 #import "HBVideo.h"
+#import "NSCodingMacro.h"
+#include "hb.h"
 
 @interface HBVideo ()
 
@@ -374,6 +376,68 @@
     return retval;
 }
 
+#pragma mark - NSCoding
+
+- (void)encodeWithCoder:(NSCoder *)coder
+{
+    [coder encodeInt:1 forKey:@"HBVideoVersion"];
+
+    encodeInt(_encoder);
+
+    encodeInt(_qualityType);
+    encodeInt(_avgBitrate);
+    encodeDouble(_quality);
+
+    encodeInt(_frameRate);
+    encodeInt(_frameRateMode);
+
+    encodeBool(_twoPass);
+    encodeBool(_turboTwoPass);
+
+    encodeBool(_turboTwoPass);
+
+    encodeInt(_advancedOptions);
+    encodeObject(_preset);
+    encodeObject(_tune);
+    encodeObject(_profile);
+    encodeObject(_level);
+
+    encodeObject(_videoOptionExtra);
+
+    encodeBool(_fastDecode);
+}
+
+- (id)initWithCoder:(NSCoder *)decoder
+{
+    self = [super init];
+
+    decodeInt(_encoder);
+
+    decodeInt(_qualityType);
+    decodeInt(_avgBitrate);
+    decodeDouble(_quality);
+
+    decodeInt(_frameRate);
+    decodeInt(_frameRateMode);
+
+    decodeBool(_twoPass);
+    decodeBool(_turboTwoPass);
+
+    decodeBool(_turboTwoPass);
+
+    decodeInt(_advancedOptions);
+    decodeObject(_preset);
+    decodeObject(_tune);
+    decodeObject(_profile);
+    decodeObject(_level);
+
+    decodeObject(_videoOptionExtra);
+
+    decodeBool(_fastDecode);
+
+    return self;
+}
+
 #pragma mark - Various conversion methods from dict/preset/queue/etc
 
 - (void)applySettingsFromPreset:(NSDictionary *)preset
diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj
index 14361e830..11b7933a4 100644
--- a/macosx/HandBrake.xcodeproj/project.pbxproj
+++ b/macosx/HandBrake.xcodeproj/project.pbxproj
@@ -383,6 +383,7 @@
 		A98C29C21977B10600AF5DED /* HBLanguagesSelection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBLanguagesSelection.h; sourceTree = "<group>"; };
 		A98C29C31977B10600AF5DED /* HBLanguagesSelection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBLanguagesSelection.m; sourceTree = "<group>"; };
 		A9935212196F38A70069C6B7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = ChaptersTitles.xib; sourceTree = "<group>"; };
+		A9A2A77F1A4737DD006C219C /* NSCodingMacro.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NSCodingMacro.h; sourceTree = "<group>"; };
 		A9AA44781970664A00D7DEFC /* HBUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBUtilities.h; sourceTree = "<group>"; };
 		A9AA44791970664A00D7DEFC /* HBUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBUtilities.m; sourceTree = "<group>"; };
 		A9AA447B1970724D00D7DEFC /* HBAdvancedController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HBAdvancedController.h; sourceTree = "<group>"; };
@@ -871,6 +872,7 @@
 				A9D1E41618262364002F6424 /* HBPreviewGenerator.m */,
 				273F209714ADBE670021BE6D /* HBDVDDetector.h */,
 				273F209814ADBE670021BE6D /* HBDVDDetector.m */,
+				A9A2A77F1A4737DD006C219C /* NSCodingMacro.h */,
 			);
 			name = Core;
 			sourceTree = "<group>";
diff --git a/macosx/NSCodingMacro.h b/macosx/NSCodingMacro.h
new file mode 100644
index 000000000..7cfbfc755
--- /dev/null
+++ b/macosx/NSCodingMacro.h
@@ -0,0 +1,23 @@
+/*  NSCodingMacro.h $
+
+ 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. */
+
+#ifndef HandBrake_NSCodingMacro_h
+#define HandBrake_NSCodingMacro_h
+
+#define OBJC_STRINGIFY(x) @#x
+#define encodeInt(x) [coder encodeInt:x forKey:OBJC_STRINGIFY(x)]
+#define encodeInteger(x) [coder encodeInteger:x forKey:OBJC_STRINGIFY(x)]
+#define encodeBool(x) [coder encodeBool:x forKey:OBJC_STRINGIFY(x)]
+#define encodeDouble(x) [coder encodeDouble:x forKey:OBJC_STRINGIFY(x)]
+#define encodeObject(x) [coder encodeObject:x forKey:OBJC_STRINGIFY(x)]
+
+#define decodeInt(x) x = [decoder decodeIntForKey:OBJC_STRINGIFY(x)]
+#define decodeInteger(x) x = [decoder decodeIntegerForKey:OBJC_STRINGIFY(x)]
+#define decodeBool(x) x = [decoder decodeBoolForKey:OBJC_STRINGIFY(x)]
+#define decodeDouble(x) x = [decoder decodeDoubleForKey:OBJC_STRINGIFY(x)]
+#define decodeObject(x) x = [decoder decodeObjectForKey:OBJC_STRINGIFY(x)]
+
+#endif
-- 
2.40.0