[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> names = QAbstractItemModel::roleNames();
names.insert(NameModelRole, "name");
names.insert(IdModelRole, "deviceId");
names.insert(IconNameRole, "iconName");
names.insert(DeviceRole, "device");

View file

@ -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<ConversationsSortFilterProxyModel>("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<ConversationModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationModel");
qmlRegisterType<ConversationListModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel");
qmlRegisterSingletonType<SmsHelper>("org.kde.kdeconnect.sms", 1, 0, "SmsHelper", SmsHelper::singletonProvider);
qmlRegisterSingletonInstance<AppData>("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"

View file

@ -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
}

View file

@ -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

View file

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

View file

@ -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;
}