| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2012~2017 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 "mainwindow.h" |
| 9 |
#include "fcitxqtconfiguifactory.h" |
| 10 |
#include "fcitxqtcontrollerproxy.h" |
| 11 |
#include "fcitxqtwatcher.h" |
| 12 |
#include <QCloseEvent> |
| 13 |
#include <QDebug> |
| 14 |
#include <QLocale> |
| 15 |
#include <QMessageBox> |
| 16 |
#include <QPushButton> |
| 17 |
#include <QShowEvent> |
| 18 |
#include <QWidget> |
| 19 |
#include <QWindow> |
| 20 |
#include <fcitx-utils/i18n.h> |
| 21 |
|
| 22 |
namespace fcitx { |
| 23 |
|
| 24 |
MainWindow::MainWindow(const QString &path, FcitxQtConfigUIWidget *pluginWidget, |
| 25 |
QWidget *parent) |
| 26 |
: QWidget(parent), path_(path), watcher_(new FcitxQtWatcher(this)), |
| 27 |
pluginWidget_(pluginWidget), proxy_(0) { |
| 28 |
setupUi(this); |
| 29 |
watcher_->setConnection(QDBusConnection::sessionBus()); |
| 30 |
verticalLayout->insertWidget(0, pluginWidget_); |
| 31 |
buttonBox->button(QDialogButtonBox::Ok)->setText(_("&Ok")); |
| 32 |
buttonBox->button(QDialogButtonBox::Apply)->setText(_("&Apply")); |
| 33 |
buttonBox->button(QDialogButtonBox::Reset)->setText(_("&Reset")); |
| 34 |
buttonBox->button(QDialogButtonBox::Close)->setText(_("&Close")); |
| 35 |
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); |
| 36 |
buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false); |
| 37 |
buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false); |
| 38 |
setWindowIcon(QIcon::fromTheme(pluginWidget_->icon())); |
| 39 |
setWindowTitle(pluginWidget_->title()); |
| 40 |
|
| 41 |
connect(pluginWidget_, &FcitxQtConfigUIWidget::changed, this, |
| 42 |
&MainWindow::changed); |
| 43 |
if (pluginWidget_->asyncSave()) { |
| 44 |
connect(pluginWidget_, &FcitxQtConfigUIWidget::saveFinished, this, |
| 45 |
&MainWindow::saveFinished); |
| 46 |
} |
| 47 |
connect(pluginWidget_, &FcitxQtConfigUIWidget::saveSubConfig, this, |
| 48 |
&MainWindow::saveSubConfig); |
| 49 |
connect(buttonBox, &QDialogButtonBox::clicked, this, &MainWindow::clicked); |
| 50 |
connect(watcher_, &FcitxQtWatcher::availabilityChanged, this, |
| 51 |
&MainWindow::availabilityChanged); |
| 52 |
|
| 53 |
watcher_->watch(); |
| 54 |
} |
| 55 |
|
| 56 |
void MainWindow::availabilityChanged(bool avail) { |
| 57 |
if (!avail) { |
| 58 |
return; |
| 59 |
} |
| 60 |
if (proxy_) { |
| 61 |
delete proxy_; |
| 62 |
} |
| 63 |
proxy_ = new FcitxQtControllerProxy(watcher_->serviceName(), |
| 64 |
QLatin1String("/controller"), |
| 65 |
watcher_->connection(), this); |
| 66 |
} |
| 67 |
|
| 68 |
void MainWindow::clicked(QAbstractButton *button) { |
| 69 |
QDialogButtonBox::StandardButton standardButton = |
| 70 |
buttonBox->standardButton(button); |
| 71 |
if (standardButton == QDialogButtonBox::Apply || |
| 72 |
standardButton == QDialogButtonBox::Ok) { |
| 73 |
if (pluginWidget_->asyncSave()) |
| 74 |
pluginWidget_->setEnabled(false); |
| 75 |
if (standardButton == QDialogButtonBox::Ok) { |
| 76 |
closeAfterSave_ = true; |
| 77 |
} |
| 78 |
pluginWidget_->save(); |
| 79 |
if (!pluginWidget_->asyncSave()) |
| 80 |
saveFinished(); |
| 81 |
} else if (standardButton == QDialogButtonBox::Close) { |
| 82 |
qApp->quit(); |
| 83 |
} else if (standardButton == QDialogButtonBox::Reset) { |
| 84 |
pluginWidget_->load(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void MainWindow::saveFinished() { |
| 89 |
if (proxy_) { |
| 90 |
// Pass some arbitrary thing. |
| 91 |
auto watcher = new QDBusPendingCallWatcher( |
| 92 |
proxy_->SetConfig(path_, QDBusVariant(0)), this); |
| 93 |
connect(watcher, &QDBusPendingCallWatcher::finished, this, |
| 94 |
&MainWindow::saveFinishedPhase2); |
| 95 |
} else { |
| 96 |
saveFinishedPhase2(nullptr); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void MainWindow::saveFinishedPhase2(QDBusPendingCallWatcher *watcher) { |
| 101 |
if (watcher) { |
| 102 |
watcher->deleteLater(); |
| 103 |
} |
| 104 |
if (pluginWidget_->asyncSave()) { |
| 105 |
pluginWidget_->setEnabled(true); |
| 106 |
} |
| 107 |
if (!watcher || watcher->isError()) { |
| 108 |
QMessageBox::warning( |
| 109 |
this, _("Failed to notify Fcitx"), |
| 110 |
_("Failed to notify Fcitx about the configuration change.")); |
| 111 |
closeAfterSave_ = false; |
| 112 |
return; |
| 113 |
} |
| 114 |
if (closeAfterSave_) { |
| 115 |
qApp->quit(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void MainWindow::saveSubConfig(const QString &path) { |
| 120 |
if (proxy_) { |
| 121 |
// Pass some arbitrary thing. |
| 122 |
proxy_->SetConfig(path, QDBusVariant(0)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void MainWindow::changed(bool changed) { |
| 127 |
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(changed); |
| 128 |
buttonBox->button(QDialogButtonBox::Apply)->setEnabled(changed); |
| 129 |
buttonBox->button(QDialogButtonBox::Reset)->setEnabled(changed); |
| 130 |
} |
| 131 |
|
| 132 |
void MainWindow::setParentWindow(WId id) { wid_ = id; } |
| 133 |
|
| 134 |
void MainWindow::showEvent(QShowEvent *event) { |
| 135 |
if (!wid_) { |
| 136 |
return; |
| 137 |
} |
| 138 |
setAttribute(Qt::WA_NativeWindow, true); |
| 139 |
QWindow *subWindow = windowHandle(); |
| 140 |
Q_ASSERT(subWindow); |
| 141 |
|
| 142 |
QWindow *mainWindow = QWindow::fromWinId(wid_); |
| 143 |
wid_ = 0; |
| 144 |
if (!mainWindow) { |
| 145 |
// foreign windows not supported on all platforms |
| 146 |
return; |
| 147 |
} |
| 148 |
connect(this, &QObject::destroyed, mainWindow, &QObject::deleteLater); |
| 149 |
subWindow->setTransientParent(mainWindow); |
| 150 |
|
| 151 |
QWidget::showEvent(event); |
| 152 |
} |
| 153 |
|
| 154 |
void MainWindow::closeEvent(QCloseEvent *event) { |
| 155 |
QWidget::closeEvent(event); |
| 156 |
|
| 157 |
qApp->quit(); |
| 158 |
} |
| 159 |
} // namespace fcitx |