[OpenDocString] kdeconnect-kde (cpp)
sendfileitemaction.cpp
SendFileItemAction::SendFileItemAction(QObject *parent, const QVariantList &)
    : KAbstractFileItemActionPlugin(parent)
{
}
This constructor builds a file item action plugin object from an existing parent object and a list of variants.
QList SendFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
{
    QList actions;

    DaemonDbusInterface iface;
    if (!iface.isValid()) {
        return actions;
    }

    QDBusPendingReply reply = iface.devices(true, true);
    reply.waitForFinished();
    const QStringList devices = reply.value();
    for (const QString &id : devices) {
        DeviceDbusInterface deviceIface(id);
        if (!deviceIface.isValid()) {
            continue;
        }
        if (!deviceIface.hasPlugin(QStringLiteral("kdeconnect_share"))) {
            continue;
        }
        QAction *action = new QAction(QIcon::fromTheme(deviceIface.iconName()), deviceIface.name(), parentWidget);
        action->setProperty("id", id);
        action->setProperty("urls", QVariant::fromValue(fileItemInfos.urlList()));
        action->setProperty("parentWidget", QVariant::fromValue(parentWidget));
        connect(action, &QAction::triggered, this, &SendFileItemAction::sendFile);
        actions += action;
    }

    if (actions.count() > 1) {
        QAction *menuAction = new QAction(QIcon::fromTheme(QStringLiteral("kdeconnect")), i18n("Send via KDE Connect"), parentWidget);
        QMenu *menu = new QMenu(parentWidget);
        menu->addActions(actions);
        menuAction->setMenu(menu);
        return QList() << menuAction;
    } else {
        if (actions.count() == 1) {
            actions.first()->setText(i18n("Send to '%1' via KDE Connect", actions.first()->text()));
        }
        return actions;
    }
}
This function returns a list of send file item actions. It first checks if the interface is valid, if it is a DaemonDbusInterface, it gets the list of devices, and creates a QAction object for each device. It also connects the sendFileItemAction signal to each device's signal. If the list has only one action, it adds it to the menu. If the list has only one action, it adds it to the list. If the list has only one action, it returns a QList of actions. If there are multiple actions, the first action is added to the menu.
void SendFileItemAction::sendFile()
{
    const QList urls = sender()->property("urls").value>();
    QString id = sender()->property("id").toString();
    for (const QUrl &url : urls) {
        QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
                                                          QStringLiteral("/modules/kdeconnect/devices/") + id + QStringLiteral("/share"),
                                                          QStringLiteral("org.kde.kdeconnect.device.share"),
                                                          QStringLiteral("shareUrl"));
        msg.setArguments(QVariantList() << url.toString());
        QDBusConnection::sessionBus().asyncCall(msg);
    }
}
This sends a file item to all connected devices.