1 /* This file is part of Clementine.
2    Copyright 2010-2011, David Sansome <me@davidsansome.com>
3    Copyright 2011, Jonathan Anderson <jontis@gmail.com>
4    Copyright 2011, Angus Gratton <gus@projectgus.com>
5    Copyright 2014, vkrishtal <krishtalhost@gmail.com>
6    Copyright 2014, John Maguire <john.maguire@gmail.com>
7    Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
8 
9    Clementine is free software: you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation, either version 3 of the License, or
12    (at your option) any later version.
13 
14    Clementine is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "filesystemmusicstorage.h"
24 #include "core/logging.h"
25 #include "core/utilities.h"
26 
27 #include <QDir>
28 #include <QFile>
29 #include <QUrl>
30 
FilesystemMusicStorage(const QString & root)31 FilesystemMusicStorage::FilesystemMusicStorage(const QString& root)
32     : root_(root) {}
33 
CopyToStorage(const CopyJob & job)34 bool FilesystemMusicStorage::CopyToStorage(const CopyJob& job) {
35   const QFileInfo src = QFileInfo(job.source_);
36   const QFileInfo dest = QFileInfo(root_ + "/" + job.destination_);
37 
38   // Don't do anything if the destination is the same as the source
39   if (src == dest) return true;
40 
41   // Create directories as required
42   QDir dir;
43   if (!dir.mkpath(dest.absolutePath())) {
44     qLog(Warning) << "Failed to create directory" << dest.dir().absolutePath();
45     return false;
46   }
47 
48   // Remove the destination file if it exists and we want to overwrite
49   if (job.overwrite_ && dest.exists()) QFile::remove(dest.absoluteFilePath());
50 
51   // Copy or move
52   if (job.remove_original_)
53     return QFile::rename(src.absoluteFilePath(), dest.absoluteFilePath());
54   else
55     return QFile::copy(src.absoluteFilePath(), dest.absoluteFilePath());
56 }
57 
DeleteFromStorage(const DeleteJob & job)58 bool FilesystemMusicStorage::DeleteFromStorage(const DeleteJob& job) {
59   QString path = job.metadata_.url().toLocalFile();
60   QFileInfo fileInfo(path);
61   if (fileInfo.isDir())
62     return Utilities::RemoveRecursive(path);
63   else
64     return QFile::remove(path);
65 }
66