-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlplaylist.cpp
More file actions
201 lines (177 loc) · 7.21 KB
/
Copy pathhtmlplaylist.cpp
File metadata and controls
201 lines (177 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* qmmp-htmlplaylist -- exports your playlist as a single HTML file
* Copyright (C) 2020 Georg Gadinger <nilsding@nilsding.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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <QAction>
#include <QApplication>
#include <QDesktopServices>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
#include <QSettings>
#include <QTemporaryFile>
#include <QTime>
#include <qmmpui/mediaplayer.h>
#include <qmmpui/playlisttrack.h>
#include <qmmpui/uihelper.h>
#include "template.h"
#include "htmlplaylist.h"
HTMLPlaylist::HTMLPlaylist(QObject* parent) : QObject(parent)
{
m_action = new QAction(tr("Generate HTML playlist"), this);
m_action->setShortcut(Qt::CTRL + Qt::ALT + Qt::Key_G);
UiHelper::instance()->addAction(m_action, UiHelper::PLAYLIST_MENU);
connect(m_action, &QAction::triggered, this, &HTMLPlaylist::exportPlaylistToHTML);
}
HTMLPlaylist::~HTMLPlaylist()
{
}
void HTMLPlaylist::exportPlaylistToHTML()
{
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
PlayListManager* pl = MediaPlayer::instance()->playListManager();
auto selectedPlayList = pl->selectedPlayList();
int trackCount = selectedPlayList->trackCount();
QList<PlayListTrack*> tracks;
quint64 totalTrackLength = 0; //!< in milliseconds
int invalidTracks = 0;
for (int i = 0; i < trackCount + invalidTracks; i++)
{
auto track = selectedPlayList->track(i);
if (track == nullptr)
{
invalidTracks++;
continue;
}
tracks.append(track);
totalTrackLength += track->duration();
}
quint64 totalTrackLengthDays = totalTrackLength / 86400000; // 1 day in ms
quint64 totalTrackLengthMsec = totalTrackLength % 86400000; // 1 day in ms
QTime totalTrackLengthTime = QTime::fromMSecsSinceStartOfDay(totalTrackLengthMsec);
quint64 totalTrackLengthHours =
(totalTrackLengthDays * 24) + totalTrackLengthTime.hour(); // days are counted in hours
quint64 averageTrackLength = trackCount == 0 ? 0 : totalTrackLength / trackCount;
quint64 averageTrackLengthDays = averageTrackLength / 86400000; // 1 day in ms
quint64 averageTrackLengthMsec = averageTrackLength % 86400000; // 1 day in ms
QTime averageTrackLengthTime = QTime::fromMSecsSinceStartOfDay(averageTrackLengthMsec);
quint64 averageTrackLengthHours =
(averageTrackLengthDays * 24) + averageTrackLengthTime.hour(); // days are counted in hours
QTemporaryFile tempFile(QDir::temp().filePath("playlist_XXXXXX.html"));
tempFile.setAutoRemove(false);
if (!tempFile.open())
{
QMessageBox::critical(qApp->activeWindow(), tr("Error"), tr("Could not create a temporary file."));
return;
}
QUrl tempFileUrl = QUrl("file://" + tempFile.fileName(), QUrl::TolerantMode);
QString html = "";
int theme = settings.value("htmlplaylist_plugin/theme", Template::Winamp).toInt();
TemplateBase* tpl = Template::templateFor(static_cast<Template::AvailableTemplates>(theme));
// add header
html.append(tpl->headerStart());
// header: add title
html.append(settings.value("htmlplaylist_plugin/title", "Winamp Generated PlayList").toString());
// header: add rest of </head> and start body
html.append(tpl->headerEnd());
// playlist stats: begin
QString averageTrackLengthFormatted = "";
if (averageTrackLengthHours > 0)
{
averageTrackLengthFormatted.append(QString::number(averageTrackLengthHours));
averageTrackLengthFormatted.append(":");
averageTrackLengthFormatted.append(
averageTrackLengthTime.toString("mm:ss")); // QTime only counts until 24 hours
}
else
{
averageTrackLengthFormatted.append(averageTrackLengthTime.toString("m:ss"));
}
html.append(tpl->playlistStatsStart(trackCount, averageTrackLengthFormatted));
// playlist stats: append total track length
if (totalTrackLengthHours > 0)
{
QString hour = tr("hour");
QString hours = tr("hours");
html.append(
tpl->playlistStatsTotalNumberSingleUnit(totalTrackLengthHours, totalTrackLengthHours != 1 ? hours : hour));
}
if (totalTrackLengthTime.minute() > 0)
{
QString minute = tr("minute");
QString minutes = tr("minutes");
html.append(tpl->playlistStatsTotalNumberSingleUnit(totalTrackLengthTime.minute(),
totalTrackLengthTime.minute() != 1 ? minutes : minute));
}
QString second = tr("second");
QString seconds = tr("seconds");
html.append(tpl->playlistStatsTotalNumberSingleUnit(totalTrackLengthTime.second(),
totalTrackLengthTime.second() != 1 ? seconds : second));
if (settings.value("htmlplaylist_plugin/include_save_as", true).toBool())
{
// right click to save
html.append(tpl->playlistStatsRightClickToSave(tempFileUrl.toString().replace("\"", "\\\"")));
}
// playlist stats: end
html.append(tpl->playlistStatsEnd());
// playlist files: start
html.append(tpl->playlistFilesStart());
// playlist files: add each file
bool showTrackPosition = settings.value("htmlplaylist_plugin/show_track_position", true).toBool();
bool showTrackDuration = settings.value("htmlplaylist_plugin/show_track_duration", true).toBool();
for (int i = 0; i < tracks.size(); i++)
{
auto tr = tracks.at(i);
auto artist = tr->value(Qmmp::ARTIST);
auto title = tr->value(Qmmp::TITLE);
auto filename = QFileInfo(tr->path()).fileName();
QString entry = "";
if (showTrackPosition)
{
entry.append(QString::number(i + 1));
entry.append(". ");
}
if (!artist.isEmpty())
{
entry.append(artist);
entry.append(" - ");
}
if (title.isEmpty())
{
entry.append(filename);
}
else
{
entry.append(title);
}
if (showTrackDuration && !tr->formattedLength().isEmpty())
{
entry.append(" (");
entry.append(tr->formattedLength());
entry.append(")");
}
html.append(tpl->playlistFilesEntry(entry));
}
// playlist files: end
html.append(tpl->playlistFilesEnd());
// add footer
html.append(tpl->footer());
tempFile.write(html.toUtf8());
tempFile.close();
qInfo("Wrote playlist to %s", tempFile.fileName().toUtf8().data());
QDesktopServices::openUrl(tempFileUrl);
Template::free(tpl, static_cast<Template::AvailableTemplates>(theme));
}