From 9b2ce7538d28529f3c47e713779e4b0ae7b6aaf1 Mon Sep 17 00:00:00 2001 From: link2xt Date: Wed, 28 Jul 2021 10:41:54 +0300 Subject: Add New Chat page --- context.cpp | 19 +++++++++++++++ context.h | 2 ++ qml.qrc | 1 + qml/ChatlistPage.qml | 14 +++++++++++ qml/NewChatPage.qml | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 qml/NewChatPage.qml diff --git a/context.cpp b/context.cpp index cf0d24f..4c9163d 100644 --- a/context.cpp +++ b/context.cpp @@ -62,6 +62,12 @@ Context::getChatlist(int flags) return new DcChatlist{chatlist}; } +uint32_t +Context::createChatByContactId(uint32_t contactId) +{ + return dc_create_chat_by_contact_id(m_context, contactId); +} + void Context::setChatVisibility(uint32_t chatId, int visibility) { @@ -119,6 +125,19 @@ Context::getMsgIdList(uint32_t chatId) { return result; } +QVariantList +Context::getContacts(uint32_t flags, QString query) +{ + QVariantList result; + QByteArray utf8Query = query.toUtf8(); + dc_array_t *contactsArray = dc_get_contacts(m_context, flags, utf8Query.constData()); + for (size_t i = 0; i < dc_array_get_cnt(contactsArray); i++) { + result << dc_array_get_id(contactsArray, i); + } + dc_array_unref(contactsArray); + return result; +} + int Context::getFreshMsgCnt(uint32_t chatId) { return dc_get_fresh_msg_cnt(m_context, chatId); diff --git a/context.h b/context.h index e183699..235bd28 100644 --- a/context.h +++ b/context.h @@ -27,6 +27,7 @@ public: Q_INVOKABLE void stopIo(); Q_INVOKABLE void maybeNetwork(); Q_INVOKABLE DcChatlist *getChatlist(int flags); + Q_INVOKABLE uint32_t createChatByContactId(uint32_t contactId); Q_INVOKABLE void setChatVisibility(uint32_t chatId, int visibility); Q_INVOKABLE void deleteChat(uint32_t chatId); Q_INVOKABLE void blockChat(uint32_t chatId); @@ -34,6 +35,7 @@ public: Q_INVOKABLE QString getChatEncrinfo(uint32_t chatId); Q_INVOKABLE uint32_t getChatEphemeralTimer(uint32_t chatId); Q_INVOKABLE DcChat *getChat(uint32_t chatId); + Q_INVOKABLE QVariantList getContacts(uint32_t flags, QString query); Q_INVOKABLE QVariantList getMsgIdList(uint32_t chatId); Q_INVOKABLE int getFreshMsgCnt(uint32_t chatId); Q_INVOKABLE void marknoticedChat(uint32_t chatId); diff --git a/qml.qrc b/qml.qrc index d915549..4bd7cb1 100644 --- a/qml.qrc +++ b/qml.qrc @@ -9,6 +9,7 @@ qml/Message.qml qml/ComposePane.qml qml/HtmlViewSheet.qml + qml/NewChatPage.qml qtquickcontrols2.conf diff --git a/qml/ChatlistPage.qml b/qml/ChatlistPage.qml index 8348091..a0d171a 100644 --- a/qml/ChatlistPage.qml +++ b/qml/ChatlistPage.qml @@ -36,6 +36,20 @@ Kirigami.ScrollablePage { updateChatlist() } + mainAction: Kirigami.Action { + text: "New chat" + iconName: "list-add" + onTriggered: { + let newChatPageComponent = Qt.createComponent("qrc:/qml/NewChatPage.qml") + if (newChatPageComponent.status == Component.Ready) { + let newChatPage = newChatPageComponent.createObject(pageStack, {context: chatlistPage.context}) + pageStack.layers.push(newChatPage) + } else if (newChatPageComponent.status == Component.Error) { + console.log("Error loading new chat page: " + newChatPageComponent.errorString()) + } + } + } + contextualActions: [ Kirigami.Action { text: "Settings" diff --git a/qml/NewChatPage.qml b/qml/NewChatPage.qml new file mode 100644 index 0000000..cc14185 --- /dev/null +++ b/qml/NewChatPage.qml @@ -0,0 +1,68 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Layouts 1.12 +import org.kde.kirigami 2.12 as Kirigami + +import DeltaChat 1.0 + +Kirigami.ScrollablePage { + id: newChatPageRoot + + title: "New chat" + + required property DcContext context + + function updateContacts() { + let contacts = context.getContacts(0, ""); + + for (let i = 0; i < contacts.length; i++) { + let contactId = contacts[i] + + const item = { + contactId: contactId + } + contactsModel.insert(i, item) + } + } + + Component.onCompleted: { + newChatPageRoot.updateContacts() + } + + ListModel { + id: contactsModel + } + + ListView { + id: contactsList + + anchors.fill: parent + model: contactsModel + currentIndex: -1 + + delegate: Kirigami.BasicListItem { + property DcContact contact: context.getContact(model.contactId) + + label: contact.displayName + subtitle: contact.addr + } + + Kirigami.PlaceholderMessage { + anchors.centerIn: parent + visible: contactsList.count == 0 + text: "You have no contacts in addressbook yet" + } + + onCurrentItemChanged: { + if (currentIndex == -1) { + return; + } + + let contactId = contactsModel.get(contactsList.currentIndex).contactId; + + console.log("Creating chat with " + contactId); + context.createChatByContactId(contactId); + pageStack.layers.pop(); + } + } +} -- cgit v1.2.3-54-g00ecf