diff --git a/core/smb4kcustomoptionsmanager.cpp b/core/smb4kcustomoptionsmanager.cpp index 8711710..3c99c15 100644 --- a/core/smb4kcustomoptionsmanager.cpp +++ b/core/smb4kcustomoptionsmanager.cpp @@ -1,1384 +1,1176 @@ /*************************************************************************** Manage custom options ------------------- begin : Fr 29 Apr 2011 copyright : (C) 2011-2019 by Alexander Reinholdt email : alexander.reinholdt@kdemail.net ***************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* * MA 02110-1335, USA * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include #endif // application specific includes #include "smb4kcustomoptionsmanager.h" #include "smb4kcustomoptionsmanager_p.h" #include "smb4kcustomoptions.h" #include "smb4khomesshareshandler.h" #include "smb4knotification.h" #include "smb4khost.h" #include "smb4kshare.h" #include "smb4ksettings.h" #include "smb4kglobal.h" #include "smb4kprofilemanager.h" #if defined(Q_OS_LINUX) #include "smb4kmountsettings_linux.h" #elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) #include "smb4kmountsettings_bsd.h" #endif // Qt includes #include #include #include #include #include // KDE includes #define TRANSLATION_DOMAIN "smb4k-core" #include using namespace Smb4KGlobal; Q_GLOBAL_STATIC(Smb4KCustomOptionsManagerStatic, p); Smb4KCustomOptionsManager::Smb4KCustomOptionsManager(QObject *parent) : QObject(parent), d(new Smb4KCustomOptionsManagerPrivate) { // First we need the directory. QString path = dataLocation(); QDir dir; if (!dir.exists(path)) { dir.mkpath(path); } - else - { - // Do nothing - } readCustomOptions(); // Connections connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(slotAboutToQuit())); } Smb4KCustomOptionsManager::~Smb4KCustomOptionsManager() { } Smb4KCustomOptionsManager *Smb4KCustomOptionsManager::self() { return &p->instance; } +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) void Smb4KCustomOptionsManager::addRemount(const SharePtr &share, bool always) { if (share) { OptionsPtr options = findOptions(share, true); if (options) { // If the options are already in the list, check if the share is // always to be remounted. If so, ignore the 'always' argument // and leave that option untouched. if (options->remount() != Smb4KCustomOptions::RemountAlways) { options->setRemount(always ? Smb4KCustomOptions::RemountAlways : Smb4KCustomOptions::RemountOnce); } - else - { - // Do nothing - } } else { options = OptionsPtr(new Smb4KCustomOptions(share.data())); options->setProfile(Smb4KProfileManager::self()->activeProfile()); options->setRemount(always ? Smb4KCustomOptions::RemountAlways : Smb4KCustomOptions::RemountOnce); d->options << options; } writeCustomOptions(); } - else - { - // Do nothing - } } void Smb4KCustomOptionsManager::removeRemount(const SharePtr &share, bool force) { if (share) { OptionsPtr options = findOptions(share, true); if (options) { if (options->remount() == Smb4KCustomOptions::RemountOnce) { options->setRemount(Smb4KCustomOptions::RemountNever); } else if (options->remount() == Smb4KCustomOptions::RemountAlways && force) { options->setRemount(Smb4KCustomOptions::RemountNever); - } - else - { - // Do nothing - } - } - else - { - // Do nothing + } } writeCustomOptions(); } - else - { - // Do nothing - } } void Smb4KCustomOptionsManager::clearRemounts(bool force) { // // List of relevant custom options // QList options = customOptions(false); // // Remove the remount flags // for (const OptionsPtr &o : options) { if (o->type() == Share) { if (o->remount() == Smb4KCustomOptions::RemountOnce) { o->setRemount(Smb4KCustomOptions::RemountNever); } else if (o->remount() == Smb4KCustomOptions::RemountAlways && force) { o->setRemount(Smb4KCustomOptions::RemountNever); } - else - { - // Do nothing - } - } - else - { - // Do nothing } } // // Write the options // writeCustomOptions(); } QList Smb4KCustomOptionsManager::sharesToRemount() { // // List of relevant custom options // QList options = customOptions(false); // // List of remounts // QList remounts; // // Get the list of remounts // for (const OptionsPtr &o : options) { if (o->remount() == Smb4KCustomOptions::RemountOnce) { remounts << o; } else if (o->remount() == Smb4KCustomOptions::RemountAlways) { remounts << o; } - else - { - // Do nothing - } } // // Return relevant options // return remounts; } +#endif OptionsPtr Smb4KCustomOptionsManager::findOptions(const NetworkItemPtr &networkItem, bool exactMatch) { // // The options that are to be returned // OptionsPtr options; // // Get the list of options // QList optionsList = customOptions(false); // // Only do something if the list of options is not empty. // if (!optionsList.isEmpty()) { switch (networkItem->type()) { case Host: { HostPtr host = networkItem.staticCast(); if (host) { for (const OptionsPtr &o : optionsList) { // In case of a host, there can only be an exact match. if (QString::compare(host->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), o->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), Qt::CaseInsensitive) == 0 || (host->url().isEmpty() && host->ipAddress() == o->ipAddress())) { options = o; break; } - else - { - // Do nothing - } } } - else - { - // Do nothing - } break; } case Share: { SharePtr share = networkItem.staticCast(); if (share) { for (const OptionsPtr &o : optionsList) { if (QString::compare(share->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), o->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), Qt::CaseInsensitive) == 0) { options = o; break; } else if (!exactMatch && o->type() == Host && QString::compare(share->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::RemovePath), o->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::RemovePath), Qt::CaseInsensitive) == 0) { options = o; } - else - { - // Do nothing - } } } - else - { - // Do nothing - } break; } default: { break; } } } - else - { - // Do nothing - } return options; } OptionsPtr Smb4KCustomOptionsManager::findOptions(const QUrl &url) { // // The options that are to be returned // OptionsPtr options; // // Search the options for the given URL // if (url.isValid() && url.scheme() == "smb") { // // Get the relevant options // QList optionsList = customOptions(false); // // Get the options // for (const OptionsPtr &o : optionsList) { if (o->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::StripTrailingSlash) == url.toString(QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::StripTrailingSlash)) { options = o; break; } - else - { - // Do nothing - } } } - else - { - // Do nothing - } // // Return the options // return options; } void Smb4KCustomOptionsManager::readCustomOptions() { // // Clear the list of options // while (!d->options.isEmpty()) { d->options.takeFirst().clear(); } // // Set the XML file // QFile xmlFile(dataLocation()+QDir::separator()+"custom_options.xml"); if (xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QXmlStreamReader xmlReader(&xmlFile); while (!xmlReader.atEnd()) { xmlReader.readNext(); if (xmlReader.isStartElement()) { if (xmlReader.name() == "custom_options" && (xmlReader.attributes().value("version") != "1.2" && xmlReader.attributes().value("version") != "2.0")) { xmlReader.raiseError(i18n("The format of %1 is not supported.", xmlFile.fileName())); break; } else { if (xmlReader.name() == "options") { OptionsPtr options = OptionsPtr(new Smb4KCustomOptions()); options->setProfile(xmlReader.attributes().value("profile").toString()); // // Initialize the options // if (QString::compare(xmlReader.attributes().value("type").toString(), "host", Qt::CaseInsensitive) == 0) { options->setHost(new Smb4KHost()); } else { options->setShare(new Smb4KShare()); } while (!(xmlReader.isEndElement() && xmlReader.name() == "options")) { xmlReader.readNext(); if (xmlReader.isStartElement()) { if (xmlReader.name() == "workgroup") { options->setWorkgroupName(xmlReader.readElementText()); } else if (xmlReader.name() == "url") { QUrl url(xmlReader.readElementText()); options->setUrl(url); } else if (xmlReader.name() == "unc") { QUrl url = QUrl::fromUserInput(xmlReader.readElementText()); url.setScheme("smb"); options->setUrl(url); } else if (xmlReader.name() == "ip") { options->setIpAddress(xmlReader.readElementText()); } else if (xmlReader.name() == "custom") { while (!(xmlReader.isEndElement() && xmlReader.name() == "custom")) { xmlReader.readNext(); if (xmlReader.isStartElement()) { if (xmlReader.name() == "smb_port") { bool ok = false; int portNumber = xmlReader.readElementText().toInt(&ok); if (ok) { options->setSmbPort(portNumber); } - else - { - // Do nothing - } } else if (xmlReader.name() == "use_smb_port") { QString useSmbPort = xmlReader.readElementText(); if (useSmbPort == "true") { options->setUseSmbPort(true); } else { options->setUseSmbPort(false); } } else if (xmlReader.name() == "kerberos") { QString useKerberos = xmlReader.readElementText(); if (useKerberos == "true") { options->setUseKerberos(true); } else { options->setUseKerberos(false); } } else if (xmlReader.name() == "mac_address") { QString macAddress = xmlReader.readElementText(); QRegExp exp("..\\:..\\:..\\:..\\:..\\:.."); if (exp.exactMatch(macAddress)) { options->setMACAddress(macAddress); } - else - { - // Do nothing - } } else if (xmlReader.name() == "wol_send_before_first_scan") { QString send = xmlReader.readElementText(); if (send == "true") { options->setWOLSendBeforeNetworkScan(true); } else { options->setWOLSendBeforeNetworkScan(false); } } else if (xmlReader.name() == "wol_send_before_mount") { QString send = xmlReader.readElementText(); if (send == "true") { options->setWOLSendBeforeMount(true); } else { options->setWOLSendBeforeMount(false); } } #if !defined(SMB4K_UNSUPPORTED_PLATFORM) else if (xmlReader.name() == "remount") { QString remount = xmlReader.readElementText(); if (remount == "once") { options->setRemount(Smb4KCustomOptions::RemountOnce); } else if (remount == "always") { options->setRemount(Smb4KCustomOptions::RemountAlways); } else if (remount == "never") { options->setRemount(Smb4KCustomOptions::RemountNever); } else { options->setRemount(Smb4KCustomOptions::UndefinedRemount); } } else if (xmlReader.name() == "use_user") { QString useUser = xmlReader.readElementText(); if (useUser == "true") { options->setUseUser(true); } else { options->setUseUser(false); } } else if (xmlReader.name() == "uid") { KUser user((K_UID)xmlReader.readElementText().toInt()); if (user.isValid()) { options->setUser(user); } - else - { - // Do nothing. Use default value. - } } else if (xmlReader.name() == "use_group") { QString useGroup = xmlReader.readElementText(); if (useGroup == "true") { options->setUseGroup(true); } else { options->setUseGroup(false); } } else if (xmlReader.name() == "gid") { KUserGroup group((K_GID)xmlReader.readElementText().toInt()); if (group.isValid()) { options->setGroup(group); } - else - { - // Do nothing. Use default value. - } } else if (xmlReader.name() == "use_file_mode") { QString useFileMode = xmlReader.readElementText(); if (useFileMode == "true") { options->setUseFileMode(true); } else { options->setUseFileMode(false); } } else if (xmlReader.name() == "file_mode") { options->setFileMode(xmlReader.readElementText()); } else if (xmlReader.name() == "use_directory_mode") { QString useDirectoryMode = xmlReader.readElementText(); if (useDirectoryMode == "true") { options->setUseDirectoryMode(true); } else { options->setUseDirectoryMode(false); } } else if (xmlReader.name() == "directory_mode") { options->setDirectoryMode(xmlReader.readElementText()); } #endif #if defined(Q_OS_LINUX) else if (xmlReader.name() == "cifs_unix_extensions_support") { QString support = xmlReader.readElementText(); if (support == "true") { options->setCifsUnixExtensionsSupport(true); } else { options->setCifsUnixExtensionsSupport(false); } } else if (xmlReader.name() == "use_filesystem_port") { QString useFilesystemPort = xmlReader.readElementText(); if (useFilesystemPort == "true") { options->setUseFileSystemPort(true); } else { options->setUseFileSystemPort(false); } } else if (xmlReader.name() == "filesystem_port") { bool ok = false; int portNumber = xmlReader.readElementText().toInt(&ok); if (ok) { options->setFileSystemPort(portNumber); } - else - { - // Do nothing - } } else if (xmlReader.name() == "use_security_mode") { QString useSecurityMode = xmlReader.readElementText(); if (useSecurityMode == "true") { options->setUseSecurityMode(true); } else { options->setUseSecurityMode(false); } } else if (xmlReader.name() == "security_mode") { QString securityMode = xmlReader.readElementText(); if (securityMode == "none") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::None); } else if (securityMode == "krb5") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Krb5); } else if (securityMode == "krb5i") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Krb5i); } else if (securityMode == "ntlm") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Ntlm); } else if (securityMode == "ntlmi") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Ntlmi); } else if (securityMode == "ntlmv2") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Ntlmv2); } else if (securityMode == "ntlmv2i") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Ntlmv2i); } else if (securityMode == "ntlmssp") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Ntlmssp); } else if (securityMode == "ntlmsspi") { options->setSecurityMode(Smb4KMountSettings::EnumSecurityMode::Ntlmsspi); } - else - { - // Do nothing - } } else if (xmlReader.name() == "use_write_access") { QString useWriteAccess = xmlReader.readElementText(); if (useWriteAccess == "true") { options->setUseWriteAccess(true); } else { options->setUseWriteAccess(false); } } else if (xmlReader.name() == "write_access") { QString writeAccess = xmlReader.readElementText(); if (writeAccess == "true") { options->setWriteAccess(Smb4KMountSettings::EnumWriteAccess::ReadWrite); } else if (writeAccess == "false") { options->setWriteAccess(Smb4KMountSettings::EnumWriteAccess::ReadWrite); } - else - { - // Do nothing - } } #endif - else - { - // Do nothing - } - } - else - { - // Do nothing } } } - else - { - // Do nothing - } continue; } else { continue; } } d->options << options; } - else - { - // Do nothing - } } } - else - { - // Do nothing - } } xmlFile.close(); if (xmlReader.hasError()) { Smb4KNotification::readingFileFailed(xmlFile, xmlReader.errorString()); } - else - { - // Do nothing - } } else { if (xmlFile.exists()) { Smb4KNotification::openingFileFailed(xmlFile); } - else - { - // Do nothing - } } } void Smb4KCustomOptionsManager::writeCustomOptions() { // // Set the XML file // QFile xmlFile(dataLocation()+QDir::separator()+"custom_options.xml"); // // Write the options to the file // if (!d->options.isEmpty()) { if (xmlFile.open(QIODevice::WriteOnly|QIODevice::Text)) { QXmlStreamWriter xmlWriter(&xmlFile); xmlWriter.setAutoFormatting(true); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("custom_options"); xmlWriter.writeAttribute("version", "2.0"); for (const OptionsPtr &options : d->options) { +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) if (options->hasOptions() || options->remount() == Smb4KCustomOptions::RemountOnce) +#else + if (options->hasOptions()) +#endif { xmlWriter.writeStartElement("options"); xmlWriter.writeAttribute("type", options->type() == Host ? "host" : "share"); xmlWriter.writeAttribute("profile", options->profile()); xmlWriter.writeTextElement("workgroup", options->workgroupName()); xmlWriter.writeTextElement("url", options->url().toDisplayString()); xmlWriter.writeTextElement("ip", options->ipAddress()); xmlWriter.writeStartElement("custom"); QMap map = options->customOptions(); QMapIterator it(map); while (it.hasNext()) { it.next(); if (!it.value().isEmpty()) { xmlWriter.writeTextElement(it.key(), it.value()); } - else - { - // Do nothing - } } xmlWriter.writeEndElement(); xmlWriter.writeEndElement(); } - else - { - // Do nothing - } } xmlWriter.writeEndDocument(); xmlFile.close(); } else { Smb4KNotification::openingFileFailed(xmlFile); } } else { xmlFile.remove(); } // FIXME: Flush the list of custom options and reload it? } QList Smb4KCustomOptionsManager::customOptions(bool optionsOnly) { // // Options list // QList options; // // Get this list of options // for (const OptionsPtr &o : d->options) { if (Smb4KSettings::useProfiles() && o->profile() != Smb4KProfileManager::self()->activeProfile()) { continue; } - else - { - // Do nothing - } +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) if (o->hasOptions() || (!optionsOnly && o->remount() == Smb4KCustomOptions::RemountOnce)) +#else + if (o->hasOptions()) +#endif { options << o; } - else - { - // Do nothing - } } // // Return the list of relevant options // return options; } void Smb4KCustomOptionsManager::replaceCustomOptions(const QList &optionsList) { // // Clear the list of options. Honor profiles. // QMutableListIterator it(d->options); while (it.hasNext()) { OptionsPtr options = it.next(); if (Smb4KSettings::useProfiles() && options->profile() != Smb4KProfileManager::self()->activeProfile()) { continue; } - else - { - // Do nothing - } it.remove(); } // // Append the new list // if (!optionsList.isEmpty()) { for (const OptionsPtr &options : optionsList) { if (Smb4KSettings::useProfiles()) { options->setProfile(Smb4KProfileManager::self()->activeProfile()); } - else - { - // Do nothing - } +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) if (options->hasOptions() || options->remount() == Smb4KCustomOptions::RemountOnce) +#else + if (options->hasOptions()) +#endif { d->options << options; } - else - { - // Do nothing - } } } - else - { - // Do nothing - } writeCustomOptions(); } void Smb4KCustomOptionsManager::openCustomOptionsDialog(const NetworkItemPtr &item) { if (item) { OptionsPtr options; switch (item->type()) { case Host: { - qDebug() << "Host"; HostPtr host = item.staticCast(); if (host) { options = findOptions(host); if (!options) { options = OptionsPtr(new Smb4KCustomOptions(host.data())); options->setProfile(Smb4KProfileManager::self()->activeProfile()); } - else - { - // Do nothing - } - } - else - { - // Do nothing } break; } case Share: { - qDebug() << "Share"; SharePtr share = item.staticCast(); if (share && !share->isPrinter()) { if (share->isHomesShare()) { if (!Smb4KHomesSharesHandler::self()->specifyUser(share, true)) { return; } - else - { - // Do nothing - } - } - else - { - // Do nothing } options = findOptions(share); if (!options) { options = OptionsPtr(new Smb4KCustomOptions(share.data())); options->setProfile(Smb4KProfileManager::self()->activeProfile()); // Get rid of the 'homes' share if (share->isHomesShare()) { options->setUrl(share->homeUrl()); } - else - { - // Do nothing - } } else { // In case the custom options object for the host has been // returned, change its internal network item, otherwise we // will change the host's custom options... options->setShare(share.data()); } } - else - { - // Do nothing - } break; } default: { break; } } if (options) { QPointer dlg = new Smb4KCustomOptionsDialog(options, QApplication::activeWindow()); if (dlg->exec() == QDialog::Accepted) { if (options->hasOptions()) { addCustomOptions(options); } else { removeCustomOptions(options); } } else { resetCustomOptions(); } delete dlg; } - else - { - // Do nothing - } - } - else - { - // Do nothing } } void Smb4KCustomOptionsManager::addCustomOptions(const OptionsPtr &options) { if (options) { // // Check if options for the URL already exist // OptionsPtr knownOptions = findOptions(options->url()); if (knownOptions) { // // Update the options // knownOptions->update(options.data()); } else { // // Add the options // if (options->profile().isEmpty()) { options->setProfile(Smb4KProfileManager::self()->activeProfile()); } - else - { - // Do nothing - } d->options << options; } // // In case the options are defined for a host, propagate them // to the options of shares belonging to that host. Overwrite // the settings // if (options->type() == Host) { for (const OptionsPtr &o : d->options) { if (o->type() == Share && o->hostName() == options->hostName() && o->workgroupName() == options->workgroupName()) { o->setIpAddress(options->ipAddress()); #if !defined(SMB4K_UNSUPPORTED_PLATFORM) o->setUseUser(options->useUser()); o->setUser(options->user()); o->setUseGroup(options->useGroup()); o->setGroup(options->group()); o->setUseFileMode(options->useFileMode()); o->setFileMode(options->fileMode()); o->setUseDirectoryMode(options->useDirectoryMode()); o->setDirectoryMode(options->directoryMode()); #endif #if defined(Q_OS_LINUX) o->setCifsUnixExtensionsSupport(options->cifsUnixExtensionsSupport()); o->setUseFileSystemPort(options->useFileSystemPort()); o->setFileSystemPort(options->fileSystemPort()); o->setUseSecurityMode(options->useSecurityMode()); o->setSecurityMode(options->securityMode()); o->setUseWriteAccess(options->useWriteAccess()); o->setWriteAccess(options->writeAccess()); #endif o->setUseSmbPort(options->useSmbPort()); o->setSmbPort(options->smbPort()); o->setUseKerberos(options->useKerberos()); o->setMACAddress(options->macAddress()); o->setWOLSendBeforeNetworkScan(options->wolSendBeforeNetworkScan()); o->setWOLSendBeforeMount(options->wolSendBeforeMount()); } - else - { - // Do nothing - } } } - else - { - // Do nothing - } // // Write the custom options to the file // writeCustomOptions(); } - else - { - // Do nothing - } } void Smb4KCustomOptionsManager::removeCustomOptions(const OptionsPtr &options) { if (options) { // // Find the custom options and remove them // for (int i = 0; i < d->options.size(); ++i) { if ((!Smb4KSettings::useProfiles() || Smb4KProfileManager::self()->activeProfile() == d->options.at(i)->profile()) && d->options.at(i)->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::StripTrailingSlash) == options->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort|QUrl::StripTrailingSlash)) { d->options.takeAt(i).clear(); break; } - else - { - // Do nothing - } } // // Write the custom options to the file // writeCustomOptions(); } - else - { - // Do nothing - } } QList Smb4KCustomOptionsManager::wakeOnLanEntries() const { QList list; // // Get the Wake-On-LAN entries // for (const OptionsPtr &options : d->options) { if (!options->macAddress().isEmpty() && (options->wolSendBeforeNetworkScan() || options->wolSendBeforeMount())) { list << options; } - else - { - // Do nothing - } } // // Return them // return list; } void Smb4KCustomOptionsManager::resetCustomOptions() { readCustomOptions(); } void Smb4KCustomOptionsManager::migrateProfile(const QString& from, const QString& to) { // // Replace the old with the new profile // for (const OptionsPtr &options : d->options) { if (options->profile() == from) { options->setProfile(to); } - else - { - // Do nothing - } } // // Write all custom options to the file. // writeCustomOptions(); } void Smb4KCustomOptionsManager::removeProfile(const QString& name) { // // Remove all entries belonging to the profile // QMutableListIterator it(d->options); while (it.hasNext()) { OptionsPtr options = it.next(); if (QString::compare(options->profile(), name, Qt::CaseSensitive) == 0) { it.remove(); } - else - { - // Do nothing - } } // // Write all custom options to the file. // writeCustomOptions(); } ///////////////////////////////////////////////////////////////////////////// // SLOT IMPLEMENTATIONS ///////////////////////////////////////////////////////////////////////////// void Smb4KCustomOptionsManager::slotAboutToQuit() { writeCustomOptions(); } diff --git a/core/smb4kcustomoptionsmanager.h b/core/smb4kcustomoptionsmanager.h index 88e5ed9..a66dc47 100644 --- a/core/smb4kcustomoptionsmanager.h +++ b/core/smb4kcustomoptionsmanager.h @@ -1,250 +1,252 @@ /*************************************************************************** Manage custom options ------------------- begin : Fr 29 Apr 2011 copyright : (C) 2011-2019 by Alexander Reinholdt email : alexander.reinholdt@kdemail.net ***************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* * MA 02110-1335, USA * ***************************************************************************/ #ifndef SMB4KCUSTOMOPTIONSMANAGER_H #define SMB4KCUSTOMOPTIONSMANAGER_H // application specific includes #include "smb4kglobal.h" // Qt includes #include #include // forward declarations class Smb4KCustomOptionsManagerPrivate; class Smb4KProfileManager; /** * This classes manages the custom options that were defined * for a certain share or host. * * @author Alexander Reinholdt * @since 1.0.0 */ class Q_DECL_EXPORT Smb4KCustomOptionsManager : public QObject { Q_OBJECT friend class Smb4KCustomOptionsManagerPrivate; friend class Smb4KProfileManager; public: /** * Constructor */ explicit Smb4KCustomOptionsManager(QObject *parent = 0); /** * Destructor */ ~Smb4KCustomOptionsManager(); /** * Returns a static pointer to this class * * @returns a static pointer to this class */ static Smb4KCustomOptionsManager *self(); - + +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) /** * Add the share to the list of shares that are to be remounted * either only on next program start or always Smb4K is restarted. * * @param share The share object * * @param always If set to TRUE the share is always mounted * when Smb4K is restarted. */ void addRemount(const SharePtr &share, bool always = false); /** * Remove the share @p share from the list of shares that are to be * remounted. If @p force is set to TRUE, the share is removed even * if it should always be removed (option is set to * Smb4KCustomOptions::AlwaysRemount). Apart from that, the share is only * removed when the option is set to Smb4KCustomOptions::DoRemount. * * @param share The share object * * @param force If set to TRUE, the share is removed regardless of the * remount setting. */ void removeRemount(const SharePtr &share, bool force = false); /** * Removes all remounts from the list of custom options. If @p force * is set to TRUE, even those are removed that should always be remounted. * * @param force If set to TRUE, even those shares are removed that should * always be remounted. */ void clearRemounts(bool force); /** * Returns the list of shares that are to be remounted. * * @returns the list of shares that are to be remounted */ QList sharesToRemount(); +#endif /** * Find custom options for the network item @p networkItem. * * If the network item represents a share and custom options for it are not * defined, but for the host that provides the share, the options for the host * are returned. If neither is in the list, NULL is returned. * * If you set @p exactMatch to TRUE, NULL will be returned if the URL is not found. * Except in some special cases, you should not set @p exactMatch to true, * because options that are defined for all shares provided by a certain host and * stored in a host-type custom options object are ignored then. * * @param networkItem The network item * @param exaxtMatch If TRUE, only exact matches are returned * * @returns the custom options for the network item */ OptionsPtr findOptions(const NetworkItemPtr &networkItem, bool exactMatch = false); /** * Find custom options for the provided @p url. * * This function searches the list of custom options and compares the host entry * and, if applicable, the path (i.e. the share name). If an exact match was found, * the corresponding custom options are returned. * * @param url The network item's URL * * @returns the custom options */ OptionsPtr findOptions(const QUrl &url); /** * Get the list of custom options. By default, the list not only comprises of those * items that have custom options defined but also of those that are "only" to be * remounted. If @p optionsOnly is defined, only those entries are returned that have * custom options defined. Those that are only to be remounted won't be returned. * * @param optionsOnly Only return those entries that have custom options defined * * @returns the list of custom options objects. */ QList customOptions(bool optionsOnly = false); /** * Replace all previously defined custom options with a list of new ones. If you * just want to change certain custom options, use the findOptions() functions. * * @param optionsList The list of new or updated options */ void replaceCustomOptions(const QList &optionsList); /** * This function opens the custom options dialog. * * @param item The network item - either host or share * * @param parent The parent widget */ void openCustomOptionsDialog(const NetworkItemPtr &item); /** * This function adds custom options for a single network item to the list * of options. If there already are options defined for that network item, * they are updated. * * Please note that this function will store a copy of @p options and not * the original object. * * @param options The custom options */ void addCustomOptions(const OptionsPtr &options); /** * This function removes custom options for a single network item from the * list of options. * * @param options The custom options */ void removeCustomOptions(const OptionsPtr &options); /** * This function returns a list of custom option objects that have * Wake-On-LAN features defined. * * @returns a list of custom options objects with WOL features defined. */ QList wakeOnLanEntries() const; /** * Reload custom options from the file. */ void resetCustomOptions(); protected Q_SLOTS: /** * Called when the application exits */ void slotAboutToQuit(); private: /** * Read custom options */ void readCustomOptions(); /** * This function writes the custom options to the disk. */ void writeCustomOptions(); /** * Migrates one profile to another. * * This function is meant to be used by the profile manager. * * @param from The name of the old profile. * @param to The name of the new profile. */ void migrateProfile(const QString &from, const QString &to); /** * Removes a profile from the list of profiles. * * This function is meant to be used by the profile manager. * * @param name The name of the profile. */ void removeProfile(const QString &name); /** * Pointer to Smb4KCustomOptionsManagerPrivate class */ const QScopedPointer d; }; #endif diff --git a/core/smb4kglobal.cpp b/core/smb4kglobal.cpp index fe70060..6b301d7 100644 --- a/core/smb4kglobal.cpp +++ b/core/smb4kglobal.cpp @@ -1,1204 +1,1213 @@ /*************************************************************************** This is the global namespace for Smb4K. ------------------- begin : Sa Apr 2 2005 copyright : (C) 2005-2019 by Alexander Reinholdt email : alexander.reinholdt@kdemail.net ***************************************************************************/ /*************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,* * MA 02110-1335, USA * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include #endif // application specific includes #include "smb4kglobal.h" #include "smb4kglobal_p.h" #include "smb4knotification.h" -#include "smb4kmounter.h" #include "smb4ksynchronizer.h" #include "smb4kclient.h" +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) +#include "smb4kmounter.h" +#endif + // Qt includes #include #include #include #include // KDE includes #include #include Q_GLOBAL_STATIC(Smb4KGlobalPrivate, p); QMutex mutex(QMutex::Recursive /* needed to avoid dead-locks */); void Smb4KGlobal::initCore(bool modifyCursor, bool initClasses) { if (!p->coreInitialized) { // // Busy cursor // p->modifyCursor = modifyCursor; // // Initialize the necessary core classes // if (initClasses) { Smb4KClient::self()->start(); +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) Smb4KMounter::self()->start(); +#endif } else { // Do nothing } p->coreInitialized = true; } else { // Do nothing } } void Smb4KGlobal::abortCore() { Smb4KClient::self()->abort(); +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) Smb4KMounter::self()->abort(); +#endif Smb4KSynchronizer::self()->abort(); } bool Smb4KGlobal::coreIsRunning() { return (Smb4KClient::self()->isRunning() || +#if !defined(SMB4K_UNSUPPORTED_PLATFORM) Smb4KMounter::self()->isRunning() || +#endif Smb4KSynchronizer::self()->isRunning()); } bool Smb4KGlobal::coreIsInitialized() { return p->coreInitialized; } const QList &Smb4KGlobal::workgroupsList() { return p->workgroupsList; } WorkgroupPtr Smb4KGlobal::findWorkgroup(const QString &name) { WorkgroupPtr workgroup; mutex.lock(); for (const WorkgroupPtr &w : p->workgroupsList) { if (QString::compare(w->workgroupName(), name, Qt::CaseInsensitive) == 0) { workgroup = w; break; } else { // Do nothing } } mutex.unlock(); return workgroup; } bool Smb4KGlobal::addWorkgroup(WorkgroupPtr workgroup) { Q_ASSERT(workgroup); bool added = false; if (workgroup) { mutex.lock(); if (!findWorkgroup(workgroup->workgroupName())) { p->workgroupsList.append(workgroup); added = true; } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return added; } bool Smb4KGlobal::updateWorkgroup(WorkgroupPtr workgroup) { Q_ASSERT(workgroup); bool updated = false; if (workgroup) { mutex.lock(); WorkgroupPtr existingWorkgroup = findWorkgroup(workgroup->workgroupName()); if (existingWorkgroup) { existingWorkgroup->update(workgroup.data()); updated = true; } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return updated; } bool Smb4KGlobal::removeWorkgroup(WorkgroupPtr workgroup) { Q_ASSERT(workgroup); bool removed = false; if (workgroup) { mutex.lock(); int index = p->workgroupsList.indexOf(workgroup); if (index != -1) { // The workgroup was found. Remove it. p->workgroupsList.takeAt(index).clear(); removed = true; } else { // Try harder to find the workgroup. WorkgroupPtr wg = findWorkgroup(workgroup->workgroupName()); if (wg) { index = p->workgroupsList.indexOf(wg); if (index != -1) { p->workgroupsList.takeAt(index).clear(); removed = true; } else { // Do nothing } } else { // Do nothing } workgroup.clear(); } mutex.unlock(); } else { // Do nothing } return removed; } void Smb4KGlobal::clearWorkgroupsList() { mutex.lock(); while (!p->workgroupsList.isEmpty()) { p->workgroupsList.takeFirst().clear(); } mutex.unlock(); } const QList &Smb4KGlobal::hostsList() { return p->hostsList; } HostPtr Smb4KGlobal::findHost(const QString &name, const QString &workgroup) { HostPtr host; mutex.lock(); for (const HostPtr &h : p->hostsList) { if ((workgroup.isEmpty() || QString::compare(h->workgroupName(), workgroup, Qt::CaseInsensitive) == 0) && QString::compare(h->hostName(), name, Qt::CaseInsensitive) == 0) { host = h; break; } else { // Do nothing } } mutex.unlock(); return host; } bool Smb4KGlobal::addHost(HostPtr host) { Q_ASSERT(host); bool added = false; if (host) { mutex.lock(); if (!findHost(host->hostName(), host->workgroupName())) { p->hostsList.append(host); added = true; } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return added; } bool Smb4KGlobal::updateHost(HostPtr host) { Q_ASSERT(host); bool updated = false; if (host) { mutex.lock(); HostPtr existingHost = findHost(host->hostName(), host->workgroupName()); if (existingHost) { existingHost->update(host.data()); updated = true; } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return updated; } bool Smb4KGlobal::removeHost(HostPtr host) { Q_ASSERT(host); bool removed = false; if (host) { mutex.lock(); int index = p->hostsList.indexOf(host); if (index != -1) { // The host was found. Remove it. p->hostsList.takeAt(index).clear(); removed = true; } else { // Try harder to find the host. HostPtr h = findHost(host->hostName(), host->workgroupName()); if (h) { index = p->hostsList.indexOf(h); if (index != -1) { p->hostsList.takeAt(index).clear(); removed = true; } else { // Do nothing } } else { // Do nothing } host.clear(); } mutex.unlock(); } else { // Do nothing } return removed; } void Smb4KGlobal::clearHostsList() { mutex.lock(); while (!p->hostsList.isEmpty()) { p->hostsList.takeFirst().clear(); } mutex.unlock(); } QList Smb4KGlobal::workgroupMembers(WorkgroupPtr workgroup) { QList hosts; mutex.lock(); for (const HostPtr &h : p->hostsList) { if (QString::compare(h->workgroupName(), workgroup->workgroupName(), Qt::CaseInsensitive) == 0) { hosts << h; } else { // Do nothing } } mutex.unlock(); return hosts; } const QList &Smb4KGlobal::sharesList() { return p->sharesList; } SharePtr Smb4KGlobal::findShare(const QUrl& url, const QString& workgroup) { SharePtr share; mutex.lock(); for (const SharePtr &s : p->sharesList) { if (QString::compare(s->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), url.toString(QUrl::RemoveUserInfo|QUrl::RemovePort), Qt::CaseInsensitive) == 0 && (workgroup.isEmpty() || QString::compare(s->workgroupName(), workgroup, Qt::CaseInsensitive) == 0)) { share = s; break; } else { // Do nothing } } mutex.unlock(); return share; } bool Smb4KGlobal::addShare(SharePtr share) { Q_ASSERT(share); bool added = false; if (share) { mutex.lock(); // // Add the share // if (!findShare(share->url(), share->workgroupName())) { // // Set the share mounted // Only honor shares that are owned by the user // QList mountedShares = findShareByUrl(share->url()); if (!mountedShares.isEmpty()) { for (const SharePtr &s : mountedShares) { if (!s->isForeign()) { share->setMountData(s.data()); break; } else { continue; } } } else { // Do nothing } // // Add it // p->sharesList.append(share); added = true; } else { // Do nothing } } else { // Do nothing } mutex.unlock(); return added; } bool Smb4KGlobal::updateShare(SharePtr share) { Q_ASSERT(share); bool updated = false; if (share) { mutex.lock(); // // Updated the share // SharePtr existingShare = findShare(share->url(), share->workgroupName()); if (existingShare) { // // Set the share mounted // Only honor shares that are owned by the user // QList mountedShares = findShareByUrl(share->url()); if (!mountedShares.isEmpty()) { for (const SharePtr &s : mountedShares) { if (!s->isForeign()) { share->setMountData(s.data()); break; } else { continue; } } } else { // Do nothing } // // Update it // existingShare->update(share.data()); updated = true; } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return updated; } bool Smb4KGlobal::removeShare(SharePtr share) { Q_ASSERT(share); bool removed = false; if (share) { mutex.lock(); int index = p->sharesList.indexOf(share); if (index != -1) { // The share was found. Remove it. p->sharesList.takeAt(index).clear(); removed = true; } else { // Try harder to find the share. SharePtr s = findShare(share->url(), share->workgroupName()); if (s) { index = p->sharesList.indexOf(s); if (index != -1) { p->sharesList.takeAt(index).clear(); removed = true; } else { // Do nothing } } else { // Do nothing } share.clear(); } mutex.unlock(); } else { // Do nothing } return removed; } void Smb4KGlobal::clearSharesList() { mutex.lock(); while (!p->sharesList.isEmpty()) { p->sharesList.takeFirst().clear(); } mutex.unlock(); } QList Smb4KGlobal::sharedResources(HostPtr host) { QList shares; mutex.lock(); for (const SharePtr &s : p->sharesList) { if (QString::compare(s->hostName(), host->hostName(), Qt::CaseInsensitive) == 0 && QString::compare(s->workgroupName(), host->workgroupName(), Qt::CaseInsensitive) == 0) { shares += s; } else { // Do nothing } } mutex.unlock(); return shares; } const QList &Smb4KGlobal::mountedSharesList() { return p->mountedSharesList; } SharePtr Smb4KGlobal::findShareByPath(const QString &path) { SharePtr share; mutex.lock(); if (!path.isEmpty() && !p->mountedSharesList.isEmpty()) { for (const SharePtr &s : p->mountedSharesList) { if (QString::compare(s->path(), path, Qt::CaseInsensitive) == 0 || QString::compare(s->canonicalPath(), path, Qt::CaseInsensitive) == 0) { share = s; break; } else { // Do nothing } } } else { // Do nothing } mutex.unlock(); return share; } QList Smb4KGlobal::findShareByUrl(const QUrl &url) { QList shares; mutex.lock(); if (!url.isEmpty() && url.isValid() && !p->mountedSharesList.isEmpty()) { for (const SharePtr &s : p->mountedSharesList) { if (QString::compare(s->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), url.toString(QUrl::RemoveUserInfo|QUrl::RemovePort), Qt::CaseInsensitive) == 0) { shares << s; } else { // Do nothing } } } else { // Do nothing } mutex.unlock(); return shares; } QList Smb4KGlobal::findInaccessibleShares() { QList inaccessibleShares; mutex.lock(); for (const SharePtr &s : p->mountedSharesList) { if (s->isInaccessible()) { inaccessibleShares += s; } else { // Do nothing } } mutex.unlock(); return inaccessibleShares; } bool Smb4KGlobal::addMountedShare(SharePtr share) { Q_ASSERT(share); bool added = false; if (share) { mutex.lock(); // // Copy the mount data to the network share and the search results. // Only honor shares that were mounted by the user. // if (!share->isForeign()) { // Network shares SharePtr networkShare = findShare(share->url(), share->workgroupName()); if (networkShare) { networkShare->setMountData(share.data()); } else { // Do nothing } } else { // Do nothing } if (!findShareByPath(share->path())) { // // Check if we have to add a workgroup name and/or IP address // HostPtr networkHost = findHost(share->hostName(), share->workgroupName()); if (networkHost) { // Set the IP address if (!share->hasHostIpAddress() || networkHost->ipAddress() != share->hostIpAddress()) { share->setHostIpAddress(networkHost->ipAddress()); } else { // Do nothing } // Set the workgroup name if (share->workgroupName().isEmpty()) { share->setWorkgroupName(networkHost->workgroupName()); } else { // Do nothing } } else { // Do nothing } p->mountedSharesList.append(share); added = true; p->onlyForeignShares = true; for (const SharePtr &s : p->mountedSharesList) { if (!s->isForeign()) { p->onlyForeignShares = false; break; } else { // Do nothing } } } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return added; } bool Smb4KGlobal::updateMountedShare(SharePtr share) { Q_ASSERT(share); bool updated = false; if (share) { mutex.lock(); // // Copy the mount data to the network share (needed for unmounting from the network browser) // Only honor shares that were mounted by the user. // if (!share->isForeign()) { SharePtr networkShare = findShare(share->url(), share->workgroupName()); if (networkShare) { networkShare->setMountData(share.data()); } else { // Do nothing } } else { // Do nothing } SharePtr mountedShare = findShareByPath(share->path()); if (mountedShare) { // // Check if we have to add a workgroup name and/or IP address // HostPtr networkHost = findHost(share->hostName(), share->workgroupName()); if (networkHost) { // Set the IP address if (!share->hasHostIpAddress() || networkHost->ipAddress() != share->hostIpAddress()) { share->setHostIpAddress(networkHost->ipAddress()); } else { // Do nothing } // Set the workgroup name if (share->workgroupName().isEmpty()) { share->setWorkgroupName(networkHost->workgroupName()); } else { // Do nothing } } else { // Do nothing } // // Update share // mountedShare->setMountData(share.data()); updated = true; } else { // Do nothing } mutex.unlock(); } else { // Do nothing } return updated; } bool Smb4KGlobal::removeMountedShare(SharePtr share) { Q_ASSERT(share); bool removed = false; if (share) { mutex.lock(); // // Reset the mount data for the network share and the // search result // if (!share->isForeign()) { // Network share SharePtr networkShare = findShare(share->url(), share->workgroupName()); if (networkShare) { networkShare->resetMountData(); } else { // Do nothing } } else { // Do nothing } // // Remove the mounted share // int index = p->mountedSharesList.indexOf(share); if (index != -1) { // The share was found. Remove it. p->mountedSharesList.takeAt(index).clear(); removed = true; } else { // Try harder to find the share. SharePtr s = findShareByPath(share->isInaccessible() ? share->path() : share->canonicalPath()); if (s) { index = p->mountedSharesList.indexOf(s); if (index != -1) { p->mountedSharesList.takeAt(index).clear(); removed = true; } else { // Do nothing } } else { // Do nothing } share.clear(); } for (const SharePtr &s : p->mountedSharesList) { if (!s->isForeign()) { p->onlyForeignShares = false; break; } else { // Do nothing } } mutex.unlock(); } else { // Do nothing } return removed; } bool Smb4KGlobal::onlyForeignMountedShares() { return p->onlyForeignShares; } void Smb4KGlobal::openShare(SharePtr share, OpenWith openWith) { if (!share || share->isInaccessible()) { return; } switch (openWith) { case FileManager: { QUrl url = QUrl::fromLocalFile(share->canonicalPath()); (void) new KRun(url, 0); break; } case Konsole: { QString konsole = QStandardPaths::findExecutable("konsole"); if (konsole.isEmpty()) { Smb4KNotification::commandNotFound("konsole"); } else { KRun::runCommand(konsole+" --workdir "+KShell::quoteArg(share->canonicalPath()), 0); } break; } default: { break; } } } const QMap &Smb4KGlobal::globalSambaOptions() { return p->globalSambaOptions(); } bool Smb4KGlobal::modifyCursor() { return p->modifyCursor; } #if defined(Q_OS_LINUX) QStringList Smb4KGlobal::whitelistedMountArguments() { return p->whitelistedMountArguments; } #endif const QString Smb4KGlobal::findMountExecutable() { QStringList paths; paths << "/bin"; paths << "/sbin"; paths << "/usr/bin"; paths << "/usr/sbin"; paths << "/usr/local/bin"; paths << "/usr/local/sbin"; #if defined(Q_OS_LINUX) return QStandardPaths::findExecutable("mount.cifs", paths); #elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) return QStandardPaths::findExecutable("mount_smbfs", paths); #else return QString(); #endif } const QString Smb4KGlobal::findUmountExecutable() { QStringList paths; paths << "/bin"; paths << "/sbin"; paths << "/usr/bin"; paths << "/usr/sbin"; paths << "/usr/local/bin"; paths << "/usr/local/sbin"; return QStandardPaths::findExecutable("umount", paths); } const QString Smb4KGlobal::dataLocation() { return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+QDir::separator()+"smb4k"; }