File indexing completed on 2024-04-21 05:01:42

0001 /*
0002     The configuration dialog of Smb4K
0003 
0004     SPDX-FileCopyrightText: 2004-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4kconfigdialog.h"
0010 #include "core/smb4ksettings.h"
0011 
0012 #if defined(Q_OS_LINUX)
0013 #include "smb4kmountsettings_linux.h"
0014 #elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
0015 #include "smb4kmountsettings_bsd.h"
0016 #endif
0017 
0018 // Qt includes
0019 #include <QList>
0020 #include <QPair>
0021 #include <QScrollArea>
0022 #include <QSize>
0023 #include <QStandardPaths>
0024 #include <QWindow>
0025 
0026 // KDE includes
0027 #include <KLocalizedString>
0028 #include <KMessageBox>
0029 #include <KPluginFactory>
0030 #include <KUrlRequester>
0031 #include <KWindowConfig>
0032 
0033 using namespace Smb4KGlobal;
0034 
0035 K_PLUGIN_FACTORY(Smb4KConfigDialogFactory, registerPlugin<Smb4KConfigDialog>();)
0036 
0037 Smb4KConfigDialog::Smb4KConfigDialog(QWidget *parent, const QList<QVariant> & /*args*/)
0038     : KConfigDialog(parent, QStringLiteral("ConfigDialog"), Smb4KSettings::self())
0039 {
0040     setAttribute(Qt::WA_DeleteOnClose, true);
0041 
0042     setupDialog();
0043 }
0044 
0045 Smb4KConfigDialog::~Smb4KConfigDialog()
0046 {
0047 }
0048 
0049 void Smb4KConfigDialog::setupDialog()
0050 {
0051     // Add the pages:
0052     m_userInterfacePage = new Smb4KConfigPageUserInterface(this);
0053     QScrollArea *userInterfaceArea = new QScrollArea(this);
0054     userInterfaceArea->setWidget(m_userInterfacePage);
0055     userInterfaceArea->setWidgetResizable(true);
0056     userInterfaceArea->setFrameStyle(QFrame::NoFrame);
0057 
0058     m_networkPage = new Smb4KConfigPageNetwork(this);
0059     QScrollArea *networkArea = new QScrollArea(this);
0060     networkArea->setWidget(m_networkPage);
0061     networkArea->setWidgetResizable(true);
0062     networkArea->setFrameStyle(QFrame::NoFrame);
0063 
0064     m_mountingPage = new Smb4KConfigPageMounting(this);
0065     QScrollArea *mountingArea = new QScrollArea(this);
0066     mountingArea->setWidget(m_mountingPage);
0067     mountingArea->setWidgetResizable(true);
0068     mountingArea->setFrameStyle(QFrame::NoFrame);
0069 
0070     m_authenticationPage = new Smb4KConfigPageAuthentication(this);
0071     QScrollArea *authenticationArea = new QScrollArea(this);
0072     authenticationArea->setWidget(m_authenticationPage);
0073     authenticationArea->setWidgetResizable(true);
0074     authenticationArea->setFrameStyle(QFrame::NoFrame);
0075 
0076     m_synchronizationPage = new Smb4KConfigPageSynchronization(this);
0077     QScrollArea *synchronizationArea = new QScrollArea(this);
0078     synchronizationArea->setWidget(m_synchronizationPage);
0079     synchronizationArea->setWidgetResizable(true);
0080     synchronizationArea->setFrameStyle(QFrame::NoFrame);
0081 
0082     m_synchronizationPage->setEnabled(!QStandardPaths::findExecutable(QStringLiteral("rsync")).isEmpty());
0083 
0084     m_customSettingsPage = new Smb4KConfigPageCustomSettings(this);
0085     QScrollArea *customSettingsArea = new QScrollArea(this);
0086     customSettingsArea->setWidget(m_customSettingsPage);
0087     customSettingsArea->setWidgetResizable(true);
0088     customSettingsArea->setFrameStyle(QFrame::NoFrame);
0089 
0090     m_profilesPage = new Smb4KConfigPageProfiles(this);
0091     QScrollArea *profilesArea = new QScrollArea(this);
0092     profilesArea->setWidget(m_profilesPage);
0093     profilesArea->setWidgetResizable(true);
0094     profilesArea->setFrameStyle(QFrame::NoFrame);
0095 
0096     m_bookmarksPage = new Smb4KConfigPageBookmarks(this);
0097     QScrollArea *bookmarksArea = new QScrollArea(this);
0098     bookmarksArea->setWidget(m_bookmarksPage);
0099     bookmarksArea->setWidgetResizable(true);
0100     bookmarksArea->setFrameStyle(QFrame::NoFrame);
0101 
0102     KConfigGroup bookmarkGroup(Smb4KSettings::self()->config(), QStringLiteral("BookmarkEditor"));
0103 
0104     if (bookmarkGroup.exists()) {
0105         QMap<QString, QStringList> completionItems;
0106         completionItems[QStringLiteral("CategoryCompletion")] = bookmarkGroup.readEntry("CategoryCompletion", QStringList());
0107         completionItems[QStringLiteral("LabelCompletion")] = bookmarkGroup.readEntry("LabelCompletion", QStringList());
0108         // For backward compatibility (since Smb4K 3.3.0)
0109         if (bookmarkGroup.hasKey(QStringLiteral("IPCompletion"))) {
0110             completionItems[QStringLiteral("IpAddressCompletion")] = bookmarkGroup.readEntry("IPCompletion", QStringList());
0111             bookmarkGroup.deleteEntry("IPCompletion");
0112         } else {
0113             completionItems[QStringLiteral("IpAddressCompletion")] = bookmarkGroup.readEntry("IpAddressCompletion", QStringList());
0114         }
0115         completionItems[QStringLiteral("LoginCompletion")] = bookmarkGroup.readEntry("LoginCompletion", QStringList());
0116         completionItems[QStringLiteral("WorkgroupCompletion")] = bookmarkGroup.readEntry("WorkgroupCompletion", QStringList());
0117 
0118         m_bookmarksPage->setCompletionItems(completionItems);
0119     }
0120 
0121     //
0122     // Pages to the configuration dialog
0123     //
0124     m_userInterface = addPage(userInterfaceArea, Smb4KSettings::self(), i18n("User Interface"), QStringLiteral("preferences-desktop"));
0125     m_network = addPage(networkArea, Smb4KSettings::self(), i18n("Network"), QStringLiteral("preferences-system-network-server-share-windows"));
0126     m_mounting = addPage(mountingArea, Smb4KMountSettings::self(), i18n("Mounting"), QStringLiteral("media-mount"));
0127     m_authentication = addPage(authenticationArea, Smb4KSettings::self(), i18n("Authentication"), QStringLiteral("preferences-desktop-user-password"));
0128     m_synchronization = addPage(synchronizationArea, Smb4KSettings::self(), i18n("Synchronization"), QStringLiteral("folder-sync"));
0129     m_bookmarks = addPage(bookmarksArea, Smb4KSettings::self(), i18n("Bookmarks"), QStringLiteral("bookmarks"));
0130     m_customSettings = addPage(customSettingsArea, Smb4KSettings::self(), i18n("Custom Settings"), QStringLiteral("settings-configure"));
0131     m_profiles = addPage(profilesArea, Smb4KSettings::self(), i18n("Profiles"), QStringLiteral("preferences-system-users"));
0132 
0133     //
0134     // Connections
0135     //
0136     connect(m_customSettingsPage, &Smb4KConfigPageCustomSettings::customSettingsModified, this, &Smb4KConfigDialog::slotEnableApplyButton);
0137     connect(m_authenticationPage, &Smb4KConfigPageAuthentication::walletEntriesModified, this, &Smb4KConfigDialog::slotEnableApplyButton);
0138     connect(m_bookmarksPage, &Smb4KConfigPageBookmarks::bookmarksModified, this, &Smb4KConfigDialog::slotEnableApplyButton);
0139     connect(this, &Smb4KConfigDialog::currentPageChanged, this, &Smb4KConfigDialog::slotCheckPage);
0140 
0141     //
0142     // Dialog size
0143     //
0144     create();
0145     windowHandle()->resize(QSize(800, 600));
0146 
0147     KConfigGroup group(Smb4KSettings::self()->config(), QStringLiteral("ConfigDialog"));
0148     KWindowConfig::restoreWindowSize(windowHandle(), group);
0149     resize(windowHandle()->size()); // workaround for QTBUG-40584
0150 }
0151 
0152 bool Smb4KConfigDialog::checkSettings(KPageWidgetItem *page)
0153 {
0154     QString errorMessage = i18n("<qt>An incorrect setting has been found. You are now taken to the corresponding configuration page to fix it.</qt>");
0155 
0156     if (!page || page == m_mounting) {
0157         if (!m_mountingPage->checkSettings()) {
0158             KMessageBox::error(this, errorMessage);
0159             setCurrentPage(m_mounting);
0160             return false;
0161         }
0162     }
0163 
0164     if (!page || page == m_synchronization) {
0165         if (!m_synchronizationPage->checkSettings()) {
0166             KMessageBox::error(this, errorMessage);
0167             setCurrentPage(m_synchronization);
0168             return false;
0169         }
0170     }
0171 
0172     return true;
0173 }
0174 
0175 /////////////////////////////////////////////////////////////////////////////
0176 // SLOT IMPLEMENTATIONS
0177 /////////////////////////////////////////////////////////////////////////////
0178 
0179 void Smb4KConfigDialog::updateSettings()
0180 {
0181     m_authenticationPage->saveLoginCredentials();
0182     m_customSettingsPage->saveCustomSettings();
0183 
0184     m_bookmarksPage->saveBookmarks();
0185     QMap<QString, QStringList> completionItems = m_bookmarksPage->completionItems();
0186 
0187     KConfigGroup bookmarkGroup(Smb4KSettings::self()->config(), QStringLiteral("BookmarkEditor"));
0188 
0189     bookmarkGroup.writeEntry("CategoryCompletion", completionItems[QStringLiteral("CategoryCompletion")]);
0190     bookmarkGroup.writeEntry("LabelCompletion", completionItems[QStringLiteral("LabelCompletion")]);
0191     bookmarkGroup.writeEntry("IpAddressCompletion", completionItems[QStringLiteral("IpAddressCompletion")]);
0192     bookmarkGroup.writeEntry("LoginCompletion", completionItems[QStringLiteral("LoginCompletion")]);
0193     bookmarkGroup.writeEntry("WorkgroupCompletion", completionItems[QStringLiteral("WorkgroupCompletion")]);
0194 
0195     if (m_profilesPage->profilesChanged()) {
0196         m_profilesPage->applyChanges();
0197         m_customSettingsPage->loadCustomSettings();
0198         m_bookmarksPage->loadBookmarks();
0199     }
0200 
0201     (void)checkSettings();
0202 
0203     KConfigGroup group(Smb4KSettings::self()->config(), QStringLiteral("ConfigDialog"));
0204     KWindowConfig::saveWindowSize(windowHandle(), group);
0205 
0206     KConfigDialog::updateSettings();
0207 }
0208 
0209 void Smb4KConfigDialog::slotEnableApplyButton()
0210 {
0211     // FIXME: Can this be moved to updateButtons()?
0212     bool enable = false;
0213 
0214     enable = m_authenticationPage->loginCredentialsChanged();
0215 
0216     if (!enable) {
0217         enable = m_customSettingsPage->customSettingsChanged();
0218     }
0219 
0220     if (!enable && m_bookmarksPage) {
0221         enable = m_bookmarksPage->bookmarksChanged();
0222     }
0223 
0224     QPushButton *applyButton = buttonBox()->button(QDialogButtonBox::Apply);
0225 
0226     if (applyButton) {
0227         applyButton->setEnabled(enable);
0228     }
0229 }
0230 
0231 void Smb4KConfigDialog::slotCheckPage(KPageWidgetItem *current, KPageWidgetItem *before)
0232 {
0233     Q_UNUSED(current);
0234     checkSettings(before);
0235 }
0236 
0237 #include "smb4kconfigdialog.moc"