File indexing completed on 2024-12-01 04:36:38

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "configureaccountserverwidget.h"
0008 #include "removeaccountdialog.h"
0009 #include "ui_configureaccountserverwidget.h"
0010 #include <KLocalizedString>
0011 #include <QPointer>
0012 
0013 ConfigureAccountServerWidget::ConfigureAccountServerWidget(QWidget *parent)
0014     : QWidget(parent)
0015     , ui(new Ui::ConfigureAccountServerWidget)
0016 {
0017     ui->setupUi(this);
0018     connect(ui->modifyServer, &QPushButton::clicked, this, &ConfigureAccountServerWidget::slotModifyServer);
0019     connect(ui->addServer, &QPushButton::clicked, this, &ConfigureAccountServerWidget::slotAddServer);
0020     connect(ui->removeServer, &QPushButton::clicked, this, &ConfigureAccountServerWidget::slotDeleteServer);
0021     connect(ui->accountServerListwidget, &AccountServerListWidget::itemSelectionChanged, this, &ConfigureAccountServerWidget::slotItemSelectionChanged);
0022     connect(ui->accountServerListwidget->model(), &QAbstractItemModel::rowsMoved, this, &ConfigureAccountServerWidget::slotItemSelectionChanged);
0023 
0024     ui->moveUpServer->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
0025     ui->moveUpServer->setToolTip(i18nc("Move selected account up.", "Up"));
0026     ui->moveUpServer->setEnabled(false); // b/c no item is selected yet
0027     ui->moveUpServer->setFocusPolicy(Qt::StrongFocus);
0028     ui->moveUpServer->setAutoRepeat(true);
0029 
0030     ui->moveDownServer->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
0031     ui->moveDownServer->setToolTip(i18nc("Move selected account down.", "Down"));
0032     ui->moveDownServer->setEnabled(false); // b/c no item is selected yet
0033     ui->moveDownServer->setFocusPolicy(Qt::StrongFocus);
0034     ui->moveDownServer->setAutoRepeat(true);
0035 
0036     connect(ui->moveUpServer, &QPushButton::clicked, ui->accountServerListwidget, &AccountServerListWidget::slotMoveAccountUp);
0037     connect(ui->moveDownServer, &QPushButton::clicked, ui->accountServerListwidget, &AccountServerListWidget::slotMoveAccountDown);
0038 
0039     slotItemSelectionChanged();
0040 }
0041 
0042 ConfigureAccountServerWidget::~ConfigureAccountServerWidget()
0043 {
0044     delete ui;
0045 }
0046 
0047 void ConfigureAccountServerWidget::save()
0048 {
0049     ui->accountServerListwidget->save();
0050 }
0051 
0052 void ConfigureAccountServerWidget::load()
0053 {
0054     ui->accountServerListwidget->load();
0055 }
0056 
0057 void ConfigureAccountServerWidget::slotModifyServer()
0058 {
0059     ui->accountServerListwidget->modifyAccountConfig();
0060 }
0061 
0062 void ConfigureAccountServerWidget::slotAddServer()
0063 {
0064     ui->accountServerListwidget->addAccountConfig();
0065 }
0066 
0067 void ConfigureAccountServerWidget::slotDeleteServer()
0068 {
0069     QListWidgetItem *item = ui->accountServerListwidget->currentItem();
0070     if (!item) {
0071         return;
0072     }
0073     QPointer<RemoveAccountDialog> dlg = new RemoveAccountDialog(this);
0074     dlg->setAccountName(item->text());
0075     if (dlg->exec()) {
0076         ui->accountServerListwidget->deleteAccountConfig(item, dlg->removeLogs());
0077         delete item;
0078         slotItemSelectionChanged();
0079     }
0080     delete dlg;
0081 }
0082 
0083 void ConfigureAccountServerWidget::slotItemSelectionChanged()
0084 {
0085     const bool hasItemSelected = ui->accountServerListwidget->currentItem();
0086     ui->modifyServer->setEnabled(hasItemSelected);
0087     ui->removeServer->setEnabled(hasItemSelected);
0088 
0089     ui->moveUpServer->setEnabled(hasItemSelected && ui->accountServerListwidget->currentRow() != 0);
0090     ui->moveDownServer->setEnabled(hasItemSelected && ui->accountServerListwidget->currentRow() != ui->accountServerListwidget->count() - 1);
0091 }
0092 
0093 #include "moc_configureaccountserverwidget.cpp"