File indexing completed on 2024-12-08 07:32:11

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
0004    Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 */
0011 #include "transfersettingsdialog.h"
0012 #include "mirror/mirrorsettings.h"
0013 #include "renamefile.h"
0014 #include "settings.h"
0015 #include "signaturedlg.h"
0016 #include "verificationdialog.h"
0017 
0018 #include "core/filemodel.h"
0019 #include "core/transferhandler.h"
0020 #include "core/verifier.h"
0021 
0022 #include <KLocalizedString>
0023 #include <KMessageBox>
0024 
0025 #include <QLineEdit>
0026 #include <QSortFilterProxyModel>
0027 
0028 TransferSettingsDialog::TransferSettingsDialog(QWidget *parent, TransferHandler *transfer)
0029     : KGetSaveSizeDialog("TransferSettingsDialog", parent)
0030     , m_transfer(transfer)
0031     , m_model(m_transfer->fileModel())
0032 {
0033     setWindowTitle(i18n("Transfer Settings for %1", m_transfer->source().fileName()));
0034 
0035     ui.setupUi(this);
0036 
0037     ui.ktitlewidget->setIcon(QIcon::fromTheme("preferences-other"));
0038     ui.ktitlewidget->setIconSize(QSize(16, 16));
0039     ui.downloadSpin->setValue(m_transfer->downloadLimit(Transfer::VisibleSpeedLimit));
0040     ui.uploadSpin->setValue(m_transfer->uploadLimit(Transfer::VisibleSpeedLimit));
0041     ui.ratioSpin->setValue(m_transfer->maximumShareRatio());
0042     ui.destination->setUrl(m_transfer->directory());
0043     ui.destination->lineEdit()->setReadOnly(true);
0044     ui.rename->setIcon(QIcon::fromTheme("edit-rename"));
0045     ui.mirrors->setIcon(QIcon::fromTheme("download"));
0046     ui.signature->setIcon(QIcon::fromTheme("application-pgp-signature"));
0047     ui.verification->setIcon(QIcon::fromTheme("document-decrypt"));
0048 
0049     if (m_model) {
0050         m_model->watchCheckState();
0051         m_proxy = new QSortFilterProxyModel(this);
0052         m_proxy->setSourceModel(m_model);
0053         ui.treeView->setModel(m_proxy);
0054         ui.treeView->sortByColumn(0, Qt::AscendingOrder);
0055 
0056         QByteArray loadedState = QByteArray::fromBase64(Settings::transferSettingsHeaderState().toLatin1());
0057         if (!loadedState.isEmpty()) {
0058             ui.treeView->header()->restoreState(loadedState);
0059         } else {
0060             ui.treeView->header()->resizeSection(0, ui.treeView->header()->defaultSectionSize() * 3);
0061         }
0062     }
0063 
0064     updateCapabilities();
0065 
0066     connect(m_transfer, &TransferHandler::capabilitiesChanged, this, &TransferSettingsDialog::updateCapabilities);
0067     connect(this, &TransferSettingsDialog::accepted, this, &TransferSettingsDialog::save);
0068     connect(this, &TransferSettingsDialog::finished, this, &TransferSettingsDialog::slotFinished);
0069     connect(ui.treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TransferSettingsDialog::slotSelectionChanged);
0070     connect(ui.rename, &QPushButton::clicked, this, &TransferSettingsDialog::slotRename);
0071     connect(ui.mirrors, &QPushButton::clicked, this, &TransferSettingsDialog::slotMirrors);
0072     connect(ui.verification, &QPushButton::clicked, this, &TransferSettingsDialog::slotVerification);
0073     connect(ui.signature, &QPushButton::clicked, this, &TransferSettingsDialog::slotSignature);
0074 
0075     connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0076     connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0077 }
0078 
0079 TransferSettingsDialog::~TransferSettingsDialog()
0080 {
0081     if (m_model) {
0082         Settings::setTransferSettingsHeaderState(ui.treeView->header()->saveState().toBase64());
0083     }
0084 }
0085 
0086 QSize TransferSettingsDialog::sizeHint() const
0087 {
0088     QSize sh = QDialog::sizeHint();
0089     sh.setWidth(sh.width() * 1.7);
0090     return sh;
0091 }
0092 
0093 void TransferSettingsDialog::updateCapabilities()
0094 {
0095     const int capabilities = m_transfer->capabilities();
0096 
0097     const bool supportsSpeedLimit = capabilities & Transfer::Cap_SpeedLimit;
0098     ui.labelDownload->setVisible(supportsSpeedLimit);
0099     ui.downloadSpin->setVisible(supportsSpeedLimit);
0100     ui.labelUpload->setVisible(supportsSpeedLimit);
0101     ui.uploadSpin->setVisible(supportsSpeedLimit);
0102     ui.labelShareRatio->setVisible(supportsSpeedLimit);
0103     ui.ratioSpin->setVisible(supportsSpeedLimit);
0104 
0105     ui.destination->setEnabled(capabilities & Transfer::Cap_Moving);
0106     ui.mirrors->setVisible(capabilities & Transfer::Cap_MultipleMirrors);
0107     ui.rename->setVisible(capabilities & Transfer::Cap_Renaming);
0108 }
0109 
0110 void TransferSettingsDialog::slotMirrors()
0111 {
0112     const QModelIndex index = m_proxy->mapToSource(ui.treeView->selectionModel()->selectedIndexes().first());
0113     QDialog *mirrors = new MirrorSettings(this, m_transfer, m_model->getUrl(index));
0114     mirrors->setAttribute(Qt::WA_DeleteOnClose);
0115     mirrors->show();
0116 }
0117 
0118 void TransferSettingsDialog::slotRename()
0119 {
0120     const QModelIndex index = m_proxy->mapToSource(ui.treeView->selectionModel()->selectedIndexes().first());
0121     auto *renameDlg = new RenameFile(m_model, index, this);
0122     renameDlg->setAttribute(Qt::WA_DeleteOnClose);
0123     renameDlg->show();
0124 }
0125 
0126 void TransferSettingsDialog::slotVerification()
0127 {
0128     const QModelIndex index = m_proxy->mapToSource(ui.treeView->selectionModel()->selectedIndexes().first());
0129     QDialog *verification = new VerificationDialog(this, m_transfer, m_model->getUrl(index));
0130     verification->setAttribute(Qt::WA_DeleteOnClose);
0131     verification->show();
0132 }
0133 
0134 void TransferSettingsDialog::slotSignature()
0135 {
0136     const QModelIndex index = m_proxy->mapToSource(ui.treeView->selectionModel()->selectedIndexes().first());
0137 
0138     auto *signature = new SignatureDlg(m_transfer, m_model->getUrl(index), this);
0139     signature->setAttribute(Qt::WA_DeleteOnClose);
0140     signature->show();
0141 }
0142 
0143 void TransferSettingsDialog::slotSelectionChanged()
0144 {
0145     bool enabled = false;
0146     // only enable rename when one item is selected and when this item is a file
0147     if (ui.treeView->selectionModel()->selectedRows().count() == 1) {
0148         const QModelIndex index = m_proxy->mapToSource(ui.treeView->selectionModel()->selectedIndexes().first());
0149         if (index.isValid() && !(static_cast<FileItem *>(index.internalPointer()))->childCount()) {
0150             enabled = true;
0151         }
0152     }
0153     ui.mirrors->setEnabled(enabled);
0154     ui.rename->setEnabled(enabled);
0155     ui.verification->setEnabled(enabled);
0156     ui.signature->setEnabled(enabled);
0157 }
0158 
0159 void TransferSettingsDialog::save()
0160 { // TODO: Set to -1 when no limit
0161     QUrl oldDirectory = m_transfer->directory();
0162     QUrl newDirectory = ui.destination->url();
0163     if ((oldDirectory != newDirectory) && !m_transfer->setDirectory(newDirectory)) {
0164         KMessageBox::error(this, i18n("Changing the destination did not work, the destination stays unmodified."), i18n("Destination unmodified"));
0165     }
0166 
0167     m_transfer->setDownloadLimit(ui.downloadSpin->value(), Transfer::VisibleSpeedLimit);
0168     m_transfer->setUploadLimit(ui.uploadSpin->value(), Transfer::VisibleSpeedLimit);
0169     m_transfer->setMaximumShareRatio(ui.ratioSpin->value());
0170 }
0171 
0172 void TransferSettingsDialog::slotFinished()
0173 {
0174     if (m_model) {
0175         m_model->stopWatchCheckState();
0176     }
0177 }
0178 
0179 #include "moc_transfersettingsdialog.cpp"