Skip to content

Conversation

@caixr23
Copy link
Contributor

@caixr23 caixr23 commented Apr 17, 2025

Synchronize dss-network-plugin 107x interface

pms: TASK-361719

Summary by Sourcery

Enhance network plugin localization by adding support for dynamic language updates across network services

New Features:

  • Add a new method to dynamically update language settings for network services via D-Bus

Bug Fixes:

  • Ensure proper handling of language update requests with robust error checking

Enhancements:

  • Implement a mechanism to synchronize language settings between network plugin and system network service
  • Add service watcher to handle language updates when network service is not immediately available

@sourcery-ai
Copy link

sourcery-ai bot commented Apr 17, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new D-Bus method message in the dss-network-plugin to handle locale updates. It synchronizes the locale with the system network service, handling cases where the service is not immediately available. The changes involve parsing a JSON message to extract the locale, installing the translator, and using D-Bus to communicate with the system network service.

Sequence diagram for locale update via D-Bus

sequenceDiagram
    participant NP as NetworkPlugin
    participant SN as SystemNetworkService
    participant DBus as D-Bus

    NP->>NP: message(msgData)
    NP->>NP: Parse msgData for locale
    NP->>NP: installTranslator(locale)
    alt SN is registered
        NP->>DBus: asyncCall("UpdateLanguage", locale) to SN
        DBus-->>SN: UpdateLanguage(locale)
        SN-->>DBus: Reply
        DBus-->>NP: Reply
    else SN is not registered
        NP->>NP: Create QDBusServiceWatcher
        NP->>NP: Connect to serviceRegistered signal
        NP->>NP: Wait for SN to register
        activate SN
        SN->>DBus: Register service
        DBus->>NP: serviceRegistered(networkService)
        NP->>DBus: asyncCall("UpdateLanguage", locale) to SN
        DBus-->>SN: UpdateLanguage(locale)
        SN-->>DBus: Reply
        DBus-->>NP: Reply
        deactivate SN
    end
    NP->>NP: Return success JSON
Loading

Updated class diagram for NetworkPlugin

classDiagram
    class NetworkPlugin {
        -QWidget* m_contentWidget
        -dde::network::NetManager* m_manager
        -dde::network::NetStatus* m_netStatus
        -QList<QObject*> m_networkItems
        -QTimer* m_timer
        -bool m_visible
        -QString m_userJson
        +void requestShowContent()
        +void setMessage(bool visible)
        +QString message(QString msgData)
        <<signals>>
        +void notifyNetworkStateChanged(bool)
    }
Loading

File-Level Changes

Change Details Files
Introduces a new D-Bus method message to handle locale updates and synchronize them with the system network service.
  • Adds a message method to NetworkPlugin to receive JSON messages containing locale information.
  • Parses the JSON message to extract the locale.
  • Installs the translator for the given locale.
  • Updates the system network service with the new locale via D-Bus.
  • Handles cases where the system network service is not immediately available by using QDBusServiceWatcher to wait for the service to register.
  • Returns a JSON response indicating success or failure.
dss-network-plugin/networkmodule.cpp
dss-network-plugin/networkmodule.h
Adds necessary includes and defines constants for D-Bus communication with the system network service.
  • Includes <QDBusConnectionInterface>, <QDBusServiceWatcher>.
  • Defines constants for the network service name, path, and interface.
dss-network-plugin/networkmodule.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @caixr23 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a more specific name than message for the new method in NetworkPlugin to better reflect its purpose.
  • The UpdateLanguage calls on the D-Bus interface should check for errors and log them appropriately.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +309 to +310
} else {
qWarning() << networkService << "don't start, wait for it start";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider handling DBus call errors.

Using reply.waitForFinished() blocks the thread until the call completes, but there is no subsequent error checking. It might be beneficial to inspect the reply for any errors to avoid silent failures in UpdateLanguage.

Suggested implementation:

        QDBusPendingCall reply = dbusInter.asyncCall("UpdateLanguage", locale);
        reply.waitForFinished();
        QDBusPendingReply<void> replyCheck = reply;
        if (replyCheck.isError()) {
            qWarning() << "Error updating language:" << replyCheck.error().message();
        } else {
            qDebug() << "UpdateLanguage call succeeded.";
        }
                QDBusPendingCall reply = dbusInter.asyncCall("UpdateLanguage", locale);
                reply.waitForFinished();
                QDBusPendingReply<void> replyCheck = reply;
                if (replyCheck.isError()) {
                    qWarning() << "Error updating language on service registration:" << replyCheck.error().message();
                } else {
                    qDebug() << "UpdateLanguage call succeeded on service registration.";
                }

If further error handling is needed (for example, retrying the call or handling specific DBus error codes), additional adjustments may be required.

QJsonObject jsonObject = json.object();
if (!jsonObject.contains("data")) {
qWarning() << "msgData don't containt data" << msgData;
QJsonDocument jsonResult;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Fix typo in error message for missing data.

The error message uses "msgData don't containt data"; consider correcting it to "msgData doesn't contain data" to improve clarity and professionalism.

Suggested implementation:

        qWarning() << "msgData doesn't contain data" << msgData;
        resultObject.insert("data", QString("msgData doesn't contain data %1").arg(msgData));


QString NetworkPlugin::message(const QString &msgData)
{
qDebug() << "message" << msgData;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the DBus update logic into a separate helper function to improve code clarity and reduce nesting in the message function, focusing it on JSON handling and translation installation..

Consider extracting the DBus update logic into a separate helper to reduce nesting and improve clarity. For instance, create a function like updateNetworkLanguage(QString locale) that handles both the synchronous and asynchronous cases. This makes the main message function more focused on JSON handling and translation installation.

Example:

QString NetworkPlugin::updateNetworkLanguage(const QString &locale) {
    if (QDBusConnection::systemBus().interface()->isServiceRegistered(networkService)) {
        qDebug() << "update SystemNetwork Language" << locale;
        QDBusInterface dbusInter(networkService, networkPath, networkInterface, QDBusConnection::systemBus());
        QDBusPendingCall reply = dbusInter.asyncCall("UpdateLanguage", locale);
        reply.waitForFinished();
    } else {
        qWarning() << networkService << "don't start, wait for it start";
        QDBusServiceWatcher *serviceWatcher = new QDBusServiceWatcher(this);
        serviceWatcher->setConnection(QDBusConnection::systemBus());
        serviceWatcher->addWatchedService(networkService);
        connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [locale](const QString &service) {
            if (service == networkService) {
                QDBusInterface dbusInter(networkService, networkPath, networkInterface, QDBusConnection::systemBus());
                QDBusPendingCall reply = dbusInter.asyncCall("UpdateLanguage", locale);
                reply.waitForFinished();
            }
        });
    }
    return "success";
}

Then simplify the message function:

QString NetworkPlugin::message(const QString &msgData) {
    qDebug() << "message" << msgData;
    QJsonDocument json = QJsonDocument::fromJson(msgData.toLatin1());
    QJsonObject jsonObject = json.object();

    if (!jsonObject.contains("data")) {
        qWarning() << "msgData doesn't contain data:" << msgData;
        QJsonObject resultObject{{"data", QString("msgData doesn't contain data %1").arg(msgData)}};
        return QJsonDocument(resultObject).toJson();
    }

    QJsonObject dataObject = jsonObject.value("data").toObject();
    QString locale = dataObject.value("locale").toString();
    qDebug() << "read locale" << locale;
    m_network->installTranslator(locale);

    QString status = updateNetworkLanguage(locale);

    QJsonObject resultObject{{"data", status}};
    return QJsonDocument(resultObject).toJson();
}

By moving the DBus logic into a helper, the control flow in message becomes easier to follow and maintain.

robertkill
robertkill previously approved these changes Apr 17, 2025
Synchronize dss-network-plugin 107x interface

pms: TASK-361719
@deepin-ci-robot
Copy link

deepin pr auto review

关键摘要:

  • NetworkPlugin::message函数中,QDBusPendingCall reply = dbusInter.asyncCall("UpdateLanguage", locale);后直接调用了reply.waitForFinished();,这可能会导致阻塞主线程,建议使用异步处理方式。
  • QDBusServiceWatcherserviceRegistered信号处理函数中使用了Lambda表达式,但没有捕获this指针,可能会导致内存泄漏。
  • NetManagerPrivate::retranslateUi函数中使用了QVector来遍历m_dataMap,但没有检查m_dataMap是否为空,可能会导致访问空指针。
  • NetManagerThreadPrivate::retranslate函数中直接调用了NetworkController::installTranslator,但没有检查locale是否为空,可能会导致未定义行为。

是否建议立即修改:

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@caixr23 caixr23 merged commit 5a46619 into linuxdeepin:master Apr 17, 2025
15 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants