diff --git a/autotests/taglibwritertest.cpp b/autotests/taglibwritertest.cpp index 87288e8..2cc0e86 100644 --- a/autotests/taglibwritertest.cpp +++ b/autotests/taglibwritertest.cpp @@ -1,149 +1,169 @@ +/* + * Copyright (C) 2016 Varun Joshi + * Copyright (C) 2018 Alexander Stippich + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + #include "taglibwritertest.h" #include "indexerextractortestsconfig.h" #include "writers/taglibwriter.h" #include "writedata.h" #include #include #include #include "taglib.h" #include "fileref.h" #include #include #include #include using namespace KFileMetaData; QString TagLibWriterTest::testFilePath(const QString& fileName) const { return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName; } void TagLibWriterTest::testCommonData() { QFETCH(QString, fileType); QFETCH(QString, mimeType); QFETCH(QString, stringSuffix); QString temporaryFileName = QStringLiteral("writertest.") + fileType; QFile::copy(testFilePath("test." + fileType), testFilePath(temporaryFileName)); TagLibWriter writerPlugin{this}; QCOMPARE(writerPlugin.writeMimetypes().contains(mimeType),true); WriteData data(testFilePath(temporaryFileName), mimeType); data.add(Property::Title, QString(QStringLiteral("Title1") + stringSuffix)); data.add(Property::Artist, QString(QStringLiteral("Artist1") + stringSuffix)); data.add(Property::Album, QString(QStringLiteral("Album1") + stringSuffix)); data.add(Property::TrackNumber, 10); data.add(Property::ReleaseYear, 1999); data.add(Property::Genre, QString(QStringLiteral("Genre1") + stringSuffix)); data.add(Property::Comment, QString(QStringLiteral("Comment1") + stringSuffix)); writerPlugin.write(data); KFileMetaData::ExtractorCollection extractors; QList extractorList = extractors.fetchExtractors(mimeType); KFileMetaData::Extractor* ex = extractorList.first(); KFileMetaData::SimpleExtractionResult result(testFilePath(temporaryFileName), mimeType, KFileMetaData::ExtractionResult::ExtractMetaData); ex->extract(&result); QCOMPARE(result.properties().value(Property::Title), QVariant(QStringLiteral("Title1") + stringSuffix)); QCOMPARE(result.properties().value(Property::Artist), QVariant(QStringLiteral("Artist1") + stringSuffix)); QCOMPARE(result.properties().value(Property::Album), QVariant(QStringLiteral("Album1") + stringSuffix)); QCOMPARE(result.properties().value(Property::TrackNumber).toInt(), 10); QCOMPARE(result.properties().value(Property::ReleaseYear).toInt(), 1999); QCOMPARE(result.properties().value(Property::Genre), QVariant(QStringLiteral("Genre1") + stringSuffix)); QCOMPARE(result.properties().value(Property::Comment), QVariant(QStringLiteral("Comment1") + stringSuffix)); QFile::remove(testFilePath(temporaryFileName)); } void TagLibWriterTest::testCommonData_data() { // Add some unicode characters, use codepoints to avoid any issues with // source encoding: "€µ" static const QChar data[2] = { 0x20ac, 0xb5 }; QString unicodeTestStringSuffix(data, 2); QTest::addColumn("fileType"); QTest::addColumn("mimeType"); QTest::addColumn("stringSuffix"); QTest::addRow("flac") << QStringLiteral("flac") << QStringLiteral("audio/flac") << QString() ; QTest::addRow("flac_unicode") << QStringLiteral("flac") << QStringLiteral("audio/flac") << unicodeTestStringSuffix ; QTest::addRow("m4a") << QStringLiteral("m4a") << QStringLiteral("audio/mp4") << QString() ; QTest::addRow("m4a_unicode") << QStringLiteral("m4a") << QStringLiteral("audio/mp4") << unicodeTestStringSuffix ; QTest::addRow("mp3") << QStringLiteral("mp3") << QStringLiteral("audio/mpeg3") << QString() ; QTest::addRow("mp3_unicode") << QStringLiteral("mp3") << QStringLiteral("audio/mpeg3") << unicodeTestStringSuffix ; QTest::addRow("mpc") << QStringLiteral("mpc") << QStringLiteral("audio/x-musepack") << QString() ; QTest::addRow("mpc_unicode") << QStringLiteral("mpc") << QStringLiteral("audio/x-musepack") << unicodeTestStringSuffix ; QTest::addRow("ogg") << QStringLiteral("ogg") << QStringLiteral("audio/ogg") << QString() ; QTest::addRow("ogg_unicode") << QStringLiteral("ogg") << QStringLiteral("audio/ogg") << unicodeTestStringSuffix ; QTest::addRow("opus") << QStringLiteral("opus") << QStringLiteral("audio/opus") << QString() ; QTest::addRow("opus_unicode") << QStringLiteral("opus") << QStringLiteral("audio/opus") << unicodeTestStringSuffix ; } QTEST_GUILESS_MAIN(TagLibWriterTest) diff --git a/autotests/taglibwritertest.h b/autotests/taglibwritertest.h index 951ef3d..1f42599 100644 --- a/autotests/taglibwritertest.h +++ b/autotests/taglibwritertest.h @@ -1,17 +1,37 @@ +/* + * Copyright (C) 2016 Varun Joshi + * Copyright (C) 2018 Alexander Stippich + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + #ifndef TAGLIBWRITERTEST_H #define TAGLIBWRITERTEST_H #include class TagLibWriterTest : public QObject { Q_OBJECT private: QString testFilePath(const QString& fileName) const; private Q_SLOTS: void testCommonData(); void testCommonData_data(); }; #endif // TAGLIBWRITERTEST_H diff --git a/src/config-kfilemetadata.h.in b/src/config-kfilemetadata.h.in index febdbf6..2fe3712 100644 --- a/src/config-kfilemetadata.h.in +++ b/src/config-kfilemetadata.h.in @@ -1,7 +1,26 @@ +/* + * Copyright (C) 2016 Varun Joshi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + #ifndef CONFIGKFILEMETADATA_H #define CONFIGKFILEMETADATA_H #define LIBEXEC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${KF5_LIBEXEC_INSTALL_DIR}" #cmakedefine01 HAVE_AVSTREAM_CODECPAR #endif // CONFIGKFILEMETADATA_H diff --git a/src/writers/taglibwriter.cpp b/src/writers/taglibwriter.cpp index d48d034..73e83f6 100644 --- a/src/writers/taglibwriter.cpp +++ b/src/writers/taglibwriter.cpp @@ -1,94 +1,114 @@ +/* + * Copyright (C) 2016 Varun Joshi + * Copyright (C) 2018 Alexander Stippich + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + #include "taglibwriter.h" #include #include #include #include using namespace KFileMetaData; TagLibWriter::TagLibWriter(QObject* parent) : WriterPlugin(parent) { } QStringList TagLibWriter::writeMimetypes() const { QStringList types = { QStringLiteral("audio/flac"), QStringLiteral("audio/mp4"), QStringLiteral("audio/mpeg"), QStringLiteral("audio/mpeg3"), QStringLiteral("audio/ogg"), QStringLiteral("audio/opus"), QStringLiteral("audio/x-mpeg"), QStringLiteral("audio/x-musepack"), QStringLiteral("audio/x-opus+ogg"), QStringLiteral("audio/x-vorbis+ogg"), }; return types; } void TagLibWriter::write(const WriteData& data) { const QString fileUrl = data.inputUrl(); const PropertyMap properties = data.getAllProperties(); TagLib::FileRef file(fileUrl.toUtf8().constData(), true); if (file.isNull()) { return; } TagLib::Tag* tags = file.tag(); TagLib::String title; TagLib::String artist; TagLib::String album; TagLib::String genre; TagLib::String comment; if (properties.contains(Property::Title)) { title = QStringToTString(properties.value(Property::Title).toString()); tags->setTitle(title); } if (properties.contains(Property::Artist)) { artist = QStringToTString(properties.value(Property::Artist).toString()); tags->setArtist(artist); } if (properties.contains(Property::Album)) { album = QStringToTString(properties.value(Property::Album).toString()); tags->setAlbum(album); } if (properties.contains(Property::TrackNumber)) { int trackNumber = properties.value(Property::TrackNumber).toInt(); //taglib requires uint if (trackNumber >= 0) { tags->setTrack(trackNumber); } } if (properties.contains(Property::ReleaseYear)) { int year = properties.value(Property::ReleaseYear).toInt(); //taglib requires uint if (year >= 0) { tags->setYear(year); } } if (properties.contains(Property::Genre)) { genre = QStringToTString(properties.value(Property::Genre).toString()); tags->setGenre(genre); } if (properties.contains(Property::Comment)) { comment = QStringToTString(properties.value(Property::Comment).toString()); tags->setComment(comment); } file.save(); } diff --git a/src/writers/taglibwriter.h b/src/writers/taglibwriter.h index ffefb9d..0f72b1a 100644 --- a/src/writers/taglibwriter.h +++ b/src/writers/taglibwriter.h @@ -1,25 +1,45 @@ +/* + * Copyright (C) 2016 Varun Joshi + * Copyright (C) 2018 Alexander Stippich + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + #ifndef TAGLIBWRITER_H #define TAGLIBWRITER_H #include "writerplugin.h" namespace KFileMetaData { class TagLibWriter : public WriterPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.kde.kf5.kfilemetadata.WriterPlugin") Q_INTERFACES(KFileMetaData::WriterPlugin) public: TagLibWriter(QObject* parent = nullptr); void write(const WriteData& data) override; QStringList writeMimetypes() const override; }; } #endif // TAGLIBWRITER_H