1
/***************************************************************************
2
 *   Copyright (C) 2005 by Markus Brueffer <markus@brueffer.de>            *
3
 *                                                                         *
4
 *   This program is free software; you can redistribute it and/or modify  *
5
 *   it under the terms of the GNU General Public License as published by  *
6
 *   the Free Software Foundation; either version 2 of the License, or     *
7
 *   (at your option) any later version.                                   *
8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.          *
18
 ***************************************************************************/
19
20
#include "equalizerpresetmanager.h"
21
22
#include <k3listview.h>
23
#include <KInputDialog>
24
#include <KLocale>
25
#include <KMessageBox>
26
#include <KStandardDirs> //locate()
27
28
#include <QDomDocument>
29
#include <QDomElement>
30
#include <QDomNode>
31
#include <QFile>
32
#include <QPushButton>
33
#include <QTextStream>
34
35
EqualizerPresetManager::EqualizerPresetManager( QWidget *parent )
36
        : KDialog( parent )
37
{
38
    setCaption( i18n("Presets") );
39
    setModal( true );
40
    setButtons( Ok | Cancel | Default );
41
    setDefaultButton( Ok );
42
    showButtonSeparator( true );
43
44
    QWidget *mainWidget = new QWidget( this );
45
    setMainWidget( mainWidget );
46
    QHBoxLayout *mainLayout = new QHBoxLayout( mainWidget, 0, spacingHint() );
47
48
    m_presetsView = new K3ListView( mainWidget );
49
    m_presetsView->setObjectName( "presetListView" );
50
    m_presetsView->addColumn( i18n( "Presets" ) );
51
    m_presetsView->setFullWidth( true );
52
    connect(m_presetsView, SIGNAL( selectionChanged() ), SLOT( updateButtonState() ));
53
    connect(m_presetsView, SIGNAL( doubleClicked ( Q3ListViewItem*, const QPoint&, int ) ), SLOT( slotRename() ));
54
    mainLayout->addWidget( m_presetsView );
55
56
    QVBoxLayout* buttonsLayout = new QVBoxLayout( mainLayout );
57
58
    m_renameBtn = new QPushButton( i18n("&Rename"), mainWidget, "renameBtn" );
59
    m_deleteBtn = new QPushButton( i18n("&Delete"), mainWidget, "deleteBtn" );
60
61
    buttonsLayout->addWidget( m_renameBtn );
62
    buttonsLayout->addWidget( m_deleteBtn );
63
64
    connect(m_renameBtn, SIGNAL( clicked() ), SLOT( slotRename() ));
65
    connect(m_deleteBtn, SIGNAL( clicked() ), SLOT( slotDelete() ));
66
    connect(this, SIGNAL( defaultClicked() ), SLOT( slotDefault() ));
67
68
    QSpacerItem* spacer = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding );
69
    buttonsLayout->addItem( spacer );
70
71
    updateButtonState();
72
73
    resize( QSize(300, 250).expandedTo(minimumSizeHint()) );
74
}
75
76
77
EqualizerPresetManager::~EqualizerPresetManager()
78
{
79
}
80
81
void
82
EqualizerPresetManager::setPresets(QMap< QString, QList<int> > presets)
83
{
84
    if ( presets.empty() )
85
        return;
86
87
    m_presets = presets;
88
    m_presetsView->clear();
89
90
    QMap< QString, QList<int> >::ConstIterator end = presets.constEnd();
91
    for ( QMap< QString, QList<int> >::ConstIterator it = presets.constBegin(); it != end; ++it )
92
        if ( it.key() != i18n( "Zero" ) && it.key() != i18n( "Manual" ) ) // Don't add 'Manual' and 'Zero'
93
            new K3ListViewItem( m_presetsView, it.key() );
94
}
95
96
QMap< QString, QList<int> >
97
EqualizerPresetManager::presets()
98
{
99
    return m_presets;
100
}
101
102
void
103
EqualizerPresetManager::slotRename()
104
{
105
    bool ok;
106
    Q3ListViewItem* item = m_presetsView->selectedItem();
107
    const QString title = KInputDialog::getText( i18n("Rename Equalizer Preset"),
108
                                                 i18n("Enter new preset name:"), item->text(0), &ok, this);
109
110
    if ( ok && item->text(0) != title ) {
111
        // Check if the new preset title exists
112
        if ( m_presets.find( title ) != m_presets.end() ) {
113
            int button = KMessageBox::warningYesNo( this, i18n( "A preset with the name %1 already exists. Overwrite?", title ) );
114
115
            if ( button != KMessageBox::Yes )
116
                return;
117
        }
118
119
        m_presets[ title ] = m_presets[ item->text(0)];
120
        m_presets.remove( item->text(0) );
121
        item->setText(0, title);
122
    }
123
}
124
125
void
126
EqualizerPresetManager::slotDefault()
127
{
128
    int button = KMessageBox::warningYesNo( this, i18n( "All presets will be deleted and defaults will be restored. Are you sure?" ) );
129
130
    if ( button != KMessageBox::Yes )
131
        return;
132
133
    // Preserve the 'Manual' preset
134
    QList<int> manualGains = m_presets[ i18n("Manual") ];
135
136
    // Delete all presets
137
    m_presets.clear();
138
139
    // Create predefined presets 'Zero' and 'Manual'
140
    QList<int> zeroGains;
141
    zeroGains << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0;
142
    m_presets[ i18n("Zero") ] = zeroGains;
143
    m_presets[ i18n("Manual") ] = manualGains;
144
145
    // Load the default presets
146
    QFile file( KStandardDirs::locate( "data", "amarok/data/equalizer_presets.xml" ) );
147
148
    QTextStream stream( &file );
149
    stream.setCodec( "UTF8" );
150
151
    QDomDocument d;
152
153
    if( !file.open( QIODevice::ReadOnly ) || !d.setContent( stream.readAll() ) )
154
        return;
155
156
    QDomNode n = d.namedItem( "equalizerpresets" ).namedItem("preset");
157
158
    for( ; !n.isNull();  n = n.nextSibling() )
159
    {
160
        QDomElement e = n.toElement();
161
        QString title = e.attribute( "name" );
162
163
        QList<int> gains;
164
        gains << e.namedItem( "b0" ).toElement().text().toInt();
165
        gains << e.namedItem( "b1" ).toElement().text().toInt();
166
        gains << e.namedItem( "b2" ).toElement().text().toInt();
167
        gains << e.namedItem( "b3" ).toElement().text().toInt();
168
        gains << e.namedItem( "b4" ).toElement().text().toInt();
169
        gains << e.namedItem( "b5" ).toElement().text().toInt();
170
        gains << e.namedItem( "b6" ).toElement().text().toInt();
171
        gains << e.namedItem( "b7" ).toElement().text().toInt();
172
        gains << e.namedItem( "b8" ).toElement().text().toInt();
173
        gains << e.namedItem( "b9" ).toElement().text().toInt();
174
175
        m_presets[ title ] = gains;
176
    }
177
178
    file.close();
179
180
    // Update listview
181
    setPresets( m_presets );
182
}
183
184
void
185
EqualizerPresetManager::slotDelete()
186
{
187
    Q3ListViewItem* item = m_presetsView->selectedItem();
188
189
    m_presets.remove( item->text(0) );
190
191
    delete item;
192
}
193
194
void
195
EqualizerPresetManager::updateButtonState()
196
{
197
    bool selected = ( m_presetsView->selectedItem() != 0 );
198
199
    m_deleteBtn->setEnabled( selected );
200
    m_renameBtn->setEnabled( selected );
201
}
202
203
#include "equalizerpresetmanager.moc"