historyplaylist.cpp 4.44 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
#include <klocale.h>
#include <kglobal.h>
26 27

#include "juk_debug.h"
28

29 30 31 32
////////////////////////////////////////////////////////////////////////////////
// HistoryPlayList public members
////////////////////////////////////////////////////////////////////////////////

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

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

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

HistoryPlaylist::~HistoryPlaylist()
{

}

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

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

64 65 66 67
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

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

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

void HistoryPlaylist::slotCreateNewItem()
{
80 81
    createItem(m_file);
    m_file = FileHandle::null();
82 83
}

84 85 86 87
////////////////////////////////////////////////////////////////////////////////
// HistoryPlaylistItem public members
////////////////////////////////////////////////////////////////////////////////

88
HistoryPlaylistItem::HistoryPlaylistItem(CollectionListItem *item, Playlist *parent, QTreeWidgetItem *after) :
89 90 91
    PlaylistItem(item, parent, after),
    m_dateTime(QDateTime::currentDateTime())
{
92
    setText(0, KGlobal::locale()->formatDateTime(m_dateTime));
93 94 95 96 97 98
}

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

HistoryPlaylistItem::~HistoryPlaylistItem()
{

}

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

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

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

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

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

    return s;
}

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

    HistoryPlaylistItem *after = 0;

    QString fileName;
    QDateTime dateTime;

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

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

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

156
    p.dataChanged();
157 158 159 160

    return s;
}

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