File indexing completed on 2023-12-10 04:59:19
0001 /* 0002 smb4ksystemtray - This is the system tray window class of Smb4K. 0003 0004 SPDX-FileCopyrightText: 2007-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net> 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 // application specific includes 0009 #include "smb4ksystemtray.h" 0010 #include "core/smb4kclient.h" 0011 #include "core/smb4kglobal.h" 0012 #include "core/smb4kmounter.h" 0013 #include "core/smb4kshare.h" 0014 #include "core/smb4kworkgroup.h" 0015 #include "smb4kbookmarkmenu.h" 0016 #include "smb4kprofilesmenu.h" 0017 #include "smb4ksharesmenu.h" 0018 0019 // Qt includes 0020 #include <QDebug> 0021 #include <QMenu> 0022 0023 // KDE specific includes 0024 #include <KAboutData> 0025 #include <KConfigDialog> 0026 #include <KIconLoader> 0027 #include <KLocalizedString> 0028 #include <KMessageBox> 0029 #include <KPluginFactory> 0030 #include <KPluginMetaData> 0031 #include <KStandardAction> 0032 #include <knotifications_version.h> 0033 0034 using namespace Smb4KGlobal; 0035 0036 Smb4KSystemTray::Smb4KSystemTray(QWidget *parent) 0037 : KStatusNotifierItem(QStringLiteral("smb4k_systemtray"), parent) 0038 { 0039 // 0040 // Set the icon for the system tray 0041 // 0042 QString iconName; 0043 0044 if (KIconLoader::global()->hasIcon(QStringLiteral("network-workgroup-symbolic"))) { 0045 iconName = QStringLiteral("network-workgroup-symbolic"); 0046 } else { 0047 iconName = QStringLiteral("network-workgroup"); 0048 } 0049 0050 setIconByName(iconName); 0051 0052 // 0053 // Set the tooltip text 0054 // 0055 setToolTip(iconName, i18n("Smb4K"), KAboutData::applicationData().shortDescription()); 0056 0057 // 0058 // Set the status of the icon. By default, it is active. It will become passive, 0059 // if the scanner could not find something and no shares were mounted. 0060 // 0061 setStatus(Active); 0062 0063 // 0064 // Set the category 0065 // 0066 setCategory(ApplicationStatus); 0067 0068 // 0069 // Add the actions to the action collection 0070 // 0071 QAction *mountAction = new QAction(KDE::icon(QStringLiteral("view-form"), QStringList(QStringLiteral("emblem-mounted"))), i18n("&Open Mount Dialog"), this); 0072 connect(mountAction, SIGNAL(triggered(bool)), SLOT(slotMountDialog())); 0073 0074 addAction(QStringLiteral("shares_menu"), new Smb4KSharesMenu(this)); 0075 addAction(QStringLiteral("bookmarks_menu"), new Smb4KBookmarkMenu(Smb4KBookmarkMenu::SystemTray, this)); 0076 addAction(QStringLiteral("profiles_menu"), new Smb4KProfilesMenu(this)); 0077 addAction(QStringLiteral("mount_action"), mountAction); 0078 addAction(QStringLiteral("config_action"), KStandardAction::preferences(this, SLOT(slotConfigDialog()), this)); 0079 0080 // 0081 // Set up the menu 0082 // 0083 contextMenu()->addAction(action(QStringLiteral("shares_menu"))); 0084 contextMenu()->addAction(action(QStringLiteral("bookmarks_menu"))); 0085 contextMenu()->addAction(action(QStringLiteral("profiles_menu"))); 0086 contextMenu()->addSeparator(); 0087 contextMenu()->addAction(action(QStringLiteral("mount_action"))); 0088 contextMenu()->addAction(action(QStringLiteral("config_action"))); 0089 0090 // 0091 // Connections 0092 // 0093 connect(Smb4KMounter::self(), SIGNAL(mountedSharesListChanged()), SLOT(slotSetStatus())); 0094 connect(Smb4KClient::self(), SIGNAL(workgroups()), SLOT(slotSetStatus())); 0095 } 0096 0097 Smb4KSystemTray::~Smb4KSystemTray() 0098 { 0099 } 0100 0101 void Smb4KSystemTray::loadSettings() 0102 { 0103 // 0104 // Adjust the bookmarks menu 0105 // 0106 Smb4KBookmarkMenu *bookmarkMenu = static_cast<Smb4KBookmarkMenu *>(action(QStringLiteral("bookmarks_menu"))); 0107 0108 if (bookmarkMenu) { 0109 bookmarkMenu->refreshMenu(); 0110 } 0111 0112 // 0113 // Adjust the shares menu 0114 // 0115 Smb4KSharesMenu *sharesMenu = static_cast<Smb4KSharesMenu *>(action(QStringLiteral("shares_menu"))); 0116 0117 if (sharesMenu) { 0118 sharesMenu->refreshMenu(); 0119 } 0120 0121 // 0122 // Adjust the profiles menu 0123 // 0124 Smb4KProfilesMenu *profilesMenu = static_cast<Smb4KProfilesMenu *>(action(QStringLiteral("profiles_menu"))); 0125 0126 if (profilesMenu) { 0127 profilesMenu->refreshMenu(); 0128 } 0129 } 0130 0131 ///////////////////////////////////////////////////////////////////////////// 0132 // SLOT IMPLEMENTATIONS 0133 ///////////////////////////////////////////////////////////////////////////// 0134 0135 void Smb4KSystemTray::slotMountDialog() 0136 { 0137 if (m_mountDialog.isNull()) { 0138 m_mountDialog = new Smb4KMountDialog(); 0139 m_mountDialog->open(); 0140 } else { 0141 m_mountDialog->raise(); 0142 } 0143 } 0144 0145 void Smb4KSystemTray::slotConfigDialog() 0146 { 0147 if (KConfigDialog::showDialog(QStringLiteral("ConfigDialog"))) { 0148 return; 0149 } 0150 0151 KPluginMetaData metaData(QStringLiteral("smb4kconfigdialog")); 0152 KPluginFactory::Result<KPluginFactory> result = KPluginFactory::loadFactory(metaData); 0153 0154 if (result.errorReason == KPluginFactory::NO_PLUGIN_ERROR) { 0155 #if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5, 240, 0) 0156 QPointer<KConfigDialog> dlg = result.plugin->create<KConfigDialog>(associatedWindow()); 0157 #else 0158 QPointer<KConfigDialog> dlg = result.plugin->create<KConfigDialog>(associatedWidget()); 0159 #endif 0160 0161 if (dlg) { 0162 connect(dlg, SIGNAL(settingsChanged(QString)), this, SLOT(slotSettingsChanged(QString)), Qt::UniqueConnection); 0163 connect(dlg, SIGNAL(settingsChanged(QString)), this, SIGNAL(settingsChanged(QString)), Qt::UniqueConnection); 0164 dlg->show(); 0165 } 0166 } else { 0167 KMessageBox::error(nullptr, result.errorString); 0168 return; 0169 } 0170 } 0171 0172 void Smb4KSystemTray::slotSettingsChanged(const QString &) 0173 { 0174 // 0175 // Execute loadSettings() 0176 // 0177 loadSettings(); 0178 } 0179 0180 void Smb4KSystemTray::slotSetStatus() 0181 { 0182 // 0183 // Set the status of the system tray icon 0184 // 0185 if (!mountedSharesList().isEmpty() || !workgroupsList().isEmpty()) { 0186 setStatus(KStatusNotifierItem::Active); 0187 } else { 0188 setStatus(KStatusNotifierItem::Passive); 0189 } 0190 }