File indexing completed on 2024-04-28 09:41:44
0001 // SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com> 0002 // 0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 0005 #include "kupengine.h" 0006 #include "kupservice.h" 0007 #include "kupdaemon.h" 0008 0009 #include <QJsonArray> 0010 #include <QJsonDocument> 0011 #include <QTimer> 0012 0013 KupEngine::KupEngine(QObject *pParent, const QVariantList &pArgs) 0014 : Plasma::DataEngine(pParent /* pArgs*/) 0015 { 0016 Q_UNUSED(pArgs); 0017 mSocketName = QStringLiteral("kup-daemon-"); 0018 mSocketName += QString::fromLocal8Bit(qgetenv("USER")); 0019 mSocket = new QLocalSocket(this); 0020 connect(mSocket, &QLocalSocket::readyRead, this, &KupEngine::processData); 0021 connect(mSocket, &QLocalSocket::stateChanged, this, &KupEngine::checkConnection); 0022 // wait 5 seconds before trying to connect first time 0023 QTimer::singleShot(5000, mSocket, [&]{mSocket->connectToServer(mSocketName);}); 0024 setData(QStringLiteral("common"), QStringLiteral("plan count"), 0); 0025 } 0026 0027 Plasma::Service *KupEngine::serviceForSource(const QString &pSource) { 0028 if (pSource == "daemon") { 0029 return new KupDaemonService(mSocket, this); 0030 } 0031 0032 bool lIntOk; 0033 int lPlanNumber = pSource.toInt(&lIntOk); 0034 if(lIntOk) { 0035 return new KupService(lPlanNumber, mSocket, this); 0036 } 0037 return nullptr; 0038 } 0039 0040 void KupEngine::processData() { 0041 if(mSocket->bytesAvailable() <= 0) { 0042 return; 0043 } 0044 QJsonDocument lDoc = QJsonDocument::fromJson(mSocket->readAll()); 0045 if(!lDoc.isObject()) { 0046 return; 0047 } 0048 QJsonObject lEvent = lDoc.object(); 0049 if(lEvent["event"] == QStringLiteral("status update")) { 0050 QJsonArray lPlans = lEvent["plans"].toArray(); 0051 setData(QStringLiteral("common"), QStringLiteral("plan count"), lPlans.count()); 0052 setCommonData(lEvent, QStringLiteral("tray icon active")); 0053 setCommonData(lEvent, QStringLiteral("tooltip icon name")); 0054 setCommonData(lEvent, QStringLiteral("tooltip title")); 0055 setCommonData(lEvent, QStringLiteral("tooltip subtitle")); 0056 setCommonData(lEvent, QStringLiteral("any plan busy")); 0057 setCommonData(lEvent, QStringLiteral("no plan reason")); 0058 for(int i = 0; i < lPlans.count(); ++i) { 0059 QJsonObject lPlan = lPlans[i].toObject(); 0060 setPlanData(i, lPlan, QStringLiteral("description")); 0061 setPlanData(i, lPlan, QStringLiteral("destination available")); 0062 setPlanData(i, lPlan, QStringLiteral("status heading")); 0063 setPlanData(i, lPlan, QStringLiteral("status details")); 0064 setPlanData(i, lPlan, QStringLiteral("icon name")); 0065 setPlanData(i, lPlan, QStringLiteral("log file exists")); 0066 setPlanData(i, lPlan, QStringLiteral("busy")); 0067 setPlanData(i, lPlan, QStringLiteral("bup type")); 0068 } 0069 } 0070 } 0071 0072 void KupEngine::checkConnection(QLocalSocket::LocalSocketState pState) { 0073 if(pState != QLocalSocket::ConnectedState && pState != QLocalSocket::ConnectingState) { 0074 QTimer::singleShot(10000, mSocket, [&]{mSocket->connectToServer(mSocketName);}); 0075 } 0076 if(pState == QLocalSocket::UnconnectedState) { 0077 // Don't bother to translate this error message, guessing the error string from qt 0078 // is not translated also. 0079 QString lErrorText = QStringLiteral("Error, no connection to kup-daemon: "); 0080 lErrorText += mSocket->errorString(); 0081 setData(QStringLiteral("common"), QStringLiteral("no plan reason"), lErrorText); 0082 } 0083 } 0084 0085 void KupEngine::setPlanData(int i, const QJsonObject &pPlan, const QString &pKey) { 0086 setData(QString(QStringLiteral("plan %1")).arg(i), pKey, pPlan[pKey].toVariant()); 0087 } 0088 0089 void KupEngine::setCommonData(const QJsonObject &pCommonStatus, const QString &pKey) { 0090 setData(QStringLiteral("common"), pKey, pCommonStatus[pKey].toVariant()); 0091 } 0092 0093 K_PLUGIN_CLASS_WITH_JSON(KupEngine, "plasma-dataengine-kup.json") 0094 0095 #include "kupengine.moc" 0096