File indexing completed on 2024-09-29 04:30:04
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/smb4kcustomsettingsmanager.h" 0011 #include "core/smb4kprofilemanager.h" 0012 #include "core/smb4ksettings.h" 0013 #include "smb4khomesuserdialog.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(Smb4KCustomSettingsManager::self(), &Smb4KCustomSettingsManager::updated, this, &Smb4KCustomSettingsEditor::slotCustomSettingsUpdated); 0081 0082 setMinimumWidth(sizeHint().width() > 350 ? sizeHint().width() : 350); 0083 0084 create(); 0085 0086 KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("CustomSettingsDialog")); 0087 QSize dialogSize; 0088 0089 // FIXME: Insert completion objects? 0090 0091 if (dialogGroup.exists()) { 0092 KWindowConfig::restoreWindowSize(windowHandle(), dialogGroup); 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 if (networkItem) { 0110 switch (networkItem->type()) { 0111 case Host: { 0112 HostPtr host = networkItem.staticCast<Smb4KHost>(); 0113 m_descriptionText->setText(i18n("Define custom settings for host <b>%1</b> and all the shares it provides.", host->hostName())); 0114 0115 m_customSettings = Smb4KCustomSettingsManager::self()->findCustomSettings(host); 0116 0117 if (!m_customSettings) { 0118 m_customSettings = CustomSettingsPtr(new Smb4KCustomSettings(host.data())); 0119 m_customSettings->setProfile(Smb4KProfileManager::self()->activeProfile()); 0120 } 0121 0122 m_editorWidget->setCustomSettings(*m_customSettings.data()); 0123 break; 0124 } 0125 case Share: { 0126 SharePtr share = networkItem.staticCast<Smb4KShare>(); 0127 0128 if (!share->isPrinter()) { 0129 if (share->isHomesShare()) { 0130 QPointer<Smb4KHomesUserDialog> homesUserDialog = new Smb4KHomesUserDialog(this); 0131 bool proceed = false; 0132 0133 if (homesUserDialog->setShare(share)) { 0134 // We want to get a return value here, so we use exec() 0135 if (homesUserDialog->exec() == QDialog::Accepted) { 0136 proceed = true; 0137 } 0138 } 0139 0140 delete homesUserDialog; 0141 0142 if (!proceed) { 0143 return false; 0144 } 0145 } 0146 0147 m_descriptionText->setText(i18n("Define custom settings for share <b>%1</b>.", share->displayString(true))); 0148 m_customSettings = Smb4KCustomSettingsManager::self()->findCustomSettings(share); 0149 0150 if (!m_customSettings) { 0151 m_customSettings = CustomSettingsPtr(new Smb4KCustomSettings(share.data())); 0152 m_customSettings->setProfile(Smb4KProfileManager::self()->activeProfile()); 0153 0154 // Get rid of the 'homes' share 0155 if (share->isHomesShare()) { 0156 m_customSettings->setUrl(share->homeUrl()); 0157 } 0158 } 0159 0160 m_editorWidget->setCustomSettings(*m_customSettings.data()); 0161 } 0162 0163 break; 0164 } 0165 default: { 0166 break; 0167 } 0168 } 0169 } 0170 0171 return true; 0172 } 0173 0174 void Smb4KCustomSettingsEditor::slotRestoreDefaults() 0175 { 0176 Smb4KCustomSettings defaultCustomSettings; 0177 Smb4KCustomSettings customSettings = *m_customSettings.data(); 0178 customSettings.update(&defaultCustomSettings); 0179 m_editorWidget->setCustomSettings(customSettings); 0180 m_resetButton->setEnabled(false); 0181 m_defaultsRestored = true; 0182 } 0183 0184 void Smb4KCustomSettingsEditor::slotSaveCustomSettings() 0185 { 0186 CustomSettingsPtr tempCustomSettings = CustomSettingsPtr(new Smb4KCustomSettings(m_editorWidget->getCustomSettings())); 0187 m_customSettings.swap(tempCustomSettings); 0188 0189 m_savingCustomSettings = true; 0190 Smb4KCustomSettingsManager::self()->addCustomSettings(m_customSettings); 0191 m_savingCustomSettings = false; 0192 0193 KConfigGroup dialogGroup(Smb4KSettings::self()->config(), QStringLiteral("CustomSettingsDialog")); 0194 KWindowConfig::saveWindowSize(windowHandle(), dialogGroup); 0195 0196 // FIXME: Save completion objects? 0197 0198 accept(); 0199 } 0200 0201 void Smb4KCustomSettingsEditor::slotCustomSettingsEdited(bool changed) 0202 { 0203 m_saveButton->setEnabled(changed || m_defaultsRestored); 0204 m_resetButton->setEnabled((changed && m_defaultsRestored) || !m_defaultsRestored); 0205 0206 m_changedCustomSettings = changed; 0207 } 0208 0209 void Smb4KCustomSettingsEditor::slotCustomSettingsUpdated() 0210 { 0211 if (!m_savingCustomSettings) { 0212 CustomSettingsPtr customSettings = Smb4KCustomSettingsManager::self()->findCustomSettings(m_customSettings->url()); 0213 0214 // Only reload existing custom settings, because only those could have 0215 // been changed externally. 0216 if (customSettings && !m_changedCustomSettings && !m_defaultsRestored) { 0217 m_customSettings = customSettings; 0218 m_editorWidget->setCustomSettings(*m_customSettings.data()); 0219 } 0220 } 0221 }