summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2026-02-03 16:19:53 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2026-02-03 16:26:52 +0100
commit0311bed34d80abe518f11838271e39d1615ebf67 (patch)
treed89854d97ba7b326909e10027d863d677dc5b15d
parentfdb64c59865e4db76addfb8222f6421443e25240 (diff)
downloadyachat6-0311bed34d80abe518f11838271e39d1615ebf67.tar.gz
Import xxcc.{cpp,h} as yc.{cpp,h}
-rw-r--r--CMakeLists.txt5
-rw-r--r--ContactList.qml3
-rw-r--r--Main.qml13
-rw-r--r--main.cpp19
-rw-r--r--test.h37
-rw-r--r--yc.cpp194
-rw-r--r--yc.h37
7 files changed, 247 insertions, 61 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index be9668a..8f3bcb9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,13 +28,14 @@ qt_add_executable(${PROJECT_NAME}
main.cpp
omemo_db.cpp
omemo_db.h
- test.h
trust_db.cpp
trust_db.h
+ yc.cpp
+ yc.h
)
qt_add_qml_module(${PROJECT_NAME}
- URI org.yachat.lib
+ URI org.yachat.app
VERSION 1.0
DEPENDENCIES QtQuick
QML_FILES
diff --git a/ContactList.qml b/ContactList.qml
index ba552c3..f75aca6 100644
--- a/ContactList.qml
+++ b/ContactList.qml
@@ -19,13 +19,12 @@ Page
ListView
{
width: parent.width
- model: ["Alice", "Bob"]
anchors.fill: parent
delegate: ItemDelegate
{
width: parent.width
text: modelData
- onClicked: stack.push("qrc:/org/yachat/lib/ChatView.qml",
+ onClicked: stack.push("qrc:/org/yachat/app/ChatView.qml",
{
destination: modelData
})
diff --git a/Main.qml b/Main.qml
index 71472a3..7c5f217 100644
--- a/Main.qml
+++ b/Main.qml
@@ -2,7 +2,7 @@ import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
-import org.yachat.lib 1.0
+import org.yachat.app 1.0
ApplicationWindow
{
@@ -31,10 +31,11 @@ ApplicationWindow
Component.onCompleted:
{
- Test.awesome = true;
- Test.updateAwesomeness(true);
- console.log("awesome is ", Test.isAwesome);
- Test.updateAwesomeness(false);
- console.log("awesome is ", Test.isAwesome);
+ Yc.init()
+ // Test.awesome = true;
+ // Test.updateAwesomeness(true);
+ // console.log("awesome is ", Test.isAwesome);
+ // Test.updateAwesomeness(false);
+ // console.log("awesome is ", Test.isAwesome);
}
}
diff --git a/main.cpp b/main.cpp
index c0aab91..fd9a56e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -4,28 +4,19 @@
#include <QObject>
#include <QVariant>
#include <memory>
-#include "test.h"
+#include "yc.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
- QQmlApplicationEngine engine("qrc:/org/yachat/lib/Main.qml");
+ QQmlApplicationEngine engine("qrc:/org/yachat/app/Main.qml");
-#if 0
- qRegisterMetaType<Test>("Test");
-
- qmlRegisterType<Test>("org.yachat.lib", 1, 0, "Test");
-#endif
-#if 1
- qmlRegisterSingletonType<Test>("org.yachat.lib", 1, 0, "Test",
+ qmlRegisterSingletonType<Yc>("org.yachat.app", 1, 0, "Yc",
[](QQmlEngine *, QJSEngine *) -> QObject *
{
- return std::make_unique<Test>().release();
+ return std::make_unique<Yc>().release();
}
);
-#else
- Test test;
- engine.rootContext()->setContextProperty("Test", &test);
-#endif
+
return app.exec();
}
diff --git a/test.h b/test.h
deleted file mode 100644
index 30f3a8a..0000000
--- a/test.h
+++ /dev/null
@@ -1,37 +0,0 @@
-#ifndef TEST_H
-#define TEST_H
-
-#include <QGuiApplication>
-#include <QQmlApplicationEngine>
-#include <QQmlContext>
-#include <QObject>
-#include <QDebug>
-
-class Test : public QObject
-{
- Q_OBJECT
- QML_SINGLETON
- QML_ELEMENT
- Q_PROPERTY(bool isAwesome READ isAwesome NOTIFY updateAwesomeness);
-public:
- Test(QObject *parent = nullptr) : QObject(parent)
- {
- connect(this, &Test::updateAwesomeness, this,
- [this](bool value)
- {
- qDebug() << "hey!";
- awesome = value;
- });
- }
- bool awesome;
- bool isAwesome() const
- {
- qDebug() << "this is awesome!";
- return awesome;
- }
-
-signals:
- void updateAwesomeness(bool value);
-};
-
-#endif
diff --git a/yc.cpp b/yc.cpp
new file mode 100644
index 0000000..fc132c1
--- /dev/null
+++ b/yc.cpp
@@ -0,0 +1,194 @@
+#include "yc.h"
+#include "client.h"
+#include "direction.h"
+#include <QXmppMessage.h>
+#include <QXmppOmemoElement_p.h>
+#include <QXmppRosterManager.h>
+#include <QXmppUtils.h>
+#include <iostream>
+#include <stdexcept>
+#include <utility>
+#include <variant>
+
+void Yc::connectAccounts(const QList<Credentials::Pair> &pairs)
+{
+ for (const auto &p : pairs)
+ {
+ QXmppConfiguration cfg;
+
+ cfg.setStreamSecurityMode(QXmppConfiguration::TLSRequired);
+ cfg.setJid(p.first);
+ cfg.setPassword(p.second);
+ cfg.setAutoReconnectionEnabled(true);
+
+ const auto client = new Client(p.first);
+
+ addAccount(client);
+ client->connectToServer(cfg);
+ }
+}
+
+void Yc::startChat(const QString from, const QString to)
+{
+ bool found = false;
+
+#if 0
+ for (int i = 0; i < ui.conversations_list->count(); i++)
+ {
+ const auto it =
+ static_cast<const Conversation *>(ui.conversations_list->item(i));
+
+ if (it->from == from && it->to == to)
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ new Conversation(from, to, ui.conversations_list);
+
+ for (const auto c : clients)
+ if (c->jidBare() == from)
+ {
+ selected = c;
+ break;
+ }
+
+ ui.sw->setCurrentIndex(Tab::Chat);
+ ui.jid->setText(to);
+ ui.messages->scrollToBottom();
+#endif
+}
+
+void Yc::addInMessage(const QXmppMessage &msg)
+{
+#if 0
+ new Message(msg.body(), msg.stamp().toLocalTime(), Direction::In,
+ ui.messages);
+#endif
+}
+
+void Yc::addOutMessage(const QString &msg, const QDateTime &dt)
+{
+#if 0
+ new Message(msg, dt.toLocalTime(), Direction::Out,
+ ui.messages);
+#endif
+}
+
+void Yc::addAccount(Client *const c)
+{
+ c->configuration().setAutoReconnectionEnabled(true);
+ clients.append(c);
+
+ c->connect(c, &Client::messageReceived, this,
+ [this] (QXmppMessage msg)
+ {
+ if (msg.body().isEmpty())
+ return;
+ else if (msg.stamp().isNull())
+ msg.setStamp(QDateTime::currentDateTimeUtc());
+
+ storeMessage(msg, Direction::In);
+
+ if (selected)
+ addInMessage(msg);
+ });
+
+ const auto roster = c->findExtension<QXmppRosterManager>();
+
+ if (roster)
+ roster->connect(roster, &QXmppRosterManager::rosterReceived, c,
+ [this, c, roster]
+ {
+ c->database().addToRoster(roster->getRosterBareJids());
+ });
+ else
+ throw std::runtime_error("Expected non-null QXmppRosterManager");
+}
+
+void Yc::storeMessage(const QString &from, const QString &to,
+ const QString &msg, const QDateTime &dt, const Direction dir) const
+{
+ QString jid, contact;
+
+ switch (dir)
+ {
+ case Direction::In:
+ jid = QXmppUtils::jidToBareJid(to);
+ contact = QXmppUtils::jidToBareJid(from);
+ break;
+
+ case Direction::Out:
+ jid = from;
+ contact = to;
+ break;
+ }
+
+ for (const auto c : clients)
+ {
+ if (c->jidBare() == jid)
+ {
+ const auto &db = c->database();
+ JidDb::Message m;
+
+ m.body = msg;
+ m.dt = dt;
+ m.direction = dir;
+ m.contact = contact;
+ db.store(m);
+ }
+ }
+}
+
+void Yc::storeMessage(const QXmppMessage &msg, const Direction dir) const
+{
+ storeMessage(msg.from(), msg.to(), msg.body(), msg.stamp(), dir);
+}
+
+void Yc::retrieveConversations() const
+{
+#if 0
+ for (const auto c : clients)
+ {
+ const auto &db = c->database();
+
+ for (const auto &conv : db.conversations())
+ new Conversation(db.jid, conv.to,
+ ui.conversations_list, conv.last_msg, conv.dt);
+ }
+#endif
+}
+
+void Yc::init()
+{
+ creds.load().then(this,
+ [=] (Credentials::PairListResult &&result)
+ {
+ if (std::holds_alternative<Credentials::PairList>(result))
+ {
+ const auto &pairs = std::get<Credentials::PairList>(result);
+
+ connectAccounts(pairs);
+ retrieveConversations();
+ }
+ else if (std::holds_alternative<Credentials::Error>(result))
+ {
+ const auto &error = std::get<Credentials::Error>(result);
+
+ std::cerr << qPrintable(error.description) << std::endl;
+ }
+ });
+}
+
+Yc::~Yc()
+{
+ for (const auto c : clients)
+ delete c;
+}
+
+Yc::Yc(QObject *parent) :
+ QObject(parent)
+{
+}
diff --git a/yc.h b/yc.h
new file mode 100644
index 0000000..3d07176
--- /dev/null
+++ b/yc.h
@@ -0,0 +1,37 @@
+#ifndef YC_H
+#define YC_H
+
+#include "credentials.h"
+#include <QQmlApplicationEngine>
+#include <QObject>
+#include <QList>
+
+class Yc : public QObject
+{
+ Q_OBJECT
+ QML_SINGLETON
+ QML_ELEMENT
+
+public:
+ Yc(QObject *parent = nullptr);
+ ~Yc();
+ Q_INVOKABLE void init();
+
+private:
+ void connectAccounts(const QList<Credentials::Pair> &pairs);
+ void startChat(QString from, QString to);
+ void addInMessage(const QXmppMessage &msg);
+ void addOutMessage(const QString &msg, const QDateTime &dt);
+ void addAccount(Client *c);
+ void storeMessage(const QString &from, const QString &to,
+ const QString &msg, const QDateTime &dt, const Direction dir) const;
+ void storeMessage(const QXmppMessage &msg, const Direction dir) const;
+ void retrieveConversations() const;
+
+ enum Tab {Conversations, Chat};
+ QList<Client *> clients;
+ Credentials creds;
+ Client *selected;
+};
+
+#endif