filerenamer.cpp 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * filerenamer.cpp - (c) 2003 Frerich Raabe <raabe@kde.org>
 *
 * 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 "filerenamer.h"
#include "playlistitem.h"

#include <kapplication.h>
#include <kconfig.h>
Scott Wheeler's avatar
build  
Scott Wheeler committed
14
#include <kdebug.h>
15
#include <kmacroexpander.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#include <qdir.h>

FileRenamer::FileRenamer()
{
}

FileRenamer::FileRenamer(const PlaylistItem *item)
{
    rename(item);
}

void FileRenamer::rename(const PlaylistItem *item)
{
    if(item == 0 || item->tag() == 0)
        return;

    QString newFilename;
    QString titleToken, artistToken, albumToken, trackToken, commentToken;
    KConfig *cfg = kapp->config();
    {
        KConfigGroupSaver saver(cfg, "FileRenamer");
38
        newFilename = cfg->readPathEntry("FilenameScheme", item->absFilePath());
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

        if(!item->tag()->track().isNull()) {
            titleToken = cfg->readEntry("Title token");
            titleToken.replace("%s", item->tag()->track());
        }

        if(!item->tag()->artist().isNull()) {
            artistToken = cfg->readEntry("Artist token");
            artistToken.replace("%s", item->tag()->artist());
        }

        if(!item->tag()->album().isNull()) {
            albumToken = cfg->readEntry("Album token");
            albumToken.replace("%s", item->tag()->album());
        }

        if(item->tag()->trackNumber() > 0) {
            trackToken = cfg->readEntry("Track token");
            trackToken.replace("%s", item->tag()->trackNumberString());
        }

        if(!item->tag()->comment().isNull()) {
            commentToken = cfg->readEntry("Comment token");
            trackToken.replace("%s", item->tag()->comment());
        }
    }

    QMap<QChar, QString> substitutions;
    substitutions[ 't' ] = titleToken;
    substitutions[ 'a' ] = artistToken;
    substitutions[ 'A' ] = albumToken;
    substitutions[ 'T' ] = trackToken;
    substitutions[ 'c' ] = commentToken;

    newFilename = KMacroExpander::expandMacros(newFilename, substitutions);

    newFilename = newFilename.stripWhiteSpace();

    if(QFileInfo(newFilename).isRelative())
        newFilename = item->absFilePath().left( item->absFilePath().findRev( "/" ) )
                      + "/" + newFilename;

    newFilename += "." + QFileInfo(item->absFilePath()).extension();

    moveFile(item->absFilePath(), newFilename);
}

void FileRenamer::moveFile(const QString &src, const QString &dest)
{
Zack Rusin's avatar
Zack Rusin committed
88
    kdDebug(65432) << "Moving file " << src << " to " << dest << endl;
89 90 91 92 93 94 95 96 97 98 99 100

    if(src == dest)
        return;

    QString dest_ = dest.mid(1); // strip the leading "/"
    if(dest_.find("/") > 0) {
        const QStringList components = QStringList::split("/", dest_.left( dest.findRev("/")));
        QStringList::ConstIterator it = components.begin();
        QStringList::ConstIterator end = components.end();
        QString processedComponents;
        for(; it != end; ++it) {
            processedComponents += "/" + *it;
Zack Rusin's avatar
Zack Rusin committed
101
            kdDebug(65432) << "Checking path " << processedComponents << endl;
102 103
            QDir dir(processedComponents);
            if (!dir.exists())
Zack Rusin's avatar
Zack Rusin committed
104
                kdDebug(65432) << "Need to create " << processedComponents << endl;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        }
    }

	return;

    QFile srcFile(src);
    if(!srcFile.open(IO_ReadOnly)) {
        kdWarning() << "Could not open" << src << " for reading." << endl;
        return;
    }

    QFile destFile(dest);
    if(!destFile.open(IO_WriteOnly)) {
        kdWarning() << "Could not open " << dest << " for writing." << endl;
        return;
    }

    destFile.writeBlock(srcFile.readAll());

    if(!srcFile.remove())
        kdWarning() << "Could not delete source file " << src << endl;
}
127
// vim:ts=4:sw=4:noet