First commit

This commit is contained in:
Xavier Del Campo Romero 2023-06-12 23:47:17 +02:00
parent f25f3e8f6e
commit 06d2e36000
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
35 changed files with 2290 additions and 0 deletions

74
.gitignore vendored Normal file
View File

@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
build*/

95
CMakeLists.txt Normal file
View File

@ -0,0 +1,95 @@
cmake_minimum_required(VERSION 3.13.5)
project(xxcc VERSION 0.1 LANGUAGES CXX)
add_compile_options(-fdata-sections -ffunction-sections)
set(QT_VERSION_MAJOR 5)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# QXmpp requires C++17.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Sql REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Sql REQUIRED)
set(PROJECT_SOURCES
account.cpp
account.ui
accounts.cpp
accounts.ui
client.cpp
contact.cpp
contact.ui
contacts.cpp
contacts.ui
conversation.cpp
conversation.ui
credentials.cpp
jiddb.cpp
login.cpp
login.ui
main.cpp
message.cpp
message.ui
xxcc.cpp
xxcc.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(${PROJECT_NAME}
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(${PROJECT_NAME} SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(${PROJECT_NAME}
${PROJECT_SOURCES}
)
endif()
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(qca REQUIRED IMPORTED_TARGET qca2-qt${QT_VERSION_MAJOR})
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::qca)
find_package(Qt5Keychain REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5Keychain::Qt5Keychain)
target_link_libraries(${PROJECT_NAME} PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Sql)
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--gc-sections)
set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(${PROJECT_NAME})
endif()
add_subdirectory(qxmpp)
#target_compile_definitions(${PROJECT_NAME} PUBLIC QT_NO_KEYWORDS)
target_compile_options(${PROJECT_NAME} PUBLIC -Wall)

3
README.md Normal file
View File

@ -0,0 +1,3 @@
```
# apt install qtkeychain-qt5-dev qttools5-dev-tools qtbase5-dev
```

25
account.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "account.h"
Account::Account(Client *const c, QListWidget *const list,
QWidget *const parent) :
QWidget(parent)
{
const auto it = new QListWidgetItem(list);
const auto w = new QWidget;
ui.setupUi(w);
ui.jid->setText(c->configuration().jidBare());
ui.connected->setChecked(
c->state() == Client::State::ConnectedState);
connect(c, &Client::stateChanged, this,
[this] (const Client::State state)
{
ui.connected->setChecked(
state == Client::State::ConnectedState);
});
it->setSizeHint(w->sizeHint());
list->addItem(it);
list->setItemWidget(it, w);
}

18
account.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "client.h"
#include "ui_account.h"
#include <QListWidget>
#include <QWidget>
class Account : public QWidget
{
public:
Account(Client *c, QListWidget *list, QWidget *parent = nullptr);
private:
Ui_account ui;
};
#endif

63
account.ui Normal file
View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>account</class>
<widget class="QWidget" name="account">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>139</width>
<height>36</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="jid">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="alias">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="connected">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Connected</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

39
accounts.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "accounts.h"
#include "account.h"
#include "login.h"
#include <utility>
#include <QListWidgetItem>
#include <QScroller>
Accounts::Accounts(const QList<Client *> &accounts,
QWidget *const parent) :
QDialog(parent)
{
ui.setupUi(this);
QScroller::grabGesture(ui.accounts_list, QScroller::TouchGesture);
for (const auto &a : accounts)
add(a);
connect(ui.add, &QPushButton::released, this,
[this]
{
Login l(this);
l.connect(&l, &Login::auth_success, this,
[this] (Client *c)
{
add(c);
Q_EMIT new_account(c);
});
l.exec();
});
showMaximized();
}
void Accounts::add(Client *c)
{
new Account(c, ui.accounts_list, this);
}

26
accounts.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef ACCOUNTS_H
#define ACCOUNTS_H
#include "client.h"
#include "ui_accounts.h"
#include <QDialog>
#include <QWidget>
#include <QVBoxLayout>
#include <QString>
class Accounts : public QDialog
{
Q_OBJECT
public:
Accounts(const QList<Client *> &accounts, QWidget *parent = nullptr);
Q_SIGNALS:
void new_account(Client *c);
private:
void add(Client *c);
Ui_accounts ui;
};
#endif

98
accounts.ui Normal file
View File

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>accounts</class>
<widget class="QDialog" name="accounts">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>188</width>
<height>215</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>100</horstretch>
<verstretch>100</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>xxcc - Accounts</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="close">
<property name="maximumSize">
<size>
<width>32</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>&lt;</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;Accounts&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="accounts_list">
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="rm">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="add">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>close</sender>
<signal>released()</signal>
<receiver>accounts</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>48</x>
<y>147</y>
</hint>
<hint type="destinationlabel">
<x>138</x>
<y>86</y>
</hint>
</hints>
</connection>
</connections>
</ui>

8
client.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "client.h"
#include <QXmppMamManager.h>
Client::Client(QObject *const parent) :
QXmppClient(parent)
{
// addExtension(new QXmppMamManager);
}

12
client.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef CLIENT_H
#define CLIENT_H
#include <QXmppClient.h>
class Client : public QXmppClient
{
public:
Client(QObject *parent = nullptr);
};
#endif

16
contact.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "contact.h"
Contact::Contact(const QString &own, const QString &other,
QListWidget *const list, QWidget *const parent) :
QListWidgetItem(list),
QWidget(parent),
own(own),
other(other)
{
ui.setupUi(this);
ui.own_jid->setText(own);
ui.contact_jid->setText(other);
QListWidgetItem::setSizeHint(QWidget::sizeHint());
list->addItem(this);
list->setItemWidget(this, this);
}

20
contact.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef CONTACT_H
#define CONTACT_H
#include "ui_contact.h"
#include <QListWidgetItem>
#include <QString>
#include <QWidget>
class Contact : public QListWidgetItem, public QWidget
{
public:
Contact(const QString &own, const QString &other,
QListWidget *list, QWidget *parent = nullptr);
const QString own, other;
private:
Ui_contact ui;
};
#endif

64
contact.ui Normal file
View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>contact</class>
<widget class="QWidget" name="contact">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>117</width>
<height>60</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="picture">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Picture</string>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="contact_jid">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Contact</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="own_jid">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Own</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

50
contacts.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "contacts.h"
#include "contact.h"
#include <QListWidgetItem>
#include <QScroller>
Contacts::Contacts(QList<JidDb *> &databases, QWidget *const parent) :
QDialog(parent),
databases(databases)
{
ui.setupUi(this);
QScroller::grabGesture(ui.contacts_list, QScroller::TouchGesture);
for (const auto db : databases)
for (const auto &contact : db->roster())
add(db->jid, contact);
connect(ui.contacts_list, &QListWidget::itemActivated, this,
[this]
{
ui.chat->setEnabled(true);
});
connect(ui.chat, &QPushButton::released, this,
[this]
{
const auto items = ui.contacts_list->selectedItems();
if (items.isEmpty())
{
ui.chat->setEnabled(false);
return;
}
for (const auto it : items)
{
const auto c = static_cast<Contact *>(it);
Q_EMIT startChat(c->own, c->other);
}
close();
});
showMaximized();
}
void Contacts::add(const QString &own, const QString &other)
{
new Contact(own, other, ui.contacts_list);
}

28
contacts.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef CONTACTS_H
#define CONTACTS_H
#include "ui_contacts.h"
#include "jiddb.h"
#include <QDialog>
#include <QList>
#include <QString>
#include <QWidget>
class Contacts : public QDialog
{
Q_OBJECT
public:
Contacts(QList<JidDb *> &databases, QWidget *parent = nullptr);
void add(const QString &own, const QString &other);
private:
Ui_contacts ui;
QList<JidDb *> &databases;
enum Role {From = Qt::UserRole, To};
Q_SIGNALS:
void startChat(QString from, QString to);
};
#endif

114
contacts.ui Normal file
View File

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>contacts</class>
<widget class="QDialog" name="contacts">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>278</width>
<height>215</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>100</horstretch>
<verstretch>100</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>xxcc - Contacts</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="close">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>32</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>&lt;</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;Contacts&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="contacts_list">
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="rm">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="add">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="chat">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Chat</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>close</sender>
<signal>released()</signal>
<receiver>contacts</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>48</x>
<y>147</y>
</hint>
<hint type="destinationlabel">
<x>183</x>
<y>86</y>
</hint>
</hints>
</connection>
</connections>
</ui>

14
conversation.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "conversation.h"
Conversation::Conversation(const QString &from, const QString &to,
QListWidget *const list, QWidget *const parent) :
QWidget(parent),
it(list)
{
ui.setupUi(this);
ui.account->setText(from);
ui.contact->setText(to);
it.setSizeHint(sizeHint());
list->addItem(&it);
list->setItemWidget(&it, this);
}

21
conversation.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef CONVERSATION_H
#define CONVERSATION_H
#include "ui_conversation.h"
#include <QListWidget>
#include <QListWidgetItem>
#include <QWidget>
class Conversation : public QWidget
{
public:
Conversation(const QString &from, const QString &to,
QListWidget *list, QWidget *parent = nullptr);
void setText(const QString &msg);
private:
Ui_conversation ui;
QListWidgetItem it;
};
#endif

81
conversation.ui Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>conversation</class>
<widget class="QWidget" name="conversation">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>195</width>
<height>86</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="picture">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Picture</string>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="account">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="contact">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="message">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="time">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

103
credentials.cpp Normal file
View File

@ -0,0 +1,103 @@
#include "credentials.h"
#include <QEventLoop>
#include <qt5keychain/keychain.h>
#include <QXmppConfiguration.h>
#include <iostream>
static const QString service = "xxcc", sep = ";";
QList<Credentials::Pair> Credentials::load()
{
const QStringList users = storedUsers();
QList<Pair> ret;
for (const auto &user : users)
{
QKeychain::ReadPasswordJob job(service);
QEventLoop loop;
job.setKey(user);
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
job.start();
loop.exec();
const QString pwd = job.textData();
if (job.error())
std::cerr << "Failed to retrieve password for " << qPrintable(user)
<< ": " << qPrintable(job.errorString()) << std::endl;
else
ret.append(Pair(user, pwd));
}
return ret;
}
void Credentials::store(Client *c)
{
QKeychain::WritePasswordJob job(service);
const QXmppConfiguration &cfg = c->configuration();
const QString user = cfg.jidBare();
QEventLoop loop;
job.setKey(user);
job.setTextData(cfg.password());
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
job.start();
loop.exec();
if (job.error())
std::cerr << "Failed to store password: "
<< qPrintable(job.errorString()) << std::endl;
else
storeUser(user);
}
void Credentials::storeUser(const QString &user)
{
QString list = storedUsersList();
QKeychain::WritePasswordJob job(service);
QEventLoop loop;
if (!list.isEmpty())
list += sep;
list += user;
job.setKey("users");
job.setTextData(list);
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
job.start();
loop.exec();
if (job.error())
std::cerr << "Failed to store user: "
<< qPrintable(job.errorString()) << std::endl;
}
QString Credentials::storedUsersList()
{
QKeychain::ReadPasswordJob job(service);
QString ret;
QEventLoop loop;
job.setKey("users");
job.connect(&job, &QKeychain::Job::finished, &loop, &QEventLoop::quit);
job.start();
loop.exec();
const QString users = job.textData();
if (job.error())
std::cerr << "Failed to retrieve users: "
<< qPrintable(job.errorString()) << std::endl;
else
ret = users;
return ret;
}
QStringList Credentials::storedUsers()
{
return storedUsersList().split(sep);
}

26
credentials.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef CREDENTIALS_H
#define CREDENTIALS_H
#include "client.h"
#include <QObject>
#include <QList>
#include <QPair>
#include <QStringList>
class Credentials : public QObject
{
Q_OBJECT
public:
using Pair = QPair<QString, QString>;
QList<Pair> load();
public Q_SLOTS:
void store(Client *c);
private:
static void storeUser(const QString &user);
static QString storedUsersList();
static QStringList storedUsers();
};
#endif

125
jiddb.cpp Normal file
View File

@ -0,0 +1,125 @@
#include "jiddb.h"
#include <QDir>
#include <QSqlQuery>
#include <QStandardPaths>
#include <QTimeZone>
#include <QVariant>
#include <iostream>
#include <stdexcept>
JidDb::JidDb(const QString &jid) :
jid(jid),
db(QSqlDatabase::addDatabase("QSQLITE"))
{
const auto path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation);
const QDir dir;
if (!dir.exists(path) && !dir.mkdir(path))
throw std::runtime_error("Failed to create app dir");
const auto abspath = path + "/" + jid + ".db";
db.setDatabaseName(abspath);
if (!db.open())
throw std::runtime_error(qPrintable("database" + abspath
+ "could not be created"));
QSqlQuery q(db);
if (!q.exec("create table if not exists roster "
"(jid TEXT unique) strict;"))
std::cerr << "JidDb::JidDb: query exec failed" << std::endl;
}
void JidDb::addToRoster(const QString &jid)
{
QSqlQuery q(db);
if (!q.exec("insert or ignore into roster (jid) values ('" + jid + "');"))
throw std::runtime_error("JidDb::addToRoster: query exec failed");
Q_EMIT addedToRoster(jid);
}
void JidDb::addToRoster(const QStringList &roster)
{
for (const auto &r : roster)
addToRoster(r);
}
QStringList JidDb::roster() const
{
QStringList ret;
QSqlQuery q(db);
if (!q.exec("select jid from roster;"))
std::cerr << "query exec failed" << std::endl;
else
while (q.next())
ret.append(q.value("jid").toString());
return ret;
}
QList<JidDb::Message> JidDb::getMessages(const QString &jid,
const int tail) const
{
QList<Message> ret;
QSqlQuery q(db);
if (tail < 0)
{
if (!q.exec("select * from " + jid + " order by time;"))
throw std::runtime_error("JidDb::getMessages: query exec failed");
}
else if (!q.exec("select * from " + jid + " order by time desc limit "
+ QString::number(tail) + ";"))
throw std::runtime_error("JidDb::getMessages: query exec failed");
while (q.next())
{
Message m;
bool ok;
qlonglong t = q.value("time").toLongLong(&ok);
if (!ok)
throw std::runtime_error("JidDb::getMessages: invalid time");
m.body = q.value("body").toString();
m.dt = QDateTime::fromSecsSinceEpoch(t, QTimeZone::systemTimeZone());
ret << m;
}
return ret;
}
void JidDb::storeMessage(const JidDb::Message &msg)
const
{
QSqlQuery q(db);
QString dir;
switch (msg.direction)
{
case Message::In:
dir = "in";
break;
case Message::Out:
dir = "out";
break;
}
if (!q.exec("create table if not exists '" + msg.contact +
"' (direction TEXT, time INTEGER, body TEXT) strict;"))
throw std::runtime_error("JidDb::storeMessage: query exec failed");
else if (!q.exec("insert into '" + msg.contact
+ "' (direction, time, body) values "
"('" + dir + "', "
+ QString::number(msg.dt.toSecsSinceEpoch()) + ", "
"'" + msg.body + "')"))
throw std::runtime_error("JidDb::storeMessage: query exec 2 failed");
}

40
jiddb.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef JID_DB_H
#define JID_DB_H
#include <QDateTime>
#include <QList>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QSqlDatabase>
class JidDb : public QObject
{
Q_OBJECT
public:
struct Message
{
enum Direction {In, Out} direction;
QDateTime dt;
QString contact, body;
};
JidDb(const QString &jid);
QStringList roster() const;
const QString jid;
public Q_SLOTS:
QList<Message> getMessages(const QString &jid, int tail = -1) const;
void storeMessage(const Message &msg) const;
void addToRoster(const QString &jid);
void addToRoster(const QStringList &roster);
Q_SIGNALS:
void addedToRoster(QString jid);
private:
QSqlDatabase db;
};
#endif

137
login.cpp Normal file
View File

@ -0,0 +1,137 @@
#include "login.h"
#include "ui_login.h"
#include <QString>
#include "QXmppStanza.h"
#include <memory>
static QString error_to_str(const QXmppStanza::Error::Condition c)
{
switch (c)
{
case QXmppStanza::Error::BadRequest:
return "BadRequest";
case QXmppStanza::Error::Conflict:
return "Conflict";
case QXmppStanza::Error::FeatureNotImplemented:
return "FeatureNotImplemented";
case QXmppStanza::Error::Forbidden:
return "Forbidden";
case QXmppStanza::Error::Gone:
return "Gone";
case QXmppStanza::Error::InternalServerError:
return "internal server error";
case QXmppStanza::Error::ItemNotFound:
return "item not found";
case QXmppStanza::Error::JidMalformed:
return "JID Malformed";
case QXmppStanza::Error::NotAcceptable:
return "not acceptable";
case QXmppStanza::Error::NotAllowed:
return "not allowed";
case QXmppStanza::Error::NotAuthorized:
return "not authorized";
case QXmppStanza::Error::RecipientUnavailable:
return "recipient unavailable";
case QXmppStanza::Error::Redirect:
return "Redirect";
case QXmppStanza::Error::RegistrationRequired:
return "RegistrationRequired";
case QXmppStanza::Error::RemoteServerNotFound:
return "RemoteServerNotFound";
case QXmppStanza::Error::RemoteServerTimeout:
return "RemoteServerTimeout";
case QXmppStanza::Error::ResourceConstraint:
return "ResourceConstraint";
case QXmppStanza::Error::ServiceUnavailable:
return "ServiceUnavailable";
case QXmppStanza::Error::SubscriptionRequired:
return "SubscriptionRequired";
case QXmppStanza::Error::UndefinedCondition:
return "UndefinedCondition";
case QXmppStanza::Error::UnexpectedRequest:
return "UnexpectedRequest";
case QXmppStanza::Error::PolicyViolation:
return "PolicyViolation";
default:
break;
}
return "UnknownError";
}
Login::Login(QDialog *const parent) :
QDialog(parent)
{
ui.setupUi(this);
ui.error->setVisible(false);
connect(ui.login_b, &QPushButton::released,
[this]
{
const QString jid = ui.username->text(), pwd = ui.password->text();
QString domain, error;
if (!jid_is_valid(jid, domain))
error = tr("Invalid username");
else if (pwd.isEmpty())
error = tr("Invalid password");
else
setup(jid, pwd, domain);
ui.error->setText(error);
ui.error->setVisible(!error.isEmpty());
});
showMaximized();
}
bool Login::jid_is_valid(const QString &jid, QString &domain)
{
if (jid.isEmpty() || jid.count('@') != 1)
return false;
else if ((domain = jid.mid(jid.indexOf('@') + 1)).isEmpty())
return false;
return true;
}
void Login::setup(const QString &jid, const QString pwd, const QString &domain)
{
QXmppConfiguration cfg;
cfg.setStreamSecurityMode(QXmppConfiguration::TLSRequired);
cfg.setJid(jid);
cfg.setPassword(pwd);
cfg.setAutoReconnectionEnabled(false);
const auto client = new Client;
connect(client, &Client::stateChanged,
[d = domain, e = ui.error] (const Client::State state)
{
if (state == Client::ConnectingState)
{
e->setVisible(true);
e->setText(tr("Connecting to ") + d + "...");
}
});
connect(client, &Client::connected,
[this, client]
{
Q_EMIT auth_success(client);
close();
});
connect(client, &Client::disconnected,
[client, e = ui.error]
{
e->setText("Disconnected from server: "
+ error_to_str(client->xmppStreamError()));
e->setVisible(true);
delete client;
});
client->connectToServer(cfg);
}

26
login.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef LOGIN_H
#define LOGIN_H
#include "client.h"
#include "ui_login.h"
#include <QDialog>
#include <QString>
class Login : public QDialog
{
Q_OBJECT
public:
Login(QDialog *parent = nullptr);
Q_SIGNALS:
void auth_success(Client *c);
private:
void setup(const QString &jid, const QString pwd, const QString &domain);
static bool jid_is_valid(const QString &jid, QString &domain);
Ui_login ui;
};
#endif

140
login.ui Normal file
View File

@ -0,0 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>login</class>
<widget class="QDialog" name="login">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>221</width>
<height>202</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle">
<string>Log into a XMPP server</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="close">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>32</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>&lt;</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="username">
<property name="inputMethodHints">
<set>Qt::ImhEmailCharactersOnly|Qt::ImhNoAutoUppercase|Qt::ImhNoPredictiveText</set>
</property>
<property name="placeholderText">
<string>Enter JID</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLineEdit" name="password">
<property name="inputMethodHints">
<set>Qt::ImhHiddenText|Qt::ImhNoAutoUppercase|Qt::ImhNoPredictiveText|Qt::ImhSensitiveData</set>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
<property name="placeholderText">
<string>Enter password</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="error">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="create_account">
<property name="text">
<string>Create account</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="login_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>close</sender>
<signal>released()</signal>
<receiver>login</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>86</x>
<y>71</y>
</hint>
<hint type="destinationlabel">
<x>161</x>
<y>51</y>
</hint>
</hints>
</connection>
</connections>
</ui>

11
main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "xxcc.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
xxcc x;
x.show();
return a.exec();
}

46
message.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "message.h"
#include <Qt>
Message::Message(const QString &msg, const QDateTime &dt,
const Message::Dir dir, QListWidget *const list,
QWidget *const parent) :
QWidget(parent),
it(list)
{
ui.setupUi(this);
ui.edit->setVisible(false);
ui.quote->setVisible(false);
ui.retry->setVisible(false);
switch (dir)
{
case In:
ui.in_msg->setText(msg);
ui.out_msg->setVisible(false);
ui.time->setAlignment(Qt::AlignLeft);
break;
case Out:
ui.out_msg->setText(msg);
ui.in_msg->setVisible(false);
ui.time->setAlignment(Qt::AlignRight);
break;
}
ui.time->setText(dt.toString("ddd MMMM d yyyy hh:mm:ss"));
it.setSizeHint(sizeHint());
connect(list, &QListWidget::itemActivated,
[this] (QListWidgetItem *const item)
{
const bool visible = item == &it;
ui.edit->setVisible(visible);
ui.quote->setVisible(visible);
ui.retry->setVisible(visible);
it.setSizeHint(sizeHint());
});
list->addItem(&it);
list->setItemWidget(&it, this);
}

23
message.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef MESSAGE_H
#define MESSAGE_H
#include "ui_message.h"
#include <QDateTime>
#include <QListWidgetItem>
#include <QListWidget>
#include <QWidget>
class Message : public QWidget
{
public:
enum Dir {In, Out};
Message(const QString &msg, const QDateTime &dt,
Dir dir, QListWidget *list,
QWidget *parent = nullptr);
private:
Ui_message ui;
QListWidgetItem it;
};
#endif

220
message.ui Normal file
View File

@ -0,0 +1,220 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>message</class>
<widget class="QWidget" name="message">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>278</width>
<height>100</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="quote">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Quote</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="edit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="retry">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Retry</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="time">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="out_msg">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="palette">
<palette>
<active>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>40</red>
<green>66</green>
<blue>62</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>40</red>
<green>66</green>
<blue>62</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>26</red>
<green>29</green>
<blue>31</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="in_msg">
<property name="palette">
<palette>
<active>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>21</red>
<green>83</green>
<blue>74</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>21</red>
<green>83</green>
<blue>74</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>26</red>
<green>29</green>
<blue>31</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

15
qxmpp/CMakeLists.txt Normal file
View File

@ -0,0 +1,15 @@
set(BUILD_SHARED OFF)
set(BUILD_OMEMO ON)
set(BUILD_TESTS OFF)
add_subdirectory(qxmpp)
target_link_libraries(xxcc PRIVATE
QXmppQt${QT_VERSION_MAJOR}
QXmppOmemoQt${QT_VERSION_MAJOR})
target_include_directories(QXmppQt${QT_VERSION_MAJOR} PRIVATE
${CMAKE_BINARY_DIR}/qxmpp/qxmpp/src)
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_BINARY_DIR}/qxmpp/qxmpp/src)
target_include_directories(QXmppOmemoQt${QT_VERSION_MAJOR} PRIVATE
${CMAKE_BINARY_DIR}/qxmpp/qxmpp/src/omemo)
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_BINARY_DIR}/qxmpp/qxmpp/src/omemo)

219
xxcc.cpp Normal file
View File

@ -0,0 +1,219 @@
#include "xxcc.h"
#include "client.h"
#include "accounts.h"
#include "contacts.h"
#include "conversation.h"
#include "message.h"
#include <QXmppMessage.h>
#include <QXmppRosterManager.h>
#include <QXmppUtils.h>
#include <QKeyEvent>
#include <QPushButton>
#include <QScroller>
#include <stdexcept>
#include <utility>
xxcc::xxcc(QWidget *const parent) :
QWidget(parent),
selected(nullptr)
{
const auto pairs = creds.load();
ui.setupUi(this);
QScroller::grabGesture(ui.conversations_list, QScroller::TouchGesture);
QScroller::grabGesture(ui.messages, QScroller::TouchGesture);
setupDatabases(pairs);
connectAccounts(pairs);
connect(ui.accounts, &QPushButton::released, this,
[this]
{
Accounts a(clients, this);
a.connect(&a, &Accounts::new_account, this, &xxcc::addAccount);
a.connect(&a, &Accounts::new_account, &creds, &Credentials::store);
a.exec();
});
connect(ui.contacts, &QPushButton::released, this,
[this]
{
Contacts c(databases, this);
connect(&c, &Contacts::startChat, this, &xxcc::startChat);
for (const auto db : databases)
c.connect(db, &JidDb::addedToRoster, &c,
[&c, db] (const QString jid)
{
c.add(db->jid, jid);
});
c.exec();
});
connect(ui.send, &QPushButton::released, this, &xxcc::send);
connect(ui.back, &QPushButton::released, this,
[this]
{
ui.sw->setCurrentIndex(static_cast<int>(Tab::Conversations));
ui.messages->clear();
selected = nullptr;
});
connect(ui.conversations_list, &QListWidget::itemActivated, this,
[this]
{
ui.sw->setCurrentIndex(static_cast<int>(Tab::Chat));
});
connect(ui.messages->model(), &QAbstractItemModel::rowsInserted,
ui.messages, &QListWidget::scrollToBottom);
}
xxcc::~xxcc()
{
for (const auto c : clients)
delete c;
for (const auto db : databases)
delete db;
}
void xxcc::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;
addAccount(client);
client->connectToServer(cfg);
}
}
void xxcc::setupDatabases(const QList<Credentials::Pair> &pairs)
{
for (const auto &p : pairs)
databases.append(new JidDb(p.first));
}
void xxcc::startChat(const QString from, const QString to)
{
new Conversation(from, to, ui.conversations_list);
for (const auto c : clients)
if (from == c->configuration().jidBare())
{
selected = c;
break;
}
ui.sw->setCurrentIndex(static_cast<int>(Tab::Chat));
ui.jid->setText(to);
ui.messages->scrollToBottom();
}
void xxcc::addInMessage(const QXmppMessage &msg)
{
new Message(msg.body(), msg.stamp().toLocalTime(), Message::In,
ui.messages);
}
void xxcc::addOutMessage(const QXmppMessage &msg)
{
new Message(msg.body(), msg.stamp().toLocalTime(), Message::Out,
ui.messages);
}
void xxcc::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, JidDb::Message::Direction::In);
if (selected)
addInMessage(msg);
});
const auto roster = c->findExtension<QXmppRosterManager>();
if (roster)
roster->connect(roster, &QXmppRosterManager::rosterReceived, c,
[this, c, roster]
{
for (const auto db : databases)
if (db->jid == c->configuration().jidBare())
{
db->addToRoster(roster->getRosterBareJids());
break;
}
});
else
throw std::runtime_error("Expected non-null QXmppRosterManager");
}
void xxcc::send(void)
{
if (!selected)
throw std::runtime_error("Expected non-null selected client");
const auto from = selected->configuration().jidBare(),
to = ui.jid->text(), msg = ui.chatinput->toPlainText();
QXmppMessage out(from, to, msg);
out.setStamp(QDateTime::currentDateTimeUtc());
selected->sendPacket(out);
addOutMessage(out);
storeMessage(out, JidDb::Message::Direction::Out);
ui.chatinput->clear();
}
void xxcc::storeMessage(const QXmppMessage &msg,
const enum JidDb::Message::Direction dir) const
{
QString jid, contact;
switch (dir)
{
case JidDb::Message::Direction::In:
jid = QXmppUtils::jidToBareJid(msg.to());
contact = QXmppUtils::jidToBareJid(msg.from());
break;
case JidDb::Message::Direction::Out:
jid = msg.from();
contact = msg.to();
break;
}
for (const auto db : databases)
{
if (db->jid == jid)
{
JidDb::Message m;
m.body = msg.body();
m.dt = msg.stamp();
m.direction = dir;
m.contact = contact;
db->storeMessage(m);
break;
}
}
}

43
xxcc.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef XXCC_H
#define XXCC_H
#include "ui_xxcc.h"
#include "client.h"
#include "credentials.h"
#include "jiddb.h"
#include <QDateTime>
#include <QList>
#include <QWidget>
#include <QSqlDatabase>
#include <QString>
#include <QXmppMessage.h>
class xxcc : public QWidget
{
Q_OBJECT
public:
xxcc(QWidget *parent = nullptr);
~xxcc();
private:
enum Tab {Conversations, Chat};
Ui_main ui;
QList<Client *> clients;
QList<JidDb *> databases;
Credentials creds;
Client *selected;
void setupDatabases(const QList<Credentials::Pair> &pairs);
void connectAccounts(const QList<Credentials::Pair> &pairs);
void storeMessage(const QXmppMessage &msg,
enum JidDb::Message::Direction dir) const;
private Q_SLOTS:
void startChat(QString from, QString to);
void addInMessage(const QXmppMessage &msg);
void addOutMessage(const QXmppMessage &msg);
void addAccount(Client *c);
void send(void);
};
#endif

247
xxcc.ui Normal file
View File

@ -0,0 +1,247 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>main</class>
<widget class="QWidget" name="main">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>290</width>
<height>278</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>100</horstretch>
<verstretch>100</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>xxcc</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QStackedWidget" name="sw">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeIncrement">
<size>
<width>13767</width>
<height>13767</height>
</size>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="conversations">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:12pt; font-weight:600;&quot;&gt;xxcc&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="conversations_list">
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="accounts">
<property name="text">
<string>Accounts</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="contacts">
<property name="text">
<string>Contacts</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="rooms">
<property name="text">
<string>Rooms</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="chat_tab">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QPushButton" name="back">
<property name="maximumSize">
<size>
<width>32</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>&lt;</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="picture">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Picture</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="jid">
<property name="text">
<string notr="true">Contact name</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="messages">
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="chatinput_hbox">
<property name="spacing">
<number>8</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QPlainTextEdit" name="chatinput">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="midLineWidth">
<number>0</number>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="tabChangesFocus">
<bool>true</bool>
</property>
<property name="backgroundVisible">
<bool>false</bool>
</property>
<property name="placeholderText">
<string>Write a message...</string>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QPushButton" name="send">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Send</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>OMEMO</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>