summaryrefslogtreecommitdiffstats
path: root/libhb/platform/macosx/config.m
blob: b03eae43ed961a41bb85895e0acf467550fbac5e (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
#import <Foundation/Foundation.h>

static NSURL * macOS_last_modified_url(NSURL *url1, NSURL* url2)
{
    NSString *presetFile = @"HandBrake/UserPresets.json";

    NSURL *presetsUrl1 = [url1 URLByAppendingPathComponent:presetFile isDirectory:NO];
    NSURL *presetsUrl2 = [url2 URLByAppendingPathComponent:presetFile isDirectory:NO];

    NSDate *date1 = nil;
    [presetsUrl1 getResourceValue:&date1 forKey:NSURLAttributeModificationDateKey error:nil];

    NSDate *date2 = nil;
    [presetsUrl2 getResourceValue:&date2 forKey:NSURLAttributeModificationDateKey error:nil];

    return presetsUrl2 && (date1 == nil || [date2 compare:date1] == NSOrderedDescending) ? url2 : url1;
}

static NSURL * macOS_get_application_support_url()
{
    NSFileManager *fileManager = NSFileManager.defaultManager;
    NSArray<NSURL *> *applicationSupportUrls = [fileManager URLsForDirectory:NSApplicationSupportDirectory
                                                                   inDomains:NSUserDomainMask];

    NSURL *appSupportURL = applicationSupportUrls.firstObject;

    NSArray<NSURL *> *libraryUrls = [fileManager URLsForDirectory:NSLibraryDirectory
                                                        inDomains:NSUserDomainMask];
    NSString *sandboxPath = @"Containers/fr.handbrake.HandBrake/Data/Library/Application Support";
    NSURL *sandboxAppSupportURL = [libraryUrls.firstObject URLByAppendingPathComponent:sandboxPath isDirectory:YES];

    return macOS_last_modified_url(appSupportURL, sandboxAppSupportURL);
}

int macOS_get_user_config_directory(char path[512])
{
    @autoreleasepool
    {
        NSURL *url = macOS_get_application_support_url();

        if (url == nil)
        {
            return -1;
        }

        strncpy(path, url.fileSystemRepresentation, 511);
        path[511] = 0;
        return 0;
    }
}