File indexing completed on 2023-12-03 05:04:28
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 typeName->setText(i18n("Workgroup")); 0129 0130 QLabel *masterBrowserName = new QLabel(m_contentsWidget); 0131 masterBrowserName->setObjectName(QStringLiteral("MasterBrowserName")); 0132 descriptionLayout->addRow(i18n("Master Browser:"), masterBrowserName); 0133 0134 if (workgroup->hasMasterBrowser()) { 0135 if (workgroup->hasMasterBrowserIpAddress()) { 0136 masterBrowserName->setText(workgroup->masterBrowserName() + QStringLiteral(" (") + workgroup->masterBrowserIpAddress() + QStringLiteral(")")); 0137 } else { 0138 masterBrowserName->setText(workgroup->masterBrowserName()); 0139 } 0140 } else { 0141 masterBrowserName->setText(QStringLiteral("-")); 0142 } 0143 0144 break; 0145 } 0146 case Host: { 0147 HostPtr host = m_item.staticCast<Smb4KHost>(); 0148 caption->setText(host->hostName()); 0149 typeName->setText(i18n("Host")); 0150 0151 QLabel *commentString = new QLabel(!host->comment().isEmpty() ? host->comment() : QStringLiteral("-"), m_contentsWidget); 0152 commentString->setObjectName(QStringLiteral("CommentString")); 0153 descriptionLayout->addRow(i18n("Comment:"), commentString); 0154 0155 QLabel *ipAddress = new QLabel(host->hasIpAddress() ? host->ipAddress() : QStringLiteral("-"), m_contentsWidget); 0156 ipAddress->setObjectName(QStringLiteral("IPAddressString")); 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 commentString->setObjectName(QStringLiteral("CommentString")); 0171 descriptionLayout->addRow(i18n("Comment:"), commentString); 0172 0173 QLabel *mountedState = new QLabel(m_contentsWidget); 0174 mountedState->setObjectName(QStringLiteral("MountedState")); 0175 descriptionLayout->addRow(i18n("Mounted:"), mountedState); 0176 0177 if (!share->isPrinter()) { 0178 mountedState->setText(share->isMounted() ? i18n("yes") : i18n("no")); 0179 } else { 0180 mountedState->setText(QStringLiteral("-")); 0181 } 0182 0183 QLabel *hostName = new QLabel(share->hostName(), m_contentsWidget); 0184 descriptionLayout->addRow(i18n("Host:"), hostName); 0185 0186 QLabel *ipAddressString = new QLabel(share->hasHostIpAddress() ? share->hostIpAddress() : QStringLiteral("-"), m_contentsWidget); 0187 ipAddressString->setObjectName(QStringLiteral("IPAddressString")); 0188 descriptionLayout->addRow(i18n("IP Address:"), ipAddressString); 0189 0190 QLabel *locationString = new QLabel(share->displayString(), m_contentsWidget); 0191 descriptionLayout->addRow(i18n("Location:"), locationString); 0192 0193 break; 0194 } 0195 default: { 0196 break; 0197 } 0198 } 0199 0200 m_contentsWidget->adjustSize(); 0201 m_contentsWidget->ensurePolished(); 0202 } 0203 0204 void Smb4KToolTip::setupMountedShareContents() 0205 { 0206 SharePtr share = m_item.staticCast<Smb4KShare>(); 0207 0208 QLabel *iconLabel = new QLabel(m_contentsWidget); 0209 iconLabel->setPixmap(share->icon().pixmap(KIconLoader::SizeEnormous)); 0210 iconLabel->setObjectName(QStringLiteral("IconLabel")); 0211 m_mainLayout->addWidget(iconLabel, Qt::AlignHCenter); 0212 0213 QFormLayout *descriptionLayout = new QFormLayout(); 0214 m_mainLayout->addLayout(descriptionLayout); 0215 0216 QFont captionFont = font(); 0217 captionFont.setBold(true); 0218 0219 QLabel *caption = new QLabel(share->shareName(), m_contentsWidget); 0220 caption->setAlignment(Qt::AlignCenter); 0221 caption->setFont(captionFont); 0222 descriptionLayout->addRow(caption); 0223 0224 KSeparator *separator = new KSeparator(Qt::Horizontal, m_contentsWidget); 0225 descriptionLayout->addRow(separator); 0226 0227 QLabel *locationString = new QLabel(share->displayString(), m_contentsWidget); 0228 descriptionLayout->addRow(i18n("Location:"), locationString); 0229 0230 QLabel *mountpointString = new QLabel(share->path(), m_contentsWidget); 0231 descriptionLayout->addRow(i18n("Mountpoint:"), mountpointString); 0232 0233 QLabel *loginString = new QLabel(!share->userName().isEmpty() ? share->userName() : i18n("unknown"), m_contentsWidget); 0234 loginString->setObjectName(QStringLiteral("LoginString")); 0235 descriptionLayout->addRow(i18n("Username:"), loginString); 0236 0237 QString owner(!share->user().loginName().isEmpty() ? share->user().loginName() : i18n("unknown")); 0238 QString group(!share->group().name().isEmpty() ? share->group().name() : i18n("unknown")); 0239 0240 QLabel *ownerString = new QLabel(owner + QStringLiteral(" - ") + group, m_contentsWidget); 0241 descriptionLayout->addRow(i18n("Owner:"), ownerString); 0242 0243 QLabel *fileSystemString = new QLabel(share->fileSystemString(), m_contentsWidget); 0244 descriptionLayout->addRow(i18n("File system:"), fileSystemString); 0245 0246 QString sizeIndication; 0247 0248 if (share->totalDiskSpace() != 0 && share->freeDiskSpace() != 0) { 0249 sizeIndication = i18n("%1 free of %2 (%3 used)", share->freeDiskSpaceString(), share->totalDiskSpaceString(), share->diskUsageString()); 0250 } else { 0251 sizeIndication = i18n("unknown"); 0252 } 0253 0254 QLabel *sizeString = new QLabel(sizeIndication, m_contentsWidget); 0255 sizeString->setObjectName(QStringLiteral("SizeString")); 0256 descriptionLayout->addRow(i18n("Size:"), sizeString); 0257 0258 m_contentsWidget->adjustSize(); 0259 m_contentsWidget->ensurePolished(); 0260 }