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

0001 /*
0002     smb4knetworkbrowseritem  -  Smb4K's network browser list item.
0003 
0004     SPDX-FileCopyrightText: 2007-2021 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4knetworkbrowseritem.h"
0010 #include "core/smb4kglobal.h"
0011 #include "core/smb4khost.h"
0012 #include "core/smb4kshare.h"
0013 #include "core/smb4kworkgroup.h"
0014 
0015 // Qt includes
0016 #include <QApplication>
0017 #include <QBrush>
0018 #include <QDebug>
0019 
0020 using namespace Smb4KGlobal;
0021 
0022 Smb4KNetworkBrowserItem::Smb4KNetworkBrowserItem(QTreeWidget *parent, const NetworkItemPtr &item)
0023     : QTreeWidgetItem(parent, item->type())
0024     , m_item(item)
0025 {
0026     switch (m_item->type()) {
0027     case Workgroup: {
0028         WorkgroupPtr workgroup = m_item.staticCast<Smb4KWorkgroup>();
0029         setText(Network, workgroup->workgroupName());
0030         setIcon(Network, workgroup->icon());
0031         break;
0032     }
0033     case Host: {
0034         HostPtr host = m_item.staticCast<Smb4KHost>();
0035         setText(Network, host->hostName());
0036         setText(IP, host->ipAddress());
0037         setText(Comment, host->comment());
0038 
0039         if (host->isMasterBrowser()) {
0040             for (int i = 0; i < columnCount(); ++i) {
0041                 QBrush brush(Qt::darkBlue);
0042                 setForeground(i, brush);
0043             }
0044         }
0045 
0046         setIcon(Network, host->icon());
0047         break;
0048     }
0049     case Share: {
0050         SharePtr share = m_item.staticCast<Smb4KShare>();
0051         setText(Network, share->shareName());
0052         setText(Type, share->shareTypeString());
0053         setText(Comment, share->comment());
0054 
0055         if (!share->isPrinter() && share->isMounted()) {
0056             for (int i = 0; i < columnCount(); ++i) {
0057                 QFont f = font(i);
0058                 f.setItalic(true);
0059                 setFont(i, f);
0060             }
0061         }
0062 
0063         setIcon(Network, share->icon());
0064         break;
0065     }
0066     default: {
0067         break;
0068     }
0069     }
0070 }
0071 
0072 Smb4KNetworkBrowserItem::Smb4KNetworkBrowserItem(QTreeWidgetItem *parent, const NetworkItemPtr &item)
0073     : QTreeWidgetItem(parent, item->type())
0074     , m_item(item)
0075 {
0076     switch (m_item->type()) {
0077     case Workgroup: {
0078         WorkgroupPtr workgroup = m_item.staticCast<Smb4KWorkgroup>();
0079         setText(Network, workgroup->workgroupName());
0080         setIcon(Network, workgroup->icon());
0081         break;
0082     }
0083     case Host: {
0084         HostPtr host = m_item.staticCast<Smb4KHost>();
0085         setText(Network, host->hostName());
0086         setText(IP, host->ipAddress());
0087         setText(Comment, host->comment());
0088 
0089         if (host->isMasterBrowser()) {
0090             for (int i = 0; i < columnCount(); ++i) {
0091                 QBrush brush(Qt::darkBlue);
0092                 setForeground(i, brush);
0093             }
0094         }
0095 
0096         setIcon(Network, host->icon());
0097         break;
0098     }
0099     case Share: {
0100         SharePtr share = m_item.staticCast<Smb4KShare>();
0101         setText(Network, share->shareName());
0102         setText(Type, share->shareTypeString());
0103         setText(Comment, share->comment());
0104 
0105         if (!share->isPrinter() && share->isMounted()) {
0106             for (int i = 0; i < columnCount(); ++i) {
0107                 QFont f = font(i);
0108                 f.setItalic(true);
0109                 setFont(i, f);
0110             }
0111         }
0112 
0113         setIcon(Network, share->icon());
0114         break;
0115     }
0116     default: {
0117         break;
0118     }
0119     }
0120 }
0121 
0122 Smb4KNetworkBrowserItem::~Smb4KNetworkBrowserItem()
0123 {
0124 }
0125 
0126 WorkgroupPtr Smb4KNetworkBrowserItem::workgroupItem()
0127 {
0128     if (!m_item || (m_item->type() != Workgroup)) {
0129         return WorkgroupPtr();
0130     }
0131 
0132     return m_item.staticCast<Smb4KWorkgroup>();
0133 }
0134 
0135 HostPtr Smb4KNetworkBrowserItem::hostItem()
0136 {
0137     if (!m_item || (m_item->type() != Host)) {
0138         return HostPtr();
0139     }
0140 
0141     return m_item.staticCast<Smb4KHost>();
0142 }
0143 
0144 SharePtr Smb4KNetworkBrowserItem::shareItem()
0145 {
0146     if (!m_item || (m_item->type() != Share)) {
0147         return SharePtr();
0148     }
0149 
0150     return m_item.staticCast<Smb4KShare>();
0151 }
0152 
0153 NetworkItemPtr Smb4KNetworkBrowserItem::networkItem()
0154 {
0155     return m_item;
0156 }
0157 
0158 void Smb4KNetworkBrowserItem::update()
0159 {
0160     switch (m_item->type()) {
0161     case Host: {
0162         HostPtr host = m_item.staticCast<Smb4KHost>();
0163 
0164         // Adjust the item's color.
0165         if (host->isMasterBrowser()) {
0166             for (int i = 0; i < columnCount(); ++i) {
0167                 QBrush brush(Qt::darkBlue);
0168                 setForeground(i, brush);
0169             }
0170         } else {
0171             for (int i = 0; i < columnCount(); ++i) {
0172                 QBrush brush = QApplication::palette().text();
0173                 setForeground(i, brush);
0174             }
0175         }
0176 
0177         // Set the IP address
0178         setText(IP, host->ipAddress());
0179 
0180         // Set the comment
0181         setText(Comment, host->comment());
0182         break;
0183     }
0184     case Share: {
0185         SharePtr share = m_item.staticCast<Smb4KShare>();
0186 
0187         // Set the comment
0188         setText(Comment, share->comment());
0189 
0190         // Set the icon
0191         setIcon(Network, share->icon());
0192 
0193         // Set the font
0194         for (int i = 0; i < columnCount(); ++i) {
0195             QFont f = font(i);
0196             f.setItalic(share->isMounted());
0197             setFont(i, f);
0198         }
0199 
0200         break;
0201     }
0202     default: {
0203         break;
0204     }
0205     }
0206 }