~bfiller/gallery-app/gallery-app-46-new

« back to all changes in this revision

Viewing changes to src/photoeditor/photo-caches.cpp

mergeĀ lp:~artmello/gallery-app/gallery-app-remove_local_photoeditor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2012 Canonical Ltd
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 * Authors:
17
 
 * Charles Lindsay <chaz@yorba.org>
18
 
 */
19
 
 
20
 
#include "photo-caches.h"
21
 
 
22
 
#include <QDir>
23
 
#include <utime.h>
24
 
 
25
 
const QString PhotoCaches::ORIGINAL_DIR = ".original";
26
 
const QString PhotoCaches::ENHANCED_DIR = ".enhanced";
27
 
 
28
 
/*!
29
 
 * \brief PhotoCaches::PhotoCaches
30
 
 * \param file
31
 
 */
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()))
37
 
{
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);
42
 
}
43
 
 
44
 
/*!
45
 
 * \brief PhotoCaches::hasCachedOriginal
46
 
 * \return
47
 
 */
48
 
bool PhotoCaches::hasCachedOriginal() const
49
 
{
50
 
    return m_originalFile.exists();
51
 
}
52
 
 
53
 
/*!
54
 
 * \brief PhotoCaches::hasCachedEnhanced
55
 
 * \return
56
 
 */
57
 
bool PhotoCaches::hasCachedEnhanced() const
58
 
{
59
 
    return m_enhancedFile.exists();
60
 
}
61
 
 
62
 
/*!
63
 
 * \brief PhotoCaches::originalFile
64
 
 * \return
65
 
 */
66
 
const QFileInfo& PhotoCaches::originalFile() const
67
 
{
68
 
    return m_originalFile;
69
 
}
70
 
 
71
 
/*!
72
 
 * \brief PhotoCaches::enhancedFile
73
 
 * \return
74
 
 */
75
 
const QFileInfo& PhotoCaches::enhancedFile() const
76
 
{
77
 
    return m_enhancedFile;
78
 
}
79
 
 
80
 
/*!
81
 
 * \brief PhotoCaches::pristineFile
82
 
 * Returns original_file() if it exists; otherwise, returns the file passed
83
 
 * to the constructor.
84
 
 * \return
85
 
 */
86
 
const QFileInfo& PhotoCaches::pristineFile() const
87
 
{
88
 
    return (hasCachedOriginal() ? m_originalFile : m_file);
89
 
}
90
 
 
91
 
/*!
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.
96
 
 * \return
97
 
 */
98
 
bool PhotoCaches::cacheOriginal()
99
 
{
100
 
    if (hasCachedOriginal()) {
101
 
        return true;
102
 
    }
103
 
 
104
 
    m_file.dir().mkdir(ORIGINAL_DIR);
105
 
 
106
 
    return rename(m_file, m_originalFile);
107
 
}
108
 
 
109
 
/*!
110
 
 * \brief PhotoCaches::restoreOriginal
111
 
 * Moves the file out of .original, overwriting the main file.  Note that
112
 
 * this removes the .original file.
113
 
 * \return
114
 
 */
115
 
bool PhotoCaches::restoreOriginal()
116
 
{
117
 
    if (!hasCachedOriginal()) {
118
 
        return true;
119
 
    }
120
 
 
121
 
    remove(m_file);
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);
125
 
}
126
 
 
127
 
/*!
128
 
 * \brief PhotoCaches::cacheEnhancedFromOriginal
129
 
 * Copies the file in .original to .enhanced so it can then be enhanced.
130
 
 * \return
131
 
 */
132
 
bool PhotoCaches::cacheEnhancedFromOriginal()
133
 
{
134
 
    m_file.dir().mkdir(ENHANCED_DIR);
135
 
 
136
 
    // If called subsequently, the previously cached version is replaced.
137
 
    remove(m_enhancedFile);
138
 
    return copy(pristineFile(), m_enhancedFile);
139
 
}
140
 
 
141
 
/*!
142
 
 * \brief PhotoCaches::overwriteFromCache
143
 
 * Tries to overwrite the file from one of its cached versions.
144
 
 * \param preferEnhanced
145
 
 * \return
146
 
 */
147
 
bool PhotoCaches::overwriteFromCache(bool preferEnhanced)
148
 
{
149
 
    if (preferEnhanced && hasCachedEnhanced()) {
150
 
        remove(m_file);
151
 
        return copy(m_enhancedFile, m_file);
152
 
    } else if (hasCachedOriginal()) {
153
 
        remove(m_file);
154
 
        return copy(m_originalFile, m_file);
155
 
    } else {
156
 
        return true;
157
 
    }
158
 
}
159
 
 
160
 
/*!
161
 
 * \brief PhotoCaches::discardCachedOriginal
162
 
 */
163
 
void PhotoCaches::discardCachedOriginal()
164
 
{
165
 
    remove(m_originalFile);
166
 
}
167
 
 
168
 
/*!
169
 
 * \brief PhotoCaches::discardCachedEnhanced
170
 
 */
171
 
void PhotoCaches::discardCachedEnhanced()
172
 
{
173
 
    remove(m_enhancedFile);
174
 
}
175
 
 
176
 
/*!
177
 
 * \brief PhotoCaches::discardAll
178
 
 */
179
 
void PhotoCaches::discardAll()
180
 
{
181
 
    discardCachedOriginal();
182
 
    discardCachedEnhanced();
183
 
}