tageditor.cpp 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/***************************************************************************
                          tageditor.cpp  -  description
                             -------------------
    begin                : Sat Sep 7 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : scott@slackorama.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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kmessagebox.h>
19
#include <kconfig.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <klocale.h>
#include <kdebug.h>

#include <qlabel.h>
#include <qlayout.h>
#include <qdir.h>

#include "tageditor.h"
#include "genrelistlist.h"

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

TagEditor::TagEditor(QWidget *parent, const char *name) : QWidget(parent, name) 
{
    setupLayout();
    readConfig();
    dataChanged = false;
}

TagEditor::~TagEditor()
{
    saveConfig();
}

void TagEditor::readConfig()
{
48 49 50 51 52 53 54 55 56 57 58 59 60 61
    KConfig *config = KGlobal::config();
    { // combo box completion modes
        KConfigGroupSaver saver(config, "TagEditor");
        if(artistNameBox && albumNameBox) {
            KGlobalSettings::Completion artistNameBoxMode = 
		KGlobalSettings::Completion(config->readNumEntry("ArtistNameBoxMode", KGlobalSettings::CompletionAuto));
	    artistNameBox->setCompletionMode(artistNameBoxMode);
	    
            KGlobalSettings::Completion albumNameBoxMode = 
		KGlobalSettings::Completion(config->readNumEntry("AlbumNameBoxMode", KGlobalSettings::CompletionAuto));
	    albumNameBox->setCompletionMode(albumNameBoxMode);
        }
    }

62 63 64 65 66 67 68 69 70 71 72 73
    genreList = GenreListList::ID3v1List(); // this should later be read from a config file 
    if(genreList && genreBox) {
        genreBox->clear();
        // add values to the genre box
        genreBox->insertItem(QString::null);
        for(GenreList::Iterator it = genreList->begin(); it != genreList->end(); ++it)
            genreBox->insertItem((*it));
    }
}

void TagEditor::saveConfig()
{
74 75 76 77 78 79 80 81
    KConfig *config = KGlobal::config();
    { // combo box completion modes
        KConfigGroupSaver saver(config, "TagEditor");
        if(artistNameBox && albumNameBox) {
	    config->writeEntry("ArtistNameBoxMode", artistNameBox->completionMode());
	    config->writeEntry("AlbumNameBoxMode", albumNameBox->completionMode());
        }
    }
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

}

void TagEditor::setupLayout()
{
    const int horizontalSpacing = 10;
    const int verticalSpacing = 2;

    QHBoxLayout *layout = new QHBoxLayout(this, 0, horizontalSpacing);

    //////////////////////////////////////////////////////////////////////////////
    // define two columns of the bottem layout
    //////////////////////////////////////////////////////////////////////////////
    QVBoxLayout *leftColumnLayout = new QVBoxLayout(layout, verticalSpacing);
    QVBoxLayout *rightColumnLayout = new QVBoxLayout(layout, verticalSpacing);

    layout->setStretchFactor(leftColumnLayout, 2);
    layout->setStretchFactor(rightColumnLayout, 3);

    //////////////////////////////////////////////////////////////////////////////
    // put stuff in the left column -- all of the field names are class wide
    //////////////////////////////////////////////////////////////////////////////
    { // just for organization
        leftColumnLayout->addWidget(new QLabel(i18n("Artist Name"), this));

        artistNameBox = new KComboBox(true, this, "artistNameBox");
        leftColumnLayout->addWidget(artistNameBox);
109
	artistNameBox->setCompletionMode(KGlobalSettings::CompletionAuto);
110 111 112 113 114 115 116 117 118 119

        leftColumnLayout->addWidget(new QLabel(i18n("Track Name"), this));

        trackNameBox = new KLineEdit(this, "trackNameBox");
        leftColumnLayout->addWidget(trackNameBox);

        leftColumnLayout->addWidget(new QLabel(i18n("Album Name"), this));

        albumNameBox = new KComboBox(true, this, "albumNameBox");
        leftColumnLayout->addWidget(albumNameBox);
120
	albumNameBox->setCompletionMode(KGlobalSettings::CompletionAuto);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

        leftColumnLayout->addWidget(new QLabel(i18n("Genre"), this));

        genreBox = new KComboBox(true, this, "genreBox");

        leftColumnLayout->addWidget(genreBox);

        // this fills the space at the bottem of the left column
        leftColumnLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
    }
    //////////////////////////////////////////////////////////////////////////////
    // put stuff in the right column
    //////////////////////////////////////////////////////////////////////////////
    { // just for organization
        rightColumnLayout->addWidget(new QLabel(i18n("File Name"), this));

        fileNameBox = new KLineEdit(this, "fileNameBox");
        rightColumnLayout->addWidget(fileNameBox);
        { // lay out the track row
            QHBoxLayout *trackRowLayout = new QHBoxLayout(rightColumnLayout, horizontalSpacing);

            trackRowLayout->addWidget(new QLabel(i18n("Track"), this));

            trackSpin = new KIntSpinBox(0, 255, 1, 0, 10, this, "trackSpin");
            trackRowLayout->addWidget(trackSpin);

            trackRowLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));

            trackRowLayout->addWidget(new QLabel(i18n("Year"), this));

            yearSpin = new KIntSpinBox(0, 9999, 1, 0, 10, this, "yearSpin");
            trackRowLayout->addWidget(yearSpin);

            trackRowLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));

            trackRowLayout->addWidget(new QLabel(i18n("Length"), this));

            lengthBox = new KLineEdit(this, "lengthBox");
            lengthBox->setMaximumWidth(50);
            lengthBox->setReadOnly(true);
            trackRowLayout->addWidget(lengthBox);

            trackRowLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));

            trackRowLayout->addWidget(new QLabel(i18n("Bitrate"), this));

            bitrateBox = new KLineEdit(this, "bitrateBox");
            bitrateBox->setMaximumWidth(50);
            bitrateBox->setReadOnly(true);
            trackRowLayout->addWidget(bitrateBox);
        }
        rightColumnLayout->addWidget(new QLabel(i18n("Comment"), this));

        commentBox = new KEdit(this, "commentBox");
        rightColumnLayout->addWidget(commentBox);
    }

    connect(artistNameBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(changed()));
    connect(trackNameBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(changed()));
    connect(albumNameBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(changed()));
    connect(genreBox, SIGNAL(activated(int)), this, SIGNAL(changed()));
    connect(genreBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(changed()));
    connect(fileNameBox, SIGNAL(textChanged(const QString&)), this, SIGNAL(changed()));
    connect(yearSpin, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
    connect(trackSpin, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
    connect(commentBox, SIGNAL(textChanged()), this, SIGNAL(changed()));

    connect(this, SIGNAL(changed()), this, SLOT(setDataChanged()));
}

////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

195
void TagEditor::setItems(const PlaylistItemList &list)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
{
    saveChangesPrompt();
    items = list;
    refresh();
}

void TagEditor::refresh()
{
    // currently this only works for one item

    PlaylistItem *item = items.getFirst();

    if(item) {
	Tag *tag = item->getTag();
	
211 212 213
	artistNameBox->setEditText(tag->artist());
	trackNameBox->setText(tag->track());
	albumNameBox->setEditText(tag->album());
214
	
215 216
	if(genreList && genreList->findIndex(tag->genre()) >= 0)
	    genreBox->setCurrentItem(genreList->findIndex(tag->genre()) + 1);
217 218
	else {
	    genreBox->setCurrentItem(0);
219
	    genreBox->setEditText(tag->genre());
220 221 222
	}
	
	fileNameBox->setText(item->fileName());
223 224
	trackSpin->setValue(tag->trackNumber());
	yearSpin->setValue(tag->year());
225
	
226 227
	lengthBox->setText(tag->lengthString());
	bitrateBox->setText(tag->bitrateString());
228
	
229
	commentBox->setText(tag->comment());
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	
	dataChanged = false;
    }
    else
	clear();
}

void TagEditor::clear()
{
    artistNameBox->lineEdit()->clear();
    trackNameBox->clear();
    albumNameBox->lineEdit()->clear();
    genreBox->setCurrentItem(0);
    fileNameBox->clear();
    trackSpin->setValue(0);
    yearSpin->setValue(0);
    lengthBox->clear();
    bitrateBox->clear();
    commentBox->clear();    
}

void TagEditor::save()
{
    save(items);
}

256
void TagEditor::updateCollection()
257
{
258 259 260 261 262
    CollectionList *list = CollectionList::instance();

    if(!list)
	return;
    
263 264 265 266 267 268 269
    if(artistNameBox->listBox()) {
        artistNameBox->listBox()->clear();
	
	// This is another case where a sorted value list would be useful.  It's
	// silly to build and maintain unsorted lists and have to call sort 
	// every time that you want to verify that a list is sorted.	

270
	QStringList artistList = list->artists();
271 272 273
	artistList.sort();

        artistNameBox->listBox()->insertStringList(artistList);
274
	artistNameBox->completionObject()->setItems(artistList);
275 276 277 278 279
    }

    if(albumNameBox->listBox()) {
        albumNameBox->listBox()->clear();

280
	QStringList albumList = list->albums();
281 282 283
	albumList.sort();

        albumNameBox->listBox()->insertStringList(albumList);
284
	albumNameBox->completionObject()->setItems(albumList);
285 286 287 288 289 290 291
    }    
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

292
void TagEditor::save(const PlaylistItemList &list)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
    if(list.count() > 0) {
	
	// While this accepts a list of items, it currently only works for the first item.
	
        PlaylistItem *item = list.getFirst();
	
        if(item && dataChanged) {
            QFileInfo newFile(item->dirPath() + QDir::separator() + fileNameBox->text());
            QFileInfo directory(item->dirPath());

            // if (the new file is writable or the new file doesn't exist and it's directory is writable)
            // and the old file is writable...
            if((newFile.isWritable() || (!newFile.exists() && directory.isWritable())) && item->isWritable()) {
                // if the file name in the box doesn't match the current file name
                if(item->fileName()!=newFile.fileName()) {
                    // rename the file if it doesn't exist or we say it's ok
                    if(!newFile.exists() ||
                       KMessageBox::warningYesNo(this, i18n("This file already exists.\nDo you want to replace it?"),
                                                 i18n("File Exists")) == KMessageBox::Yes)
                    {
                        QDir currentDir;
                        currentDir.rename(item->filePath(), newFile.filePath());
                        item->setFile(newFile.filePath());
                    }
                }

                item->getTag()->setArtist(artistNameBox->currentText());
                item->getTag()->setTrack(trackNameBox->text());
                item->getTag()->setAlbum(albumNameBox->currentText());
                item->getTag()->setTrackNumber(trackSpin->value());
                item->getTag()->setYear(yearSpin->value());
                item->getTag()->setComment(commentBox->text());

                //  item->getTag()->setGenre(genreBox->currentText());
                //  item->getTag()->setGenre(genreBox->currentItem() - 1);
                if(genreList->findIndex(genreBox->currentText()) >= 0)
                    item->getTag()->setGenre((*genreList)[genreList->findIndex(genreBox->currentText())]);
                else
332
                    item->getTag()->setGenre(Genre(genreBox->currentText(), item->getTag()->genre().getID3v1()));
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374


                item->getTag()->save();

                item->refresh();

                dataChanged = false;
            }
            else
                KMessageBox::sorry(this, i18n("Could not save to specified file."));
        }
    }
}

void TagEditor::saveChangesPrompt()
{
    if(dataChanged && !items.isEmpty()) {

        QString message = i18n("Do you want to save your changes to:\n");

        PlaylistItem *item = items.first();

        while(item) {
            message.append(item->fileName() + "\n");
            item = items.next();
        }

        if(KMessageBox::warningYesNo(this, message, i18n("Save Changes")) == KMessageBox::Yes)
            save(items);
    }
}

////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void TagEditor::setDataChanged(bool c)
{
    dataChanged = c;
}

#include "tageditor.moc"