Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make loading of languages consistent across invocation types #3313

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 46 additions & 39 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,45 @@ QSharedMemory* guiMutexLock()
return shm;
}

QTranslator translator, qtTranslator;

void configureApp(bool gui)
{
if (gui) {
QApplication::setStyle(new StyleOverride);
}

// Configure translations
for (const QString& path : PathInfo::translationsPaths()) {
bool match = translator.load(QLocale(),
QStringLiteral("Internationalization"),
QStringLiteral("_"),
path);
if (match) {
break;
}
}

qtTranslator.load(QLocale::system(),
"qt",
"_",
QLibraryInfo::location(QLibraryInfo::TranslationsPath));

auto app = QCoreApplication::instance();
app->installTranslator(&translator);
app->installTranslator(&qtTranslator);
app->setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true);
}

// TODO find a way so we don't have to do this
/// Recreate the application as a QApplication
void reinitializeAsQApplication(int argc, char* argv[])
{
delete QCoreApplication::instance();
new QApplication(argc, argv);
configureApp(true);
}

int main(int argc, char* argv[])
{
#ifdef Q_OS_LINUX
Expand All @@ -105,31 +144,7 @@ int main(int argc, char* argv[])
#else
QtSingleApplication app(argc, argv);
#endif
QApplication::setStyle(new StyleOverride);

QTranslator translator, qtTranslator;
QStringList trPaths = PathInfo::translationsPaths();

for (const QString& path : trPaths) {
bool match = translator.load(QLocale(),
QStringLiteral("Internationalization"),
QStringLiteral("_"),
path);
if (match) {
break;
}
}

qtTranslator.load(
QLocale::system(),
"qt",
"_",
QLibraryInfo::location(QLibraryInfo::TranslationsPath));

qApp->installTranslator(&translator);
qApp->installTranslator(&qtTranslator);
qApp->setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true);

configureApp(true);
auto c = Flameshot::instance();
FlameshotDaemon::start();

Expand All @@ -151,6 +166,7 @@ int main(int argc, char* argv[])
* CLI parsing |
* ------------*/
new QCoreApplication(argc, argv);
configureApp(false);
CommandLineParser parser;
// Add description
parser.setDescription(
Expand Down Expand Up @@ -344,14 +360,12 @@ int main(int argc, char* argv[])
Flameshot::setOrigin(Flameshot::CLI);
if (parser.isSet(helpOption) || parser.isSet(versionOption)) {
} else if (parser.isSet(launcherArgument)) { // LAUNCHER
delete qApp;
new QApplication(argc, argv);
reinitializeAsQApplication(argc, argv);
Flameshot* flameshot = Flameshot::instance();
flameshot->launcher();
qApp->exec();
} else if (parser.isSet(guiArgument)) { // GUI
delete qApp;
new QApplication(argc, argv);
reinitializeAsQApplication(argc, argv);
// Prevent multiple instances of 'flameshot gui' from running if not
// configured to do so.
if (!ConfigHandler().allowMultipleGuiInstances()) {
Expand Down Expand Up @@ -414,10 +428,7 @@ int main(int argc, char* argv[])
}
requestCaptureAndWait(req);
} else if (parser.isSet(fullArgument)) { // FULL
// Recreate the application as a QApplication
// TODO find a way so we don't have to do this
delete qApp;
new QApplication(argc, argv);
reinitializeAsQApplication(argc, argv);

// Option values
QString path = parser.value(pathOption);
Expand Down Expand Up @@ -452,10 +463,7 @@ int main(int argc, char* argv[])
}
requestCaptureAndWait(req);
} else if (parser.isSet(screenArgument)) { // SCREEN
// Recreate the application as a QApplication
// TODO find a way so we don't have to do this
delete qApp;
new QApplication(argc, argv);
reinitializeAsQApplication(argc, argv);

QString numberStr = parser.value(screenNumberOption);
// Option values
Expand Down Expand Up @@ -526,8 +534,7 @@ int main(int argc, char* argv[])
}
if (!someFlagSet) {
// Open gui when no options are given
delete qApp;
new QApplication(argc, argv);
reinitializeAsQApplication(argc, argv);
QObject::connect(
qApp, &QApplication::lastWindowClosed, qApp, &QApplication::quit);
Flameshot::instance()->config();
Expand Down
Loading