File indexing completed on 2021-12-21 13:27:54

0001 /**
0002  * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "historyplaylist.h"
0018 
0019 #include <QTimer>
0020 
0021 #include <KLocalizedString>
0022 
0023 #include "collectionlist.h"
0024 #include "playermanager.h"
0025 #include "juk-exception.h"
0026 #include "juk_debug.h"
0027 
0028 ////////////////////////////////////////////////////////////////////////////////
0029 // HistoryPlayList public members
0030 ////////////////////////////////////////////////////////////////////////////////
0031 
0032 HistoryPlaylist::HistoryPlaylist(PlaylistCollection *collection) :
0033     Playlist(collection, true, 1),
0034     m_timer(new QTimer(this))
0035 {
0036     setAllowDuplicates(true);
0037 
0038     m_timer->setSingleShot(true);
0039     connect(m_timer, SIGNAL(timeout()), this, SLOT(slotCreateNewItem()));
0040 
0041     setSortingEnabled(false);
0042     headerItem()->setText(0, i18n("Time"));
0043 }
0044 
0045 HistoryPlaylist::~HistoryPlaylist()
0046 {
0047 
0048 }
0049 
0050 HistoryPlaylistItem *HistoryPlaylist::createItem(const FileHandle &file, QTreeWidgetItem *after)
0051 {
0052     if(!after)
0053         after = topLevelItem(topLevelItemCount() - 1);
0054     return Playlist::createItem<HistoryPlaylistItem>(file, after);
0055 }
0056 
0057 void HistoryPlaylist::createItems(const PlaylistItemList &siblings)
0058 {
0059     Playlist::createItems<QVector, HistoryPlaylistItem, PlaylistItem>(siblings);
0060 }
0061 
0062 ////////////////////////////////////////////////////////////////////////////////
0063 // private slots
0064 ////////////////////////////////////////////////////////////////////////////////
0065 
0066 void HistoryPlaylist::appendProposedItem(const FileHandle &file)
0067 {
0068     m_file = file;
0069 
0070     if(!m_file.isNull())
0071         m_timer->start(delay());
0072     else
0073         m_timer->stop();
0074 }
0075 
0076 void HistoryPlaylist::slotCreateNewItem()
0077 {
0078     createItem(m_file);
0079     m_file = FileHandle();
0080     playlistItemsChanged();
0081 }
0082 
0083 ////////////////////////////////////////////////////////////////////////////////
0084 // HistoryPlaylistItem public members
0085 ////////////////////////////////////////////////////////////////////////////////
0086 
0087 HistoryPlaylistItem::HistoryPlaylistItem(CollectionListItem *item, Playlist *parent, QTreeWidgetItem *after) :
0088     PlaylistItem(item, parent, after),
0089     m_dateTime(QDateTime::currentDateTime())
0090 {
0091     setText(0, m_dateTime.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss")));
0092 }
0093 
0094 void HistoryPlaylistItem::setDateTime(const QDateTime &dt)
0095 {
0096     m_dateTime = dt;
0097     setText(0, m_dateTime.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss")));
0098 }
0099 
0100 ////////////////////////////////////////////////////////////////////////////////
0101 // helper functions
0102 ////////////////////////////////////////////////////////////////////////////////
0103 
0104 QDataStream &operator<<(QDataStream &s, const HistoryPlaylist &p)
0105 {
0106     PlaylistItemList l = const_cast<HistoryPlaylist *>(&p)->items();
0107 
0108     s << qint32(l.count());
0109 
0110     for(PlaylistItemList::ConstIterator it = l.constBegin(); it != l.constEnd(); ++it) {
0111         const HistoryPlaylistItem *i = static_cast<HistoryPlaylistItem *>(*it);
0112         s << i->file().absFilePath();
0113         s << i->dateTime();
0114     }
0115 
0116     return s;
0117 }
0118 
0119 QDataStream &operator>>(QDataStream &s, HistoryPlaylist &p)
0120 {
0121     qint32 count;
0122     s >> count;
0123 
0124     HistoryPlaylistItem *after = 0;
0125 
0126     QString fileName;
0127     QDateTime dateTime;
0128 
0129     for(int i = 0; i < count; i++) {
0130         s >> fileName;
0131         s >> dateTime;
0132 
0133         if(fileName.isEmpty() || !dateTime.isValid())
0134             throw BICStreamException();
0135 
0136         HistoryPlaylistItem *a = p.createItem(FileHandle(fileName), after);
0137         if(Q_LIKELY(a)) {
0138             after = a;
0139             after->setDateTime(dateTime);
0140         }
0141     }
0142 
0143     p.playlistItemsChanged();
0144 
0145     return s;
0146 }
0147 
0148 // vim: set et sw=4 tw=0 sta: