File indexing completed on 2024-04-21 05:01:35

0001 /*
0002     This class provides the basic network item for the core library of
0003     Smb4K.
0004 
0005     SPDX-FileCopyrightText: 2009-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // application specific includes
0010 #include "smb4kbasicnetworkitem.h"
0011 
0012 // Qt includes
0013 #include <QDebug>
0014 #include <QtGlobal>
0015 
0016 using namespace Smb4KGlobal;
0017 
0018 class Smb4KBasicNetworkItemPrivate
0019 {
0020 public:
0021     NetworkItem type;
0022     QIcon icon;
0023     QUrl url;
0024     bool dnsDiscovered;
0025     QString comment;
0026 };
0027 
0028 Smb4KBasicNetworkItem::Smb4KBasicNetworkItem(NetworkItem type)
0029     : d(new Smb4KBasicNetworkItemPrivate)
0030 {
0031     d->type = type;
0032     d->dnsDiscovered = false;
0033 
0034     pUrl = &d->url;
0035     pIcon = &d->icon;
0036     pComment = &d->comment;
0037     pType = &d->type;
0038 }
0039 
0040 Smb4KBasicNetworkItem::Smb4KBasicNetworkItem(const Smb4KBasicNetworkItem &item)
0041     : d(new Smb4KBasicNetworkItemPrivate)
0042 {
0043     *d = *item.d;
0044 
0045     pUrl = &d->url;
0046     pIcon = &d->icon;
0047     pComment = &d->comment;
0048     pType = &d->type;
0049 }
0050 
0051 Smb4KBasicNetworkItem::~Smb4KBasicNetworkItem()
0052 {
0053 }
0054 
0055 void Smb4KBasicNetworkItem::setType(Smb4KGlobal::NetworkItem type) const
0056 {
0057     d->type = type;
0058 }
0059 
0060 Smb4KGlobal::NetworkItem Smb4KBasicNetworkItem::type() const
0061 {
0062     return d->type;
0063 }
0064 
0065 void Smb4KBasicNetworkItem::setIcon(const QIcon &icon) const
0066 {
0067     d->icon = icon;
0068 }
0069 
0070 QIcon Smb4KBasicNetworkItem::icon() const
0071 {
0072     return d->icon;
0073 }
0074 
0075 void Smb4KBasicNetworkItem::setUrl(const QUrl &url) const
0076 {
0077     //
0078     // Check that the URL is valid
0079     //
0080     if (!url.isValid()) {
0081         return;
0082     }
0083 
0084     //
0085     // Do some checks depending on the type of the network item
0086     //
0087     switch (d->type) {
0088     case Network: {
0089         break;
0090     }
0091     case Workgroup:
0092     case Host: {
0093         //
0094         // Check that the host name is present and there is no path
0095         //
0096         if (url.host().isEmpty() || !url.path().isEmpty()) {
0097             return;
0098         }
0099 
0100         break;
0101     }
0102     case Share: {
0103         //
0104         // Check that the share name is present
0105         //
0106         if (url.path().isEmpty() || (url.path().size() == 1 && url.path().endsWith(QStringLiteral("/")))) {
0107             return;
0108         }
0109 
0110         break;
0111     }
0112     default: {
0113         break;
0114     }
0115     }
0116 
0117     d->url = url;
0118     d->url.setScheme(QStringLiteral("smb"));
0119 }
0120 
0121 QUrl Smb4KBasicNetworkItem::url() const
0122 {
0123     return d->url;
0124 }
0125 
0126 void Smb4KBasicNetworkItem::setDnsDiscovered(bool discovered) const
0127 {
0128     d->dnsDiscovered = discovered;
0129 }
0130 
0131 bool Smb4KBasicNetworkItem::dnsDiscovered() const
0132 {
0133     return d->dnsDiscovered;
0134 }
0135 
0136 void Smb4KBasicNetworkItem::setComment(const QString &comment) const
0137 {
0138     d->comment = comment;
0139 }
0140 
0141 QString Smb4KBasicNetworkItem::comment() const
0142 {
0143     return d->comment;
0144 }
0145 
0146 bool Smb4KBasicNetworkItem::hasUserInfo() const
0147 {
0148     return !d->url.userInfo().isEmpty();
0149 }
0150 
0151 Smb4KBasicNetworkItem &Smb4KBasicNetworkItem::operator=(const Smb4KBasicNetworkItem &other)
0152 {
0153     *d = *other.d;
0154     return *this;
0155 }