File indexing completed on 2021-12-21 13:27:52

0001 /**
0002  * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
0003  * Copyright (C) 2009 Georg Grabler <ggrabler@gmail.com>
0004  *
0005  * This program is free software; you can redistribute it and/or modify it under
0006  * the terms of the GNU General Public License as published by the Free Software
0007  * Foundation; either version 2 of the License, or (at your option) any later
0008  * version.
0009  *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along with
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "directorylist.h"
0019 
0020 #include <QCheckBox>
0021 #include <QStringListModel>
0022 #include <QVariant>
0023 #include <QPushButton>
0024 #include <QFileDialog>
0025 #include <QStandardPaths>
0026 
0027 #include "juk_debug.h"
0028 
0029 ////////////////////////////////////////////////////////////////////////////////
0030 // static helpers
0031 ////////////////////////////////////////////////////////////////////////////////
0032 
0033 static QStringList defaultFolders()
0034 {
0035     return QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
0036 }
0037 
0038 ////////////////////////////////////////////////////////////////////////////////
0039 // public methods
0040 ////////////////////////////////////////////////////////////////////////////////
0041 
0042 DirectoryList::DirectoryList(const QStringList &directories,
0043                              const QStringList &excludedDirectories,
0044                              bool importPlaylists,
0045                              QWidget *parent)
0046   : QDialog(parent)
0047   , m_dirListModel(new QStringListModel(directories, this))
0048   , m_excludedDirListModel(new QStringListModel(excludedDirectories, this))
0049 {
0050     if(directories.isEmpty()) {
0051         const auto defaultDirs = defaultFolders();
0052         m_dirListModel->setStringList(defaultDirs);
0053         m_result.addedDirs = defaultDirs;
0054     }
0055 
0056     setWindowTitle(i18n("Folder List"));
0057     setModal(true);
0058 
0059     m_base = new DirectoryListBase(this);
0060     auto layout = new QVBoxLayout(this);
0061     layout->addWidget(m_base);
0062 
0063     connect(m_base->addDirectoryButton, SIGNAL(clicked()),
0064         SLOT(slotAddDirectory()));
0065     connect(m_base->removeDirectoryButton, SIGNAL(clicked()),
0066         SLOT(slotRemoveDirectory()));
0067     connect(m_base->addExcludeDirectoryButton, SIGNAL(clicked()),
0068         SLOT(slotAddExcludeDirectory()));
0069     connect(m_base->removeExcludeDirectoryButton, SIGNAL(clicked()),
0070         SLOT(slotRemoveExcludeDirectory()));
0071     connect(m_base->dlgButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0072     connect(m_base->dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0073 
0074     m_base->directoryListView->setModel(m_dirListModel);
0075     m_base->excludeDirectoryListView->setModel(m_excludedDirListModel);
0076     m_base->importPlaylistsCheckBox->setChecked(importPlaylists);
0077 
0078     resize(QSize(440, 280).expandedTo(minimumSizeHint()));
0079 }
0080 
0081 ////////////////////////////////////////////////////////////////////////////////
0082 // public slots
0083 ////////////////////////////////////////////////////////////////////////////////
0084 
0085 int DirectoryList::exec()
0086 {
0087     m_result.status = static_cast<QDialog::DialogCode>(QDialog::exec());
0088     m_result.addPlaylists = m_base->importPlaylistsCheckBox->isChecked();
0089     return m_result.status;
0090 }
0091 
0092 ////////////////////////////////////////////////////////////////////////////////
0093 // private slots
0094 ////////////////////////////////////////////////////////////////////////////////
0095 
0096 void DirectoryList::slotAddDirectory()
0097 {
0098     QString dir = QFileDialog::getExistingDirectory();
0099     if(dir.isEmpty())
0100         return;
0101 
0102     QStringList dirs = m_dirListModel->stringList();
0103     if(!dirs.contains(dir)) {
0104         dirs.append(dir);
0105         m_dirListModel->setStringList(dirs);
0106 
0107         m_result.addedDirs.append(dir);
0108         m_result.removedDirs.removeAll(dir);
0109     }
0110 }
0111 
0112 void DirectoryList::slotRemoveDirectory()
0113 {
0114     QItemSelectionModel *itemSelection = m_base->directoryListView->selectionModel();
0115 
0116     // These will be used in the loop below
0117     QModelIndexList indexes;
0118     QModelIndex firstIndex;
0119     QString dir;
0120 
0121     // The multiple indexes that are possibly present cannot be deleted one
0122     // after the other, as changing the layout of the model can change the
0123     // indexes (similar to iterators and container remove methods).  So, just
0124     // loop deleting the first index until there is no selection.
0125 
0126     while(itemSelection->hasSelection()) {
0127         indexes = itemSelection->selectedIndexes();
0128         firstIndex = indexes.first();
0129         dir = m_dirListModel->data(firstIndex, Qt::DisplayRole).toString();
0130 
0131         m_dirListModel->removeRow(firstIndex.row());
0132 
0133         // Don't mess up if user removes directory they've just added before
0134         // closing out of the dialog.
0135         if(m_result.addedDirs.contains(dir))
0136             m_result.addedDirs.removeAll(dir);
0137         else
0138             m_result.removedDirs.append(dir);
0139     }
0140 }
0141 
0142 void DirectoryList::slotAddExcludeDirectory()
0143 {
0144     QString dir = QFileDialog::getExistingDirectory();
0145     if(dir.isEmpty())
0146         return;
0147 
0148     QStringList dirs = m_excludedDirListModel->stringList();
0149     if(!dirs.contains(dir)) {
0150         dirs.append(dir);
0151         m_excludedDirListModel->setStringList(dirs);
0152     }
0153     m_result.excludedDirs = m_excludedDirListModel->stringList();
0154 }
0155 
0156 void DirectoryList::slotRemoveExcludeDirectory()
0157 {
0158     QItemSelectionModel *itemSelection = m_base->excludeDirectoryListView->selectionModel();
0159 
0160     // These will be used in the loop below
0161     QModelIndexList indexes;
0162     QModelIndex firstIndex;
0163 
0164     // The multiple indexes that are possibly present cannot be deleted one
0165     // after the other, as changing the layout of the model can change the
0166     // indexes (similar to iterators and container remove methods).  So, just
0167     // loop deleting the first index until there is no selection.
0168 
0169     while(itemSelection->hasSelection()) {
0170         indexes = itemSelection->selectedIndexes();
0171         firstIndex = indexes.first();
0172 
0173         m_excludedDirListModel->removeRow(firstIndex.row());
0174     }
0175     m_result.excludedDirs = m_excludedDirListModel->stringList();
0176 }
0177 
0178 // vim: set et sw=4 tw=0 sta: