| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2012 CSSlayer <[email protected]> |
| 3 |
* SPDX-FileCopyrightText: 2017~2017 xzhao <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "wrapperapp.h" |
| 9 |
#include "fcitxqtconfiguifactory.h" |
| 10 |
#include "fcitxqtconfiguiwidget.h" |
| 11 |
#include "mainwindow.h" |
| 12 |
#include <QApplication> |
| 13 |
#include <QCommandLineParser> |
| 14 |
#include <QDebug> |
| 15 |
#include <QLatin1String> |
| 16 |
#include <QWindow> |
| 17 |
#include <Qt> |
| 18 |
#include <QtGlobal> |
| 19 |
#include <fcitx-utils/i18n.h> |
| 20 |
|
| 21 |
namespace fcitx { |
| 22 |
|
| 23 |
WrapperApp::WrapperApp(int &argc, char **argv) |
| 24 |
: QApplication(argc, argv), factory_(new FcitxQtConfigUIFactory(this)), |
| 25 |
mainWindow_(nullptr) { |
| 26 |
setApplicationName(QLatin1String( |
| 27 |
"fcitx5-qt" QT_STRINGIFY(QT_VERSION_MAJOR) "-gui-wrapper")); |
| 28 |
setApplicationVersion(QLatin1String(FCITX5_QT_VERSION)); |
| 29 |
setOrganizationDomain("fcitx.org"); |
| 30 |
} |
| 31 |
|
| 32 |
void WrapperApp::init() { |
| 33 |
QCommandLineParser parser; |
| 34 |
parser.setApplicationDescription(_("A launcher for Fcitx Gui plugin.")); |
| 35 |
parser.addHelpOption(); |
| 36 |
parser.addOptions({ |
| 37 |
{{"w", "winid"}, _("Parent window ID"), _("winid")}, |
| 38 |
{{"t", "test"}, _("Test if config exists")}, |
| 39 |
}); |
| 40 |
parser.addPositionalArgument(_("path"), _("Config path")); |
| 41 |
parser.process(*this); |
| 42 |
|
| 43 |
auto args = parser.positionalArguments(); |
| 44 |
if (args.empty()) { |
| 45 |
qWarning("Missing path argument."); |
| 46 |
::exit(1); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
QString path = args[0]; |
| 51 |
if (!path.startsWith("fcitx://config/addon/")) { |
| 52 |
path.prepend("fcitx://config/addon/"); |
| 53 |
} |
| 54 |
if (parser.isSet("test")) { |
| 55 |
if (factory_->test(path)) { |
| 56 |
::exit(0); |
| 57 |
} else { |
| 58 |
::exit(1); |
| 59 |
} |
| 60 |
} else { |
| 61 |
WId winid = 0; |
| 62 |
bool ok = false; |
| 63 |
if (parser.isSet("winid")) { |
| 64 |
winid = parser.value("winid").toLong(&ok, 0); |
| 65 |
} |
| 66 |
FcitxQtConfigUIWidget *widget = factory_->create(path); |
| 67 |
if (!widget) { |
| 68 |
qWarning("Could not find plugin for file."); |
| 69 |
QMetaObject::invokeMethod(this, "errorExit", Qt::QueuedConnection); |
| 70 |
return; |
| 71 |
} |
| 72 |
mainWindow_ = new MainWindow(path, widget); |
| 73 |
if (ok && winid) { |
| 74 |
mainWindow_->setParentWindow(winid); |
| 75 |
mainWindow_->setWindowModality(Qt::WindowModal); |
| 76 |
mainWindow_->setWindowFlag(Qt::Dialog); |
| 77 |
} |
| 78 |
QMetaObject::invokeMethod(this, "run", Qt::QueuedConnection); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void WrapperApp::run() { mainWindow_->show(); } |
| 83 |
|
| 84 |
WrapperApp::~WrapperApp() { delete mainWindow_; } |
| 85 |
|
| 86 |
void WrapperApp::errorExit() { exit(1); } |
| 87 |
} // namespace fcitx |