From d3a09cc0dad254894cba5bed2b79b2a4484cc926 Mon Sep 17 00:00:00 2001 From: Petr Bena Date: Sat, 6 Nov 2021 11:13:20 +0100 Subject: [PATCH 1/5] implemented identities --- src/GrumpyIRC/lib/favoriteswin.cpp | 8 +- src/GrumpyIRC/lib/favoriteswin.h | 5 +- src/GrumpyIRC/lib/favoriteswin.ui | 65 +++++++++++++- src/GrumpyIRC/lib/identityeditorwin.cpp | 27 ++++++ src/GrumpyIRC/lib/identityeditorwin.h | 37 ++++++++ src/GrumpyIRC/lib/identityeditorwin.ui | 111 ++++++++++++++++++++++++ src/GrumpyIRC/lib/mainwindow.cpp | 2 +- src/GrumpyIRC/lib/scrollbackframe.cpp | 8 -- src/libcore/identity.cpp | 53 +++++++++++ src/libcore/identity.h | 38 ++++++++ src/libcore/query.h | 3 + 11 files changed, 333 insertions(+), 24 deletions(-) create mode 100644 src/GrumpyIRC/lib/identityeditorwin.cpp create mode 100644 src/GrumpyIRC/lib/identityeditorwin.h create mode 100644 src/GrumpyIRC/lib/identityeditorwin.ui create mode 100644 src/libcore/identity.cpp create mode 100644 src/libcore/identity.h diff --git a/src/GrumpyIRC/lib/favoriteswin.cpp b/src/GrumpyIRC/lib/favoriteswin.cpp index 1e80794..e9ead5c 100644 --- a/src/GrumpyIRC/lib/favoriteswin.cpp +++ b/src/GrumpyIRC/lib/favoriteswin.cpp @@ -8,7 +8,7 @@ //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Lesser General Public License for more details. -// Copyright (c) Petr Bena 2015 - 2018 +// Copyright (c) Petr Bena 2015 - 2021 #include "favoriteswin.h" #include "ui_favoriteswin.h" @@ -18,8 +18,6 @@ using namespace GrumpyIRC; FavoritesWin::FavoritesWin(QWidget *parent) : QDialog(parent), ui(new Ui::FavoritesWin) { this->ui->setupUi(this); - this->ui->comboBox->addItem("Local"); - this->ui->comboBox->setCurrentIndex(0); } FavoritesWin::~FavoritesWin() @@ -27,7 +25,3 @@ FavoritesWin::~FavoritesWin() delete this->ui; } -void GrumpyIRC::FavoritesWin::on_tableWidget_customContextMenuRequested(const QPoint &pos) -{ - -} diff --git a/src/GrumpyIRC/lib/favoriteswin.h b/src/GrumpyIRC/lib/favoriteswin.h index ceb3e01..c25af53 100644 --- a/src/GrumpyIRC/lib/favoriteswin.h +++ b/src/GrumpyIRC/lib/favoriteswin.h @@ -8,7 +8,7 @@ //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Lesser General Public License for more details. -// Copyright (c) Petr Bena 2015 - 2018 +// Copyright (c) Petr Bena 2015 - 2021 #ifndef FAVORITESWIN_H #define FAVORITESWIN_H @@ -31,9 +31,6 @@ namespace GrumpyIRC explicit FavoritesWin(QWidget *parent = 0); ~FavoritesWin(); - private slots: - void on_tableWidget_customContextMenuRequested(const QPoint &pos); - private: Ui::FavoritesWin *ui; }; diff --git a/src/GrumpyIRC/lib/favoriteswin.ui b/src/GrumpyIRC/lib/favoriteswin.ui index b73b6c6..0d6776b 100644 --- a/src/GrumpyIRC/lib/favoriteswin.ui +++ b/src/GrumpyIRC/lib/favoriteswin.ui @@ -18,11 +18,68 @@ :/icons/img/grumpy_logo.png:/icons/img/grumpy_logo.png + + 0 + + + 0 + + + 0 + + + 0 + - - - - + + + 0 + + + + Networks + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + Identities + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + diff --git a/src/GrumpyIRC/lib/identityeditorwin.cpp b/src/GrumpyIRC/lib/identityeditorwin.cpp new file mode 100644 index 0000000..680c86c --- /dev/null +++ b/src/GrumpyIRC/lib/identityeditorwin.cpp @@ -0,0 +1,27 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#include "identityeditorwin.h" +#include "ui_identityeditorwin.h" +#include "../../libcore/identity.h" + +using namespace GrumpyIRC; + +IdentityEditorWin::IdentityEditorWin(QWidget *parent) : QDialog(parent), ui(new Ui::IdentityEditorWin) +{ + this->ui->setupUi(this); +} + +IdentityEditorWin::~IdentityEditorWin() +{ + delete this->ui; +} diff --git a/src/GrumpyIRC/lib/identityeditorwin.h b/src/GrumpyIRC/lib/identityeditorwin.h new file mode 100644 index 0000000..6202cba --- /dev/null +++ b/src/GrumpyIRC/lib/identityeditorwin.h @@ -0,0 +1,37 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#ifndef IDENTITYEDITORWIN_H +#define IDENTITYEDITORWIN_H + +#include + +namespace Ui +{ + class IdentityEditorWin; +} + +namespace GrumpyIRC +{ + class IdentityEditorWin : public QDialog + { + Q_OBJECT + public: + explicit IdentityEditorWin(QWidget *parent = nullptr); + ~IdentityEditorWin(); + + private: + Ui::IdentityEditorWin *ui; + }; +} + +#endif // IDENTITYEDITORWIN_H diff --git a/src/GrumpyIRC/lib/identityeditorwin.ui b/src/GrumpyIRC/lib/identityeditorwin.ui new file mode 100644 index 0000000..c4938a8 --- /dev/null +++ b/src/GrumpyIRC/lib/identityeditorwin.ui @@ -0,0 +1,111 @@ + + + IdentityEditorWin + + + + 0 + 0 + 373 + 252 + + + + Identity editor + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + Nick + + + + + + + Ident + + + + + + + Real name + + + + + + + Away message + + + + + + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 81 + + + + + + + + + + Save + + + true + + + + + + + Cancel + + + + + + + + + + diff --git a/src/GrumpyIRC/lib/mainwindow.cpp b/src/GrumpyIRC/lib/mainwindow.cpp index 4cdfb9c..d539698 100644 --- a/src/GrumpyIRC/lib/mainwindow.cpp +++ b/src/GrumpyIRC/lib/mainwindow.cpp @@ -8,7 +8,7 @@ //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Lesser General Public License for more details. -// Copyright (c) Petr Bena 2015 - 2020 +// Copyright (c) Petr Bena 2015 - 2021 #include "aboutwin.h" #include "corewrapper.h" diff --git a/src/GrumpyIRC/lib/scrollbackframe.cpp b/src/GrumpyIRC/lib/scrollbackframe.cpp index f56038b..e47e76c 100644 --- a/src/GrumpyIRC/lib/scrollbackframe.cpp +++ b/src/GrumpyIRC/lib/scrollbackframe.cpp @@ -276,7 +276,6 @@ static QString ItemToTXTString(const ScrollbackItem &item) QString format_string = CONF->GetLineFormat(); QString text = item.GetText(); QString user = item.GetUser().GetNick(); - bool system = false; switch (item.GetType()) { case ScrollbackItemType_Act: @@ -284,14 +283,12 @@ static QString ItemToTXTString(const ScrollbackItem &item) format_string.replace("$string", CONF->GetActionFormat()); break; case ScrollbackItemType_Join: - system = true; format_string.replace("$string", CONF->GetActionFormat()); user = item.GetUser().ToString(); text = "joined channel"; //result = FormatAction(item.GetUser(), "joined channel", true); break; case ScrollbackItemType_Part: - system = true; format_string.replace("$string", CONF->GetActionFormat()); user = item.GetUser().ToString(); if (item.GetText().isEmpty()) @@ -300,22 +297,18 @@ static QString ItemToTXTString(const ScrollbackItem &item) text = "left channel (" + item.GetText() + ")"; break; case ScrollbackItemType_Quit: - system = true; format_string.replace("$string", CONF->GetActionFormat()); user = item.GetUser().ToString(); text = "quit (" + item.GetText() + ")"; break; case ScrollbackItemType_Kick: - system = true; format_string.replace("$string", CONF->GetActionFormat()); break; case ScrollbackItemType_Mode: - system = true; format_string.replace("$string", CONF->GetActionFormat()); text = "set mode " + item.GetText(); break; case ScrollbackItemType_Nick: - system = true; format_string.replace("$string", CONF->GetActionFormat()); text = "changed nick to " + item.GetText(); break; @@ -334,7 +327,6 @@ static QString ItemToTXTString(const ScrollbackItem &item) case ScrollbackItemType_SystemWarning: break; case ScrollbackItemType_Topic: - system = true; format_string.replace("$string", CONF->GetActionFormat()); text = "changed topic to: " + item.GetText(); break; diff --git a/src/libcore/identity.cpp b/src/libcore/identity.cpp new file mode 100644 index 0000000..635c9a1 --- /dev/null +++ b/src/libcore/identity.cpp @@ -0,0 +1,53 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#include "identity.h" + +using namespace GrumpyIRC; + +int Identity::LastID = 0; + +Identity::Identity(QString nick, QString ident, QString real_name, QString away_msg, int id) +{ + if (id < 0) + { + this->ID = Identity::LastID++; + } else + { + this->ID = id; + if (id > Identity::LastID) + Identity::LastID = id + 1; + } + + this->Nick = nick; + this->Ident = ident; + this->RealName = real_name; + this->AwayMessage = away_msg; +} + +QHash Identity::ToHash() +{ + QHash hash; + SERIALIZE(Nick); + SERIALIZE(Ident); + SERIALIZE(RealName); + SERIALIZE(AwayMessage); + return hash; +} + +void Identity::LoadHash(const QHash &hash) +{ + UNSERIALIZE_STRING(Nick); + UNSERIALIZE_STRING(Ident); + UNSERIALIZE_STRING(RealName); + UNSERIALIZE_STRING(AwayMessage); +} diff --git a/src/libcore/identity.h b/src/libcore/identity.h new file mode 100644 index 0000000..4a27b39 --- /dev/null +++ b/src/libcore/identity.h @@ -0,0 +1,38 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#ifndef IDENTITY_H +#define IDENTITY_H + +#include +#include "libcore_global.h" +#include "../libirc/libirc/serializableitem.h" + +namespace GrumpyIRC +{ + class LIBCORESHARED_EXPORT Identity : public libirc::SerializableItem + { + public: + static int LastID; + + Identity(QString nick, QString ident, QString real_name, QString away_msg = "", int id = -1); + QHash ToHash() override; + void LoadHash(const QHash &hash) override; + int ID; + QString Nick; + QString Ident; + QString RealName; + QString AwayMessage; + }; +} + +#endif // IDENTITY_H diff --git a/src/libcore/query.h b/src/libcore/query.h index 8a66f28..239b801 100644 --- a/src/libcore/query.h +++ b/src/libcore/query.h @@ -20,6 +20,9 @@ namespace GrumpyIRC { class ScrollbackItem; + + //! This is unused work in progress + //! query for matching text, used by searching feature which is to be implemented class LIBCORESHARED_EXPORT Query : public libirc::SerializableItem { public: From 4b3ba85c43d0891443bd649abedad72f6004808c Mon Sep 17 00:00:00 2001 From: Petr Bena Date: Mon, 22 Nov 2021 12:58:59 +0100 Subject: [PATCH 2/5] identity editor --- .gitignore | 7 + src/GrumpyIRC/lib/favoriteswin.cpp | 163 ++++++++++++++++++ src/GrumpyIRC/lib/favoriteswin.h | 8 + src/GrumpyIRC/lib/favoriteswin.ui | 33 +++- src/GrumpyIRC/lib/identityeditorwin.cpp | 35 ++++ src/GrumpyIRC/lib/identityeditorwin.h | 7 + src/GrumpyIRC/lib/mainwindow.cpp | 1 - src/GrumpyIRC/lib/mainwindow.ui | 4 +- src/GrumpyIRC/lib/networkwin.cpp | 47 +++++ .../favorite.h => GrumpyIRC/lib/networkwin.h} | 35 ++-- src/GrumpyIRC/lib/networkwin.ui | 103 +++++++++++ src/libcore/favorite.cpp | 21 --- src/libcore/identity.cpp | 19 ++ src/libcore/identity.h | 4 + src/libcore/networkinfo.cpp | 79 +++++++++ src/libcore/networkinfo.h | 46 +++++ 16 files changed, 569 insertions(+), 43 deletions(-) create mode 100644 src/GrumpyIRC/lib/networkwin.cpp rename src/{libcore/favorite.h => GrumpyIRC/lib/networkwin.h} (55%) create mode 100644 src/GrumpyIRC/lib/networkwin.ui delete mode 100644 src/libcore/favorite.cpp create mode 100644 src/libcore/networkinfo.cpp create mode 100644 src/libcore/networkinfo.h diff --git a/.gitignore b/.gitignore index ec8673a..6e86115 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ +# Releases +release/ +macos/package/ +macos/release/ +macos/grumpy.icns +macos/grumpy_logo.png + # C++ objects and libs *.slo *.lo diff --git a/src/GrumpyIRC/lib/favoriteswin.cpp b/src/GrumpyIRC/lib/favoriteswin.cpp index e9ead5c..0dcf7dd 100644 --- a/src/GrumpyIRC/lib/favoriteswin.cpp +++ b/src/GrumpyIRC/lib/favoriteswin.cpp @@ -12,12 +12,34 @@ #include "favoriteswin.h" #include "ui_favoriteswin.h" +#include "networkwin.h" +#include "identityeditorwin.h" +#include "../../libcore/generic.h" +#include "../../libcore/networkinfo.h" +#include "../../libcore/identity.h" +#include using namespace GrumpyIRC; FavoritesWin::FavoritesWin(QWidget *parent) : QDialog(parent), ui(new Ui::FavoritesWin) { this->ui->setupUi(this); + + QStringList heading_1; + heading_1 << "ID" << "Network name" << "Hostname"; + this->ui->tv_NetworkList->verticalHeader()->setVisible(false); + this->ui->tv_NetworkList->setColumnCount(heading_1.size()); + this->ui->tv_NetworkList->setShowGrid(false); + this->ui->tv_NetworkList->setHorizontalHeaderLabels(heading_1); + + QStringList heading_2; + heading_2 << "ID" << "Nick" << "Ident" << "Real Name" << "Auto connect"; + this->ui->tv_IdentityList->verticalHeader()->setVisible(false); + this->ui->tv_IdentityList->setColumnCount(heading_2.size()); + this->ui->tv_IdentityList->setShowGrid(false); + this->ui->tv_IdentityList->setHorizontalHeaderLabels(heading_2); + + this->RefreshIdentities(); } FavoritesWin::~FavoritesWin() @@ -25,3 +47,144 @@ FavoritesWin::~FavoritesWin() delete this->ui; } +void FavoritesWin::RefreshNetworks() +{ + while (this->ui->tv_NetworkList->rowCount()) + this->ui->tv_NetworkList->removeRow(0); + + int row = 0; + QList network_list = NetworkInfo::NetworksInfo.keys(); + qSort(network_list); + + foreach (int id, network_list) + { + NetworkInfo *network = NetworkInfo::NetworksInfo[id]; + this->ui->tv_NetworkList->insertRow(row); + this->ui->tv_NetworkList->setItem(row, 0, new QTableWidgetItem(QString::number(network->ID))); + this->ui->tv_NetworkList->setItem(row, 1, new QTableWidgetItem(network->NetworkName)); + this->ui->tv_NetworkList->setItem(row, 2, new QTableWidgetItem(network->Hostname)); + this->ui->tv_NetworkList->setItem(row, 3, new QTableWidgetItem(Generic::Bool2String(network->AutoReconnect))); + row++; + } +} + +void FavoritesWin::RefreshIdentities() +{ + while(this->ui->tv_IdentityList->rowCount()) + this->ui->tv_IdentityList->removeRow(0); + + int row = 0; + QList identity_list = Identity::Identities.keys(); + qSort(identity_list); + + foreach (int id, identity_list) + { + Identity *identity = Identity::Identities[id]; + this->ui->tv_IdentityList->insertRow(row); + this->ui->tv_IdentityList->setItem(row, 0, new QTableWidgetItem(QString::number(identity->ID))); + this->ui->tv_IdentityList->setItem(row, 1, new QTableWidgetItem(identity->Nick)); + this->ui->tv_IdentityList->setItem(row, 2, new QTableWidgetItem(identity->Ident)); + this->ui->tv_IdentityList->setItem(row, 3, new QTableWidgetItem(identity->RealName)); + row++; + } +} + +void GrumpyIRC::FavoritesWin::on_tv_IdentityList_customContextMenuRequested(const QPoint &pos) +{ + QPoint globalPos = this->ui->tv_IdentityList->viewport()->mapToGlobal(pos); + QMenu Menu; + // Items + QAction *menuInsert = new QAction("Insert", &Menu); + Menu.addAction(menuInsert); + QAction *menuEdit = new QAction("Edit", &Menu); + Menu.addAction(menuEdit); + QAction *menuRemove = new QAction("Remove", &Menu); + Menu.addAction(menuRemove); + + QAction* selectedItem = Menu.exec(globalPos); + if (!selectedItem) + return; + if (selectedItem == menuInsert) + { + IdentityEditorWin iw; + iw.exec(); + this->RefreshIdentities(); + } else if (selectedItem == menuRemove) + { + QList selected = this->selectedIdents(); + foreach (int id, selected) + { + if (Identity::Identities.contains(id)) + { + delete Identity::Identities[id]; + Identity::Identities.remove(id); + } + } + this->RefreshIdentities(); + } +} + +void GrumpyIRC::FavoritesWin::on_tv_NetworkList_customContextMenuRequested(const QPoint &pos) +{ + QPoint globalPos = this->ui->tv_NetworkList->viewport()->mapToGlobal(pos); + QMenu Menu; + // Items + QAction *menuInsert = new QAction("Insert", &Menu); + Menu.addAction(menuInsert); + QAction *menuRemove = new QAction("Remove", &Menu); + Menu.addAction(menuRemove); + + QAction* selectedItem = Menu.exec(globalPos); + if (!selectedItem) + return; + if (selectedItem == menuInsert) + { + NetworkWin nx; + nx.exec(); + this->RefreshNetworks(); + } else if (selectedItem == menuRemove) + { + QList selected = this->selectedNetworks(); + foreach (int id, selected) + { + if (NetworkInfo::NetworksInfo.contains(id)) + { + delete NetworkInfo::NetworksInfo[id]; + NetworkInfo::NetworksInfo.remove(id); + } + } + this->RefreshNetworks(); + } +} + +QList FavoritesWin::selectedNetworks() +{ + QList results; + QList items = this->ui->tv_NetworkList->selectedItems(); + foreach (QTableWidgetItem *xx, items) + { + if (xx->column() > 0) + continue; + int id = xx->text().toInt(); + if (!results.contains(id)) + results.append(id); + } + + return results; +} + +QList FavoritesWin::selectedIdents() +{ + QList results; + QList items = this->ui->tv_IdentityList->selectedItems(); + foreach (QTableWidgetItem *xx, items) + { + if (xx->column() > 0) + continue; + int id = xx->text().toInt(); + if (!results.contains(id)) + results.append(id); + } + + return results; +} diff --git a/src/GrumpyIRC/lib/favoriteswin.h b/src/GrumpyIRC/lib/favoriteswin.h index c25af53..f66d232 100644 --- a/src/GrumpyIRC/lib/favoriteswin.h +++ b/src/GrumpyIRC/lib/favoriteswin.h @@ -30,8 +30,16 @@ namespace GrumpyIRC public: explicit FavoritesWin(QWidget *parent = 0); ~FavoritesWin(); + void RefreshNetworks(); + void RefreshIdentities(); + + private slots: + void on_tv_IdentityList_customContextMenuRequested(const QPoint &pos); + void on_tv_NetworkList_customContextMenuRequested(const QPoint &pos); private: + QList selectedNetworks(); + QList selectedIdents(); Ui::FavoritesWin *ui; }; } diff --git a/src/GrumpyIRC/lib/favoriteswin.ui b/src/GrumpyIRC/lib/favoriteswin.ui index 0d6776b..a1f4067 100644 --- a/src/GrumpyIRC/lib/favoriteswin.ui +++ b/src/GrumpyIRC/lib/favoriteswin.ui @@ -53,7 +53,26 @@ 0 - + + + Qt::CustomContextMenu + + + QAbstractItemView::NoEditTriggers + + + false + + + false + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + @@ -75,7 +94,17 @@ 0 - + + + Qt::CustomContextMenu + + + QAbstractItemView::NoEditTriggers + + + QAbstractItemView::SingleSelection + + diff --git a/src/GrumpyIRC/lib/identityeditorwin.cpp b/src/GrumpyIRC/lib/identityeditorwin.cpp index 680c86c..705ad13 100644 --- a/src/GrumpyIRC/lib/identityeditorwin.cpp +++ b/src/GrumpyIRC/lib/identityeditorwin.cpp @@ -25,3 +25,38 @@ IdentityEditorWin::~IdentityEditorWin() { delete this->ui; } + +void IdentityEditorWin::Load(int id) +{ + if (!Identity::Identities.contains(id)) + return; + + Identity *x = Identity::Identities[id]; + this->identID = id; + this->ui->le_RealName->setText(x->RealName); + this->ui->le_Nick->setText(x->Nick); + this->ui->le_Ident->setText(x->Ident); + this->ui->le_AwayMsg->setText(x->AwayMessage); +} + +void GrumpyIRC::IdentityEditorWin::on_pbSave_clicked() +{ + if (this->identID < 0) + { + Identity *i = new Identity(this->ui->le_Nick->text(), this->ui->le_Ident->text(), this->ui->le_RealName->text(), this->ui->le_AwayMsg->text()); + Identity::Identities.insert(i->ID, i); + this->close(); + return; + } + Identity *x = Identity::Identities[this->identID]; + x->Nick = this->ui->le_Nick->text(); + x->Ident = this->ui->le_Ident->text(); + x->RealName = this->ui->le_RealName->text(); + x->AwayMessage = this->ui->le_AwayMsg->text(); + this->close(); +} + +void GrumpyIRC::IdentityEditorWin::on_pbCancel_clicked() +{ + this->close(); +} diff --git a/src/GrumpyIRC/lib/identityeditorwin.h b/src/GrumpyIRC/lib/identityeditorwin.h index 6202cba..16fa94e 100644 --- a/src/GrumpyIRC/lib/identityeditorwin.h +++ b/src/GrumpyIRC/lib/identityeditorwin.h @@ -28,8 +28,15 @@ namespace GrumpyIRC public: explicit IdentityEditorWin(QWidget *parent = nullptr); ~IdentityEditorWin(); + void Load(int id); + + private slots: + void on_pbSave_clicked(); + + void on_pbCancel_clicked(); private: + int identID = -1; Ui::IdentityEditorWin *ui; }; } diff --git a/src/GrumpyIRC/lib/mainwindow.cpp b/src/GrumpyIRC/lib/mainwindow.cpp index d539698..12e6837 100644 --- a/src/GrumpyIRC/lib/mainwindow.cpp +++ b/src/GrumpyIRC/lib/mainwindow.cpp @@ -180,7 +180,6 @@ MainWindow::MainWindow(bool fork, MainWindow *parent) // Try to restore geometry this->restoreGeometry(GCFG->GetValue("mainwindow_geometry").toByteArray()); this->restoreState(GCFG->GetValue("mainwindow_state").toByteArray()); - this->ui->actionFavorites->setVisible(false); } MainWindow::~MainWindow() diff --git a/src/GrumpyIRC/lib/mainwindow.ui b/src/GrumpyIRC/lib/mainwindow.ui index 2c39a6f..cbc186f 100644 --- a/src/GrumpyIRC/lib/mainwindow.ui +++ b/src/GrumpyIRC/lib/mainwindow.ui @@ -35,9 +35,9 @@ + - @@ -134,7 +134,7 @@ &Favorites - false + true diff --git a/src/GrumpyIRC/lib/networkwin.cpp b/src/GrumpyIRC/lib/networkwin.cpp new file mode 100644 index 0000000..c8e2806 --- /dev/null +++ b/src/GrumpyIRC/lib/networkwin.cpp @@ -0,0 +1,47 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#include "../../libcore/networkinfo.h" +#include "networkwin.h" +#include "ui_networkwin.h" + +using namespace GrumpyIRC; + +NetworkWin::NetworkWin(QWidget *parent) : QDialog(parent), ui(new Ui::NetworkWin) +{ + this->ui->setupUi(this); +} + +NetworkWin::~NetworkWin() +{ + delete this->ui; +} + +void GrumpyIRC::NetworkWin::on_pushSave_clicked() +{ + if (this->networkID < 0) + { + NetworkInfo *network = new NetworkInfo(this->ui->le_Name->text(), this->ui->le_HostName->text(), this->ui->le_Port->text().toInt(), -1); + NetworkInfo::NetworksInfo.insert(network->ID, network); + this->close(); + return; + } + NetworkInfo *x = NetworkInfo::NetworksInfo[this->networkID]; + x->NetworkName = this->ui->le_Name->text(); + + this->close(); +} + +void GrumpyIRC::NetworkWin::on_pushExit_clicked() +{ + this->close(); +} diff --git a/src/libcore/favorite.h b/src/GrumpyIRC/lib/networkwin.h similarity index 55% rename from src/libcore/favorite.h rename to src/GrumpyIRC/lib/networkwin.h index b1ce559..7d143b8 100644 --- a/src/libcore/favorite.h +++ b/src/GrumpyIRC/lib/networkwin.h @@ -8,34 +8,35 @@ //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Lesser General Public License for more details. -// Copyright (c) Petr Bena 2015 +// Copyright (c) Petr Bena 2021 -#ifndef FAVORITE_H -#define FAVORITE_H +#ifndef NETWORKWIN_H +#define NETWORKWIN_H -#include "definitions.h" -#include "../libirc/libirc/serializableitem.h" -#include "libcore_global.h" +#include -#include -#include +namespace Ui { + class NetworkWin; +} namespace GrumpyIRC { - class LIBCORESHARED_EXPORT Favorite : public libirc::SerializableItem + class NetworkWin : public QDialog { + Q_OBJECT + public: - Favorite(); + explicit NetworkWin(QWidget *parent = nullptr); + ~NetworkWin(); + private slots: + void on_pushSave_clicked(); + void on_pushExit_clicked(); private: - QString name; - QString host; - QString nick; - QString username; - QString password; - + Ui::NetworkWin *ui; + int networkID = -1; }; } -#endif // FAVORITE_H +#endif // NETWORKWIN_H diff --git a/src/GrumpyIRC/lib/networkwin.ui b/src/GrumpyIRC/lib/networkwin.ui new file mode 100644 index 0000000..407d86b --- /dev/null +++ b/src/GrumpyIRC/lib/networkwin.ui @@ -0,0 +1,103 @@ + + + NetworkWin + + + + 0 + 0 + 470 + 192 + + + + Dialog + + + + + + + + Name + + + + + + + + + + Hostname + + + + + + + Port + + + + + + + Identity + + + + + + + + + + 6667 + + + + + + + + + + + + + + Save + + + true + + + + + + + Cancel + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + diff --git a/src/libcore/favorite.cpp b/src/libcore/favorite.cpp deleted file mode 100644 index 5e8ef6c..0000000 --- a/src/libcore/favorite.cpp +++ /dev/null @@ -1,21 +0,0 @@ -//This program is free software: you can redistribute it and/or modify -//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. - -// Copyright (c) Petr Bena 2015 - -#include "favorite.h" - -using namespace GrumpyIRC; - -Favorite::Favorite() -{ - -} - diff --git a/src/libcore/identity.cpp b/src/libcore/identity.cpp index 635c9a1..c9d5cc2 100644 --- a/src/libcore/identity.cpp +++ b/src/libcore/identity.cpp @@ -15,6 +15,18 @@ using namespace GrumpyIRC; int Identity::LastID = 0; +QHash Identity::Identities; + +void Identity::Clear() +{ + QList list = Identity::Identities.values(); + foreach (Identity *identity, list) + { + delete identity; + } + Identity::Identities.clear(); + Identity::LastID = 0; +} Identity::Identity(QString nick, QString ident, QString real_name, QString away_msg, int id) { @@ -51,3 +63,10 @@ void Identity::LoadHash(const QHash &hash) UNSERIALIZE_STRING(RealName); UNSERIALIZE_STRING(AwayMessage); } + +void Identity::ToString() +{ + QString result = this->Nick; + if (!this->Ident.isEmpty()) + result += "@" + this->Ident; +} diff --git a/src/libcore/identity.h b/src/libcore/identity.h index 4a27b39..d771212 100644 --- a/src/libcore/identity.h +++ b/src/libcore/identity.h @@ -14,6 +14,7 @@ #define IDENTITY_H #include +#include #include "libcore_global.h" #include "../libirc/libirc/serializableitem.h" @@ -22,11 +23,14 @@ namespace GrumpyIRC class LIBCORESHARED_EXPORT Identity : public libirc::SerializableItem { public: + static void Clear(); static int LastID; + static QHash Identities; Identity(QString nick, QString ident, QString real_name, QString away_msg = "", int id = -1); QHash ToHash() override; void LoadHash(const QHash &hash) override; + void ToString(); int ID; QString Nick; QString Ident; diff --git a/src/libcore/networkinfo.cpp b/src/libcore/networkinfo.cpp new file mode 100644 index 0000000..c5f278a --- /dev/null +++ b/src/libcore/networkinfo.cpp @@ -0,0 +1,79 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#include "networkinfo.h" + +using namespace GrumpyIRC; + +QHash NetworkInfo::NetworksInfo; +int NetworkInfo::LastID = 0; + +void NetworkInfo::Clear() +{ + QList list = NetworkInfo::NetworksInfo.values(); + foreach (NetworkInfo *network_info, list) + { + delete network_info; + } + NetworkInfo::NetworksInfo.clear(); + NetworkInfo::LastID = 0; +} + +NetworkInfo::NetworkInfo() +{ + +} + +NetworkInfo::NetworkInfo(QString name, QString host, int port, int identity, int id) +{ + if (id < 0) + { + this->ID = NetworkInfo::LastID++; + } else + { + this->ID = id; + if (id > NetworkInfo::LastID) + NetworkInfo::LastID = id + 1; + } + + this->NetworkName = name; + this->Hostname = host; + this->Port = port; + this->PreferredIdentity = identity; +} + +NetworkInfo::NetworkInfo(const QHash &hash) +{ + this->LoadHash(hash); +} + +QHash NetworkInfo::ToHash() +{ + QHash hash; + SERIALIZE(NetworkName); + SERIALIZE(Hostname); + SERIALIZE(PreferredIdentity); + SERIALIZE(SSL); + SERIALIZE(AutoReconnect); + SERIALIZE(Port); + return hash; +} + +void NetworkInfo::LoadHash(const QHash &hash) +{ + UNSERIALIZE_STRING(NetworkName); + UNSERIALIZE_STRING(Hostname); + UNSERIALIZE_INT(PreferredIdentity); + UNSERIALIZE_BOOL(SSL); + UNSERIALIZE_INT(Port); + UNSERIALIZE_BOOL(AutoReconnect); +} diff --git a/src/libcore/networkinfo.h b/src/libcore/networkinfo.h new file mode 100644 index 0000000..9e03e7a --- /dev/null +++ b/src/libcore/networkinfo.h @@ -0,0 +1,46 @@ +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + +// Copyright (c) Petr Bena 2021 + +#ifndef NETWORKINFO_H +#define NETWORKINFO_H + +#include +#include +#include "libcore_global.h" +#include "../libirc/libirc/serializableitem.h" +#include "../libirc/libirc/irc_standards.h" + +namespace GrumpyIRC +{ + class NetworkInfo : public libirc::SerializableItem + { + public: + static void Clear(); + static int LastID; + static QHash NetworksInfo; + + NetworkInfo(); + NetworkInfo(QString name, QString host, int port, int identity, int id = -1); + NetworkInfo(const QHash &hash); + QHash ToHash() override; + void LoadHash(const QHash &hash) override; + int ID; + QString NetworkName; + QString Hostname; + int Port = IRC_STANDARD_PORT; + int PreferredIdentity = -1; + bool AutoReconnect = false; + bool SSL = false; + }; +} + +#endif // NETWORKINFO_H From 65e0f5dd7f2a05a6826c41b28e9a7c52555279a4 Mon Sep 17 00:00:00 2001 From: Petr Bena Date: Wed, 24 Nov 2021 13:28:50 +0100 Subject: [PATCH 3/5] implemented saving and loading --- src/GrumpyIRC/lib/favoriteswin.cpp | 65 ++++++++++++++++++++++++++++-- src/GrumpyIRC/lib/favoriteswin.h | 8 ++++ src/GrumpyIRC/lib/favoriteswin.ui | 4 +- src/GrumpyIRC/lib/grumpyconf.cpp | 20 +++++++++ src/GrumpyIRC/lib/grumpyconf.h | 4 ++ src/GrumpyIRC/lib/mainwindow.cpp | 13 +++--- src/libcore/configuration.cpp | 8 ++++ src/libcore/configuration.h | 19 +++++---- src/libcore/identity.cpp | 7 ++++ src/libcore/identity.h | 1 + src/libcore/networkinfo.cpp | 9 ++--- src/libcore/networkinfo.h | 1 - 12 files changed, 135 insertions(+), 24 deletions(-) diff --git a/src/GrumpyIRC/lib/favoriteswin.cpp b/src/GrumpyIRC/lib/favoriteswin.cpp index 0dcf7dd..ad77a67 100644 --- a/src/GrumpyIRC/lib/favoriteswin.cpp +++ b/src/GrumpyIRC/lib/favoriteswin.cpp @@ -11,16 +11,29 @@ // Copyright (c) Petr Bena 2015 - 2021 #include "favoriteswin.h" +#include "grumpyconf.h" #include "ui_favoriteswin.h" #include "networkwin.h" #include "identityeditorwin.h" -#include "../../libcore/generic.h" -#include "../../libcore/networkinfo.h" -#include "../../libcore/identity.h" +#include +#include +#include #include using namespace GrumpyIRC; +void FavoritesWin::Load() +{ + FavoritesWin::loadIdentities(); + FavoritesWin::loadNetworks(); +} + +void FavoritesWin::Save() +{ + FavoritesWin::saveIdentities(); + FavoritesWin::saveNetworks(); +} + FavoritesWin::FavoritesWin(QWidget *parent) : QDialog(parent), ui(new Ui::FavoritesWin) { this->ui->setupUi(this); @@ -32,6 +45,8 @@ FavoritesWin::FavoritesWin(QWidget *parent) : QDialog(parent), ui(new Ui::Favor this->ui->tv_NetworkList->setShowGrid(false); this->ui->tv_NetworkList->setHorizontalHeaderLabels(heading_1); + this->RefreshNetworks(); + QStringList heading_2; heading_2 << "ID" << "Nick" << "Ident" << "Real Name" << "Auto connect"; this->ui->tv_IdentityList->verticalHeader()->setVisible(false); @@ -157,6 +172,50 @@ void GrumpyIRC::FavoritesWin::on_tv_NetworkList_customContextMenuRequested(const } } +void FavoritesWin::saveNetworks() +{ + QList networks = NetworkInfo::NetworksInfo.keys(); + QHash list; + foreach (int id, networks) + { + list.insert(QString::number(id), QVariant(NetworkInfo::NetworksInfo[id]->ToHash())); + } + CONF->SetNetworks(list); +} + +void FavoritesWin::loadNetworks() +{ + QHash nl = CONF->GetNetworks(); + foreach (QString key, nl.keys()) + { + QHash network_data = nl[key].toHash(); + NetworkInfo *network_info = new NetworkInfo(network_data); + NetworkInfo::NetworksInfo.insert(network_info->ID, network_info); + } +} + +void FavoritesWin::saveIdentities() +{ + QList identities = Identity::Identities.keys(); + QHash list; + foreach (int id, identities) + { + list.insert(QString::number(id), QVariant(Identity::Identities[id]->ToHash())); + } + CONF->SetIdentities(list); +} + +void FavoritesWin::loadIdentities() +{ + QHash nl = CONF->GetNetworks(); + foreach (QString key, nl.keys()) + { + QHash identity_hash = nl[key].toHash(); + Identity *identity = new Identity(identity_hash); + Identity::Identities.insert(identity->ID, identity); + } +} + QList FavoritesWin::selectedNetworks() { QList results; diff --git a/src/GrumpyIRC/lib/favoriteswin.h b/src/GrumpyIRC/lib/favoriteswin.h index f66d232..9ee606b 100644 --- a/src/GrumpyIRC/lib/favoriteswin.h +++ b/src/GrumpyIRC/lib/favoriteswin.h @@ -28,6 +28,9 @@ namespace GrumpyIRC Q_OBJECT public: + static void Load(); + static void Save(); + explicit FavoritesWin(QWidget *parent = 0); ~FavoritesWin(); void RefreshNetworks(); @@ -38,6 +41,11 @@ namespace GrumpyIRC void on_tv_NetworkList_customContextMenuRequested(const QPoint &pos); private: + static void saveNetworks(); + static void loadNetworks(); + static void saveIdentities(); + static void loadIdentities(); + QList selectedNetworks(); QList selectedIdents(); Ui::FavoritesWin *ui; diff --git a/src/GrumpyIRC/lib/favoriteswin.ui b/src/GrumpyIRC/lib/favoriteswin.ui index a1f4067..18aa388 100644 --- a/src/GrumpyIRC/lib/favoriteswin.ui +++ b/src/GrumpyIRC/lib/favoriteswin.ui @@ -67,7 +67,7 @@ false - QAbstractItemView::SingleSelection + QAbstractItemView::MultiSelection QAbstractItemView::SelectRows @@ -102,7 +102,7 @@ QAbstractItemView::NoEditTriggers - QAbstractItemView::SingleSelection + QAbstractItemView::MultiSelection diff --git a/src/GrumpyIRC/lib/grumpyconf.cpp b/src/GrumpyIRC/lib/grumpyconf.cpp index 70a9561..86ca405 100644 --- a/src/GrumpyIRC/lib/grumpyconf.cpp +++ b/src/GrumpyIRC/lib/grumpyconf.cpp @@ -416,6 +416,26 @@ void GrumpyConf::SetContinuousLoggingPath(QString path) GCFG->SetValue("continuous_logging_path", path); } +QHash GrumpyConf::GetIdentities() +{ + return GCFG->GetValueAsHash("identities"); +} + +QHash GrumpyConf::GetNetworks() +{ + return GCFG->GetValueAsHash("networks"); +} + +void GrumpyConf::SetIdentities(const QHash &list) +{ + GCFG->SetValue("identities", list); +} + +void GrumpyConf::SetNetworks(const QHash &hash) +{ + GCFG->SetValue("networks", hash); +} + void GrumpyConf::SetProxy(int proxy) { GCFG->SetValue("proxy", proxy); diff --git a/src/GrumpyIRC/lib/grumpyconf.h b/src/GrumpyIRC/lib/grumpyconf.h index 162f335..bb7cdaa 100644 --- a/src/GrumpyIRC/lib/grumpyconf.h +++ b/src/GrumpyIRC/lib/grumpyconf.h @@ -112,6 +112,10 @@ namespace GrumpyIRC void SetContinuousLoggingEnabled(bool enabled); QString GetContinuousLoggingPath(); void SetContinuousLoggingPath(QString path); + QHash GetIdentities(); + QHash GetNetworks(); + void SetIdentities(const QHash &list); + void SetNetworks(const QHash &hash); ///////////////////////////////////////////////////////////////////////// // Proxy diff --git a/src/GrumpyIRC/lib/mainwindow.cpp b/src/GrumpyIRC/lib/mainwindow.cpp index 12e6837..5f82d79 100644 --- a/src/GrumpyIRC/lib/mainwindow.cpp +++ b/src/GrumpyIRC/lib/mainwindow.cpp @@ -157,6 +157,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi Proxy::Init(); this->SetupAutoAway(); + // Load identities and favorite networks + FavoritesWin::Load(); + if (!CONF->SafeMode) this->Execute(CONF->GetAutorun()); @@ -512,9 +515,9 @@ void MainWindow::on_actionOpen_window_triggered() void MainWindow::on_actionFavorites_triggered() { - FavoritesWin *favorites = new FavoritesWin(this); - favorites->setAttribute(Qt::WA_DeleteOnClose); - favorites->show(); + FavoritesWin favorites(this); + favorites.exec(); + FavoritesWin::Save(); } void MainWindow::on_actionToggle_secret_triggered() @@ -524,13 +527,13 @@ void MainWindow::on_actionToggle_secret_triggered() void MainWindow::on_actionProxy_triggered() { - Proxy p; + Proxy p(this); p.exec(); } void MainWindow::on_actionEnable_proxy_toggled(bool arg1) { - Proxy p; + Proxy p(this); p.Enable(arg1); } diff --git a/src/libcore/configuration.cpp b/src/libcore/configuration.cpp index 614e098..f32621d 100644 --- a/src/libcore/configuration.cpp +++ b/src/libcore/configuration.cpp @@ -80,6 +80,14 @@ QString Configuration::GetValueAsString(const QString &key, const QString &defau return this->Options[key].toString(); } +QHash Configuration::GetValueAsHash(const QString &key, const QHash &default_value) +{ + if (!this->Options.contains(key)) + return default_value; + + return this->Options[key].toHash(); +} + int Configuration::GetValueAsInt(const QString &key, int none) { if (!this->Options.contains(key)) diff --git a/src/libcore/configuration.h b/src/libcore/configuration.h index c494ac1..bf07931 100644 --- a/src/libcore/configuration.h +++ b/src/libcore/configuration.h @@ -39,14 +39,16 @@ namespace GrumpyIRC virtual bool GetValueAsBool(const QString &key, bool none = false); virtual unsigned int GetValueAsUInt(const QString &key, unsigned int none = 0); virtual QString GetValueAsString(const QString &key, const QString &default_value = ""); + virtual QHash GetValueAsHash(const QString &key, const QHash &default_value = QHash()); virtual int GetValueAsInt(const QString &key, int none = 0); virtual float GetValueAsFloat(const QString &key, float none = 0); virtual void RemoveValue(const QString &key); virtual void SetValue(const QString &key, const QVariant &value); - virtual void SetValue(QString key, bool value); - virtual void SetValue(QString key, int value); - virtual void SetValue(QString key, unsigned int value); - virtual void SetValue(QString key, QString value); + virtual void SetValue(const QString &key, bool value); + virtual void SetValue(const QString &ey, int value); + virtual void SetValue(const QString &key, unsigned int value); + virtual void SetValue(const QString &key, const QString &value); + virtual void SetValue(const QString &ey, const QHash &hash); virtual void SetHomePath(const QString &path); // Extension config @@ -90,10 +92,11 @@ namespace GrumpyIRC inline QString mkExt(QString e, QString k) { return "ext_" + e + "/" + k; } }; - inline void Configuration::SetValue(QString key, bool value) { this->SetValue(key, QVariant(value)); } - inline void Configuration::SetValue(QString key, unsigned int value) { this->SetValue(key, QVariant(value)); } - inline void Configuration::SetValue(QString key, int value) { this->SetValue(key, QVariant(value)); } - inline void Configuration::SetValue(QString key, QString value) { this->SetValue(key, QVariant(value)); } + inline void Configuration::SetValue(const QString &key, bool value) { this->SetValue(key, QVariant(value)); } + inline void Configuration::SetValue(const QString &key, unsigned int value) { this->SetValue(key, QVariant(value)); } + inline void Configuration::SetValue(const QString &key, int value) { this->SetValue(key, QVariant(value)); } + inline void Configuration::SetValue(const QString &key, const QString &value) { this->SetValue(key, QVariant(value)); } + inline void Configuration::SetValue(const QString &key, const QHash &hash) { this->SetValue(key, QVariant(hash)); } } #endif // CONFIGURATION_H diff --git a/src/libcore/identity.cpp b/src/libcore/identity.cpp index c9d5cc2..02ab56e 100644 --- a/src/libcore/identity.cpp +++ b/src/libcore/identity.cpp @@ -28,6 +28,13 @@ void Identity::Clear() Identity::LastID = 0; } +Identity::Identity(QHash hash) +{ + this->LoadHash(hash); + if (this->ID > Identity::LastID) + Identity::LastID = this->ID + 1; +} + Identity::Identity(QString nick, QString ident, QString real_name, QString away_msg, int id) { if (id < 0) diff --git a/src/libcore/identity.h b/src/libcore/identity.h index d771212..2bb0282 100644 --- a/src/libcore/identity.h +++ b/src/libcore/identity.h @@ -27,6 +27,7 @@ namespace GrumpyIRC static int LastID; static QHash Identities; + Identity(QHash hash); Identity(QString nick, QString ident, QString real_name, QString away_msg = "", int id = -1); QHash ToHash() override; void LoadHash(const QHash &hash) override; diff --git a/src/libcore/networkinfo.cpp b/src/libcore/networkinfo.cpp index c5f278a..d574ac5 100644 --- a/src/libcore/networkinfo.cpp +++ b/src/libcore/networkinfo.cpp @@ -28,11 +28,6 @@ void NetworkInfo::Clear() NetworkInfo::LastID = 0; } -NetworkInfo::NetworkInfo() -{ - -} - NetworkInfo::NetworkInfo(QString name, QString host, int port, int identity, int id) { if (id < 0) @@ -54,11 +49,14 @@ NetworkInfo::NetworkInfo(QString name, QString host, int port, int identity, int NetworkInfo::NetworkInfo(const QHash &hash) { this->LoadHash(hash); + if (this->ID > LastID) + LastID = this->ID + 1; } QHash NetworkInfo::ToHash() { QHash hash; + SERIALIZE(ID); SERIALIZE(NetworkName); SERIALIZE(Hostname); SERIALIZE(PreferredIdentity); @@ -71,6 +69,7 @@ QHash NetworkInfo::ToHash() void NetworkInfo::LoadHash(const QHash &hash) { UNSERIALIZE_STRING(NetworkName); + UNSERIALIZE_INT(ID); UNSERIALIZE_STRING(Hostname); UNSERIALIZE_INT(PreferredIdentity); UNSERIALIZE_BOOL(SSL); diff --git a/src/libcore/networkinfo.h b/src/libcore/networkinfo.h index 9e03e7a..c857cd3 100644 --- a/src/libcore/networkinfo.h +++ b/src/libcore/networkinfo.h @@ -28,7 +28,6 @@ namespace GrumpyIRC static int LastID; static QHash NetworksInfo; - NetworkInfo(); NetworkInfo(QString name, QString host, int port, int identity, int id = -1); NetworkInfo(const QHash &hash); QHash ToHash() override; From c069120be55f14598c5294c42743a364181923f6 Mon Sep 17 00:00:00 2001 From: Petr Bena Date: Wed, 1 Dec 2021 13:48:05 +0100 Subject: [PATCH 4/5] finished --- src/GrumpyIRC/lib/favoriteswin.cpp | 48 ++++++++++++++++++++++++++++-- src/GrumpyIRC/lib/favoriteswin.ui | 3 ++ src/GrumpyIRC/lib/mainwindow.cpp | 2 ++ src/GrumpyIRC/lib/networkwin.cpp | 48 ++++++++++++++++++++++++++++-- src/GrumpyIRC/lib/networkwin.h | 2 ++ src/GrumpyIRC/lib/networkwin.ui | 9 +++++- src/libcore/identity.cpp | 12 +++++--- src/libcore/identity.h | 2 +- src/libcore/networkinfo.cpp | 20 +++++++++++-- src/libcore/networkinfo.h | 4 ++- 10 files changed, 135 insertions(+), 15 deletions(-) diff --git a/src/GrumpyIRC/lib/favoriteswin.cpp b/src/GrumpyIRC/lib/favoriteswin.cpp index ad77a67..1c4c1bc 100644 --- a/src/GrumpyIRC/lib/favoriteswin.cpp +++ b/src/GrumpyIRC/lib/favoriteswin.cpp @@ -15,6 +15,7 @@ #include "ui_favoriteswin.h" #include "networkwin.h" #include "identityeditorwin.h" +#include "mainwindow.h" #include #include #include @@ -116,6 +117,13 @@ void GrumpyIRC::FavoritesWin::on_tv_IdentityList_customContextMenuRequested(cons QAction *menuRemove = new QAction("Remove", &Menu); Menu.addAction(menuRemove); + QList selected = this->selectedIdents(); + if (selected.count() != 1) + { + menuEdit->setEnabled(false); + } + + QAction* selectedItem = Menu.exec(globalPos); if (!selectedItem) return; @@ -126,16 +134,27 @@ void GrumpyIRC::FavoritesWin::on_tv_IdentityList_customContextMenuRequested(cons this->RefreshIdentities(); } else if (selectedItem == menuRemove) { - QList selected = this->selectedIdents(); foreach (int id, selected) { if (Identity::Identities.contains(id)) { + // Remove reference to this identity from all networks + foreach (int network_id, NetworkInfo::NetworksInfo.keys()) + { + if (NetworkInfo::NetworksInfo[network_id]->PreferredIdentity == id) + NetworkInfo::NetworksInfo[network_id]->PreferredIdentity = -1; + } + delete Identity::Identities[id]; Identity::Identities.remove(id); } } this->RefreshIdentities(); + } else if (selectedItem == menuEdit) + { + IdentityEditorWin iw; + iw.Load(selected[0]); + iw.exec(); } } @@ -144,11 +163,24 @@ void GrumpyIRC::FavoritesWin::on_tv_NetworkList_customContextMenuRequested(const QPoint globalPos = this->ui->tv_NetworkList->viewport()->mapToGlobal(pos); QMenu Menu; // Items + QAction *menuConnect = new QAction("Connect", &Menu); + Menu.addAction(menuConnect); + Menu.addSeparator(); QAction *menuInsert = new QAction("Insert", &Menu); Menu.addAction(menuInsert); + QAction *menuEdit = new QAction("Edit", &Menu); + Menu.addAction(menuEdit); QAction *menuRemove = new QAction("Remove", &Menu); Menu.addAction(menuRemove); + QList selected = this->selectedNetworks(); + + if (selected.count() != 1) + { + menuConnect->setEnabled(false); + menuEdit->setEnabled(false); + } + QAction* selectedItem = Menu.exec(globalPos); if (!selectedItem) return; @@ -159,7 +191,6 @@ void GrumpyIRC::FavoritesWin::on_tv_NetworkList_customContextMenuRequested(const this->RefreshNetworks(); } else if (selectedItem == menuRemove) { - QList selected = this->selectedNetworks(); foreach (int id, selected) { if (NetworkInfo::NetworksInfo.contains(id)) @@ -169,6 +200,17 @@ void GrumpyIRC::FavoritesWin::on_tv_NetworkList_customContextMenuRequested(const } } this->RefreshNetworks(); + } else if (selectedItem == menuEdit) + { + NetworkWin nx; + nx.Load(selected[0]); + nx.exec(); + this->RefreshNetworks(); + } else if (selectedItem == menuConnect) + { + NetworkInfo *network = NetworkInfo::NetworksInfo[selected[0]]; + MainWindow::Main->OpenServer(network->ToServerAddress()); + this->close(); } } @@ -207,7 +249,7 @@ void FavoritesWin::saveIdentities() void FavoritesWin::loadIdentities() { - QHash nl = CONF->GetNetworks(); + QHash nl = CONF->GetIdentities(); foreach (QString key, nl.keys()) { QHash identity_hash = nl[key].toHash(); diff --git a/src/GrumpyIRC/lib/favoriteswin.ui b/src/GrumpyIRC/lib/favoriteswin.ui index 18aa388..4d9b993 100644 --- a/src/GrumpyIRC/lib/favoriteswin.ui +++ b/src/GrumpyIRC/lib/favoriteswin.ui @@ -104,6 +104,9 @@ QAbstractItemView::MultiSelection + + QAbstractItemView::SelectRows + diff --git a/src/GrumpyIRC/lib/mainwindow.cpp b/src/GrumpyIRC/lib/mainwindow.cpp index 5f82d79..32bf2f8 100644 --- a/src/GrumpyIRC/lib/mainwindow.cpp +++ b/src/GrumpyIRC/lib/mainwindow.cpp @@ -481,6 +481,7 @@ void MainWindow::on_actionExit_triggered() void MainWindow::closeEvent(QCloseEvent *event) { + (void) event; Exit(); } @@ -518,6 +519,7 @@ void MainWindow::on_actionFavorites_triggered() FavoritesWin favorites(this); favorites.exec(); FavoritesWin::Save(); + CONF->Save(); } void MainWindow::on_actionToggle_secret_triggered() diff --git a/src/GrumpyIRC/lib/networkwin.cpp b/src/GrumpyIRC/lib/networkwin.cpp index c8e2806..8e188d5 100644 --- a/src/GrumpyIRC/lib/networkwin.cpp +++ b/src/GrumpyIRC/lib/networkwin.cpp @@ -10,8 +10,9 @@ // Copyright (c) Petr Bena 2021 -#include "../../libcore/networkinfo.h" #include "networkwin.h" +#include +#include #include "ui_networkwin.h" using namespace GrumpyIRC; @@ -19,6 +20,16 @@ using namespace GrumpyIRC; NetworkWin::NetworkWin(QWidget *parent) : QDialog(parent), ui(new Ui::NetworkWin) { this->ui->setupUi(this); + QList identities = Identity::Identities.keys(); + int i = 1; + this->ui->cb_IdentityNumber->addItem("Default"); + this->identityMap.insert(0, -1); + foreach (int identity, identities) + { + this->identityMap.insert(i, identity); + this->ui->cb_IdentityNumber->addItem(Identity::Identities[identity]->ToString()); + i++; + } } NetworkWin::~NetworkWin() @@ -26,17 +37,50 @@ NetworkWin::~NetworkWin() delete this->ui; } +void NetworkWin::Load(int network_id) +{ + NetworkInfo *x = NetworkInfo::NetworksInfo[network_id]; + this->networkID = network_id; + this->ui->le_Port->setText(QString::number(x->Port)); + this->ui->checkBox_SSL->setChecked(x->SSL); + this->ui->le_HostName->setText(x->Hostname); + this->ui->le_Name->setText(x->NetworkName); + // find identity ID + if (x->PreferredIdentity > -1) + { + int identity = -1; + foreach (int id, this->identityMap.keys()) + { + if (this->identityMap[id] == x->PreferredIdentity) + { + identity = id; + break; + } + } + if (identity > -1) + this->ui->cb_IdentityNumber->setCurrentIndex(identity); + } +} + void GrumpyIRC::NetworkWin::on_pushSave_clicked() { if (this->networkID < 0) { - NetworkInfo *network = new NetworkInfo(this->ui->le_Name->text(), this->ui->le_HostName->text(), this->ui->le_Port->text().toInt(), -1); + NetworkInfo *network = new NetworkInfo(this->ui->le_Name->text(), + this->ui->le_HostName->text(), + this->ui->le_Port->text().toInt(), + this->ui->checkBox_SSL->isChecked(), + this->identityMap[this->ui->cb_IdentityNumber->currentIndex()]); NetworkInfo::NetworksInfo.insert(network->ID, network); this->close(); return; } NetworkInfo *x = NetworkInfo::NetworksInfo[this->networkID]; x->NetworkName = this->ui->le_Name->text(); + x->Hostname = this->ui->le_HostName->text(); + x->Port = this->ui->le_Port->text().toInt(); + x->SSL = this->ui->checkBox_SSL->isChecked(); + x->PreferredIdentity = this->identityMap[this->ui->cb_IdentityNumber->currentIndex()]; this->close(); } diff --git a/src/GrumpyIRC/lib/networkwin.h b/src/GrumpyIRC/lib/networkwin.h index 7d143b8..f28c870 100644 --- a/src/GrumpyIRC/lib/networkwin.h +++ b/src/GrumpyIRC/lib/networkwin.h @@ -28,12 +28,14 @@ namespace GrumpyIRC public: explicit NetworkWin(QWidget *parent = nullptr); ~NetworkWin(); + void Load(int network_id); private slots: void on_pushSave_clicked(); void on_pushExit_clicked(); private: + QHash identityMap; Ui::NetworkWin *ui; int networkID = -1; }; diff --git a/src/GrumpyIRC/lib/networkwin.ui b/src/GrumpyIRC/lib/networkwin.ui index 407d86b..8589ab9 100644 --- a/src/GrumpyIRC/lib/networkwin.ui +++ b/src/GrumpyIRC/lib/networkwin.ui @@ -7,7 +7,7 @@ 0 0 470 - 192 + 197 @@ -62,6 +62,13 @@ + + + + SSL + + + diff --git a/src/libcore/identity.cpp b/src/libcore/identity.cpp index 02ab56e..647cda8 100644 --- a/src/libcore/identity.cpp +++ b/src/libcore/identity.cpp @@ -31,7 +31,7 @@ void Identity::Clear() Identity::Identity(QHash hash) { this->LoadHash(hash); - if (this->ID > Identity::LastID) + if (this->ID >= Identity::LastID) Identity::LastID = this->ID + 1; } @@ -43,7 +43,7 @@ Identity::Identity(QString nick, QString ident, QString real_name, QString away_ } else { this->ID = id; - if (id > Identity::LastID) + if (id >= Identity::LastID) Identity::LastID = id + 1; } @@ -56,6 +56,7 @@ Identity::Identity(QString nick, QString ident, QString real_name, QString away_ QHash Identity::ToHash() { QHash hash; + SERIALIZE(ID); SERIALIZE(Nick); SERIALIZE(Ident); SERIALIZE(RealName); @@ -65,15 +66,18 @@ QHash Identity::ToHash() void Identity::LoadHash(const QHash &hash) { + UNSERIALIZE_INT(ID); UNSERIALIZE_STRING(Nick); UNSERIALIZE_STRING(Ident); UNSERIALIZE_STRING(RealName); UNSERIALIZE_STRING(AwayMessage); } -void Identity::ToString() +QString Identity::ToString() { QString result = this->Nick; if (!this->Ident.isEmpty()) - result += "@" + this->Ident; + result += "!" + this->Ident; + + return result; } diff --git a/src/libcore/identity.h b/src/libcore/identity.h index 2bb0282..899fb51 100644 --- a/src/libcore/identity.h +++ b/src/libcore/identity.h @@ -31,7 +31,7 @@ namespace GrumpyIRC Identity(QString nick, QString ident, QString real_name, QString away_msg = "", int id = -1); QHash ToHash() override; void LoadHash(const QHash &hash) override; - void ToString(); + QString ToString(); int ID; QString Nick; QString Ident; diff --git a/src/libcore/networkinfo.cpp b/src/libcore/networkinfo.cpp index d574ac5..6a6adf6 100644 --- a/src/libcore/networkinfo.cpp +++ b/src/libcore/networkinfo.cpp @@ -11,6 +11,7 @@ // Copyright (c) Petr Bena 2021 #include "networkinfo.h" +#include "identity.h" using namespace GrumpyIRC; @@ -28,7 +29,7 @@ void NetworkInfo::Clear() NetworkInfo::LastID = 0; } -NetworkInfo::NetworkInfo(QString name, QString host, int port, int identity, int id) +NetworkInfo::NetworkInfo(QString name, QString host, int port, int identity, bool ssl, int id) { if (id < 0) { @@ -36,23 +37,36 @@ NetworkInfo::NetworkInfo(QString name, QString host, int port, int identity, int } else { this->ID = id; - if (id > NetworkInfo::LastID) + if (id >= NetworkInfo::LastID) NetworkInfo::LastID = id + 1; } this->NetworkName = name; this->Hostname = host; this->Port = port; + this->SSL = ssl; this->PreferredIdentity = identity; } NetworkInfo::NetworkInfo(const QHash &hash) { this->LoadHash(hash); - if (this->ID > LastID) + if (this->ID >= LastID) LastID = this->ID + 1; } +libirc::ServerAddress NetworkInfo::ToServerAddress() +{ + libirc::ServerAddress addr(this->Hostname, this->SSL, this->Port); + if (this->PreferredIdentity >= 0 && Identity::Identities.contains(this->PreferredIdentity)) + { + Identity *identity = Identity::Identities[this->PreferredIdentity]; + addr.SetNick(identity->Nick); + addr.SetIdent(identity->Ident); + } + return addr; +} + QHash NetworkInfo::ToHash() { QHash hash; diff --git a/src/libcore/networkinfo.h b/src/libcore/networkinfo.h index c857cd3..3773026 100644 --- a/src/libcore/networkinfo.h +++ b/src/libcore/networkinfo.h @@ -16,6 +16,7 @@ #include #include #include "libcore_global.h" +#include "../libirc/libirc/serveraddress.h" #include "../libirc/libirc/serializableitem.h" #include "../libirc/libirc/irc_standards.h" @@ -28,8 +29,9 @@ namespace GrumpyIRC static int LastID; static QHash NetworksInfo; - NetworkInfo(QString name, QString host, int port, int identity, int id = -1); + NetworkInfo(QString name, QString host, int port, int identity, bool ssl, int id = -1); NetworkInfo(const QHash &hash); + libirc::ServerAddress ToServerAddress(); QHash ToHash() override; void LoadHash(const QHash &hash) override; int ID; From 82a7b881ba9d4fe228eb2b2c49f99075a05a98d6 Mon Sep 17 00:00:00 2001 From: Petr Bena Date: Sat, 11 Dec 2021 16:19:10 +0100 Subject: [PATCH 5/5] fixed window title --- src/GrumpyIRC/lib/networkwin.ui | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GrumpyIRC/lib/networkwin.ui b/src/GrumpyIRC/lib/networkwin.ui index 8589ab9..773c06d 100644 --- a/src/GrumpyIRC/lib/networkwin.ui +++ b/src/GrumpyIRC/lib/networkwin.ui @@ -7,11 +7,11 @@ 0 0 470 - 197 + 220 - Dialog + Network