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

cherry-pick two changes from v25 dev #571

Merged
merged 2 commits into from
Nov 21, 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
49 changes: 36 additions & 13 deletions src/lib/cooperation/core/net/helper/sharehelper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -83,6 +83,8 @@ void ShareHelperPrivate::initConnect()
connect(taskDialog(), &CooperationTaskDialog::acceptRequest, this, [this] { onActionTriggered(NotifyAcceptAction); });
connect(taskDialog(), &CooperationTaskDialog::waitCanceled, this, &ShareHelperPrivate::cancelShareApply);

connect(ConfigManager::instance(), &ConfigManager::appAttributeChanged, this, &ShareHelperPrivate::onAppAttributeChanged);

connect(qApp, &QApplication::aboutToQuit, this, &ShareHelperPrivate::stopCooperation);
}

Expand All @@ -109,11 +111,17 @@ void ShareHelperPrivate::notifyMessage(const QString &body, const QStringList &a
void ShareHelperPrivate::stopCooperation()
{
if (targetDeviceInfo && targetDeviceInfo->connectStatus() == DeviceInfo::Connected) {
#ifdef linux
static QString body(tr("Coordination with \"%1\" has ended"));
notifyMessage(body.arg(CommonUitls::elidedText(targetDeviceInfo->deviceName(), Qt::ElideMiddle, 15)), {}, 3 * 1000);
#endif
ShareHelper::instance()->disconnectToDevice(targetDeviceInfo);
q->disconnectToDevice(targetDeviceInfo);
}
}

void ShareHelperPrivate::onAppAttributeChanged(const QString &group, const QString &key, const QVariant &value)
{
if (group != AppSettings::GenericGroup)
return;

if (key == AppSettings::PeripheralShareKey) {
q->switchPeripheralShared(value.toBool());
}
}

Expand Down Expand Up @@ -163,12 +171,8 @@ void ShareHelperPrivate::onActionTriggered(const QString &action)
// write server's fingerprint into trust server file.
SslCertConf::ins()->writeTrustPrint(true, recvServerPrint.toStdString());
}
client->setClientTargetIp(senderDeviceIp);

bool started = client->setClientTargetIp(senderDeviceIp) && client->startBarrier();
if (!started) {
WLOG << "Failed to start barrier client! " << senderDeviceIp.toStdString();
return;
}
auto info = DiscoverController::instance()->findDeviceByIP(senderDeviceIp);
if (!info) {
WLOG << "AcceptAction, but not find: " << senderDeviceIp.toStdString();
Expand All @@ -187,6 +191,10 @@ void ShareHelperPrivate::onActionTriggered(const QString &action)

static QString body(tr("Connection successful, coordinating with \"%1\""));
notifyMessage(body.arg(CommonUitls::elidedText(info->deviceName(), Qt::ElideMiddle, 15)), {}, 3 * 1000);

// check setting and turn on/off client
auto on = CooperationUtil::deviceInfo().value(AppSettings::PeripheralShareKey).toBool();
q->switchPeripheralShared(on);
}
recvServerPrint = ""; // clear received server's ingerprint
}
Expand Down Expand Up @@ -275,7 +283,7 @@ void ShareHelper::disconnectToDevice(const DeviceInfoPointer info)

info->setConnectStatus(DeviceInfo::Connectable);
d->targetDeviceInfo->setConnectStatus(DeviceInfo::Connectable);
DiscoverController::instance()->updateDeviceState({ d->targetDeviceInfo });
DiscoverController::instance()->updateDeviceState({ DeviceInfoPointer::create(*d->targetDeviceInfo.data()) });

static QString body(tr("Coordination with \"%1\" has ended"));
d->notifyMessage(body.arg(CommonUitls::elidedText(d->targetDeviceInfo->deviceName(), Qt::ElideMiddle, 15)), {}, 3 * 1000);
Expand All @@ -296,7 +304,7 @@ void ShareHelper::buttonClicked(const QString &id, const DeviceInfoPointer info)

bool ShareHelper::buttonVisible(const QString &id, const DeviceInfoPointer info)
{
if (qApp->property("onlyTransfer").toBool() || !info->peripheralShared())
if (qApp->property("onlyTransfer").toBool())
return false;

if (id == ConnectButtonId && info->connectStatus() == DeviceInfo::ConnectStatus::Connectable)
Expand Down Expand Up @@ -526,3 +534,18 @@ int ShareHelper::selfSharing(const QString &shareIp)

return -1;
}


bool ShareHelper::switchPeripheralShared(bool on)
{
auto client = ShareCooperationServiceManager::instance()->client();
if (client.isNull()) {
return false;
}

if (on) {
return client->startBarrier();
}
client->stopBarrier();
return true;
}
5 changes: 4 additions & 1 deletion src/lib/cooperation/core/net/helper/sharehelper.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -40,6 +40,9 @@ public Q_SLOTS:
// check the self is sharing with someone
int selfSharing(const QString &shareIp);

//switch the client peripheral share by setting
bool switchPeripheralShared(bool on);

private:
explicit ShareHelper(QObject *parent = nullptr);
~ShareHelper();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/cooperation/core/net/helper/sharehelper_p.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -34,6 +34,7 @@ public Q_SLOTS:
void onActionTriggered(const QString &action);
void stopCooperation();
void cancelShareApply();
void onAppAttributeChanged(const QString &group, const QString &key, const QVariant &value);

public:
ShareHelper *q;
Expand Down
93 changes: 50 additions & 43 deletions src/lib/cooperation/core/share/sharecooperationservice.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -102,48 +102,10 @@ bool ShareCooperationService::setServerConfig(const ShareServerConfig &config)
return true;
}

bool ShareCooperationService::setClientTargetIp(const QString &screen, const QString &ip, const int &port)
void ShareCooperationService::setClientTargetIp(const QString &ip)
{
if (BarrierType::Server == _brrierType) {
ELOG << "not the brrier client !!!!!!!";
return false;
}
if (!_cooConfig) {
ELOG << "the _cooConfig is null !!!!!"
<< " ip = " << ip.toStdString() << ":" << port;
return false;
}
if (ip.isEmpty()) {
ELOG << "error param !!!!!"
<< " ip = " << ip.toStdString() << ":" << port;
return false;
}

_cooConfig->setServerIp(ip);
_cooConfig->setPort(port == 0 ? UNI_SHARE_SERVER_PORT : port);
return true;
}

bool ShareCooperationService::setClientTargetIp(const QString &ip)
{
if (BarrierType::Server == _brrierType) {
ELOG << "not the brrier client !!!!!!!";
return false;
}
if (!_cooConfig) {
ELOG << "the _cooConfig is null !!!!!"
<< " ip = " << ip.toStdString() << ":" << UNI_SHARE_SERVER_PORT;
return false;
}
if (ip.isEmpty()) {
ELOG << "error param !!!!!"
<< " ip = " << ip.toStdString() << ":" << UNI_SHARE_SERVER_PORT;
return false;
}

_cooConfig->setServerIp(ip);
_cooConfig->setPort(UNI_SHARE_SERVER_PORT);
return true;
cooConfig().setServerIp(ip);
cooConfig().setPort(UNI_SHARE_SERVER_PORT);
}

void ShareCooperationService::setEnableCrypto(bool enable)
Expand All @@ -159,7 +121,7 @@ void ShareCooperationService::setBarrierProfile(const QString &dir)
pdir.mkpath(pdir.absolutePath());
}

_cooConfig->setProfileDir(dir);
cooConfig().setProfileDir(dir);
}

bool ShareCooperationService::isRunning()
Expand All @@ -171,6 +133,45 @@ bool ShareCooperationService::isRunning()
return barrierProcess()->state() == QProcess::Running;
}

void ShareCooperationService::terminateAllBarriers()
{
#if defined(Q_OS_WIN)
// Windows
QProcess process;
process.start("tasklist");
process.waitForFinished();

QString output = process.readAllStandardOutput();
QStringList processList = output.split('\n', QString::SkipEmptyParts);

for (const QString &line : processList) {
if (line.contains("barrier", Qt::CaseInsensitive)) {
QStringList tokens = line.split(QStringLiteral(" "), QString::SkipEmptyParts);
if (tokens.size() >= 2) {
QString pid = tokens[1]; // PID 在第二个字段
QProcess::execute("taskkill", QStringList() << "/F" << "/PID" << pid);
LOG << "Terminated barrier process with PID:" << pid.toStdString();
}
}
}
#else
// Linux
QProcess process;
process.start("pgrep", QStringList() << "barrier");
process.waitForFinished();

QString output = process.readAllStandardOutput();
QStringList pidList = output.split('\n', QString::SkipEmptyParts);

for (const QString &pid : pidList) {
if (!pid.isEmpty()) {
QProcess::execute("kill", QStringList() << pid);
LOG << "Terminated barrier process with PID:" << pid.toStdString();
}
}
#endif
}

bool ShareCooperationService::startBarrier()
{
LOG << "starting process";
Expand Down Expand Up @@ -204,6 +205,8 @@ bool ShareCooperationService::startBarrier()
return false;
}

terminateAllBarriers();

setBarrierProcess(new QProcess());
connect(barrierProcess(), SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(barrierFinished(int, QProcess::ExitStatus)));
connect(barrierProcess(), SIGNAL(readyReadStandardOutput()), this, SLOT(logOutput()));
Expand Down Expand Up @@ -264,6 +267,10 @@ bool ShareCooperationService::clientArgs(QStringList &args, QString &app)
WLOG << "Barrier client not found:" << app.toStdString();
return false;
}
if (cooConfig().serverIp().isEmpty()) {
WLOG << "Barrier client serverIp not setting!";
return false;
}

#if defined(Q_OS_WIN)
// wrap in quotes so a malicious user can't start \Program.exe as admin.
Expand Down
6 changes: 3 additions & 3 deletions src/lib/cooperation/core/share/sharecooperationservice.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -59,13 +59,13 @@ class ShareCooperationService : public QObject
void setServerConfig(const DeviceInfoPointer selfDevice, const DeviceInfoPointer targetDevice);
bool setServerConfig(const ShareServerConfig &config);

bool setClientTargetIp(const QString &screen, const QString &ip, const int &port);
bool setClientTargetIp(const QString &ip);
void setClientTargetIp(const QString &ip);

void setEnableCrypto(bool enable);
void setBarrierProfile(const QString &dir);

bool isRunning();
void terminateAllBarriers();

public slots:
bool restartBarrier();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -46,6 +46,7 @@ QSharedPointer<ShareCooperationService> ShareCooperationServiceManager::server()
void ShareCooperationServiceManager::stop()
{
_client->stopBarrier();
_client->setClientTargetIp(""); // reset client by serverIp
emit stopShareServer();
}

Expand Down
Loading