File indexing completed on 2024-10-06 04:35:40

0001 /*
0002     smb4kprofilesmenu  -  The menu for the profiles
0003 
0004     SPDX-FileCopyrightText: 2014-2022 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // application specific includes
0009 #include "smb4kprofilesmenu.h"
0010 #include "core/smb4kprofilemanager.h"
0011 #include "core/smb4ksettings.h"
0012 
0013 // Qt includes
0014 #include <QAction>
0015 #include <QStringList>
0016 
0017 // KDE includes
0018 #include <KIconLoader>
0019 #include <KLocalizedString>
0020 
0021 Smb4KProfilesMenu::Smb4KProfilesMenu(QObject *parent)
0022     : KSelectAction(KDE::icon(QStringLiteral("system-users")), i18n("Profiles"), parent)
0023 {
0024     QStringList profiles = Smb4KProfileManager::self()->profilesList();
0025     slotProfilesListChanged(profiles);
0026 
0027     setToolBarMode(KSelectAction::MenuMode);
0028 
0029     //
0030     // Connections
0031     //
0032     connect(Smb4KProfileManager::self(), SIGNAL(activeProfileChanged(QString)), this, SLOT(slotActiveProfileChanged(QString)));
0033     connect(Smb4KProfileManager::self(), SIGNAL(profilesListChanged(QStringList)), this, SLOT(slotProfilesListChanged(QStringList)));
0034     connect(Smb4KProfileManager::self(), SIGNAL(profileUsageChanged(bool)), this, SLOT(slotProfileUsageChanged(bool)));
0035     connect(this, SIGNAL(textTriggered(QString)), this, SLOT(slotActionTriggered(QString)));
0036 }
0037 
0038 Smb4KProfilesMenu::~Smb4KProfilesMenu()
0039 {
0040 }
0041 
0042 void Smb4KProfilesMenu::refreshMenu()
0043 {
0044     //
0045     // Clear the select action
0046     //
0047     clear();
0048 
0049     //
0050     // Get the list of profiles and add all profiles to the select action
0051     //
0052     QStringList profiles = Smb4KProfileManager::self()->profilesList();
0053 
0054     for (const QString &profile : qAsConst(profiles)) {
0055         QAction *action = addAction(profile);
0056 
0057         if (action) {
0058             action->setEnabled(Smb4KProfileManager::self()->useProfiles());
0059         }
0060     }
0061 
0062     //
0063     // Enable the action if the user chose to use profiles
0064     //
0065     setEnabled(Smb4KProfileManager::self()->useProfiles());
0066 
0067     //
0068     // Set the current action
0069     //
0070     setCurrentAction(Smb4KProfileManager::self()->activeProfile());
0071 }
0072 
0073 void Smb4KProfilesMenu::slotActiveProfileChanged(const QString &newProfile)
0074 {
0075     setCurrentAction(newProfile);
0076 }
0077 
0078 void Smb4KProfilesMenu::slotProfilesListChanged(const QStringList & /*profiles*/)
0079 {
0080     refreshMenu();
0081 }
0082 
0083 void Smb4KProfilesMenu::slotProfileUsageChanged(bool use)
0084 {
0085     QList<QAction *> actionList = actions();
0086 
0087     for (QAction *action : qAsConst(actionList)) {
0088         if (action) {
0089             action->setEnabled(use);
0090         }
0091     }
0092 
0093     setEnabled(use);
0094 }
0095 
0096 void Smb4KProfilesMenu::slotActionTriggered(const QString &name)
0097 {
0098     Smb4KProfileManager::self()->setActiveProfile(name);
0099 }