From 8010739a8a2ebdb4158a0298a101f204e8e4de39 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Thu, 12 May 2022 20:34:24 +0200 Subject: [PATCH] [smsapp] Refactor and fix argument handling Use a singleton instead of context properties for data handling Fix passing initial message Make device menu creation more declarative Fix initial device handling Update current device when new instance with initial device is requested --- interfaces/devicesmodel.cpp | 1 + smsapp/main.cpp | 50 +++++++++++++----- smsapp/qml/ConversationDisplay.qml | 4 +- smsapp/qml/ConversationList.qml | 4 +- smsapp/qml/SendingArea.qml | 1 + smsapp/qml/main.qml | 82 ++++++++++++++++-------------- 6 files changed, 86 insertions(+), 56 deletions(-) diff --git a/interfaces/devicesmodel.cpp b/interfaces/devicesmodel.cpp index 0b691bed2..51feaf579 100644 --- a/interfaces/devicesmodel.cpp +++ b/interfaces/devicesmodel.cpp @@ -53,6 +53,7 @@ DevicesModel::DevicesModel(QObject* parent) QHash< int, QByteArray > DevicesModel::roleNames() const { QHash names = QAbstractItemModel::roleNames(); + names.insert(NameModelRole, "name"); names.insert(IdModelRole, "deviceId"); names.insert(IconNameRole, "iconName"); names.insert(DeviceRole, "device"); diff --git a/smsapp/main.cpp b/smsapp/main.cpp index 5e8c69f30..427a24c01 100644 --- a/smsapp/main.cpp +++ b/smsapp/main.cpp @@ -24,6 +24,20 @@ #include "smshelper.h" +class AppData : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString initialMessage MEMBER m_initialMessage NOTIFY initialMessageChanged) + Q_PROPERTY(QString initialDevice MEMBER m_initialDevice NOTIFY initialDeviceChanged) + +public: + Q_SIGNAL void initialMessageChanged(); + Q_SIGNAL void initialDeviceChanged(); + + QString m_initialMessage; + QString m_initialDevice; +}; + int main(int argc, char *argv[]) { QIcon::setFallbackThemeName(QStringLiteral("breeze")); @@ -53,37 +67,47 @@ int main(int argc, char *argv[]) QQuickStyle::setStyle(QStringLiteral("org.kde.desktop")); } - QString initialMessage, deviceid; + AppData data; - { - QCommandLineParser parser; - aboutData.setupCommandLine(&parser); - parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id"))); - parser.addOption(QCommandLineOption(QStringLiteral("message"), i18n("Send a message"), i18n("message"))); - parser.process(app); - aboutData.processCommandLine(&parser); + QCommandLineParser parser; + aboutData.setupCommandLine(&parser); + parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id"))); + parser.addOption(QCommandLineOption(QStringLiteral("message"), i18n("Send a message"), i18n("message"))); + parser.process(app); + aboutData.processCommandLine(&parser); - initialMessage = parser.value(QStringLiteral("message")); - deviceid = parser.value(QStringLiteral("device")); - } + data.m_initialMessage = parser.value(QStringLiteral("message")); + data.m_initialDevice = parser.value(QStringLiteral("device")); KDBusService service(KDBusService::Unique); + QObject::connect(&service, &KDBusService::activateRequested, &service, [&parser, &data](const QStringList &args, const QString &/*workDir*/) { + parser.parse(args); + + data.m_initialMessage = parser.value(QStringLiteral("message")); + data.m_initialDevice = parser.value(QStringLiteral("device")); + + Q_EMIT data.initialDeviceChanged(); + Q_EMIT data.initialMessageChanged(); + }); + qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel"); qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "ConversationModel"); qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel"); qmlRegisterSingletonType("org.kde.kdeconnect.sms", 1, 0, "SmsHelper", SmsHelper::singletonProvider); + qmlRegisterSingletonInstance("org.kde.kdeconnect.sms", 1,0, "AppData", &data); + QQmlApplicationEngine engine; engine.rootContext()->setContextObject(new KLocalizedContext(&engine)); engine.addImageProvider(QStringLiteral("thumbnailsProvider"), new ThumbnailsProvider); engine.rootContext()->setContextProperties({ - { QStringLiteral("initialMessage"), initialMessage }, - { QStringLiteral("initialDevice"), deviceid }, { QStringLiteral("aboutData"), QVariant::fromValue(KAboutData::applicationData()) } }); engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); return app.exec(); } + +#include "main.moc" diff --git a/smsapp/qml/ConversationDisplay.qml b/smsapp/qml/ConversationDisplay.qml index 4efe11d1a..9c3aa9e86 100644 --- a/smsapp/qml/ConversationDisplay.qml +++ b/smsapp/qml/ConversationDisplay.qml @@ -43,7 +43,7 @@ Kirigami.ScrollablePage Component.onCompleted: { if (initialMessage.length > 0) { - messageField.text = initialMessage; + sendingArea.text = initialMessage; initialMessage = "" } if (conversationId == invalidId) { @@ -175,6 +175,8 @@ Kirigami.ScrollablePage } footer: SendingArea { + id: sendingArea + width: parent.width addresses: page.addresses } diff --git a/smsapp/qml/ConversationList.qml b/smsapp/qml/ConversationList.qml index ccaa2b4b9..71ed7fc17 100644 --- a/smsapp/qml/ConversationList.qml +++ b/smsapp/qml/ConversationList.qml @@ -72,9 +72,7 @@ Kirigami.ScrollablePage } } - property string initialMessage - property string initialDevice - property int currentDeviceIndex: -1 + property string initialMessage : AppData.initialMessage header: Kirigami.InlineMessage { Layout.fillWidth: true diff --git a/smsapp/qml/SendingArea.qml b/smsapp/qml/SendingArea.qml index 84ce26061..fafbbe582 100644 --- a/smsapp/qml/SendingArea.qml +++ b/smsapp/qml/SendingArea.qml @@ -17,6 +17,7 @@ ColumnLayout { property var addresses property var selectedFileUrls: [] readonly property int maxMessageSize: 600000 + property alias text: messageField.text MessageDialog { id: messageDialog diff --git a/smsapp/qml/main.qml b/smsapp/qml/main.qml index ae89d3e68..5c9081b50 100644 --- a/smsapp/qml/main.qml +++ b/smsapp/qml/main.qml @@ -18,64 +18,68 @@ Kirigami.ApplicationWindow width: 800 height: 600 - property int currentDeviceIndex - property int devicesCount - property string initialDevice + property alias devicesCount : instantiator.count property QtObject device + property var deviceActions : [] + Component { id: deviceActionComponent Kirigami.Action { - property int deviceIndex + required property string deviceId + required property string name + required property var device + + text: name + onTriggered: { - root.currentDeviceIndex = deviceIndex + root.device = device + AppData.initialDevice = "" } - icon.name: root.currentDeviceIndex === deviceIndex ? "checkmark" : "" + icon.name: root.device === device ? "checkmark" : "" } } - DevicesSortProxyModel { - id: devicesModel - //TODO: make it possible to filter if they can do sms - sourceModel: DevicesModel { displayFilter: DevicesModel.Paired | DevicesModel.Reachable } - function populateDevicesMenu() { - root.globalDrawer.actions[0].children = []; - for (var i = 0; i < devicesModel.rowCount(); i++) { - var dev = devicesModel.data(devicesModel.index(i, 0), DevicesSortProxyModel.DisplayRole); - var obj = deviceActionComponent.createObject(root.globalDrawer.actions[0], { - text: dev, - deviceIndex: i - }); - root.globalDrawer.actions[0].children.push(obj); - } - } - onRowsInserted: { - if (root.currentDeviceIndex < 0) { - if (root.initialDevice) { - root.currentDeviceIndex = devicesModel.rowForDevice(root.initialDevice); - } else { - root.currentDeviceIndex = 0; + Connections { + target: AppData + function onInitialDeviceChanged() { + for (var action of root.deviceActions) { + if (action.deviceId == AppData.initialDevice) { + root.device = action.device } } - root.device = root.currentDeviceIndex >= 0 ? devicesModel.data(devicesModel.index(root.currentDeviceIndex, 0), DevicesModel.DeviceRole) : null - root.devicesCount = devicesModel.rowCount(); - populateDevicesMenu(); - } - onRowsRemoved: { - root.devicesCount = devicesModel.rowCount(); - populateDevicesMenu(); } } - onCurrentDeviceIndexChanged: { - root.device = root.currentDeviceIndex >= 0 ? devicesModel.data(devicesModel.index(root.currentDeviceIndex, 0), DevicesModel.DeviceRole) : null + + Instantiator { + id: instantiator + + model: DevicesSortProxyModel { + id: devicesModel + //TODO: make it possible to filter if they can do sms + sourceModel: DevicesModel { displayFilter: DevicesModel.Paired | DevicesModel.Reachable } + } + + onObjectAdded: (idx, obj) => { + root.deviceActions.push(obj) + root.globalDrawer.actions[0].children = root.deviceActions + + if (!root.device && (AppData.initialDevice == "" || AppData.initialDevice === obj.deviceId)) { + root.device = obj.device + } + } + + onObjectRemoved: (idx, obj) => { + root.deviceActions.splice(idx, 1) + root.globalDrawer.actions[0].children = root.deviceActions + } + + delegate: deviceActionComponent } pageStack.initialPage: ConversationList { title: i18nd("kdeconnect-sms", "KDE Connect SMS") - initialMessage: initialMessage device: root.device; - initialDevice: initialDevice - currentDeviceIndex: root.currentDeviceIndex; devicesCount: root.devicesCount; }