~artmello/gallery-app/gallery-app-fix_crash_adding_files

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
/*
 * Copyright (C) 2012 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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/>.
 *
 * Authors:
 * Eric Gregory <eric@yorba.org>
 */

#include "album-table.h"
#include "database.h"

// album
#include "album.h"

#include <QtSql>

/*!
 * \brief AlbumTable::AlbumTable
 * \param db
 * \param parent
 */
AlbumTable::AlbumTable(Database* db, QObject* parent)
    : QObject(parent),
      m_db(db)
{
}

/*!
 * \brief AlbumTable::get_albums returns a set of all albums
 * Returns a set of all getAlbums
 * \param albumSet
 */
void AlbumTable::getAlbums(QList<Album*>* albumSet)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("SELECT id, title, subtitle, time_added, is_closed, current_page, "
                  "cover_nickname FROM AlbumTable ORDER BY time_added DESC");
    if (!query.exec())
        m_db->logSqlError(query);

    while (query.next()) {
        QDateTime timestamp;

        qint64 id = query.value(0).toLongLong();
        QString title = query.value(1).toString();
        QString subtitle = query.value(2).toString();
        timestamp.setMSecsSinceEpoch(query.value(3).toLongLong());
        bool is_closed = query.value(4).toBool();
        int current_page = query.value(5).toInt();
        QString cover_nickname = query.value(6).toString();

        Album* a = new Album(this, title, subtitle, id,
                             timestamp, is_closed, current_page, cover_nickname);
        a->setAlbumTable(this);
        albumSet->append(a);
    }
}

/*!
 * \brief AlbumTable::addAlbum adds an album to the DB
 * \param album
 */
void AlbumTable::addAlbum(Album* album)
{
    if (album->id() != INVALID_ID)
        return; // Nothing to do here.

    QSqlQuery query(*m_db->getDB());
    query.prepare("INSERT INTO AlbumTable (title, subtitle, time_added, is_closed, "
                  "current_page, cover_nickname) "
                  "VALUES (:title, :subtitle, :time_added, :is_closed, :page, "
                  ":cover_nickname)");
    query.bindValue(":title", album->title());
    query.bindValue(":subtitle", album->subtitle());
    query.bindValue(":time_added", album->creationDateTime().toMSecsSinceEpoch());
    query.bindValue(":is_closed", album->isClosed());
    query.bindValue(":page", album->currentPage());
    query.bindValue(":cover_nickname", album->coverNickname());
    if (!query.exec())
        m_db->logSqlError(query);

    album->setId(query.lastInsertId().toLongLong());
}

/*!
 * \brief AlbumTable::removeAlbum removes an album from the DB.
 * \param album
 */
void AlbumTable::removeAlbum(Album* album)
{
    if (album->id() == INVALID_ID)
        return; // Nothing to remove.

    QSqlQuery query(*m_db->getDB());
    query.prepare("DELETE FROM AlbumTable WHERE id = :id");
    query.bindValue(":id", album->id());
    if (!query.exec())
        m_db->logSqlError(query);

    album->setId(INVALID_ID);
}

/*!
 * \brief AlbumTable::isAttachedToAlbum check if a photo is attached to album
 * \param albumId
 * \param mediaId
 */
bool AlbumTable::isAttachedToAlbum(qint64 albumId, qint64 mediaId) const
{
    QSqlQuery query(*m_db->getDB());

    query.prepare("SELECT COUNT(*) FROM MediaAlbumTable WHERE album_id = :album_id AND media_id = :media_id");
    query.bindValue(":album_id", albumId);
    query.bindValue(":media_id", mediaId);
    if (!query.exec())
        m_db->logSqlError(query);
    else if (query.next() && (query.value(0).toInt() > 0))
        return true;

    return false;
}

/*!
 * \brief AlbumTable::attachToAlbum adds a photo to an album.
 * \param albumId
 * \param mediaId
 */
void AlbumTable::attachToAlbum(qint64 albumId, qint64 mediaId)
{
    if (isAttachedToAlbum(albumId, mediaId))
        return;

    QSqlQuery query(*m_db->getDB());
    query.prepare("INSERT INTO MediaAlbumTable (album_id, media_id) "
                  "VALUES (:album_id, :media_id)");
    query.bindValue(":album_id", albumId);
    query.bindValue(":media_id", mediaId);
    if (!query.exec())
        m_db->logSqlError(query);
}

/*!
 * \brief AlbumTable::detachFromAlbum removes a photo from an album.
 * \param albumId
 * \param mediaId
 */
void AlbumTable::detachFromAlbum(qint64 albumId, qint64 mediaId)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("DELETE FROM MediaAlbumTable WHERE album_id = :album_id AND "
                  "media_id = :media_id");
    query.bindValue(":album_id", albumId);
    query.bindValue(":media_id", mediaId);
    if (!query.exec())
        m_db->logSqlError(query);
}

/*!
 * \brief AlbumTable::mediaForAlbum returns a list of photos for an album.
 * \param albumId
 * \param list
 */
void AlbumTable::mediaForAlbum(qint64 albumId, QList<qint64>* list) const
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("SELECT media_id FROM MediaAlbumTable WHERE "
                  "album_id = :album_id");
    query.bindValue(":album_id", albumId);
    if (!query.exec())
        m_db->logSqlError(query);

    while (query.next())
        list->append(query.value(0).toLongLong());
}

/*!
 * \brief AlbumTable::setIsClosed Sets whether or not an album is open
 * \param albumId
 * \param isClosed
 */
void AlbumTable::setIsClosed(qint64 albumId, bool isClosed)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("UPDATE AlbumTable SET is_closed = :is_closed WHERE "
                  "id = :album_id");
    query.bindValue(":is_closed", isClosed);
    query.bindValue(":album_id", albumId);
    if (!query.exec())
        m_db->logSqlError(query);
}

/*!
 * \brief AlbumTable::setCurrentPage Sets the current page of the album.
 * \param albumId
 * \param page
 */
void AlbumTable::setCurrentPage(qint64 albumId, int page)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("UPDATE AlbumTable SET current_page = :page WHERE "
                  "id = :album_id");
    query.bindValue(":page", page);
    query.bindValue(":album_id", albumId);
    if (!query.exec())
        m_db->logSqlError(query);
}

/*!
 * \brief AlbumTable::setCoverNickname Sets the cover style for the album.
 * \param albumId
 * \param coverNickname
 */
void AlbumTable::setCoverNickname(qint64 albumId, QString coverNickname)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("UPDATE AlbumTable SET cover_nickname = :cover_nickname WHERE "
                  "id = :album_id");
    query.bindValue(":cover_nickname", coverNickname);
    query.bindValue(":album_id", albumId);
    if (!query.exec())
        m_db->logSqlError(query);
}

/*!
 * \brief AlbumTable::setTitle Sets the title of the album.
 * \param albumId
 * \param title
 */
void AlbumTable::setTitle(qint64 albumId, QString title)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("UPDATE AlbumTable SET title = :title WHERE "
                  "id = :album_id");
    query.bindValue(":title", title);
    query.bindValue(":album_id", albumId);
    if (!query.exec())
        m_db->logSqlError(query);
}

/*!
 * \brief AlbumTable::setSubtitle Sets the subtitle of the album.
 * \param albumId
 * \param subtitle
 */
void AlbumTable::setSubtitle(qint64 albumId, QString subtitle)
{
    QSqlQuery query(*m_db->getDB());
    query.prepare("UPDATE AlbumTable SET subtitle = :subtitle WHERE "
                  "id = :album_id");
    query.bindValue(":subtitle", subtitle);
    query.bindValue(":album_id", albumId);
    if (!query.exec())
        m_db->logSqlError(query);
}