1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2015 VLC authors and VideoLAN
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #import "intf-prefs.h"
27 #import "CoreInteraction.h"
29 #include <unistd.h> /* execl() */
31 @implementation VLCMain(OldPrefs)
33 static NSString * kVLCPreferencesVersion = @"VLCPreferencesVersion";
34 static const int kCurrentPreferencesVersion = 3;
38 NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCurrentPreferencesVersion]
39 forKey:kVLCPreferencesVersion];
41 [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
44 - (void)resetAndReinitializeUserDefaults
46 // note that [NSUserDefaults resetStandardUserDefaults] will NOT correctly reset to the defaults
48 NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
49 [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
51 // set correct version to avoid question about outdated config
52 [[NSUserDefaults standardUserDefaults] setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
53 [[NSUserDefaults standardUserDefaults] synchronize];
56 - (void)removeOldPreferences
58 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
59 int version = [defaults integerForKey:kVLCPreferencesVersion];
62 * Store version explicitely in file, for ease of debugging.
63 * Otherwise, the value will be just defined at app startup,
64 * as initialized above.
66 [defaults setInteger:version forKey:kVLCPreferencesVersion];
67 if (version >= kCurrentPreferencesVersion)
71 [defaults setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
72 [defaults synchronize];
74 if (![[VLCCoreInteraction sharedInstance] fixPreferences])
77 config_SaveConfigFile(VLCIntf); // we need to do manually, since we won't quit libvlc cleanly
78 } else if (version == 2) {
79 /* version 2 (used by VLC 2.0.x and early versions of 2.1) can lead to exceptions within 2.1 or later
80 * so we reset the OS X specific prefs here - in practice, no user will notice */
81 [self resetAndReinitializeUserDefaults];
84 NSArray *libraries = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
85 NSUserDomainMask, YES);
86 if (!libraries || [libraries count] == 0) return;
87 NSString * preferences = [[libraries firstObject] stringByAppendingPathComponent:@"Preferences"];
89 int res = NSRunInformationalAlertPanel(_NS("Remove old preferences?"),
90 _NS("We just found an older version of VLC's preferences files."),
91 _NS("Move To Trash and Relaunch VLC"), _NS("Ignore"), nil, nil);
92 if (res != NSOKButton) {
93 [defaults setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
97 // Do NOT add the current plist file here as this would conflict with caching.
98 // Instead, just reset below.
99 NSArray * ourPreferences = [NSArray arrayWithObjects:@"org.videolan.vlc", @"VLC", nil];
101 /* Move the file to trash one by one. Using above array the method would stop after first file
103 for (NSString *file in ourPreferences) {
104 [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:preferences destination:@"" files:[NSArray arrayWithObject:file] tag:nil];
107 [self resetAndReinitializeUserDefaults];
111 const char * path = [[[NSBundle mainBundle] executablePath] UTF8String];
113 /* For some reason we need to fork(), not just execl(), which reports a ENOTSUP then. */
117 execl(path, path, NULL);