Migrate configuration files on OS X to ~/Library/Application Support/Aseprite (fix #1165)

This commit is contained in:
David Capello 2016-07-06 12:22:40 -03:00
parent d91ca36edd
commit 0722d95b5d
5 changed files with 93 additions and 2 deletions

View File

@ -12,10 +12,16 @@
#include "app/ini_file.h"
#include "app/resource_finder.h"
#include "base/path.h"
#include "base/split_string.h"
#include "base/string.h"
#include "cfg/cfg.h"
#ifdef __APPLE__
#include "she/logger.h"
#include "she/system.h"
#endif
#ifndef _WIN32
#include "base/fs.h"
#endif
@ -34,9 +40,48 @@ ConfigModule::ConfigModule()
{
ResourceFinder rf;
rf.includeUserDir("aseprite.ini");
// getFirstOrCreateDefault() will create the Aseprite directory
// inside the OS configuration folder (~/.config/aseprite/, etc.).
std::string fn = rf.getFirstOrCreateDefault();
#ifndef _WIN32 // Migrate the configuration file to the new location in Unix-like systems
#ifdef __APPLE__
// On OS X we migrate from ~/.config/aseprite/* -> "~/Library/Application Support/Aseprite/*"
if (!base::is_file(fn)) {
try {
std::string new_dir = base::get_file_path(fn);
// Now we try to move all old configuration files into the new
// directory.
ResourceFinder old_rf;
old_rf.includeHomeDir(".config/aseprite/aseprite.ini");
std::string old_config_fn = old_rf.defaultFilename();
if (base::is_file(old_config_fn)) {
std::string old_dir = base::get_file_path(old_config_fn);
for (std::string old_fn : base::list_files(old_dir)) {
std::string from = base::join_path(old_dir, old_fn);
std::string to = base::join_path(new_dir, old_fn);
base::move_file(from, to);
}
base::remove_directory(old_dir);
}
}
// Something failed
catch (const std::exception& ex) {
std::string err = "Error in configuration migration: ";
err += ex.what();
auto system = she::instance();
if (system && system->logger())
system->logger()->logError(err.c_str());
}
}
#elif !defined(_WIN32)
// On Linux we migrate the old configuration file name
// (.asepriterc -> ~/.config/aseprite/aseprite.ini)
{
ResourceFinder old_rf;
old_rf.includeHomeDir(".asepriterc");
@ -44,6 +89,7 @@ ConfigModule::ConfigModule()
if (base::is_file(old_fn))
base::move_file(old_fn, fn);
}
#endif
set_config_file(fn.c_str());

View File

@ -160,6 +160,14 @@ void ResourceFinder::includeUserDir(const char* filename)
includeHomeDir(filename);
}
#elif __APPLE__
// $HOME/Library/Application Support/Aseprite/filename
addPath(
base::join_path(
base::join_path(base::get_lib_app_support_path(), PACKAGE),
filename).c_str());
#else
// $HOME/.config/aseprite/filename

View File

@ -59,6 +59,11 @@ set(BASE_SOURCES
trim_string.cpp
version.cpp)
if(APPLE)
set(BASE_SOURCES ${BASE_SOURCES}
fs_osx.mm)
endif()
if(WIN32)
set(BASE_SOURCES ${BASE_SOURCES}
win32_exception.cpp)

View File

@ -1,5 +1,5 @@
// Aseprite Base Library
// Copyright (c) 2001-2013, 2015 David Capello
// Copyright (c) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -36,6 +36,9 @@ namespace base {
std::string get_app_path();
std::string get_temp_path();
std::string get_user_docs_folder();
#if __APPLE__
std::string get_lib_app_support_path();
#endif
// If the given filename is a relative path, it converts the
// filename to an absolute one.

29
src/base/fs_osx.mm Normal file
View File

@ -0,0 +1,29 @@
// Aseprite Base Library
// Copyright (c) 2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Foundation/Foundation.h>
#include <string>
namespace base {
std::string get_lib_app_support_path()
{
NSArray* dirs = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, YES);
if (dirs) {
NSString* dir = [dirs firstObject];
if (dir)
return std::string([dir UTF8String]);
}
return std::string();
}
} // namespace base