2018-08-02 14:23:27 +02:00
|
|
|
#include "RunGui.hpp"
|
|
|
|
|
|
|
|
#include "Application.hpp"
|
2020-09-26 16:03:51 +02:00
|
|
|
#include "common/Args.hpp"
|
2019-10-05 16:40:04 +02:00
|
|
|
#include "common/Modes.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "common/NetworkManager.hpp"
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2023-12-24 15:38:58 +01:00
|
|
|
#include "singletons/CrashHandler.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#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"
|
2019-08-25 22:58:19 +02:00
|
|
|
#include "util/CombinePath.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "widgets/dialogs/LastRunCrashDialog.hpp"
|
|
|
|
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QStyleFactory>
|
|
|
|
#include <Qt>
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
|
|
|
#include <csignal>
|
2023-10-17 03:50:18 +02:00
|
|
|
#include <tuple>
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
|
2018-10-09 18:37:51 +02:00
|
|
|
#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
|
|
|
|
|
2021-10-02 12:58:28 +02:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
# include "corefoundation/CFBundle.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
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::Base, QColor("#333"));
|
|
|
|
dark.setColor(QPalette::AlternateBase, QColor("#444"));
|
|
|
|
dark.setColor(QPalette::ToolTipBase, Qt::white);
|
|
|
|
dark.setColor(QPalette::ToolTipText, Qt::white);
|
|
|
|
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::BrightText, Qt::red);
|
|
|
|
dark.setColor(QPalette::Link, QColor(42, 130, 218));
|
|
|
|
dark.setColor(QPalette::Highlight, QColor(42, 130, 218));
|
2023-03-29 10:21:35 +02:00
|
|
|
dark.setColor(QPalette::HighlightedText, Qt::white);
|
|
|
|
dark.setColor(QPalette::PlaceholderText, QColor(127, 127, 127));
|
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
dark.setColor(QPalette::Disabled, QPalette::Highlight,
|
|
|
|
QColor(80, 80, 80));
|
|
|
|
dark.setColor(QPalette::Disabled, QPalette::HighlightedText,
|
|
|
|
QColor(127, 127, 127));
|
2023-03-29 10:21:35 +02:00
|
|
|
dark.setColor(QPalette::Disabled, QPalette::ButtonText,
|
|
|
|
QColor(127, 127, 127));
|
|
|
|
dark.setColor(QPalette::Disabled, QPalette::Text,
|
|
|
|
QColor(127, 127, 127));
|
|
|
|
dark.setColor(QPalette::Disabled, QPalette::WindowText,
|
|
|
|
QColor(127, 127, 127));
|
2018-08-15 22:46:20 +02:00
|
|
|
|
|
|
|
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);
|
2023-12-16 12:24:28 +01:00
|
|
|
#if defined(Q_OS_WIN32) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
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
|
|
|
|
2023-04-29 17:45:08 +02:00
|
|
|
#ifndef Q_OS_MAC
|
2022-01-30 16:08:02 +01:00
|
|
|
QApplication::setWindowIcon(QIcon(":/icon.ico"));
|
2023-04-29 17:45:08 +02:00
|
|
|
#endif
|
2022-01-30 16:08:02 +01:00
|
|
|
|
2023-07-22 13:36:52 +02:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
// On the Mac/Cocoa platform this attribute is enabled by default
|
|
|
|
// We override it to ensure shortcuts show in context menus on that platform
|
|
|
|
QApplication::setAttribute(Qt::AA_DontShowShortcutsInContextMenus,
|
|
|
|
false);
|
|
|
|
#endif
|
|
|
|
|
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()
|
|
|
|
{
|
2023-12-24 15:38:58 +01:00
|
|
|
auto *dialog = new LastRunCrashDialog;
|
|
|
|
// Use exec() over open() to block the app from being loaded
|
|
|
|
// and to be able to set the safe mode.
|
|
|
|
dialog->exec();
|
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;
|
|
|
|
|
|
|
|
[[noreturn]] void handleSignal(int signum)
|
|
|
|
{
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2023-12-24 15:38:58 +01:00
|
|
|
if (std::chrono::steady_clock::now() - signalsInitTime > 30s &&
|
|
|
|
getIApp()->getCrashHandler()->shouldRecover())
|
2019-09-22 16:14:16 +02:00
|
|
|
{
|
|
|
|
QProcess proc;
|
2021-10-02 12:58:28 +02:00
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
// On macOS, programs are bundled into ".app" Application bundles,
|
2021-10-17 15:06:58 +02:00
|
|
|
// when restarting Chatterino that bundle should be opened with the "open"
|
2021-10-02 12:58:28 +02:00
|
|
|
// terminal command instead of directly starting the underlying executable,
|
|
|
|
// as those are 2 different things for the OS and i.e. do not use
|
2021-10-17 15:06:58 +02:00
|
|
|
// the same dock icon (resulting in a second Chatterino icon on restarting)
|
2021-10-02 12:58:28 +02:00
|
|
|
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
|
|
CFStringRef macPath =
|
|
|
|
CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
|
|
|
|
const char *pathPtr =
|
|
|
|
CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
|
|
|
|
|
|
|
|
proc.setProgram("open");
|
2021-10-20 08:34:19 +02:00
|
|
|
proc.setArguments({pathPtr, "-n", "--args", "--crash-recovery"});
|
2021-10-02 12:58:28 +02:00
|
|
|
|
|
|
|
CFRelease(appUrlRef);
|
|
|
|
CFRelease(macPath);
|
|
|
|
#else
|
2019-09-22 16:14:16 +02:00
|
|
|
proc.setProgram(QApplication::applicationFilePath());
|
|
|
|
proc.setArguments({"--crash-recovery"});
|
2021-10-02 12:58:28 +02:00
|
|
|
#endif
|
|
|
|
|
2019-09-22 16:14:16 +02:00
|
|
|
proc.startDetached();
|
|
|
|
}
|
2019-09-22 15:32:36 +02:00
|
|
|
|
|
|
|
_exit(signum);
|
|
|
|
}
|
|
|
|
|
2021-10-17 15:06:58 +02:00
|
|
|
// We want to restart Chatterino when it crashes and the setting is set to
|
2019-09-22 15:32:36 +02:00
|
|
|
// true.
|
|
|
|
void initSignalHandler()
|
|
|
|
{
|
2023-02-12 20:36:58 +01:00
|
|
|
#if defined(NDEBUG) && !defined(CHATTERINO_WITH_CRASHPAD)
|
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
|
|
|
}
|
2019-10-07 22:14:00 +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)
|
|
|
|
{
|
2023-10-17 03:50:18 +02:00
|
|
|
size_t deletedCount = 0;
|
|
|
|
for (const auto &info : dir.entryInfoList(QDir::Files))
|
2019-10-07 22:14:00 +02:00
|
|
|
{
|
|
|
|
if (info.lastModified().addDays(14) < QDateTime::currentDateTime())
|
|
|
|
{
|
2021-07-03 13:40:24 +02:00
|
|
|
bool res = QFile(info.absoluteFilePath()).remove();
|
|
|
|
if (res)
|
2023-10-17 03:50:18 +02:00
|
|
|
{
|
2021-07-03 13:40:24 +02:00
|
|
|
++deletedCount;
|
2023-10-17 03:50:18 +02:00
|
|
|
}
|
2019-10-07 22:14:00 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-17 03:50:18 +02:00
|
|
|
qCDebug(chatterinoCache)
|
|
|
|
<< "Deleted" << deletedCount << "files in" << dir.path();
|
2019-10-07 22:14:00 +02:00
|
|
|
}
|
2023-02-19 23:20:41 +01:00
|
|
|
|
|
|
|
// We delete all but the five most recent crashdumps. This strategy may be
|
|
|
|
// improved in the future.
|
|
|
|
void clearCrashes(QDir dir)
|
|
|
|
{
|
|
|
|
// crashpad crashdumps are stored inside the Crashes/report directory
|
|
|
|
if (!dir.cd("reports"))
|
|
|
|
{
|
|
|
|
// no reports directory exists = no files to delete
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.setNameFilters({"*.dmp"});
|
|
|
|
|
|
|
|
size_t deletedCount = 0;
|
|
|
|
// TODO: use std::views::drop once supported by all compilers
|
|
|
|
size_t filesToSkip = 5;
|
|
|
|
for (auto &&info : dir.entryInfoList(QDir::Files, QDir::Time))
|
|
|
|
{
|
|
|
|
if (filesToSkip > 0)
|
|
|
|
{
|
|
|
|
filesToSkip--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (QFile(info.absoluteFilePath()).remove())
|
|
|
|
{
|
|
|
|
deletedCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qCDebug(chatterinoApp) << "Deleted" << deletedCount << "crashdumps";
|
|
|
|
}
|
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();
|
|
|
|
|
2023-12-24 15:38:58 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (getArgs().crashRecovery)
|
|
|
|
{
|
|
|
|
showLastCrashDialog();
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-22 10:53:39 +02:00
|
|
|
|
2019-08-25 22:58:19 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-07 22:14:00 +02:00
|
|
|
// Clear the cache 1 minute after start.
|
2023-02-19 23:20:41 +01:00
|
|
|
QTimer::singleShot(60 * 1000, [cachePath = paths.cacheDirectory(),
|
2023-10-17 03:50:18 +02:00
|
|
|
crashDirectory = paths.crashdumpDirectory,
|
|
|
|
avatarPath = paths.twitchProfileAvatars] {
|
|
|
|
std::ignore = QtConcurrent::run([cachePath] {
|
2020-11-08 12:02:19 +01:00
|
|
|
clearCache(cachePath);
|
|
|
|
});
|
2023-10-17 03:50:18 +02:00
|
|
|
std::ignore = QtConcurrent::run([avatarPath] {
|
|
|
|
clearCache(avatarPath);
|
|
|
|
});
|
|
|
|
std::ignore = QtConcurrent::run([crashDirectory] {
|
2023-02-19 23:20:41 +01:00
|
|
|
clearCrashes(crashDirectory);
|
|
|
|
});
|
2019-10-07 22:14:00 +02:00
|
|
|
});
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
chatterino::NetworkManager::init();
|
2019-10-07 22:42:34 +02:00
|
|
|
chatterino::Updates::instance().checkForUpdates();
|
2018-08-02 14:23:27 +02:00
|
|
|
|
|
|
|
Application app(settings, paths);
|
|
|
|
app.initialize(settings, paths);
|
|
|
|
app.run(a);
|
|
|
|
app.save();
|
|
|
|
|
2020-09-26 16:03:51 +02:00
|
|
|
if (!getArgs().dontSaveSettings)
|
|
|
|
{
|
|
|
|
pajlada::Settings::SettingManager::gSave();
|
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
|
|
|
chatterino::NetworkManager::deinit();
|
|
|
|
|
2018-10-09 18:37:51 +02:00
|
|
|
#ifdef USEWINSDK
|
|
|
|
// flushing windows clipboard to keep copied messages
|
|
|
|
flushClipboard();
|
|
|
|
#endif
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
} // namespace chatterino
|