From b628f485ff1cc66c20b9888b73e3427eb86d062e Mon Sep 17 00:00:00 2001
From: Chethan2k1 <40890937+Chethan2k1@users.noreply.github.com>
Date: Thu, 4 Jun 2020 19:14:15 +0530
Subject: [PATCH] Tweak UI for device verification and Add more slots

---
 resources/qml/UserProfile.qml                 | 38 +++++++---
 .../DeviceVerification.qml                    | 41 ++++++++++-
 src/DeviceVerificationFlow.cpp                | 70 ++++++++++++++++++-
 src/DeviceVerificationFlow.h                  | 10 ++-
 4 files changed, 146 insertions(+), 13 deletions(-)

diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml
index f29fb4c16..a85c41c33 100644
--- a/resources/qml/UserProfile.qml
+++ b/resources/qml/UserProfile.qml
@@ -5,6 +5,8 @@ import QtQuick.Window 2.3
 
 import im.nheko 1.0
 
+import "./device-verification"
+
 ApplicationWindow{
     property var user_data
     property var colors: currentActivePalette
@@ -20,7 +22,6 @@ ApplicationWindow{
         id: userProfileList
         userId: user_data.userId
         onUserIdChanged : {
-            console.log(userId)
             userProfileList.updateDeviceList()
         }
         onDeviceListUpdated : {
@@ -84,15 +85,34 @@ ApplicationWindow{
 
                     delegate: RowLayout{
                         width: parent.width
-                        Text{
-                            Layout.fillWidth: true
-                            color: colors.text
-                            text: deviceID
+                        ColumnLayout{
+                            Text{
+                                Layout.fillWidth: true
+                                color: colors.text
+                                Layout.alignment: Qt.AlignRight
+                                text: deviceID
+                            }
+                            Text{
+                                Layout.fillWidth: true
+                                color:colors.text
+                                Layout.alignment: Qt.AlignRight
+                                text: displayName
+                            }
+                            Component {
+			                    id: deviceVerificationDialog
+			                    DeviceVerification {}
+		                    }
+                            DeviceVerificationFlow {
+                                id: deviceVerificationFlow
+                            }
                         }
-                        Text{
-                            Layout.fillWidth: true
-                            color:colors.text
-                            text: displayName
+                        Button{
+                            text:"Verify"
+                            onClicked: {
+								var dialog = deviceVerificationDialog.createObject(userProfileDialog, 
+                                    {flow: deviceVerificationFlow,sender: true});
+				                dialog.show();
+                            }
                         }
                     }
                 }
diff --git a/resources/qml/device-verification/DeviceVerification.qml b/resources/qml/device-verification/DeviceVerification.qml
index dd637e596..ce2485ff1 100644
--- a/resources/qml/device-verification/DeviceVerification.qml
+++ b/resources/qml/device-verification/DeviceVerification.qml
@@ -7,6 +7,7 @@ import Qt.labs.settings 1.0
 import im.nheko 1.0
 
 ApplicationWindow {
+    property bool sender: true
 	title: stack.currentItem.title
 	id: dialog
 
@@ -24,7 +25,7 @@ ApplicationWindow {
 	width: stack.implicitWidth
 	StackView {
 		id: stack
-		initialItem: newVerificationRequest
+		initialItem: sender == true?newVerificationRequest:acceptNewVerificationRequest
 		implicitWidth: currentItem.implicitWidth
 		implicitHeight: currentItem.implicitHeight
 	}
@@ -47,7 +48,7 @@ ApplicationWindow {
 	Component {
 		id: newVerificationRequest
 		Pane {
-			property string title: "Device Verification Request"
+			property string title: "Sending Device Verification Request"
 			ColumnLayout {
 				spacing: 16
 				Label {
@@ -82,6 +83,42 @@ ApplicationWindow {
 					Button {
 						Layout.alignment: Qt.AlignRight
 						text: "Start verification"
+						onClicked: { stack.replace(awaitingVerificationRequestAccept); flow.sendVerificationRequest(); }
+					}
+				}
+			}
+		}
+	}
+
+	Component {
+		id: acceptNewVerificationRequest
+		Pane {
+			property string title: "Recieving Device Verification Request"
+			ColumnLayout {
+				spacing: 16
+
+				Label {
+					Layout.maximumWidth: 400
+					Layout.fillHeight: true
+					Layout.fillWidth: true
+					wrapMode: Text.Wrap
+					text: "The device was requested to be verified"
+
+					verticalAlignment: Text.AlignVCenter
+				}
+
+				RowLayout {
+					Button {
+						Layout.alignment: Qt.AlignLeft
+						text: "Deny"
+						onClicked: { dialog.close(); flow.cancelVerification(); }
+					}
+					Item {
+						Layout.fillWidth: true
+					}
+					Button {
+						Layout.alignment: Qt.AlignRight
+						text: "Accept"
 						onClicked: { stack.replace(awaitingVerificationRequestAccept); flow.acceptVerificationRequest(); }
 					}
 				}
diff --git a/src/DeviceVerificationFlow.cpp b/src/DeviceVerificationFlow.cpp
index 69d6ab9cc..12e31c048 100644
--- a/src/DeviceVerificationFlow.cpp
+++ b/src/DeviceVerificationFlow.cpp
@@ -1,5 +1,8 @@
 #include "DeviceVerificationFlow.h"
 
+#include <MatrixClient.h>
+#include <QDateTime>
+#include <QDebug> // only for debugging
 #include <QTimer>
 
 static constexpr int TIMEOUT = 2 * 60 * 1000; // 2 minutes
@@ -15,17 +18,80 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *)
         timeout->start(TIMEOUT);
 }
 
-//! accepts a verification and starts the verification flow
+std::string
+DeviceVerificationFlow::generate_txn_id()
+{
+        this->transaction_id = mtx::client::utils::random_token(32, false);
+        return this->transaction_id;
+}
+
+//! accepts a verification
 void
 DeviceVerificationFlow::acceptVerificationRequest()
 {
+        mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationAccept> body;
+        mtx::events::msg::KeyVerificationAccept req;
+
+        req.transaction_id              = this->transaction_id;
+        req.method                      = mtx::events::msg::VerificationMethods::SASv1;
+        req.key_agreement_protocol      = "";
+        req.hash                        = "";
+        req.message_authentication_code = "";
+        // req.short_authentication_string = "";
+        req.commitment = "";
+
         emit verificationRequestAccepted(rand() % 2 ? Emoji : Decimal);
+
+        // Yet to add send to_device message
+}
+//! starts the verification flow
+void
+DeviceVerificationFlow::startVerificationRequest()
+{
+        mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationAccept> body;
+        mtx::events::msg::KeyVerificationAccept req;
+
+        // req.from_device = "";
+        req.transaction_id         = this->transaction_id;
+        req.method                 = mtx::events::msg::VerificationMethods::SASv1;
+        req.key_agreement_protocol = {};
+        // req.hashes = {};
+        req.message_authentication_code = {};
+        // req.short_authentication_string = "";
+
+        // Yet to add send to_device message
+}
+//! sends a verification request
+void
+DeviceVerificationFlow::sendVerificationRequest()
+{
+        QDateTime CurrentTime = QDateTime::currentDateTimeUtc();
+
+        mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationRequest> body;
+        mtx::events::msg::KeyVerificationRequest req;
+
+        req.from_device    = "";
+        req.transaction_id = generate_txn_id();
+        req.methods.resize(1);
+        req.methods[0] = mtx::events::msg::VerificationMethods::SASv1;
+        req.timestamp  = (uint64_t)CurrentTime.toTime_t();
+
+        // Yet to add send to_device message
 }
 //! cancels a verification flow
 void
 DeviceVerificationFlow::cancelVerification()
 {
+        mtx::requests::ToDeviceMessages<mtx::events::msg::KeyVerificationCancel> body;
+        mtx::events::msg::KeyVerificationCancel req;
+
+        req.transaction_id = this->transaction_id;
+        req.reason         = "";
+        req.code           = "";
+
         this->deleteLater();
+
+        // Yet to add send to_device message
 }
 //! Completes the verification flow
 void
@@ -33,4 +99,6 @@ DeviceVerificationFlow::acceptDevice()
 {
         emit deviceVerified();
         this->deleteLater();
+
+        // Yet to add send to_device message
 }
diff --git a/src/DeviceVerificationFlow.h b/src/DeviceVerificationFlow.h
index 038f1e13a..71c40cd5e 100644
--- a/src/DeviceVerificationFlow.h
+++ b/src/DeviceVerificationFlow.h
@@ -20,8 +20,12 @@ public:
         DeviceVerificationFlow(QObject *parent = nullptr);
 
 public slots:
-        //! accepts a verification and starts the verification flow
+        //! sends a verification request
+        void sendVerificationRequest();
+        //! accepts a verification
         void acceptVerificationRequest();
+        //! starts the verification flow
+        void startVerificationRequest();
         //! cancels a verification flow
         void cancelVerification();
         //! Completes the verification flow
@@ -34,5 +38,9 @@ signals:
         void verificationCanceled();
 
 private:
+        //! generates a unique transaction id
+        std::string generate_txn_id();
+
         QTimer *timeout = nullptr;
+        std::string transaction_id;
 };
-- 
GitLab