OSDN Git Service

import 0.9.3
[handbrake-jp/handbrake-jp.git] / macosx / InstantHandBrake / DeviceController.m
1 //
2 //  DeviceController.m
3 //  InstantHandBrake
4 //
5 //  Created by Damiano Galassi on 23/01/08.
6 //  This file is part of the HandBrake source code.
7 //  Homepage: <http://handbrake.fr/>.
8 //  It may be used under the terms of the GNU General Public License.
9 //
10 //
11
12 #import "DeviceController.h"
13 #import "hb.h"
14
15 @implementation DeviceController
16
17 - (id)init
18 {
19     if (self = [super init])
20     {
21         devicesArray = [[NSMutableArray alloc] init];
22         appSupportPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Instant HandBrake"];
23         [self loadBuiltInDevices];
24         [self loadDevices];
25     }
26     return self;
27 }
28
29 - (void) dealloc
30 {
31     [devicesArray release];
32     [super dealloc];
33 }
34
35 - (BOOL) loadBuiltInDevices
36 {
37     NSBundle *bundle = [NSBundle mainBundle];
38     NSArray *path = [bundle pathsForResourcesOfType:@"ihbdevice" inDirectory:@"Devices"];
39
40     Device *newDevice;
41     NSString *file;
42     
43     NSEnumerator *dirEnum = [path objectEnumerator];
44     while (file = [dirEnum nextObject])
45     {
46         newDevice = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
47         [devicesArray addObject:newDevice];
48     }
49     
50     if ( ![devicesArray count] )
51         return NO;
52     else
53         return YES;
54 }
55
56 - (BOOL) loadDevices
57 {
58     NSString *file;
59     NSFileManager *fileManager = [NSFileManager defaultManager];
60     Device *newDevice;
61     
62     NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:appSupportPath];
63     while (file = [dirEnum nextObject])
64     {
65         if ([[file pathExtension] isEqualToString: @"ihbdevice"])
66         {
67             newDevice = [NSKeyedUnarchiver unarchiveObjectWithFile:[appSupportPath stringByAppendingPathComponent:file]];
68             [devicesArray addObject:newDevice];
69         }
70     }
71     
72     if ( ![devicesArray count] )
73         return NO;
74     else
75         return YES;
76 }
77
78 /* Use this to create a new device preset for now */
79
80 - (id) populateList
81 {
82     [devicesArray addObject:[[Device alloc] initWithDeviceName: @"iPod"]];
83     [devicesArray addObject:[[Device alloc] initWithDeviceName: @"PSP"]];
84     [devicesArray addObject:[[Device alloc] initWithDeviceName: @"AppleTV"]];
85     
86     Preset *newPreset;
87     newPreset = [[Preset alloc] initWithMuxer: HB_MUX_IPOD
88                                    videoCodec: HB_VCODEC_X264
89                                  videoBitRate: 1500
90                             videoCodecOptions: @"bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subq=6:no-fast-pskip=1:level=30"
91                                    audioCodec: HB_ACODEC_FAAC
92                                  audioBitrate: 128
93                               audioSampleRate: 48000
94                                      maxWidth: 640
95                                     maxHeight: 480
96                                    anamorphic: 0];
97     
98     [[devicesArray objectAtIndex:0] addPreset:newPreset];
99     
100     newPreset = [[Preset alloc] initWithMuxer: HB_MUX_MP4
101                                    videoCodec: HB_VCODEC_X264
102                                  videoBitRate: 600
103                             videoCodecOptions: @""
104                                    audioCodec: HB_ACODEC_FAAC
105                                  audioBitrate: 128
106                               audioSampleRate: 48000
107                                      maxWidth: 480
108                                     maxHeight: 272
109                                    anamorphic: 0];
110                                    
111     [[devicesArray objectAtIndex:1] addPreset:newPreset];
112     
113     newPreset = [[Preset alloc] initWithMuxer: HB_MUX_MP4
114                                    videoCodec: HB_VCODEC_X264
115                                  videoBitRate: 2500
116                             videoCodecOptions: @"bframes=3:ref=1:subq=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0"
117                                    audioCodec: HB_ACODEC_FAAC
118                                  audioBitrate: 160
119                               audioSampleRate: 48000
120                                      maxWidth: 720
121                                     maxHeight: 576
122                                    anamorphic: 1];
123                                    
124     [[devicesArray objectAtIndex:2] addPreset:newPreset];
125 }
126
127 - (NSArray *) devicesList
128 {
129     NSArray *deviceList = [devicesArray copy];
130     [deviceList autorelease];
131     
132     return deviceList;
133 }
134
135 - (BOOL) saveDevices
136 {
137     NSFileManager *fileManager = [NSFileManager defaultManager];
138     BOOL noErr = YES;
139     
140     if( ![fileManager fileExistsAtPath:appSupportPath] )
141         [fileManager createDirectoryAtPath:appSupportPath attributes:nil];
142     
143     NSEnumerator *enumerator;
144     Device *object;
145     enumerator = [devicesArray objectEnumerator];
146     
147     while( object = [enumerator nextObject] )
148     {
149         NSString * saveLocation = [NSString stringWithFormat:@"%@/%@.ihbdevice", appSupportPath, [object name]];
150         if (![fileManager fileExistsAtPath:saveLocation]) 
151         {
152             noErr = [NSKeyedArchiver archiveRootObject:object
153                         toFile:saveLocation];
154         }
155     }
156     return noErr;
157 }
158
159 @end