Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy over config file to new location on windows #768

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/libsync/configfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,40 @@ static chrono::milliseconds millisecondsValue(const QSettings &setting, const ch
return chrono::milliseconds(setting.value(QLatin1String(key), qlonglong(defaultValue.count())).toLongLong());
}


bool copy_dir_recursive(QString from_dir, QString to_dir)
{
QDir dir;
dir.setPath(from_dir);

from_dir += QDir::separator();
to_dir += QDir::separator();

foreach (QString copy_file, dir.entryList(QDir::Files)) {
QString from = from_dir + copy_file;
QString to = to_dir + copy_file;

if (QFile::copy(from, to) == false) {
return false;
}
}

foreach (QString copy_dir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
QString from = from_dir + copy_dir;
QString to = to_dir + copy_dir;

if (dir.mkpath(to) == false) {
return false;
}

if (copy_dir_recursive(from, to) == false) {
return false;
}
}

return true;
}

ConfigFile::ConfigFile()
{
// QDesktopServices uses the application name to create a config path
Expand Down Expand Up @@ -262,9 +296,26 @@ QVariant ConfigFile::getPolicySetting(const QString &setting, const QVariant &de
QString ConfigFile::configPath() const
{
if (_confDir.isEmpty()) {
// On Unix, use the AppConfigLocation for the settings, that's configurable with the XDG_CONFIG_HOME env variable.
// On Windows, use AppDataLocation, that's where the roaming data is and where we should store the config file
_confDir = QStandardPaths::writableLocation(Utility::isWindows() ? QStandardPaths::AppDataLocation : QStandardPaths::AppConfigLocation);
if (!Utility::isWindows()) {
// On Unix, use the AppConfigLocation for the settings, that's configurable with the XDG_CONFIG_HOME env variable.
_confDir = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
} else {
// On Windows, use AppDataLocation, that's where the roaming data is and where we should store the config file
auto newLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);

// Check if this is the first time loading the new location
if (!QFileInfo(newLocation).isDir()) {
// Migrate data to the new locations
auto oldLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);

// Only migrate if the old location exists.
if (QFileInfo(oldLocation).isDir()) {
QDir().mkpath(newLocation);
copy_dir_recursive(oldLocation, newLocation);
}
}
_confDir = newLocation;
}
}
QString dir = _confDir;

Expand Down