File indexing completed on 2023-12-10 04:59:14

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