[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
This commit is contained in:
Nicolas Fella 2022-05-12 20:34:24 +02:00
parent d76207db04
commit 8010739a8a
6 changed files with 86 additions and 56 deletions

View file

@ -53,6 +53,7 @@ DevicesModel::DevicesModel(QObject* parent)
QHash< int, QByteArray > DevicesModel::roleNames() const QHash< int, QByteArray > DevicesModel::roleNames() const
{ {
QHash<int, QByteArray> names = QAbstractItemModel::roleNames(); QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
names.insert(NameModelRole, "name");
names.insert(IdModelRole, "deviceId"); names.insert(IdModelRole, "deviceId");
names.insert(IconNameRole, "iconName"); names.insert(IconNameRole, "iconName");
names.insert(DeviceRole, "device"); names.insert(DeviceRole, "device");

View file

@ -24,6 +24,20 @@
#include "smshelper.h" #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[]) int main(int argc, char *argv[])
{ {
QIcon::setFallbackThemeName(QStringLiteral("breeze")); QIcon::setFallbackThemeName(QStringLiteral("breeze"));
@ -53,9 +67,8 @@ int main(int argc, char *argv[])
QQuickStyle::setStyle(QStringLiteral("org.kde.desktop")); QQuickStyle::setStyle(QStringLiteral("org.kde.desktop"));
} }
QString initialMessage, deviceid; AppData data;
{
QCommandLineParser parser; QCommandLineParser parser;
aboutData.setupCommandLine(&parser); aboutData.setupCommandLine(&parser);
parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id"))); parser.addOption(QCommandLineOption(QStringLiteral("device"), i18n("Select a device"), i18n("id")));
@ -63,27 +76,38 @@ int main(int argc, char *argv[])
parser.process(app); parser.process(app);
aboutData.processCommandLine(&parser); aboutData.processCommandLine(&parser);
initialMessage = parser.value(QStringLiteral("message")); data.m_initialMessage = parser.value(QStringLiteral("message"));
deviceid = parser.value(QStringLiteral("device")); data.m_initialDevice = parser.value(QStringLiteral("device"));
}
KDBusService service(KDBusService::Unique); 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<ConversationsSortFilterProxyModel>("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel"); qmlRegisterType<ConversationsSortFilterProxyModel>("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<ConversationModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationModel"); qmlRegisterType<ConversationModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationModel");
qmlRegisterType<ConversationListModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel"); qmlRegisterType<ConversationListModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel");
qmlRegisterSingletonType<SmsHelper>("org.kde.kdeconnect.sms", 1, 0, "SmsHelper", SmsHelper::singletonProvider); qmlRegisterSingletonType<SmsHelper>("org.kde.kdeconnect.sms", 1, 0, "SmsHelper", SmsHelper::singletonProvider);
qmlRegisterSingletonInstance<AppData>("org.kde.kdeconnect.sms", 1,0, "AppData", &data);
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.rootContext()->setContextObject(new KLocalizedContext(&engine)); engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
engine.addImageProvider(QStringLiteral("thumbnailsProvider"), new ThumbnailsProvider); engine.addImageProvider(QStringLiteral("thumbnailsProvider"), new ThumbnailsProvider);
engine.rootContext()->setContextProperties({ engine.rootContext()->setContextProperties({
{ QStringLiteral("initialMessage"), initialMessage },
{ QStringLiteral("initialDevice"), deviceid },
{ QStringLiteral("aboutData"), QVariant::fromValue(KAboutData::applicationData()) } { QStringLiteral("aboutData"), QVariant::fromValue(KAboutData::applicationData()) }
}); });
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
return app.exec(); return app.exec();
} }
#include "main.moc"

View file

@ -43,7 +43,7 @@ Kirigami.ScrollablePage
Component.onCompleted: { Component.onCompleted: {
if (initialMessage.length > 0) { if (initialMessage.length > 0) {
messageField.text = initialMessage; sendingArea.text = initialMessage;
initialMessage = "" initialMessage = ""
} }
if (conversationId == invalidId) { if (conversationId == invalidId) {
@ -175,6 +175,8 @@ Kirigami.ScrollablePage
} }
footer: SendingArea { footer: SendingArea {
id: sendingArea
width: parent.width width: parent.width
addresses: page.addresses addresses: page.addresses
} }

View file

@ -72,9 +72,7 @@ Kirigami.ScrollablePage
} }
} }
property string initialMessage property string initialMessage : AppData.initialMessage
property string initialDevice
property int currentDeviceIndex: -1
header: Kirigami.InlineMessage { header: Kirigami.InlineMessage {
Layout.fillWidth: true Layout.fillWidth: true

View file

@ -17,6 +17,7 @@ ColumnLayout {
property var addresses property var addresses
property var selectedFileUrls: [] property var selectedFileUrls: []
readonly property int maxMessageSize: 600000 readonly property int maxMessageSize: 600000
property alias text: messageField.text
MessageDialog { MessageDialog {
id: messageDialog id: messageDialog

View file

@ -18,64 +18,68 @@ Kirigami.ApplicationWindow
width: 800 width: 800
height: 600 height: 600
property int currentDeviceIndex property alias devicesCount : instantiator.count
property int devicesCount
property string initialDevice
property QtObject device property QtObject device
property var deviceActions : []
Component { Component {
id: deviceActionComponent id: deviceActionComponent
Kirigami.Action { Kirigami.Action {
property int deviceIndex required property string deviceId
required property string name
required property var device
text: name
onTriggered: { onTriggered: {
root.currentDeviceIndex = deviceIndex root.device = device
AppData.initialDevice = ""
} }
icon.name: root.currentDeviceIndex === deviceIndex ? "checkmark" : "" icon.name: root.device === device ? "checkmark" : ""
} }
} }
DevicesSortProxyModel { Connections {
target: AppData
function onInitialDeviceChanged() {
for (var action of root.deviceActions) {
if (action.deviceId == AppData.initialDevice) {
root.device = action.device
}
}
}
}
Instantiator {
id: instantiator
model: DevicesSortProxyModel {
id: devicesModel id: devicesModel
//TODO: make it possible to filter if they can do sms //TODO: make it possible to filter if they can do sms
sourceModel: DevicesModel { displayFilter: DevicesModel.Paired | DevicesModel.Reachable } sourceModel: DevicesModel { displayFilter: DevicesModel.Paired | DevicesModel.Reachable }
function populateDevicesMenu() { }
root.globalDrawer.actions[0].children = [];
for (var i = 0; i < devicesModel.rowCount(); i++) { onObjectAdded: (idx, obj) => {
var dev = devicesModel.data(devicesModel.index(i, 0), DevicesSortProxyModel.DisplayRole); root.deviceActions.push(obj)
var obj = deviceActionComponent.createObject(root.globalDrawer.actions[0], { root.globalDrawer.actions[0].children = root.deviceActions
text: dev,
deviceIndex: i if (!root.device && (AppData.initialDevice == "" || AppData.initialDevice === obj.deviceId)) {
}); root.device = obj.device
root.globalDrawer.actions[0].children.push(obj);
} }
} }
onRowsInserted: {
if (root.currentDeviceIndex < 0) { onObjectRemoved: (idx, obj) => {
if (root.initialDevice) { root.deviceActions.splice(idx, 1)
root.currentDeviceIndex = devicesModel.rowForDevice(root.initialDevice); root.globalDrawer.actions[0].children = root.deviceActions
} else {
root.currentDeviceIndex = 0;
} }
}
root.device = root.currentDeviceIndex >= 0 ? devicesModel.data(devicesModel.index(root.currentDeviceIndex, 0), DevicesModel.DeviceRole) : null delegate: deviceActionComponent
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
} }
pageStack.initialPage: ConversationList { pageStack.initialPage: ConversationList {
title: i18nd("kdeconnect-sms", "KDE Connect SMS") title: i18nd("kdeconnect-sms", "KDE Connect SMS")
initialMessage: initialMessage
device: root.device; device: root.device;
initialDevice: initialDevice
currentDeviceIndex: root.currentDeviceIndex;
devicesCount: root.devicesCount; devicesCount: root.devicesCount;
} }