Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Problem with password change pop-up not displaying #372

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dnetwork-secret-dialog/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QTimer>
#include <QTranslator>

DCORE_USE_NAMESPACE
Expand Down Expand Up @@ -64,6 +65,8 @@ int main(int argc, char *argv[])
}

file.close();
// GetSecrets超时时间120000,参考NM中nm-secret-agent.c中函数nm_secret_agent_get_secrets里的设置
QTimer::singleShot(120000, &app, &DApplication::quit);

NetworkDialog *networkDialog = new NetworkDialog();
QObject::connect(&app, &QCoreApplication::aboutToQuit, [networkDialog]() {
Expand Down
169 changes: 109 additions & 60 deletions dnetwork-secret-dialog/src/networkdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@

#include "networkdialog.h"

#include <QFile>
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QLocalSocket>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingReply>
#include <QDebug>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocalSocket>

#include <unistd.h>

static const QString NetworkDialogApp = "dde-network-dialog"; //网络列表执行文件
static QMap<QString, void (NetworkDialog::*)(QLocalSocket *, const QByteArray &)> s_FunMap = {
static const QString identity = "identity";
static const QMap<QString, void (NetworkDialog::*)(QLocalSocket *, const QByteArray &)> s_FunMap = {
{ "secretsResult", &NetworkDialog::onSecretsResult },
{ "password", &NetworkDialog::onPassword }
};

NetworkDialog::NetworkDialog(QObject *parent)
: QObject(parent)
, m_needIdentity(false)
, m_quitNow(false)
, m_clinet(new QLocalSocket(this))
{
connect(m_clinet, SIGNAL(connected()), this, SLOT(connectedHandler()));
Expand All @@ -37,49 +41,6 @@
m_clinet->close();
}

void NetworkDialog::onPassword(QLocalSocket *socket, const QByteArray &data)
{
qInfo() << "Received password and deal with it.";
QJsonDocument doc = QJsonDocument::fromJson(data);
if (!doc.isObject()) {
qWarning() << "Wrong format for password, it is not Object.";
return;
}

QJsonObject obj = doc.object();
QString key = obj.value("key").toString();
QString passwd = obj.value("password").toString();
bool input = obj.value("input").toBool();

if (m_key != key) {
qWarning() << "Missmatched ssid, old key is " << m_key << ", and now is" << key;
return;
}

if (!input) {
qWarning() << "Only deal with password from input.";
qApp->exit(1);
return;
}
QJsonObject resultJsonObj;
QJsonArray secretsJsonArray;
secretsJsonArray.append(passwd);
resultJsonObj.insert("secrets", secretsJsonArray);

QFile file;
if (!file.open(stdout, QFile::WriteOnly)) {
qWarning() << "open STDOUT failed";
qApp->exit(-4);
}
file.write(QJsonDocument(resultJsonObj).toJson());
file.flush();
file.close();
m_clinet->flush();
m_clinet->close();
qInfo() << "Wirte password to dde-session-daemon and exit it.";
qApp->exit(0);
}

/**
* @brief NetworkDialog::exec
* 调dde-network-dialog处理密码输入
Expand All @@ -95,29 +56,105 @@
qDebug() << "Only deal with wireless for the connection type, now connType:" << obj.value("connType");
return false;
}
QJsonArray array = obj.value("devices").toArray();
QString device;
if (!array.isEmpty()) {
device = array.first().toString();
}
QString connName = obj.value("connId").toString();
if (!connName.isEmpty() && (1 == obj.value("secrets").toArray().size())) {
if (!connName.isEmpty()) {
m_data = "\nrequestSecrets:" + doc.toJson(QJsonDocument::Compact) + "\n";
QJsonArray array = obj.value("devices").toArray();
QString device;
if (!array.isEmpty()) {
device = array.first().toString();
}
QJsonObject propsObj = obj.value("props").toObject();
QString password;
int count = 0;
for (auto secret : obj.value("secrets").toArray()) {
const QString& key = secret.toString();
if (key == identity) {
m_needIdentity = true;
continue;
}
count++;
if (propsObj.contains(key)) {
password = propsObj.value(key).toString();
}
}
m_key = connName;
QJsonObject json;
json.insert("dev", device);
json.insert("ssid", m_key);
json.insert("wait", true);
QJsonDocument doc;
doc.setObject(json);
m_data = "\nconnect:" + doc.toJson(QJsonDocument::Compact) + "\n";
if (m_needIdentity) {
m_identityContent = propsObj.value(identity).toString();
json.insert(identity, m_identityContent);
}
json.insert("password", password);
QJsonDocument passwordDoc;
passwordDoc.setObject(json);
m_data += "\nconnect:" + passwordDoc.toJson(QJsonDocument::Compact) + "\n";
ConnectToServer();
qDebug() << "Connect to server :" << m_clinet->serverName() << ", ssid:" << m_key;
return true;
}
return false;
}

void NetworkDialog::onSecretsResult(QLocalSocket *socket, const QByteArray &data)
{
Q_UNUSED(socket);
if (data.isEmpty()) {
exit(1);
return;
}

QFile file;
if (!file.open(stdout, QFile::WriteOnly)) {
qWarning() << "Open stdout failed";
exit(-4);
}
file.write(data);
file.flush();
file.close();
exit(0);
}

void NetworkDialog::onPassword(QLocalSocket *socket, const QByteArray &data)
{
Q_UNUSED(socket);
QJsonDocument doc = QJsonDocument::fromJson(data);
if (!doc.isObject())
return;
QJsonObject obj = doc.object();
QString key = obj.value("key").toString();
QString passwd = obj.value("password").toString();
QString identityContent;
bool input = obj.value("input").toBool();
if (m_key != key)
return;
if (!input) {
exit(1);
return;
}
QJsonObject resultJsonObj;
QJsonArray secretsJsonArray;
if (m_needIdentity) {
// 如果从networkcore获取的用户身份信息为空,则使用调用者传进来的数据.(用于兼容dde-network-core低版本情况)
identityContent = obj.value(identity).toString().isEmpty() ? m_identityContent : obj.value(identity).toString();
secretsJsonArray.append(identityContent);
}
secretsJsonArray.append(passwd);
resultJsonObj.insert("secrets", secretsJsonArray);
QFile file;
if (!file.open(stdout, QFile::WriteOnly)) {
qDebug() << "open STDOUT failed";
exit(-4);
}
file.write(QJsonDocument(resultJsonObj).toJson());
file.flush();
file.close();
exit(0);
}

void NetworkDialog::connectedHandler()

Check warning on line 157 in dnetwork-secret-dialog/src/networkdialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'connectedHandler' is never used.
{
if (!m_clinet->isOpen() || m_data.isEmpty())
return;
Expand All @@ -126,10 +163,10 @@
m_clinet->flush();
}

void NetworkDialog::disConnectedHandler()

Check warning on line 166 in dnetwork-secret-dialog/src/networkdialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'disConnectedHandler' is never used.
{
qDebug() << "Received disconnect event from the socket and exit it.";
qApp->exit(0);
exit(0);
}

bool NetworkDialog::ConnectToServer()
Expand All @@ -152,7 +189,16 @@
return state == QLocalSocket::ConnectedState;
}

void NetworkDialog::exit(int returnCode)
{
if (m_quitNow)
return;

m_quitNow = true;
qApp->exit(returnCode);
}

void NetworkDialog::readyReadHandler()

Check warning on line 201 in dnetwork-secret-dialog/src/networkdialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'readyReadHandler' is never used.
{
QLocalSocket *socket = static_cast<QLocalSocket *>(sender());
if (!socket)
Expand All @@ -163,6 +209,9 @@
QList<QByteArray> dataArray = allData.split('\n');
m_lastData = dataArray.last();
for (const QByteArray &data : dataArray) {
if (m_quitNow)
return;

int keyIndex = data.indexOf(':');
if (keyIndex == -1)
continue;
Expand Down
8 changes: 6 additions & 2 deletions dnetwork-secret-dialog/src/networkdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ class NetworkDialog : public QObject
~NetworkDialog();

bool exec(const QJsonDocument &doc);
void onSecretsResult(QLocalSocket *socket, const QByteArray &data);
void onPassword(QLocalSocket *socket, const QByteArray &data);

private Q_SLOTS:
void connectedHandler();
void disConnectedHandler();
void readyReadHandler();
bool ConnectToServer();
void exit(int returnCode);
void readyReadHandler();

private:
QString m_key;
QByteArray m_lastData; // 用于数据拼接

bool m_needIdentity; // 认证信息是否需要用户名
QString m_identityContent;
bool m_quitNow;
QByteArray m_data;
QLocalSocket *m_clinet;
};
Expand Down
Loading