directorylist.cpp 6.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
 * Copyright (C) 2009 Georg Grabler <ggrabler@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

18 19
#include "directorylist.h"

20 21
#include <QCheckBox>
#include <QStringListModel>
22
#include <QtCore/QVariant>
23
#include <QPushButton>
24

25
#include <kfiledialog.h>
Nadeem Hasan's avatar
Nadeem Hasan committed
26
#include <klocale.h>
27
#include <kdebug.h>
28 29 30 31 32

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

33
DirectoryList::DirectoryList(QStringList directories,
34
                             QStringList excludedDirectories,
35
                             bool importPlaylists,
Laurent Montel's avatar
Laurent Montel committed
36
                             QWidget *parent) :
Richard Lärkäng's avatar
Richard Lärkäng committed
37
    KDialog(parent),
38
    m_dirListModel(0)
39
{
40 41 42 43 44 45
    if(directories.isEmpty()) {
        directories = defaultFolders();
        m_result.addedDirs = directories;
    }

    m_dirListModel = new QStringListModel(directories, this);
46
    m_excludedDirListModel = new QStringListModel(excludedDirectories, this);
47

Richard Lärkäng's avatar
Richard Lärkäng committed
48
    setCaption(i18n("Folder List"));
49 50
    setModal(true);
    showButtonSeparator(true);
Richard Lärkäng's avatar
Richard Lärkäng committed
51
    setButtons(KDialog::Ok | KDialog::Cancel);
Laurent Montel's avatar
Laurent Montel committed
52

Nadeem Hasan's avatar
Nadeem Hasan committed
53
    m_base = new DirectoryListBase(this);
54

Nadeem Hasan's avatar
Nadeem Hasan committed
55 56
    setMainWidget(m_base);

57 58 59 60
    connect(m_base->addDirectoryButton, SIGNAL(clicked()),
        SLOT(slotAddDirectory()));
    connect(m_base->removeDirectoryButton, SIGNAL(clicked()),
        SLOT(slotRemoveDirectory()));
61 62 63 64
    connect(m_base->addExcludeDirectoryButton, SIGNAL(clicked()),
        SLOT(slotAddExcludeDirectory()));
    connect(m_base->removeExcludeDirectoryButton, SIGNAL(clicked()),
        SLOT(slotRemoveExcludeDirectory()));
Nadeem Hasan's avatar
Nadeem Hasan committed
65

66
    m_base->directoryListView->setModel(m_dirListModel);
67
    m_base->excludeDirectoryListView->setModel(m_excludedDirListModel);
68 69
    m_base->importPlaylistsCheckBox->setChecked(importPlaylists);

70
    resize(QSize(440, 280).expandedTo(minimumSizeHint()));
71 72 73 74
}

DirectoryList::~DirectoryList()
{
75

76 77
}

78 79 80 81
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

82
int DirectoryList::exec()
83
{
Laurent Montel's avatar
Laurent Montel committed
84
    m_result.status = static_cast<DialogCode>(KDialog::exec());
85
    m_result.addPlaylists = m_base->importPlaylistsCheckBox->isChecked();
86
    // FIXME signal
87 88
    //return m_result;
    return 0;
89 90
}

91 92 93 94
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

95
void DirectoryList::slotAddDirectory()
96 97
{
    QString dir = KFileDialog::getExistingDirectory();
98 99 100 101 102 103 104 105 106

    if(dir.isEmpty())
        return;

    QStringList dirs = m_dirListModel->stringList();
    if(!dirs.contains(dir)) {
        dirs.append(dir);
        m_dirListModel->setStringList(dirs);

107
        m_result.addedDirs.append(dir);
108
        m_result.removedDirs.removeAll(dir);
109 110 111
    }
}

112
void DirectoryList::slotRemoveDirectory()
113
{
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    QItemSelectionModel *itemSelection = m_base->directoryListView->selectionModel();

    // These will be used in the loop below
    QModelIndexList indexes;
    QModelIndex firstIndex;
    QString dir;

    // The multiple indexes that are possibly present cannot be deleted one
    // after the other, as changing the layout of the model can change the
    // indexes (similar to iterators and container remove methods).  So, just
    // loop deleting the first index until there is no selection.

    while(itemSelection->hasSelection()) {
        indexes = itemSelection->selectedIndexes();
        firstIndex = indexes.first();
        dir = m_dirListModel->data(firstIndex, Qt::DisplayRole).toString();

        m_dirListModel->removeRow(firstIndex.row());

        // Don't mess up if user removes directory they've just added before
        // closing out of the dialog.
        if(m_result.addedDirs.contains(dir))
            m_result.addedDirs.removeAll(dir);
        else
            m_result.removedDirs.append(dir);
    }
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
void DirectoryList::slotAddExcludeDirectory()
{
    QString dir = KFileDialog::getExistingDirectory();

    if(dir.isEmpty())
        return;

    QStringList dirs = m_excludedDirListModel->stringList();
    if(!dirs.contains(dir)) {
        dirs.append(dir);
        m_excludedDirListModel->setStringList(dirs);
    }
    m_result.excludedDirs = m_excludedDirListModel->stringList();
}

void DirectoryList::slotRemoveExcludeDirectory()
{
    QItemSelectionModel *itemSelection = m_base->excludeDirectoryListView->selectionModel();

    // These will be used in the loop below
    QModelIndexList indexes;
    QModelIndex firstIndex;
    QString dir;

    // The multiple indexes that are possibly present cannot be deleted one
    // after the other, as changing the layout of the model can change the
    // indexes (similar to iterators and container remove methods).  So, just
    // loop deleting the first index until there is no selection.

    while(itemSelection->hasSelection()) {
        indexes = itemSelection->selectedIndexes();
        firstIndex = indexes.first();

        m_excludedDirListModel->removeRow(firstIndex.row());
    }
    m_result.excludedDirs = m_excludedDirListModel->stringList();
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

QStringList DirectoryList::defaultFolders()
{
    QDir home = QDir::home();
    if(home.cd("Music"))
        return QStringList(home.path());
    if(home.cd("music"))
        return QStringList(home.path());
    if(home.cd(i18n("Music")))
        return QStringList(home.path());
    return QStringList();
}

196
#include "directorylist.moc"
Nadeem Hasan's avatar
Nadeem Hasan committed
197

198
// vim: set et sw=4 tw=0 sta: