File indexing completed on 2024-04-28 12:45:23

0001 /*
0002     This class provides a container for the authentication data.
0003 
0004     SPDX-FileCopyrightText: 2004-2022 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4kauthinfo.h"
0010 #include "smb4khost.h"
0011 #include "smb4kshare.h"
0012 
0013 // Qt includes
0014 #include <QDebug>
0015 #include <QHostAddress>
0016 
0017 // KDE includes
0018 #include <KLocalizedString>
0019 
0020 class Smb4KAuthInfoPrivate
0021 {
0022 public:
0023     QUrl url;
0024     NetworkItem type;
0025     QHostAddress ip;
0026 };
0027 
0028 Smb4KAuthInfo::Smb4KAuthInfo(Smb4KBasicNetworkItem *item)
0029     : d(new Smb4KAuthInfoPrivate)
0030 {
0031     d->type = item->type();
0032 
0033     switch (d->type) {
0034     case Host: {
0035         Smb4KHost *host = static_cast<Smb4KHost *>(item);
0036 
0037         if (host) {
0038             d->url = host->url();
0039             d->ip.setAddress(host->ipAddress());
0040         }
0041 
0042         break;
0043     }
0044     case Share: {
0045         Smb4KShare *share = static_cast<Smb4KShare *>(item);
0046 
0047         if (share) {
0048             d->url = !share->isHomesShare() ? share->homeUrl() : share->url();
0049             d->ip.setAddress(share->hostIpAddress());
0050         }
0051 
0052         break;
0053     }
0054     default: {
0055         break;
0056     }
0057     }
0058 }
0059 
0060 Smb4KAuthInfo::Smb4KAuthInfo()
0061     : d(new Smb4KAuthInfoPrivate)
0062 {
0063     d->type = UnknownNetworkItem;
0064     d->url.clear();
0065     d->ip.clear();
0066 }
0067 
0068 Smb4KAuthInfo::Smb4KAuthInfo(const Smb4KAuthInfo &i)
0069     : d(new Smb4KAuthInfoPrivate)
0070 {
0071     *d = *i.d;
0072 }
0073 
0074 Smb4KAuthInfo::~Smb4KAuthInfo()
0075 {
0076 }
0077 
0078 void Smb4KAuthInfo::setUrl(const QUrl &url)
0079 {
0080     d->url = url;
0081     d->url.setScheme(QStringLiteral("smb"));
0082 
0083     //
0084     // Set the type
0085     //
0086     if (!d->url.path().remove(QStringLiteral("/")).isEmpty()) {
0087         d->type = Share;
0088 
0089         //
0090         // Fix 'homes' URLs
0091         //
0092         if (d->url.path().remove(QStringLiteral("/")) == QStringLiteral("homes") && !d->url.userName().isEmpty()) {
0093             d->url.setPath(d->url.userName());
0094         }
0095     } else {
0096         d->type = Host;
0097     }
0098 }
0099 
0100 QUrl Smb4KAuthInfo::url() const
0101 {
0102     return d->url;
0103 }
0104 
0105 void Smb4KAuthInfo::setUserName(const QString &username)
0106 {
0107     d->url.setUserName(username);
0108 
0109     if (d->url.path().remove(QStringLiteral("/")) == QStringLiteral("homes")) {
0110         d->url.setPath(username);
0111     }
0112 }
0113 
0114 QString Smb4KAuthInfo::userName() const
0115 {
0116     return d->url.userName();
0117 }
0118 
0119 void Smb4KAuthInfo::setPassword(const QString &passwd)
0120 {
0121     d->url.setPassword(passwd);
0122 }
0123 
0124 QString Smb4KAuthInfo::password() const
0125 {
0126     return d->url.password();
0127 }
0128 
0129 Smb4KGlobal::NetworkItem Smb4KAuthInfo::type() const
0130 {
0131     return d->type;
0132 }
0133 
0134 QString Smb4KAuthInfo::displayString() const
0135 {
0136     //
0137     // Host name
0138     //
0139     QString hostName = d->url.host().toUpper();
0140 
0141     //
0142     // Return only the host name if the network item
0143     // has type Smb4Global:Host
0144     //
0145     if (d->type == Host) {
0146         return hostName;
0147     }
0148 
0149     //
0150     // Share name
0151     //
0152     QString shareName = d->url.path().remove(QStringLiteral("/"));
0153 
0154     //
0155     // Return the full display string
0156     //
0157     return i18n("%1 on %2", shareName, hostName);
0158 }