File indexing completed on 2023-12-03 05:04:25
0001 /* 0002 * Editor dialog for the custom settings 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 "smb4kcustomsettingseditor.h" 0010 #include "core/smb4kcustomoptionsmanager.h" 0011 #include "core/smb4khomesshareshandler.h" 0012 #include "core/smb4kprofilemanager.h" 0013 #include "core/smb4ksettings.h" 0014 0015 // Qt includes 0016 #include <QDialogButtonBox> 0017 #include <QFrame> 0018 #include <QVBoxLayout> 0019 #include <QWindow> 0020 0021 // KDE includes 0022 #include <KConfigGroup> 0023 #include <KLocalizedString> 0024 #include <KWindowConfig> 0025 0026 Smb4KCustomSettingsEditor::Smb4KCustomSettingsEditor(QWidget *parent) 0027 : QDialog(parent) 0028 { 0029 setWindowTitle(i18n("Custom Settings Editor")); 0030 setAttribute(Qt::WA_DeleteOnClose); 0031 0032 m_customSettings = nullptr; 0033 m_defaultsRestored = false; 0034 m_savingCustomSettings = false; 0035 m_changedCustomSettings = false; 0036 0037 QVBoxLayout *layout = new QVBoxLayout(this); 0038 0039 QWidget *descriptionWidget = new QWidget(this); 0040 QHBoxLayout *descriptionWidgetLayout = new QHBoxLayout(descriptionWidget); 0041 0042 QLabel *descriptionPixmap = new QLabel(descriptionWidget); 0043 descriptionPixmap->setPixmap(KDE::icon(QStringLiteral("media-mount")).pixmap(KIconLoader::SizeHuge)); 0044 descriptionPixmap->setAlignment(Qt::AlignVCenter); 0045 descriptionPixmap->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); 0046 0047 descriptionWidgetLayout->addWidget(descriptionPixmap); 0048 0049 m_descriptionText = new QLabel(descriptionWidget); 0050 m_descriptionText->setWordWrap(true); 0051 m_descriptionText->setAlignment(Qt::AlignBottom); 0052 m_descriptionText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 0053 m_descriptionText->setText(i18n("No network item was set.")); 0054 0055 descriptionWidgetLayout->addWidget(m_descriptionText); 0056 0057 layout->addWidget(descriptionWidget); 0058 layout->addSpacing(layout->spacing()); 0059 0060 m_editorWidget = new Smb4KCustomSettingsEditorWidget(this); 0061 connect(m_editorWidget, &Smb4KCustomSettingsEditorWidget::edited, this, &Smb4KCustomSettingsEditor::slotCustomSettingsEdited); 0062 0063 QDialogButtonBox *buttonBox = new QDialogButtonBox(this); 0064 m_resetButton = buttonBox->addButton(QDialogButtonBox::RestoreDefaults); 0065 0066 m_saveButton = buttonBox->addButton(QDialogButtonBox::Save); 0067 m_saveButton->setEnabled(false); 0068 m_saveButton->setShortcut(QKeySequence::Save); 0069 0070 m_cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel); 0071 m_cancelButton->setShortcut(QKeySequence::Cancel); 0072 0073 connect(m_resetButton, &QPushButton::clicked, this, &Smb4KCustomSettingsEditor::slotRestoreDefaults); 0074 connect(m_saveButton, &QPushButton::clicked, this, &Smb4KCustomSettingsEditor::slotSaveCustomSettings); 0075 connect(m_cancelButton, &QPushButton::clicked, this, &Smb4KCustomSettingsEditor::reject); 0076 0077 layout->addWidget(m_editorWidget); 0078 layout->addWidget(buttonBox); 0079 0080 connect(Smb4KCustomOptionsManager::self(), &Smb4KCustomOptionsManager::updated, this, &Smb4KCustomSettingsEditor::slotCustomSettingsUpdated); 0081 0082 setMinimumWidth(sizeHint().width() > 350 ? sizeHint().width() : 350); 0083 0084 create(); 0085 0086 KConfigGroup group(Smb4KSettings::self()->config(), "CustomOptionsDialog"); 0087 QSize dialogSize; 0088 0089 // FIXME: Insert completion objects? 0090 0091 if (group.exists()) { 0092 KWindowConfig::restoreWindowSize(windowHandle(), group); 0093 dialogSize = windowHandle()->size(); 0094 } else { 0095 dialogSize = sizeHint(); 0096 } 0097 0098 resize(dialogSize); // workaround for QTBUG-40584 0099 } 0100 0101 Smb4KCustomSettingsEditor::~Smb4KCustomSettingsEditor() 0102 { 0103 } 0104 0105 bool Smb4KCustomSettingsEditor::setNetworkItem(NetworkItemPtr networkItem) 0106 { 0107 Q_ASSERT(networkItem); 0108 0109 bool setCustomSettings = false; 0110 0111 if (networkItem) { 0112 switch (networkItem->type()) { 0113 case Host: { 0114 HostPtr host = networkItem.staticCast<Smb4KHost>(); 0115 m_descriptionText->setText(i18n("Define custom settings for host <b>%1</b> and all the shares it provides.", host->hostName())); 0116 0117 m_customSettings = Smb4KCustomOptionsManager::self()->findOptions(host); 0118 0119 if (!m_customSettings) { 0120 m_customSettings = OptionsPtr(new Smb4KCustomOptions(host.data())); 0121 m_customSettings->setProfile(Smb4KProfileManager::self()->activeProfile()); 0122 } 0123 0124 m_editorWidget->setCustomSettings(*m_customSettings.data()); 0125 setCustomSettings = true; 0126 0127 break; 0128 } 0129 case Share: { 0130 SharePtr share = networkItem.staticCast<Smb4KShare>(); 0131 m_descriptionText->setText(i18n("Define custom settings for share <b>%1</b>.", share->displayString())); 0132 0133 if (!share->isPrinter()) { 0134 if (share->isHomesShare()) { 0135 if (!Smb4KHomesSharesHandler::self()->specifyUser(share, true)) { 0136 return setCustomSettings; 0137 } 0138 } 0139 0140 m_customSettings = Smb4KCustomOptionsManager::self()->findOptions(share); 0141 0142 if (!m_customSettings) { 0143 m_customSettings = OptionsPtr(new Smb4KCustomOptions(share.data())); 0144 m_customSettings->setProfile(Smb4KProfileManager::self()->activeProfile()); 0145 0146 // Get rid of the 'homes' share 0147 if (share->isHomesShare()) { 0148 m_customSettings->setUrl(share->homeUrl()); 0149 } 0150 } 0151 0152 m_editorWidget->setCustomSettings(*m_customSettings.data()); 0153 setCustomSettings = true; 0154 } 0155 0156 break; 0157 } 0158 default: { 0159 break; 0160 } 0161 } 0162 } 0163 0164 return setCustomSettings; 0165 } 0166 0167 void Smb4KCustomSettingsEditor::slotRestoreDefaults() 0168 { 0169 Smb4KCustomOptions defaultCustomSettings; 0170 Smb4KCustomOptions customSettings = *m_customSettings.data(); 0171 customSettings.update(&defaultCustomSettings); 0172 m_editorWidget->setCustomSettings(customSettings); 0173 m_resetButton->setEnabled(false); 0174 m_defaultsRestored = true; 0175 } 0176 0177 void Smb4KCustomSettingsEditor::slotSaveCustomSettings() 0178 { 0179 OptionsPtr tempCustomSettings = OptionsPtr(new Smb4KCustomOptions(m_editorWidget->getCustomSettings())); 0180 m_customSettings.swap(tempCustomSettings); 0181 0182 m_savingCustomSettings = true; 0183 Smb4KCustomOptionsManager::self()->addCustomOptions(m_customSettings, true); 0184 m_savingCustomSettings = false; 0185 0186 KConfigGroup group(Smb4KSettings::self()->config(), "CustomOptionsDialog"); 0187 KWindowConfig::saveWindowSize(windowHandle(), group); 0188 0189 // FIXME: Save completion objects? 0190 0191 accept(); 0192 } 0193 0194 void Smb4KCustomSettingsEditor::slotCustomSettingsEdited(bool changed) 0195 { 0196 m_saveButton->setEnabled(changed || m_defaultsRestored); 0197 m_resetButton->setEnabled((changed && m_defaultsRestored) || !m_defaultsRestored); 0198 0199 m_changedCustomSettings = changed; 0200 } 0201 0202 void Smb4KCustomSettingsEditor::slotCustomSettingsUpdated() 0203 { 0204 if (!m_savingCustomSettings) { 0205 OptionsPtr customSettings = Smb4KCustomOptionsManager::self()->findOptions(m_customSettings->url()); 0206 0207 // Only reload existing custom settings, because only those could have 0208 // been changed externally. 0209 if (customSettings && !m_changedCustomSettings && !m_defaultsRestored) { 0210 m_customSettings = customSettings; 0211 m_editorWidget->setCustomSettings(*m_customSettings.data()); 0212 } 0213 } 0214 }