historyplaylist.cpp 4.43 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"
#include "collectionlist.h"
19
#include "playermanager.h"
20
#include "juk-exception.h"
21

22 23
#include <QTimer>

24 25 26 27
#include <klocale.h>
#include <kglobal.h>
#include <kdebug.h>

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,
51
                                                 QTreeWidgetItem *after, bool emitChanged)
52 53
{
    if(!after)
54
        after = topLevelItem(topLevelItemCount() - 1);
55
    return Playlist::createItem<HistoryPlaylistItem>(file, after, emitChanged);
56 57
}

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

63 64 65 66
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

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

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

void HistoryPlaylist::slotCreateNewItem()
{
79 80
    createItem(m_file);
    m_file = FileHandle::null();
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, KGlobal::locale()->formatDateTime(m_dateTime));
92 93 94 95 96 97
}

HistoryPlaylistItem::HistoryPlaylistItem(CollectionListItem *item, Playlist *parent) :
    PlaylistItem(item, parent),
    m_dateTime(QDateTime::currentDateTime())
{
98
    setText(0, KGlobal::locale()->formatDateTime(m_dateTime));
99 100 101 102 103 104 105 106 107 108
}

HistoryPlaylistItem::~HistoryPlaylistItem()
{

}

void HistoryPlaylistItem::setDateTime(const QDateTime &dt)
{
    m_dateTime = dt;
109
    setText(0, KGlobal::locale()->formatDateTime(m_dateTime));
110 111 112 113 114 115 116 117 118 119
}

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

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

Laurent Montel's avatar
Laurent Montel committed
120
    s << qint32(l.count());
121

Laurent Montel's avatar
Laurent Montel committed
122
    for(PlaylistItemList::ConstIterator it = l.constBegin(); it != l.constEnd(); ++it) {
123
        const HistoryPlaylistItem *i = static_cast<HistoryPlaylistItem *>(*it);
124
        s << i->file().absFilePath();
125
        s << i->dateTime();
126 127 128 129 130 131 132
    }

    return s;
}

QDataStream &operator>>(QDataStream &s, HistoryPlaylist &p)
{
Laurent Montel's avatar
Laurent Montel committed
133
    qint32 count;
134 135 136 137 138 139 140 141
    s >> count;

    HistoryPlaylistItem *after = 0;

    QString fileName;
    QDateTime dateTime;

    for(int i = 0; i < count; i++) {
142 143
        s >> fileName;
        s >> dateTime;
144

145 146 147
        if(fileName.isEmpty() || !dateTime.isValid())
            throw BICStreamException();

148 149 150
        HistoryPlaylistItem *a = p.createItem(FileHandle(fileName), after, false);
        if(a) {
            after = a;
151
            after->setDateTime(dateTime);
152
        }
153 154
    }

155
    p.dataChanged();
156 157 158 159

    return s;
}

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