mirror-chatterino2/src/RunGui.cpp

235 lines
6.4 KiB
C++
Raw Normal View History

2018-08-02 14:23:27 +02:00
#include "RunGui.hpp"
#include <QApplication>
#include <QFile>
#include <QPalette>
#include <QStyleFactory>
2019-09-22 15:32:36 +02:00
#include <Qt>
#include <QtConcurrent>
2019-09-22 15:32:36 +02:00
#include <csignal>
2018-08-02 14:23:27 +02:00
#include "Application.hpp"
#include "common/Modes.hpp"
2018-08-02 14:23:27 +02:00
#include "common/NetworkManager.hpp"
#include "singletons/Paths.hpp"
2019-09-22 10:53:39 +02:00
#include "singletons/Resources.hpp"
2019-09-22 15:32:36 +02:00
#include "singletons/Settings.hpp"
2018-08-02 14:23:27 +02:00
#include "singletons/Updates.hpp"
#include "util/CombinePath.hpp"
2018-08-02 14:23:27 +02:00
#include "widgets/dialogs/LastRunCrashDialog.hpp"
#ifdef USEWINSDK
# include "util/WindowsHelper.hpp"
#endif
2018-08-02 14:23:27 +02:00
#ifdef C_USE_BREAKPAD
2018-08-15 22:46:20 +02:00
# include <QBreakpadHandler.h>
2018-08-02 14:23:27 +02:00
#endif
namespace chatterino {
namespace {
2018-08-15 22:46:20 +02:00
void installCustomPalette()
{
// borrowed from
// https://stackoverflow.com/questions/15035767/is-the-qt-5-dark-fusion-theme-available-for-windows
auto dark = qApp->palette();
dark.setColor(QPalette::Window, QColor(22, 22, 22));
dark.setColor(QPalette::WindowText, Qt::white);
dark.setColor(QPalette::Text, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::WindowText,
QColor(127, 127, 127));
dark.setColor(QPalette::Base, QColor("#333"));
dark.setColor(QPalette::AlternateBase, QColor("#444"));
dark.setColor(QPalette::ToolTipBase, Qt::white);
dark.setColor(QPalette::ToolTipText, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::Text,
QColor(127, 127, 127));
dark.setColor(QPalette::Dark, QColor(35, 35, 35));
dark.setColor(QPalette::Shadow, QColor(20, 20, 20));
dark.setColor(QPalette::Button, QColor(70, 70, 70));
dark.setColor(QPalette::ButtonText, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::ButtonText,
QColor(127, 127, 127));
dark.setColor(QPalette::BrightText, Qt::red);
dark.setColor(QPalette::Link, QColor(42, 130, 218));
dark.setColor(QPalette::Highlight, QColor(42, 130, 218));
dark.setColor(QPalette::Disabled, QPalette::Highlight,
QColor(80, 80, 80));
dark.setColor(QPalette::HighlightedText, Qt::white);
dark.setColor(QPalette::Disabled, QPalette::HighlightedText,
QColor(127, 127, 127));
qApp->setPalette(dark);
}
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
void initQt()
{
// set up the QApplication flags
QApplication::setAttribute(Qt::AA_Use96Dpi, true);
2018-08-02 14:23:27 +02:00
#ifdef Q_OS_WIN32
2018-08-15 22:46:20 +02:00
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
2018-08-02 14:23:27 +02:00
#endif
2018-08-15 22:46:20 +02:00
QApplication::setStyle(QStyleFactory::create("Fusion"));
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
installCustomPalette();
}
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
void showLastCrashDialog()
{
2019-08-18 20:14:00 +02:00
//#ifndef C_DISABLE_CRASH_DIALOG
// LastRunCrashDialog dialog;
// switch (dialog.exec())
// {
// case QDialog::Accepted:
// {
// };
// break;
// default:
// {
// _exit(0);
// }
// }
//#endif
2018-08-15 22:46:20 +02:00
}
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
void createRunningFile(const QString &path)
{
QFile runningFile(path);
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
runningFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
runningFile.flush();
runningFile.close();
}
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
void removeRunningFile(const QString &path)
{
QFile::remove(path);
}
2019-09-22 15:32:36 +02:00
std::chrono::steady_clock::time_point signalsInitTime;
bool restartOnSignal = false;
[[noreturn]] void handleSignal(int signum)
{
using namespace std::chrono_literals;
2019-09-22 16:14:16 +02:00
if (restartOnSignal &&
std::chrono::steady_clock::now() - signalsInitTime > 30s)
{
QProcess proc;
proc.setProgram(QApplication::applicationFilePath());
proc.setArguments({"--crash-recovery"});
proc.startDetached();
}
2019-09-22 15:32:36 +02:00
_exit(signum);
}
// We want to restart chatterino when it crashes and the setting is set to
// true.
void initSignalHandler()
{
2019-09-22 16:14:16 +02:00
#ifndef C_DEBUG
2019-09-22 15:32:36 +02:00
signalsInitTime = std::chrono::steady_clock::now();
signal(SIGSEGV, handleSignal);
2019-09-22 16:14:16 +02:00
#endif
2019-09-22 15:32:36 +02:00
}
// We delete cache files that haven't been modified in 14 days. This strategy may be
// improved in the future.
void clearCache(const QDir &dir)
{
qDebug() << "[Cache] cleared cache";
QStringList toBeRemoved;
for (auto &&info : dir.entryInfoList(QDir::Files))
{
if (info.lastModified().addDays(14) < QDateTime::currentDateTime())
{
toBeRemoved << info.absoluteFilePath();
}
}
for (auto &&path : toBeRemoved)
{
qDebug() << path << QFile(path).remove();
}
}
2018-08-02 14:23:27 +02:00
} // namespace
void runGui(QApplication &a, Paths &paths, Settings &settings)
{
2018-08-07 08:12:44 +02:00
initQt();
2019-09-22 10:53:39 +02:00
initResources();
2019-09-22 15:32:36 +02:00
initSignalHandler();
settings.restartOnCrash.connect(
[](const bool &value) { restartOnSignal = value; });
2019-09-22 10:53:39 +02:00
auto thread = std::thread([dir = paths.miscDirectory] {
{
auto path = combinePath(dir, "Update.exe");
if (QFile::exists(path))
{
QFile::remove(path);
}
}
{
auto path = combinePath(dir, "update.zip");
if (QFile::exists(path))
{
QFile::remove(path);
}
}
});
// Clear the cache 1 minute after start.
QTimer::singleShot(60 * 1000, [cachePath = paths.cacheDirectory()] {
QtConcurrent::run([cachePath]() { clearCache(cachePath); });
});
2018-08-02 14:23:27 +02:00
chatterino::NetworkManager::init();
chatterino::Updates::instance().checkForUpdates();
2018-08-02 14:23:27 +02:00
#ifdef C_USE_BREAKPAD
QBreakpadInstance.setDumpPath(getPaths()->settingsFolderPath + "/Crashes");
2018-08-02 14:23:27 +02:00
#endif
// Running file
2018-08-06 21:17:03 +02:00
auto runningPath =
paths.miscDirectory + "/running_" + paths.applicationFilePathHash;
2018-08-02 14:23:27 +02:00
2018-10-21 13:43:02 +02:00
if (QFile::exists(runningPath))
{
2018-08-02 14:23:27 +02:00
showLastCrashDialog();
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-02 14:23:27 +02:00
createRunningFile(runningPath);
}
Application app(settings, paths);
app.initialize(settings, paths);
app.run(a);
app.save();
removeRunningFile(runningPath);
pajlada::Settings::SettingManager::gSave();
chatterino::NetworkManager::deinit();
#ifdef USEWINSDK
2018-10-21 13:43:02 +02:00
// flushing windows clipboard to keep copied messages
flushClipboard();
#endif
2018-08-02 14:23:27 +02:00
_exit(0);
}
} // namespace chatterino