forked from blue/squawk
refactoring: UI squawk now belongs to a new class, it enables me doing trayed mode, when main window is destroyed
This commit is contained in:
parent
83cb220175
commit
721d3a1a89
16 changed files with 908 additions and 769 deletions
7
main/CMakeLists.txt
Normal file
7
main/CMakeLists.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
target_sources(squawk PRIVATE
|
||||
main.cpp
|
||||
application.cpp
|
||||
application.h
|
||||
dialogqueue.cpp
|
||||
dialogqueue.h
|
||||
)
|
476
main/application.cpp
Normal file
476
main/application.cpp
Normal file
|
@ -0,0 +1,476 @@
|
|||
// 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 "application.h"
|
||||
|
||||
Application::Application(Core::Squawk* p_core):
|
||||
QObject(),
|
||||
availability(Shared::Availability::offline),
|
||||
core(p_core),
|
||||
squawk(nullptr),
|
||||
notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()),
|
||||
roster(),
|
||||
conversations(),
|
||||
dialogueQueue(roster),
|
||||
nowQuitting(false),
|
||||
destroyingSquawk(false)
|
||||
{
|
||||
connect(&roster, &Models::Roster::unnoticedMessage, this, &Application::notify);
|
||||
|
||||
|
||||
//connecting myself to the backed
|
||||
connect(this, &Application::changeState, core, &Core::Squawk::changeState);
|
||||
connect(this, &Application::setRoomJoined, core, &Core::Squawk::setRoomJoined);
|
||||
connect(this, &Application::setRoomAutoJoin, core, &Core::Squawk::setRoomAutoJoin);
|
||||
connect(this, &Application::subscribeContact, core, &Core::Squawk::subscribeContact);
|
||||
connect(this, &Application::unsubscribeContact, core, &Core::Squawk::unsubscribeContact);
|
||||
connect(this, &Application::replaceMessage, core, &Core::Squawk::replaceMessage);
|
||||
connect(this, &Application::sendMessage, core, &Core::Squawk::sendMessage);
|
||||
connect(this, &Application::resendMessage, core, &Core::Squawk::resendMessage);
|
||||
connect(&roster, &Models::Roster::requestArchive,
|
||||
std::bind(&Core::Squawk::requestArchive, core, std::placeholders::_1, std::placeholders::_2, 20, std::placeholders::_3));
|
||||
|
||||
connect(&dialogueQueue, &DialogQueue::modifyAccountRequest, core, &Core::Squawk::modifyAccountRequest);
|
||||
connect(&dialogueQueue, &DialogQueue::responsePassword, core, &Core::Squawk::responsePassword);
|
||||
connect(&dialogueQueue, &DialogQueue::disconnectAccount, core, &Core::Squawk::disconnectAccount);
|
||||
|
||||
connect(&roster, &Models::Roster::fileDownloadRequest, core, &Core::Squawk::fileDownloadRequest);
|
||||
connect(&roster, &Models::Roster::localPathInvalid, core, &Core::Squawk::onLocalPathInvalid);
|
||||
|
||||
|
||||
//coonecting backend to myself
|
||||
connect(core, &Core::Squawk::stateChanged, this, &Application::stateChanged);
|
||||
|
||||
connect(core, &Core::Squawk::accountMessage, &roster, &Models::Roster::addMessage);
|
||||
connect(core, &Core::Squawk::responseArchive, &roster, &Models::Roster::responseArchive);
|
||||
connect(core, &Core::Squawk::changeMessage, &roster, &Models::Roster::changeMessage);
|
||||
|
||||
connect(core, &Core::Squawk::newAccount, &roster, &Models::Roster::addAccount);
|
||||
connect(core, &Core::Squawk::changeAccount, this, &Application::changeAccount);
|
||||
connect(core, &Core::Squawk::removeAccount, this, &Application::removeAccount);
|
||||
|
||||
connect(core, &Core::Squawk::addContact, this, &Application::addContact);
|
||||
connect(core, &Core::Squawk::addGroup, this, &Application::addGroup);
|
||||
connect(core, &Core::Squawk::removeGroup, &roster, &Models::Roster::removeGroup);
|
||||
connect(core, qOverload<const QString&, const QString&>(&Core::Squawk::removeContact),
|
||||
&roster, qOverload<const QString&, const QString&>(&Models::Roster::removeContact));
|
||||
connect(core, qOverload<const QString&, const QString&, const QString&>(&Core::Squawk::removeContact),
|
||||
&roster, qOverload<const QString&, const QString&, const QString&>(&Models::Roster::removeContact));
|
||||
connect(core, &Core::Squawk::changeContact, &roster, &Models::Roster::changeContact);
|
||||
connect(core, &Core::Squawk::addPresence, &roster, &Models::Roster::addPresence);
|
||||
connect(core, &Core::Squawk::removePresence, &roster, &Models::Roster::removePresence);
|
||||
|
||||
connect(core, &Core::Squawk::addRoom, &roster, &Models::Roster::addRoom);
|
||||
connect(core, &Core::Squawk::changeRoom, &roster, &Models::Roster::changeRoom);
|
||||
connect(core, &Core::Squawk::removeRoom, &roster, &Models::Roster::removeRoom);
|
||||
connect(core, &Core::Squawk::addRoomParticipant, &roster, &Models::Roster::addRoomParticipant);
|
||||
connect(core, &Core::Squawk::changeRoomParticipant, &roster, &Models::Roster::changeRoomParticipant);
|
||||
connect(core, &Core::Squawk::removeRoomParticipant, &roster, &Models::Roster::removeRoomParticipant);
|
||||
|
||||
|
||||
connect(core, &Core::Squawk::fileDownloadComplete, std::bind(&Models::Roster::fileComplete, &roster, std::placeholders::_1, false));
|
||||
connect(core, &Core::Squawk::fileUploadComplete, std::bind(&Models::Roster::fileComplete, &roster, std::placeholders::_1, true));
|
||||
connect(core, &Core::Squawk::fileProgress, &roster, &Models::Roster::fileProgress);
|
||||
connect(core, &Core::Squawk::fileError, &roster, &Models::Roster::fileError);
|
||||
|
||||
connect(core, &Core::Squawk::requestPassword, this, &Application::requestPassword);
|
||||
connect(core, &Core::Squawk::ready, this, &Application::readSettings);
|
||||
|
||||
}
|
||||
|
||||
Application::~Application() {}
|
||||
|
||||
void Application::quit()
|
||||
{
|
||||
if (!nowQuitting) {
|
||||
nowQuitting = true;
|
||||
emit quitting();
|
||||
|
||||
writeSettings();
|
||||
for (Conversations::const_iterator itr = conversations.begin(), end = conversations.end(); itr != end; ++itr) {
|
||||
disconnect(itr->second, &Conversation::destroyed, this, &Application::onConversationClosed);
|
||||
itr->second->close();
|
||||
}
|
||||
conversations.clear();
|
||||
dialogueQueue.quit();
|
||||
|
||||
if (squawk != nullptr) {
|
||||
squawk->close();
|
||||
}
|
||||
if (!destroyingSquawk) {
|
||||
checkForTheLastWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::checkForTheLastWindow()
|
||||
{
|
||||
if (QApplication::topLevelWidgets().size() > 0) {
|
||||
emit readyToQuit();
|
||||
} else {
|
||||
connect(qApp, &QApplication::lastWindowClosed, this, &Application::readyToQuit);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::createMainWindow()
|
||||
{
|
||||
if (squawk == nullptr) {
|
||||
squawk = new Squawk(roster);
|
||||
|
||||
connect(squawk, &Squawk::notify, this, &Application::notify);
|
||||
connect(squawk, &Squawk::changeSubscription, this, &Application::changeSubscription);
|
||||
connect(squawk, &Squawk::openedConversation, this, &Application::onSquawkOpenedConversation);
|
||||
connect(squawk, &Squawk::openConversation, this, &Application::openConversation);
|
||||
connect(squawk, &Squawk::changeState, this, &Application::setState);
|
||||
connect(squawk, &Squawk::closing, this, &Application::onSquawkClosing);
|
||||
|
||||
connect(squawk, &Squawk::modifyAccountRequest, core, &Core::Squawk::modifyAccountRequest);
|
||||
connect(squawk, &Squawk::newAccountRequest, core, &Core::Squawk::newAccountRequest);
|
||||
connect(squawk, &Squawk::removeAccountRequest, core, &Core::Squawk::removeAccountRequest);
|
||||
connect(squawk, &Squawk::connectAccount, core, &Core::Squawk::connectAccount);
|
||||
connect(squawk, &Squawk::disconnectAccount, core, &Core::Squawk::disconnectAccount);
|
||||
|
||||
connect(squawk, &Squawk::addContactRequest, core, &Core::Squawk::addContactRequest);
|
||||
connect(squawk, &Squawk::removeContactRequest, core, &Core::Squawk::removeContactRequest);
|
||||
connect(squawk, &Squawk::removeRoomRequest, core, &Core::Squawk::removeRoomRequest);
|
||||
connect(squawk, &Squawk::addRoomRequest, core, &Core::Squawk::addRoomRequest);
|
||||
connect(squawk, &Squawk::addContactToGroupRequest, core, &Core::Squawk::addContactToGroupRequest);
|
||||
connect(squawk, &Squawk::removeContactFromGroupRequest, core, &Core::Squawk::removeContactFromGroupRequest);
|
||||
connect(squawk, &Squawk::renameContactRequest, core, &Core::Squawk::renameContactRequest);
|
||||
connect(squawk, &Squawk::requestVCard, core, &Core::Squawk::requestVCard);
|
||||
connect(squawk, &Squawk::uploadVCard, core, &Core::Squawk::uploadVCard);
|
||||
connect(squawk, &Squawk::changeDownloadsPath, core, &Core::Squawk::changeDownloadsPath);
|
||||
|
||||
connect(core, &Core::Squawk::responseVCard, squawk, &Squawk::responseVCard);
|
||||
|
||||
dialogueQueue.setParentWidnow(squawk);
|
||||
squawk->stateChanged(availability);
|
||||
squawk->show();
|
||||
}
|
||||
}
|
||||
|
||||
void Application::onSquawkClosing()
|
||||
{
|
||||
dialogueQueue.setParentWidnow(nullptr);
|
||||
|
||||
disconnect(core, &Core::Squawk::responseVCard, squawk, &Squawk::responseVCard);
|
||||
|
||||
destroyingSquawk = true;
|
||||
squawk->deleteLater();
|
||||
squawk = nullptr;
|
||||
|
||||
//for now
|
||||
quit();
|
||||
}
|
||||
|
||||
void Application::onSquawkDestroyed() {
|
||||
destroyingSquawk = false;
|
||||
if (nowQuitting) {
|
||||
checkForTheLastWindow();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Application::notify(const QString& account, const Shared::Message& msg)
|
||||
{
|
||||
QString name = QString(roster.getContactName(account, msg.getPenPalJid()));
|
||||
QString path = QString(roster.getContactIconPath(account, msg.getPenPalJid(), msg.getPenPalResource()));
|
||||
QVariantList args;
|
||||
args << QString();
|
||||
|
||||
args << qHash(msg.getId());
|
||||
if (path.size() > 0) {
|
||||
args << path;
|
||||
} else {
|
||||
args << QString("mail-message"); //TODO should here better be unknown user icon?
|
||||
}
|
||||
if (msg.getType() == Shared::Message::groupChat) {
|
||||
args << msg.getFromResource() + " from " + name;
|
||||
} else {
|
||||
args << name;
|
||||
}
|
||||
|
||||
QString body(msg.getBody());
|
||||
QString oob(msg.getOutOfBandUrl());
|
||||
if (body == oob) {
|
||||
body = tr("Attached file");
|
||||
}
|
||||
|
||||
args << body;
|
||||
args << QStringList();
|
||||
args << QVariantMap({
|
||||
{"desktop-entry", QString(QCoreApplication::applicationName())},
|
||||
{"category", QString("message")},
|
||||
// {"sound-file", "/path/to/macaw/squawk"},
|
||||
{"sound-name", QString("message-new-instant")}
|
||||
});
|
||||
args << -1;
|
||||
notifications.callWithArgumentList(QDBus::AutoDetect, "Notify", args);
|
||||
|
||||
if (squawk != nullptr) {
|
||||
QApplication::alert(squawk);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::setState(Shared::Availability p_availability)
|
||||
{
|
||||
if (availability != p_availability) {
|
||||
availability = p_availability;
|
||||
emit changeState(availability);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::stateChanged(Shared::Availability state)
|
||||
{
|
||||
availability = state;
|
||||
if (squawk != nullptr) {
|
||||
squawk->stateChanged(state);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::readSettings()
|
||||
{
|
||||
QSettings settings;
|
||||
settings.beginGroup("ui");
|
||||
int avail;
|
||||
if (settings.contains("availability")) {
|
||||
avail = settings.value("availability").toInt();
|
||||
} else {
|
||||
avail = static_cast<int>(Shared::Availability::online);
|
||||
}
|
||||
settings.endGroup();
|
||||
|
||||
setState(Shared::Global::fromInt<Shared::Availability>(avail));
|
||||
createMainWindow();
|
||||
}
|
||||
|
||||
void Application::writeSettings()
|
||||
{
|
||||
QSettings settings;
|
||||
settings.setValue("availability", static_cast<int>(availability));
|
||||
}
|
||||
|
||||
void Application::requestPassword(const QString& account, bool authenticationError) {
|
||||
if (authenticationError) {
|
||||
dialogueQueue.addAction(account, DialogQueue::askCredentials);
|
||||
} else {
|
||||
dialogueQueue.addAction(account, DialogQueue::askPassword);
|
||||
}
|
||||
|
||||
}
|
||||
void Application::onConversationClosed()
|
||||
{
|
||||
Conversation* conv = static_cast<Conversation*>(sender());
|
||||
Models::Roster::ElId id(conv->getAccount(), conv->getJid());
|
||||
Conversations::const_iterator itr = conversations.find(id);
|
||||
if (itr != conversations.end()) {
|
||||
conversations.erase(itr);
|
||||
}
|
||||
if (conv->isMuc) {
|
||||
Room* room = static_cast<Room*>(conv);
|
||||
if (!room->autoJoined()) {
|
||||
emit setRoomJoined(id.account, id.name, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::changeSubscription(const Models::Roster::ElId& id, bool subscribe)
|
||||
{
|
||||
Models::Item::Type type = roster.getContactType(id);
|
||||
|
||||
switch (type) {
|
||||
case Models::Item::contact:
|
||||
if (subscribe) {
|
||||
emit subscribeContact(id.account, id.name, "");
|
||||
} else {
|
||||
emit unsubscribeContact(id.account, id.name, "");
|
||||
}
|
||||
break;
|
||||
case Models::Item::room:
|
||||
setRoomAutoJoin(id.account, id.name, subscribe);
|
||||
if (!isConverstationOpened(id)) {
|
||||
emit setRoomJoined(id.account, id.name, subscribe);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Application::subscribeConversation(Conversation* conv)
|
||||
{
|
||||
connect(conv, &Conversation::destroyed, this, &Application::onConversationClosed);
|
||||
connect(conv, &Conversation::sendMessage, this, &Application::onConversationMessage);
|
||||
connect(conv, &Conversation::replaceMessage, this, &Application::onConversationReplaceMessage);
|
||||
connect(conv, &Conversation::resendMessage, this, &Application::onConversationResend);
|
||||
connect(conv, &Conversation::notifyableMessage, this, &Application::notify);
|
||||
}
|
||||
|
||||
void Application::openConversation(const Models::Roster::ElId& id, const QString& resource)
|
||||
{
|
||||
Conversations::const_iterator itr = conversations.find(id);
|
||||
Models::Account* acc = roster.getAccount(id.account);
|
||||
Conversation* conv = nullptr;
|
||||
bool created = false;
|
||||
if (itr != conversations.end()) {
|
||||
conv = itr->second;
|
||||
} else {
|
||||
Models::Element* el = roster.getElement(id);
|
||||
if (el != NULL) {
|
||||
if (el->type == Models::Item::room) {
|
||||
created = true;
|
||||
Models::Room* room = static_cast<Models::Room*>(el);
|
||||
conv = new Room(acc, room);
|
||||
if (!room->getJoined()) {
|
||||
emit setRoomJoined(id.account, id.name, true);
|
||||
}
|
||||
} else if (el->type == Models::Item::contact) {
|
||||
created = true;
|
||||
conv = new Chat(acc, static_cast<Models::Contact*>(el));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (conv != nullptr) {
|
||||
if (created) {
|
||||
conv->setAttribute(Qt::WA_DeleteOnClose);
|
||||
subscribeConversation(conv);
|
||||
conversations.insert(std::make_pair(id, conv));
|
||||
}
|
||||
|
||||
conv->show();
|
||||
conv->raise();
|
||||
conv->activateWindow();
|
||||
|
||||
if (resource.size() > 0) {
|
||||
conv->setPalResource(resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::onConversationMessage(const Shared::Message& msg)
|
||||
{
|
||||
Conversation* conv = static_cast<Conversation*>(sender());
|
||||
QString acc = conv->getAccount();
|
||||
|
||||
roster.addMessage(acc, msg);
|
||||
emit sendMessage(acc, msg);
|
||||
}
|
||||
|
||||
void Application::onConversationReplaceMessage(const QString& originalId, const Shared::Message& msg)
|
||||
{
|
||||
Conversation* conv = static_cast<Conversation*>(sender());
|
||||
QString acc = conv->getAccount();
|
||||
|
||||
roster.changeMessage(acc, msg.getPenPalJid(), originalId, {
|
||||
{"state", static_cast<uint>(Shared::Message::State::pending)}
|
||||
});
|
||||
emit replaceMessage(acc, originalId, msg);
|
||||
}
|
||||
|
||||
void Application::onConversationResend(const QString& id)
|
||||
{
|
||||
Conversation* conv = static_cast<Conversation*>(sender());
|
||||
QString acc = conv->getAccount();
|
||||
QString jid = conv->getJid();
|
||||
|
||||
emit resendMessage(acc, jid, id);
|
||||
}
|
||||
|
||||
void Application::onSquawkOpenedConversation() {
|
||||
subscribeConversation(squawk->currentConversation);
|
||||
Models::Roster::ElId id = squawk->currentConversationId();
|
||||
|
||||
const Models::Element* el = roster.getElementConst(id);
|
||||
if (el != NULL && el->isRoom() && !static_cast<const Models::Room*>(el)->getJoined()) {
|
||||
emit setRoomJoined(id.account, id.name, true);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::removeAccount(const QString& account)
|
||||
{
|
||||
Conversations::const_iterator itr = conversations.begin();
|
||||
while (itr != conversations.end()) {
|
||||
if (itr->first.account == account) {
|
||||
Conversations::const_iterator lItr = itr;
|
||||
++itr;
|
||||
Conversation* conv = lItr->second;
|
||||
disconnect(conv, &Conversation::destroyed, this, &Application::onConversationClosed);
|
||||
conv->close();
|
||||
conversations.erase(lItr);
|
||||
} else {
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
if (squawk != nullptr && squawk->currentConversationId().account == account) {
|
||||
squawk->closeCurrentConversation();
|
||||
}
|
||||
|
||||
roster.removeAccount(account);
|
||||
}
|
||||
|
||||
void Application::changeAccount(const QString& account, const QMap<QString, QVariant>& data)
|
||||
{
|
||||
for (QMap<QString, QVariant>::const_iterator itr = data.begin(), end = data.end(); itr != end; ++itr) {
|
||||
QString attr = itr.key();
|
||||
roster.updateAccount(account, attr, *itr);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::addContact(const QString& account, const QString& jid, const QString& group, const QMap<QString, QVariant>& data)
|
||||
{
|
||||
roster.addContact(account, jid, group, data);
|
||||
|
||||
if (squawk != nullptr) {
|
||||
QSettings settings;
|
||||
settings.beginGroup("ui");
|
||||
settings.beginGroup("roster");
|
||||
settings.beginGroup(account);
|
||||
if (settings.value("expanded", false).toBool()) {
|
||||
QModelIndex ind = roster.getAccountIndex(account);
|
||||
squawk->expand(ind);
|
||||
}
|
||||
settings.endGroup();
|
||||
settings.endGroup();
|
||||
settings.endGroup();
|
||||
}
|
||||
}
|
||||
|
||||
void Application::addGroup(const QString& account, const QString& name)
|
||||
{
|
||||
roster.addGroup(account, name);
|
||||
|
||||
if (squawk != nullptr) {
|
||||
QSettings settings;
|
||||
settings.beginGroup("ui");
|
||||
settings.beginGroup("roster");
|
||||
settings.beginGroup(account);
|
||||
if (settings.value("expanded", false).toBool()) {
|
||||
QModelIndex ind = roster.getAccountIndex(account);
|
||||
squawk->expand(ind);
|
||||
if (settings.value(name + "/expanded", false).toBool()) {
|
||||
squawk->expand(roster.getGroupIndex(account, name));
|
||||
}
|
||||
}
|
||||
settings.endGroup();
|
||||
settings.endGroup();
|
||||
settings.endGroup();
|
||||
}
|
||||
}
|
||||
|
||||
bool Application::isConverstationOpened(const Models::Roster::ElId& id) const {
|
||||
return (conversations.count(id) > 0) || (squawk != nullptr && squawk->currentConversationId() == id);}
|
111
main/application.h
Normal file
111
main/application.h
Normal file
|
@ -0,0 +1,111 @@
|
|||
// 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 APPLICATION_H
|
||||
#define APPLICATION_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <QObject>
|
||||
#include <QDBusInterface>
|
||||
|
||||
#include "dialogqueue.h"
|
||||
#include "core/squawk.h"
|
||||
|
||||
#include "ui/squawk.h"
|
||||
#include "ui/models/roster.h"
|
||||
#include "ui/widgets/conversation.h"
|
||||
|
||||
#include "shared/message.h"
|
||||
#include "shared/enums.h"
|
||||
|
||||
/**
|
||||
* @todo write docs
|
||||
*/
|
||||
class Application : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Application(Core::Squawk* core);
|
||||
~Application();
|
||||
|
||||
bool isConverstationOpened(const Models::Roster::ElId& id) const;
|
||||
|
||||
signals:
|
||||
void sendMessage(const QString& account, const Shared::Message& data);
|
||||
void replaceMessage(const QString& account, const QString& originalId, const Shared::Message& data);
|
||||
void resendMessage(const QString& account, const QString& jid, const QString& id);
|
||||
|
||||
void changeState(Shared::Availability state);
|
||||
|
||||
void setRoomJoined(const QString& account, const QString& jid, bool joined);
|
||||
void setRoomAutoJoin(const QString& account, const QString& jid, bool joined);
|
||||
void subscribeContact(const QString& account, const QString& jid, const QString& reason);
|
||||
void unsubscribeContact(const QString& account, const QString& jid, const QString& reason);
|
||||
|
||||
void quitting();
|
||||
void readyToQuit();
|
||||
|
||||
public slots:
|
||||
void readSettings();
|
||||
void quit();
|
||||
|
||||
protected slots:
|
||||
void notify(const QString& account, const Shared::Message& msg);
|
||||
void setState(Shared::Availability availability);
|
||||
|
||||
void changeAccount(const QString& account, const QMap<QString, QVariant>& data);
|
||||
void removeAccount(const QString& account);
|
||||
void openConversation(const Models::Roster::ElId& id, const QString& resource = "");
|
||||
|
||||
void addGroup(const QString& account, const QString& name);
|
||||
void addContact(const QString& account, const QString& jid, const QString& group, const QMap<QString, QVariant>& data);
|
||||
|
||||
void requestPassword(const QString& account, bool authenticationError);
|
||||
|
||||
void writeSettings();
|
||||
|
||||
private slots:
|
||||
void onConversationClosed();
|
||||
void changeSubscription(const Models::Roster::ElId& id, bool subscribe);
|
||||
void onSquawkOpenedConversation();
|
||||
void onConversationMessage(const Shared::Message& msg);
|
||||
void onConversationReplaceMessage(const QString& originalId, const Shared::Message& msg);
|
||||
void onConversationResend(const QString& id);
|
||||
void stateChanged(Shared::Availability state);
|
||||
void onSquawkClosing();
|
||||
void onSquawkDestroyed();
|
||||
|
||||
private:
|
||||
void createMainWindow();
|
||||
void subscribeConversation(Conversation* conv);
|
||||
void checkForTheLastWindow();
|
||||
|
||||
private:
|
||||
typedef std::map<Models::Roster::ElId, Conversation*> Conversations;
|
||||
|
||||
Shared::Availability availability;
|
||||
Core::Squawk* core;
|
||||
Squawk* squawk;
|
||||
QDBusInterface notifications;
|
||||
Models::Roster roster;
|
||||
Conversations conversations;
|
||||
DialogQueue dialogueQueue;
|
||||
bool nowQuitting;
|
||||
bool destroyingSquawk;
|
||||
};
|
||||
|
||||
#endif // APPLICATION_H
|
187
main/dialogqueue.cpp
Normal file
187
main/dialogqueue.cpp
Normal file
|
@ -0,0 +1,187 @@
|
|||
// 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 "dialogqueue.h"
|
||||
#include <QDebug>
|
||||
|
||||
DialogQueue::DialogQueue(const Models::Roster& p_roster):
|
||||
QObject(),
|
||||
currentSource(),
|
||||
currentAction(none),
|
||||
queue(),
|
||||
collection(queue.get<0>()),
|
||||
sequence(queue.get<1>()),
|
||||
prompt(nullptr),
|
||||
parent(nullptr),
|
||||
roster(p_roster)
|
||||
{
|
||||
}
|
||||
|
||||
DialogQueue::~DialogQueue()
|
||||
{
|
||||
}
|
||||
|
||||
void DialogQueue::quit()
|
||||
{
|
||||
queue.clear();
|
||||
if (currentAction != none) {
|
||||
actionDone();
|
||||
}
|
||||
}
|
||||
|
||||
void DialogQueue::setParentWidnow(QMainWindow* p_parent)
|
||||
{
|
||||
parent = p_parent;
|
||||
}
|
||||
|
||||
bool DialogQueue::addAction(const QString& source, DialogQueue::Action action)
|
||||
{
|
||||
if (action == none) {
|
||||
return false;
|
||||
}
|
||||
if (currentAction == none) {
|
||||
currentAction = action;
|
||||
currentSource = source;
|
||||
performNextAction();
|
||||
return true;
|
||||
} else {
|
||||
if (currentAction != action || currentSource != source) {
|
||||
std::pair<Queue::iterator, bool> result = queue.emplace(source, action);
|
||||
return result.second;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DialogQueue::cancelAction(const QString& source, DialogQueue::Action action)
|
||||
{
|
||||
if (source == currentSource && action == currentAction) {
|
||||
actionDone();
|
||||
return true;
|
||||
} else {
|
||||
Collection::iterator itr = collection.find(ActionId{source, action});
|
||||
if (itr != collection.end()) {
|
||||
collection.erase(itr);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DialogQueue::performNextAction()
|
||||
{
|
||||
switch (currentAction) {
|
||||
case none:
|
||||
actionDone();
|
||||
break;
|
||||
case askPassword: {
|
||||
QInputDialog* dialog = new QInputDialog(parent);
|
||||
prompt = dialog;
|
||||
connect(dialog, &QDialog::accepted, this, &DialogQueue::onPropmtAccepted);
|
||||
connect(dialog, &QDialog::rejected, this, &DialogQueue::onPropmtRejected);
|
||||
dialog->setInputMode(QInputDialog::TextInput);
|
||||
dialog->setTextEchoMode(QLineEdit::Password);
|
||||
dialog->setLabelText(tr("Input the password for account %1").arg(currentSource));
|
||||
dialog->setWindowTitle(tr("Password for account %1").arg(currentSource));
|
||||
dialog->setTextValue("");
|
||||
dialog->exec();
|
||||
}
|
||||
break;
|
||||
case askCredentials: {
|
||||
CredentialsPrompt* dialog = new CredentialsPrompt(parent);
|
||||
prompt = dialog;
|
||||
connect(dialog, &QDialog::accepted, this, &DialogQueue::onPropmtAccepted);
|
||||
connect(dialog, &QDialog::rejected, this, &DialogQueue::onPropmtRejected);
|
||||
const Models::Account* acc = roster.getAccountConst(currentSource);
|
||||
dialog->setAccount(currentSource);
|
||||
dialog->setLogin(acc->getLogin());
|
||||
dialog->setPassword(acc->getPassword());
|
||||
dialog->exec();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void DialogQueue::onPropmtAccepted()
|
||||
{
|
||||
switch (currentAction) {
|
||||
case none:
|
||||
break;
|
||||
case askPassword: {
|
||||
QInputDialog* dialog = static_cast<QInputDialog*>(prompt);
|
||||
emit responsePassword(currentSource, dialog->textValue());
|
||||
}
|
||||
break;
|
||||
case askCredentials: {
|
||||
CredentialsPrompt* dialog = static_cast<CredentialsPrompt*>(prompt);
|
||||
emit modifyAccountRequest(currentSource, {
|
||||
{"login", dialog->getLogin()},
|
||||
{"password", dialog->getPassword()}
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
actionDone();
|
||||
}
|
||||
|
||||
void DialogQueue::onPropmtRejected()
|
||||
{
|
||||
switch (currentAction) {
|
||||
case none:
|
||||
break;
|
||||
case askPassword:
|
||||
case askCredentials:
|
||||
emit disconnectAccount(currentSource);
|
||||
break;
|
||||
}
|
||||
actionDone();
|
||||
}
|
||||
|
||||
void DialogQueue::actionDone()
|
||||
{
|
||||
prompt->deleteLater();
|
||||
prompt = nullptr;
|
||||
|
||||
if (queue.empty()) {
|
||||
currentAction = none;
|
||||
currentSource = "";
|
||||
} else {
|
||||
Sequence::iterator itr = sequence.begin();
|
||||
currentAction = itr->action;
|
||||
currentSource = itr->source;
|
||||
sequence.erase(itr);
|
||||
performNextAction();
|
||||
}
|
||||
}
|
||||
|
||||
DialogQueue::ActionId::ActionId(const QString& p_source, DialogQueue::Action p_action):
|
||||
source(p_source),
|
||||
action(p_action) {}
|
||||
|
||||
bool DialogQueue::ActionId::operator < (const DialogQueue::ActionId& other) const
|
||||
{
|
||||
if (action == other.action) {
|
||||
return source < other.source;
|
||||
} else {
|
||||
return action < other.action;
|
||||
}
|
||||
}
|
||||
|
||||
DialogQueue::ActionId::ActionId(const DialogQueue::ActionId& other):
|
||||
source(other.source),
|
||||
action(other.action) {}
|
101
main/dialogqueue.h
Normal file
101
main/dialogqueue.h
Normal file
|
@ -0,0 +1,101 @@
|
|||
// 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 DIALOGQUEUE_H
|
||||
#define DIALOGQUEUE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QInputDialog>
|
||||
#include <QMainWindow>
|
||||
|
||||
#include <boost/multi_index_container.hpp>
|
||||
#include <boost/multi_index/ordered_index.hpp>
|
||||
#include <boost/multi_index/sequenced_index.hpp>
|
||||
|
||||
#include <ui/widgets/accounts/credentialsprompt.h>
|
||||
#include <ui/models/roster.h>
|
||||
|
||||
class DialogQueue : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Action {
|
||||
none,
|
||||
askPassword,
|
||||
askCredentials
|
||||
};
|
||||
|
||||
DialogQueue(const Models::Roster& roster);
|
||||
~DialogQueue();
|
||||
|
||||
bool addAction(const QString& source, Action action);
|
||||
bool cancelAction(const QString& source, Action action);
|
||||
|
||||
signals:
|
||||
void modifyAccountRequest(const QString&, const QMap<QString, QVariant>&);
|
||||
void responsePassword(const QString& account, const QString& password);
|
||||
void disconnectAccount(const QString&);
|
||||
|
||||
public:
|
||||
void setParentWidnow(QMainWindow* parent);
|
||||
void quit();
|
||||
|
||||
private:
|
||||
void performNextAction();
|
||||
void actionDone();
|
||||
|
||||
private slots:
|
||||
void onPropmtAccepted();
|
||||
void onPropmtRejected();
|
||||
|
||||
private:
|
||||
QString currentSource;
|
||||
Action currentAction;
|
||||
|
||||
struct ActionId {
|
||||
public:
|
||||
ActionId(const QString& p_source, Action p_action);
|
||||
ActionId(const ActionId& other);
|
||||
|
||||
const QString source;
|
||||
const Action action;
|
||||
|
||||
bool operator < (const ActionId& other) const;
|
||||
};
|
||||
|
||||
typedef boost::multi_index_container <
|
||||
ActionId,
|
||||
boost::multi_index::indexed_by <
|
||||
boost::multi_index::ordered_unique <
|
||||
boost::multi_index::identity <ActionId>
|
||||
>,
|
||||
boost::multi_index::sequenced<>
|
||||
>
|
||||
> Queue;
|
||||
|
||||
typedef Queue::nth_index<0>::type Collection;
|
||||
typedef Queue::nth_index<1>::type Sequence;
|
||||
|
||||
Queue queue;
|
||||
Collection& collection;
|
||||
Sequence& sequence;
|
||||
|
||||
QDialog* prompt;
|
||||
QMainWindow* parent;
|
||||
const Models::Roster& roster;
|
||||
};
|
||||
|
||||
#endif // DIALOGQUEUE_H
|
139
main/main.cpp
Normal file
139
main/main.cpp
Normal file
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* 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 "shared/global.h"
|
||||
#include "shared/messageinfo.h"
|
||||
#include "shared/pathcheck.h"
|
||||
#include "main/application.h"
|
||||
#include "core/signalcatcher.h"
|
||||
#include "core/squawk.h"
|
||||
|
||||
#include <QLibraryInfo>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <QTranslator>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QThread>
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QDir>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
qRegisterMetaType<Shared::Message>("Shared::Message");
|
||||
qRegisterMetaType<Shared::MessageInfo>("Shared::MessageInfo");
|
||||
qRegisterMetaType<Shared::VCard>("Shared::VCard");
|
||||
qRegisterMetaType<std::list<Shared::Message>>("std::list<Shared::Message>");
|
||||
qRegisterMetaType<std::list<Shared::MessageInfo>>("std::list<Shared::MessageInfo>");
|
||||
qRegisterMetaType<QSet<QString>>("QSet<QString>");
|
||||
qRegisterMetaType<Shared::ConnectionState>("Shared::ConnectionState");
|
||||
qRegisterMetaType<Shared::Availability>("Shared::Availability");
|
||||
|
||||
QApplication app(argc, argv);
|
||||
SignalCatcher sc(&app);
|
||||
|
||||
QApplication::setApplicationName("squawk");
|
||||
QApplication::setOrganizationName("macaw.me");
|
||||
QApplication::setApplicationDisplayName("Squawk");
|
||||
QApplication::setApplicationVersion("0.2.2");
|
||||
|
||||
QTranslator qtTranslator;
|
||||
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
||||
app.installTranslator(&qtTranslator);
|
||||
|
||||
QTranslator myappTranslator;
|
||||
QStringList shares = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
|
||||
bool found = false;
|
||||
for (QString share : shares) {
|
||||
found = myappTranslator.load(QLocale(), QLatin1String("squawk"), ".", share + "/l10n");
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
myappTranslator.load(QLocale(), QLatin1String("squawk"), ".", QCoreApplication::applicationDirPath());
|
||||
}
|
||||
|
||||
app.installTranslator(&myappTranslator);
|
||||
|
||||
QIcon icon;
|
||||
icon.addFile(":images/logo.svg", QSize(16, 16));
|
||||
icon.addFile(":images/logo.svg", QSize(24, 24));
|
||||
icon.addFile(":images/logo.svg", QSize(32, 32));
|
||||
icon.addFile(":images/logo.svg", QSize(48, 48));
|
||||
icon.addFile(":images/logo.svg", QSize(64, 64));
|
||||
icon.addFile(":images/logo.svg", QSize(96, 96));
|
||||
icon.addFile(":images/logo.svg", QSize(128, 128));
|
||||
icon.addFile(":images/logo.svg", QSize(256, 256));
|
||||
icon.addFile(":images/logo.svg", QSize(512, 512));
|
||||
QApplication::setWindowIcon(icon);
|
||||
|
||||
new Shared::Global(); //translates enums
|
||||
|
||||
QSettings settings;
|
||||
QVariant vs = settings.value("style");
|
||||
if (vs.isValid()) {
|
||||
QString style = vs.toString().toLower();
|
||||
if (style != "system") {
|
||||
Shared::Global::setStyle(style);
|
||||
}
|
||||
}
|
||||
if (Shared::Global::supported("colorSchemeTools")) {
|
||||
QVariant vt = settings.value("theme");
|
||||
if (vt.isValid()) {
|
||||
QString theme = vt.toString();
|
||||
if (theme.toLower() != "system") {
|
||||
Shared::Global::setTheme(theme);
|
||||
}
|
||||
}
|
||||
}
|
||||
QString path = Shared::downloadsPathCheck();
|
||||
if (path.size() > 0) {
|
||||
settings.setValue("downloadsPath", path);
|
||||
} else {
|
||||
qDebug() << "couldn't initialize directory for downloads, quitting";
|
||||
return -1;
|
||||
}
|
||||
|
||||
Core::Squawk* squawk = new Core::Squawk();
|
||||
QThread* coreThread = new QThread();
|
||||
squawk->moveToThread(coreThread);
|
||||
|
||||
Application application(squawk);
|
||||
|
||||
QObject::connect(&sc, &SignalCatcher::interrupt, &application, &Application::quit);
|
||||
|
||||
QObject::connect(coreThread, &QThread::started, squawk, &Core::Squawk::start);
|
||||
QObject::connect(&application, &Application::quitting, squawk, &Core::Squawk::stop);
|
||||
//QObject::connect(&app, &QApplication::aboutToQuit, &w, &QMainWindow::close);
|
||||
QObject::connect(squawk, &Core::Squawk::quit, squawk, &Core::Squawk::deleteLater);
|
||||
QObject::connect(squawk, &Core::Squawk::destroyed, coreThread, &QThread::quit, Qt::QueuedConnection);
|
||||
QObject::connect(coreThread, &QThread::finished, &app, &QApplication::quit, Qt::QueuedConnection);
|
||||
|
||||
coreThread->start();
|
||||
int result = app.exec();
|
||||
|
||||
if (coreThread->isRunning()) {
|
||||
//coreThread->wait();
|
||||
//todo if I uncomment that, the app will not quit if it has reconnected at least once
|
||||
//it feels like a symptom of something badly desinged in the core thread
|
||||
//need to investigate;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue