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

0001 /***************************************************************************
0002     Smb4K's container class for information about a host.
0003                              -------------------
0004     begin                : Sa Jan 26 2008
0005     copyright            : (C) 2008-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 "smb4khost.h"
0032 #include "smb4kauthinfo.h"
0033 
0034 // Qt includes
0035 #include <QStringList>
0036 #include <QDebug>
0037 #include <QUrl>
0038 
0039 // KDE includes
0040 #include <KIconThemes/KIconLoader>
0041 
0042 
0043 class Smb4KHostPrivate
0044 {
0045   public:
0046     QString workgroup;
0047     QHostAddress ip;
0048     QString comment;
0049     bool isMaster;
0050 };
0051 
0052 
0053 Smb4KHost::Smb4KHost(const QString &name)
0054 : Smb4KBasicNetworkItem(Host), d(new Smb4KHostPrivate)
0055 {
0056   d->isMaster = false;
0057   *pIcon = KDE::icon("network-server");
0058   setHostName(name);
0059 }
0060 
0061 
0062 Smb4KHost::Smb4KHost(const Smb4KHost &h)
0063 : Smb4KBasicNetworkItem(Host), d(new Smb4KHostPrivate)
0064 {
0065   *d = *h.d;
0066   
0067   if (pIcon->isNull())
0068   {
0069     *pIcon = KDE::icon("network-server");
0070   }
0071 }
0072 
0073 
0074 Smb4KHost::Smb4KHost()
0075 : Smb4KBasicNetworkItem(Host), d(new Smb4KHostPrivate)
0076 {
0077   d->isMaster = false;
0078   *pIcon = KDE::icon("network-server");
0079 }
0080 
0081 
0082 Smb4KHost::~Smb4KHost()
0083 {
0084 }
0085 
0086 
0087 void Smb4KHost::setHostName(const QString &name)
0088 {
0089   pUrl->setHost(name);
0090   pUrl->setScheme("smb");
0091 }
0092 
0093 
0094 QString Smb4KHost::hostName() const
0095 {
0096   return pUrl->host().toUpper();
0097 }
0098 
0099 
0100 void Smb4KHost::setWorkgroupName(const QString &workgroup)
0101 {
0102   d->workgroup = workgroup.toUpper();
0103 }
0104 
0105 
0106 QString Smb4KHost::workgroupName() const
0107 {
0108   return d->workgroup;
0109 }
0110 
0111 
0112 void Smb4KHost::setIpAddress(const QString &ip)
0113 {
0114   d->ip.setAddress(ip);
0115 }
0116 
0117 
0118 void Smb4KHost::setIpAddress(const QHostAddress& address)
0119 {
0120   if (!address.isNull() && address.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol)
0121   {
0122     d->ip = address;
0123   }
0124 }
0125 
0126 
0127 QString Smb4KHost::ipAddress() const
0128 {
0129   return d->ip.toString();
0130 }
0131 
0132 
0133 bool Smb4KHost::hasIpAddress() const
0134 {
0135   return !d->ip.isNull();
0136 }
0137 
0138 
0139 void Smb4KHost::setComment(const QString &comment)
0140 {
0141   d->comment = comment;
0142 }
0143 
0144 
0145 QString Smb4KHost::comment() const
0146 {
0147   return d->comment;
0148 }
0149 
0150 
0151 void Smb4KHost::setIsMasterBrowser(bool master)
0152 {
0153   d->isMaster = master;
0154 }
0155 
0156 
0157 bool Smb4KHost::isMasterBrowser() const
0158 {
0159   return d->isMaster;
0160 }
0161 
0162 
0163 void Smb4KHost::setLogin(const QString &login)
0164 {
0165   pUrl->setUserName(login);
0166 }
0167 
0168 
0169 QString Smb4KHost::login() const
0170 {
0171   return pUrl->userName();
0172 }
0173 
0174 
0175 void Smb4KHost::setPassword(const QString &passwd)
0176 {
0177   pUrl->setPassword(passwd);
0178 }
0179 
0180 
0181 QString Smb4KHost::password() const
0182 {
0183   return pUrl->password();
0184 }
0185 
0186 
0187 void Smb4KHost::setPort(int port)
0188 {
0189   pUrl->setPort(port);
0190 }
0191 
0192 
0193 int Smb4KHost::port() const
0194 {
0195   return pUrl->port();
0196 }
0197 
0198 
0199 void Smb4KHost::setAuthInfo(Smb4KAuthInfo *authInfo)
0200 {
0201   pUrl->setUserName(authInfo->userName());
0202   pUrl->setPassword(authInfo->password());
0203 }
0204 
0205 
0206 void Smb4KHost::update(Smb4KHost* host)
0207 {
0208   if (QString::compare(workgroupName(), host->workgroupName()) == 0 &&
0209       QString::compare(hostName(), host->hostName()) == 0)
0210   {
0211     *pUrl = host->url();
0212     setComment(host->comment());
0213     setIsMasterBrowser(host->isMasterBrowser());
0214     
0215     // Do not kill the already discovered IP address
0216     if (!hasIpAddress() && host->hasIpAddress())
0217     {
0218       setIpAddress(host->ipAddress());
0219     }
0220   }
0221 }
0222