File indexing completed on 2024-11-03 07:47:06
0001 /* 0002 The configuration page for the profiles 0003 0004 SPDX-FileCopyrightText: 2014-2023 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 <QPointer> 0018 #include <QVBoxLayout> 0019 0020 // KDE includes 0021 #include <KLineEdit> 0022 #include <KLocalizedString> 0023 0024 struct ProfileContainer { 0025 QString initialName; 0026 QString currentName; 0027 bool removed; 0028 bool renamed; 0029 bool added; 0030 }; 0031 0032 Smb4KConfigPageProfiles::Smb4KConfigPageProfiles(QWidget *parent) 0033 : QWidget(parent) 0034 { 0035 m_profilesChanged = false; 0036 0037 QStringList profiles = Smb4KSettings::profilesList(); 0038 0039 for (const QString &profile : qAsConst(profiles)) { 0040 ProfileContainer p; 0041 p.initialName = profile; 0042 p.currentName = profile; 0043 p.removed = false; 0044 p.renamed = false; 0045 p.added = false; 0046 0047 m_profiles << p; 0048 } 0049 0050 QVBoxLayout *layout = new QVBoxLayout(this); 0051 0052 QGroupBox *settingsBox = new QGroupBox(i18n("Settings"), this); 0053 QVBoxLayout *settingsBoxLayout = new QVBoxLayout(settingsBox); 0054 0055 m_useProfiles = new QCheckBox(Smb4KSettings::self()->useProfilesItem()->label(), settingsBox); 0056 m_useProfiles->setObjectName(QStringLiteral("kcfg_UseProfiles")); 0057 0058 settingsBoxLayout->addWidget(m_useProfiles); 0059 0060 m_transferToFirstProfile = new QCheckBox(Smb4KSettings::self()->transferToFirstProfileItem()->label(), settingsBox); 0061 m_transferToFirstProfile->setObjectName(QStringLiteral("kcfg_TransferToFirstProfile")); 0062 0063 settingsBoxLayout->addWidget(m_transferToFirstProfile); 0064 0065 m_makeAllDataAvailable = new QCheckBox(Smb4KSettings::self()->makeAllDataAvailableItem()->label(), settingsBox); 0066 m_makeAllDataAvailable->setObjectName(QStringLiteral("kcfg_MakeAllDataAvailable")); 0067 0068 settingsBoxLayout->addWidget(m_makeAllDataAvailable); 0069 0070 layout->addWidget(settingsBox); 0071 0072 QGroupBox *profilesBox = new QGroupBox(i18n("Profiles"), this); 0073 QVBoxLayout *profilesBoxLayout = new QVBoxLayout(profilesBox); 0074 0075 m_profilesWidget = new KEditListWidget(profilesBox); 0076 m_profilesWidget->setObjectName(QStringLiteral("kcfg_ProfilesList")); 0077 m_profilesWidget->setEnabled(Smb4KSettings::self()->useProfiles()); 0078 0079 profilesBoxLayout->addWidget(m_profilesWidget); 0080 0081 layout->addWidget(profilesBox); 0082 0083 connect(m_useProfiles, &QCheckBox::toggled, this, &Smb4KConfigPageProfiles::slotProfileUsageChanged); 0084 connect(m_profilesWidget, &KEditListWidget::added, this, &Smb4KConfigPageProfiles::slotProfileAdded); 0085 connect(m_profilesWidget, &KEditListWidget::removed, this, &Smb4KConfigPageProfiles::slotProfileRemoved); 0086 connect(m_profilesWidget->lineEdit(), &QLineEdit::editingFinished, this, &Smb4KConfigPageProfiles::slotProfileChanged); 0087 } 0088 0089 Smb4KConfigPageProfiles::~Smb4KConfigPageProfiles() 0090 { 0091 } 0092 0093 void Smb4KConfigPageProfiles::applyChanges() 0094 { 0095 if (m_profilesChanged) { 0096 QMutableListIterator<ProfileContainer> it(m_profiles); 0097 0098 while (it.hasNext()) { 0099 ProfileContainer p = it.next(); 0100 0101 if (!p.removed && !p.renamed && !p.added) { 0102 continue; 0103 } 0104 0105 if (p.removed) { 0106 Smb4KProfileManager::self()->removeProfile(p.initialName); 0107 it.remove(); 0108 } 0109 0110 if (p.renamed && !p.added) { 0111 // When we just rename a profile, do not use the migration dialog 0112 Smb4KProfileManager::self()->migrateProfile(p.initialName, p.currentName); 0113 it.value().initialName = p.currentName; 0114 it.value().renamed = false; 0115 } 0116 0117 if (p.added) { 0118 // We do not need to do anything here, because the new profile 0119 // will be saved by KConfig XT. 0120 it.value().initialName = p.currentName; 0121 it.value().added = false; 0122 it.value().renamed = false; 0123 } 0124 } 0125 0126 // Migrate all data from the default profile to the first profile in the list 0127 if (m_useProfiles->isChecked() && m_transferToFirstProfile->isChecked() && m_useProfiles->isChecked() != Smb4KSettings::useProfiles()) { 0128 QString firstProfile = m_profilesWidget->items().first(); 0129 Smb4KProfileManager::self()->migrateProfile(QStringLiteral(""), firstProfile); 0130 } 0131 0132 // Migrate all data from all profiles to the default profile 0133 if (!m_useProfiles->isChecked() && m_makeAllDataAvailable->isChecked() && m_useProfiles->isChecked() != Smb4KSettings::useProfiles()) { 0134 Smb4KProfileManager::self()->migrateProfile(QStringLiteral("*"), QStringLiteral("")); 0135 } 0136 0137 m_profilesChanged = false; 0138 } 0139 } 0140 0141 bool Smb4KConfigPageProfiles::profilesChanged() const 0142 { 0143 return m_profilesChanged; 0144 } 0145 0146 void Smb4KConfigPageProfiles::slotProfileUsageChanged(bool checked) 0147 { 0148 m_profilesChanged = (checked != Smb4KSettings::useProfiles()); 0149 m_profilesWidget->setEnabled(checked); 0150 } 0151 0152 void Smb4KConfigPageProfiles::slotProfileAdded(const QString &text) 0153 { 0154 Q_UNUSED(text); 0155 0156 ProfileContainer p; 0157 p.initialName = text; 0158 p.currentName = text; 0159 p.removed = false; 0160 p.renamed = false; 0161 p.added = true; 0162 0163 m_profiles << p; 0164 0165 m_profilesChanged = true; 0166 } 0167 0168 void Smb4KConfigPageProfiles::slotProfileRemoved(const QString &text) 0169 { 0170 for (int i = 0; i < m_profiles.size(); i++) { 0171 if (m_profiles.at(i).initialName == text || m_profiles.at(i).currentName == text) { 0172 m_profiles[i].removed = true; 0173 break; 0174 } 0175 } 0176 0177 m_profilesChanged = true; 0178 } 0179 0180 void Smb4KConfigPageProfiles::slotProfileChanged() 0181 { 0182 QStringList listedProfiles = m_profilesWidget->items(); 0183 int renamedIndex = -1; 0184 0185 for (int i = 0; i < m_profiles.size(); i++) { 0186 if (!m_profiles.at(i).removed) { 0187 int index = listedProfiles.indexOf(m_profiles.at(i).currentName); 0188 0189 if (index == -1) { 0190 renamedIndex = i; 0191 break; 0192 } else { 0193 listedProfiles.removeAt(index); 0194 } 0195 } 0196 } 0197 0198 if (renamedIndex != -1) { 0199 QString newName = listedProfiles.first(); 0200 m_profiles[renamedIndex].currentName = newName; 0201 m_profiles[renamedIndex].renamed = true; 0202 0203 m_profilesChanged = true; 0204 } 0205 }