File indexing completed on 2024-04-28 05:01:57

0001 /*
0002     Manage custom settings
0003 
0004     SPDX-FileCopyrightText: 2011-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4kcustomsettingsmanager.h"
0010 #include "smb4kcustomsettings.h"
0011 #include "smb4kglobal.h"
0012 #include "smb4khomesshareshandler.h"
0013 #include "smb4khost.h"
0014 #include "smb4knotification.h"
0015 #include "smb4kprofilemanager.h"
0016 #include "smb4ksettings.h"
0017 #include "smb4kshare.h"
0018 
0019 #if defined(Q_OS_LINUX)
0020 #include "smb4kmountsettings_linux.h"
0021 #elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
0022 #include "smb4kmountsettings_bsd.h"
0023 #endif
0024 
0025 // Qt includes
0026 #include <QDebug>
0027 #include <QRegularExpression>
0028 #include <QXmlStreamReader>
0029 #include <QXmlStreamWriter>
0030 
0031 // KDE includes
0032 #include <KLocalizedString>
0033 
0034 using namespace Smb4KGlobal;
0035 
0036 class Smb4KCustomSettingsManagerPrivate
0037 {
0038 public:
0039     QList<CustomSettingsPtr> customSettings;
0040 };
0041 
0042 class Smb4KCustomSettingsManagerStatic
0043 {
0044 public:
0045     Smb4KCustomSettingsManager instance;
0046 };
0047 
0048 Q_GLOBAL_STATIC(Smb4KCustomSettingsManagerStatic, p);
0049 
0050 Smb4KCustomSettingsManager::Smb4KCustomSettingsManager(QObject *parent)
0051     : QObject(parent)
0052     , d(new Smb4KCustomSettingsManagerPrivate)
0053 {
0054     // First we need the directory.
0055     QString path = dataLocation();
0056 
0057     QDir dir;
0058 
0059     if (!dir.exists(path)) {
0060         dir.mkpath(path);
0061     }
0062 
0063     read();
0064 
0065     connect(Smb4KProfileManager::self(), &Smb4KProfileManager::profileRemoved, this, &Smb4KCustomSettingsManager::slotProfileRemoved);
0066     connect(Smb4KProfileManager::self(), &Smb4KProfileManager::profileMigrated, this, &Smb4KCustomSettingsManager::slotProfileMigrated);
0067 }
0068 
0069 Smb4KCustomSettingsManager::~Smb4KCustomSettingsManager()
0070 {
0071 }
0072 
0073 Smb4KCustomSettingsManager *Smb4KCustomSettingsManager::self()
0074 {
0075     return &p->instance;
0076 }
0077 
0078 void Smb4KCustomSettingsManager::addRemount(const SharePtr &share, bool always)
0079 {
0080     if (share) {
0081         CustomSettingsPtr settings = findCustomSettings(share, true);
0082 
0083         if (!settings) {
0084             settings = CustomSettingsPtr(new Smb4KCustomSettings(share.data()));
0085             // add() takes care of the profile, we do not need to set it here.
0086             add(settings);
0087         }
0088 
0089         // If the options are already in the list, check if the share is
0090         // always to be remounted. If so, ignore the 'always' argument
0091         // and leave that option untouched.
0092         if (settings->remount() != Smb4KCustomSettings::RemountAlways) {
0093             settings->setRemount(always ? Smb4KCustomSettings::RemountAlways : Smb4KCustomSettings::RemountOnce);
0094         }
0095 
0096         write();
0097         Q_EMIT updated();
0098     }
0099 }
0100 
0101 void Smb4KCustomSettingsManager::removeRemount(const SharePtr &share, bool force)
0102 {
0103     if (share) {
0104         CustomSettingsPtr settings = findCustomSettings(share, true);
0105 
0106         if (settings) {
0107             if (settings->remount() == Smb4KCustomSettings::RemountOnce) {
0108                 settings->setRemount(Smb4KCustomSettings::UndefinedRemount);
0109             } else if (settings->remount() == Smb4KCustomSettings::RemountAlways && force) {
0110                 settings->setRemount(Smb4KCustomSettings::UndefinedRemount);
0111             }
0112 
0113             if (!settings->hasCustomSettings()) {
0114                 remove(settings);
0115             }
0116         }
0117 
0118         write();
0119         Q_EMIT updated();
0120     }
0121 }
0122 
0123 void Smb4KCustomSettingsManager::clearRemounts(bool force)
0124 {
0125     QList<CustomSettingsPtr> settingsList = customSettings(false);
0126 
0127     for (const CustomSettingsPtr &settings : qAsConst(settingsList)) {
0128         if (settings->type() == Share) {
0129             if (settings->remount() == Smb4KCustomSettings::RemountOnce) {
0130                 settings->setRemount(Smb4KCustomSettings::UndefinedRemount);
0131             } else if (settings->remount() == Smb4KCustomSettings::RemountAlways && force) {
0132                 settings->setRemount(Smb4KCustomSettings::UndefinedRemount);
0133             }
0134         }
0135 
0136         if (!settings->hasCustomSettings()) {
0137             remove(settings);
0138         }
0139     }
0140 
0141     write();
0142     Q_EMIT updated();
0143 }
0144 
0145 QList<CustomSettingsPtr> Smb4KCustomSettingsManager::sharesToRemount()
0146 {
0147     QList<CustomSettingsPtr> settingsList = customSettings(false);
0148     QList<CustomSettingsPtr> remountsList;
0149 
0150     for (const CustomSettingsPtr &settings : qAsConst(settingsList)) {
0151         if (settings->remount() != Smb4KCustomSettings::UndefinedRemount) {
0152             remountsList << settings;
0153         }
0154     }
0155 
0156     return remountsList;
0157 }
0158 
0159 CustomSettingsPtr Smb4KCustomSettingsManager::findCustomSettings(const NetworkItemPtr &networkItem, bool exactMatch)
0160 {
0161     CustomSettingsPtr settings = findCustomSettings(networkItem->url());
0162 
0163     if (!settings && !exactMatch && networkItem->type() == Share) {
0164         CustomSettingsPtr hostSettings = findCustomSettings(networkItem->url().adjusted(QUrl::RemovePath | QUrl::StripTrailingSlash));
0165 
0166         if (hostSettings) {
0167             settings = CustomSettingsPtr(new Smb4KCustomSettings(networkItem.data()));
0168             settings->update(hostSettings.data());
0169         }
0170     }
0171 
0172     return settings;
0173 }
0174 
0175 CustomSettingsPtr Smb4KCustomSettingsManager::findCustomSettings(const QUrl &url)
0176 {
0177     CustomSettingsPtr settings;
0178 
0179     if (url.isValid() && url.scheme() == QStringLiteral("smb")) {
0180         QList<CustomSettingsPtr> settingsList = customSettings(false);
0181 
0182         for (const CustomSettingsPtr &cs : qAsConst(settingsList)) {
0183             if (cs->url().toString(QUrl::RemoveUserInfo | QUrl::RemovePort | QUrl::StripTrailingSlash)
0184                 == url.toString(QUrl::RemoveUserInfo | QUrl::RemovePort | QUrl::StripTrailingSlash)) {
0185                 settings = cs;
0186                 break;
0187             }
0188         }
0189     }
0190 
0191     return settings;
0192 }
0193 
0194 QList<CustomSettingsPtr> Smb4KCustomSettingsManager::customSettings(bool withoutRemountOnce) const
0195 {
0196     QList<CustomSettingsPtr> settingsList;
0197 
0198     for (const CustomSettingsPtr &settings : qAsConst(d->customSettings)) {
0199         if (Smb4KSettings::useProfiles() && settings->profile() != Smb4KProfileManager::self()->activeProfile()) {
0200             continue;
0201         }
0202 
0203         if (settings->hasCustomSettings(withoutRemountOnce)) {
0204             settingsList << settings;
0205         }
0206     }
0207 
0208     return settingsList;
0209 }
0210 
0211 void Smb4KCustomSettingsManager::addCustomSettings(const CustomSettingsPtr &settings)
0212 {
0213     if (settings) {
0214         add(settings);
0215         write();
0216         Q_EMIT updated();
0217     }
0218 }
0219 
0220 void Smb4KCustomSettingsManager::removeCustomSettings(const CustomSettingsPtr &settings)
0221 {
0222     if (settings) {
0223         remove(settings);
0224         write();
0225         Q_EMIT updated();
0226     }
0227 }
0228 
0229 QList<CustomSettingsPtr> Smb4KCustomSettingsManager::wakeOnLanEntries() const
0230 {
0231     QList<CustomSettingsPtr> wakeOnLanList;
0232     QList<CustomSettingsPtr> settingsList = customSettings();
0233 
0234     for (const CustomSettingsPtr &settings : qAsConst(settingsList)) {
0235         if (!settings->macAddress().isEmpty() && (settings->wakeOnLanSendBeforeNetworkScan() || settings->wakeOnLanSendBeforeMount())) {
0236             wakeOnLanList << settings;
0237         }
0238     }
0239 
0240     return wakeOnLanList;
0241 }
0242 
0243 void Smb4KCustomSettingsManager::saveCustomSettings(const QList<CustomSettingsPtr> &settingsList)
0244 {
0245     QMutableListIterator<CustomSettingsPtr> it(d->customSettings);
0246 
0247     while (it.hasNext()) {
0248         CustomSettingsPtr settings = it.next();
0249         remove(settings);
0250     }
0251 
0252     for (const CustomSettingsPtr &settings : settingsList) {
0253         add(settings);
0254     }
0255 
0256     write();
0257     Q_EMIT updated();
0258 }
0259 
0260 void Smb4KCustomSettingsManager::add(const CustomSettingsPtr &settings)
0261 {
0262     CustomSettingsPtr knownSettings = findCustomSettings(settings->url());
0263 
0264     if (knownSettings) {
0265         knownSettings->update(settings.data());
0266     } else {
0267         if (settings->profile().isEmpty()) {
0268             settings->setProfile(Smb4KProfileManager::self()->activeProfile());
0269         }
0270 
0271         d->customSettings << settings;
0272     }
0273 
0274     // Propagate the settings to the host's shares if the type is 'Host'
0275     if (settings->type() == Host) {
0276         QList<CustomSettingsPtr> customSettingsList = customSettings(true);
0277 
0278         for (const CustomSettingsPtr &cs : qAsConst(customSettingsList)) {
0279             // Since only the URL is important, do not check for the workgroup.
0280             // Also, if the workgroup is a DNS-SD domain, it is most likely not
0281             // a valid SMB workgroup or domain.
0282             if (cs->type() == Share && cs->hostName() == settings->hostName()) {
0283                 cs->update(settings.data());
0284             }
0285         }
0286     }
0287 }
0288 
0289 void Smb4KCustomSettingsManager::remove(const CustomSettingsPtr &settings)
0290 {
0291     for (int i = 0; i < d->customSettings.size(); i++) {
0292         if ((!Smb4KSettings::useProfiles() || Smb4KProfileManager::self()->activeProfile() == d->customSettings.at(i)->profile())
0293             && d->customSettings.at(i)->url().matches(settings->url(), QUrl::RemoveUserInfo | QUrl::RemovePort | QUrl::StripTrailingSlash)) {
0294             d->customSettings.takeAt(i).clear();
0295             break;
0296         }
0297     }
0298 }
0299 
0300 void Smb4KCustomSettingsManager::read()
0301 {
0302     while (!d->customSettings.isEmpty()) {
0303         d->customSettings.takeFirst().clear();
0304     }
0305 
0306     QFile xmlFile(dataLocation() + QDir::separator() + QStringLiteral("custom_options.xml"));
0307 
0308     if (xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
0309         QXmlStreamReader xmlReader(&xmlFile);
0310 
0311         while (!xmlReader.atEnd()) {
0312             xmlReader.readNext();
0313 
0314             if (xmlReader.isStartElement()) {
0315                 if (xmlReader.name() == QStringLiteral("custom_options") && xmlReader.attributes().value(QStringLiteral("version")) != QStringLiteral("3.0")) {
0316                     xmlReader.raiseError(i18n("The format of %1 is not supported.", xmlFile.fileName()));
0317                     break;
0318                 } else {
0319                     if (xmlReader.name() == QStringLiteral("options")) {
0320                         CustomSettingsPtr settings = CustomSettingsPtr(new Smb4KCustomSettings());
0321                         settings->setProfile(xmlReader.attributes().value(QStringLiteral("profile")).toString());
0322 
0323                         if (QString::compare(xmlReader.attributes().value(QStringLiteral("type")).toString(), QStringLiteral("host"), Qt::CaseInsensitive)
0324                             == 0) {
0325                             settings->setNetworkItem(new Smb4KHost());
0326                         } else {
0327                             settings->setNetworkItem(new Smb4KShare());
0328                         }
0329 
0330                         while (!(xmlReader.isEndElement() && xmlReader.name() == QStringLiteral("options"))) {
0331                             xmlReader.readNext();
0332 
0333                             if (xmlReader.isStartElement()) {
0334                                 if (xmlReader.name() == QStringLiteral("workgroup")) {
0335                                     settings->setWorkgroupName(xmlReader.readElementText());
0336                                 } else if (xmlReader.name() == QStringLiteral("url")) {
0337                                     QUrl url(xmlReader.readElementText());
0338                                     settings->setUrl(url);
0339                                 } else if (xmlReader.name() == QStringLiteral("ip")) {
0340                                     settings->setIpAddress(xmlReader.readElementText());
0341                                 } else if (xmlReader.name() == QStringLiteral("custom")) {
0342                                     while (!(xmlReader.isEndElement() && xmlReader.name() == QStringLiteral("custom"))) {
0343                                         xmlReader.readNext();
0344 
0345                                         if (xmlReader.isStartElement()) {
0346                                             if (xmlReader.name() == QStringLiteral("smb_port")) {
0347                                                 bool ok = false;
0348                                                 int portNumber = xmlReader.readElementText().toInt(&ok);
0349 
0350                                                 if (ok) {
0351                                                     settings->setSmbPort(portNumber);
0352                                                 }
0353                                             } else if (xmlReader.name() == QStringLiteral("use_smb_port")) {
0354                                                 bool ok = false;
0355                                                 bool useSmbPort = xmlReader.readElementText().toInt(&ok);
0356 
0357                                                 if (ok) {
0358                                                     settings->setUseSmbPort(useSmbPort);
0359                                                 }
0360                                             } else if (xmlReader.name() == QStringLiteral("kerberos")) {
0361                                                 bool ok = false;
0362                                                 bool useKerberos = xmlReader.readElementText().toInt(&ok);
0363 
0364                                                 if (ok) {
0365                                                     settings->setUseKerberos(useKerberos);
0366                                                 }
0367                                             } else if (xmlReader.name() == QStringLiteral("mac_address")) {
0368                                                 QString macAddress = xmlReader.readElementText();
0369 
0370                                                 QRegularExpression expression(QStringLiteral("..\\:..\\:..\\:..\\:..\\:.."));
0371 
0372                                                 if (expression.match(macAddress).hasMatch()) {
0373                                                     settings->setMACAddress(macAddress);
0374                                                 }
0375                                             } else if (xmlReader.name() == QStringLiteral("wol_send_before_first_scan")) {
0376                                                 bool ok = false;
0377                                                 bool send = xmlReader.readElementText().toInt(&ok);
0378 
0379                                                 if (ok) {
0380                                                     settings->setWakeOnLanSendBeforeNetworkScan(send);
0381                                                 }
0382                                             } else if (xmlReader.name() == QStringLiteral("wol_send_before_mount")) {
0383                                                 bool ok = false;
0384                                                 bool send = xmlReader.readElementText().toInt(&ok);
0385 
0386                                                 if (ok) {
0387                                                     settings->setWakeOnLanSendBeforeMount(send);
0388                                                 }
0389                                             } else if (xmlReader.name() == QStringLiteral("remount")) {
0390                                                 bool ok = false;
0391                                                 int remount = xmlReader.readElementText().toInt(&ok);
0392 
0393                                                 if (ok) {
0394                                                     settings->setRemount(remount);
0395                                                 }
0396                                             } else if (xmlReader.name() == QStringLiteral("use_user")) {
0397                                                 bool ok = false;
0398                                                 bool useUser = xmlReader.readElementText().toInt(&ok);
0399 
0400                                                 if (ok) {
0401                                                     settings->setUseUser(useUser);
0402                                                 }
0403                                             } else if (xmlReader.name() == QStringLiteral("uid")) {
0404                                                 bool ok = false;
0405                                                 int uid = xmlReader.readElementText().toInt(&ok);
0406 
0407                                                 if (ok) {
0408                                                     KUser user((K_UID)uid);
0409 
0410                                                     if (user.isValid()) {
0411                                                         settings->setUser(user);
0412                                                     }
0413                                                 }
0414                                             } else if (xmlReader.name() == QStringLiteral("use_group")) {
0415                                                 bool ok = false;
0416                                                 bool useGroup = xmlReader.readElementText().toInt(&ok);
0417 
0418                                                 if (ok) {
0419                                                     settings->setUseGroup(useGroup);
0420                                                 }
0421                                             } else if (xmlReader.name() == QStringLiteral("gid")) {
0422                                                 bool ok = false;
0423                                                 int gid = xmlReader.readElementText().toInt(&ok);
0424 
0425                                                 if (ok) {
0426                                                     KUserGroup group((K_GID)gid);
0427 
0428                                                     if (group.isValid()) {
0429                                                         settings->setGroup(group);
0430                                                     }
0431                                                 }
0432                                             } else if (xmlReader.name() == QStringLiteral("use_file_mode")) {
0433                                                 bool ok = false;
0434                                                 bool useFileMode = xmlReader.readElementText().toInt(&ok);
0435 
0436                                                 if (ok) {
0437                                                     settings->setUseFileMode(useFileMode);
0438                                                 }
0439                                             } else if (xmlReader.name() == QStringLiteral("file_mode")) {
0440                                                 settings->setFileMode(xmlReader.readElementText());
0441                                             } else if (xmlReader.name() == QStringLiteral("use_directory_mode")) {
0442                                                 bool ok = false;
0443                                                 bool useDirectoryMode = xmlReader.readElementText().toInt(&ok);
0444 
0445                                                 if (ok) {
0446                                                     settings->setUseDirectoryMode(useDirectoryMode);
0447                                                 }
0448                                             } else if (xmlReader.name() == QStringLiteral("directory_mode")) {
0449                                                 settings->setDirectoryMode(xmlReader.readElementText());
0450                                             } else if (xmlReader.name() == QStringLiteral("use_client_protocol_versions")) {
0451                                                 bool ok = false;
0452                                                 bool useClientProtocolVersions = xmlReader.readElementText().toInt(&ok);
0453 
0454                                                 if (ok) {
0455                                                     settings->setUseClientProtocolVersions(useClientProtocolVersions);
0456                                                 }
0457                                             } else if (xmlReader.name() == QStringLiteral("minimal_client_protocol_version")) {
0458                                                 bool ok = false;
0459                                                 int minimalClientProtocolVersion = xmlReader.readElementText().toInt(&ok);
0460 
0461                                                 if (ok) {
0462                                                     settings->setMinimalClientProtocolVersion(minimalClientProtocolVersion);
0463                                                 }
0464                                             } else if (xmlReader.name() == QStringLiteral("maximal_client_protocol_version")) {
0465                                                 bool ok = false;
0466                                                 int maximalClientProtocolVersion = xmlReader.readElementText().toInt(&ok);
0467 
0468                                                 if (ok) {
0469                                                     settings->setMaximalClientProtocolVersion(maximalClientProtocolVersion);
0470                                                 }
0471                                             }
0472 #if defined(Q_OS_LINUX)
0473                                             else if (xmlReader.name() == QStringLiteral("cifs_unix_extensions_support")) {
0474                                                 bool ok = false;
0475                                                 bool cifsUnixExtensionsSupported = xmlReader.readElementText().toInt(&ok);
0476 
0477                                                 if (ok) {
0478                                                     settings->setCifsUnixExtensionsSupport(cifsUnixExtensionsSupported);
0479                                                 }
0480                                             } else if (xmlReader.name() == QStringLiteral("use_filesystem_port")) {
0481                                                 bool ok = false;
0482                                                 bool useFilesystemPort = xmlReader.readElementText().toInt(&ok);
0483 
0484                                                 if (ok) {
0485                                                     settings->setUseFileSystemPort(useFilesystemPort);
0486                                                 }
0487                                             } else if (xmlReader.name() == QStringLiteral("filesystem_port")) {
0488                                                 bool ok = false;
0489                                                 int portNumber = xmlReader.readElementText().toInt(&ok);
0490 
0491                                                 if (ok) {
0492                                                     settings->setFileSystemPort(portNumber);
0493                                                 }
0494                                             } else if (xmlReader.name() == QStringLiteral("use_smb_mount_protocol_version")) {
0495                                                 bool ok = false;
0496                                                 bool useMountProtocolVersion = xmlReader.readElementText().toInt(&ok);
0497 
0498                                                 if (ok) {
0499                                                     settings->setUseMountProtocolVersion(useMountProtocolVersion);
0500                                                 }
0501                                             } else if (xmlReader.name() == QStringLiteral("smb_mount_protocol_version")) {
0502                                                 bool ok = false;
0503                                                 int mountProtocolVersion = xmlReader.readElementText().toInt(&ok);
0504 
0505                                                 if (ok) {
0506                                                     settings->setMountProtocolVersion(mountProtocolVersion);
0507                                                 }
0508                                             } else if (xmlReader.name() == QStringLiteral("use_security_mode")) {
0509                                                 bool ok = false;
0510                                                 bool useSecurityMode = xmlReader.readElementText().toInt(&ok);
0511 
0512                                                 if (ok) {
0513                                                     settings->setUseSecurityMode(useSecurityMode);
0514                                                 }
0515                                             } else if (xmlReader.name() == QStringLiteral("security_mode")) {
0516                                                 bool ok = false;
0517                                                 int securityMode = xmlReader.readElementText().toInt(&ok);
0518 
0519                                                 if (ok) {
0520                                                     settings->setSecurityMode(securityMode);
0521                                                 }
0522                                             } else if (xmlReader.name() == QStringLiteral("use_write_access")) {
0523                                                 bool ok = false;
0524                                                 bool useWriteAccess = xmlReader.readElementText().toInt(&ok);
0525 
0526                                                 if (ok) {
0527                                                     settings->setUseWriteAccess(useWriteAccess);
0528                                                 }
0529                                             } else if (xmlReader.name() == QStringLiteral("write_access")) {
0530                                                 bool ok = false;
0531                                                 int writeAccess = xmlReader.readElementText().toInt(&ok);
0532 
0533                                                 if (ok) {
0534                                                     settings->setWriteAccess(writeAccess);
0535                                                 }
0536                                             }
0537 #endif
0538                                         }
0539                                     }
0540                                 }
0541                             }
0542                         }
0543 
0544                         d->customSettings << settings;
0545                     }
0546                 }
0547             }
0548         }
0549 
0550         xmlFile.close();
0551 
0552         if (xmlReader.hasError()) {
0553             Smb4KNotification::readingFileFailed(xmlFile, xmlReader.errorString());
0554         }
0555     } else {
0556         if (xmlFile.exists()) {
0557             Smb4KNotification::openingFileFailed(xmlFile);
0558         }
0559     }
0560 }
0561 
0562 void Smb4KCustomSettingsManager::write()
0563 {
0564     QFile xmlFile(dataLocation() + QDir::separator() + QStringLiteral("custom_options.xml"));
0565 
0566     if (d->customSettings.isEmpty()) {
0567         xmlFile.remove();
0568         return;
0569     }
0570 
0571     if (xmlFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
0572         QXmlStreamWriter xmlWriter(&xmlFile);
0573         xmlWriter.setAutoFormatting(true);
0574         xmlWriter.writeStartDocument();
0575         xmlWriter.writeStartElement(QStringLiteral("custom_options"));
0576         xmlWriter.writeAttribute(QStringLiteral("version"), QStringLiteral("3.0"));
0577 
0578         for (const CustomSettingsPtr &settings : qAsConst(d->customSettings)) {
0579             if (settings->hasCustomSettings()) {
0580                 xmlWriter.writeStartElement(QStringLiteral("options"));
0581                 xmlWriter.writeAttribute(QStringLiteral("type"), settings->type() == Host ? QStringLiteral("host") : QStringLiteral("share"));
0582                 xmlWriter.writeAttribute(QStringLiteral("profile"), settings->profile());
0583 
0584                 xmlWriter.writeTextElement(QStringLiteral("workgroup"), settings->workgroupName());
0585                 xmlWriter.writeTextElement(QStringLiteral("url"), settings->url().toDisplayString());
0586                 xmlWriter.writeTextElement(QStringLiteral("ip"), settings->ipAddress());
0587 
0588                 xmlWriter.writeStartElement(QStringLiteral("custom"));
0589 
0590                 QMap<QString, QString> map = settings->customSettings();
0591                 QMapIterator<QString, QString> i(map);
0592 
0593                 while (i.hasNext()) {
0594                     i.next();
0595 
0596                     if (!i.value().isEmpty()) {
0597                         xmlWriter.writeTextElement(i.key(), i.value());
0598                     }
0599                 }
0600 
0601                 xmlWriter.writeEndElement();
0602                 xmlWriter.writeEndElement();
0603             }
0604         }
0605 
0606         xmlWriter.writeEndDocument();
0607         xmlFile.close();
0608     } else {
0609         Smb4KNotification::openingFileFailed(xmlFile);
0610     }
0611 }
0612 
0613 /////////////////////////////////////////////////////////////////////////////
0614 // SLOT IMPLEMENTATIONS
0615 /////////////////////////////////////////////////////////////////////////////
0616 
0617 void Smb4KCustomSettingsManager::slotProfileRemoved(const QString &name)
0618 {
0619     QMutableListIterator<CustomSettingsPtr> it(d->customSettings);
0620 
0621     while (it.hasNext()) {
0622         CustomSettingsPtr settings = it.next();
0623 
0624         if (name == settings->profile()) {
0625             it.remove();
0626         }
0627     }
0628 
0629     write();
0630     Q_EMIT updated();
0631 }
0632 
0633 void Smb4KCustomSettingsManager::slotProfileMigrated(const QString &oldName, const QString &newName)
0634 {
0635     for (const CustomSettingsPtr &settings : qAsConst(d->customSettings)) {
0636         if (oldName == settings->profile()) {
0637             settings->setProfile(newName);
0638         }
0639     }
0640 
0641     write();
0642     Q_EMIT updated();
0643 }