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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/**
* Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
* Copyright (C) 2008, 2013 Michael Pyne <mpyne@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/>.
*/
#include "cache.h"
#include "juk-exception.h"
#include <kmessagebox.h>
#include <kconfig.h>
#include <ktoggleaction.h>
#include <KLocalizedString>
#include <QDir>
#include <QSaveFile>
#include "juktag.h"
#include "searchplaylist.h"
#include "historyplaylist.h"
#include "upcomingplaylist.h"
#include "folderplaylist.h"
#include "playlistcollection.h"
#include "actioncollection.h"
#include "juk.h"
#include "juk_debug.h"
using namespace ActionCollection;
const int Cache::playlistListCacheVersion = 3;
const int Cache::playlistItemsCacheVersion = 2;
enum PlaylistType
{
Normal = 0,
Search = 1,
History = 2,
Upcoming = 3,
Folder = 4
};
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
Cache *Cache::instance()
{
static Cache cache;
return &cache;
}
static void parsePlaylistStream(QDataStream &s, PlaylistCollection *collection)
{
while(!s.atEnd()) {
qint32 playlistType;
s >> playlistType;
Playlist *playlist = nullptr;
switch(playlistType) {
case Search:
{
SearchPlaylist *p = new SearchPlaylist(collection, *(new PlaylistSearch(JuK::JuKInstance())));
s >> *p;
playlist = p;
break;
}
case History:
{
action<KToggleAction>("showHistory")->setChecked(true);
collection->setHistoryPlaylistEnabled(true);
s >> *collection->historyPlaylist();
playlist = collection->historyPlaylist();
break;
}
case Upcoming:
{
collection->setUpcomingPlaylistEnabled(true);
UpcomingPlaylist *p = collection->upcomingPlaylist();
action<KToggleAction>("saveUpcomingTracks")->setChecked(true);
s >> *p;
playlist = p;
break;
}
case Folder:
{
FolderPlaylist *p = new FolderPlaylist(collection);
s >> *p;
playlist = p;
break;
}
case Normal:
{
Playlist *p = new Playlist(collection, true);
s >> *p;
// We may have already read this playlist from the folder
// scanner, if an .m3u playlist
if(collection->containsPlaylistFile(p->fileName())) {
delete p;
p = nullptr;
}
playlist = p;
break;
}
default:
qCWarning(JUK_LOG) << "Unknown playlist type" << playlistType << "loading saved playlists";
throw BICStreamException();
} // switch
qint32 sortColumn;
s >> sortColumn;
if(playlist)
playlist->sortByColumn(sortColumn);
}
}
void Cache::loadPlaylists(PlaylistCollection *collection) // static
{
const QString playlistsFile = playlistsCacheFileName();
QFile f(playlistsFile);
if(!f.open(QIODevice::ReadOnly))
return;
QDataStream fs(&f);
qint32 version;
fs >> version;
if(version != 3 || fs.status() != QDataStream::Ok) {
// Either the file is corrupt or is from a truly ancient version
// of JuK.
qCWarning(JUK_LOG) << "Found the playlist cache but it was clearly corrupt.";
return;
}
// Our checksum is only for the values after the version and checksum so
// we want to get a byte array with just the checksummed data.
QByteArray data;
quint16 checksum;
fs >> checksum >> data;
if(fs.status() != QDataStream::Ok || checksum != qChecksum(data))
return;
QDataStream s(&data, QIODevice::ReadOnly);
s.setVersion(QDataStream::Qt_4_3);
try { // Loading failures are indicated by an exception
parsePlaylistStream(s, collection);
}
catch(BICStreamException &) {
qCCritical(JUK_LOG) << "Exception loading playlists - binary incompatible stream.";
// TODO Restructure the Playlist data model and PlaylistCollection data model
// to be separate from the view/controllers.
return;
}
}
void Cache::savePlaylists(const PlaylistList &playlists)
{
QString playlistsFile = playlistsCacheFileName();
QSaveFile f(playlistsFile);
if(!f.open(QIODevice::WriteOnly)) {
qCCritical(JUK_LOG) << "Error saving collection:" << f.errorString();
return;
}
QByteArray data;
QDataStream s(&data, QIODevice::WriteOnly);
s.setVersion(QDataStream::Qt_4_3);
for(const auto &it : playlists) {
if(!(it)) {
continue;
}
// TODO back serialization type into Playlist itself
if(dynamic_cast<HistoryPlaylist *>(it)) {
s << qint32(History)
<< *static_cast<HistoryPlaylist *>(it);
}
else if(dynamic_cast<SearchPlaylist *>(it)) {
s << qint32(Search)
<< *static_cast<SearchPlaylist *>(it);
}
else if(dynamic_cast<UpcomingPlaylist *>(it)) {
if(!action<KToggleAction>("saveUpcomingTracks")->isChecked())
continue;
s << qint32(Upcoming)
<< *static_cast<UpcomingPlaylist *>(it);
}
else if(dynamic_cast<FolderPlaylist *>(it)) {
s << qint32(Folder)
<< *static_cast<FolderPlaylist *>(it);
}
else {
s << qint32(Normal)
<< *(it);
}
s << qint32(it->sortColumn());
}
QDataStream fs(&f);
fs << qint32(playlistListCacheVersion);
fs << qChecksum(data);
fs << data;
if(!f.commit())
qCCritical(JUK_LOG) << "Error saving collection:" << f.errorString();
}
void Cache::ensureAppDataStorageExists() // static
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QDir appDataDir(dirPath);
if(!appDataDir.exists() && !appDataDir.mkpath(dirPath))
qCCritical(JUK_LOG) << "Unable to create appdata storage in" << dirPath;
}
bool Cache::cacheFileExists() // static
{
return QFile::exists(fileHandleCacheFileName());
}
// Despite the 'Cache' class name, these data files are not regenerable and so
// should not be stored in cache directory.
QString Cache::fileHandleCacheFileName() // static
{
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/cache";
}
QString Cache::playlistsCacheFileName() // static
{
return QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/playlists";
}
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////
Cache::Cache()
{
}
bool Cache::prepareToLoadCachedItems()
{
m_loadFile.setFileName(fileHandleCacheFileName());
if(!m_loadFile.open(QIODevice::ReadOnly))
return false;
m_loadDataStream.setDevice(&m_loadFile);
int dataStreamVersion = CacheDataStream::Qt_3_3;
qint32 version;
m_loadDataStream >> version;
switch(version) {
case 2:
dataStreamVersion = CacheDataStream::Qt_4_3;
Q_FALLTHROUGH();
// Other than that we're compatible with cache v1, so fallthrough
// to setCacheVersion
case 1: {
m_loadDataStream.setCacheVersion(1);
m_loadDataStream.setVersion(dataStreamVersion);
qint32 checksum;
m_loadDataStream >> checksum
>> m_loadFileBuffer.buffer();
m_loadFileBuffer.open(QIODevice::ReadOnly);
m_loadDataStream.setDevice(&m_loadFileBuffer);
qint32 checksumExpected = qChecksum(m_loadFileBuffer.data());
if(m_loadDataStream.status() != CacheDataStream::Ok ||
checksum != checksumExpected)
{
qCCritical(JUK_LOG) << "Music cache checksum expected to get" << checksumExpected <<
"actually was" << checksum;
KMessageBox::error(0, i18n("The music data cache has been corrupted. JuK "
"needs to rescan it now. This may take some time."));
return false;
}
break;
}
default: {
m_loadDataStream.device()->reset();
m_loadDataStream.setCacheVersion(0);
// This cache is so old that this is just a wild guess here that 3.3
// is compatible.
m_loadDataStream.setVersion(CacheDataStream::Qt_3_3);
break;
}
}
return true;
}
FileHandle Cache::loadNextCachedItem()
{
if(!m_loadFile.isOpen() || !m_loadDataStream.device()) {
qCWarning(JUK_LOG) << "Already completed reading cache file.";
return FileHandle();
}
if(m_loadDataStream.status() == QDataStream::ReadCorruptData) {
qCCritical(JUK_LOG) << "Attempted to read file handle from corrupt cache file.";
return FileHandle();
}
if(!m_loadDataStream.atEnd()) {
QString fileName;
m_loadDataStream >> fileName;
fileName.squeeze();
return FileHandle(fileName, m_loadDataStream);
}
else {
m_loadDataStream.setDevice(0);
m_loadFile.close();
return FileHandle();
}
}
// vim: set et sw=4 tw=0 sta:
|