Add setting import from Cutter before RadareOrg to rizin renames. (#2588)

This commit is contained in:
karliss 2021-03-06 19:26:58 +02:00 committed by GitHub
parent 35b9ffa548
commit d73d486ca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 113 additions and 7 deletions

View File

@ -73,6 +73,12 @@ int main(int argc, char *argv[])
QCoreApplication::setOrganizationName("rizin"); QCoreApplication::setOrganizationName("rizin");
QCoreApplication::setApplicationName("cutter"); 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(); Cutter::initializeSettings();
QCoreApplication::setAttribute( QCoreApplication::setAttribute(

View File

@ -309,3 +309,11 @@ QStringList ColorThemeWorker::customThemes() const
} }
return ret; return ret;
} }
const QStringList &ColorThemeWorker::getRizinSpecificOptions()
{
if (rizinSpecificOptions.isEmpty()) {
rizinSpecificOptions = Core()->cmdj("ecj").object().keys();
}
return rizinSpecificOptions;
}

View File

@ -17,11 +17,6 @@ class ColorThemeWorker : public QObject
{ {
Q_OBJECT Q_OBJECT
public: 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. * @brief cutterSpecificOptions is list of all available Cutter-only color options.
*/ */
@ -117,7 +112,17 @@ public:
*/ */
QStringList customThemes() const; QStringList customThemes() const;
QString getStandardThemesPath() { return standardRzThemesLocationPath; }
QString getCustomThemesPath() { return customRzThemesLocationPath; }
const QStringList &getRizinSpecificOptions();
private: private:
/**
* @brief list of all available Rizin-only color options.
*/
QStringList rizinSpecificOptions;
QString standardRzThemesLocationPath; QString standardRzThemesLocationPath;
QString customRzThemesLocationPath; QString customRzThemesLocationPath;

View File

@ -1,4 +1,6 @@
#include "SettingsUpgrade.h" #include "SettingsUpgrade.h"
#include <QApplication>
#include <QMessageBox>
#include "common/ColorThemeWorker.h" #include "common/ColorThemeWorker.h"
@ -206,3 +208,77 @@ void Cutter::migrateThemes()
settings.setValue(THEME_VERSION_KEY, THEME_VERSION_CURRENT); 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();
}
}

View File

@ -6,6 +6,17 @@
namespace Cutter { namespace Cutter {
void initializeSettings(); 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(); void migrateThemes();
} }

View File

@ -240,7 +240,7 @@ void ColorThemeListView::currentChanged(const QModelIndex &current, const QModel
{ {
ColorOption prev = previous.data(Qt::UserRole).value<ColorOption>(); ColorOption prev = previous.data(Qt::UserRole).value<ColorOption>();
Config()->setColor(prev.optionName, prev.color); 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())); 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) { auto updateColor = [](const QString &name, const QColor &color) {
Config()->setColor(name, 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())); Core()->cmdRaw(QString("ec %1 %2").arg(name).arg(color.name()));
} }
}; };