From d73d486ca007cc35edf739408a2bd7884b31abc1 Mon Sep 17 00:00:00 2001 From: karliss Date: Sat, 6 Mar 2021 19:26:58 +0200 Subject: [PATCH] Add setting import from Cutter before RadareOrg to rizin renames. (#2588) --- src/Main.cpp | 6 +++ src/common/ColorThemeWorker.cpp | 8 ++++ src/common/ColorThemeWorker.h | 15 ++++-- src/common/SettingsUpgrade.cpp | 76 ++++++++++++++++++++++++++++++ src/common/SettingsUpgrade.h | 11 +++++ src/widgets/ColorThemeListView.cpp | 4 +- 6 files changed, 113 insertions(+), 7 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index cc7b5901..0e665061 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -73,6 +73,12 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationName("rizin"); QCoreApplication::setApplicationName("cutter"); + // Importing settings after setting rename, needs separate handling in addition to regular version to version upgrade. + if (Cutter::shouldOfferSettingImport()) + { + Cutter::showSettingImportDialog(argc, argv); + } + Cutter::initializeSettings(); QCoreApplication::setAttribute( diff --git a/src/common/ColorThemeWorker.cpp b/src/common/ColorThemeWorker.cpp index b842a148..04211277 100644 --- a/src/common/ColorThemeWorker.cpp +++ b/src/common/ColorThemeWorker.cpp @@ -309,3 +309,11 @@ QStringList ColorThemeWorker::customThemes() const } return ret; } + +const QStringList &ColorThemeWorker::getRizinSpecificOptions() +{ + if (rizinSpecificOptions.isEmpty()) { + rizinSpecificOptions = Core()->cmdj("ecj").object().keys(); + } + return rizinSpecificOptions; +} diff --git a/src/common/ColorThemeWorker.h b/src/common/ColorThemeWorker.h index cc5f7d66..7c228804 100644 --- a/src/common/ColorThemeWorker.h +++ b/src/common/ColorThemeWorker.h @@ -17,11 +17,6 @@ class ColorThemeWorker : public QObject { Q_OBJECT public: - /** - * @brief rizinSpecificOptions is list of all available Rizin-only color options. - */ - const QStringList rizinSpecificOptions = Core()->cmdj("ecj").object().keys(); - /** * @brief cutterSpecificOptions is list of all available Cutter-only color options. */ @@ -117,7 +112,17 @@ public: */ QStringList customThemes() const; + QString getStandardThemesPath() { return standardRzThemesLocationPath; } + QString getCustomThemesPath() { return customRzThemesLocationPath; } + + const QStringList &getRizinSpecificOptions(); + private: + /** + * @brief list of all available Rizin-only color options. + */ + QStringList rizinSpecificOptions; + QString standardRzThemesLocationPath; QString customRzThemesLocationPath; diff --git a/src/common/SettingsUpgrade.cpp b/src/common/SettingsUpgrade.cpp index 1f3563ea..33b5bff9 100644 --- a/src/common/SettingsUpgrade.cpp +++ b/src/common/SettingsUpgrade.cpp @@ -1,4 +1,6 @@ #include "SettingsUpgrade.h" +#include +#include #include "common/ColorThemeWorker.h" @@ -206,3 +208,77 @@ void Cutter::migrateThemes() settings.setValue(THEME_VERSION_KEY, THEME_VERSION_CURRENT); } } + +static const char PRE_RIZIN_ORG[] = "RadareOrg"; +static const char PRE_RIZIN_APP[] = "Cutter"; + +bool Cutter::shouldOfferSettingImport() +{ + QSettings::setDefaultFormat(QSettings::IniFormat); + QSettings settings; + if (settings.contains("firstExecution")) { + return false; + } + QSettings r2CutterSettings(QSettings::IniFormat, QSettings::Scope::UserScope, PRE_RIZIN_ORG, + PRE_RIZIN_APP); + QString f = r2CutterSettings.fileName(); + if (r2CutterSettings.value("firstExecution", true) != QVariant(false)) { + return false; // no Cutter <= 1.12 settings to import + } + int version = r2CutterSettings.value("version", -1).toInt(); + if (version < 1 || version > 6) { + return false; // version too new maybe it's from r2Cutter fork instead of pre-rizin Cutter. + } + return true; +} + +static void importOldSettings() +{ + // QSettings + QSettings::setDefaultFormat(QSettings::IniFormat); + QSettings r2CutterSettings(QSettings::IniFormat, QSettings::Scope::UserScope, PRE_RIZIN_ORG, + PRE_RIZIN_APP); + QSettings newSettings; + for (auto key : r2CutterSettings.allKeys()) { + newSettings.setValue(key, r2CutterSettings.value(key)); + } + + // Color Themes + char *szThemes = rz_str_home(".local/share/radare2/cons"); + QString r2ThemesPath = szThemes; + rz_mem_free(szThemes); + QDir r2ThemesDir(r2ThemesPath); + if (QFileInfo(r2ThemesPath).isDir()) { + QDir rzThemesDir(ThemeWorker().getCustomThemesPath()); + if (!rzThemesDir.exists()) { + QDir().mkpath(rzThemesDir.absolutePath()); + } + for (auto f : r2ThemesDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files)) { + auto dst = rzThemesDir.absoluteFilePath(f.fileName()); + if (QDir(dst).exists()) { + qInfo() << "Theme" << dst << "already exists. Not overwriting with" + << f.absoluteFilePath(); + continue; + } + qInfo() << "Copying Theme" << f.absoluteFilePath() << "to" << dst; + QFile::copy(f.absoluteFilePath(), dst); + } + } +} + +void Cutter::showSettingImportDialog(int &argc, char **argv) +{ + // Creating temporary QApplication because this happens before anything else in Cutter is + // initialized + QApplication temporaryApp(argc, argv); + QSettings r2CutterSettings(QSettings::IniFormat, QSettings::Scope::UserScope, PRE_RIZIN_ORG, + PRE_RIZIN_APP); + QString oldFile = r2CutterSettings.fileName(); + // Can't use message translations because settings have not been imported + auto result = + QMessageBox::question(nullptr, "Setting import", + QString("Do you want to import settings from %1?").arg(oldFile)); + if (result == QMessageBox::Yes) { + importOldSettings(); + } +} diff --git a/src/common/SettingsUpgrade.h b/src/common/SettingsUpgrade.h index bd06c931..5e445abd 100644 --- a/src/common/SettingsUpgrade.h +++ b/src/common/SettingsUpgrade.h @@ -6,6 +6,17 @@ namespace Cutter { void initializeSettings(); +/** + * @brief Check if Cutter should offer importing settings from version that can't be directly updated. + * @return True if this is first time running Cutter and r2 based Cutter <= 1.12 settings exist. + */ +bool shouldOfferSettingImport(); +/** + * @brief Ask user if Cutter should import settings from pre-rizin Cutter. + * + * This function assume that QApplication isn't running yet. + */ +void showSettingImportDialog(int &argc, char **argv); void migrateThemes(); } diff --git a/src/widgets/ColorThemeListView.cpp b/src/widgets/ColorThemeListView.cpp index 7b37392f..05cc4f2f 100644 --- a/src/widgets/ColorThemeListView.cpp +++ b/src/widgets/ColorThemeListView.cpp @@ -240,7 +240,7 @@ void ColorThemeListView::currentChanged(const QModelIndex ¤t, const QModel { ColorOption prev = previous.data(Qt::UserRole).value(); Config()->setColor(prev.optionName, prev.color); - if (ThemeWorker().rizinSpecificOptions.contains(prev.optionName)) { + if (ThemeWorker().getRizinSpecificOptions().contains(prev.optionName)) { Core()->cmdRaw(QString("ec %1 %2").arg(prev.optionName).arg(prev.color.name())); } @@ -306,7 +306,7 @@ void ColorThemeListView::blinkTimeout() auto updateColor = [](const QString &name, const QColor &color) { Config()->setColor(name, color); - if (ThemeWorker().rizinSpecificOptions.contains(name)) { + if (ThemeWorker().getRizinSpecificOptions().contains(name)) { Core()->cmdRaw(QString("ec %1 %2").arg(name).arg(color.name())); } };