From c295fa1c1df84edd2449e664e46d7d09bea27805 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 4 Sep 2019 19:38:52 +0300 Subject: [PATCH] minor fixes, form to join MUC --- core/account.cpp | 43 ++++++---- core/account.h | 3 +- core/squawk.cpp | 11 +++ core/squawk.h | 1 + main.cpp | 2 + ui/CMakeLists.txt | 7 +- ui/models/accounts.cpp | 18 ++-- ui/models/roster.cpp | 2 +- ui/squawk.cpp | 23 ++++++ ui/squawk.h | 8 +- ui/squawk.ui | 17 +++- ui/{ => widgets}/account.cpp | 0 ui/{ => widgets}/account.h | 0 ui/{ => widgets}/account.ui | 0 ui/{ => widgets}/accounts.cpp | 0 ui/{ => widgets}/accounts.h | 2 +- ui/{ => widgets}/accounts.ui | 0 ui/widgets/joinconference.cpp | 79 ++++++++++++++++++ ui/widgets/joinconference.h | 57 +++++++++++++ ui/widgets/joinconference.ui | 142 ++++++++++++++++++++++++++++++++ ui/{ => widgets}/newcontact.cpp | 0 ui/{ => widgets}/newcontact.h | 2 +- ui/{ => widgets}/newcontact.ui | 2 +- 23 files changed, 385 insertions(+), 34 deletions(-) rename ui/{ => widgets}/account.cpp (100%) rename ui/{ => widgets}/account.h (100%) rename ui/{ => widgets}/account.ui (100%) rename ui/{ => widgets}/accounts.cpp (100%) rename ui/{ => widgets}/accounts.h (98%) rename ui/{ => widgets}/accounts.ui (100%) create mode 100644 ui/widgets/joinconference.cpp create mode 100644 ui/widgets/joinconference.h create mode 100644 ui/widgets/joinconference.ui rename ui/{ => widgets}/newcontact.cpp (100%) rename ui/{ => widgets}/newcontact.h (97%) rename ui/{ => widgets}/newcontact.ui (98%) diff --git a/core/account.cpp b/core/account.cpp index c721b4f..b9f39f5 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -1002,19 +1002,7 @@ void Core::Account::bookmarksReceived(const QXmppBookmarkSet& bookmarks) QString jid = c.jid(); std::map::const_iterator cItr = conferences.find(jid); if (cItr == conferences.end()) { - QXmppMucRoom* room = mm->addRoom(jid); - QString nick = c.nickName(); - Conference* conf = new Conference(jid, getName(), c.autoJoin(), c.name(), nick == "" ? getName() : nick, room); - conferences.insert(std::make_pair(jid, conf)); - - handleNewConference(conf); - - emit addRoom(jid, { - {"autoJoin", conf->getAutoJoin()}, - {"joined", conf->getJoined()}, - {"nick", conf->getNick()}, - {"name", conf->getName()} - }); + addNewRoom(jid, c.nickName(), c.name(), c.autoJoin()); } else { qDebug() << "Received a bookmark to a MUC " << jid << " which is already booked by another bookmark, skipping"; } @@ -1133,6 +1121,33 @@ void Core::Account::removeRoomRequest(const QString& jid) storeConferences(); } -void Core::Account::addRoomRequest(const QString& jid, const QString& nick, bool autoJoin) +void Core::Account::addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin) { + std::map::const_iterator cItr = conferences.find(jid); + if (cItr == conferences.end()) { + addNewRoom(jid, nick, "", autoJoin); + storeConferences(); + } else { + qDebug() << "An attempt to add a MUC " << jid << " which is already present in the rester, skipping"; + } +} + +void Core::Account::addNewRoom(const QString& jid, const QString& nick, const QString& roomName, bool autoJoin) +{ + QXmppMucRoom* room = mm->addRoom(jid); + QString lNick = nick; + if (lNick.size() == 0) { + lNick = getName(); + } + Conference* conf = new Conference(jid, getName(), autoJoin, roomName, lNick, room); + conferences.insert(std::make_pair(jid, conf)); + + handleNewConference(conf); + + emit addRoom(jid, { + {"autoJoin", conf->getAutoJoin()}, + {"joined", conf->getJoined()}, + {"nick", conf->getNick()}, + {"name", conf->getName()} + }); } diff --git a/core/account.h b/core/account.h index da9033a..2dda75c 100644 --- a/core/account.h +++ b/core/account.h @@ -74,7 +74,7 @@ public: void setRoomJoined(const QString& jid, bool joined); void setRoomAutoJoin(const QString& jid, bool joined); void removeRoomRequest(const QString& jid); - void addRoomRequest(const QString& jid, const QString& nick, bool autoJoin); + void addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin); signals: void connectionStateChanged(int); @@ -164,6 +164,7 @@ private: void handleNewConference(Conference* contact); bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false); bool handleGroupMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false); + void addNewRoom(const QString& jid, const QString& nick, const QString& roomName, bool autoJoin); void addToGroup(const QString& jid, const QString& group); void removeFromGroup(const QString& jid, const QString& group); void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const; diff --git a/core/squawk.cpp b/core/squawk.cpp index ca76cc7..66b7101 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -474,3 +474,14 @@ void Core::Squawk::removeRoomRequest(const QString& account, const QString& jid) } itr->second->removeRoomRequest(jid); } + +void Core::Squawk::addRoomRequest(const QString& account, const QString& jid, const QString& nick, const QString& password, bool autoJoin) +{ + AccountsMap::const_iterator itr = amap.find(account); + if (itr == amap.end()) { + qDebug() << "An attempt to add the room" << jid << "to non existing account" << account << ", skipping"; + return; + } + itr->second->addRoomRequest(jid, nick, password, autoJoin); +} + diff --git a/core/squawk.h b/core/squawk.h index 42a6ab9..cdf4d84 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -79,6 +79,7 @@ public slots: void addContactRequest(const QString& account, const QString& jid, const QString& name, const QSet& groups); void setRoomJoined(const QString& account, const QString& jid, bool joined); void setRoomAutoJoin(const QString& account, const QString& jid, bool joined); + void addRoomRequest(const QString& account, const QString& jid, const QString& nick, const QString& password, bool autoJoin); void removeRoomRequest(const QString& account, const QString& jid); private: diff --git a/main.cpp b/main.cpp index 5247d39..b627ab8 100644 --- a/main.cpp +++ b/main.cpp @@ -85,6 +85,8 @@ int main(int argc, char *argv[]) QObject::connect(&w, SIGNAL(removeRoomRequest(const QString&, const QString&)), squawk, SLOT(removeRoomRequest(const QString&, const QString&))); + QObject::connect(&w, SIGNAL(addRoomRequest(const QString&, const QString&, const QString&, const QString&, bool)), + squawk, SLOT(addRoomRequest(const QString&, const QString&, const QString&, const QString&, bool))); QObject::connect(squawk, SIGNAL(newAccount(const QMap&)), &w, SLOT(newAccount(const QMap&))); QObject::connect(squawk, SIGNAL(addContact(const QString&, const QString&, const QString&, const QMap&)), diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index 65bdec5..3039215 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -12,8 +12,6 @@ find_package(Qt5DBus CONFIG REQUIRED) set(squawkUI_SRC squawk.cpp - accounts.cpp - account.cpp models/accounts.cpp models/roster.cpp models/item.cpp @@ -28,7 +26,10 @@ set(squawkUI_SRC widgets/chat.cpp widgets/room.cpp widgets/messageline.cpp - newcontact.cpp + widgets/newcontact.cpp + widgets/accounts.cpp + widgets/account.cpp + widgets/joinconference.cpp ) # Tell CMake to create the helloworld executable diff --git a/ui/models/accounts.cpp b/ui/models/accounts.cpp index 75626c4..6a62376 100644 --- a/ui/models/accounts.cpp +++ b/ui/models/accounts.cpp @@ -90,15 +90,17 @@ void Models::Accounts::addAccount(Account* account) void Models::Accounts::onAccountChanged(Item* item, int row, int col) { - Account* acc = getAccount(row); - if (item != acc) { - return; //it means the signal is emitted by one of accounts' children, not exactly him, this model has no interest in that + if (row < accs.size()) { + Account* acc = getAccount(row); + if (item != acc) { + return; //it means the signal is emitted by one of accounts' children, not exactly him, this model has no interest in that + } + + if (col < columnCount(QModelIndex())) { + emit dataChanged(createIndex(row, col, this), createIndex(row, col, this)); + } + emit changed(); } - - if (col < columnCount(QModelIndex())) { - emit dataChanged(createIndex(row, col, this), createIndex(row, col, this)); - } - emit changed(); } Models::Account * Models::Accounts::getAccount(int index) diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 78ef7c1..70b03d5 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -153,7 +153,7 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const if (mc > 0) { str += QString("New messages: ") + std::to_string(mc).c_str() + "\n"; } - str += "Jabber ID: " + contact->getJid(); + str += "Jabber ID: " + contact->getJid() + "\n"; Shared::SubscriptionState ss = contact->getState(); if (ss == Shared::both) { Shared::Availability av = contact->getAvailability(); diff --git a/ui/squawk.cpp b/ui/squawk.cpp index dae3a97..63820e9 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -42,6 +42,7 @@ Squawk::Squawk(QWidget *parent) : connect(m_ui->actionAccounts, SIGNAL(triggered()), this, SLOT(onAccounts())); connect(m_ui->actionAddContact, SIGNAL(triggered()), this, SLOT(onNewContact())); + connect(m_ui->actionAddConference, SIGNAL(triggered()), this, SLOT(onNewConference())); connect(m_ui->comboBox, SIGNAL(activated(int)), this, SLOT(onComboboxActivated(int))); connect(m_ui->roster, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(onRosterItemDoubleClicked(const QModelIndex&))); connect(m_ui->roster, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(onRosterContextMenu(const QPoint&))); @@ -78,8 +79,10 @@ void Squawk::onAccountsSizeChanged(unsigned int size) { if (size > 0) { m_ui->actionAddContact->setEnabled(true); + m_ui->actionAddConference->setEnabled(true); } else { m_ui->actionAddContact->setEnabled(false); + m_ui->actionAddConference->setEnabled(false); } } @@ -93,6 +96,16 @@ void Squawk::onNewContact() nc->exec(); } +void Squawk::onNewConference() +{ + JoinConference* jc = new JoinConference(rosterModel.accountsModel, this); + + connect(jc, SIGNAL(accepted()), this, SLOT(onJoinConferenceAccepted())); + connect(jc, SIGNAL(rejected()), jc, SLOT(deleteLater())); + + jc->exec(); +} + void Squawk::onNewContactAccepted() { NewContact* nc = static_cast(sender()); @@ -103,6 +116,16 @@ void Squawk::onNewContactAccepted() nc->deleteLater(); } +void Squawk::onJoinConferenceAccepted() +{ + JoinConference* jc = static_cast(sender()); + JoinConference::Data value = jc->value(); + + emit addRoomRequest(value.account, value.jid, value.nick, value.password, value.autoJoin); + + jc->deleteLater(); +} + void Squawk::closeEvent(QCloseEvent* event) { if (accounts != 0) { diff --git a/ui/squawk.h b/ui/squawk.h index 7ad372d..a36caf5 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -27,11 +27,12 @@ #include #include -#include "accounts.h" +#include "widgets/accounts.h" #include "widgets/chat.h" #include "widgets/room.h" +#include "widgets/newcontact.h" +#include "widgets/joinconference.h" #include "models/roster.h" -#include "newcontact.h" #include "../global.h" @@ -62,6 +63,7 @@ signals: void addContactRequest(const QString& account, const QString& jid, const QString& name, const QSet& groups); void setRoomJoined(const QString& account, const QString& jid, bool joined); void setRoomAutoJoin(const QString& account, const QString& jid, bool joined); + void addRoomRequest(const QString& account, const QString& jid, const QString& nick, const QString& password, bool autoJoin); void removeRoomRequest(const QString& account, const QString& jid); public slots: @@ -103,7 +105,9 @@ protected: private slots: void onAccounts(); void onNewContact(); + void onNewConference(); void onNewContactAccepted(); + void onJoinConferenceAccepted(); void onAccountsSizeChanged(unsigned int size); void onAccountsClosed(QObject* parent = 0); void onConversationClosed(QObject* parent = 0); diff --git a/ui/squawk.ui b/ui/squawk.ui index a137729..642ded2 100644 --- a/ui/squawk.ui +++ b/ui/squawk.ui @@ -78,9 +78,10 @@ - File + Squawk + @@ -109,12 +110,24 @@ false - + + .. Add contact + + + false + + + + + + Add conference + + diff --git a/ui/account.cpp b/ui/widgets/account.cpp similarity index 100% rename from ui/account.cpp rename to ui/widgets/account.cpp diff --git a/ui/account.h b/ui/widgets/account.h similarity index 100% rename from ui/account.h rename to ui/widgets/account.h diff --git a/ui/account.ui b/ui/widgets/account.ui similarity index 100% rename from ui/account.ui rename to ui/widgets/account.ui diff --git a/ui/accounts.cpp b/ui/widgets/accounts.cpp similarity index 100% rename from ui/accounts.cpp rename to ui/widgets/accounts.cpp diff --git a/ui/accounts.h b/ui/widgets/accounts.h similarity index 98% rename from ui/accounts.h rename to ui/widgets/accounts.h index 4093fd2..31dc9ee 100644 --- a/ui/accounts.h +++ b/ui/widgets/accounts.h @@ -24,7 +24,7 @@ #include #include "account.h" -#include "models/accounts.h" +#include "../models/accounts.h" namespace Ui { diff --git a/ui/accounts.ui b/ui/widgets/accounts.ui similarity index 100% rename from ui/accounts.ui rename to ui/widgets/accounts.ui diff --git a/ui/widgets/joinconference.cpp b/ui/widgets/joinconference.cpp new file mode 100644 index 0000000..648de25 --- /dev/null +++ b/ui/widgets/joinconference.cpp @@ -0,0 +1,79 @@ +/* + * Squawk messenger. + * Copyright (C) 2019 Yury Gubich + * + * 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 . + */ + +#include "joinconference.h" +#include "ui_joinconference.h" + +#include + +JoinConference::JoinConference(const Models::Accounts* accounts, QWidget* parent): + QDialog(parent), + m_ui(new Ui::JoinConference()) +{ + m_ui->setupUi ( this ); + std::deque names = accounts->getNames(); + + for (std::deque::const_iterator i = names.begin(), end = names.end(); i != end; i++) { + m_ui->account->addItem(*i); + } + + m_ui->account->setCurrentIndex(0); +} + +JoinConference::JoinConference(const QString& acc, const Models::Accounts* accounts, QWidget* parent): + QDialog(parent), + m_ui(new Ui::JoinConference()) +{ + m_ui->setupUi ( this ); + std::deque names = accounts->getNames(); + + int index = 0; + bool found = false; + for (std::deque::const_iterator i = names.begin(), end = names.end(); i != end; i++) { + const QString& name = *i; + m_ui->account->addItem(name); + if (!found) { + if (name == acc) { + found = true; + } else { + index++; + } + } + } + + if (!found) { + qDebug() << "Couldn't find a correct account among available accounts creating JoinConference dialog, setting to 0"; + } + + m_ui->account->setCurrentIndex(index); +} + +JoinConference::~JoinConference() +{ +} + +JoinConference::Data JoinConference::value() const +{ + return { + m_ui->jid->text(), + m_ui->nick->text(), + m_ui->account->currentText(), + m_ui->autoJoin->isChecked(), + "" + }; +} diff --git a/ui/widgets/joinconference.h b/ui/widgets/joinconference.h new file mode 100644 index 0000000..bf343fe --- /dev/null +++ b/ui/widgets/joinconference.h @@ -0,0 +1,57 @@ +/* + * Squawk messenger. + * Copyright (C) 2019 Yury Gubich + * + * 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 . + */ + +#ifndef JOINCONFERENCE_H +#define JOINCONFERENCE_H + +#include +#include + +#include "../models/accounts.h" + +namespace Ui +{ +class JoinConference; +} + +/** + * @todo write docs + */ +class JoinConference : public QDialog +{ + Q_OBJECT +public: + struct Data { + QString jid; + QString nick; + QString account; + bool autoJoin; + QString password; + }; + + JoinConference(const Models::Accounts* accounts, QWidget* parent = 0); + JoinConference(const QString& acc, const Models::Accounts* accounts, QWidget* parent = 0); + ~JoinConference(); + + Data value() const; + +private: + QScopedPointer m_ui; +}; + +#endif // JOINCONFERENCE_H diff --git a/ui/widgets/joinconference.ui b/ui/widgets/joinconference.ui new file mode 100644 index 0000000..65eda82 --- /dev/null +++ b/ui/widgets/joinconference.ui @@ -0,0 +1,142 @@ + + + JoinConference + + + + 0 + 0 + 324 + 189 + + + + Join new conference + + + + + + + + JID + + + + + + + Room JID + + + identifier@conference.server.org + + + + + + + Account + + + + + + + + + + Join on login + + + + + + + If checked Squawk will try to join this conference on login + + + + + + + + + + Nick name + + + + + + + Your nick name for that conference. If you leave this field empty your account name will be used as a nick name + + + John + + + + + + + + + Qt::Vertical + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + JoinConference + accept() + + + 20 + 20 + + + 20 + 20 + + + + + buttonBox + rejected() + JoinConference + reject() + + + 20 + 20 + + + 20 + 20 + + + + + diff --git a/ui/newcontact.cpp b/ui/widgets/newcontact.cpp similarity index 100% rename from ui/newcontact.cpp rename to ui/widgets/newcontact.cpp diff --git a/ui/newcontact.h b/ui/widgets/newcontact.h similarity index 97% rename from ui/newcontact.h rename to ui/widgets/newcontact.h index eeacaae..79a2abf 100644 --- a/ui/newcontact.h +++ b/ui/widgets/newcontact.h @@ -23,7 +23,7 @@ #include #include -#include "models/accounts.h" +#include "../models/accounts.h" namespace Ui { diff --git a/ui/newcontact.ui b/ui/widgets/newcontact.ui similarity index 98% rename from ui/newcontact.ui rename to ui/widgets/newcontact.ui index 4a08801..f92ed8d 100644 --- a/ui/newcontact.ui +++ b/ui/widgets/newcontact.ui @@ -11,7 +11,7 @@ - NewContact + Add new contact