File indexing completed on 2024-04-21 15:42:43

0001 /***************************************************************************
0002     This class provides a container for the authentication data.
0003                              -------------------
0004     begin                : Sa Feb 28 2004
0005     copyright            : (C) 2004-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 "smb4kauthinfo.h"
0032 #include "smb4khost.h"
0033 #include "smb4kshare.h"
0034 
0035 // Qt includes
0036 #include <QHostAddress>
0037 #include <QDebug>
0038 
0039 // KDE includes
0040 #include <KI18n/KLocalizedString>
0041 
0042 
0043 class Smb4KAuthInfoPrivate
0044 {
0045   public:
0046     QUrl url;
0047     QString workgroup;
0048     NetworkItem type;
0049     bool homesShare;
0050     QHostAddress ip;
0051 };
0052 
0053 
0054 Smb4KAuthInfo::Smb4KAuthInfo(Smb4KBasicNetworkItem* item)
0055 : d(new Smb4KAuthInfoPrivate)
0056 {
0057   d->type = item->type();
0058 
0059   switch (d->type)
0060   {
0061     case Host:
0062     {
0063       Smb4KHost *host = static_cast<Smb4KHost *>(item);
0064       
0065       if (host)
0066       {
0067         d->url = host->url();
0068         d->workgroup = host->workgroupName();
0069         d->homesShare = false;
0070         d->ip.setAddress(host->ipAddress());
0071       }
0072       
0073       break;
0074     }
0075     case Share:
0076     {
0077       Smb4KShare *share = static_cast<Smb4KShare *>(item);
0078       
0079       if (share)
0080       {
0081         d->url = !share->isHomesShare() ? share->homeUrl() : share->url();
0082         d->workgroup = share->workgroupName();
0083         d->homesShare = share->isHomesShare();
0084         d->ip.setAddress(share->hostIpAddress());
0085       }
0086       
0087       break;
0088     }
0089     default:
0090     {
0091       break;
0092     }
0093   }
0094 }
0095 
0096 
0097 Smb4KAuthInfo::Smb4KAuthInfo()
0098 : d(new Smb4KAuthInfoPrivate)
0099 {
0100   d->type = UnknownNetworkItem;
0101   d->homesShare = false;
0102   d->url.clear();
0103   d->workgroup.clear();
0104   d->ip.clear();
0105 }
0106 
0107 
0108 Smb4KAuthInfo::Smb4KAuthInfo(const Smb4KAuthInfo &i)
0109 : d(new Smb4KAuthInfoPrivate)
0110 {
0111   *d = *i.d;
0112 }
0113 
0114 
0115 Smb4KAuthInfo::~Smb4KAuthInfo()
0116 {
0117 }
0118 
0119 
0120 void Smb4KAuthInfo::setWorkgroupName(const QString &workgroup)
0121 {
0122   d->workgroup = workgroup;
0123 }
0124 
0125 
0126 QString Smb4KAuthInfo::workgroupName() const
0127 {
0128   return d->workgroup;
0129 }
0130 
0131 
0132 void Smb4KAuthInfo::setUrl(const QUrl &url)
0133 {
0134   d->url = url;
0135   d->url.setScheme("smb");
0136 
0137   // Set the type.
0138   if (!d->url.path().isEmpty() && d->url.path().length() > 1 && !d->url.path().endsWith('/'))
0139   {
0140     d->type = Share;
0141   }
0142   else
0143   {
0144     d->type = Host;
0145   }
0146   
0147   // Determine whether this is a homes share.
0148   d->homesShare = (QString::compare(d->url.path().remove('/'), "homes", Qt::CaseSensitive) == 0);
0149 }
0150 
0151 
0152 void Smb4KAuthInfo::setUrl(const QString &url)
0153 {
0154   QUrl tempUrl(url, QUrl::TolerantMode);
0155   tempUrl.setScheme("smb");
0156   setUrl(tempUrl);
0157 }
0158 
0159 
0160 
0161 QUrl Smb4KAuthInfo::url() const
0162 {
0163   return d->url;
0164 }
0165 
0166 
0167 QString Smb4KAuthInfo::hostName() const
0168 {
0169   return d->url.host().toUpper();
0170 }
0171 
0172 
0173 
0174 QString Smb4KAuthInfo::shareName() const
0175 {
0176   if (d->url.path().startsWith('/'))
0177   {
0178     return d->url.path().remove(0, 1);
0179   }
0180 
0181   return d->url.path();
0182 }
0183 
0184 
0185 void Smb4KAuthInfo::setUserName(const QString &username)
0186 {
0187   d->url.setUserName(username);
0188 
0189   if (d->homesShare)
0190   {
0191     d->url.setPath(username);
0192   }
0193 }
0194 
0195 
0196 QString Smb4KAuthInfo::userName() const
0197 {
0198   return d->url.userName();
0199 }
0200 
0201 
0202 void Smb4KAuthInfo::setPassword(const QString &passwd)
0203 {
0204   d->url.setPassword(passwd);
0205 }
0206 
0207 
0208 QString Smb4KAuthInfo::password() const
0209 {
0210   return d->url.password();
0211 }
0212 
0213 
0214 Smb4KGlobal::NetworkItem Smb4KAuthInfo::type() const
0215 {
0216   return d->type;
0217 }
0218 
0219 
0220 bool Smb4KAuthInfo::isHomesShare() const
0221 {
0222   return d->homesShare;
0223 }
0224 
0225 
0226 void Smb4KAuthInfo::setIpAddress(const QString &ip)
0227 {
0228   d->ip.setAddress(ip);
0229 }
0230 
0231 
0232 QString Smb4KAuthInfo::ipAddress() const
0233 {
0234   return d->ip.toString();
0235 }
0236 
0237 
0238 QString Smb4KAuthInfo::displayString() const
0239 {
0240   return i18n("%1 on %2", shareName(), hostName());
0241 }
0242 
0243