hopefully end of refactoring of vcard to Info widget

This commit is contained in:
Blue 2023-02-20 21:12:32 +03:00
parent bf11d8a74e
commit e4a2728ef8
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
20 changed files with 268 additions and 1717 deletions

View file

@ -29,7 +29,7 @@ Squawk::Squawk(Models::Roster& p_rosterModel, QWidget *parent) :
about(nullptr),
rosterModel(p_rosterModel),
contextMenu(new QMenu()),
vCards(),
infoWidgets(),
currentConversation(nullptr),
restoreSelection(),
needToRestore(false)
@ -96,8 +96,7 @@ Squawk::~Squawk() {
delete contextMenu;
}
void Squawk::onAccounts()
{
void Squawk::onAccounts() {
if (accounts == nullptr) {
accounts = new Accounts(rosterModel.accountsModel);
accounts->setAttribute(Qt::WA_DeleteOnClose);
@ -116,8 +115,7 @@ void Squawk::onAccounts()
}
}
void Squawk::onPreferences()
{
void Squawk::onPreferences() {
if (preferences == nullptr) {
preferences = new Settings();
preferences->setAttribute(Qt::WA_DeleteOnClose);
@ -133,8 +131,7 @@ void Squawk::onPreferences()
}
}
void Squawk::onAccountsChanged()
{
void Squawk::onAccountsChanged() {
unsigned int size = rosterModel.accountsModel->activeSize();
if (size > 0) {
m_ui->actionAddContact->setEnabled(true);
@ -145,8 +142,7 @@ void Squawk::onAccountsChanged()
}
}
void Squawk::onNewContact()
{
void Squawk::onNewContact() {
NewContact* nc = new NewContact(rosterModel.accountsModel, this);
connect(nc, &NewContact::accepted, this, &Squawk::onNewContactAccepted);
@ -155,8 +151,7 @@ void Squawk::onNewContact()
nc->exec();
}
void Squawk::onNewConference()
{
void Squawk::onNewConference() {
JoinConference* jc = new JoinConference(rosterModel.accountsModel, this);
connect(jc, &JoinConference::accepted, this, &Squawk::onJoinConferenceAccepted);
@ -165,8 +160,7 @@ void Squawk::onNewConference()
jc->exec();
}
void Squawk::onNewContactAccepted()
{
void Squawk::onNewContactAccepted() {
NewContact* nc = static_cast<NewContact*>(sender());
NewContact::Data value = nc->value();
@ -175,8 +169,7 @@ void Squawk::onNewContactAccepted()
nc->deleteLater();
}
void Squawk::onJoinConferenceAccepted()
{
void Squawk::onJoinConferenceAccepted() {
JoinConference* jc = static_cast<JoinConference*>(sender());
JoinConference::Data value = jc->value();
@ -185,23 +178,20 @@ void Squawk::onJoinConferenceAccepted()
jc->deleteLater();
}
void Squawk::closeEvent(QCloseEvent* event)
{
if (accounts != nullptr) {
void Squawk::closeEvent(QCloseEvent* event) {
if (accounts != nullptr)
accounts->close();
}
if (preferences != nullptr) {
if (preferences != nullptr)
preferences->close();
}
if (about != nullptr) {
if (about != nullptr)
about->close();
}
for (std::map<QString, VCard*>::const_iterator itr = vCards.begin(), end = vCards.end(); itr != end; ++itr) {
disconnect(itr->second, &VCard::destroyed, this, &Squawk::onVCardClosed);
itr->second->close();
for (const std::pair<const QString, UI::Info*>& pair : infoWidgets) {
disconnect(pair.second, &UI::Info::destroyed, this, &Squawk::onInfoClosed);
pair.second->close();
}
vCards.clear();
infoWidgets.clear();
writeSettings();
emit closing();;
@ -217,8 +207,7 @@ void Squawk::onPreferencesClosed() {
void Squawk::onAboutSquawkClosed() {
about = nullptr;}
void Squawk::onComboboxActivated(int index)
{
void Squawk::onComboboxActivated(int index) {
Shared::Availability av = Shared::Global::fromInt<Shared::Availability>(index);
emit changeState(av);
}
@ -229,8 +218,7 @@ void Squawk::expand(const QModelIndex& index) {
void Squawk::stateChanged(Shared::Availability state) {
m_ui->comboBox->setCurrentIndex(static_cast<int>(state));}
void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
{
void Squawk::onRosterItemDoubleClicked(const QModelIndex& item) {
if (item.isValid()) {
Models::Item* node = static_cast<Models::Item*>(item.internalPointer());
if (node->type == Models::Item::reference) {
@ -258,8 +246,7 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item)
}
}
void Squawk::closeCurrentConversation()
{
void Squawk::closeCurrentConversation() {
if (currentConversation != nullptr) {
currentConversation->deleteLater();
currentConversation = nullptr;
@ -267,8 +254,7 @@ void Squawk::closeCurrentConversation()
}
}
void Squawk::onRosterContextMenu(const QPoint& point)
{
void Squawk::onRosterContextMenu(const QPoint& point) {
QModelIndex index = m_ui->roster->indexAt(point);
if (index.isValid()) {
Models::Item* item = static_cast<Models::Item*>(index.internalPointer());
@ -292,9 +278,9 @@ void Squawk::onRosterContextMenu(const QPoint& point)
connect(con, &QAction::triggered, std::bind(&Squawk::connectAccount, this, name));
}
QAction* card = contextMenu->addAction(Shared::icon("user-properties"), tr("VCard"));
card->setEnabled(active);
connect(card, &QAction::triggered, std::bind(&Squawk::onActivateVCard, this, name, acc->getBareJid(), true));
QAction* info = contextMenu->addAction(Shared::icon("user-properties"), tr("Info"));
info->setEnabled(active);
connect(info, &QAction::triggered, std::bind(&Squawk::onActivateInfo, this, name, acc->getBareJid()));
QAction* remove = contextMenu->addAction(Shared::icon("edit-delete"), tr("Remove"));
connect(remove, &QAction::triggered, std::bind(&Squawk::removeAccountRequest, this, name));
@ -379,9 +365,9 @@ void Squawk::onRosterContextMenu(const QPoint& point)
});
QAction* card = contextMenu->addAction(Shared::icon("user-properties"), tr("VCard"));
card->setEnabled(active);
connect(card, &QAction::triggered, std::bind(&Squawk::onActivateVCard, this, id.account, id.name, false));
QAction* info = contextMenu->addAction(Shared::icon("user-properties"), tr("Info"));
info->setEnabled(active);
connect(info, &QAction::triggered, std::bind(&Squawk::onActivateInfo, this, id.account, id.name));
QAction* remove = contextMenu->addAction(Shared::icon("edit-delete"), tr("Remove"));
remove->setEnabled(active);
@ -425,65 +411,61 @@ void Squawk::onRosterContextMenu(const QPoint& point)
}
}
void Squawk::responseVCard(const QString& jid, const Shared::VCard& card)
{
std::map<QString, VCard*>::const_iterator itr = vCards.find(jid);
if (itr != vCards.end()) {
itr->second->setVCard(card);
void Squawk::responseInfo(const Shared::Info& info) {
std::map<QString, UI::Info*>::const_iterator itr = infoWidgets.find(info.jid);
if (itr != infoWidgets.end()) {
itr->second->setData(info);
itr->second->hideProgress();
}
}
void Squawk::onVCardClosed()
{
VCard* vCard = static_cast<VCard*>(sender());
void Squawk::onInfoClosed() {
UI::Info* info = static_cast<UI::Info*>(sender());
std::map<QString, VCard*>::const_iterator itr = vCards.find(vCard->getJid());
if (itr == vCards.end()) {
qDebug() << "VCard has been closed but can not be found among other opened vCards, application is most probably going to crash";
std::map<QString, UI::Info*>::const_iterator itr = infoWidgets.find(info->jid);
if (itr == infoWidgets.end()) {
qDebug() << "Info widget has been closed but can not be found among other opened vCards, application is most probably going to crash";
return;
}
vCards.erase(itr);
infoWidgets.erase(itr);
}
void Squawk::onActivateVCard(const QString& account, const QString& jid, bool edition)
{
std::map<QString, VCard*>::const_iterator itr = vCards.find(jid);
VCard* card;
if (itr != vCards.end()) {
card = itr->second;
void Squawk::onActivateInfo(const QString& account, const QString& jid) {
std::map<QString, UI::Info*>::const_iterator itr = infoWidgets.find(jid);
UI::Info* info;
if (itr != infoWidgets.end()) {
info = itr->second;
} else {
card = new VCard(jid, edition);
if (edition) {
card->setWindowTitle(tr("%1 account card").arg(account));
} else {
card->setWindowTitle(tr("%1 contact card").arg(jid));
}
card->setAttribute(Qt::WA_DeleteOnClose);
vCards.insert(std::make_pair(jid, card));
info = new UI::Info(jid);
// TODO need to handle it somewhere else
// if (edition) {
// card->setWindowTitle(tr("%1 account card").arg(account));
// } else {
// card->setWindowTitle(tr("%1 contact card").arg(jid));
// }
info->setAttribute(Qt::WA_DeleteOnClose);
infoWidgets.insert(std::make_pair(jid, info));
connect(card, &VCard::destroyed, this, &Squawk::onVCardClosed);
connect(card, &VCard::saveVCard, std::bind( &Squawk::onVCardSave, this, std::placeholders::_1, account));
connect(info, &UI::Info::destroyed, this, &Squawk::onInfoClosed);
connect(info, &UI::Info::saveInfo, std::bind(&Squawk::onInfoSave, this, std::placeholders::_1, account));
}
card->show();
card->raise();
card->activateWindow();
card->showProgress(tr("Downloading vCard"));
info->show();
info->raise();
info->activateWindow();
info->showProgress();
emit requestVCard(account, jid);
emit requestInfo(account, jid);
}
void Squawk::onVCardSave(const Shared::VCard& card, const QString& account)
{
VCard* widget = static_cast<VCard*>(sender());
emit uploadVCard(account, card);
void Squawk::onInfoSave(const Shared::Info& info, const QString& account) {
UI::Info* widget = static_cast<UI::Info*>(sender());
emit updateInfo(account, info);
widget->deleteLater();
}
void Squawk::writeSettings()
{
void Squawk::writeSettings() {
QSettings settings;
settings.beginGroup("ui");
settings.beginGroup("window");
@ -495,8 +477,7 @@ void Squawk::writeSettings()
settings.endGroup();
}
void Squawk::onRosterSelectionChanged(const QModelIndex& current, const QModelIndex& previous)
{
void Squawk::onRosterSelectionChanged(const QModelIndex& current, const QModelIndex& previous) {
if (restoreSelection.isValid() && restoreSelection == current) {
restoreSelection = QModelIndex();
return;
@ -504,9 +485,9 @@ void Squawk::onRosterSelectionChanged(const QModelIndex& current, const QModelIn
if (current.isValid()) {
Models::Item* node = static_cast<Models::Item*>(current.internalPointer());
if (node->type == Models::Item::reference) {
if (node->type == Models::Item::reference)
node = static_cast<Models::Reference*>(node)->dereference();
}
Models::Contact* contact = nullptr;
Models::Room* room = nullptr;
QString res;
@ -550,9 +531,9 @@ void Squawk::onRosterSelectionChanged(const QModelIndex& current, const QModelIn
if (id != nullptr) {
if (currentConversation != nullptr) {
if (currentConversation->getId() == *id) {
if (contact != nullptr) {
if (contact != nullptr)
currentConversation->setPalResource(res);
}
return;
} else {
currentConversation->deleteLater();
@ -588,16 +569,14 @@ void Squawk::onRosterSelectionChanged(const QModelIndex& current, const QModelIn
}
}
void Squawk::onContextAboutToHide()
{
void Squawk::onContextAboutToHide() {
if (needToRestore) {
needToRestore = false;
m_ui->roster->selectionModel()->setCurrentIndex(restoreSelection, QItemSelectionModel::ClearAndSelect);
}
}
void Squawk::onAboutSquawkCalled()
{
void Squawk::onAboutSquawkCalled() {
if (about == nullptr) {
about = new About();
about->setAttribute(Qt::WA_DeleteOnClose);
@ -609,17 +588,14 @@ void Squawk::onAboutSquawkCalled()
about->show();
}
Models::Roster::ElId Squawk::currentConversationId() const
{
if (currentConversation == nullptr) {
Models::Roster::ElId Squawk::currentConversationId() const {
if (currentConversation == nullptr)
return Models::Roster::ElId();
} else {
else
return Models::Roster::ElId(currentConversation->getAccount(), currentConversation->getJid());
}
}
void Squawk::select(QModelIndex index)
{
void Squawk::select(QModelIndex index) {
m_ui->roster->scrollTo(index, QAbstractItemView::EnsureVisible);
m_ui->roster->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
}