Warning, file /multimedia/juk/directorylist.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 <QFileDialog>
0022 #include <QPushButton>
0023 #include <QStandardPaths>
0024 #include <QStringListModel>
0025 
0026 #include "juk_debug.h"
0027 
0028 ////////////////////////////////////////////////////////////////////////////////
0029 // static helpers
0030 ////////////////////////////////////////////////////////////////////////////////
0031 
0032 static QStringList defaultFolders()
0033 {
0034     return QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
0035 }
0036 
0037 ////////////////////////////////////////////////////////////////////////////////
0038 // public methods
0039 ////////////////////////////////////////////////////////////////////////////////
0040 
0041 DirectoryList::DirectoryList(const QStringList &directories,
0042                              const QStringList &excludedDirectories,
0043                              bool importPlaylists,
0044                              QWidget *parent)
0045   : QDialog(parent)
0046   , m_dirListModel(new QStringListModel(directories, this))
0047   , m_excludedDirListModel(new QStringListModel(excludedDirectories, this))
0048 {
0049     if(directories.isEmpty()) {
0050         const auto defaultDirs = defaultFolders();
0051         m_dirListModel->setStringList(defaultDirs);
0052         m_result.addedDirs = defaultDirs;
0053     }
0054 
0055     setWindowTitle(i18n("Folder List"));
0056     setModal(true);
0057 
0058     m_base = new DirectoryListBase(this);
0059     auto layout = new QVBoxLayout(this);
0060     layout->addWidget(m_base);
0061 
0062     connect(m_base->addDirectoryButton, SIGNAL(clicked()),
0063         SLOT(slotAddDirectory()));
0064     connect(m_base->removeDirectoryButton, SIGNAL(clicked()),
0065         SLOT(slotRemoveDirectory()));
0066     connect(m_base->addExcludeDirectoryButton, SIGNAL(clicked()),
0067         SLOT(slotAddExcludeDirectory()));
0068     connect(m_base->removeExcludeDirectoryButton, SIGNAL(clicked()),
0069         SLOT(slotRemoveExcludeDirectory()));
0070     connect(m_base->dlgButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0071     connect(m_base->dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0072 
0073     m_base->directoryListView->setModel(m_dirListModel);
0074     m_base->excludeDirectoryListView->setModel(m_excludedDirListModel);
0075     m_base->importPlaylistsCheckBox->setChecked(importPlaylists);
0076 
0077     resize(QSize(440, 280).expandedTo(minimumSizeHint()));
0078 }
0079 
0080 ////////////////////////////////////////////////////////////////////////////////
0081 // public slots
0082 ////////////////////////////////////////////////////////////////////////////////
0083 
0084 int DirectoryList::exec()
0085 {
0086     m_result.status = static_cast<QDialog::DialogCode>(QDialog::exec());
0087     m_result.addPlaylists = m_base->importPlaylistsCheckBox->isChecked();
0088     return m_result.status;
0089 }
0090 
0091 ////////////////////////////////////////////////////////////////////////////////
0092 // private slots
0093 ////////////////////////////////////////////////////////////////////////////////
0094 
0095 void DirectoryList::slotAddDirectory()
0096 {
0097     QString dir = QFileDialog::getExistingDirectory();
0098     if(dir.isEmpty())
0099         return;
0100 
0101     QStringList dirs = m_dirListModel->stringList();
0102     if(!dirs.contains(dir)) {
0103         dirs.append(dir);
0104         m_dirListModel->setStringList(dirs);
0105 
0106         m_result.addedDirs.append(dir);
0107         m_result.removedDirs.removeAll(dir);
0108     }
0109 }
0110 
0111 void DirectoryList::slotRemoveDirectory()
0112 {
0113     QItemSelectionModel *itemSelection = m_base->directoryListView->selectionModel();
0114 
0115     // These will be used in the loop below
0116     QModelIndexList indexes;
0117     QModelIndex firstIndex;
0118     QString dir;
0119 
0120     // The multiple indexes that are possibly present cannot be deleted one
0121     // after the other, as changing the layout of the model can change the
0122     // indexes (similar to iterators and container remove methods).  So, just
0123     // loop deleting the first index until there is no selection.
0124 
0125     while(itemSelection->hasSelection()) {
0126         indexes = itemSelection->selectedIndexes();
0127         firstIndex = indexes.first();
0128         dir = m_dirListModel->data(firstIndex, Qt::DisplayRole).toString();
0129 
0130         m_dirListModel->removeRow(firstIndex.row());
0131 
0132         // Don't mess up if user removes directory they've just added before
0133         // closing out of the dialog.
0134         if(m_result.addedDirs.contains(dir))
0135             m_result.addedDirs.removeAll(dir);
0136         else
0137             m_result.removedDirs.append(dir);
0138     }
0139 }
0140 
0141 void DirectoryList::slotAddExcludeDirectory()
0142 {
0143     QString dir = QFileDialog::getExistingDirectory();
0144     if(dir.isEmpty())
0145         return;
0146 
0147     QStringList dirs = m_excludedDirListModel->stringList();
0148     if(!dirs.contains(dir)) {
0149         dirs.append(dir);
0150         m_excludedDirListModel->setStringList(dirs);
0151     }
0152     m_result.excludedDirs = m_excludedDirListModel->stringList();
0153 }
0154 
0155 void DirectoryList::slotRemoveExcludeDirectory()
0156 {
0157     QItemSelectionModel *itemSelection = m_base->excludeDirectoryListView->selectionModel();
0158 
0159     // These will be used in the loop below
0160     QModelIndexList indexes;
0161     QModelIndex firstIndex;
0162 
0163     // The multiple indexes that are possibly present cannot be deleted one
0164     // after the other, as changing the layout of the model can change the
0165     // indexes (similar to iterators and container remove methods).  So, just
0166     // loop deleting the first index until there is no selection.
0167 
0168     while(itemSelection->hasSelection()) {
0169         indexes = itemSelection->selectedIndexes();
0170         firstIndex = indexes.first();
0171 
0172         m_excludedDirListModel->removeRow(firstIndex.row());
0173     }
0174     m_result.excludedDirs = m_excludedDirListModel->stringList();
0175 }
0176 
0177 // vim: set et sw=4 tw=0 sta: