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