Changeset 19916cb


Ignore:
Timestamp:
10/10/15 19:50:26 (11 months ago)
Author:
Damiano Galassi <damiog@…>
Branches:
master
Children:
1a04b05
Parents:
656c08b
git-author:
Damiano Galassi <damiog@…> (10/10/15 19:48:31)
git-committer:
Damiano Galassi <damiog@…> (10/10/15 19:50:26)
Message:

MacGui: make preset backups when import upgrades presets and load them if UserPresets?.json is newer than the HandBrake version.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • macosx/HBPresetsManager.m

    r656c08b r19916cb  
    8080}
    8181
    82 - (BOOL)checkIfOutOfDate:(NSDictionary *)dict
     82- (BOOL)isNewer:(NSDictionary *)dict
     83{
     84    int major, minor, micro;
     85    hb_presets_current_version(&major, &minor, &micro);
     86
     87    if ([dict[@"VersionMajor"] intValue] > major)
     88    {
     89        return YES;
     90    }
     91
     92    return NO;
     93}
     94
     95- (BOOL)isOutOfDate:(NSDictionary *)dict
    8396{
    8497    int major, minor, micro;
     
    94107}
    95108
    96 - (BOOL)loadPresetsFromURL:(NSURL *)url
     109- (NSURL *)backupURLfromURL:(NSURL *)url versionMajor:(int)major minor:(int)minor micro:(int)micro
     110{
     111    NSString *backupName = [NSString stringWithFormat:@"%@.%d.%d.%d.json",
     112                            url.lastPathComponent.stringByDeletingPathExtension,
     113                            major, minor, micro];
     114
     115    return [url.URLByDeletingLastPathComponent URLByAppendingPathComponent:backupName];
     116}
     117
     118typedef NS_ENUM(NSUInteger, HBPresetLoadingResult) {
     119    HBPresetLoadingResultOK,
     120    HBPresetLoadingResultOKUpgraded,
     121    HBPresetLoadingResultFailedNewer,
     122    HBPresetLoadingResultFailed
     123};
     124
     125- (NSDictionary *)dictionaryWithPresetsAtURL:(NSURL *)url backup:(BOOL)backup result:(HBPresetLoadingResult *)result;
    97126{
    98127    NSData *presetData = [[NSData alloc] initWithContentsOfURL:url];
    99128
    100     // Try to load to load the old presets file
    101     // if the new one is empty
    102     if (presetData == nil)
    103     {
     129    if (presetData)
     130    {
     131        const char *json = [[NSString alloc] initWithData:presetData encoding:NSUTF8StringEncoding].UTF8String;
     132        NSDictionary *presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:json options:0 error:NULL];
     133
     134        if ([presetsDict isKindOfClass:[NSDictionary class]])
     135        {
     136            if ([self isNewer:presetsDict])
     137            {
     138                *result = HBPresetLoadingResultFailedNewer;
     139                return nil;
     140            }
     141            else if ([self isOutOfDate:presetsDict])
     142            {
     143                // There is a new presets file format,
     144                // make a backup of the old one for the previous versions.
     145                if (backup)
     146                {
     147                    NSURL *backupURL = [self backupURLfromURL:self.fileURL
     148                                                 versionMajor:[presetsDict[@"VersionMajor"] intValue]
     149                                                        minor:[presetsDict[@"VersionMinor"] intValue]
     150                                                        micro:[presetsDict[@"VersionMicro"] intValue]];
     151                    [[NSFileManager defaultManager] copyItemAtURL:url toURL:backupURL error:NULL];
     152                }
     153
     154                // Update the presets to the current format.
     155                char *updatedJson;
     156                hb_presets_import_json(json, &updatedJson);
     157                presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:updatedJson options:0 error:NULL];
     158                free(updatedJson);
     159
     160                *result = HBPresetLoadingResultOKUpgraded;
     161            }
     162            else
     163            {
     164                // Else, clean up the presets just to be sure
     165                // it's in the right format.
     166                char *cleanedJson = hb_presets_clean_json(json);
     167                presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:cleanedJson options:0 error:NULL];
     168                free(cleanedJson);
     169
     170                *result = HBPresetLoadingResultOK;
     171            }
     172
     173            return presetsDict;
     174        }
     175    }
     176
     177    *result = HBPresetLoadingResultFailed;
     178    return nil;
     179}
     180
     181- (void)loadPresetsFromURL:(NSURL *)url;
     182{
     183    HBPresetLoadingResult result;
     184    NSDictionary *presetsDict;
     185
     186    // Load the presets
     187    presetsDict = [self dictionaryWithPresetsAtURL:url backup:YES result:&result];
     188
     189    if (result == HBPresetLoadingResultFailedNewer)
     190    {
     191        // Try a backup
     192        int major, minor, micro;
     193        hb_presets_current_version(&major, &minor, &micro);
     194
     195        NSURL *backupURL = [self backupURLfromURL:url versionMajor:major minor:minor micro:micro];
     196
     197        HBPresetLoadingResult backupResult;
     198        presetsDict = [self dictionaryWithPresetsAtURL:backupURL backup:NO result:&backupResult];
     199
     200        // Change the fileURL so we don't overwrite
     201        // the latest version presets.
     202        _fileURL = backupURL;
     203    }
     204
     205    // Add the presets to the root
     206    if (presetsDict)
     207    {
     208        for (NSDictionary *child in presetsDict[@"PresetList"])
     209        {
     210            [self.root.children addObject:[[HBPreset alloc] initWithDictionary:child]];
     211        }
     212
     213        if (result == HBPresetLoadingResultOKUpgraded)
     214        {
     215            [self generateBuiltInPresets];
     216        }
     217    }
     218
     219    // If there is no json presets file try to read the old plist
     220    if (result == HBPresetLoadingResultFailed && presetsDict == nil)
     221    {
     222        // Try to load to load the old presets file
     223        // if the new one is empty
    104224        [self loadOldPresetsFromURL:[url.URLByDeletingPathExtension URLByAppendingPathExtension:@"plist"]];
    105225        [self generateBuiltInPresets];
    106     }
    107     else
    108     {
    109         const char *json = [[NSString alloc] initWithData:presetData encoding:NSUTF8StringEncoding].UTF8String;
    110         char *cleanedJson = hb_presets_clean_json(json);
    111         NSDictionary *presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:cleanedJson options:0 error:NULL];
    112 
    113         if ([self checkIfOutOfDate:presetsDict])
    114         {
    115             char *updatedJson;
    116             int   result;
    117 
    118             result = hb_presets_import_json(cleanedJson, &updatedJson);
    119             presetsDict = [NSJSONSerialization HB_JSONObjectWithUTF8String:updatedJson options:0 error:NULL];
    120             free(updatedJson);
    121         }
    122 
    123         free(cleanedJson);
    124 
    125         for (NSDictionary *child in presetsDict[@"PresetList"])
    126         {
    127             [self.root.children addObject:[[HBPreset alloc] initWithDictionary:child]];
    128         }
    129 
    130         if ([self checkIfOutOfDate:presetsDict])
    131         {
    132             [self generateBuiltInPresets];
    133         }
    134226    }
    135227
     
    151243
    152244    [self selectNewDefault];
    153 
    154     return YES;
    155245}
    156246
     
    269359/**
    270360 * Built-in preset folders at the root of the hierarchy
    271  *
    272  * Note: the built-in presets will *not* sort themselves alphabetically,
    273  * so they will appear in the order you create them.
    274361 */
    275362- (void)generateBuiltInPresets
Note: See TracChangeset for help on using the changeset viewer.