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

0001 /***************************************************************************
0002     Smb4K's container class for information about a share.
0003                              -------------------
0004     begin                : Mo Jan 28 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 "smb4kshare.h"
0032 #include "smb4kauthinfo.h"
0033 
0034 // Qt include
0035 #include <QDir>
0036 #include <QUrl>
0037 
0038 // KDE includes
0039 #define TRANSLATION_DOMAIN "smb4k-core"
0040 #include <KI18n/KLocalizedString>
0041 #include <KIOCore/KIO/Global>
0042 #include <KIconThemes/KIconLoader>
0043 #include <KIOCore/KMountPoint>
0044 #include "kiconthemes_version.h"
0045 
0046 
0047 class Smb4KSharePrivate
0048 {
0049   public:
0050     QString workgroup;
0051     QString comment;
0052     QHostAddress ip;
0053     QString path;
0054     bool inaccessible;
0055     bool foreign;
0056     KUser user;
0057     KUserGroup group;
0058     qulonglong totalSpace;
0059     qulonglong freeSpace;
0060     qulonglong usedSpace;
0061     bool mounted;
0062     QString filesystem;
0063     Smb4KGlobal::ShareType shareType;
0064 };
0065 
0066 
0067 Smb4KShare::Smb4KShare(const QString &host, const QString &name)
0068 : Smb4KBasicNetworkItem(Share), d(new Smb4KSharePrivate)
0069 {
0070   d->inaccessible = false;
0071   d->foreign      = false;
0072   d->filesystem   = QString();
0073   d->user         = KUser(KUser::UseRealUserID);
0074   d->group        = KUserGroup(KUser::UseRealUserID);
0075   d->totalSpace   = -1;
0076   d->freeSpace    = -1;
0077   d->usedSpace    = -1;
0078   d->mounted      = false;
0079   d->shareType    = FileShare;
0080   setHostName(host);
0081   setShareName(name);
0082   setShareIcon();
0083 }
0084 
0085 
0086 Smb4KShare::Smb4KShare(const QUrl &url)
0087 : Smb4KBasicNetworkItem(Share), d(new Smb4KSharePrivate)
0088 {
0089   //
0090   // Set the private variables
0091   // 
0092   d->inaccessible = false;
0093   d->foreign      = false;
0094   d->filesystem   = QString();
0095   d->user         = KUser(KUser::UseRealUserID);
0096   d->group        = KUserGroup(KUser::UseRealUserID);
0097   d->totalSpace   = -1;
0098   d->freeSpace    = -1;
0099   d->usedSpace    = -1;
0100   d->mounted      = false;
0101   d->shareType    = FileShare;
0102   
0103   //
0104   // Set the URL
0105   // 
0106   *pUrl = url;
0107     
0108   //
0109   // Set the icon
0110   // 
0111   setShareIcon();
0112 }
0113 
0114 
0115 Smb4KShare::Smb4KShare(const Smb4KShare &s)
0116 : Smb4KBasicNetworkItem(Share), d(new Smb4KSharePrivate)
0117 {
0118   //
0119   // Copy the private variables
0120   // 
0121   *d = *s.d;
0122 
0123   //
0124   // Set the icon if necessary
0125   // 
0126   if (pIcon->isNull())
0127   {
0128     setShareIcon();
0129   }
0130 }
0131 
0132 
0133 
0134 Smb4KShare::Smb4KShare()
0135 : Smb4KBasicNetworkItem(Share), d(new Smb4KSharePrivate)
0136 {
0137   //
0138   // Set the private variables
0139   // 
0140   d->inaccessible = false;
0141   d->foreign      = false;
0142   d->filesystem   = QString();
0143   d->user         = KUser(KUser::UseRealUserID);
0144   d->group        = KUserGroup(KUser::UseRealUserID);
0145   d->totalSpace   = -1;
0146   d->freeSpace    = -1;
0147   d->usedSpace    = -1;
0148   d->mounted      = false;
0149   d->shareType    = FileShare;
0150   
0151   //
0152   // Set the URL
0153   //
0154   pUrl->setScheme("smb");
0155   
0156   //
0157   // Set the icon
0158   // 
0159   setShareIcon();
0160 }
0161 
0162 
0163 Smb4KShare::~Smb4KShare()
0164 {
0165 }
0166 
0167 
0168 void Smb4KShare::setShareName(const QString &name)
0169 {
0170   if (name.startsWith('/'))
0171   {
0172     pUrl->setPath(name.trimmed());
0173   }
0174   else
0175   {
0176     pUrl->setPath('/'+name.trimmed());
0177   }
0178   
0179   pUrl->setScheme("smb");
0180 }
0181 
0182 
0183 QString Smb4KShare::shareName() const
0184 {
0185   return pUrl->path().remove('/');
0186 }
0187 
0188 
0189 void Smb4KShare::setHostName(const QString &hostName)
0190 {
0191   pUrl->setHost(hostName.trimmed());
0192   pUrl->setScheme("smb");
0193 }
0194 
0195 
0196 QString Smb4KShare::hostName() const
0197 {
0198   return pUrl->host().toUpper();
0199 }
0200 
0201 
0202 QUrl Smb4KShare::homeUrl() const
0203 {
0204   QUrl u;
0205   
0206   if (isHomesShare() && !pUrl->userName().isEmpty())
0207   {
0208     u = *pUrl;
0209     u.setPath('/'+pUrl->userName(), QUrl::TolerantMode);
0210   }
0211   
0212   return u;
0213 }
0214 
0215 
0216 QString Smb4KShare::displayString(bool showHomesShare) const
0217 {
0218   if (showHomesShare && isHomesShare())
0219   {
0220     return i18n("%1 on %2", homeUrl().path().remove('/'), hostName());
0221   }
0222   
0223   return i18n("%1 on %2", shareName(), hostName());
0224 }
0225 
0226 
0227 void Smb4KShare::setWorkgroupName(const QString &workgroup)
0228 {
0229   d->workgroup = workgroup;
0230 }
0231 
0232 
0233 QString Smb4KShare::workgroupName() const
0234 {
0235   return d->workgroup;
0236 }
0237 
0238 
0239 void Smb4KShare::setShareType(Smb4KGlobal::ShareType type)
0240 {
0241   d->shareType = type;
0242   setShareIcon();
0243 }
0244 
0245 
0246 Smb4KGlobal::ShareType Smb4KShare::shareType() const
0247 {
0248   return d->shareType;
0249 }
0250 
0251 
0252 QString Smb4KShare::shareTypeString() const
0253 {
0254   QString typeString;
0255   
0256   switch (d->shareType)
0257   {
0258     case FileShare:
0259     {
0260       typeString = i18n("Disk");
0261       break;
0262     }
0263     case PrinterShare:
0264     {
0265       typeString = i18n("Printer");
0266       break;
0267     }
0268     case IpcShare:
0269     {
0270       typeString = i18n("IPC");
0271       break;
0272     }
0273     default:
0274     {
0275       break;
0276     }
0277   }
0278   
0279   return typeString;
0280 }
0281 
0282 
0283 void Smb4KShare::setComment(const QString &comment)
0284 {
0285   d->comment = comment;
0286 }
0287 
0288 
0289 QString Smb4KShare::comment() const
0290 {
0291   return d->comment;
0292 }
0293 
0294 
0295 void Smb4KShare::setHostIpAddress(const QString &ip)
0296 {
0297   d->ip.setAddress(ip);
0298 }
0299 
0300 
0301 void Smb4KShare::setHostIpAddress(const QHostAddress& address)
0302 {
0303   if (!address.isNull() && address.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol)
0304   {
0305     d->ip = address;
0306   }
0307 }
0308 
0309 
0310 QString Smb4KShare::hostIpAddress() const
0311 {
0312   return d->ip.toString();
0313 }
0314 
0315 
0316 bool Smb4KShare::hasHostIpAddress() const
0317 {
0318   return !d->ip.isNull();
0319 }
0320 
0321 
0322 bool Smb4KShare::isHidden() const
0323 {
0324   return pUrl->path().endsWith('$');
0325 }
0326 
0327 
0328 bool Smb4KShare::isPrinter() const
0329 {
0330   return (d->shareType == PrinterShare);
0331 }
0332 
0333 
0334 void Smb4KShare::setPath(const QString &mountpoint)
0335 {
0336   d->path = mountpoint;
0337 }
0338 
0339 
0340 QString Smb4KShare::path() const
0341 {
0342   return d->path;
0343 }
0344 
0345 
0346 QString Smb4KShare::canonicalPath() const
0347 {
0348   return (d->inaccessible ? d->path : QDir(d->path).canonicalPath());
0349 }
0350 
0351 
0352 void Smb4KShare::setInaccessible(bool in)
0353 {
0354   d->inaccessible = in;
0355   setShareIcon();
0356 }
0357 
0358 
0359 bool Smb4KShare::isInaccessible() const
0360 {
0361   return (d->mounted && d->inaccessible);
0362 }
0363 
0364 
0365 void Smb4KShare::setForeign(bool foreign)
0366 {
0367   d->foreign = foreign;
0368   setShareIcon();
0369 }
0370 
0371 
0372 bool Smb4KShare::isForeign() const
0373 {
0374   return (d->mounted && d->foreign);
0375 }
0376 
0377 
0378 QString Smb4KShare::fileSystemString() const
0379 {
0380   if (!path().isEmpty() && d->filesystem.isEmpty())
0381   {
0382     KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(path());
0383     d->filesystem = mp->mountType().toUpper();
0384   }
0385   
0386   return d->filesystem;
0387 }
0388 
0389 
0390 void Smb4KShare::setUser(const KUser &user)
0391 {
0392   d->user = user;
0393 }
0394 
0395 
0396 KUser Smb4KShare::user() const
0397 {
0398   return d->user;
0399 }
0400 
0401 
0402 void Smb4KShare::setGroup(const KUserGroup &group)
0403 {
0404   d->group = group;
0405 }
0406 
0407 
0408 KUserGroup Smb4KShare::group() const
0409 {
0410   return d->group;
0411 }
0412 
0413 
0414 void Smb4KShare::setMounted(bool mounted)
0415 {
0416   if (!isPrinter())
0417   {
0418     d->mounted = mounted;
0419     setShareIcon();
0420   }
0421 }
0422 
0423 
0424 bool Smb4KShare::isMounted() const
0425 {
0426   return d->mounted;
0427 }
0428 
0429 
0430 void Smb4KShare::setTotalDiskSpace(qulonglong size)
0431 {
0432   d->totalSpace = size;
0433 }
0434 
0435 
0436 qulonglong Smb4KShare::totalDiskSpace() const
0437 {
0438   return d->totalSpace;
0439 }
0440 
0441 
0442 QString Smb4KShare::totalDiskSpaceString() const
0443 {
0444   return KIO::convertSize(d->totalSpace);
0445 }
0446 
0447 
0448 void Smb4KShare::setFreeDiskSpace(qulonglong size)
0449 {
0450   d->freeSpace = size;
0451 }
0452 
0453 
0454 qulonglong Smb4KShare::freeDiskSpace() const
0455 {
0456   return d->freeSpace;
0457 }
0458 
0459 
0460 QString Smb4KShare::freeDiskSpaceString() const
0461 {
0462   return KIO::convertSize(d->freeSpace);
0463 }
0464 
0465 
0466 void Smb4KShare::setUsedDiskSpace(qulonglong size)
0467 {
0468   d->usedSpace = size;
0469 }
0470 
0471 
0472 qulonglong Smb4KShare::usedDiskSpace() const
0473 {
0474   return d->usedSpace;
0475 }
0476 
0477 
0478 QString Smb4KShare::usedDiskSpaceString() const
0479 {
0480   return KIO::convertSize(d->usedSpace);
0481 }
0482 
0483 
0484 qreal Smb4KShare::diskUsage() const
0485 {
0486   qreal used(usedDiskSpace());
0487   qreal total(totalDiskSpace());
0488 
0489   if (total > 0)
0490   {
0491     return used * 100 / total;
0492   }
0493   
0494   return 0;
0495 }
0496 
0497 
0498 QString Smb4KShare::diskUsageString() const
0499 {
0500   return QString("%1 %").arg(diskUsage(), 0, 'f', 1);
0501 }
0502 
0503 
0504 void Smb4KShare::setMountData(Smb4KShare *share)
0505 {
0506   Q_ASSERT(share);
0507 
0508   if (QString::compare(url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), 
0509                        share->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort), Qt::CaseInsensitive) == 0 &&
0510       (share->workgroupName().isEmpty() || QString::compare(workgroupName(), share->workgroupName(), Qt::CaseInsensitive) == 0))
0511   {
0512     d->path         = share->path();
0513     d->inaccessible = share->isInaccessible();
0514     d->foreign      = share->isForeign();
0515     d->user         = share->user();
0516     d->group        = share->group();
0517     d->totalSpace   = share->totalDiskSpace();
0518     d->freeSpace    = share->freeDiskSpace();
0519     d->usedSpace    = share->usedDiskSpace();
0520     d->mounted      = share->isMounted();
0521     d->shareType    = share->shareType();
0522     setShareIcon();
0523   }
0524 }
0525 
0526 
0527 void Smb4KShare::resetMountData()
0528 {
0529   d->path.clear();
0530   d->inaccessible = false;
0531   d->foreign      = false;
0532   d->user         = KUser(KUser::UseRealUserID);
0533   d->group        = KUserGroup(KUser::UseRealUserID);
0534   d->totalSpace   = -1;
0535   d->freeSpace    = -1;
0536   d->usedSpace    = -1;
0537   d->mounted      = false;
0538   d->shareType    = FileShare;
0539   setShareIcon();
0540 }
0541 
0542 
0543 bool Smb4KShare::isHomesShare() const
0544 {
0545   return pUrl->path().endsWith(QLatin1String("homes"));
0546 }
0547 
0548 
0549 void Smb4KShare::setPort(int port)
0550 {
0551   pUrl->setPort(port);
0552 }
0553 
0554 
0555 int Smb4KShare::port() const
0556 {
0557   return pUrl->port();
0558 }
0559 
0560 
0561 void Smb4KShare::setAuthInfo(Smb4KAuthInfo *authInfo)
0562 {
0563   // Avoid that the login is overwritten with an empty 
0564   // string if we have a homes share.
0565   if (!isHomesShare() || !authInfo->userName().isEmpty())
0566   {
0567     pUrl->setUserName(authInfo->userName());
0568     pUrl->setPassword(authInfo->password());
0569   }
0570 }
0571 
0572 
0573 void Smb4KShare::setLogin(const QString &login)
0574 {
0575   // Avoid that the login is overwritten with an empty 
0576   // string if we have a homes share.
0577   if (!isHomesShare() || !login.isEmpty())
0578   {
0579     pUrl->setUserName(login);
0580   }
0581 }
0582 
0583 
0584 QString Smb4KShare::login() const
0585 {
0586   return pUrl->userName();
0587 }
0588 
0589 
0590 void Smb4KShare::setPassword(const QString &passwd)
0591 {
0592   // Avoid that the password is overwritten with an empty 
0593   // string if we have a homes share.
0594   if (!isHomesShare() || !passwd.isEmpty())
0595   {
0596     pUrl->setPassword(passwd);
0597   }
0598 }
0599 
0600 
0601 QString Smb4KShare::password() const
0602 {
0603   return pUrl->password();
0604 }
0605 
0606 
0607 #if KICONTHEMES_VERSION < QT_VERSION_CHECK(5,52,0)
0608 void Smb4KShare::setShareIcon()
0609 {
0610   if (!isPrinter())
0611   {
0612     // Overlays
0613     QStringList overlays;
0614     
0615     if (isMounted())
0616     {
0617       overlays << "emblem-mounted";
0618     }
0619     else
0620     {
0621       overlays << "";
0622     }
0623 
0624     if (isForeign())
0625     {
0626       overlays << "emblem-warning";
0627     }
0628 
0629     if (!isInaccessible())
0630     {
0631       *pIcon = KDE::icon("folder-network", overlays);
0632     }
0633     else
0634     {
0635       *pIcon = KDE::icon("folder-locked", overlays);
0636     }
0637   }
0638   else
0639   {
0640     *pIcon = KDE::icon("printer");
0641   }
0642 }
0643 #else
0644 void Smb4KShare::setShareIcon()
0645 {
0646   if (!isPrinter())
0647   {
0648     // Overlays
0649     QStringList overlays;
0650     
0651     if (isMounted())
0652     {
0653       if (isForeign())
0654       {
0655         overlays << "emblem-warning";
0656       }
0657       else
0658       {
0659         overlays << "";
0660       }
0661       
0662       overlays << "emblem-mounted";
0663     }
0664 
0665     if (!isInaccessible())
0666     {
0667       *pIcon = KDE::icon("folder-network", overlays);
0668     }
0669     else
0670     {
0671       *pIcon = KDE::icon("folder-locked", overlays);
0672     }
0673   }
0674   else
0675   {
0676     *pIcon = KDE::icon("printer");
0677   }
0678 }
0679 #endif
0680 
0681 
0682 void Smb4KShare::update(Smb4KShare* share)
0683 {
0684   if (QString::compare(workgroupName(), share->workgroupName(), Qt::CaseInsensitive) == 0 &&
0685       (QString::compare(url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort),
0686                         share->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort),
0687                         Qt::CaseInsensitive) == 0 ||
0688        QString::compare(homeUrl().toString(QUrl::RemoveUserInfo|QUrl::RemovePort),
0689                         share->homeUrl().toString(QUrl::RemoveUserInfo|QUrl::RemovePort),
0690                         Qt::CaseInsensitive) == 0))
0691   {
0692     *pUrl = share->url();
0693     setMountData(share);
0694     setShareType(share->shareType());
0695     setComment(share->comment());
0696     setHostIpAddress(share->hostIpAddress());
0697   }
0698 }
0699