Skip to content

Commit

Permalink
Better (adb, java) exe lookup, auto-restart on theme changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavpandeyvpz committed May 9, 2019
1 parent e2b4732 commit 004720b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 92 deletions.
15 changes: 15 additions & 0 deletions sources/appearancesettingswidget.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include <QApplication>
#include <QFontComboBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QSettings>
#include "appearancesettingswidget.h"

#define CODE_RESTART 60600

AppearanceSettingsWidget::AppearanceSettingsWidget(QWidget *parent)
: QWidget(parent)
{
Expand Down Expand Up @@ -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);
}
}
}
24 changes: 17 additions & 7 deletions sources/binarysettingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
Expand Down
32 changes: 19 additions & 13 deletions sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions sources/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ MainWindow::MainWindow(const QMap<QString, QString> &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;
}
Expand Down
85 changes: 14 additions & 71 deletions sources/processutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion sources/processutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 004720b

Please sign in to comment.