filerenamer.cpp 32.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/***************************************************************************
    begin                : Thu Oct 28 2004
    copyright            : (C) 2004 by Michael Pyne
                         : (c) 2003 Frerich Raabe <raabe@kde.org>
    email                : michael.pyne@kdemail.net
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

17 18
#include "filerenamer.h"

19
#include <algorithm>
20

Scott Wheeler's avatar
build  
Scott Wheeler committed
21
#include <kdebug.h>
22 23 24
#include <kcombobox.h>
#include <kurl.h>
#include <kurlrequester.h>
25
#include <kiconloader.h>
26 27
#include <knuminput.h>
#include <kstandarddirs.h>
28
#include <kio/netaccess.h>
29 30 31 32
#include <kconfigbase.h>
#include <kconfig.h>
#include <kglobal.h>
#include <klineedit.h>
33
#include <klocale.h>
34
#include <kconfiggroup.h>
35
#include <kpushbutton.h>
36 37
#include <kapplication.h>
#include <kmessagebox.h>
38
#include <kvbox.h>
39
#include <khbox.h>
40

41
#include <Q3ScrollView>
42
#include <QFile>
43 44
#include <QObject>
#include <QTimer>
45 46 47 48 49
#include <QRegExp>
#include <QCheckBox>
#include <QDir>
#include <QLabel>
#include <QLayout>
50 51 52
#include <QSignalMapper>
#include <Q3Header>
#include <QPalette>
Laurent Montel's avatar
Laurent Montel committed
53
#include <QPixmap>
Laurent Montel's avatar
Laurent Montel committed
54
#include <QFrame>
Laurent Montel's avatar
Laurent Montel committed
55
#include <Q3ValueList>
56

57 58 59 60
#include "tag.h"
#include "filehandle.h"
#include "exampleoptions.h"
#include "playlistitem.h"
61
#include "playlist.h"
62
#include "coverinfo.h"
63

Richard Lärkäng's avatar
Richard Lärkäng committed
64
class ConfirmationDialog : public KDialog
65 66 67 68
{
public:
    ConfirmationDialog(const QMap<QString, QString> &files,
                       QWidget *parent = 0, const char *name = 0)
Richard Lärkäng's avatar
Richard Lärkäng committed
69
        : KDialog(parent)
70
    {
Richard Lärkäng's avatar
Richard Lärkäng committed
71 72 73 74 75 76 77
        setObjectName(name);
        setModal(true);
        setCaption(i18n("Warning"));
        setButtons(Ok | Cancel);

        KVBox *vbox = new KVBox(this);
        setMainWidget(vbox);
Scott Wheeler's avatar
Scott Wheeler committed
78
        KVBox *hbox = new KVBox(vbox);
79 80

        QLabel *l = new QLabel(hbox);
81
        l->setPixmap(SmallIcon("dialog-warning", 32));
82 83 84 85 86

        l = new QLabel(i18n("You are about to rename the following files. "
                            "Are you sure you want to continue?"), hbox);
        hbox->setStretchFactor(l, 1);

Laurent Montel's avatar
Laurent Montel committed
87
        K3ListView *lv = new K3ListView(vbox);
88 89 90 91 92 93 94 95

        lv->addColumn(i18n("Original Name"));
        lv->addColumn(i18n("New Name"));

        int lvHeight = 0;

        QMap<QString, QString>::ConstIterator it = files.begin();
        for(; it != files.end(); ++it) {
96 97
            K3ListViewItem *i = it.key() != it.value()
                ? new K3ListViewItem(lv, it.key(), it.value())
Laurent Montel's avatar
Laurent Montel committed
98
                : new K3ListViewItem(lv, it.key(), i18n("No Change"));
99 100 101 102
            lvHeight += i->height();
        }

        lvHeight += lv->horizontalScrollBar()->height() + lv->header()->height();
103 104
        lv->setMinimumHeight(qMin(lvHeight, 400));
        resize(qMin(width(), 500), qMin(minimumHeight(), 400));
105 106
    }
};
107 108 109 110 111 112 113

//
// Implementation of ConfigCategoryReader
//

ConfigCategoryReader::ConfigCategoryReader() : CategoryReaderInterface(),
    m_currentItem(0)
114
{
115
    KConfigGroup config(KGlobal::config(), "FileRenamer");
116

Scott Wheeler's avatar
Scott Wheeler committed
117 118
    QList<int> categoryOrder = config.readEntry("CategoryOrder", QList<int>());
    int categoryCount[NumTypes] = { 0 }; // Keep track of each category encountered.
119

120
    // Set a default:
121

122 123
    if(categoryOrder.isEmpty())
        categoryOrder << Artist << Album << Title << Track;
124

Scott Wheeler's avatar
Scott Wheeler committed
125
    QList<int>::ConstIterator catIt = categoryOrder.constBegin();
126 127
    for(; catIt != categoryOrder.constEnd(); ++catIt)
    {
Scott Wheeler's avatar
Scott Wheeler committed
128
        int catCount = categoryCount[*catIt]++;
129 130
        TagType category = static_cast<TagType>(*catIt);
        CategoryID catId(category, catCount);
131

132 133 134
        m_options[catId] = TagRenamerOptions(catId);
        m_categoryOrder << catId;
    }
135

136
    m_folderSeparators.resize(m_categoryOrder.count() - 1, false);
137

Scott Wheeler's avatar
Scott Wheeler committed
138
    QList<int> checkedSeparators = config.readEntry("CheckedDirSeparators", QList<int>());
139

Scott Wheeler's avatar
Scott Wheeler committed
140
    QList<int>::ConstIterator it = checkedSeparators.constBegin();
141
    for(; it != checkedSeparators.constEnd(); ++it) {
Scott Wheeler's avatar
Scott Wheeler committed
142 143
        if(*it < m_folderSeparators.count())
            m_folderSeparators[*it] = true;
144
    }
145

146
    m_musicFolder = config.readEntry("MusicFolder", "${HOME}/music");
147
    m_separator = config.readEntry("Separator", " - ");
148
}
149

150 151 152
QString ConfigCategoryReader::categoryValue(TagType type) const
{
    if(!m_currentItem)
153
        return QString();
154 155 156 157 158

    Tag *tag = m_currentItem->file().tag();

    switch(type) {
    case Track:
159
        return QString::number(tag->track());
160 161 162 163 164 165 166 167 168 169 170 171

    case Year:
        return QString::number(tag->year());

    case Title:
        return tag->title();

    case Artist:
        return tag->artist();

    case Album:
        return tag->album();
172

173 174 175 176
    case Genre:
        return tag->genre();

    default:
177
        return QString();
178
    }
179
}
180

181
QString ConfigCategoryReader::prefix(const CategoryID &category) const
182
{
183
    return m_options[category].prefix();
184 185
}

186
QString ConfigCategoryReader::suffix(const CategoryID &category) const
187
{
188
    return m_options[category].suffix();
189 190
}

191
TagRenamerOptions::EmptyActions ConfigCategoryReader::emptyAction(const CategoryID &category) const
192
{
193
    return m_options[category].emptyAction();
194 195
}

196
QString ConfigCategoryReader::emptyText(const CategoryID &category) const
197
{
198 199 200
    return m_options[category].emptyText();
}

201
QList<CategoryID> ConfigCategoryReader::categoryOrder() const
202 203 204 205 206 207 208 209 210
{
    return m_categoryOrder;
}

QString ConfigCategoryReader::separator() const
{
    return m_separator;
}

Scott Wheeler's avatar
Scott Wheeler committed
211
QString ConfigCategoryReader::musicFolder() const
212
{
Scott Wheeler's avatar
Scott Wheeler committed
213
    return m_musicFolder;
214 215
}

Scott Wheeler's avatar
Scott Wheeler committed
216
int ConfigCategoryReader::trackWidth(int categoryNum) const
217
{
218
    return m_options[CategoryID(Track, categoryNum)].trackWidth();
219 220
}

Scott Wheeler's avatar
Scott Wheeler committed
221
bool ConfigCategoryReader::hasFolderSeparator(int index) const
222
{
223 224
    if(index >= m_folderSeparators.count())
        return false;
225 226 227
    return m_folderSeparators[index];
}

228
bool ConfigCategoryReader::isDisabled(const CategoryID &category) const
229 230 231 232
{
    return m_options[category].disabled();
}

233 234 235 236
//
// Implementation of FileRenamerWidget
//

237
FileRenamerWidget::FileRenamerWidget(QWidget *parent) :
Scott Wheeler's avatar
Scott Wheeler committed
238 239 240
    QWidget(parent),
    Ui::FileRenamerBase(),
    CategoryReaderInterface(),
241
    m_exampleFromFile(false)
242
{
243 244
    kDebug(65432) << k_funcinfo << endl;

Tim Beaulen's avatar
Tim Beaulen committed
245 246
    setupUi(this);

247
    QLabel *temp = new QLabel(0);
248 249 250
    QPalette palette;
    palette.setColor(m_exampleText->backgroundRole(), temp->palette().color(backgroundRole()));
    m_exampleText->setPalette(palette);
251 252
    delete temp;

David Faure's avatar
David Faure committed
253
#ifdef __GNUC__
Scott Wheeler's avatar
Scott Wheeler committed
254
    #warning Repair this.
David Faure's avatar
David Faure committed
255
#endif
Scott Wheeler's avatar
Scott Wheeler committed
256
    /* layout()->setMargin(0); */ // We'll be wrapped by KDialogBase
257

258 259 260 261 262 263 264
    // This must be created before createTagRows() is called.

    m_exampleDialog = new ExampleOptionsDialog(this);

    createTagRows();
    loadConfig();

265 266
    // Add correct text to combo box.
    m_category->clear();
Scott Wheeler's avatar
Scott Wheeler committed
267
    for(int i = StartTag; i < NumTypes; ++i) {
268
        QString category = TagRenamerOptions::tagTypeText(static_cast<TagType>(i));
269
        m_category->addItem(category);
270
    }
271 272 273 274 275 276 277

    connect(m_exampleDialog, SIGNAL(signalShown()), SLOT(exampleDialogShown()));
    connect(m_exampleDialog, SIGNAL(signalHidden()), SLOT(exampleDialogHidden()));
    connect(m_exampleDialog, SIGNAL(dataChanged()), SLOT(dataSelected()));
    connect(m_exampleDialog, SIGNAL(fileChanged(const QString &)),
            this,            SLOT(fileSelected(const QString &)));

278 279 280 281 282
    connect(m_separator, SIGNAL(textChanged(const QString &)), this, SLOT(exampleTextChanged()));
    connect(m_musicFolder, SIGNAL(textChanged(const QString &)), this, SLOT(exampleTextChanged()));
    connect(m_showExample, SIGNAL(clicked()), this, SLOT(toggleExampleDialog()));
    connect(m_insertCategory, SIGNAL(clicked()), this, SLOT(insertCategory()));

283
    exampleTextChanged();
284
}
285

286
void FileRenamerWidget::loadConfig()
287
{
288
    kDebug(65432) << k_funcinfo << endl;
Scott Wheeler's avatar
Scott Wheeler committed
289
    QList<int> checkedSeparators;
290 291
    KConfigGroup config(KGlobal::config(), "FileRenamer");

Scott Wheeler's avatar
Scott Wheeler committed
292
    for(int i = 0; i < m_rows.count(); ++i)
293 294
        m_rows[i].options = TagRenamerOptions(m_rows[i].category);

Scott Wheeler's avatar
Scott Wheeler committed
295
    checkedSeparators = config.readEntry("CheckedDirSeparators", QList<int>());
296

Stephan Kulow's avatar
Stephan Kulow committed
297

Scott Wheeler's avatar
Scott Wheeler committed
298 299 300 301
    for(QList<int>::ConstIterator it = checkedSeparators.begin();
        it != checkedSeparators.end(); ++it)
    {
        int separator = *it;
302 303
        if(separator < m_folderSwitches.count())
            m_folderSwitches[separator]->setChecked(true);
304 305
    }

David Faure's avatar
David Faure committed
306 307
    QString path = config.readEntry("MusicFolder", "${HOME}/music");
    m_musicFolder->setPath(path);
308

309
    m_separator->setItemText(m_separator->currentIndex(), config.readEntry("Separator", " - "));
310
}
311

312
void FileRenamerWidget::saveConfig()
313
{
314
    kDebug(65432) << k_funcinfo << endl;
315
    KConfigGroup config(KGlobal::config(), "FileRenamer");
Scott Wheeler's avatar
Scott Wheeler committed
316 317
    QList<int> checkedSeparators;
    QList<int> categoryOrder;
318

Scott Wheeler's avatar
Scott Wheeler committed
319 320
    for(int i = 0; i < m_rows.count(); ++i) {
        int rowId = idOfPosition(i); // Write out in GUI order, not m_rows order
321 322 323
        m_rows[rowId].options.saveConfig(m_rows[rowId].category.categoryNumber);
        categoryOrder += m_rows[rowId].category.category;
    }
324

Scott Wheeler's avatar
Scott Wheeler committed
325
    for(int i = 0; i < m_folderSwitches.count(); ++i)
326 327 328 329 330
        if(m_folderSwitches[i]->isChecked() == true)
            checkedSeparators += i;

    config.writeEntry("CheckedDirSeparators", checkedSeparators);
    config.writeEntry("CategoryOrder", categoryOrder);
David Faure's avatar
David Faure committed
331
    config.writePathEntry("MusicFolder", m_musicFolder->url().path());
332 333 334
    config.writeEntry("Separator", m_separator->currentText());

    config.sync();
335 336
}

337
FileRenamerWidget::~FileRenamerWidget()
338 339 340
{
}

Scott Wheeler's avatar
Scott Wheeler committed
341
int FileRenamerWidget::addRowCategory(TagType category)
342
{
343
    kDebug(65432) << k_funcinfo << endl;
344 345
    static QIcon up   = SmallIcon("go-up");
    static QIcon down = SmallIcon("go-down");
346 347

    // Find number of categories already of this type.
Scott Wheeler's avatar
Scott Wheeler committed
348 349
    int categoryCount = 0;
    for(int i = 0; i < m_rows.count(); ++i)
350 351 352 353 354 355 356
        if(m_rows[i].category.category == category)
            ++categoryCount;

    Row row;

    row.category = CategoryID(category, categoryCount);
    row.position = m_rows.count();
Scott Wheeler's avatar
Scott Wheeler committed
357
    int id = row.position;
358

Laurent Montel's avatar
Laurent Montel committed
359
    KHBox *frame = new KHBox(m_mainFrame);
360 361 362
    QPalette palette;
    palette.setColor(frame->backgroundRole(), frame->palette().color(backgroundRole()).dark(110));
    frame->setPalette(palette);
363 364

    row.widget = frame;
Laurent Montel's avatar
Laurent Montel committed
365
    frame->setFrameShape(QFrame::Box);
366 367 368 369 370
    frame->setLineWidth(1);
    frame->setMargin(3);

    m_mainFrame->setStretchFactor(frame, 1);

Laurent Montel's avatar
Laurent Montel committed
371
    KVBox *buttons = new KVBox(frame);
Laurent Montel's avatar
Laurent Montel committed
372
    buttons->setFrameStyle(QFrame::Plain | QFrame::Box);
373 374 375 376 377
    buttons->setLineWidth(1);

    row.upButton = new KPushButton(buttons);
    row.downButton = new KPushButton(buttons);

378 379
    row.upButton->setIcon(up);
    row.downButton->setIcon(down);
380 381 382 383 384 385 386 387 388 389 390
    row.upButton->setFlat(true);
    row.downButton->setFlat(true);

    upMapper->connect(row.upButton, SIGNAL(clicked()), SLOT(map()));
    upMapper->setMapping(row.upButton, id);
    downMapper->connect(row.downButton, SIGNAL(clicked()), SLOT(map()));
    downMapper->setMapping(row.downButton, id);

    QString labelText = QString("<b>%1</b>").arg(TagRenamerOptions::tagTypeText(category));
    QLabel *label = new QLabel(labelText, frame);
    frame->setStretchFactor(label, 1);
Scott Wheeler's avatar
Scott Wheeler committed
391
    label->setAlignment(Qt::AlignCenter);
392

Laurent Montel's avatar
Laurent Montel committed
393
    KVBox *options = new KVBox(frame);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    row.enableButton = new KPushButton(i18n("Remove"), options);
    toggleMapper->connect(row.enableButton, SIGNAL(clicked()), SLOT(map()));
    toggleMapper->setMapping(row.enableButton, id);

    row.optionsButton = new KPushButton(i18n("Options"), options);
    mapper->connect(row.optionsButton, SIGNAL(clicked()), SLOT(map()));
    mapper->setMapping(row.optionsButton, id);

    row.widget->show();
    m_rows.append(row);

    // Disable add button if there's too many rows.
    if(m_rows.count() == MAX_CATEGORIES)
        m_insertCategory->setEnabled(false);

    return id;
}

Scott Wheeler's avatar
Scott Wheeler committed
412
void FileRenamerWidget::moveSignalMappings(int oldId, int newId)
413
{
414
    kDebug(65432) << k_funcinfo << endl;
415 416 417 418 419 420
    mapper->setMapping(m_rows[oldId].optionsButton, newId);
    downMapper->setMapping(m_rows[oldId].downButton, newId);
    upMapper->setMapping(m_rows[oldId].upButton, newId);
    toggleMapper->setMapping(m_rows[oldId].enableButton, newId);
}

Scott Wheeler's avatar
Scott Wheeler committed
421
bool FileRenamerWidget::removeRow(int id)
422
{
423
    kDebug(65432) << k_funcinfo << endl;
424
    if(id >= m_rows.count()) {
Scott Wheeler's avatar
Scott Wheeler committed
425
        kWarning(65432) << "Trying to remove row, but " << id << " is out-of-range.\n";
426 427 428 429
        return false;
    }

    if(m_rows.count() == 1) {
430
        kError(65432) << "Can't remove last row of File Renamer.\n";
431 432 433 434 435 436 437 438 439 440 441
        return false;
    }

    // Remove widget.  Don't delete it since it appears QSignalMapper may still need it.
    m_rows[id].widget->deleteLater();
    m_rows[id].widget = 0;
    m_rows[id].enableButton = 0;
    m_rows[id].upButton = 0;
    m_rows[id].optionsButton = 0;
    m_rows[id].downButton = 0;

Scott Wheeler's avatar
Scott Wheeler committed
442
    int checkboxPosition = 0; // Remove first checkbox.
443 444 445 446 447 448 449 450 451 452 453 454 455

    // If not the first row, remove the checkbox before it.
    if(m_rows[id].position > 0)
        checkboxPosition = m_rows[id].position - 1;

    // The checkbox is contained within a layout widget, so the layout
    // widget is the one the needs to die.
    delete m_folderSwitches[checkboxPosition]->parent();
    m_folderSwitches.erase(&m_folderSwitches[checkboxPosition]);

    // Go through all the rows and if they have the same category and a
    // higher categoryNumber, decrement the number.  Also update the
    // position identifier.
Scott Wheeler's avatar
Scott Wheeler committed
456
    for(int i = 0; i < m_rows.count(); ++i) {
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        if(i == id)
            continue; // Don't mess with ourself.

        if((m_rows[id].category.category == m_rows[i].category.category) &&
           (m_rows[id].category.categoryNumber < m_rows[i].category.categoryNumber))
        {
            --m_rows[i].category.categoryNumber;
        }

        // Items are moving up.
        if(m_rows[id].position < m_rows[i].position)
            --m_rows[i].position;
    }

    // Every row after the one we delete will have a different identifier, since
    // the identifier is simply its index into m_rows.  So we need to re-do the
    // signal mappings for the affected rows.
Scott Wheeler's avatar
Scott Wheeler committed
474
    for(int i = id + 1; i < m_rows.count(); ++i)
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
        moveSignalMappings(i, i - 1);

    m_rows.erase(&m_rows[id]);

    // Make sure we update the buttons of affected rows.
    m_rows[idOfPosition(0)].upButton->setEnabled(false);
    m_rows[idOfPosition(m_rows.count() - 1)].downButton->setEnabled(false);

    // We can insert another row now, make sure GUI is updated to match.
    m_insertCategory->setEnabled(true);

    QTimer::singleShot(0, this, SLOT(exampleTextChanged()));
    return true;
}

void FileRenamerWidget::addFolderSeparatorCheckbox()
{
492
    kDebug(65432) << k_funcinfo << endl;
493
    QWidget *temp = new QWidget(m_mainFrame);
Stephan Kulow's avatar
Stephan Kulow committed
494
    QHBoxLayout *l = new QHBoxLayout(temp);
495 496 497

    QCheckBox *cb = new QCheckBox(i18n("Insert folder separator"), temp);
    m_folderSwitches.append(cb);
Scott Wheeler's avatar
Scott Wheeler committed
498
    l->addWidget(cb, 0, Qt::AlignCenter);
499 500 501 502 503 504 505 506
    cb->setChecked(false);

    connect(cb, SIGNAL(toggled(bool)),
            SLOT(exampleTextChanged()));

    temp->show();
}

507
void FileRenamerWidget::createTagRows()
508
{
509
    kDebug(65432) << k_funcinfo << endl;
510
    KConfigGroup config(KGlobal::config(), "FileRenamer");
Scott Wheeler's avatar
Scott Wheeler committed
511
    QList<int> categoryOrder = config.readEntry("CategoryOrder", QList<int>());
512

513
    if(categoryOrder.isEmpty())
514
        categoryOrder << Artist << Album << Artist << Title << Track;
515

516 517 518
    // Setup arrays.
    m_rows.reserve(categoryOrder.count());
    m_folderSwitches.reserve(categoryOrder.count() - 1);
519

520 521 522 523 524 525 526 527
    mapper       = new QSignalMapper(this);
    mapper->setObjectName("signal mapper");
    toggleMapper = new QSignalMapper(this);
    toggleMapper->setObjectName("toggle mapper");
    upMapper     = new QSignalMapper(this);
    upMapper->setObjectName("up button mapper");
    downMapper   = new QSignalMapper(this);
    downMapper->setObjectName("down button mapper");
528 529

    connect(mapper,       SIGNAL(mapped(int)), SLOT(showCategoryOption(int)));
530
    connect(toggleMapper, SIGNAL(mapped(int)), SLOT(slotRemoveRow(int)));
531 532 533
    connect(upMapper,     SIGNAL(mapped(int)), SLOT(moveItemUp(int)));
    connect(downMapper,   SIGNAL(mapped(int)), SLOT(moveItemDown(int)));

Laurent Montel's avatar
Laurent Montel committed
534
    m_mainFrame = new KVBox(m_mainView->viewport());
Scott Wheeler's avatar
Scott Wheeler committed
535 536 537
    m_mainFrame->setMargin(10);
    m_mainFrame->setSpacing(5);

538
    m_mainView->addChild(m_mainFrame);
Laurent Montel's avatar
Laurent Montel committed
539
    m_mainView->setResizePolicy(Q3ScrollView::AutoOneFit);
540 541

    // OK, the deal with the categoryOrder variable is that we need to create
542 543 544 545
    // the rows in the order that they were saved in (the order given by categoryOrder).
    // The signal mappers operate according to the row identifier.  To find the position of
    // a row given the identifier, use m_rows[id].position.  To find the id of a given
    // position, use idOfPosition(position).
546

Scott Wheeler's avatar
Scott Wheeler committed
547
    QList<int>::ConstIterator it = categoryOrder.constBegin();
548

549 550
    for(; it != categoryOrder.constEnd(); ++it) {
        if(*it < StartTag || *it >= NumTypes) {
551
            kError(65432) << "Invalid category encountered in file renamer configuration.\n";
552 553
            continue;
        }
554

555
        if(m_rows.count() == MAX_CATEGORIES) {
556
            kError(65432) << "Maximum number of File Renamer tags reached, bailing.\n";
557
            break;
558 559
        }

560 561 562
        TagType i = static_cast<TagType>(*it);

        addRowCategory(i);
563

564 565 566
        // Insert the directory separator checkbox if this isn't the last
        // item.

Scott Wheeler's avatar
Scott Wheeler committed
567
        QList<int>::ConstIterator dup(it);
568 569 570 571

        // Check for last item
        if(++dup != categoryOrder.constEnd())
            addFolderSeparatorCheckbox();
572
    }
573

574 575 576 577 578 579 580
    m_rows.first().upButton->setEnabled(false);
    m_rows.last().downButton->setEnabled(false);

    // If we have maximum number of categories already, don't let the user
    // add more.
    if(m_rows.count() >= MAX_CATEGORIES)
        m_insertCategory->setEnabled(false);
581 582
}

583
void FileRenamerWidget::exampleTextChanged()
584
{
585
    kDebug(65432) << k_funcinfo << endl;
586
    // Just use .mp3 as an example
587
#if 0
588
    if(m_exampleFromFile && (m_exampleFile.isEmpty() ||
589 590
                             !FileHandle(m_exampleFile).tag()->isValid()))
    {
591 592 593 594
        m_exampleText->setText(i18n("No file selected, or selected file has no tags."));
        return;
    }

595
    m_exampleText->setText(FileRenamer::fileName(*this) + ".mp3");
596
#endif
597 598
}

599
QString FileRenamerWidget::fileCategoryValue(TagType category) const
600
{
601
    kDebug(65432) << k_funcinfo << endl;
602 603
    FileHandle file(m_exampleFile);
    Tag *tag = file.tag();
604 605 606

    switch(category) {
    case Track:
607
        return QString::number(tag->track());
608 609 610 611 612

    case Year:
        return QString::number(tag->year());

    case Title:
613
        return tag->title();
614 615

    case Artist:
616
        return tag->artist();
617 618

    case Album:
619
        return tag->album();
620 621

    case Genre:
622
        return tag->genre();
623 624

    default:
625
        return QString();
626
    }
627 628
}

629
QString FileRenamerWidget::categoryValue(TagType category) const
630
{
631
    kDebug(65432) << k_funcinfo << endl;
632 633 634 635
    if(m_exampleFromFile)
        return fileCategoryValue(category);

    const ExampleOptions *example = m_exampleDialog->widget();
636

637 638
    switch (category) {
    case Track:
639
        return example->m_exampleTrack->text();
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

    case Year:
        return example->m_exampleYear->text();

    case Title:
        return example->m_exampleTitle->text();

    case Artist:
        return example->m_exampleArtist->text();

    case Album:
        return example->m_exampleAlbum->text();

    case Genre:
        return example->m_exampleGenre->text();

    default:
657
        return QString();
658
    }
659
}
660

661
QList<CategoryID> FileRenamerWidget::categoryOrder() const
662
{
663
    kDebug(65432) << k_funcinfo << endl;
664
    QList<CategoryID> list;
665 666

    // Iterate in GUI row order.
Scott Wheeler's avatar
Scott Wheeler committed
667 668
    for(int i = 0; i < m_rows.count(); ++i) {
        int rowId = idOfPosition(i);
669 670
        list += m_rows[rowId].category;
    }
671

672
    return list;
673 674
}

Scott Wheeler's avatar
Scott Wheeler committed
675
bool FileRenamerWidget::hasFolderSeparator(int index) const
676
{
677
    kDebug(65432) << k_funcinfo << endl;
678 679
    if(index >= m_folderSwitches.count())
        return false;
680 681
    return m_folderSwitches[index]->isChecked();
}
682

Scott Wheeler's avatar
Scott Wheeler committed
683
void FileRenamerWidget::moveItem(int id, MovementDirection direction)
684
{
685
    kDebug(65432) << k_funcinfo << endl;
686
    QWidget *l = m_rows[id].widget;
Scott Wheeler's avatar
Scott Wheeler committed
687 688 689
    int bottom = m_rows.count() - 1;
    int pos = m_rows[id].position;
    int newPos = (direction == MoveUp) ? pos - 1 : pos + 1;
690

691 692
    // Item we're moving can't go further down after this.

693 694
    if((pos == (bottom - 1) && direction == MoveDown) ||
       (pos == bottom && direction == MoveUp))
695
    {
Scott Wheeler's avatar
Scott Wheeler committed
696 697
        int idBottomRow = idOfPosition(bottom);
        int idAboveBottomRow = idOfPosition(bottom - 1);
698 699 700

        m_rows[idBottomRow].downButton->setEnabled(true);
        m_rows[idAboveBottomRow].downButton->setEnabled(false);
701 702 703 704 705
    }

    // We're moving the top item, do some button switching.

    if((pos == 0 && direction == MoveDown) || (pos == 1 && direction == MoveUp)) {
Scott Wheeler's avatar
Scott Wheeler committed
706 707
        int idTopItem = idOfPosition(0);
        int idBelowTopItem = idOfPosition(1);
708 709 710

        m_rows[idTopItem].upButton->setEnabled(true);
        m_rows[idBelowTopItem].upButton->setEnabled(false);
711 712 713 714
    }

    // This is the item we're swapping with.

Scott Wheeler's avatar
Scott Wheeler committed
715
    int idSwitchWith = idOfPosition(newPos);
716
    QWidget *w = m_rows[idSwitchWith].widget;
717 718 719

    // Update the table of widget rows.

720
    std::swap(m_rows[id].position, m_rows[idSwitchWith].position);
721 722 723 724

    // Move the item two spaces above/below its previous position.  It has to
    // be 2 spaces because of the checkbox.

Stephan Kulow's avatar
Stephan Kulow committed
725
    QBoxLayout *layout = dynamic_cast<QBoxLayout *>(m_mainFrame->layout());
Dirk Mueller's avatar
Dirk Mueller committed
726
    if ( !layout )
Stephan Kulow's avatar
Stephan Kulow committed
727
        return;
David Faure's avatar
David Faure committed
728
#ifdef __GNUC__
Stephan Kulow's avatar
Stephan Kulow committed
729
#warning double check if that still works with Qt4s layout
David Faure's avatar
David Faure committed
730
#endif
731

732
    layout->removeWidget(l);
733
    layout->insertWidget(2 * newPos, l);
734 735 736 737

    // Move the top item two spaces in the opposite direction, for a similar
    // reason.

738
    layout->removeWidget(w);
739
    layout->insertWidget(2 * pos, w);
740 741 742 743 744
    layout->invalidate();

    QTimer::singleShot(0, this, SLOT(exampleTextChanged()));
}

Scott Wheeler's avatar
Scott Wheeler committed
745
int FileRenamerWidget::idOfPosition(int position) const
746
{
747
    kDebug(65432) << k_funcinfo << endl;
748
    if(position >= m_rows.count()) {
749
        kError(65432) << "Search for position " << position << " out-of-range.\n";
Scott Wheeler's avatar
Scott Wheeler committed
750
        return -1;
751 752
    }

Scott Wheeler's avatar
Scott Wheeler committed
753
    for(int i = 0; i < m_rows.count(); ++i)
754 755
        if(m_rows[i].position == position)
            return i;
756

757
    kError(65432) << "Unable to find identifier for position " << position << endl;
Scott Wheeler's avatar
Scott Wheeler committed
758
    return -1;
759 760
}

Scott Wheeler's avatar
Scott Wheeler committed
761
int FileRenamerWidget::findIdentifier(const CategoryID &category) const
762
{
763
    kDebug(65432) << k_funcinfo << endl;
Scott Wheeler's avatar
Scott Wheeler committed
764
    for(int index = 0; index < m_rows.count(); ++index)
765
        if(m_rows[index].category == category)
766 767
            return index;

768
    kError(65432) << "Unable to find match for category " <<
769 770 771 772
        TagRenamerOptions::tagTypeText(category.category) <<
        ", number " << category.categoryNumber << endl;

    return MAX_CATEGORIES;
773 774 775 776
}

void FileRenamerWidget::enableAllUpButtons()
{
777
    kDebug(65432) << k_funcinfo << endl;
Scott Wheeler's avatar
Scott Wheeler committed
778
    for(int i = 0; i < m_rows.count(); ++i)
779 780 781 782 783
        m_rows[i].upButton->setEnabled(true);
}

void FileRenamerWidget::enableAllDownButtons()
{
784
    kDebug(65432) << k_funcinfo << endl;
Scott Wheeler's avatar
Scott Wheeler committed
785
    for(int i = 0; i < m_rows.count(); ++i)
786 787 788
        m_rows[i].downButton->setEnabled(true);
}

789
void FileRenamerWidget::showCategoryOption(int id)
790
{
791
    kDebug(65432) << k_funcinfo << endl;
792
    TagOptionsDialog *dialog = new TagOptionsDialog(this, m_rows[id].options, m_rows[id].category.categoryNumber);
793 794

    if(dialog->exec() == QDialog::Accepted) {
795
        m_rows[id].options = dialog->options();
796
        exampleTextChanged();
797
    }
798 799

    delete dialog;
800 801
}

802
void FileRenamerWidget::moveItemUp(int id)
803
{
804
    kDebug(65432) << k_funcinfo << endl;
Scott Wheeler's avatar
Scott Wheeler committed
805
    moveItem(id, MoveUp);
806 807
}

808
void FileRenamerWidget::moveItemDown(int id)
809
{
810
    kDebug(65432) << k_funcinfo << endl;
Scott Wheeler's avatar
Scott Wheeler committed
811
    moveItem(id, MoveDown);
812 813
}

814
void FileRenamerWidget::toggleExampleDialog()
815
{
816
    kDebug(65432) << k_funcinfo << endl;
Stephan Kulow's avatar
Stephan Kulow committed
817
    m_exampleDialog->setHidden(!m_exampleDialog->isHidden());
818 819
}

820
void FileRenamerWidget::insertCategory()
821
{
822
    kDebug(65432) << k_funcinfo << endl;
823 824
    TagType category = TagRenamerOptions::tagFromCategoryText(m_category->currentText());
    if(category == Unknown) {
825
        kError(65432) << "Trying to add unknown category somehow.\n";
826 827 828 829 830
        return;
    }

    // We need to enable the down button of the current bottom row since it
    // can now move down.
Scott Wheeler's avatar
Scott Wheeler committed
831
    int idBottom = idOfPosition(m_rows.count() - 1);
832 833 834 835 836
    m_rows[idBottom].downButton->setEnabled(true);

    addFolderSeparatorCheckbox();

    // Identifier of new row.
Scott Wheeler's avatar
Scott Wheeler committed
837
    int id = addRowCategory(category);
838 839 840 841 842 843 844 845 846 847

    // Set its down button to be disabled.
    m_rows[id].downButton->setEnabled(false);

    m_mainFrame->layout()->invalidate();
    m_mainView->update();

    // Now update according to the code in loadConfig().
    m_rows[id].options = TagRenamerOptions(m_rows[id].category);
    exampleTextChanged();
848 849 850 851
}

void FileRenamerWidget::exampleDialogShown()
{
852
    kDebug(65432) << k_funcinfo << endl;
853 854 855 856 857
    m_showExample->setText(i18n("Hide Renamer Test Dialog"));
}

void FileRenamerWidget::exampleDialogHidden()
{
858
    kDebug(65432) << k_funcinfo << endl;
859 860 861 862 863
    m_showExample->setText(i18n("Show Renamer Test Dialog"));
}

void FileRenamerWidget::fileSelected(const QString &file)
{
864
    kDebug(65432) << k_funcinfo << endl;
865
    m_exampleFromFile = true;
866
    m_exampleFile = file;
867 868 869 870 871
    exampleTextChanged();
}

<