From 004720bdf88c698ecbf170b91fa8125e9994249a Mon Sep 17 00:00:00 2001 From: Vaibhav Pandey Date: Thu, 9 May 2019 12:54:26 +0530 Subject: [PATCH] Better (adb, java) exe lookup, auto-restart on theme changes. --- sources/appearancesettingswidget.cpp | 15 +++++ sources/binarysettingswidget.cpp | 24 +++++--- sources/main.cpp | 32 ++++++----- sources/mainwindow.cpp | 3 + sources/processutils.cpp | 85 +++++----------------------- sources/processutils.h | 1 - 6 files changed, 68 insertions(+), 92 deletions(-) diff --git a/sources/appearancesettingswidget.cpp b/sources/appearancesettingswidget.cpp index 13778c3..811ba56 100644 --- a/sources/appearancesettingswidget.cpp +++ b/sources/appearancesettingswidget.cpp @@ -1,9 +1,13 @@ +#include #include #include #include +#include #include #include "appearancesettingswidget.h" +#define CODE_RESTART 60600 + AppearanceSettingsWidget::AppearanceSettingsWidget(QWidget *parent) : QWidget(parent) { @@ -49,9 +53,20 @@ QLayout *AppearanceSettingsWidget::buildForm() void AppearanceSettingsWidget::save() { QSettings settings; + bool dark = settings.value("dark_theme").toBool(); settings.setValue("dark_theme", m_RadioThemeDark->isChecked()); settings.setValue("editor_font", m_ComboEditorFont->currentText()); settings.setValue("editor_font_size", m_SpinEditorFontSize->value()); settings.setValue("editor_whitespaces", m_CheckShowWhitespaces->isChecked()); settings.sync(); + if (dark != m_RadioThemeDark->isChecked()) { + int btn = QMessageBox::information(this, + tr("Theme"), + tr("Changes to app theme will be applied only when you restart APK studio."), + tr("Restart"), + tr("OK")); + if (btn == 0) { + QApplication::exit(CODE_RESTART); + } + } } diff --git a/sources/binarysettingswidget.cpp b/sources/binarysettingswidget.cpp index 5e5352d..9b0879f 100644 --- a/sources/binarysettingswidget.cpp +++ b/sources/binarysettingswidget.cpp @@ -69,10 +69,20 @@ QLayout *BinarySettingsWidget::buildForm() label->setTextFormat(Qt::RichText); layout->addRow("", child); QSettings settings; - m_EditAdbExe->setText(settings.value("adb_exe").toString()); + auto adb = settings.value("adb_exe").toString(); + if (adb.isEmpty()) { + m_EditAdbExe->setText(ProcessUtils::adbExe()); + } else { + m_EditAdbExe->setText(adb); + } m_EditApktoolJar->setText(settings.value("apktool_jar").toString()); m_EditJadxExe->setText(settings.value("jadx_exe").toString()); - m_EditJavaExe->setText(settings.value("java_exe").toString()); + auto java = settings.value("java_exe").toString(); + if (adb.isEmpty()) { + m_EditJavaExe->setText(ProcessUtils::javaExe()); + } else { + m_EditJavaExe->setText(java); + } m_EditUberApkSignerJar->setText(settings.value("uas_jar").toString()); m_SpinJavaHeap->setValue(ProcessUtils::javaHeapSize()); return layout; @@ -86,7 +96,7 @@ void BinarySettingsWidget::handleBrowseAdb() #else tr("Browse ADB"), #endif - QString() + m_EditAdbExe->text() #ifdef Q_OS_WIN , tr("Executable File(s) (*.exe)") #endif @@ -100,7 +110,7 @@ void BinarySettingsWidget::handleBrowseApktool() { const QString path = QFileDialog::getOpenFileName(this, tr("Browse Apktool (apktool.jar)"), - QString(), + m_EditApktoolJar->text(), tr("JAR File(s) (*.jar)")); if (!path.isEmpty()) { m_EditApktoolJar->setText(QDir::toNativeSeparators(path)); @@ -115,7 +125,7 @@ void BinarySettingsWidget::handleBrowseJadx() #else tr("Browse Jadx"), #endif - QString() + m_EditJadxExe->text() #ifdef Q_OS_WIN , tr("Windows Batch File(s) (*.bat)") #endif @@ -133,7 +143,7 @@ void BinarySettingsWidget::handleBrowseJava() #else tr("Browse Java"), #endif - QString() + m_EditJavaExe->text() #ifdef Q_OS_WIN , tr("Executable File(s) (*.exe)") #endif @@ -147,7 +157,7 @@ void BinarySettingsWidget::handleBrowseUberApkSigner() { const QString path = QFileDialog::getOpenFileName(this, tr("Browse Uber APK Signer (uber-apk-signer.jar)"), - QString(), + m_EditUberApkSignerJar->text(), tr("JAR File(s) (*.jar)")); if (!path.isEmpty()) { m_EditUberApkSignerJar->setText(QDir::toNativeSeparators(path)); diff --git a/sources/main.cpp b/sources/main.cpp index 9886c3a..375361a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -3,19 +3,25 @@ #include "splashwindow.h" #include "../Qt-Frameless-Window-DarkStyle/DarkStyle.h" +#define CODE_RESTART 60600 + int main(int argc, char *argv[]) { - QCoreApplication::setApplicationName("APK Studio"); - QCoreApplication::setOrganizationDomain("vaibhavpandey.com"); - QCoreApplication::setOrganizationName("Vaibhav Pandey"); - QApplication app(argc, argv); - app.setWindowIcon(QIcon(":/images/icon.png")); - QSettings settings; - const bool dark = settings.value("dark_theme", false).toBool(); - if (dark) { - app.setStyle(new DarkStyle); - } - SplashWindow window; - window.show(); - return app.exec(); + QApplication::setApplicationName("APK Studio"); + QApplication::setOrganizationDomain("vaibhavpandey.com"); + QApplication::setOrganizationName("Vaibhav Pandey"); + int code = 0; + do { + QApplication app(argc, argv); + app.setWindowIcon(QIcon(":/images/icon.png")); + QSettings settings; + const bool dark = settings.value("dark_theme", false).toBool(); + if (dark) { + app.setStyle(new DarkStyle); + } + SplashWindow window; + window.show(); + code = app.exec(); + } while (code == CODE_RESTART); + return code; } diff --git a/sources/mainwindow.cpp b/sources/mainwindow.cpp index 427ba58..4dafb7d 100644 --- a/sources/mainwindow.cpp +++ b/sources/mainwindow.cpp @@ -87,6 +87,9 @@ MainWindow::MainWindow(const QMap &versions, QWidget *parent) bool missing = false; foreach (const QString &binary, versions.keys()) { if (versions[binary].isEmpty()) { +#ifdef QT_DEBUG + qDebug() << binary << "is missing"; +#endif missing = true; break; } diff --git a/sources/processutils.cpp b/sources/processutils.cpp index cfe46a7..44c0508 100644 --- a/sources/processutils.cpp +++ b/sources/processutils.cpp @@ -42,11 +42,7 @@ QString ProcessUtils::adbExe() #ifdef Q_OS_WIN name.append(".exe"); #endif - exe = findInPath(name); - if (!exe.isEmpty()) { - return exe; - } - return QString(); + return findInPath(name); } QString ProcessUtils::apktoolJar() @@ -56,51 +52,21 @@ QString ProcessUtils::apktoolJar() return (!jar.isEmpty() && QFile::exists(jar)) ? jar : QString(); } -QString ProcessUtils::expandEnvVar(const QString &name) -{ - QString value = QProcessEnvironment::systemEnvironment().value(name); - if (value.isEmpty()) { - return QString(); - } -#ifdef Q_OS_WIN - QRegularExpression regexp("(%([^%]+)%)"); -#else - QRegularExpression regexp("(\\$([a-zA-Z0-9_]+))"); -#endif - if (!value.contains(regexp)) { - return value; - } - QRegularExpressionMatchIterator matches = regexp.globalMatch(value); - while (matches.hasNext()) { - QRegularExpressionMatch match = matches.next(); - value.replace(match.capturedStart(1), match.capturedLength(1), expandEnvVar(match.captured(2))); - } - return value; -} - QString ProcessUtils::findInPath(const QString &exe) { - const QString path = expandEnvVar("PATH"); -#ifdef QT_DEBUG - qDebug() << "PATH is" << path; -#endif - if (path.isEmpty()) { - return QString(); - } + auto result = runCommand( #ifdef Q_OS_WIN - const char separator = ';'; + "where", #else - const char separator = ':'; + "which", #endif - const QStringList locations = path.split(separator); - foreach (QString location, locations) { - QDir dir(location); - if (dir.exists() && dir.exists(exe)) { + QStringList(exe)); + if ((result.code == 0) && (result.output.count() >= 1)) { + auto location = result.output.first(); #ifdef QT_DEBUG - qDebug() << exe << "found in" << location; + qDebug() << exe << "found at" << location; #endif - return QDir::toNativeSeparators(dir.filePath(exe)); - } + return location; } return QString(); } @@ -109,18 +75,7 @@ QString ProcessUtils::jadxExe() { QSettings settings; QString exe = settings.value("jadx_exe").toString(); - if (!exe.isEmpty() && QFile::exists(exe)) { - return exe; - } - QString name("jadx"); -#ifdef Q_OS_WIN - name.append(".bat"); -#endif - exe = findInPath(name); - if (!exe.isEmpty()) { - return exe; - } - return QString(); + return (!exe.isEmpty() && QFile::exists(exe)) ? exe : QString(); } QString ProcessUtils::javaExe() @@ -135,22 +90,7 @@ QString ProcessUtils::javaExe() #ifdef Q_OS_WIN name.append(".exe"); #endif - exe = findInPath(name); - if (!exe.isEmpty()) { - return exe; - } - QString jhome = expandEnvVar("JAVA_HOME"); - if (jhome.isEmpty()) { - return QString(); - } -#ifdef QT_DEBUG - qDebug() << "JAVA_HOME is" << jhome; -#endif - exe = jhome.append(QDir::separator()) - .append("bin") - .append(QDir::separator()) - .append(name); - return QFile::exists(exe) ? exe : QString(); + return findInPath(name); } int ProcessUtils::javaHeapSize() @@ -161,6 +101,9 @@ int ProcessUtils::javaHeapSize() ProcessResult ProcessUtils::runCommand(const QString &exe, const QStringList &args, const int timeout) { +#ifdef QT_DEBUG + qDebug() << "Running" << exe << args; +#endif ProcessOutput::instance()->emitCommandStarting(exe, args); QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); diff --git a/sources/processutils.h b/sources/processutils.h index 6ee59cb..793c1e8 100644 --- a/sources/processutils.h +++ b/sources/processutils.h @@ -31,7 +31,6 @@ class ProcessUtils public: static QString adbExe(); static QString apktoolJar(); - static QString expandEnvVar(const QString &name); static QString findInPath(const QString &exe); static QString jadxExe(); static QString javaExe();