File indexing completed on 2024-04-14 15:05:37

0001 /***************************************************************************
0002     smb4kprofilesmenu  -  The menu for the profiles
0003                              -------------------
0004     begin                : Do Aug 10 2014
0005     copyright            : (C) 2014-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 "smb4kprofilesmenu.h"
0032 #include "core/smb4ksettings.h"
0033 #include "core/smb4kprofilemanager.h"
0034 
0035 // Qt includes
0036 #include <QStringList>
0037 #include <QAction>
0038 
0039 // KDE includes
0040 #include <KIconThemes/KIconLoader>
0041 #include <KI18n/KLocalizedString>
0042 
0043 Smb4KProfilesMenu::Smb4KProfilesMenu(QObject* parent)
0044 : KSelectAction(KDE::icon("format-list-unordered"), i18n("Profiles"), parent)
0045 {
0046   QStringList profiles = Smb4KProfileManager::self()->profilesList();
0047   slotProfilesListChanged(profiles);
0048   
0049   setToolBarMode(KSelectAction::MenuMode);
0050   
0051   // 
0052   // Connections
0053   // 
0054   connect(Smb4KProfileManager::self(), SIGNAL(activeProfileChanged(QString)), this, SLOT(slotActiveProfileChanged(QString)));
0055   connect(Smb4KProfileManager::self(), SIGNAL(profilesListChanged(QStringList)), this, SLOT(slotProfilesListChanged(QStringList)));
0056   connect(Smb4KProfileManager::self(), SIGNAL(profileUsageChanged(bool)), this, SLOT(slotProfileUsageChanged(bool)));
0057   connect(this, SIGNAL(triggered(QString)), this, SLOT(slotActionTriggered(QString)));
0058 }
0059 
0060 
0061 Smb4KProfilesMenu::~Smb4KProfilesMenu()
0062 {
0063 }
0064 
0065 
0066 void Smb4KProfilesMenu::refreshMenu()
0067 {
0068   //
0069   // Clear the select action
0070   //
0071   clear();
0072 
0073   //
0074   // Get the list of profiles and add all profiles to the select action
0075   //
0076   QStringList profiles = Smb4KProfileManager::self()->profilesList();
0077   
0078   for (const QString &profile : profiles)
0079   {
0080     QAction *action = addAction(profile);
0081     
0082     if (action)
0083     {
0084       action->setEnabled(Smb4KProfileManager::self()->useProfiles());
0085     }
0086   }
0087   
0088   //
0089   // Enable the action if the user chose to use profiles
0090   //
0091   setEnabled(Smb4KProfileManager::self()->useProfiles());
0092   
0093   //
0094   // Set the current action
0095   // 
0096   setCurrentAction(Smb4KProfileManager::self()->activeProfile());
0097 }
0098 
0099 
0100 
0101 void Smb4KProfilesMenu::slotActiveProfileChanged(const QString& newProfile)
0102 {
0103   setCurrentAction(newProfile);
0104 }
0105 
0106 
0107 void Smb4KProfilesMenu::slotProfilesListChanged(const QStringList& /*profiles*/)
0108 {
0109   refreshMenu();
0110 }
0111 
0112 
0113 void Smb4KProfilesMenu::slotProfileUsageChanged(bool use)
0114 {
0115   for (QAction *action : actions())
0116   {
0117     if (action)
0118     {
0119       action->setEnabled(use);
0120     }
0121   }
0122   
0123   setEnabled(use);
0124 }
0125 
0126 
0127 void Smb4KProfilesMenu::slotActionTriggered(const QString& name)
0128 {
0129   Smb4KProfileManager::self()->setActiveProfile(name);
0130 }
0131