Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

tageditor.cpp 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 109 110 111 112 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 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/***************************************************************************
                          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>
#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()
{
    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()
{

}

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);

        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);

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

void TagEditor::setItems(const QPtrList<PlaylistItem> &list)
{
    saveChangesPrompt();
    items = list;
    refresh();
}

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

    PlaylistItem *item = items.getFirst();

    if(item) {
	Tag *tag = item->getTag();
	AudioData *audioData = item->getAudioData();
	
	artistNameBox->setEditText(tag->getArtist());
	trackNameBox->setText(tag->getTrack());
	albumNameBox->setEditText(tag->getAlbum());
	
	if(genreList && genreList->findIndex(tag->getGenre()) >= 0)
	    genreBox->setCurrentItem(genreList->findIndex(tag->getGenre()) + 1);
	else {
	    genreBox->setCurrentItem(0);
	    genreBox->setEditText(tag->getGenre());
	}
	
	fileNameBox->setText(item->fileName());
	trackSpin->setValue(tag->getTrackNumber());
	yearSpin->setValue(tag->getYear());
	
	if(audioData->getResult()) {
	    lengthBox->setText(audioData->getLengthChar());
	    bitrateBox->setText(QString::number(audioData->getBitrate()));
	}
	else {
	    lengthBox->clear();
	    bitrateBox->clear();
	}
	
	commentBox->setText(tag->getComment());
	
	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);
}

238
void TagEditor::updateCollection()
239
{
240 241 242 243 244 245
    kdDebug() << "TagEditor::updateCollection()" << endl;
    CollectionList *list = CollectionList::instance();

    if(!list)
	return;
    
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    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.	

	QStringList artistList = list->getArtistList();
	artistList.sort();

        artistNameBox->listBox()->insertStringList(artistList);
    }

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

	QStringList albumList = list->getAlbumList();
	albumList.sort();

        albumNameBox->listBox()->insertStringList(albumList);
    }    
}

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

void TagEditor::save(const QPtrList<PlaylistItem> &list)
{
    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
                    item->getTag()->setGenre(Genre(genreBox->currentText(), item->getTag()->getGenre().getID3v1()));


                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"