Shared namespace refactoring, enum class refactoring, going offline related crash fix

This commit is contained in:
Blue 2020-04-04 01:28:15 +03:00
parent b309100f99
commit ddfb3419cc
59 changed files with 1948 additions and 1695 deletions

114
shared/enums.h Normal file
View file

@ -0,0 +1,114 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHARED_ENUMS_H
#define SHARED_ENUMS_H
#include <deque>
#include <QString>
#include <QObject>
namespace Shared {
Q_NAMESPACE
enum class ConnectionState {
disconnected,
connecting,
connected,
error
};
Q_ENUM_NS(ConnectionState)
static const std::deque<QString> connectionStateThemeIcons = {"state-offline", "state-sync", "state-ok", "state-error"};
static const ConnectionState connectionStateHighest = ConnectionState::error;
static const ConnectionState connectionStateLowest = ConnectionState::disconnected;
enum class Availability {
online,
away,
extendedAway,
busy,
chatty,
invisible,
offline
};
Q_ENUM_NS(Availability)
static const Availability availabilityHighest = Availability::offline;
static const Availability availabilityLowest = Availability::online;
static const std::deque<QString> availabilityThemeIcons = {
"user-online",
"user-away",
"user-away-extended",
"user-busy",
"chatty",
"user-invisible",
"user-offline"
};
static const std::deque<QString> availabilityNames = {"Online", "Away", "Absent", "Busy", "Chatty", "Invisible", "Offline"};
enum class SubscriptionState {
none,
from,
to,
both,
unknown
};
Q_ENUM_NS(SubscriptionState)
static const SubscriptionState subscriptionStateHighest = SubscriptionState::unknown;
static const SubscriptionState subscriptionStateLowest = SubscriptionState::none;
static const std::deque<QString> subscriptionStateThemeIcons = {"edit-none", "arrow-down-double", "arrow-up-double", "dialog-ok", "question"};
static const std::deque<QString> subscriptionStateNames = {"None", "From", "To", "Both", "Unknown"};
enum class Affiliation {
unspecified,
outcast,
nobody,
member,
admin,
owner
};
Q_ENUM_NS(Affiliation)
static const Affiliation affiliationHighest = Affiliation::owner;
static const Affiliation affiliationLowest = Affiliation::unspecified;
static const std::deque<QString> affiliationNames = {"Unspecified", "Outcast", "Nobody", "Member", "Admin", "Owner"};
enum class Role {
unspecified,
nobody,
visitor,
participant,
moderator
};
Q_ENUM_NS(Role)
static const Role roleHighest = Role::moderator;
static const Role roleLowest = Role::unspecified;
static const std::deque<QString> roleNames = {"Unspecified", "Nobody", "Visitor", "Participant", "Moderator"};
enum class Avatar {
empty,
autocreated,
valid
};
Q_ENUM_NS(Avatar)
static const std::deque<QString> messageStateNames = {"Pending", "Sent", "Delivered", "Error"};
static const std::deque<QString> messageStateThemeIcons = {"state-offline", "state-sync", "state-ok", "state-error"};
}
#endif // SHARED_ENUMS_H

170
shared/global.cpp Normal file
View file

@ -0,0 +1,170 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "global.h"
#include "enums.h"
Shared::Global* Shared::Global::instance = 0;
Shared::Global::Global():
availability({
tr("Online"),
tr("Away"),
tr("Absent"),
tr("Busy"),
tr("Chatty"),
tr("Invisible"),
tr("Offline")
}),
connectionState({
tr("Disconnected"),
tr("Connecting"),
tr("Connected"),
tr("Error")
}),
subscriptionState({
tr("None"),
tr("From"),
tr("To"),
tr("Both"),
tr("Unknown")
}),
affiliation({
tr("Unspecified"),
tr("Outcast"),
tr("Nobody"),
tr("Member"),
tr("Admin"),
tr("Owner")
}),
role({
tr("Unspecified"),
tr("Nobody"),
tr("Visitor"),
tr("Participant"),
tr("Moderator")
}),
messageState({
tr("Pending"),
tr("Sent"),
tr("Delivered"),
tr("Error")
})
{
if (instance != 0) {
throw 551;
}
instance = this;
}
Shared::Global * Shared::Global::getInstance()
{
return instance;
}
QString Shared::Global::getName(Message::State rl)
{
return instance->messageState[int(rl)];
}
QString Shared::Global::getName(Shared::Affiliation af)
{
return instance->affiliation[int(af)];
}
QString Shared::Global::getName(Shared::Availability av)
{
return instance->availability[int(av)];
}
QString Shared::Global::getName(Shared::ConnectionState cs)
{
return instance->connectionState[int(cs)];
}
QString Shared::Global::getName(Shared::Role rl)
{
return instance->role[int(rl)];
}
QString Shared::Global::getName(Shared::SubscriptionState ss)
{
return instance->subscriptionState[int(ss)];
}
template<>
Shared::Availability Shared::Global::fromInt(int src)
{
if (src < static_cast<int>(Shared::availabilityLowest) && src > static_cast<int>(Shared::availabilityHighest)) {
qDebug("An attempt to set invalid availability to Squawk core, skipping");
}
return static_cast<Shared::Availability>(src);
}
template<>
Shared::Availability Shared::Global::fromInt(unsigned int src)
{
if (src < static_cast<int>(Shared::availabilityLowest) && src > static_cast<int>(Shared::availabilityHighest)) {
qDebug("An attempt to set invalid availability to Squawk core, skipping");
}
return static_cast<Shared::Availability>(src);
}
template<>
Shared::ConnectionState Shared::Global::fromInt(int src)
{
if (src < static_cast<int>(Shared::connectionStateLowest) && src > static_cast<int>(Shared::connectionStateHighest)) {
qDebug("An attempt to set invalid availability to Squawk core, skipping");
}
return static_cast<Shared::ConnectionState>(src);
}
template<>
Shared::ConnectionState Shared::Global::fromInt(unsigned int src)
{
if (src < static_cast<int>(Shared::connectionStateLowest) && src > static_cast<int>(Shared::connectionStateHighest)) {
qDebug("An attempt to set invalid availability to Squawk core, skipping");
}
return static_cast<Shared::ConnectionState>(src);
}
template<>
Shared::SubscriptionState Shared::Global::fromInt(int src)
{
if (src < static_cast<int>(Shared::subscriptionStateLowest) && src > static_cast<int>(Shared::subscriptionStateHighest)) {
qDebug("An attempt to set invalid availability to Squawk core, skipping");
}
return static_cast<Shared::SubscriptionState>(src);
}
template<>
Shared::SubscriptionState Shared::Global::fromInt(unsigned int src)
{
if (src < static_cast<int>(Shared::subscriptionStateLowest) && src > static_cast<int>(Shared::subscriptionStateHighest)) {
qDebug("An attempt to set invalid availability to Squawk core, skipping");
}
return static_cast<Shared::SubscriptionState>(src);
}

64
shared/global.h Normal file
View file

@ -0,0 +1,64 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHARED_GLOBAL_H
#define SHARED_GLOBAL_H
#include "enums.h"
#include "message.h"
#include <map>
#include <QCoreApplication>
#include <QDebug>
namespace Shared {
class Global {
Q_DECLARE_TR_FUNCTIONS(Global)
public:
Global();
static Global* getInstance();
static QString getName(Availability av);
static QString getName(ConnectionState cs);
static QString getName(SubscriptionState ss);
static QString getName(Affiliation af);
static QString getName(Role rl);
static QString getName(Message::State rl);
const std::deque<QString> availability;
const std::deque<QString> connectionState;
const std::deque<QString> subscriptionState;
const std::deque<QString> affiliation;
const std::deque<QString> role;
const std::deque<QString> messageState;
template<typename T>
static T fromInt(int src);
template<typename T>
static T fromInt(unsigned int src);
private:
static Global* instance;
};
}
#endif // SHARED_GLOBAL_H

96
shared/icons.cpp Normal file
View file

@ -0,0 +1,96 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "icons.h"
#include <QApplication>
#include <QPalette>
#include <QDebug>
QIcon Shared::availabilityIcon(Shared::Availability av, bool big)
{
const std::deque<QString>& fallback = QApplication::palette().window().color().lightnessF() > 0.5 ?
big ?
Shared::fallbackAvailabilityThemeIconsDarkBig:
Shared::fallbackAvailabilityThemeIconsDarkSmall:
big ?
Shared::fallbackAvailabilityThemeIconsLightBig:
Shared::fallbackAvailabilityThemeIconsLightSmall;
return QIcon::fromTheme(availabilityThemeIcons[static_cast<int>(av)], QIcon(fallback[static_cast<int>(av)]));
}
QIcon Shared::subscriptionStateIcon(Shared::SubscriptionState ss, bool big)
{
const std::deque<QString>& fallback = QApplication::palette().window().color().lightnessF() > 0.5 ?
big ?
Shared::fallbackSubscriptionStateThemeIconsDarkBig:
Shared::fallbackSubscriptionStateThemeIconsDarkSmall:
big ?
Shared::fallbackSubscriptionStateThemeIconsLightBig:
Shared::fallbackSubscriptionStateThemeIconsLightSmall;
return QIcon::fromTheme(subscriptionStateThemeIcons[static_cast<int>(ss)], QIcon(fallback[static_cast<int>(ss)]));
}
QIcon Shared::connectionStateIcon(Shared::ConnectionState cs, bool big)
{
const std::deque<QString>& fallback = QApplication::palette().window().color().lightnessF() > 0.5 ?
big ?
Shared::fallbackConnectionStateThemeIconsDarkBig:
Shared::fallbackConnectionStateThemeIconsDarkSmall:
big ?
Shared::fallbackConnectionStateThemeIconsLightBig:
Shared::fallbackConnectionStateThemeIconsLightSmall;
return QIcon::fromTheme(connectionStateThemeIcons[static_cast<int>(cs)], QIcon(fallback[static_cast<int>(cs)]));
}
static const QString ds = ":images/fallback/dark/small/";
static const QString db = ":images/fallback/dark/big/";
static const QString ls = ":images/fallback/light/small/";
static const QString lb = ":images/fallback/light/big/";
QIcon Shared::icon(const QString& name, bool big)
{
std::map<QString, std::pair<QString, QString>>::const_iterator itr = icons.find(name);
if (itr != icons.end()) {
const QString& prefix = QApplication::palette().window().color().lightnessF() > 0.5 ?
big ? db : ds:
big ? lb : ls;
return QIcon::fromTheme(itr->second.first, QIcon(prefix + itr->second.second + ".svg"));
} else {
qDebug() << "Icon" << name << "not found";
return QIcon::fromTheme(name);
}
}
QString Shared::iconPath(const QString& name, bool big)
{
QString result = "";
std::map<QString, std::pair<QString, QString>>::const_iterator itr = icons.find(name);
if (itr != icons.end()) {
const QString& prefix = QApplication::palette().window().color().lightnessF() > 0.5 ?
big ? db : ds:
big ? lb : ls;
result = prefix + itr->second.second + ".svg";
}
return result;
}

176
shared/icons.h Normal file
View file

@ -0,0 +1,176 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHARED_ICONS_H
#define SHARED_ICONS_H
#include <QIcon>
#include <map>
#include "enums.h"
namespace Shared {
static const std::deque<QString> fallbackAvailabilityThemeIconsLightBig = {
":images/fallback/light/big/online.svg",
":images/fallback/light/big/away.svg",
":images/fallback/light/big/absent.svg",
":images/fallback/light/big/busy.svg",
":images/fallback/light/big/chatty.svg",
":images/fallback/light/big/invisible.svg",
":images/fallback/light/big/offline.svg"
};
static const std::deque<QString> fallbackSubscriptionStateThemeIconsLightBig = {
":images/fallback/light/big/edit-none.svg",
":images/fallback/light/big/arrow-down-double.svg",
":images/fallback/light/big/arrow-up-double.svg",
":images/fallback/light/big/dialog-ok.svg",
":images/fallback/light/big/question.svg"
};
static const std::deque<QString> fallbackConnectionStateThemeIconsLightBig = {
":images/fallback/light/big/state-offline.svg",
":images/fallback/light/big/state-sync.svg",
":images/fallback/light/big/state-ok.svg",
":images/fallback/light/big/state-error.svg"
};
static const std::deque<QString> fallbackAvailabilityThemeIconsLightSmall = {
":images/fallback/light/small/online.svg",
":images/fallback/light/small/away.svg",
":images/fallback/light/small/absent.svg",
":images/fallback/light/small/busy.svg",
":images/fallback/light/small/chatty.svg",
":images/fallback/light/small/invisible.svg",
":images/fallback/light/small/offline.svg"
};
static const std::deque<QString> fallbackSubscriptionStateThemeIconsLightSmall = {
":images/fallback/light/small/edit-none.svg",
":images/fallback/light/small/arrow-down-double.svg",
":images/fallback/light/small/arrow-up-double.svg",
":images/fallback/light/small/dialog-ok.svg",
":images/fallback/light/small/question.svg"
};
static const std::deque<QString> fallbackConnectionStateThemeIconsLightSmall = {
":images/fallback/light/small/state-offline.svg",
":images/fallback/light/small/state-sync.svg",
":images/fallback/light/small/state-ok.svg",
":images/fallback/light/small/state-error.svg"
};
static const std::deque<QString> fallbackAvailabilityThemeIconsDarkBig = {
":images/fallback/dark/big/online.svg",
":images/fallback/dark/big/away.svg",
":images/fallback/dark/big/absent.svg",
":images/fallback/dark/big/busy.svg",
":images/fallback/dark/big/chatty.svg",
":images/fallback/dark/big/invisible.svg",
":images/fallback/dark/big/offline.svg"
};
static const std::deque<QString> fallbackSubscriptionStateThemeIconsDarkBig = {
":images/fallback/dark/big/edit-none.svg",
":images/fallback/dark/big/arrow-down-double.svg",
":images/fallback/dark/big/arrow-up-double.svg",
":images/fallback/dark/big/dialog-ok.svg",
":images/fallback/dark/big/question.svg"
};
static const std::deque<QString> fallbackConnectionStateThemeIconsDarkBig = {
":images/fallback/dark/big/state-offline.svg",
":images/fallback/dark/big/state-sync.svg",
":images/fallback/dark/big/state-ok.svg",
":images/fallback/dark/big/state-error.svg"
};
static const std::deque<QString> fallbackAvailabilityThemeIconsDarkSmall = {
":images/fallback/dark/small/online.svg",
":images/fallback/dark/small/away.svg",
":images/fallback/dark/small/absent.svg",
":images/fallback/dark/small/busy.svg",
":images/fallback/dark/small/chatty.svg",
":images/fallback/dark/small/invisible.svg",
":images/fallback/dark/small/offline.svg"
};
static const std::deque<QString> fallbackSubscriptionStateThemeIconsDarkSmall = {
":images/fallback/dark/small/edit-none.svg",
":images/fallback/dark/small/arrow-down-double.svg",
":images/fallback/dark/small/arrow-up-double.svg",
":images/fallback/dark/small/dialog-ok.svg",
":images/fallback/dark/small/question.svg"
};
static const std::deque<QString> fallbackConnectionStateThemeIconsDarkSmall = {
":images/fallback/dark/small/state-offline.svg",
":images/fallback/dark/small/state-sync.svg",
":images/fallback/dark/small/state-ok.svg",
":images/fallback/dark/small/state-error.svg"
};
QIcon availabilityIcon(Availability av, bool big = false);
QIcon subscriptionStateIcon(SubscriptionState ss, bool big = false);
QIcon connectionStateIcon(ConnectionState cs, bool big = false);
QIcon icon(const QString& name, bool big = false);
QString iconPath(const QString& name, bool big = false);
static const std::map<QString, std::pair<QString, QString>> icons = {
{"user-online", {"user-online", "online"}},
{"user-away", {"user-away", "away"}},
{"user-away-extended", {"user-away-extended", "absent"}},
{"user-busy", {"user-busy", "busy"}},
{"user-chatty", {"chatty", "chatty"}},
{"user-invisible", {"user-invisible", "invisible"}},
{"user-offline", {"offline", "offline"}},
{"edit-none", {"edit-none", "edit-none"}},
{"arrow-down-double", {"arrow-down-double", "arrow-down-double"}},
{"arrow-up-double", {"arrow-up-double", "arrow-up-double"}},
{"dialog-ok", {"dialog-ok", "dialog-ok"}},
{"question", {"question", "question"}},
{"state-offline", {"state-offline", "state-offline"}},
{"state-sync", {"state-sync", "state-sync"}},
{"state-ok", {"state-ok", "state-ok"}},
{"state-error", {"state-error", "state-error"}},
{"edit-copy", {"edit-copy", "copy"}},
{"edit-delete", {"edit-delete", "edit-delete"}},
{"edit-rename", {"edit-rename", "edit-rename"}},
{"mail-message", {"mail-message", "mail-message"}},
{"mail-attachment", {"mail-attachment", "mail-attachment"}},
{"network-connect", {"network-connect", "network-connect"}},
{"network-disconnect", {"network-disconnect", "network-disconnect"}},
{"news-subscribe", {"news-subscribe", "news-subscribe"}},
{"news-unsubscribe", {"news-unsubscribe", "news-unsubscribe"}},
{"view-refresh", {"view-refresh", "view-refresh"}},
{"send", {"document-send", "send"}},
{"clean", {"edit-clear-all", "clean"}},
{"user", {"user", "user"}},
{"user-properties", {"user-properties", "user-properties"}},
{"group", {"group", "group"}},
{"group-new", {"resurce-group-new", "group-new"}},
{"favorite", {"favorite", "favorite"}},
{"unfavorite", {"draw-star", "unfavorite"}},
{"list-add", {"list-add", "add"}},
};
}
#endif // SHARED_ICONS_H

399
shared/message.cpp Normal file
View file

@ -0,0 +1,399 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "message.h"
#include "utils.h"
Shared::Message::Message(Shared::Message::Type p_type):
jFrom(),
rFrom(),
jTo(),
rTo(),
id(),
body(),
time(),
thread(),
type(p_type),
outgoing(false),
forwarded(false),
state(State::delivered),
edited(false) {}
Shared::Message::Message():
jFrom(),
rFrom(),
jTo(),
rTo(),
id(),
body(),
time(),
thread(),
type(Message::normal),
outgoing(false),
forwarded(false),
state(State::delivered),
edited(false),
errorText(),
originalMessage(),
lastModified() {}
QString Shared::Message::getBody() const
{
return body;
}
QString Shared::Message::getFrom() const
{
QString from = jFrom;
if (rFrom.size() > 0) {
from += "/" + rFrom;
}
return from;
}
QString Shared::Message::getTo() const
{
QString to = jTo;
if (rTo.size() > 0) {
to += "/" + rTo;
}
return to;
}
QString Shared::Message::getId() const
{
return id;
}
QDateTime Shared::Message::getTime() const
{
return time;
}
void Shared::Message::setBody(const QString& p_body)
{
body = p_body;
}
void Shared::Message::setFrom(const QString& from)
{
QStringList list = from.split("/");
if (list.size() == 1) {
jFrom = from;
} else {
jFrom = list.front();
rFrom = list.back();
}
}
void Shared::Message::setTo(const QString& to)
{
QStringList list = to.split("/");
if (list.size() == 1) {
jTo = to;
} else {
jTo = list.front();
rTo = list.back();
}
}
void Shared::Message::setId(const QString& p_id)
{
id = p_id;
}
void Shared::Message::setTime(const QDateTime& p_time)
{
time = p_time;
}
QString Shared::Message::getFromJid() const
{
return jFrom;
}
QString Shared::Message::getFromResource() const
{
return rFrom;
}
QString Shared::Message::getToJid() const
{
return jTo;
}
QString Shared::Message::getToResource() const
{
return rTo;
}
QString Shared::Message::getErrorText() const
{
return errorText;
}
QString Shared::Message::getPenPalJid() const
{
if (outgoing) {
return jTo;
} else {
return jFrom;
}
}
QString Shared::Message::getPenPalResource() const
{
if (outgoing) {
return rTo;
} else {
return rFrom;
}
}
Shared::Message::State Shared::Message::getState() const
{
return state;
}
bool Shared::Message::getEdited() const
{
return edited;
}
void Shared::Message::setFromJid(const QString& from)
{
jFrom = from;
}
void Shared::Message::setFromResource(const QString& from)
{
rFrom = from;
}
void Shared::Message::setToJid(const QString& to)
{
jTo = to;
}
void Shared::Message::setToResource(const QString& to)
{
rTo = to;
}
void Shared::Message::setErrorText(const QString& err)
{
if (state == State::error) {
errorText = err;
}
}
bool Shared::Message::getOutgoing() const
{
return outgoing;
}
void Shared::Message::setOutgoing(bool og)
{
outgoing = og;
}
bool Shared::Message::getForwarded() const
{
return forwarded;
}
void Shared::Message::generateRandomId()
{
id = generateUUID();
}
QString Shared::Message::getThread() const
{
return thread;
}
void Shared::Message::setForwarded(bool fwd)
{
forwarded = fwd;
}
void Shared::Message::setThread(const QString& p_body)
{
thread = p_body;
}
QDateTime Shared::Message::getLastModified() const
{
return lastModified;
}
QString Shared::Message::getOriginalBody() const
{
return originalMessage;
}
Shared::Message::Type Shared::Message::getType() const
{
return type;
}
void Shared::Message::setType(Shared::Message::Type t)
{
type = t;
}
void Shared::Message::setState(Shared::Message::State p_state)
{
state = p_state;
if (state != State::error) {
errorText = "";
}
}
bool Shared::Message::serverStored() const
{
return state == State::delivered || state == State::sent;
}
void Shared::Message::setEdited(bool p_edited)
{
edited = p_edited;
}
void Shared::Message::serialize(QDataStream& data) const
{
data << jFrom;
data << rFrom;
data << jTo;
data << rTo;
data << id;
data << body;
data << time;
data << thread;
data << (quint8)type;
data << outgoing;
data << forwarded;
data << oob;
data << (quint8)state;
data << edited;
if (state == State::error) {
data << errorText;
}
if (edited) {
data << originalMessage;
data << lastModified;
}
}
void Shared::Message::deserialize(QDataStream& data)
{
data >> jFrom;
data >> rFrom;
data >> jTo;
data >> rTo;
data >> id;
data >> body;
data >> time;
data >> thread;
quint8 t;
data >> t;
type = static_cast<Type>(t);
data >> outgoing;
data >> forwarded;
data >> oob;
quint8 s;
data >> s;
state = static_cast<State>(s);
data >> edited;
if (state == State::error) {
data >> errorText;
}
if (edited) {
data >> originalMessage;
data >> lastModified;
}
}
bool Shared::Message::change(const QMap<QString, QVariant>& data)
{
QMap<QString, QVariant>::const_iterator itr = data.find("state");
if (itr != data.end()) {
setState(static_cast<State>(itr.value().toUInt()));
}
if (state == State::error) {
itr = data.find("errorText");
if (itr != data.end()) {
setErrorText(itr.value().toString());
}
}
bool idChanged = false;
itr = data.find("id");
if (itr != data.end()) {
QString newId = itr.value().toString();
if (id != newId) {
setId(newId);
idChanged = true;
}
}
itr = data.find("body");
if (itr != data.end()) {
QMap<QString, QVariant>::const_iterator dItr = data.find("stamp");
QDateTime correctionDate;
if (dItr != data.end()) {
correctionDate = dItr.value().toDateTime();
} else {
correctionDate = QDateTime::currentDateTimeUtc(); //in case there is no information about time of this correction it's applied
}
if (!edited || lastModified < correctionDate) {
originalMessage = body;
lastModified = correctionDate;
setBody(itr.value().toString());
setEdited(true);
}
}
return idChanged;
}
void Shared::Message::setCurrentTime()
{
time = QDateTime::currentDateTimeUtc();
}
QString Shared::Message::getOutOfBandUrl() const
{
return oob;
}
bool Shared::Message::hasOutOfBandUrl() const
{
return oob.size() > 0;
}
void Shared::Message::setOutOfBandUrl(const QString& url)
{
oob = url;
}
bool Shared::Message::storable() const
{
return id.size() > 0 && (body.size() > 0 || oob.size()) > 0;
}

125
shared/message.h Normal file
View file

@ -0,0 +1,125 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QString>
#include <QDateTime>
#include <QVariant>
#include <QMap>
#include <QDataStream>
#ifndef SHAPER_MESSAGE_H
#define SHAPER_MESSAGE_H
namespace Shared {
/**
* @todo write docs
*/
class Message {
public:
enum Type {
error,
normal,
chat,
groupChat,
headline
};
enum class State {
pending,
sent,
delivered,
error
};
Message(Type p_type);
Message();
void setFrom(const QString& from);
void setFromResource(const QString& from);
void setFromJid(const QString& from);
void setTo(const QString& to);
void setToResource(const QString& to);
void setToJid(const QString& to);
void setTime(const QDateTime& p_time);
void setId(const QString& p_id);
void setBody(const QString& p_body);
void setThread(const QString& p_body);
void setOutgoing(bool og);
void setForwarded(bool fwd);
void setType(Type t);
void setCurrentTime();
void setOutOfBandUrl(const QString& url);
void setState(State p_state);
void setEdited(bool p_edited);
void setErrorText(const QString& err);
bool change(const QMap<QString, QVariant>& data);
QString getFrom() const;
QString getFromJid() const;
QString getFromResource() const;
QString getTo() const;
QString getToJid() const;
QString getToResource() const;
QDateTime getTime() const;
QString getId() const;
QString getBody() const;
QString getThread() const;
bool getOutgoing() const;
bool getForwarded() const;
Type getType() const;
bool hasOutOfBandUrl() const;
bool storable() const;
QString getOutOfBandUrl() const;
State getState() const;
bool getEdited() const;
QString getErrorText() const;
QString getPenPalJid() const;
QString getPenPalResource() const;
void generateRandomId();
bool serverStored() const;
QDateTime getLastModified() const;
QString getOriginalBody() const;
void serialize(QDataStream& data) const;
void deserialize(QDataStream& data);
private:
QString jFrom;
QString rFrom;
QString jTo;
QString rTo;
QString id;
QString body;
QDateTime time;
QString thread;
Type type;
bool outgoing;
bool forwarded;
QString oob;
State state;
bool edited;
QString errorText;
QString originalMessage;
QDateTime lastModified;
};
}
#endif // SHAPER_MESSAGE_H

29
shared/utils.cpp Normal file
View file

@ -0,0 +1,29 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
QString Shared::generateUUID()
{
uuid_t uuid;
uuid_generate(uuid);
char uuid_str[36];
uuid_unparse_lower(uuid, uuid_str);
return uuid_str;
}

72
shared/utils.h Normal file
View file

@ -0,0 +1,72 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHARED_UTILS_H
#define SHARED_UTILS_H
#include <QString>
#include <QColor>
#include <uuid/uuid.h>
#include <vector>
namespace Shared {
QString generateUUID();
static const std::vector<QColor> colorPalette = {
QColor(244, 27, 63),
QColor(21, 104, 156),
QColor(38, 156, 98),
QColor(247, 103, 101),
QColor(121, 37, 117),
QColor(242, 202, 33),
QColor(168, 22, 63),
QColor(35, 100, 52),
QColor(52, 161, 152),
QColor(239, 53, 111),
QColor(237, 234, 36),
QColor(153, 148, 194),
QColor(211, 102, 151),
QColor(194, 63, 118),
QColor(249, 149, 51),
QColor(244, 206, 109),
QColor(121, 105, 153),
QColor(244, 199, 30),
QColor(28, 112, 28),
QColor(172, 18, 20),
QColor(25, 66, 110),
QColor(25, 149, 104),
QColor(214, 148, 0),
QColor(203, 47, 57),
QColor(4, 54, 84),
QColor(116, 161, 97),
QColor(50, 68, 52),
QColor(237, 179, 20),
QColor(69, 114, 147),
QColor(242, 212, 31),
QColor(248, 19, 20),
QColor(84, 102, 84),
QColor(25, 53, 122),
QColor(91, 91, 109),
QColor(17, 17, 80),
QColor(54, 54, 94)
};
}
#endif // SHARED_UTILS_H

288
shared/vcard.cpp Normal file
View file

@ -0,0 +1,288 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vcard.h"
Shared::VCard::Contact::Contact(Shared::VCard::Contact::Role p_role, bool p_prefered):
role(p_role),
prefered(p_prefered) {}
Shared::VCard::Email::Email(const QString& addr, Shared::VCard::Contact::Role p_role, bool p_prefered):
Contact(p_role, p_prefered),
address(addr) {}
Shared::VCard::Phone::Phone(const QString& nmbr, Shared::VCard::Phone::Type p_type, Shared::VCard::Contact::Role p_role, bool p_prefered):
Contact(p_role, p_prefered),
number(nmbr),
type(p_type) {}
Shared::VCard::Address::Address(const QString& zCode, const QString& cntry, const QString& rgn, const QString& lclty, const QString& strt, const QString& ext, Shared::VCard::Contact::Role p_role, bool p_prefered):
Contact(p_role, p_prefered),
zipCode(zCode),
country(cntry),
region(rgn),
locality(lclty),
street(strt),
external(ext) {}
Shared::VCard::VCard():
fullName(),
firstName(),
middleName(),
lastName(),
nickName(),
description(),
url(),
organizationName(),
organizationUnit(),
organizationRole(),
jobTitle(),
birthday(),
photoType(Avatar::empty),
photoPath(),
receivingTime(QDateTime::currentDateTimeUtc()),
emails(),
phones(),
addresses() {}
Shared::VCard::VCard(const QDateTime& creationTime):
fullName(),
firstName(),
middleName(),
lastName(),
nickName(),
description(),
url(),
organizationName(),
organizationUnit(),
organizationRole(),
jobTitle(),
birthday(),
photoType(Avatar::empty),
photoPath(),
receivingTime(creationTime),
emails(),
phones(),
addresses() {}
QString Shared::VCard::getAvatarPath() const
{
return photoPath;
}
Shared::Avatar Shared::VCard::getAvatarType() const
{
return photoType;
}
QDate Shared::VCard::getBirthday() const
{
return birthday;
}
QString Shared::VCard::getDescription() const
{
return description;
}
QString Shared::VCard::getFirstName() const
{
return firstName;
}
QString Shared::VCard::getLastName() const
{
return lastName;
}
QString Shared::VCard::getMiddleName() const
{
return middleName;
}
QString Shared::VCard::getNickName() const
{
return nickName;
}
void Shared::VCard::setAvatarPath(const QString& path)
{
if (path != photoPath) {
photoPath = path;
}
}
void Shared::VCard::setAvatarType(Shared::Avatar type)
{
if (photoType != type) {
photoType = type;
}
}
void Shared::VCard::setBirthday(const QDate& date)
{
if (date.isValid() && birthday != date) {
birthday = date;
}
}
void Shared::VCard::setDescription(const QString& descr)
{
if (description != descr) {
description = descr;
}
}
void Shared::VCard::setFirstName(const QString& first)
{
if (firstName != first) {
firstName = first;
}
}
void Shared::VCard::setLastName(const QString& last)
{
if (lastName != last) {
lastName = last;
}
}
void Shared::VCard::setMiddleName(const QString& middle)
{
if (middleName != middle) {
middleName = middle;
}
}
void Shared::VCard::setNickName(const QString& nick)
{
if (nickName != nick) {
nickName = nick;
}
}
QString Shared::VCard::getFullName() const
{
return fullName;
}
QString Shared::VCard::getUrl() const
{
return url;
}
void Shared::VCard::setFullName(const QString& name)
{
if (fullName != name) {
fullName = name;
}
}
void Shared::VCard::setUrl(const QString& u)
{
if (url != u) {
url = u;
}
}
QString Shared::VCard::getOrgName() const
{
return organizationName;
}
QString Shared::VCard::getOrgRole() const
{
return organizationRole;
}
QString Shared::VCard::getOrgTitle() const
{
return jobTitle;
}
QString Shared::VCard::getOrgUnit() const
{
return organizationUnit;
}
void Shared::VCard::setOrgName(const QString& name)
{
if (organizationName != name) {
organizationName = name;
}
}
void Shared::VCard::setOrgRole(const QString& role)
{
if (organizationRole != role) {
organizationRole = role;
}
}
void Shared::VCard::setOrgTitle(const QString& title)
{
if (jobTitle != title) {
jobTitle = title;
}
}
void Shared::VCard::setOrgUnit(const QString& unit)
{
if (organizationUnit != unit) {
organizationUnit = unit;
}
}
QDateTime Shared::VCard::getReceivingTime() const
{
return receivingTime;
}
std::deque<Shared::VCard::Email> & Shared::VCard::getEmails()
{
return emails;
}
std::deque<Shared::VCard::Address> & Shared::VCard::getAddresses()
{
return addresses;
}
std::deque<Shared::VCard::Phone> & Shared::VCard::getPhones()
{
return phones;
}
const std::deque<Shared::VCard::Email> & Shared::VCard::getEmails() const
{
return emails;
}
const std::deque<Shared::VCard::Address> & Shared::VCard::getAddresses() const
{
return addresses;
}
const std::deque<Shared::VCard::Phone> & Shared::VCard::getPhones() const
{
return phones;
}
const std::deque<QString>Shared::VCard::Contact::roleNames = {"Not specified", "Personal", "Business"};
const std::deque<QString>Shared::VCard::Phone::typeNames = {"Fax", "Pager", "Voice", "Cell", "Video", "Modem", "Other"};

152
shared/vcard.h Normal file
View file

@ -0,0 +1,152 @@
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHARED_VCARD_H
#define SHARED_VCARD_H
#include <QString>
#include <QDateTime>
#include <deque>
#include "enums.h"
namespace Shared {
class VCard {
class Contact {
public:
enum Role {
none,
home,
work
};
static const std::deque<QString> roleNames;
Contact(Role p_role = none, bool p_prefered = false);
Role role;
bool prefered;
};
public:
class Email : public Contact {
public:
Email(const QString& address, Role p_role = none, bool p_prefered = false);
QString address;
};
class Phone : public Contact {
public:
enum Type {
fax,
pager,
voice,
cell,
video,
modem,
other
};
static const std::deque<QString> typeNames;
Phone(const QString& number, Type p_type = voice, Role p_role = none, bool p_prefered = false);
QString number;
Type type;
};
class Address : public Contact {
public:
Address(
const QString& zCode = "",
const QString& cntry = "",
const QString& rgn = "",
const QString& lclty = "",
const QString& strt = "",
const QString& ext = "",
Role p_role = none,
bool p_prefered = false
);
QString zipCode;
QString country;
QString region;
QString locality;
QString street;
QString external;
};
VCard();
VCard(const QDateTime& creationTime);
QString getFullName() const;
void setFullName(const QString& name);
QString getFirstName() const;
void setFirstName(const QString& first);
QString getMiddleName() const;
void setMiddleName(const QString& middle);
QString getLastName() const;
void setLastName(const QString& last);
QString getNickName() const;
void setNickName(const QString& nick);
QString getDescription() const;
void setDescription(const QString& descr);
QString getUrl() const;
void setUrl(const QString& u);
QDate getBirthday() const;
void setBirthday(const QDate& date);
Avatar getAvatarType() const;
void setAvatarType(Avatar type);
QString getAvatarPath() const;
void setAvatarPath(const QString& path);
QString getOrgName() const;
void setOrgName(const QString& name);
QString getOrgUnit() const;
void setOrgUnit(const QString& unit);
QString getOrgRole() const;
void setOrgRole(const QString& role);
QString getOrgTitle() const;
void setOrgTitle(const QString& title);
QDateTime getReceivingTime() const;
std::deque<Email>& getEmails();
const std::deque<Email>& getEmails() const;
std::deque<Phone>& getPhones();
const std::deque<Phone>& getPhones() const;
std::deque<Address>& getAddresses();
const std::deque<Address>& getAddresses() const;
private:
QString fullName;
QString firstName;
QString middleName;
QString lastName;
QString nickName;
QString description;
QString url;
QString organizationName;
QString organizationUnit;
QString organizationRole;
QString jobTitle;
QDate birthday;
Avatar photoType;
QString photoPath;
QDateTime receivingTime;
std::deque<Email> emails;
std::deque<Phone> phones;
std::deque<Address> addresses;
};
}
#endif // SHARED_VCARD_H