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

0001 /***************************************************************************
0002     smb4ksystemtray  -  This is the system tray window class of Smb4K.
0003                              -------------------
0004     begin                : Mi Jun 13 2007
0005     copyright            : (C) 2007-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 "smb4ksystemtray.h"
0032 #include "smb4kbookmarkmenu.h"
0033 #include "smb4ksharesmenu.h"
0034 #include "smb4kprofilesmenu.h"
0035 #include "core/smb4kworkgroup.h"
0036 #include "core/smb4kshare.h"
0037 #include "core/smb4kglobal.h"
0038 #include "core/smb4kmounter.h"
0039 #include "core/smb4kclient.h"
0040 
0041 // Qt includes
0042 #include <QMenu>
0043 #include <QDebug>
0044 
0045 // KDE specific includes
0046 #include <KIconThemes/KIconLoader>
0047 #include <KI18n/KLocalizedString>
0048 #include <KConfigWidgets/KStandardAction>
0049 #include <KConfigWidgets/KConfigDialog>
0050 #include <KXmlGui/KActionCollection>
0051 #include <KCoreAddons/KPluginLoader>
0052 #include <KCoreAddons/KPluginFactory>
0053 #include <KCoreAddons/KAboutData>
0054 #include <KWidgetsAddons/KMessageBox>
0055 
0056 using namespace Smb4KGlobal;
0057 
0058 
0059 Smb4KSystemTray::Smb4KSystemTray(QWidget *parent)
0060 : KStatusNotifierItem("smb4k_systemtray", parent)
0061 {
0062   //
0063   // Set the icon for the system tray
0064   //
0065   QString iconName = QStringLiteral("network-workgroup");
0066   setIconByName(iconName);
0067   
0068   //
0069   // Set the tooltip text
0070   //
0071   setToolTip(iconName, i18n("Smb4K"), KAboutData::applicationData().shortDescription());
0072   
0073   //
0074   // Set the status of the icon. By default, it is active. It will become passive, 
0075   // if the scanner could not find something and no shares were mounted.
0076   //
0077   setStatus(Active);
0078   
0079   //
0080   // Set the category
0081   //
0082   setCategory(ApplicationStatus);
0083   
0084   //
0085   // Add the actions to the action collection
0086   //
0087   QAction *mountAction = new QAction(KDE::icon("view-form", QStringList("emblem-mounted")), i18n("&Open Mount Dialog"), this);
0088   connect(mountAction, SIGNAL(triggered(bool)), SLOT(slotMountDialog()));
0089     
0090   addAction("shares_menu", new Smb4KSharesMenu(associatedWidget()));
0091   addAction("bookmarks_menu", new Smb4KBookmarkMenu(Smb4KBookmarkMenu::SystemTray, associatedWidget()));
0092   addAction("profiles_menu", new Smb4KProfilesMenu());
0093   addAction("mount_action", mountAction);
0094   addAction("config_action", KStandardAction::preferences(this, SLOT(slotConfigDialog()), this));
0095   
0096   //
0097   // Set up the menu
0098   //  
0099   contextMenu()->addAction(action("shares_menu"));
0100   contextMenu()->addAction(action("bookmarks_menu"));
0101   contextMenu()->addAction(action("profiles_menu"));
0102   contextMenu()->addSeparator();
0103   contextMenu()->addAction(action("mount_action"));
0104   contextMenu()->addAction(action("config_action"));
0105   
0106   // 
0107   // Connections
0108   // 
0109   connect(Smb4KMounter::self(), SIGNAL(mountedSharesListChanged()), SLOT(slotSetStatus()));
0110   connect(Smb4KClient::self(), SIGNAL(workgroups()), SLOT(slotSetStatus()));
0111 }
0112 
0113 
0114 Smb4KSystemTray::~Smb4KSystemTray()
0115 {
0116 }
0117 
0118 
0119 void Smb4KSystemTray::loadSettings()
0120 {
0121   //
0122   // Adjust the bookmarks menu
0123   //
0124   Smb4KBookmarkMenu *bookmarkMenu = static_cast<Smb4KBookmarkMenu *>(action("bookmarks_menu"));
0125 
0126   if (bookmarkMenu)
0127   {
0128     bookmarkMenu->refreshMenu();
0129   }
0130 
0131   // 
0132   // Adjust the shares menu
0133   //
0134   Smb4KSharesMenu *sharesMenu = static_cast<Smb4KSharesMenu *>(action("shares_menu"));
0135 
0136   if (sharesMenu)
0137   {
0138     sharesMenu->refreshMenu();
0139   }
0140   
0141   //
0142   // Adjust the profiles menu
0143   //
0144   Smb4KProfilesMenu *profilesMenu = static_cast<Smb4KProfilesMenu *>(action("profiles_menu"));
0145   
0146   if (profilesMenu)
0147   {
0148     profilesMenu->refreshMenu();
0149   }
0150 }
0151 
0152 
0153 /////////////////////////////////////////////////////////////////////////////
0154 // SLOT IMPLEMENTATIONS
0155 /////////////////////////////////////////////////////////////////////////////
0156 
0157 void Smb4KSystemTray::slotMountDialog()
0158 {
0159   Smb4KMounter::self()->openMountDialog();
0160 }
0161 
0162 
0163 void Smb4KSystemTray::slotConfigDialog()
0164 {
0165   //
0166   // Check if the configuration dialog exists and try to show it.
0167   //
0168   if (KConfigDialog::exists("ConfigDialog"))
0169   {
0170     KConfigDialog::showDialog("ConfigDialog");
0171     return;
0172   }
0173   
0174   //
0175   // If the dialog does not exist, load and show it:
0176   //
0177   KPluginLoader loader("smb4kconfigdialog");
0178   KPluginFactory *configFactory = loader.factory();
0179 
0180   if (configFactory)
0181   {
0182     KConfigDialog *dlg = 0;
0183     
0184     if (associatedWidget())
0185     {
0186       dlg = configFactory->create<KConfigDialog>(associatedWidget());
0187       dlg->setObjectName("ConfigDialog");
0188     }
0189     else
0190     {
0191       dlg = configFactory->create<KConfigDialog>(contextMenu());
0192       dlg->setObjectName("ConfigDialog");
0193     }
0194     
0195     if (dlg)
0196     {
0197       dlg->setObjectName("ConfigDialog");
0198       connect(dlg, SIGNAL(settingsChanged(QString)), this, SLOT(slotSettingsChanged(QString)), Qt::UniqueConnection);
0199       connect(dlg, SIGNAL(settingsChanged(QString)), this, SIGNAL(settingsChanged(QString)), Qt::UniqueConnection);
0200       dlg->show();
0201     }
0202   }
0203   else
0204   {
0205     KMessageBox::error(0, "<qt>"+loader.errorString()+"</qt>");
0206     return;
0207   }
0208 }
0209 
0210 void Smb4KSystemTray::slotSettingsChanged(const QString &)
0211 {
0212   // 
0213   // Execute loadSettings()
0214   // 
0215   loadSettings();
0216 }
0217 
0218 
0219 void Smb4KSystemTray::slotSetStatus()
0220 {
0221   //
0222   // Set the status of the system tray icon
0223   // 
0224   if (!mountedSharesList().isEmpty() || !workgroupsList().isEmpty())
0225   {
0226     setStatus(KStatusNotifierItem::Active);
0227   }
0228   else
0229   {
0230     setStatus(KStatusNotifierItem::Passive);
0231   }
0232 }
0233 
0234