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

0001 /*
0002     smb4ktooltip  -  Provides tooltips for Smb4K
0003 
0004     SPDX-FileCopyrightText: 2020-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4ktooltip.h"
0010 #include "smb4kbasicnetworkitem.h"
0011 #include "smb4kglobal.h"
0012 #include "smb4khost.h"
0013 #include "smb4kshare.h"
0014 #include "smb4kworkgroup.h"
0015 
0016 // Qt includes
0017 #include <QApplication>
0018 #include <QFormLayout>
0019 #include <QLabel>
0020 #include <QScreen>
0021 
0022 // KDE includes
0023 #include <KIconLoader>
0024 #include <KLocalizedString>
0025 #include <KSeparator>
0026 
0027 using namespace Smb4KGlobal;
0028 
0029 Smb4KToolTip::Smb4KToolTip(QWidget *parent)
0030     : KToolTipWidget(parent)
0031 {
0032     m_contentsWidget = new QWidget(parent);
0033     m_type = Unknown;
0034 }
0035 
0036 Smb4KToolTip::~Smb4KToolTip()
0037 {
0038 }
0039 
0040 void Smb4KToolTip::setupToolTip(Smb4KToolTip::Type type, NetworkItemPtr item)
0041 {
0042     if (type != m_type) {
0043         m_type = type;
0044     }
0045 
0046     if (item != m_item) {
0047         m_item = item;
0048     }
0049 
0050     qDeleteAll(m_contentsWidget->children());
0051 
0052     m_mainLayout = new QHBoxLayout(m_contentsWidget);
0053 
0054     switch (m_type) {
0055     case NetworkItem: {
0056         setupNetworkItemContents();
0057         break;
0058     }
0059     case MountedShare: {
0060         setupMountedShareContents();
0061         break;
0062     }
0063     default: {
0064         break;
0065     }
0066     }
0067 }
0068 
0069 void Smb4KToolTip::update()
0070 {
0071     if (m_item.isNull() || m_type == Unknown) {
0072         return;
0073     }
0074 
0075     setupToolTip(m_type, m_item);
0076 }
0077 
0078 void Smb4KToolTip::show(const QPoint &pos, QWindow *transientParent)
0079 {
0080     QPoint tooltipPos = pos;
0081 
0082     int testWidth = m_contentsWidget->width() + cursor().pos().x() + layout()->contentsMargins().left() + layout()->contentsMargins().right() + 5;
0083     int testHeight = m_contentsWidget->height() + cursor().pos().y() + layout()->contentsMargins().top() + layout()->contentsMargins().bottom() + 5;
0084 
0085     if (QApplication::screenAt(pos)->virtualSize().width() < testWidth) {
0086         tooltipPos.setX(pos.x() - m_contentsWidget->width() - layout()->contentsMargins().left() - layout()->contentsMargins().right() - 5);
0087     } else {
0088         tooltipPos.setX(pos.x() + 5);
0089     }
0090 
0091     if (QApplication::screenAt(pos)->virtualSize().height() < testHeight) {
0092         tooltipPos.setY(pos.y() - m_contentsWidget->height() - layout()->contentsMargins().top() - layout()->contentsMargins().bottom() - 5);
0093     } else {
0094         tooltipPos.setY(pos.y() + 5);
0095     }
0096 
0097     showAt(tooltipPos, m_contentsWidget, transientParent);
0098 }
0099 
0100 void Smb4KToolTip::setupNetworkItemContents()
0101 {
0102     QLabel *iconLabel = new QLabel(m_contentsWidget);
0103     iconLabel->setPixmap(m_item->icon().pixmap(KIconLoader::SizeEnormous));
0104     m_mainLayout->addWidget(iconLabel, Qt::AlignHCenter);
0105 
0106     QFormLayout *descriptionLayout = new QFormLayout();
0107     m_mainLayout->addLayout(descriptionLayout);
0108 
0109     QFont captionFont = font();
0110     captionFont.setBold(true);
0111 
0112     QLabel *caption = new QLabel(m_contentsWidget);
0113     caption->setAlignment(Qt::AlignCenter);
0114     caption->setFont(captionFont);
0115     descriptionLayout->addRow(caption);
0116 
0117     KSeparator *separator = new KSeparator(Qt::Horizontal, m_contentsWidget);
0118     descriptionLayout->addRow(separator);
0119 
0120     QLabel *typeName = new QLabel(m_contentsWidget);
0121     descriptionLayout->addRow(i18n("Type:"), typeName);
0122 
0123     switch (m_item->type()) {
0124     case Workgroup: {
0125         WorkgroupPtr workgroup = m_item.staticCast<Smb4KWorkgroup>();
0126 
0127         caption->setText(workgroup->workgroupName());
0128 
0129         if (workgroup->dnsDiscovered()) {
0130             typeName->setText(i18n("DNS-SD Browsing Domain"));
0131         } else {
0132             typeName->setText(i18n("Workgroup"));
0133         }
0134 
0135         if (workgroup->hasMasterBrowser()) {
0136             QLabel *masterBrowserName = new QLabel(m_contentsWidget);
0137             descriptionLayout->addRow(i18n("Master Browser:"), masterBrowserName);
0138 
0139             if (workgroup->hasMasterBrowserIpAddress()) {
0140                 masterBrowserName->setText(workgroup->masterBrowserName() + QStringLiteral(" (") + workgroup->masterBrowserIpAddress() + QStringLiteral(")"));
0141             } else {
0142                 masterBrowserName->setText(workgroup->masterBrowserName());
0143             }
0144         }
0145 
0146         break;
0147     }
0148     case Host: {
0149         HostPtr host = m_item.staticCast<Smb4KHost>();
0150         caption->setText(host->hostName());
0151         typeName->setText(i18n("Host"));
0152 
0153         QLabel *commentString = new QLabel(!host->comment().isEmpty() ? host->comment() : QStringLiteral("-"), m_contentsWidget);
0154         descriptionLayout->addRow(i18n("Comment:"), commentString);
0155 
0156         QLabel *ipAddress = new QLabel(host->hasIpAddress() ? host->ipAddress() : QStringLiteral("-"), m_contentsWidget);
0157         descriptionLayout->addRow(i18n("IP Address:"), ipAddress);
0158 
0159         QLabel *workgroupName = new QLabel(host->workgroupName(), m_contentsWidget);
0160         descriptionLayout->addRow(i18n("Workgroup:"), workgroupName);
0161 
0162         break;
0163     }
0164     case Share: {
0165         SharePtr share = m_item.staticCast<Smb4KShare>();
0166         caption->setText(share->shareName());
0167         typeName->setText(i18n("Share (%1)", share->shareTypeString()));
0168 
0169         QLabel *commentString = new QLabel(!share->comment().isEmpty() ? share->comment() : QStringLiteral("-"), m_contentsWidget);
0170         descriptionLayout->addRow(i18n("Comment:"), commentString);
0171 
0172         QLabel *mountedState = new QLabel(m_contentsWidget);
0173         mountedState->setObjectName(QStringLiteral("MountedState"));
0174         descriptionLayout->addRow(i18n("Mounted:"), mountedState);
0175 
0176         if (!share->isPrinter()) {
0177             mountedState->setText(share->isMounted() ? i18n("yes") : i18n("no"));
0178         } else {
0179             mountedState->setText(QStringLiteral("-"));
0180         }
0181 
0182         QLabel *hostName = new QLabel(share->hostName(), m_contentsWidget);
0183         descriptionLayout->addRow(i18n("Host:"), hostName);
0184 
0185         QLabel *ipAddressString = new QLabel(share->hasHostIpAddress() ? share->hostIpAddress() : QStringLiteral("-"), m_contentsWidget);
0186         descriptionLayout->addRow(i18n("IP Address:"), ipAddressString);
0187 
0188         QLabel *locationString = new QLabel(share->displayString(), m_contentsWidget);
0189         descriptionLayout->addRow(i18n("Location:"), locationString);
0190 
0191         break;
0192     }
0193     default: {
0194         break;
0195     }
0196     }
0197 
0198     m_contentsWidget->adjustSize();
0199     m_contentsWidget->ensurePolished();
0200 }
0201 
0202 void Smb4KToolTip::setupMountedShareContents()
0203 {
0204     SharePtr share = m_item.staticCast<Smb4KShare>();
0205 
0206     QLabel *iconLabel = new QLabel(m_contentsWidget);
0207     iconLabel->setPixmap(share->icon().pixmap(KIconLoader::SizeEnormous));
0208     iconLabel->setObjectName(QStringLiteral("IconLabel"));
0209     m_mainLayout->addWidget(iconLabel, Qt::AlignHCenter);
0210 
0211     QFormLayout *descriptionLayout = new QFormLayout();
0212     m_mainLayout->addLayout(descriptionLayout);
0213 
0214     QFont captionFont = font();
0215     captionFont.setBold(true);
0216 
0217     QLabel *caption = new QLabel(share->shareName(), m_contentsWidget);
0218     caption->setAlignment(Qt::AlignCenter);
0219     caption->setFont(captionFont);
0220     descriptionLayout->addRow(caption);
0221 
0222     KSeparator *separator = new KSeparator(Qt::Horizontal, m_contentsWidget);
0223     descriptionLayout->addRow(separator);
0224 
0225     QLabel *locationString = new QLabel(share->displayString(), m_contentsWidget);
0226     descriptionLayout->addRow(i18n("Location:"), locationString);
0227 
0228     QLabel *mountpointString = new QLabel(share->path(), m_contentsWidget);
0229     descriptionLayout->addRow(i18n("Mountpoint:"), mountpointString);
0230 
0231     QLabel *loginString = new QLabel(!share->userName().isEmpty() ? share->userName() : i18n("unknown"), m_contentsWidget);
0232     loginString->setObjectName(QStringLiteral("LoginString"));
0233     descriptionLayout->addRow(i18n("Username:"), loginString);
0234 
0235     QString owner(!share->user().loginName().isEmpty() ? share->user().loginName() : i18n("unknown"));
0236     QString group(!share->group().name().isEmpty() ? share->group().name() : i18n("unknown"));
0237 
0238     QLabel *ownerString = new QLabel(owner + QStringLiteral(" - ") + group, m_contentsWidget);
0239     descriptionLayout->addRow(i18n("Owner:"), ownerString);
0240 
0241     QLabel *fileSystemString = new QLabel(share->fileSystemString(), m_contentsWidget);
0242     descriptionLayout->addRow(i18n("File system:"), fileSystemString);
0243 
0244     QString sizeIndication;
0245 
0246     if (share->totalDiskSpace() != 0 && share->freeDiskSpace() != 0) {
0247         sizeIndication = i18n("%1 free of %2 (%3 used)", share->freeDiskSpaceString(), share->totalDiskSpaceString(), share->diskUsageString());
0248     } else {
0249         sizeIndication = i18n("unknown");
0250     }
0251 
0252     QLabel *sizeString = new QLabel(sizeIndication, m_contentsWidget);
0253     sizeString->setObjectName(QStringLiteral("SizeString"));
0254     descriptionLayout->addRow(i18n("Size:"), sizeString);
0255 
0256     m_contentsWidget->adjustSize();
0257     m_contentsWidget->ensurePolished();
0258 }