File indexing completed on 2024-04-21 15:42:50
0001 /*************************************************************************** 0002 This class manages the profiles that were defined by the user. 0003 ------------------- 0004 begin : Mi Aug 06 2014 0005 copyright : (C) 2014-2019 by Alexander Reinholdt 0006 email : alexander.reinholdt@kdemail.net 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * This program is free software; you can redistribute it and/or modify * 0011 * it under the terms of the GNU General Public License as published by * 0012 * the Free Software Foundation; either version 2 of the License, or * 0013 * (at your option) any later version. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, but * 0016 * WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0018 * General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program; if not, write to the * 0022 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* 0023 * MA 02110-1335, USA * 0024 ***************************************************************************/ 0025 0026 #ifdef HAVE_CONFIG_H 0027 #include <config.h> 0028 #endif 0029 0030 // application specific includes 0031 #include "smb4kprofilemanager.h" 0032 #include "smb4kprofilemanager_p.h" 0033 #include "smb4ksettings.h" 0034 #include "smb4khomesshareshandler.h" 0035 #include "smb4kcustomoptionsmanager.h" 0036 #include "smb4kbookmarkhandler.h" 0037 0038 // Qt includes 0039 #include <QPointer> 0040 #include <QTest> 0041 #include <QApplication> 0042 0043 0044 Q_GLOBAL_STATIC(Smb4KProfileManagerStatic, p); 0045 0046 0047 // 0048 // NOTE: Do not invoke writeConfig() here, because this will/might 0049 // trigger the configChanged() signal which can lead to unwanted 0050 // effects. 0051 // 0052 0053 0054 Smb4KProfileManager::Smb4KProfileManager(QObject* parent) 0055 : QObject(parent), d(new Smb4KProfileManagerPrivate) 0056 { 0057 d->useProfiles = Smb4KSettings::useProfiles(); 0058 0059 if (d->useProfiles) 0060 { 0061 d->profiles = Smb4KSettings::profilesList(); 0062 d->activeProfile = !Smb4KSettings::activeProfile().isEmpty() ? 0063 Smb4KSettings::activeProfile() : 0064 d->profiles.first(); 0065 } 0066 else 0067 { 0068 d->profiles.clear(); 0069 d->activeProfile.clear(); 0070 } 0071 0072 connect(Smb4KSettings::self(), SIGNAL(configChanged()), this, SLOT(slotConfigChanged())); 0073 } 0074 0075 0076 Smb4KProfileManager::~Smb4KProfileManager() 0077 { 0078 } 0079 0080 0081 Smb4KProfileManager* Smb4KProfileManager::self() 0082 { 0083 return &p->instance; 0084 } 0085 0086 0087 void Smb4KProfileManager::setActiveProfile(const QString& name) 0088 { 0089 // 0090 // Check if the active profile is going to be changed. If so, 0091 // notify the program so that things can be done before the 0092 // profile is actually changed. 0093 // 0094 bool change = false; 0095 0096 if (d->useProfiles) 0097 { 0098 if (name != d->activeProfile) 0099 { 0100 emit aboutToChangeProfile(); 0101 change = true; 0102 } 0103 } 0104 else 0105 { 0106 if (!d->activeProfile.isEmpty()) 0107 { 0108 emit aboutToChangeProfile(); 0109 change = true; 0110 } 0111 } 0112 0113 // 0114 // Now change the profile 0115 // 0116 if (change) 0117 { 0118 d->activeProfile = d->useProfiles ? name : QString(); 0119 Smb4KSettings::setActiveProfile(d->activeProfile); 0120 emit activeProfileChanged(d->activeProfile); 0121 } 0122 } 0123 0124 0125 QString Smb4KProfileManager::activeProfile() const 0126 { 0127 return d->activeProfile; 0128 } 0129 0130 0131 QStringList Smb4KProfileManager::profilesList() const 0132 { 0133 return d->useProfiles ? d->profiles : QStringList(); 0134 } 0135 0136 0137 bool Smb4KProfileManager::useProfiles() const 0138 { 0139 return d->useProfiles; 0140 } 0141 0142 0143 void Smb4KProfileManager::migrateProfile(const QString& from, const QString& to) 0144 { 0145 QList<QPair<QString,QString>> list; 0146 list << QPair<QString,QString>(from, to); 0147 migrateProfiles(list); 0148 } 0149 0150 0151 void Smb4KProfileManager::migrateProfiles(const QList<QPair<QString,QString>>& list) 0152 { 0153 if (d->useProfiles || (list.size() == 1 && list.first().second.isEmpty())) 0154 { 0155 for (int i = 0; i < list.size(); ++i) 0156 { 0157 QString from = list.at(i).first; 0158 QString to = list.at(i).second; 0159 0160 if (!to.isEmpty()) 0161 { 0162 // Migrate one/the default profile to another one. 0163 // First exchange the old profile. 0164 for (int j = 0; j < d->profiles.size(); ++j) 0165 { 0166 if (QString::compare(from, d->profiles.at(j), Qt::CaseSensitive) == 0) 0167 { 0168 d->profiles.replace(j, to); 0169 break; 0170 } 0171 } 0172 0173 // Migrate profiles. 0174 Smb4KBookmarkHandler::self()->migrateProfile(from, to); 0175 Smb4KCustomOptionsManager::self()->migrateProfile(from, to); 0176 Smb4KHomesSharesHandler::self()->migrateProfile(from, to); 0177 emit migratedProfile(from, to); 0178 0179 // In case the active profile was modified, rename it according 0180 // the value passed. 0181 if (QString::compare(from, d->activeProfile, Qt::CaseSensitive) == 0) 0182 { 0183 setActiveProfile(to); 0184 } 0185 } 0186 else 0187 { 0188 // Migrate all profiles to the default one. 0189 for (int j = 0; j < d->profiles.size(); ++j) 0190 { 0191 Smb4KBookmarkHandler::self()->migrateProfile(d->profiles.at(j), to); 0192 Smb4KCustomOptionsManager::self()->migrateProfile(d->profiles.at(j), to); 0193 Smb4KHomesSharesHandler::self()->migrateProfile(d->profiles.at(j), to); 0194 emit migratedProfile(d->profiles.at(i), to); 0195 } 0196 } 0197 } 0198 0199 Smb4KSettings::setProfilesList(d->profiles); 0200 emit profilesListChanged(d->profiles); 0201 } 0202 } 0203 0204 0205 void Smb4KProfileManager::removeProfile(const QString& name) 0206 { 0207 QStringList list; 0208 list << name; 0209 removeProfiles(list); 0210 } 0211 0212 0213 void Smb4KProfileManager::removeProfiles(const QStringList& list) 0214 { 0215 if (d->useProfiles) 0216 { 0217 for (int i = 0; i < list.size(); ++i) 0218 { 0219 QString name = list.at(i); 0220 0221 // First remove the profile from the list. 0222 QMutableStringListIterator it(d->profiles); 0223 0224 while (it.hasNext()) 0225 { 0226 QString entry = it.next(); 0227 0228 if (QString::compare(name, entry, Qt::CaseSensitive) == 0) 0229 { 0230 it.remove(); 0231 break; 0232 } 0233 } 0234 0235 if (!d->profiles.isEmpty()) 0236 { 0237 // Ask the user if he/she wants to migrate the entries 0238 // of the removed profile to another one. 0239 if (Smb4KSettings::useMigrationAssistant()) 0240 { 0241 QPointer<Smb4KProfileMigrationDialog> dlg = new Smb4KProfileMigrationDialog(QStringList(name), d->profiles, QApplication::activeWindow()); 0242 0243 if (dlg->exec() == QDialog::Accepted) 0244 { 0245 migrateProfile(dlg->from(), dlg->to()); 0246 } 0247 0248 delete dlg; 0249 } 0250 } 0251 0252 // Remove the profile. 0253 Smb4KBookmarkHandler::self()->removeProfile(name); 0254 Smb4KCustomOptionsManager::self()->removeProfile(name); 0255 Smb4KHomesSharesHandler::self()->removeProfile(name); 0256 emit removedProfile(name); 0257 0258 // Set a new active profile if the user removed the current one. 0259 if (QString::compare(name, d->activeProfile, Qt::CaseSensitive) == 0) 0260 { 0261 setActiveProfile(!d->profiles.isEmpty() ? d->profiles.first() : QString()); 0262 } 0263 } 0264 0265 Smb4KSettings::setProfilesList(d->profiles); 0266 emit profilesListChanged(d->profiles); 0267 } 0268 } 0269 0270 0271 void Smb4KProfileManager::slotConfigChanged() 0272 { 0273 bool usageChanged = false; 0274 0275 // 0276 // Check if the usage of profiles changed 0277 // 0278 if (d->useProfiles != Smb4KSettings::useProfiles()) 0279 { 0280 d->useProfiles = Smb4KSettings::useProfiles(); 0281 emit profileUsageChanged(d->useProfiles); 0282 usageChanged = true; 0283 } 0284 0285 // 0286 // Updated the list of profiles 0287 // 0288 if (d->profiles != Smb4KSettings::profilesList()) 0289 { 0290 d->profiles = Smb4KSettings::profilesList(); 0291 emit profilesListChanged(d->profiles); 0292 } 0293 0294 // 0295 // Migrate profiles. 0296 // Profiles are only migrated, if the usage changed and the 0297 // user chose to use the migration assistant. 0298 // 0299 if (usageChanged && Smb4KSettings::useMigrationAssistant()) 0300 { 0301 QStringList from, to; 0302 0303 if (d->useProfiles) 0304 { 0305 // Since the setting changed, the use of profiles was 0306 // switched off before. So, ask the user if he/she wants 0307 // to migrate the default profile to any other one. 0308 // Therefore, from needs to get one empty entry (default 0309 // profile) and to has to be d->profiles. 0310 from << QString(); 0311 to << d->profiles; 0312 } 0313 else 0314 { 0315 // Here it is vice versa: Ask the user if he/she wants to 0316 // migrate all profiles to the default profile. Therefore, 0317 // set from to d->profiles and to to empty. 0318 from << d->profiles; 0319 to << QString(); 0320 } 0321 0322 // Now, launch the migration dialog. 0323 QPointer<Smb4KProfileMigrationDialog> dlg = new Smb4KProfileMigrationDialog(from, to, QApplication::activeWindow()); 0324 0325 if (dlg->exec() == QDialog::Accepted) 0326 { 0327 migrateProfile(dlg->from(), dlg->to()); 0328 } 0329 0330 delete dlg; 0331 } 0332 0333 // 0334 // Set the active profile 0335 // 0336 if (!Smb4KSettings::activeProfile().isEmpty() && d->profiles.contains(Smb4KSettings::activeProfile())) 0337 { 0338 setActiveProfile(Smb4KSettings::activeProfile()); 0339 } 0340 else 0341 { 0342 setActiveProfile(d->profiles.first()); 0343 } 0344 } 0345