File indexing completed on 2024-03-24 16:27:29

0001 /***************************************************************************
0002     The configuration page for the profiles
0003                              -------------------
0004     begin                : Do Aug 07 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 "smb4kconfigpageprofiles.h"
0032 #include "core/smb4ksettings.h"
0033 #include "core/smb4kprofilemanager.h"
0034 
0035 // Qt includes
0036 #include <QCheckBox>
0037 #include <QVBoxLayout>
0038 #include <QGroupBox>
0039 
0040 // KDE includes
0041 #include <KI18n/KLocalizedString>
0042 #include <KCompletion/KLineEdit>
0043 
0044 
0045 Smb4KConfigPageProfiles::Smb4KConfigPageProfiles(QWidget* parent)
0046 : QWidget(parent)
0047 {
0048   // Layout
0049   QVBoxLayout *layout = new QVBoxLayout(this);
0050   layout->setSpacing(5);
0051   layout->setMargin(0);
0052   
0053   QGroupBox *settings = new QGroupBox(i18n("Settings"), this);
0054   
0055   QVBoxLayout *settings_layout = new QVBoxLayout(settings);
0056   settings_layout->setSpacing(5);
0057 //   settings_layout->setMargin(0);  
0058   
0059   // Use profiles
0060   QCheckBox *use_profiles = new QCheckBox(Smb4KSettings::self()->useProfilesItem()->label(), settings);
0061   use_profiles->setObjectName("kcfg_UseProfiles");
0062   
0063   // Use profile migration assistant
0064   QCheckBox *use_assistant = new QCheckBox(Smb4KSettings::self()->useMigrationAssistantItem()->label(), settings);
0065   use_assistant->setObjectName("kcfg_UseMigrationAssistant");
0066   
0067   settings_layout->addWidget(use_profiles, 0, 0);
0068   settings_layout->addWidget(use_assistant, 1, 0);
0069   
0070   QGroupBox *profiles = new QGroupBox(i18n("Profiles"), this);
0071   
0072   QVBoxLayout *profiles_layout = new QVBoxLayout(profiles);
0073   profiles_layout->setSpacing(5);
0074   
0075   // List of profiles
0076   m_profiles = new KEditListWidget(profiles);
0077   m_profiles->setObjectName("kcfg_ProfilesList");
0078   m_profiles->setEnabled(Smb4KSettings::self()->useProfiles());
0079   
0080   profiles_layout->addWidget(m_profiles, 0, 0);
0081   
0082   layout->addWidget(settings, 0, 0);
0083   layout->addWidget(profiles, 1, 0);
0084 
0085   connect(use_profiles, SIGNAL(stateChanged(int)), this, SLOT(slotEnableWidget(int)));
0086   connect(m_profiles, SIGNAL(removed(QString)), this, SLOT(slotProfileRemoved(QString)));
0087   connect(m_profiles->lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotProfileChanged()));
0088 }
0089 
0090 
0091 Smb4KConfigPageProfiles::~Smb4KConfigPageProfiles()
0092 {
0093 }
0094 
0095 
0096 QList< QPair<QString,QString> > Smb4KConfigPageProfiles::renamedProfiles() const
0097 {
0098   return m_renamed;
0099 }
0100 
0101 
0102 void Smb4KConfigPageProfiles::clearRenamedProfiles()
0103 {
0104   m_renamed.clear();
0105 }
0106 
0107 
0108 QStringList Smb4KConfigPageProfiles::removedProfiles() const
0109 {
0110   return m_removed;
0111 }
0112 
0113 
0114 void Smb4KConfigPageProfiles::clearRemovedProfiles()
0115 {
0116   m_removed.clear();
0117 }
0118 
0119 
0120 void Smb4KConfigPageProfiles::slotEnableWidget(int state)
0121 {
0122   switch (state)
0123   {
0124     case Qt::Unchecked:
0125     {
0126       m_profiles->setEnabled(false);
0127       break;
0128     }
0129     case Qt::Checked:
0130     {
0131       m_profiles->setEnabled(true);
0132       break;
0133     }
0134     default:
0135     {
0136       break;
0137     }
0138   }
0139 }
0140 
0141 
0142 void Smb4KConfigPageProfiles::slotProfileRemoved(const QString& name)
0143 {
0144   // If the removed profile was renamed before, remove it from 
0145   // the list.
0146   QMutableListIterator< QPair<QString,QString> > it(m_renamed);
0147   
0148   while (it.hasNext())
0149   {
0150     QPair<QString,QString> entry = it.next();
0151     
0152     if (QString::compare(entry.first, name) == 0 || QString::compare(entry.second, name) == 0)
0153     {
0154       it.remove();
0155     }
0156   }
0157   
0158   m_removed << name;
0159 }
0160 
0161 
0162 void Smb4KConfigPageProfiles::slotProfileChanged()
0163 {
0164   QStringList savedProfiles = Smb4KProfileManager::self()->profilesList();
0165   QStringList currentProfiles = m_profiles->items();
0166     
0167   if (savedProfiles.size() == currentProfiles.size())
0168   {
0169     QMutableStringListIterator it(savedProfiles);
0170       
0171     while (it.hasNext())
0172     {
0173       QString entry = it.next();
0174       int index = currentProfiles.indexOf(entry);
0175           
0176       if (index != -1)
0177       {
0178         currentProfiles.removeAt(index);
0179         it.remove();
0180       }
0181     }
0182         
0183     if (!savedProfiles.isEmpty() && !currentProfiles.isEmpty())
0184     {
0185       // Take care that multiple renamings will have the correct
0186       // result.
0187       bool write = true;
0188       
0189       for (int i = 0; i < m_renamed.size(); ++i)
0190       {
0191         if (QString::compare(savedProfiles.first(), m_renamed.at(i).first, Qt::CaseSensitive) == 0)
0192         {
0193           QPair<QString,QString> pair = static_cast< QPair<QString,QString> >(m_renamed.at(i));
0194           pair.second = currentProfiles.first();
0195           write = false;
0196           break;
0197         }
0198       }
0199       
0200       // Write the renamed profile to the list, if necessary.
0201       if (write)
0202       {
0203         QPair<QString,QString> renamed(savedProfiles.first(), currentProfiles.first());
0204         m_renamed << renamed;
0205       }
0206     }
0207   }
0208 }
0209