filerenamer.cpp 3.92 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

#include <qdir.h>

FileRenamer::FileRenamer()
{
}

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

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
QString FileRenamer::filenameScheme() const
{
    return KConfigGroup(kapp->config(), "FileRenamer").readEntry("FilenameScheme");
}

QString FileRenamer::getToken(const QString &name, const QString &value) const
{
    const KConfigGroup grp(kapp->config(), "FileRenamer");

    const bool needContent = grp.readBoolEntry("Need" + name + "Content", true);
    if(value.isEmpty() && needContent)
        return QString();

    QString token = grp.readEntry(name + "Token");
    token.replace("%s", value);
    return token;
}

QString FileRenamer::titleToken(const QString &value) const
{
    return getToken("Title", value);
}

QString FileRenamer::artistToken(const QString &value) const
{
    return getToken("Artist", value);
}

QString FileRenamer::albumToken(const QString &value) const
{
    return getToken("Album", value);
}

QString FileRenamer::trackToken(const QString &value) const
{
    return getToken("Track", value);
}

QString FileRenamer::commentToken(const QString &value) const
{
    return getToken("Comment", value);
}

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

76 77 78 79 80 81 82
    QString newFilename = rename(item->absFilePath(), *item->tag());
    moveFile(item->absFilePath(), newFilename);
}

QString FileRenamer::rename(const QString &filename, const Tag &tag) const
{
    QString newFilename = filenameScheme();
83 84

    QMap<QChar, QString> substitutions;
85 86 87 88 89
    substitutions[ 't' ] = titleToken(tag.track());
    substitutions[ 'a' ] = artistToken(tag.artist());
    substitutions[ 'A' ] = albumToken(tag.album());
    substitutions[ 'T' ] = trackToken(tag.trackNumberString());
    substitutions[ 'c' ] = commentToken(tag.comment());
90 91 92 93 94

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

    if(QFileInfo(newFilename).isRelative())
95
        newFilename = filename.left( filename.findRev( "/" ) )
96
                      + "/" + newFilename;
97
    newFilename += "." + QFileInfo(filename).extension();
98

99
    return newFilename;
100 101 102 103
}

void FileRenamer::moveFile(const QString &src, const QString &dest)
{
Zack Rusin's avatar
Zack Rusin committed
104
    kdDebug(65432) << "Moving file " << src << " to " << dest << endl;
105 106 107 108 109 110 111 112 113 114 115 116

    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
117
            kdDebug(65432) << "Checking path " << processedComponents << endl;
118 119
            QDir dir(processedComponents);
            if (!dir.exists())
Zack Rusin's avatar
Zack Rusin committed
120
                kdDebug(65432) << "Need to create " << processedComponents << endl;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        }
    }

	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;
}
143
// vim:ts=4:sw=4:noet