File indexing completed on 2024-10-06 04:35:41

0001 /*
0002  *  Synchronization dialog
0003  *
0004  *  SPDX-FileCopyrightText: 2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 // application specific includes
0009 #include "smb4ksynchronizationdialog.h"
0010 #include "core/smb4ksettings.h"
0011 #include "core/smb4ksynchronizer.h"
0012 
0013 // Qt includes
0014 #include <QDialogButtonBox>
0015 #include <QHBoxLayout>
0016 #include <QVBoxLayout>
0017 #include <QWindow>
0018 
0019 // KDE includes
0020 #include <KIconLoader>
0021 #include <KLineEdit>
0022 #include <KLocalizedString>
0023 #include <KUrlCompletion>
0024 #include <KWindowConfig>
0025 
0026 Smb4KSynchronizationDialog::Smb4KSynchronizationDialog(QWidget *parent)
0027     : QDialog(parent)
0028 {
0029     setWindowTitle(i18n("Synchronization"));
0030     setAttribute(Qt::WA_DeleteOnClose);
0031 
0032     QVBoxLayout *layout = new QVBoxLayout(this);
0033 
0034     QWidget *descriptionWidget = new QWidget(this);
0035 
0036     QHBoxLayout *descriptionLayout = new QHBoxLayout(descriptionWidget);
0037     descriptionLayout->setContentsMargins(0, 0, 0, 0);
0038 
0039     QLabel *descriptionPixmap = new QLabel(descriptionWidget);
0040     descriptionPixmap->setPixmap(KDE::icon(QStringLiteral("folder-sync")).pixmap(KIconLoader::SizeHuge));
0041     descriptionPixmap->setAlignment(Qt::AlignBottom);
0042     descriptionPixmap->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0043 
0044     descriptionLayout->addWidget(descriptionPixmap);
0045 
0046     m_descriptionText = new QLabel(descriptionWidget);
0047     m_descriptionText->setText(i18n("Synchronization"));
0048     m_descriptionText->setWordWrap(true);
0049     m_descriptionText->setAlignment(Qt::AlignBottom);
0050     m_descriptionText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0051 
0052     descriptionLayout->addWidget(m_descriptionText);
0053 
0054     layout->addWidget(descriptionWidget);
0055     layout->addSpacing(layout->spacing());
0056 
0057     QWidget *inputWidget = new QWidget(this);
0058 
0059     QGridLayout *inputWidgetLayout = new QGridLayout(inputWidget);
0060     inputWidgetLayout->setContentsMargins(0, 0, 0, 0);
0061 
0062     QLabel *sourceLabel = new QLabel(i18n("Source:"));
0063 
0064     m_sourceInput = new KUrlRequester(this);
0065     m_sourceInput->setMode(KFile::Directory | KFile::LocalOnly);
0066     m_sourceInput->lineEdit()->setSqueezedTextEnabled(true);
0067     m_sourceInput->completionObject()->setCompletionMode(KCompletion::CompletionPopupAuto);
0068     m_sourceInput->completionObject()->setMode(KUrlCompletion::FileCompletion);
0069 
0070     connect(m_sourceInput, &KUrlRequester::textChanged, this, &Smb4KSynchronizationDialog::slotSourcePathChanged);
0071 
0072     QLabel *destinationLabel = new QLabel(i18n("Destination:"));
0073 
0074     m_destinationInput = new KUrlRequester(this);
0075     m_destinationInput->setMode(KFile::Directory | KFile::LocalOnly);
0076     m_destinationInput->lineEdit()->setSqueezedTextEnabled(true);
0077     m_destinationInput->completionObject()->setCompletionMode(KCompletion::CompletionPopupAuto);
0078     m_destinationInput->completionObject()->setMode(KUrlCompletion::FileCompletion);
0079 
0080     connect(m_destinationInput, &KUrlRequester::textChanged, this, &Smb4KSynchronizationDialog::slotDestinationPathChanged);
0081 
0082     inputWidgetLayout->addWidget(sourceLabel, 0, 0);
0083     inputWidgetLayout->addWidget(m_sourceInput, 0, 1);
0084     inputWidgetLayout->addWidget(destinationLabel, 1, 0);
0085     inputWidgetLayout->addWidget(m_destinationInput, 1, 1);
0086 
0087     layout->addWidget(inputWidget);
0088 
0089     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0090     m_swapButton = buttonBox->addButton(i18n("Swap Paths"), QDialogButtonBox::ActionRole);
0091     m_swapButton->setEnabled(false);
0092     m_synchronizeButton = buttonBox->addButton(i18n("Synchronize"), QDialogButtonBox::ActionRole);
0093     m_synchronizeButton->setEnabled(false);
0094     m_cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
0095     m_cancelButton->setShortcut(QKeySequence::Cancel);
0096 
0097     connect(m_swapButton, &QPushButton::clicked, this, &Smb4KSynchronizationDialog::slotSwapPaths);
0098     connect(m_synchronizeButton, &QPushButton::clicked, this, &Smb4KSynchronizationDialog::slotSynchronize);
0099     connect(m_cancelButton, &QPushButton::clicked, this, &Smb4KSynchronizationDialog::reject);
0100 
0101     layout->addWidget(buttonBox);
0102 
0103     setMinimumWidth(sizeHint().width() > 350 ? sizeHint().width() : 350);
0104 
0105     create();
0106 
0107     KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("SynchronizationDialog"));
0108     QSize dialogSize;
0109 
0110     if (dialogGroup.exists()) {
0111         KWindowConfig::restoreWindowSize(windowHandle(), dialogGroup);
0112         dialogSize = windowHandle()->size();
0113     } else {
0114         dialogSize = sizeHint();
0115     }
0116 
0117     resize(dialogSize); // workaround for QTBUG-40584
0118 }
0119 
0120 Smb4KSynchronizationDialog::~Smb4KSynchronizationDialog()
0121 {
0122 }
0123 
0124 bool Smb4KSynchronizationDialog::setShare(const SharePtr &share)
0125 {
0126     if (share->isPrinter() && share->isInaccessible()) {
0127         return false;
0128     }
0129 
0130     m_descriptionText->setText(i18n("Please provide the source and destination directory for the synchronization of <b>%1</b>.", share->displayString()));
0131 
0132     m_sourceInput->setUrl(QUrl::fromLocalFile(share->path()));
0133     m_destinationInput->setUrl(
0134         QUrl::fromLocalFile(Smb4KSettings::rsyncPrefix().path() + QDir::separator() + share->hostName() + QDir::separator() + share->shareName())
0135             .adjusted(QUrl::NormalizePathSegments));
0136 
0137     m_synchronizeButton->setDefault(true);
0138 
0139     return true;
0140 }
0141 
0142 void Smb4KSynchronizationDialog::slotSourcePathChanged(const QString &path)
0143 {
0144     Q_UNUSED(path);
0145 
0146     bool enable =
0147         !m_sourceInput->url().isEmpty() && m_sourceInput->url().isValid() && !m_destinationInput->url().isEmpty() && m_destinationInput->url().isValid();
0148 
0149     m_swapButton->setEnabled(enable);
0150     m_synchronizeButton->setEnabled(enable);
0151 }
0152 
0153 void Smb4KSynchronizationDialog::slotDestinationPathChanged(const QString &path)
0154 {
0155     Q_UNUSED(path);
0156 
0157     bool enable =
0158         !m_sourceInput->url().isEmpty() && m_sourceInput->url().isValid() && !m_destinationInput->url().isEmpty() && m_destinationInput->url().isValid();
0159 
0160     m_swapButton->setEnabled(enable);
0161     m_synchronizeButton->setEnabled(enable);
0162 }
0163 
0164 void Smb4KSynchronizationDialog::slotSwapPaths()
0165 {
0166     QUrl sourceUrl = m_sourceInput->url();
0167     QUrl destinationUrl = m_destinationInput->url();
0168 
0169     m_sourceInput->setUrl(destinationUrl);
0170     m_destinationInput->setUrl(sourceUrl);
0171 }
0172 
0173 void Smb4KSynchronizationDialog::slotSynchronize()
0174 {
0175     Smb4KSynchronizer::self()->synchronize(m_sourceInput->url(), m_destinationInput->url());
0176 
0177     KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("SynchronizationDialog"));
0178     KWindowConfig::saveWindowSize(windowHandle(), dialogGroup);
0179 
0180     accept();
0181 }