File indexing completed on 2023-05-30 12:24:18

0001 /*
0002   This file is part of Lokalize
0003 
0004   SPDX-FileCopyrightText: 2007-2014 Nick Shaforostoff <shafff@ukr.net>
0005   SPDX-FileCopyrightText: 2018-2019 Simon Depiets <sdepiets@gmail.com>
0006 
0007   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 */
0009 
0010 
0011 #include "lokalize_debug.h"
0012 
0013 #include "project.h"
0014 #include "prefs.h"
0015 #include "prefs_lokalize.h"
0016 
0017 #include "version.h"
0018 #include "projecttab.h"
0019 #include "projectmodel.h"
0020 
0021 #include "lokalizemainwindow.h"
0022 #include "stemming.h"
0023 
0024 #include "jobs.h"
0025 #include "catalogstring.h"
0026 #include "pos.h"
0027 
0028 #include <QMetaType>
0029 #include <QString>
0030 #include <QFile>
0031 #include <QFileInfo>
0032 #include <QDesktopWidget>
0033 #include <QApplication>
0034 #include <QCommandLineParser>
0035 #include <QCommandLineOption>
0036 
0037 #include <KCrash>
0038 #include <KDBusService>
0039 
0040 #include <klocalizedstring.h>
0041 
0042 #include <kaboutdata.h>
0043 #include "editortab.h"
0044 
0045 int main(int argc, char **argv)
0046 {
0047     TM::threadPool()->setMaxThreadCount(1);
0048     TM::threadPool()->setExpiryTimeout(-1);
0049     QThreadPool::globalInstance()->setMaxThreadCount(1);
0050     // Fixes blurry icons with fractional scaling, see https://phabricator.kde.org/D29376
0051     QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0052     QApplication app(argc, argv);
0053     KCrash::initialize();
0054     KLocalizedString::setApplicationDomain("lokalize");
0055     QCommandLineParser parser;
0056     KAboutData about(QStringLiteral("lokalize"), i18nc("@title", "Lokalize"), LOKALIZE_VERSION, i18n("Computer-aided translation system.\nDo not translate what had already been translated."),
0057                      KAboutLicense::GPL, i18nc("@info:credit", "(c) 2018-2019 Simon Depiets\n(c) 2007-2015 Nick Shaforostoff\n(c) 1999-2006 The KBabel developers") /*, KLocalizedString(), 0, "shafff@ukr.net"*/);
0058     about.addAuthor(i18n("Nick Shaforostoff"), QString(), QStringLiteral("shaforostoff@gmail.com"));
0059     about.addCredit(i18n("Google Inc."), i18n("sponsored development as part of Google Summer Of Code program"), QString(), QStringLiteral("https://google.com"));
0060     about.addCredit(i18n("NLNet Foundation"), i18n("sponsored XLIFF-related work"), QString(), QStringLiteral("https://nlnet.nl/"));
0061     about.addCredit(i18n("Translate-toolkit"), i18n("provided excellent cross-format converting scripts"), QString(), QStringLiteral("https://toolkit.translatehouse.org"));
0062     about.addCredit(i18n("LanguageTool"), i18n("grammar, style and spell checker"), QString(), QStringLiteral("https://toolkit.translatehouse.org"));
0063     about.addCredit(i18n("Viesturs Zarins"), i18n("project tree merging translation+templates"), QStringLiteral("https://languagetool.org"), QString());
0064     about.addCredit(i18n("Stephan Johach"), i18n("bug fixing patches"), QStringLiteral("hunsum@gmx.de"));
0065     about.addCredit(i18n("Chusslove Illich"), i18n("bug fixing patches"), QStringLiteral("caslav.ilic@gmx.net"));
0066     about.addCredit(i18n("Jure Repinc"), i18n("testing and bug fixing"), QStringLiteral("jlp@holodeck1.com"));
0067     about.addCredit(i18n("Stefan Asserhall"), i18n("patches"), QStringLiteral("stefan.asserhall@comhem.se"));
0068     about.addCredit(i18n("Papp Laszlo"), i18n("bug fixing patches"), QStringLiteral("djszapi@archlinux.us"));
0069     about.addCredit(i18n("Albert Astals Cid"), i18n("XLIFF improvements"), QStringLiteral("aacid@kde.org"));
0070     about.addCredit(i18n("Simon Depiets"), i18n("bug fixing and improvements"), QStringLiteral("sdepiets@gmail.com"));
0071     KAboutData::setApplicationData(about);
0072     about.setupCommandLine(&parser);
0073     //parser.addOption(QCommandLineOption(QStringList() <<  QLatin1String("source"), i18n( "Source for the merge mode" ), QLatin1String("URL")));
0074     parser.addOption(QCommandLineOption(QStringList() <<  QStringLiteral("noprojectscan"), i18n("Do not scan files of the project.")));
0075     parser.addOption(QCommandLineOption(QStringList() <<  QStringLiteral("project"), i18n("Load specified project."), QStringLiteral("filename")));
0076     parser.addOption(QCommandLineOption(QStringList() <<  QStringLiteral("+[URL]"), i18n("Document to open")));
0077     parser.process(app);
0078     about.processCommandLine(&parser);
0079 
0080     //qCDebug(LOKALIZE_LOG) is important as it avoids compile 'optimization'.
0081     qCDebug(LOKALIZE_LOG) << qRegisterMetaType<DocPosition>();
0082     qCDebug(LOKALIZE_LOG) << qRegisterMetaType<DocPos>();
0083     qCDebug(LOKALIZE_LOG) << qRegisterMetaType<InlineTag>();
0084     qCDebug(LOKALIZE_LOG) << qRegisterMetaType<CatalogString>();
0085     qRegisterMetaTypeStreamOperators<InlineTag>("InlineTag");
0086     qRegisterMetaTypeStreamOperators<CatalogString>("CatalogString");
0087     qAddPostRoutine(&cleanupSpellers);
0088 
0089     const KDBusService dbusService(KDBusService::Multiple);
0090 
0091     // see if we are starting with session management
0092     if (app.isSessionRestored())
0093         kRestoreMainWindows<LokalizeMainWindow>();
0094     else {
0095         // no session.. just start up normally
0096 
0097         QString projectFilePath = parser.value(QStringLiteral("project"));
0098 
0099         QVector<QString> urls;
0100         const auto filePaths = parser.positionalArguments();
0101         for (const QString& filePath : filePaths)
0102             if (filePath.endsWith(QLatin1String(".lokalize")))
0103                 projectFilePath = filePath;
0104             else if (QFileInfo::exists(filePath))
0105                 urls.append(filePath);
0106 
0107         if (projectFilePath.length()) {
0108             // load needs an absolute path
0109             // FIXME: I do not know how to handle urls here
0110             // bug 245546 regarding symlinks
0111             QFileInfo projectFileInfo(projectFilePath);
0112             projectFilePath = projectFileInfo.canonicalFilePath();
0113             if (projectFilePath.isEmpty())
0114                 projectFilePath = projectFileInfo.absoluteFilePath();
0115             Project::instance()->load(projectFilePath);
0116         }
0117         LokalizeMainWindow* lmw = new LokalizeMainWindow;
0118         SettingsController::instance()->setMainWindowPtr(lmw);
0119         lmw->show();
0120 
0121         if (urls.size())
0122             new DelayedFileOpener(urls, lmw);
0123 
0124         //Project::instance()->model()->setCompleteScan(parser.isSet("noprojectscan"));// TODO: negate check (and ensure nobody passes the no-op --noprojectscan argument)
0125     }
0126     int code = app.exec();
0127 
0128     QThreadPool::globalInstance()->clear();
0129     TM::cancelAllJobs();
0130     TM::threadPool()->clear();
0131     TM::threadPool()->waitForDone(1000);
0132     Project::instance()->model()->threadPool()->clear();
0133 
0134     if (SettingsController::instance()->dirty) //for config changes done w/o config dialog
0135         Settings::self()->save();
0136 
0137     if (Project::instance()->isLoaded())
0138         Project::instance()->save();
0139 
0140     qCDebug(LOKALIZE_LOG) << "Finishing Project jobs...";
0141     qCDebug(LOKALIZE_LOG) << "Finishing TM jobs...";
0142     int secs = 5;
0143     while (--secs >= 0) {
0144         Project::instance()->model()->threadPool()->waitForDone(1000);
0145         TM::threadPool()->waitForDone(1000);
0146         QThreadPool::globalInstance()->waitForDone(1000);
0147         //qCDebug(LOKALIZE_LOG)<<"QCoreApplication::processEvents()...";
0148         QCoreApplication::processEvents();
0149         QCoreApplication::sendPostedEvents(nullptr, 0);
0150     }
0151     return code;
0152 }
0153 
0154