2
* Copyright (C) 2012 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Lindsay <chaz@yorba.org>
20
#include "photo-caches.h"
25
const QString PhotoCaches::ORIGINAL_DIR = ".original";
26
const QString PhotoCaches::ENHANCED_DIR = ".enhanced";
29
* \brief PhotoCaches::PhotoCaches
32
PhotoCaches::PhotoCaches(const QFileInfo& file) : m_file(file),
33
m_originalFile(file.dir(),
34
QString("%1/%2").arg(ORIGINAL_DIR).arg(file.fileName())),
35
m_enhancedFile(file.dir(),
36
QString("%1/%2").arg(ENHANCED_DIR).arg(file.fileName()))
38
// We always want our file checks to hit the disk.
39
m_file.setCaching(false);
40
m_originalFile.setCaching(false);
41
m_enhancedFile.setCaching(false);
45
* \brief PhotoCaches::hasCachedOriginal
48
bool PhotoCaches::hasCachedOriginal() const
50
return m_originalFile.exists();
54
* \brief PhotoCaches::hasCachedEnhanced
57
bool PhotoCaches::hasCachedEnhanced() const
59
return m_enhancedFile.exists();
63
* \brief PhotoCaches::originalFile
66
const QFileInfo& PhotoCaches::originalFile() const
68
return m_originalFile;
72
* \brief PhotoCaches::enhancedFile
75
const QFileInfo& PhotoCaches::enhancedFile() const
77
return m_enhancedFile;
81
* \brief PhotoCaches::pristineFile
82
* Returns original_file() if it exists; otherwise, returns the file passed
86
const QFileInfo& PhotoCaches::pristineFile() const
88
return (hasCachedOriginal() ? m_originalFile : m_file);
92
* \brief PhotoCaches::cacheOriginal
93
* Moves the pristine file into .original so we don't mess it up. Note that
94
* this potentially removes the main file, so it must be followed by a copy
95
* from original (or elsewhere) back to the file.
98
bool PhotoCaches::cacheOriginal()
100
if (hasCachedOriginal()) {
104
m_file.dir().mkdir(ORIGINAL_DIR);
106
return rename(m_file, m_originalFile);
110
* \brief PhotoCaches::restoreOriginal
111
* Moves the file out of .original, overwriting the main file. Note that
112
* this removes the .original file.
115
bool PhotoCaches::restoreOriginal()
117
if (!hasCachedOriginal()) {
122
// touch the file so that the thumbnails will correctly regenerate
123
utime(m_originalFile.absoluteFilePath().toUtf8(), NULL);
124
return rename(m_originalFile, m_file);
128
* \brief PhotoCaches::cacheEnhancedFromOriginal
129
* Copies the file in .original to .enhanced so it can then be enhanced.
132
bool PhotoCaches::cacheEnhancedFromOriginal()
134
m_file.dir().mkdir(ENHANCED_DIR);
136
// If called subsequently, the previously cached version is replaced.
137
remove(m_enhancedFile);
138
return copy(pristineFile(), m_enhancedFile);
142
* \brief PhotoCaches::overwriteFromCache
143
* Tries to overwrite the file from one of its cached versions.
144
* \param preferEnhanced
147
bool PhotoCaches::overwriteFromCache(bool preferEnhanced)
149
if (preferEnhanced && hasCachedEnhanced()) {
151
return copy(m_enhancedFile, m_file);
152
} else if (hasCachedOriginal()) {
154
return copy(m_originalFile, m_file);
161
* \brief PhotoCaches::discardCachedOriginal
163
void PhotoCaches::discardCachedOriginal()
165
remove(m_originalFile);
169
* \brief PhotoCaches::discardCachedEnhanced
171
void PhotoCaches::discardCachedEnhanced()
173
remove(m_enhancedFile);
177
* \brief PhotoCaches::discardAll
179
void PhotoCaches::discardAll()
181
discardCachedOriginal();
182
discardCachedEnhanced();