From 838cf63578d2145e9264bd37e68993f6e6e8d132 Mon Sep 17 00:00:00 2001
From: Malte E <malte.e@mailbox.org>
Date: Sat, 26 Mar 2022 22:25:48 +0100
Subject: [PATCH] direct chat creator can now create direct chats

---
 resources/qml/dialogs/CreateDirect.qml | 38 +++++++++++++++++---------
 src/timeline/TimelineViewManager.cpp   |  8 ++++++
 src/timeline/TimelineViewManager.h     |  1 +
 3 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/resources/qml/dialogs/CreateDirect.qml b/resources/qml/dialogs/CreateDirect.qml
index bbb758e38..b76e728a7 100644
--- a/resources/qml/dialogs/CreateDirect.qml
+++ b/resources/qml/dialogs/CreateDirect.qml
@@ -8,29 +8,38 @@ import QtQuick 2.15
 import QtQuick.Window 2.13
 import QtQuick.Layouts 1.3
 import QtQuick.Controls 2.3
+import QtQml.Models 2.15
 import im.nheko 1.0
 
 ApplicationWindow {
     id: createDirectRoot
     title: qsTr("Create Direct Chat")
-    property var profile: null
+    property var profile
+    property bool otherUserHasE2ee: profile? dMod.count > 0 : true
     minimumHeight: layout.implicitHeight+2*layout.anchors.margins+footer.height
     minimumWidth: footer.width
+
+    DelegateModel {
+        id: dMod
+        model: profile? profile.deviceList : undefined
+    }
+
     ColumnLayout {
         id: layout
         anchors.fill: parent
         anchors.margins: Nheko.paddingSmall
         MatrixTextField {
             id: userID
+            property bool isValidMxid: text.match("@.+?:.{3,}")
             Layout.fillWidth: true
             focus: true
             placeholderText: qsTr("Name")
-            /*onTextChanged: {
-                if(isValidMxid(text))
-                    profile = getProfile(text);
-                else
+            onTextChanged: {
+                if(isValidMxid) {
+                    profile = TimelineManager.getGlobalUserProfile(text);
+                } else
                     profile = null;
-            }*/
+            }
         }
 
         GridLayout {
@@ -39,21 +48,20 @@ ApplicationWindow {
             columns: 2
             rowSpacing: Nheko.paddingSmall
             columnSpacing: Nheko.paddingMedium
-            anchors.centerIn: parent
 
             Avatar {
                 Layout.rowSpan: 2
                 Layout.preferredWidth: Nheko.avatarSize
                 Layout.preferredHeight: Nheko.avatarSize
                 Layout.alignment: Qt.AlignLeft
-                userid: profile.mxid
-                url: profile.avatarUrl.replace("mxc://", "image://MxcImage/")
-                displayName: profile.displayName
+                userid: profile? profile.mxid : ""
+                url: profile? profile.avatarUrl.replace("mxc://", "image://MxcImage/") : null
+                displayName: profile? profile.displayName : ""
                 enabled: false
             }
             Label {
                 Layout.fillWidth: true
-                text: "John Smith" //profile.displayName
+                text: profile? profile.displayName : ""
                 color: TimelineManager.userColor(userID.text, Nheko.colors.window)
                 font.pointSize: fontMetrics.font.pointSize
             }
@@ -76,7 +84,7 @@ ApplicationWindow {
             ToggleButton {
                 Layout.alignment: Qt.AlignRight
                 id: encryption
-                checked: true
+                checked: otherUserHasE2ee
             }
         }
     }
@@ -85,8 +93,12 @@ ApplicationWindow {
         Button {
             text: "Start Direct Chat"
             DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
+            enabled: userID.isValidMxid
         }
         onRejected: createDirectRoot.close();
-        //onAccepted: createRoom(newRoomName.text, newRoomTopic.text, newRoomAlias.text, newRoomVisibility.index, newRoomPreset.index)
+        onAccepted: {
+            profile.startChat()
+            createDirectRoot.close()
+        }
     }
 }
diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp
index a18f3ee2c..72f89fd49 100644
--- a/src/timeline/TimelineViewManager.cpp
+++ b/src/timeline/TimelineViewManager.cpp
@@ -199,6 +199,14 @@ TimelineViewManager::openGlobalUserProfile(QString userId)
     emit openProfile(profile);
 }
 
+UserProfile*
+TimelineViewManager::getGlobalUserProfile(QString userId)
+{
+    UserProfile *profile = new UserProfile{QString{}, userId, this};
+    QQmlEngine::setObjectOwnership(profile, QQmlEngine::JavaScriptOwnership);
+    return(profile);
+}
+
 void
 TimelineViewManager::setVideoCallItem()
 {
diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h
index 393b14790..807fe76fb 100644
--- a/src/timeline/TimelineViewManager.h
+++ b/src/timeline/TimelineViewManager.h
@@ -67,6 +67,7 @@ public:
     Q_INVOKABLE void openRoomSettings(QString room_id);
     Q_INVOKABLE void openInviteUsers(QString roomId);
     Q_INVOKABLE void openGlobalUserProfile(QString userId);
+    Q_INVOKABLE UserProfile* getGlobalUserProfile(QString userId);
 
     Q_INVOKABLE void focusMessageInput();
 
-- 
GitLab