| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/dialogs/formaddaccount.h" |
| 4 | |
| 5 | #include "core/feedsmodel.h" |
| 6 | #include "gui/guiutilities.h" |
| 7 | #include "miscellaneous/application.h" |
| 8 | #include "miscellaneous/iconfactory.h" |
| 9 | #include "services/abstract/serviceentrypoint.h" |
| 10 | |
| 11 | #include <QDialogButtonBox> |
| 12 | #include <QListWidget> |
| 13 | #include <QSize> |
| 14 | |
| 15 | FormAddAccount::FormAddAccount(const QList<ServiceEntryPoint*>& entry_points, FeedsModel* model, QWidget* parent) |
| 16 | : QDialog(parent), m_ui(new Ui::FormAddAccount), m_model(model), m_entryPoints(entry_points) { |
| 17 | m_ui->setupUi(this); |
| 18 | |
| 19 | // Set flags and attributes. |
| 20 | GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("list-add"))); |
| 21 | |
| 22 | connect(m_ui->m_listEntryPoints, &QListWidget::itemDoubleClicked, this, &FormAddAccount::addSelectedAccount); |
| 23 | connect(m_ui->m_buttonBox, &QDialogButtonBox::accepted, this, &FormAddAccount::addSelectedAccount); |
| 24 | connect(m_ui->m_listEntryPoints, &QListWidget::currentRowChanged, this, &FormAddAccount::showAccountDetails); |
| 25 | |
| 26 | loadEntryPoints(); |
| 27 | } |
| 28 | |
| 29 | FormAddAccount::~FormAddAccount() { |
| 30 | qDebugNN << LOGSEC_GUI << "Destroying FormAddAccount instance."; |
| 31 | } |
| 32 | |
| 33 | void FormAddAccount::addSelectedAccount() { |
| 34 | accept(); |
| 35 | ServiceEntryPoint* point = selectedEntryPoint(); |
| 36 | ServiceRoot* new_root = point->createNewRoot(); |
| 37 | |
| 38 | if (new_root != nullptr) { |
| 39 | m_model->addServiceAccount(new_root, true); |
| 40 | } |
| 41 | else { |
| 42 | qDebugNN << LOGSEC_CORE << "Cannot create new account."; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void FormAddAccount::showAccountDetails() { |
| 47 | ServiceEntryPoint* point = selectedEntryPoint(); |
| 48 | |
| 49 | if (point != nullptr) { |
| 50 | m_ui->m_lblDetails->setText(point->description()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | ServiceEntryPoint* FormAddAccount::selectedEntryPoint() const { |
| 55 | return reinterpret_cast<ServiceEntryPoint*>(m_ui->m_listEntryPoints->currentItem() |
| 56 | ->data(Qt::ItemDataRole::UserRole) |
| 57 | .value<intptr_t>()); |
| 58 | } |
| 59 | |
| 60 | void FormAddAccount::loadEntryPoints() { |
| 61 | for (const ServiceEntryPoint* entry_point : std::as_const(m_entryPoints)) { |
| 62 | QListWidgetItem* item = new QListWidgetItem(entry_point->icon(), entry_point->name()); |
| 63 | |
| 64 | item->setToolTip(entry_point->description()); |
| 65 | item->setData(Qt::ItemDataRole::UserRole, QVariant::fromValue<intptr_t>(reinterpret_cast<intptr_t>(entry_point))); |
| 66 | |
| 67 | if (entry_point->code() == QSL(SERVICE_CODE_STD_RSS)) { |
| 68 | m_ui->m_listEntryPoints->insertItem(0, item); |
| 69 | m_ui->m_listEntryPoints->insertItem(1, QSL("--------")); |
| 70 | |
| 71 | m_ui->m_listEntryPoints->item(1)->setFlags(Qt::ItemFlag::NoItemFlags); |
| 72 | } |
| 73 | else { |
| 74 | m_ui->m_listEntryPoints->addItem(item); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | m_ui->m_listEntryPoints->setCurrentRow(0); |
| 79 | } |