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)
......
......@@ -32,55 +32,49 @@
PlaylistItem::~PlaylistItem()
{
emit signalAboutToDelete();
m_data->deleteUser();
}
void PlaylistItem::setFile(const QString &file)
void PlaylistItem::setFile(const FileHandle &file)
{
m_data->setFile(file);
d->fileHandle = file;
slotRefresh();
}
Tag *PlaylistItem::tag()
FileHandle PlaylistItem::file() const
{
return m_data->tag();
}
const Tag *PlaylistItem::tag() const
{
return m_data->tag();
return d->fileHandle;
}
QString PlaylistItem::text(int column) const
{
if(!m_data->tag())
if(!d->fileHandle.tag())
return QString::null;
int offset = static_cast<Playlist *>(listView())->columnOffset();
switch(column - offset) {
case TrackColumn:
return m_data->tag()->title();
return d->fileHandle.tag()->title();
case ArtistColumn:
return m_data->tag()->artist();
return d->fileHandle.tag()->artist();
case AlbumColumn:
return m_data->tag()->album();
return d->fileHandle.tag()->album();
case TrackNumberColumn:
return m_data->tag()->track() > 0
? QString::number(m_data->tag()->track())
return d->fileHandle.tag()->track() > 0
? QString::number(d->fileHandle.tag()->track())
: QString::null;
case GenreColumn:
return m_data->tag()->genre();
return d->fileHandle.tag()->genre();
case YearColumn:
return m_data->tag()->year() > 0
? QString::number(m_data->tag()->year())
return d->fileHandle.tag()->year() > 0
? QString::number(d->fileHandle.tag()->year())
: QString::null;
case LengthColumn:
return m_data->tag()->lengthString();
return d->fileHandle.tag()->lengthString();
case CommentColumn:
return m_data->tag()->comment();
return d->fileHandle.tag()->comment();
case FileNameColumn:
return m_data->tag()->fileName();
return d->fileHandle.tag()->fileName();
default:
return KListViewItem::text(column);
}
......@@ -98,31 +92,6 @@ void PlaylistItem::setText(int column, const QString &text)
emit signalColumnWidthChanged(column);
}
QString PlaylistItem::fileName() const
{
return m_data->fileInfo()->fileName();
}
QString PlaylistItem::filePath() const
{
return m_data->fileInfo()->filePath();
}
QString PlaylistItem::absFilePath() const
{
return m_data->absFilePath();
}
QString PlaylistItem::dirPath(bool absPath) const
{
return m_data->fileInfo()->dirPath(absPath);
}
bool PlaylistItem::isWritable() const
{
return m_data->fileInfo()->isWritable();
}
void PlaylistItem::setSelected(bool selected)
{
static_cast<Playlist *>(listView())->markItemSelected(this, selected);
......@@ -134,20 +103,20 @@ void PlaylistItem::guessTagInfo(TagGuesser::Type type)
switch(type) {
case TagGuesser::FileName:
{
TagGuesser guesser(tag()->fileName());
TagGuesser guesser(d->fileHandle.tag()->fileName());
if(!guesser.title().isNull())
tag()->setTitle(guesser.title());
d->fileHandle.tag()->setTitle(guesser.title());
if(!guesser.artist().isNull())
tag()->setArtist(guesser.artist());
d->fileHandle.tag()->setArtist(guesser.artist());
if(!guesser.album().isNull())
tag()->setAlbum(guesser.album());
d->fileHandle.tag()->setAlbum(guesser.album());
if(!guesser.track().isNull())
tag()->setTrack(guesser.track().toInt());
d->fileHandle.tag()->setTrack(guesser.track().toInt());
if(!guesser.comment().isNull())
tag()->setComment(guesser.comment());
d->fileHandle.tag()->setComment(guesser.comment());
tag()->save();
d->fileHandle.tag()->save();
slotRefresh();
break;
}
......@@ -155,7 +124,7 @@ void PlaylistItem::guessTagInfo(TagGuesser::Type type)
{
#if HAVE_MUSICBRAINZ
MusicBrainzQuery *query = new MusicBrainzQuery(MusicBrainzQuery::File,
tag()->fileName());
d->fileHandle.tag()->fileName());
connect(query, SIGNAL(signalDone(const MusicBrainzQuery::TrackList &)),
SLOT(slotTagGuessResults(const MusicBrainzQuery::TrackList &)));
KMainWindow *win = dynamic_cast<KMainWindow *>(kapp->mainWidget());
......@@ -179,7 +148,7 @@ Playlist *PlaylistItem::playlist() const
QValueVector<int> PlaylistItem::cachedWidths() const
{
return m_data->cachedWidths();
return d->cachedWidths;
}
////////////////////////////////////////////////////////////////////////////////
......@@ -196,7 +165,7 @@ void PlaylistItem::slotRefresh()
void PlaylistItem::slotRefreshFromDisk()
{
m_data->refresh();
d->fileHandle.refresh();
slotRefresh();
}
......@@ -212,14 +181,14 @@ void PlaylistItem::slotClear()
PlaylistItem::PlaylistItem(CollectionListItem *item, Playlist *parent) :
QObject(parent), KListViewItem(parent),
m_playing(false)
d(0), m_playing(false)
{
setup(item, parent);
}
PlaylistItem::PlaylistItem(CollectionListItem *item, Playlist *parent, QListViewItem *after) :
QObject(parent), KListViewItem(parent, after),
m_playing(false)
d(0), m_playing(false)
{
setup(item, parent);
}
......@@ -229,7 +198,7 @@ PlaylistItem::PlaylistItem(CollectionListItem *item, Playlist *parent, QListView
PlaylistItem::PlaylistItem(CollectionList *parent) :
QObject(parent), KListViewItem(parent),
m_collectionItem(static_cast<CollectionListItem *>(this)), m_data(0), m_playing(false)
d(new Data), m_collectionItem(static_cast<CollectionListItem *>(this)), m_playing(false)
{
setDragEnabled(true);
}
......@@ -303,32 +272,31 @@ int PlaylistItem::compare(const PlaylistItem *firstItem, const PlaylistItem *sec
}
if(column == TrackNumberColumn + offset) {
if(firstItem->tag()->track() > secondItem->tag()->track())
if(firstItem->d->fileHandle.tag()->track() > secondItem->d->fileHandle.tag()->track())
return 1;
else if(firstItem->tag()->track() < secondItem->tag()->track())
else if(firstItem->d->fileHandle.tag()->track() < secondItem->d->fileHandle.tag()->track())
return -1;
else
return 0;
}
else if(column == LengthColumn + offset) {
if(firstItem->tag()->seconds() > secondItem->tag()->seconds())
if(firstItem->d->fileHandle.tag()->seconds() > secondItem->d->fileHandle.tag()->seconds())
return 1;
else if(firstItem->tag()->seconds() < secondItem->tag()->seconds())
else if(firstItem->d->fileHandle.tag()->seconds() < secondItem->d->fileHandle.tag()->seconds())
return -1;
else
return 0;
}
else
return strcoll(firstItem->data()->local8BitLower(column - offset),
secondItem->data()->local8BitLower(column - offset));
return strcoll(firstItem->d->local8Bit[column - offset],
secondItem->d->local8Bit[column - offset]);
}
bool PlaylistItem::isValid() const
{
return m_data && m_data->tag();
return bool(d->fileHandle.tag());
}
////////////////////////////////////////////////////////////////////////////////
// PlaylistItem protected slots
////////////////////////////////////////////////////////////////////////////////
......@@ -337,7 +305,8 @@ void PlaylistItem::slotRefreshImpl()
{
int offset = static_cast<Playlist *>(listView())->columnOffset();
int columns = lastColumn() + offset + 1;
m_data->setColumns(columns);
d->local8Bit.resize(columns);
d->cachedWidths.resize(columns);
for(int i = offset; i < columns; i++) {
int id = i - offset;
......@@ -355,13 +324,13 @@ void PlaylistItem::slotRefreshImpl()
{
lower = StringShare::tryShare(lower);
}
m_data->setLocal8BitLower(id, lower);
d->local8Bit[id] = lower;
}
int newWidth = width(listView()->fontMetrics(), listView(), i);
m_data->setCachedWidth(i, newWidth);
d->cachedWidths[i] = newWidth;
if(newWidth != m_data->cachedWidth(i))
if(newWidth != d->cachedWidths[i])
emit signalColumnWidthChanged(i);
}
......@@ -379,7 +348,7 @@ void PlaylistItem::slotTagGuessResults(const MusicBrainzQuery::TrackList &res)
return;
}
TrackPickerDialog *trackPicker = new TrackPickerDialog(fileName(), res, win);
TrackPickerDialog *trackPicker = new TrackPickerDialog(d->fileHandle.absFilePath(), res, win);
if(win && trackPicker->exec() != QDialog::Accepted) {
win->statusBar()->message(i18n("Canceled."), 2000);
......@@ -389,15 +358,15 @@ void PlaylistItem::slotTagGuessResults(const MusicBrainzQuery::TrackList &res)
MusicBrainzQuery::Track track = trackPicker->selectedTrack();
if(!track.name.isEmpty())
tag()->setTitle(track.name);
d->fileHandle.tag()->setTitle(track.name);
if(!track.artist.isEmpty())
tag()->setArtist(track.artist);
d->fileHandle.tag()->setArtist(track.artist);
if(!track.album.isEmpty())
tag()->setAlbum(track.album);
d->fileHandle.tag()->setAlbum(track.album);
if(track.number)
tag()->setTrack(track.number);
d->fileHandle.tag()->setTrack(track.number);
tag()->save();
d->fileHandle.tag()->save();
slotRefresh();
if(win)
......@@ -415,12 +384,10 @@ void PlaylistItem::setup(CollectionListItem *item, Playlist *parent)
{
m_collectionItem = item;
if(item) {
m_data = item->data()->newUser();
item->addChildItem(this);
slotRefreshImpl();
connect(this, SIGNAL(signalRefreshed()), parent, SIGNAL(signalDataChanged()));
}
d = item->d;
item->addChildItem(this);
slotRefreshImpl();
connect(this, SIGNAL(signalRefreshed()), parent, SIGNAL(signalDataChanged()));
setDragEnabled(true);
......@@ -431,76 +398,4 @@ void PlaylistItem::setup(CollectionListItem *item, Playlist *parent)
connect(this, SIGNAL(signalColumnWidthChanged(int)), parent, SLOT(slotWeightDirty(int)));
}
////////////////////////////////////////////////////////////////////////////////
// PlaylistItem::Data public methods
////////////////////////////////////////////////////////////////////////////////
PlaylistItem::Data *PlaylistItem::Data::newUser(const QFileInfo &file, const QString &path)
{
return new Data(file, path);
}
PlaylistItem::Data *PlaylistItem::Data::newUser()
{
m_referenceCount++;
return this;
}
void PlaylistItem::Data::refresh()
{
m_fileInfo.refresh();
delete m_dataTag;
m_dataTag = Tag::createTag(m_fileInfo.filePath());
Q_ASSERT(m_dataTag);
m_absFileName = m_fileInfo.absFilePath();
}
void PlaylistItem::Data::deleteUser()
{
// The delete this is safe because we control object creation through a
// protected constructor and the newUser() methods.
if(--m_referenceCount == 0)
delete this;
}
Tag *PlaylistItem::Data::tag()
{
return m_dataTag;
}
const Tag *PlaylistItem::Data::tag() const
{
return m_dataTag;
}
void PlaylistItem::Data::setFile(const QString &file)
{
m_fileInfo.setFile(file);
refresh();
}
void PlaylistItem::Data::setColumns(int columns)
{
m_local8Bit.resize(columns);
m_cachedWidths.resize(columns, -1);
}
////////////////////////////////////////////////////////////////////////////////
// PlaylistItem::Data protected methods
////////////////////////////////////////////////////////////////////////////////
PlaylistItem::Data::Data(const QFileInfo &file, const QString &path) :
m_fileInfo(file),
m_referenceCount(1),
m_absFileName(path)
{
m_dataTag = Tag::createTag(path);
}
PlaylistItem::Data::~Data()
{
delete m_dataTag;
}