Commit ff245950 authored by Michael Pyne's avatar Michael Pyne

Simplify the "atom" type for changes to filename or TagLib metadata.

parent 8c165059
......@@ -60,7 +60,7 @@ Tag::Tag(const QString &fileName) :
}
}
bool Tag::save()
bool Tag::save() const
{
bool result;
TagLib::ID3v2::FrameFactory::instance()->setDefaultTextEncoding(TagLib::String::UTF8);
......
......@@ -39,7 +39,7 @@ public:
*/
Tag(const QString &fileName, bool);
bool save();
bool save() const;
QString title() const { return m_title; }
QString artist() const { return m_artist; }
......
......@@ -35,40 +35,15 @@ using ActionCollection::action;
Q_GLOBAL_STATIC(TagTransactionManager, g_tagManager)
TagTransactionAtom::TagTransactionAtom() : m_item(0), m_tag(0)
{
action("edit_undo")->setEnabled(false);
}
TagTransactionAtom::TagTransactionAtom(const TagTransactionAtom &other) :
m_item(other.m_item), m_tag(other.m_tag)
{
other.m_tag = 0; // Only allow one owner
}
TagTransactionAtom::TagTransactionAtom(PlaylistItem *item, Tag *tag) :
m_item(item), m_tag(tag)
{
}
TagTransactionAtom::~TagTransactionAtom()
{
delete m_tag;
}
TagTransactionAtom &TagTransactionAtom::operator=(const TagTransactionAtom &other)
TagTransactionManager *TagTransactionManager::instance()
{
m_item = other.m_item;
m_tag = other.m_tag;
other.m_tag = 0; // Only allow one owner
return *this;
return g_tagManager;
}
TagTransactionManager *TagTransactionManager::instance()
TagTransactionAtom::TagTransactionAtom(PlaylistItem *item, Tag *tag)
: m_item(item)
, m_tag(tag)
{
return g_tagManager;
}
void TagTransactionManager::changeTagOnItem(PlaylistItem *item, Tag *newTag)
......@@ -83,7 +58,7 @@ void TagTransactionManager::changeTagOnItem(PlaylistItem *item, Tag *newTag)
// signals from CollectionList to ensure that the commit list and the
// playlists stay in sync.
m_list.append(TagTransactionAtom(item->collectionItem(), newTag));
m_list.emplace_back(item->collectionItem(), newTag);
}
Tag *TagTransactionManager::duplicateTag(const Tag *tag, const QString &fileName)
......@@ -113,7 +88,7 @@ void TagTransactionManager::forget()
bool TagTransactionManager::undo()
{
qCDebug(JUK_LOG) << "Undoing " << m_undoList.count() << " changes.\n";
qCDebug(JUK_LOG) << "Undoing " << m_undoList.size() << " changes.\n";
forget(); // Scrap our old changes (although the list should be empty
// anyways.
......@@ -147,17 +122,17 @@ bool TagTransactionManager::renameFile(const QFileInfo &from, const QFileInfo &t
bool TagTransactionManager::processChangeList(bool undo)
{
TagAlterationList::ConstIterator it, end;
TagAlterationList::const_iterator it, end;
QStringList errorItems;
it = undo ? m_undoList.constBegin() : m_list.constBegin();
end = undo ? m_undoList.constEnd() : m_list.constEnd();
it = undo ? m_undoList.cbegin() : m_list.cbegin();
end = undo ? m_undoList.cend() : m_list.cend();
emit signalAboutToModifyTags();
for(; it != end; ++it) {
PlaylistItem *item = (*it).item();
Tag *tag = (*it).tag();
const Tag *tag = (*it).tag();
QFileInfo newFile(tag->fileName());
......@@ -170,7 +145,7 @@ bool TagTransactionManager::processChangeList(bool undo)
if(tag->save()) {
if(!undo)
m_undoList.append(TagTransactionAtom(item, duplicateTag(item->file().tag())));
m_undoList.emplace_back(item, duplicateTag(item->file().tag()));
item->file().setFile(tag->fileName());
item->refreshFromDisk();
......@@ -193,7 +168,7 @@ bool TagTransactionManager::processChangeList(bool undo)
}
undo ? m_undoList.clear() : m_list.clear();
if(!undo && !m_undoList.isEmpty())
if(!undo && !m_undoList.empty())
action("edit_undo")->setEnabled(true);
else
action("edit_undo")->setEnabled(false);
......
/**
* Copyright (C) 2004 Michael Pyne <mpyne@kde.org>
* Copyright (C) 2004, 2017 Michael Pyne <mpyne@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
......@@ -18,13 +18,18 @@
#define TAGTRANSACTIONMANAGER_H
#include <QObject>
#include <QList>
#include <memory>
#include <vector>
#include "tag.h"
class PlaylistItem;
class QWidget;
class Tag;
class QFileInfo;
using std::unique_ptr;
/**
* Class to encapsulate a change to the tag, and optionally the file name, of
* a PlaylistItem.
......@@ -32,67 +37,27 @@ class QFileInfo;
* @author Michael Pyne <mpyne@kde.org>
* @see TagTransactionManager
*/
class TagTransactionAtom
struct TagTransactionAtom
{
public:
/**
* Default constructor, for use by QValueList.
*/
TagTransactionAtom();
/**
* Copy constructor. This takes ownership of the m_tag pointer, so the
* object being copied no longer has access to the tag. This function also
* exists mainly for QValueList's benefit.
*
* @param other The TagTransactionAtom to copy.
*/
TagTransactionAtom(const TagTransactionAtom &other);
/**
* Creates an atom detailing a change made by \p tag to \p item.
*
* @param tag Contains the new tag to apply to item.
* @param item The PlaylistItem to change.
* @param tag Contains the new tag to apply to item.
*/
TagTransactionAtom(PlaylistItem *item, Tag *tag);
/**
* Destroys the atom. This function deletes the tag, so make sure you've
* already copied out any data you need. The PlaylistItem is unaffected.
*/
~TagTransactionAtom();
/**
* Assignment operator. This operator takes ownership of the m_tag pointer,
* so the object being assigned from no longer has access to the tag. This
* function exists mainly for the benefit of QValueList.
*
* @param other The TagTransactionAtom to copy from.
* @return The TagTransactionAtom being assigned to.
*/
TagTransactionAtom &operator=(const TagTransactionAtom &other);
/**
* Accessor function to retrieve the PlaylistItem.
*
* @return The PlaylistItem being changed.
*/
PlaylistItem *item() const { return m_item; }
const Tag *tag() const { return m_tag.get(); }
/**
* Accessor function to retrieve the changed Tag.
*
* @return The Tag containing the changes to apply to item().
*/
Tag *tag() const { return m_tag; }
private:
PlaylistItem *m_item;
mutable Tag *m_tag;
unique_ptr<Tag> m_tag;
};
typedef QList<TagTransactionAtom> TagAlterationList;
// Qt's containers don't play well with std::unique_ptr, but being able to use
// unique_ptr avoids a bunch of custom code on my part.
using TagAlterationList = std::vector<TagTransactionAtom>;
/**
* This class manages alterations of a group of PlaylistItem's FileHandles. What this
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment