File indexing completed on 2023-11-26 11:42:32
0001 /* 0002 The configuration page for the profiles 0003 0004 SPDX-FileCopyrightText: 2014-2022 Alexander Reinholdt <alexander.reinholdt@kdemail.net> 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 // application specific includes 0009 #include "smb4kconfigpageprofiles.h" 0010 #include "core/smb4kprofilemanager.h" 0011 #include "core/smb4ksettings.h" 0012 0013 // Qt includes 0014 #include <QCheckBox> 0015 #include <QGroupBox> 0016 #include <QLabel> 0017 #include <QVBoxLayout> 0018 0019 // KDE includes 0020 #include <KLineEdit> 0021 #include <KLocalizedString> 0022 0023 Smb4KConfigPageProfiles::Smb4KConfigPageProfiles(QWidget *parent) 0024 : QWidget(parent) 0025 { 0026 m_profilesChanged = false; 0027 0028 // 0029 // Layout 0030 // 0031 QVBoxLayout *layout = new QVBoxLayout(this); 0032 0033 // 0034 // Profile Settings 0035 // 0036 QGroupBox *settingsBox = new QGroupBox(i18n("Settings"), this); 0037 QVBoxLayout *settingsBoxLayout = new QVBoxLayout(settingsBox); 0038 0039 QCheckBox *useProfiles = new QCheckBox(Smb4KSettings::self()->useProfilesItem()->label(), settingsBox); 0040 useProfiles->setObjectName(QStringLiteral("kcfg_UseProfiles")); 0041 0042 settingsBoxLayout->addWidget(useProfiles); 0043 0044 QCheckBox *useAssistant = new QCheckBox(Smb4KSettings::self()->useMigrationAssistantItem()->label(), settingsBox); 0045 useAssistant->setObjectName(QStringLiteral("kcfg_UseMigrationAssistant")); 0046 0047 settingsBoxLayout->addWidget(useAssistant); 0048 0049 layout->addWidget(settingsBox); 0050 0051 // 0052 // List of profiles 0053 // 0054 QGroupBox *profilesBox = new QGroupBox(i18n("Profiles"), this); 0055 QVBoxLayout *profilesBoxLayout = new QVBoxLayout(profilesBox); 0056 0057 m_profiles = new KEditListWidget(profilesBox); 0058 m_profiles->setObjectName(QStringLiteral("kcfg_ProfilesList")); 0059 m_profiles->setEnabled(Smb4KSettings::self()->useProfiles()); 0060 0061 profilesBoxLayout->addWidget(m_profiles); 0062 0063 layout->addWidget(profilesBox); 0064 0065 // 0066 // Connections 0067 // 0068 connect(useProfiles, SIGNAL(stateChanged(int)), this, SLOT(slotEnableWidget(int))); 0069 connect(m_profiles, SIGNAL(removed(QString)), this, SLOT(slotProfileRemoved(QString))); 0070 connect(m_profiles->lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotProfileChanged())); 0071 } 0072 0073 Smb4KConfigPageProfiles::~Smb4KConfigPageProfiles() 0074 { 0075 } 0076 0077 void Smb4KConfigPageProfiles::applyChanges() 0078 { 0079 if (m_profilesChanged) { 0080 // Remove the profiles 0081 if (!m_removed.isEmpty()) { 0082 Smb4KProfileManager::self()->removeProfiles(m_removed); 0083 m_removed.clear(); 0084 } 0085 0086 // Rename the profiles 0087 if (!m_renamed.isEmpty()) { 0088 Smb4KProfileManager::self()->migrateProfiles(m_renamed); 0089 m_renamed.clear(); 0090 } 0091 0092 m_profilesChanged = false; 0093 } 0094 } 0095 0096 bool Smb4KConfigPageProfiles::profilesChanged() const 0097 { 0098 return m_profilesChanged; 0099 } 0100 0101 void Smb4KConfigPageProfiles::slotEnableWidget(int state) 0102 { 0103 switch (state) { 0104 case Qt::Unchecked: { 0105 m_profiles->setEnabled(false); 0106 break; 0107 } 0108 case Qt::Checked: { 0109 m_profiles->setEnabled(true); 0110 break; 0111 } 0112 default: { 0113 break; 0114 } 0115 } 0116 } 0117 0118 void Smb4KConfigPageProfiles::slotProfileAdded(const QString &text) 0119 { 0120 Q_UNUSED(text); 0121 m_profilesChanged = true; 0122 } 0123 0124 void Smb4KConfigPageProfiles::slotProfileRemoved(const QString &text) 0125 { 0126 // If the removed profile was renamed before, remove it from 0127 // the list. 0128 QMutableListIterator<QPair<QString, QString>> it(m_renamed); 0129 0130 while (it.hasNext()) { 0131 QPair<QString, QString> entry = it.next(); 0132 0133 if (entry.first == text || entry.second == text) { 0134 it.remove(); 0135 } 0136 } 0137 0138 m_removed << text; 0139 m_profilesChanged = true; 0140 } 0141 0142 void Smb4KConfigPageProfiles::slotProfileChanged() 0143 { 0144 QStringList savedProfiles = Smb4KProfileManager::self()->profilesList(); 0145 QStringList currentProfiles = m_profiles->items(); 0146 0147 if (savedProfiles.size() == currentProfiles.size()) { 0148 QMutableStringListIterator it(savedProfiles); 0149 0150 while (it.hasNext()) { 0151 QString entry = it.next(); 0152 int index = currentProfiles.indexOf(entry); 0153 0154 if (index != -1) { 0155 currentProfiles.removeAt(index); 0156 it.remove(); 0157 } 0158 } 0159 0160 if (!savedProfiles.isEmpty() && !currentProfiles.isEmpty()) { 0161 // Take care that multiple renamings will have the correct 0162 // result. 0163 bool write = true; 0164 0165 for (int i = 0; i < m_renamed.size(); ++i) { 0166 if (savedProfiles.first() == m_renamed.at(i).first) { 0167 QPair<QString, QString> pair = static_cast<QPair<QString, QString>>(m_renamed.at(i)); 0168 pair.second = currentProfiles.first(); 0169 write = false; 0170 break; 0171 } 0172 } 0173 0174 // Write the renamed profile to the list, if necessary. 0175 if (write) { 0176 QPair<QString, QString> renamed(savedProfiles.first(), currentProfiles.first()); 0177 m_renamed << renamed; 0178 m_profilesChanged = true; 0179 } 0180 } 0181 } 0182 }