| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2012~2012 by CSSlayer * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify * |
| 5 |
* it under the terms of the GNU General Public License as published by * |
| 6 |
* the Free Software Foundation; either version 2 of the License, or * |
| 7 |
* (at your option) any later version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, * |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 |
* GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License * |
| 15 |
* along with this program; if not, write to the * |
| 16 |
* Free Software Foundation, Inc., * |
| 17 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 18 |
***************************************************************************/ |
| 19 |
|
| 20 |
#include "fcitxqtconnection_p.h" |
| 21 |
#include <QDBusConnection> |
| 22 |
#include <QDBusConnectionInterface> |
| 23 |
#include <QDBusReply> |
| 24 |
#include <QDBusServiceWatcher> |
| 25 |
#include <QDebug> |
| 26 |
#include <QDir> |
| 27 |
#include <QFile> |
| 28 |
#include <QTimer> |
| 29 |
|
| 30 |
#include <errno.h> |
| 31 |
#include <signal.h> |
| 32 |
|
| 33 |
// utils function in fcitx-utils and fcitx-config |
| 34 |
bool _pid_exists(pid_t pid) { |
| 35 |
if (pid <= 0) |
| 36 |
return 0; |
| 37 |
return !(kill(pid, 0) && (errno == ESRCH)); |
| 38 |
} |
| 39 |
|
| 40 |
FcitxQtConnection::FcitxQtConnection(QObject *parent) |
| 41 |
: QObject(parent), d_ptr(new FcitxQtConnectionPrivate(this)) {} |
| 42 |
|
| 43 |
void FcitxQtConnection::startConnection() { |
| 44 |
Q_D(FcitxQtConnection); |
| 45 |
if (!d->m_initialized) { |
| 46 |
d->initialize(); |
| 47 |
d->createConnection(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void FcitxQtConnection::endConnection() { |
| 52 |
Q_D(FcitxQtConnection); |
| 53 |
d->cleanUp(); |
| 54 |
d->finalize(); |
| 55 |
d->m_connectedOnce = false; |
| 56 |
} |
| 57 |
|
| 58 |
bool FcitxQtConnection::autoReconnect() { |
| 59 |
Q_D(FcitxQtConnection); |
| 60 |
return d->m_autoReconnect; |
| 61 |
} |
| 62 |
|
| 63 |
void FcitxQtConnection::setAutoReconnect(bool a) { |
| 64 |
Q_D(FcitxQtConnection); |
| 65 |
d->m_autoReconnect = a; |
| 66 |
} |
| 67 |
|
| 68 |
QDBusConnection *FcitxQtConnection::connection() { |
| 69 |
Q_D(FcitxQtConnection); |
| 70 |
return d->m_connection; |
| 71 |
} |
| 72 |
|
| 73 |
const QString &FcitxQtConnection::serviceName() { |
| 74 |
Q_D(FcitxQtConnection); |
| 75 |
return d->m_serviceName; |
| 76 |
} |
| 77 |
|
| 78 |
bool FcitxQtConnection::isConnected() { |
| 79 |
Q_D(FcitxQtConnection); |
| 80 |
return d->isConnected(); |
| 81 |
} |
| 82 |
|
| 83 |
FcitxQtConnection::~FcitxQtConnection() {} |
| 84 |
|
| 85 |
FcitxQtConnectionPrivate::FcitxQtConnectionPrivate(FcitxQtConnection *conn) |
| 86 |
: QObject(conn), q_ptr(conn), m_displayNumber(-1), |
| 87 |
m_serviceName( |
| 88 |
QString("%1-%2").arg("org.fcitx.Fcitx").arg(displayNumber())), |
| 89 |
m_connection(0), m_serviceWatcher(new QDBusServiceWatcher(this)), |
| 90 |
m_watcher(new QFileSystemWatcher(this)), m_autoReconnect(true), |
| 91 |
m_connectedOnce(false), m_initialized(false) {} |
| 92 |
|
| 93 |
FcitxQtConnectionPrivate::~FcitxQtConnectionPrivate() { |
| 94 |
if (m_connection) |
| 95 |
delete m_connection; |
| 96 |
} |
| 97 |
|
| 98 |
void FcitxQtConnectionPrivate::initialize() { |
| 99 |
m_serviceWatcher->setConnection(QDBusConnection::sessionBus()); |
| 100 |
m_serviceWatcher->addWatchedService(m_serviceName); |
| 101 |
|
| 102 |
QFileInfo info(socketFile()); |
| 103 |
QDir dir(info.path()); |
| 104 |
if (!dir.exists()) { |
| 105 |
QDir rt(QDir::root()); |
| 106 |
rt.mkpath(info.path()); |
| 107 |
} |
| 108 |
m_watcher->addPath(info.path()); |
| 109 |
if (info.exists()) { |
| 110 |
m_watcher->addPath(info.filePath()); |
| 111 |
} |
| 112 |
|
| 113 |
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, |
| 114 |
&FcitxQtConnectionPrivate::socketFileChanged); |
| 115 |
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, |
| 116 |
&FcitxQtConnectionPrivate::socketFileChanged); |
| 117 |
m_initialized = true; |
| 118 |
} |
| 119 |
|
| 120 |
void FcitxQtConnectionPrivate::finalize() { |
| 121 |
m_serviceWatcher->removeWatchedService(m_serviceName); |
| 122 |
m_watcher->removePaths(m_watcher->files()); |
| 123 |
m_watcher->removePaths(m_watcher->directories()); |
| 124 |
disconnect(m_watcher, &QFileSystemWatcher::fileChanged, this, |
| 125 |
&FcitxQtConnectionPrivate::socketFileChanged); |
| 126 |
disconnect(m_watcher, &QFileSystemWatcher::directoryChanged, this, |
| 127 |
&FcitxQtConnectionPrivate::socketFileChanged); |
| 128 |
m_initialized = false; |
| 129 |
} |
| 130 |
|
| 131 |
void FcitxQtConnectionPrivate::socketFileChanged() { |
| 132 |
QFileInfo info(socketFile()); |
| 133 |
if (info.exists()) { |
| 134 |
if (m_watcher->files().indexOf(info.filePath()) == -1) |
| 135 |
m_watcher->addPath(info.filePath()); |
| 136 |
} |
| 137 |
|
| 138 |
QString addr = address(); |
| 139 |
if (addr.isNull()) |
| 140 |
return; |
| 141 |
|
| 142 |
cleanUp(); |
| 143 |
createConnection(); |
| 144 |
} |
| 145 |
|
| 146 |
QByteArray FcitxQtConnectionPrivate::localMachineId() { |
| 147 |
return QDBusConnection::localMachineId(); |
| 148 |
} |
| 149 |
|
| 150 |
int FcitxQtConnectionPrivate::displayNumber() { |
| 151 |
if (m_displayNumber < 0) { |
| 152 |
QByteArray displayNumber("0"); |
| 153 |
QByteArray display(qgetenv("DISPLAY")); |
| 154 |
int pos = display.indexOf(':'); |
| 155 |
|
| 156 |
if (pos >= 0) { |
| 157 |
++pos; |
| 158 |
int pos2 = display.indexOf('.', pos); |
| 159 |
if (pos2 > 0) { |
| 160 |
displayNumber = display.mid(pos, pos2 - pos); |
| 161 |
} else { |
| 162 |
displayNumber = display.mid(pos); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
bool ok; |
| 167 |
int d = displayNumber.toInt(&ok); |
| 168 |
if (ok) { |
| 169 |
m_displayNumber = d; |
| 170 |
} else { |
| 171 |
m_displayNumber = 0; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return m_displayNumber; |
| 176 |
} |
| 177 |
|
| 178 |
const QString &FcitxQtConnectionPrivate::socketFile() { |
| 179 |
if (!m_socketFile.isEmpty()) |
| 180 |
return m_socketFile; |
| 181 |
|
| 182 |
QString filename = |
| 183 |
QString("%1-%2") |
| 184 |
.arg(QString::fromLatin1(QDBusConnection::localMachineId())) |
| 185 |
.arg(displayNumber()); |
| 186 |
|
| 187 |
QString home = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME")); |
| 188 |
if (home.isEmpty()) { |
| 189 |
home = QDir::homePath().append(QLatin1Literal("/.config")); |
| 190 |
} |
| 191 |
m_socketFile = QString("%1/fcitx/dbus/%2").arg(home).arg(filename); |
| 192 |
|
| 193 |
return m_socketFile; |
| 194 |
} |
| 195 |
|
| 196 |
QString FcitxQtConnectionPrivate::address() { |
| 197 |
QString addr; |
| 198 |
QByteArray addrVar = qgetenv("FCITX_DBUS_ADDRESS"); |
| 199 |
if (!addrVar.isNull()) |
| 200 |
return QString::fromLocal8Bit(addrVar); |
| 201 |
|
| 202 |
QFile file(socketFile()); |
| 203 |
if (!file.open(QIODevice::ReadOnly)) |
| 204 |
return QString(); |
| 205 |
|
| 206 |
const int BUFSIZE = 1024; |
| 207 |
|
| 208 |
char buffer[BUFSIZE]; |
| 209 |
size_t sz = file.read(buffer, BUFSIZE); |
| 210 |
file.close(); |
| 211 |
if (sz == 0) |
| 212 |
return QString(); |
| 213 |
char *p = buffer; |
| 214 |
while (*p) |
| 215 |
p++; |
| 216 |
size_t addrlen = p - buffer; |
| 217 |
if (sz != addrlen + 2 * sizeof(pid_t) + 1) |
| 218 |
return QString(); |
| 219 |
|
| 220 |
/* skip '\0' */ |
| 221 |
p++; |
| 222 |
pid_t *ppid = (pid_t *)p; |
| 223 |
pid_t daemonpid = ppid[0]; |
| 224 |
pid_t fcitxpid = ppid[1]; |
| 225 |
|
| 226 |
if (!_pid_exists(daemonpid) || !_pid_exists(fcitxpid)) |
| 227 |
return QString(); |
| 228 |
|
| 229 |
addr = QLatin1String(buffer); |
| 230 |
|
| 231 |
return addr; |
| 232 |
} |
| 233 |
|
| 234 |
void FcitxQtConnectionPrivate::createConnection() { |
| 235 |
if (m_connectedOnce && !m_autoReconnect) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
disconnect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, |
| 240 |
this, &FcitxQtConnectionPrivate::imChanged); |
| 241 |
QString addr = address(); |
| 242 |
if (!addr.isNull()) { |
| 243 |
QDBusConnection connection( |
| 244 |
QDBusConnection::connectToBus(addr, "fcitx")); |
| 245 |
if (connection.isConnected()) { |
| 246 |
// qDebug() << "create private"; |
| 247 |
m_connection = new QDBusConnection(connection); |
| 248 |
} else |
| 249 |
QDBusConnection::disconnectFromBus("fcitx"); |
| 250 |
} |
| 251 |
|
| 252 |
if (!m_connection) { |
| 253 |
QDBusConnection *connection = |
| 254 |
new QDBusConnection(QDBusConnection::sessionBus()); |
| 255 |
connect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, |
| 256 |
this, &FcitxQtConnectionPrivate::imChanged); |
| 257 |
QDBusReply<bool> registered = |
| 258 |
connection->interface()->isServiceRegistered(m_serviceName); |
| 259 |
if (!registered.isValid() || !registered.value()) { |
| 260 |
delete connection; |
| 261 |
} else { |
| 262 |
m_connection = connection; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
Q_Q(FcitxQtConnection); |
| 267 |
if (m_connection) { |
| 268 |
|
| 269 |
m_connection->connect("org.freedesktop.DBus.Local", |
| 270 |
"/org/freedesktop/DBus/Local", |
| 271 |
"org.freedesktop.DBus.Local", "Disconnected", |
| 272 |
this, SLOT(dbusDisconnected())); |
| 273 |
m_connectedOnce = true; |
| 274 |
Q_EMIT q->connected(); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void FcitxQtConnectionPrivate::dbusDisconnected() { |
| 279 |
cleanUp(); |
| 280 |
|
| 281 |
createConnection(); |
| 282 |
} |
| 283 |
|
| 284 |
void FcitxQtConnectionPrivate::imChanged(const QString &service, |
| 285 |
const QString &oldowner, |
| 286 |
const QString &newowner) { |
| 287 |
if (service == m_serviceName) { |
| 288 |
/* old die */ |
| 289 |
if (oldowner.length() > 0 || newowner.length() > 0) |
| 290 |
cleanUp(); |
| 291 |
|
| 292 |
/* new rise */ |
| 293 |
if (newowner.length() > 0) { |
| 294 |
QTimer::singleShot(100, this, SLOT(newServiceAppear())); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
void FcitxQtConnectionPrivate::cleanUp() { |
| 300 |
Q_Q(FcitxQtConnection); |
| 301 |
bool doemit = false; |
| 302 |
QDBusConnection::disconnectFromBus("fcitx"); |
| 303 |
if (m_connection) { |
| 304 |
delete m_connection; |
| 305 |
m_connection = nullptr; |
| 306 |
doemit = true; |
| 307 |
} |
| 308 |
|
| 309 |
if (!m_autoReconnect && m_connectedOnce) |
| 310 |
finalize(); |
| 311 |
|
| 312 |
/* we want m_connection and finalize being called before the signal |
| 313 |
* thus isConnected will return false in slot |
| 314 |
* and startConnection can be called in slot |
| 315 |
*/ |
| 316 |
if (doemit) |
| 317 |
Q_EMIT q->disconnected(); |
| 318 |
} |
| 319 |
|
| 320 |
bool FcitxQtConnectionPrivate::isConnected() { |
| 321 |
return m_connection && m_connection->isConnected(); |
| 322 |
} |
| 323 |
|
| 324 |
void FcitxQtConnectionPrivate::newServiceAppear() { |
| 325 |
if (!isConnected()) { |
| 326 |
cleanUp(); |
| 327 |
|
| 328 |
createConnection(); |
| 329 |
} |
| 330 |
} |