historyplaylist.cpp 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2003-2004 Scott Wheeler <wheeler@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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
16 17 18

#include "historyplaylist.h"

19 20
#include <QTimer>

Michael Pyne's avatar
Michael Pyne committed
21
#include <KLocalizedString>
22

23 24 25
#include "collectionlist.h"
#include "playermanager.h"
#include "juk-exception.h"
26
#include "juk_debug.h"
27

28 29 30 31
////////////////////////////////////////////////////////////////////////////////
// HistoryPlayList public members
////////////////////////////////////////////////////////////////////////////////

32
HistoryPlaylist::HistoryPlaylist(PlaylistCollection *collection) :
33
    Playlist(collection, true, 1),
34
    m_timer(new QTimer(this))
35
{
36
    setAllowDuplicates(true);
37

38
    m_timer->setSingleShot(true);
39
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotCreateNewItem()));
40

Kacper Kasper's avatar
Kacper Kasper committed
41
    setSortingEnabled(false);
42
    headerItem()->setText(0, i18n("Time"));
43 44 45 46 47 48 49
}

HistoryPlaylist::~HistoryPlaylist()
{

}

50
HistoryPlaylistItem *HistoryPlaylist::createItem(const FileHandle &file, QTreeWidgetItem *after)
51 52
{
    if(!after)
53
        after = topLevelItem(topLevelItemCount() - 1);
54
    return Playlist::createItem<HistoryPlaylistItem>(file, after);
55 56
}

57 58
void HistoryPlaylist::createItems(const PlaylistItemList &siblings)
{
59
    Playlist::createItems<HistoryPlaylistItem, PlaylistItem>(siblings);
60
}
61

62 63 64 65
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

66
void HistoryPlaylist::appendProposedItem(const FileHandle &file)
67
{
68 69 70 71 72 73
    m_file = file;

    if(!m_file.isNull())
        m_timer->start(delay());
    else
        m_timer->stop();
74 75 76 77
}

void HistoryPlaylist::slotCreateNewItem()
{
78
    createItem(m_file);
79
    m_file = FileHandle();
80
    playlistItemsChanged();
81 82
}

83 84 85 86
////////////////////////////////////////////////////////////////////////////////
// HistoryPlaylistItem public members
////////////////////////////////////////////////////////////////////////////////

87
HistoryPlaylistItem::HistoryPlaylistItem(CollectionListItem *item, Playlist *parent, QTreeWidgetItem *after) :
88 89 90
    PlaylistItem(item, parent, after),
    m_dateTime(QDateTime::currentDateTime())
{
91
    setText(0, m_dateTime.toString());
92 93 94 95 96
}

void HistoryPlaylistItem::setDateTime(const QDateTime &dt)
{
    m_dateTime = dt;
97
    setText(0, m_dateTime.toString());
98 99 100 101 102 103 104 105 106 107
}

////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

QDataStream &operator<<(QDataStream &s, const HistoryPlaylist &p)
{
    PlaylistItemList l = const_cast<HistoryPlaylist *>(&p)->items();

Laurent Montel's avatar
Laurent Montel committed
108
    s << qint32(l.count());
109

Laurent Montel's avatar
Laurent Montel committed
110
    for(PlaylistItemList::ConstIterator it = l.constBegin(); it != l.constEnd(); ++it) {
111
        const HistoryPlaylistItem *i = static_cast<HistoryPlaylistItem *>(*it);
112
        s << i->file().absFilePath();
113
        s << i->dateTime();
114 115 116 117 118 119 120
    }

    return s;
}

QDataStream &operator>>(QDataStream &s, HistoryPlaylist &p)
{
Laurent Montel's avatar
Laurent Montel committed
121
    qint32 count;
122 123 124 125 126 127 128 129
    s >> count;

    HistoryPlaylistItem *after = 0;

    QString fileName;
    QDateTime dateTime;

    for(int i = 0; i < count; i++) {
130 131
        s >> fileName;
        s >> dateTime;
132

133 134 135
        if(fileName.isEmpty() || !dateTime.isValid())
            throw BICStreamException();

136 137
        HistoryPlaylistItem *a = p.createItem(FileHandle(fileName), after);
        if(Q_LIKELY(a)) {
138
            after = a;
139
            after->setDateTime(dateTime);
140
        }
141 142
    }

143
    p.playlistItemsChanged();
144 145 146 147

    return s;
}

148
// vim: set et sw=4 tw=0 sta: