Commit 33343b90 authored by Scott Wheeler's avatar Scott Wheeler

Start porting things over to the new fandangled FileHandle class. There's

still a lot of damage control yet to be done, but this at least gets things
building and working after porting the PlaylistItem over.

svn path=/trunk/kdemultimedia/juk/; revision=293445
parent 4ae2f5f8
......@@ -192,15 +192,17 @@ void CollectionListItem::slotRefresh()
// CollectionListItem protected methods
////////////////////////////////////////////////////////////////////////////////
CollectionListItem::CollectionListItem(const QFileInfo &file, const QString &path) :
CollectionListItem::CollectionListItem(const QFileInfo &info, const QString &path) :
PlaylistItem(CollectionList::instance()),
m_path(path)
{
CollectionList *l = CollectionList::instance();
if(l) {
l->addToDict(m_path, this);
setData(Data::newUser(file, m_path));
if(data()->tag()) {
setFile(FileHandle(info, path));
if(file().tag()) {
slotRefresh();
connect(this, SIGNAL(signalRefreshed()), l, SIGNAL(signalDataChanged()));
l->emitCountChanged();
......@@ -220,8 +222,8 @@ CollectionListItem::~CollectionListItem()
{
CollectionList *l = CollectionList::instance();
if(l) {
QString path = Playlist::resolveSymLinks(*data()->fileInfo());
// l->removeWatched(m_path);
QString path = Playlist::resolveSymLinks(file().absFilePath());
// l->removeWatched(m_path);
l->removeFromDict(m_path);
}
}
......@@ -234,11 +236,11 @@ void CollectionListItem::addChildItem(PlaylistItem *child)
bool CollectionListItem::checkCurrent()
{
if(!data()->exists() || !data()->isFile())
if(!file().fileInfo().exists() || !file().fileInfo().isFile())
return false;
if(!data()->tag()->current()) {
data()->refresh();
if(!file().tag()->current()) {
file().refresh();
slotRefresh();
}
......
......@@ -87,6 +87,12 @@ void FileHandle::refresh()
d->tag = Tag::createTag(d->absFilePath);
}
void FileHandle::setFile(const QString &path)
{
d->fileInfo.setFile(path);
d->absFilePath = d->fileInfo.absFilePath();
}
Tag *FileHandle::tag() const
{
if(!d->tag)
......
......@@ -31,13 +31,14 @@ public:
FileHandle();
FileHandle(const FileHandle &f);
FileHandle(const QFileInfo &info, const QString &path = QString::null);
FileHandle(const QString &path);
explicit FileHandle(const QString &path);
~FileHandle();
/**
* Forces the FileHandle to reread its information from the disk.
*/
void refresh();
void setFile(const QString &path);
Tag *tag() const;
QString absFilePath() const;
......
......@@ -147,17 +147,17 @@ QString FileRenamer::expandToken(TokenType type, const QString &value_) const
void FileRenamer::rename(PlaylistItem *item)
{
if(item == 0 || item->tag() == 0)
if(item == 0 || item->file().tag() == 0)
return;
QString newFilename = rename(item->absFilePath(), *item->tag());
QString newFilename = rename(item->file().absFilePath(), *item->file().tag());
if(KMessageBox::warningContinueCancel(0,
i18n("<qt>You are about to rename the file<br/><br/> '%1'<br/><br/> to <br/><br/>'%2'<br/><br/>Are you sure you "
"want to continue?</qt>").arg(item->absFilePath()).arg(newFilename),
"want to continue?</qt>").arg(item->file().absFilePath()).arg(newFilename),
i18n("Warning"), KStdGuiItem::cont(), "ShowFileRenamerWarning")
== KMessageBox::Continue) {
if(moveFile(item->absFilePath(), newFilename))
item->setFile(newFilename);
if(moveFile(item->file().absFilePath(), newFilename))
item->setFile(FileHandle(newFilename));
}
}
......@@ -168,13 +168,13 @@ void FileRenamer::rename(const PlaylistItemList &items)
PlaylistItemList::ConstIterator it = items.begin();
for(; it != items.end(); ++it) {
if(!*it || !(*it)->tag())
if(!*it || !(*it)->file().tag())
continue;
const QString oldName = (*it)->absFilePath();
const QString newName = rename(oldName, *(*it)->tag());
map[oldName] = newName;
itemMap[oldName] = *it;
const QString oldName = (*it)->file().absFilePath();
const QString newName = rename(oldName, *(*it)->file().tag());
map[oldName] = newName;
itemMap[oldName] = *it;
}
if(ConfirmationDialog(map).exec() == QDialog::Accepted) {
......@@ -184,7 +184,7 @@ void FileRenamer::rename(const PlaylistItemList &items)
QMap<QString, QString>::ConstIterator it = map.begin();
for(; it != map.end(); ++it, ++j) {
if(moveFile(it.key(), it.data()))
itemMap[it.key()]->setFile(it.data());
itemMap[it.key()]->setFile(FileHandle(it.data()));
if(j % 5 == 0)
kapp->processEvents();
......
......@@ -93,7 +93,7 @@ QDataStream &operator<<(QDataStream &s, const HistoryPlaylist &p)
for(PlaylistItemList::ConstIterator it = l.begin(); it != l.end(); ++it) {
const HistoryPlaylistItem *i = static_cast<HistoryPlaylistItem *>(*it);
s << i->filePath();
s << i->file().absFilePath();
s << i->dateTime();
}
......
......@@ -298,7 +298,7 @@ void Playlist::saveAs()
void Playlist::clearItem(PlaylistItem *item, bool emitChanged)
{
emit signalAboutToRemove(item);
m_members.remove(item->absFilePath());
m_members.remove(item->file().absFilePath());
if(!m_randomList.isEmpty() && !m_visibleChanged)
m_randomList.remove(item);
item->deleteLater();
......@@ -324,7 +324,7 @@ QStringList Playlist::files()
QStringList list;
for(QListViewItemIterator it(this); it.current(); ++it)
list.append(static_cast<PlaylistItem *>(*it)->absFilePath());
list.append(static_cast<PlaylistItem *>(*it)->file().absFilePath());
return list;
}
......@@ -458,8 +458,8 @@ int Playlist::totalTime()
QListViewItemIterator it(this);
while (it.current()) {
PlaylistItem *item = static_cast<PlaylistItem *>(it.current());
if (item->tag()->current())
time += item->tag()->seconds();
if(item->file().tag()->current())
time += item->file().tag()->seconds();
it++;
}
return time;
......@@ -535,7 +535,7 @@ void Playlist::slotRefresh()
for(PlaylistItemList::Iterator it = l.begin(); it != l.end(); ++it) {
(*it)->slotRefreshFromDisk();
if(!(*it)->tag()) {
if(!(*it)->file().tag()) {
kdDebug(65432) << "Error while trying to refresh the tag. "
<< "This file has probably been removed."
<< endl;
......@@ -604,7 +604,7 @@ void Playlist::removeFromDisk(const PlaylistItemList &items)
QStringList files;
for(PlaylistItemList::ConstIterator it = items.begin(); it != items.end(); ++it)
files.append((*it)->fileName());
files.append((*it)->file().absFilePath());
QString message;
......@@ -615,13 +615,13 @@ void Playlist::removeFromDisk(const PlaylistItemList &items)
if(KMessageBox::warningYesNoList(this, message, files) == KMessageBox::Yes) {
for(PlaylistItemList::ConstIterator it = items.begin(); it != items.end(); ++it) {
if(QFile::remove((*it)->filePath())) {
if(QFile::remove((*it)->file().absFilePath())) {
if(!m_randomList.isEmpty() && !m_visibleChanged)
m_randomList.remove(*it);
CollectionList::instance()->clearItem((*it)->collectionItem());
}
else
KMessageBox::sorry(this, i18n("Could not delete ") + (*it)->fileName() + ".");
KMessageBox::sorry(this, i18n("Could not delete ") + (*it)->file().absFilePath() + ".");
}
}
......@@ -635,7 +635,7 @@ QDragObject *Playlist::dragObject(QWidget *parent)
KURL::List urls;
for(PlaylistItemList::Iterator it = items.begin(); it != items.end(); ++it) {
KURL url;
url.setPath((*it)->absFilePath());
url.setPath((*it)->file().absFilePath());
urls.append(url);
}
......@@ -1339,36 +1339,36 @@ void Playlist::editTag(PlaylistItem *item, const QString &text, int column)
switch(column - columnOffset())
{
case PlaylistItem::TrackColumn:
item->tag()->setTitle(text);
item->file().tag()->setTitle(text);
break;
case PlaylistItem::ArtistColumn:
item->tag()->setArtist(text);
item->file().tag()->setArtist(text);
break;
case PlaylistItem::AlbumColumn:
item->tag()->setAlbum(text);
item->file().tag()->setAlbum(text);
break;
case PlaylistItem::TrackNumberColumn:
{
bool ok;
int value = text.toInt(&ok);
if(ok)
item->tag()->setTrack(value);
item->file().tag()->setTrack(value);
break;
}
case PlaylistItem::GenreColumn:
item->tag()->setGenre(text);
item->file().tag()->setGenre(text);
break;
case PlaylistItem::YearColumn:
{
bool ok;
int value = text.toInt(&ok);
if(ok)
item->tag()->setYear(value);
item->file().tag()->setYear(value);
break;
}
}
item->tag()->save();
item->file().tag()->save();
item->slotRefresh();
}
......
......@@ -704,7 +704,7 @@ void Playlist::createItems(const QValueList<SiblingType *> &siblings)
QValueListConstIterator<SiblingType *> it = siblings.begin();
for(; it != siblings.end(); ++it) {
if(!m_members.insert(resolveSymLinks((*it)->absFilePath())) || m_allowDuplicates) {
if(!m_members.insert(resolveSymLinks((*it)->file().absFilePath())) || m_allowDuplicates) {
newItem = new ItemType((*it)->collectionItem(), this, newItem);
setupItem(newItem);
if(!m_randomList.isEmpty() && !m_visibleChanged)
......
This diff is collapsed.
......@@ -21,6 +21,7 @@
#include <config.h>
#include <klistview.h>
#include <ksharedptr.h>
#include <qptrstack.h>
#include <qptrdict.h>
......@@ -30,6 +31,7 @@
#include "tagguesser.h"
#include "tag.h"
#include "cache.h"
#include "filehandle.h"
#if HAVE_MUSICBRAINZ == 0
// a bit of a hack so that the slots type definition is still valid
......@@ -78,9 +80,8 @@ public:
static int lastColumn() { return FileNameColumn; }
void setFile(const QString &file);
Tag *tag();
const Tag *tag() const;
void setFile(const FileHandle &file);
FileHandle file() const;
virtual QString text(int column) const;
virtual void setText(int column, const QString &text);
......@@ -88,15 +89,11 @@ public:
// These are just forwarding methods to PlaylistItem::Data, a QFileInfo
// subclass.
QString fileName() const;
QString filePath() const;
QString absFilePath() const;
QString dirPath(bool absPath = false) const;
bool isWritable() const;
void setPlaying(bool playing = true) { m_playing = playing; }
virtual void setSelected(bool selected);
void guessTagInfo(TagGuesser::Type type);
Playlist *playlist() const;
/**
......@@ -144,11 +141,6 @@ protected:
*/
virtual ~PlaylistItem();
class Data;
Data *data() { return m_data; }
const Data *data() const { return m_data; }
void setData(Data *d) { m_data = d; }
virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align);
virtual int compare(QListViewItem *item, int column, bool ascending) const;
......@@ -168,70 +160,23 @@ signals:
void signalAboutToDelete();
private:
void setup(CollectionListItem *item, Playlist *parent);
CollectionListItem *m_collectionItem;
Data *m_data;
bool m_playing;
};
/**
* This is the data class for PlaylistItems. Several PlaylistItems that are
* based on the same file will share the m_data member. This has both the
* advantages of being memory efficient and allowing the PlaylistItems to stay
* synchronized.
*
* The sharing is implemented through a refcount and protected constructors and
* destructors that make it necessary to obtain pointers via newUser() and to
* free an instance using deleteUser().
*/
class PlaylistItem::Data
{
public:
static Data *newUser(const QFileInfo &file, const QString &path);
Data *newUser();
void deleteUser();
void refresh();
struct Data : public KShared
{
Data() {}
Data(const QFileInfo &info, const QString &path) : fileHandle(info, path) {}
Data(const QString &path) : fileHandle(path) {}
Tag *tag();
const Tag *tag() const;
FileHandle fileHandle;
QValueVector<QCString> local8Bit;
QValueVector<int> cachedWidths;
};
void setFile(const QString &file);
bool isFile() const { return m_fileInfo.isFile(); }
bool exists() const { return m_fileInfo.exists(); }
const QFileInfo *fileInfo() const { return &m_fileInfo; }
KSharedPtr<Data> d;
QString absFilePath() const { return m_absFileName; }
void setColumns(int columns);
void setLocal8BitLower(int column, const QCString &value) { m_local8Bit[column] = value; }
QCString local8BitLower(int column) const { return m_local8Bit[column]; }
void setCachedWidth(int column, int value) { m_cachedWidths[column] = value; }
int cachedWidth(int column) const { return m_cachedWidths[column]; }
QValueVector<int> cachedWidths() const { return m_cachedWidths; }
protected:
/**
* Because we're trying to use this as a shared item, we want all access
* to be through pointers (so that it's safe to use delete this). Thus
* creation of the object should be done by the newUser methods above
* and deletion should be handled by deleteUser. Making the constructor
* and destructor protected ensures this.
*/
Data(const QFileInfo &file, const QString &path);
~Data();
private:
QFileInfo m_fileInfo;
int m_referenceCount;
Tag *m_dataTag;
QString m_absFileName;
QValueVector<QCString> m_local8Bit;
QValueVector<int> m_cachedWidths;
void setup(CollectionListItem *item, Playlist *parent);
CollectionListItem *m_collectionItem;
bool m_playing;
};
#endif
......@@ -273,7 +273,7 @@ void PlaylistSplitter::populatePlayHistoryMenu(QPopupMenu *menu, bool random)
menu->clear();
int i = 0;
for(PlaylistItemList::Iterator it = list.begin(); it != list.end(); ++it)
menu->insertItem((*it)->tag()->title(), ++i);
menu->insertItem((*it)->file().tag()->title(), ++i);
}
QString PlaylistSplitter::playSelectedFile()
......@@ -796,7 +796,7 @@ QString PlaylistSplitter::play(PlaylistItem *item)
m_history->createItems(l);
}
return item->absFilePath();
return item->file().absFilePath();
}
void PlaylistSplitter::redisplaySearch()
......
......@@ -85,7 +85,7 @@ void TagEditor::slotRefresh()
// the most common case -- is to just process the first item. Then we
// check after that to see if there are other m_items and adjust accordingly.
if(m_items.isEmpty() || !m_items.first()->tag()) {
if(m_items.isEmpty() || !m_items.first()->file().tag()) {
slotClear();
setEnabled(false);
return;
......@@ -95,13 +95,13 @@ void TagEditor::slotRefresh()
PlaylistItem *item = m_items.first();
Tag *tag = item->tag();
Tag *tag = item->file().tag();
m_artistNameBox->setEditText(tag->artist());
m_trackNameBox->setText(tag->title());
m_albumNameBox->setEditText(tag->album());
m_fileNameBox->setText(item->fileName());
m_fileNameBox->setText(item->file().absFilePath());
m_bitrateBox->setText(QString::number(tag->bitrate()));
m_lengthBox->setText(tag->lengthString());
......@@ -153,7 +153,7 @@ void TagEditor::slotRefresh()
}
else {
for(; it != m_items.end(); ++it) {
tag = (*it)->tag();
tag = (*it)->file().tag();
if(tag) {
......@@ -430,23 +430,24 @@ void TagEditor::save(const PlaylistItemList &list)
for(PlaylistItemList::ConstIterator it = list.begin(); it != list.end(); ++it) {
PlaylistItem *item = *it;
QFileInfo newFile(item->dirPath() + QDir::separator() + m_fileNameBox->text());
QFileInfo directory(item->dirPath());
QFileInfo newFile(item->file().fileInfo().dirPath() + QDir::separator() +
m_fileNameBox->text());
QFileInfo directory(item->file().fileInfo().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 not we'll append it to errorFiles to tell the user which
// files we couldn't write to.
if(item->tag() &&
if(item->file().tag() &&
(newFile.isWritable() || (!newFile.exists() && directory.isWritable())) &&
item->isWritable())
item->file().fileInfo().isWritable())
{
// If the file name in the box doesn't match the current file
// name...
if(list.count() == 1 && item->fileName() != newFile.fileName()) {
if(list.count() == 1 && item->file().absFilePath() != newFile.fileName()) {
// Rename the file if it doesn't exist or the user says
// that it's ok.
......@@ -458,8 +459,8 @@ void TagEditor::save(const PlaylistItemList &list)
i18n("File Exists")) == KMessageBox::Yes)
{
QDir currentDir;
currentDir.rename(item->filePath(), newFile.filePath());
item->setFile(newFile.filePath());
currentDir.rename(item->file().absFilePath(), newFile.filePath());
item->file().setFile(newFile.filePath());
}
}
......@@ -470,27 +471,27 @@ void TagEditor::save(const PlaylistItemList &list)
// each field that we write.
if(m_enableBoxes[m_artistNameBox]->isOn())
item->tag()->setArtist(m_artistNameBox->currentText());
item->file().tag()->setArtist(m_artistNameBox->currentText());
if(m_enableBoxes[m_trackNameBox]->isOn())
item->tag()->setTitle(m_trackNameBox->text());
item->file().tag()->setTitle(m_trackNameBox->text());
if(m_enableBoxes[m_albumNameBox]->isOn())
item->tag()->setAlbum(m_albumNameBox->currentText());
item->file().tag()->setAlbum(m_albumNameBox->currentText());
if(m_enableBoxes[m_trackSpin]->isOn())
item->tag()->setTrack(m_trackSpin->value());
item->file().tag()->setTrack(m_trackSpin->value());
if(m_enableBoxes[m_yearSpin]->isOn())
item->tag()->setYear(m_yearSpin->value());
item->file().tag()->setYear(m_yearSpin->value());
if(m_enableBoxes[m_commentBox]->isOn())
item->tag()->setComment(m_commentBox->text());
item->file().tag()->setComment(m_commentBox->text());
if(m_enableBoxes[m_genreBox]->isOn())
item->tag()->setGenre(m_genreBox->currentText());
item->file().tag()->setGenre(m_genreBox->currentText());
item->tag()->save();
item->file().tag()->save();
item->slotRefresh();
}
else
errorFiles.append(item->fileName());
errorFiles.append(item->file().absFilePath());
kapp->processEvents();
}
......@@ -513,7 +514,7 @@ void TagEditor::saveChangesPrompt()
QStringList files;
for(PlaylistItemList::Iterator it = m_items.begin(); it != m_items.end(); it++)
files.append((*it)->fileName());
files.append((*it)->file().absFilePath());
if(KMessageBox::questionYesNoList(this,
i18n("Do you want to save your changes to:\n"),
......
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