Folder.cpp 5.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23
#include "Folder.h"
24
#include "Device.h"
25
#include "Media.h"
26

27
#include "database/SqliteTools.h"
28
#include "filesystem/IDirectory.h"
29
30
31
32

namespace policy
{
    const std::string FolderTable::Name = "Folder";
33
    const std::string FolderTable::PrimaryKeyColumn = "id_folder";
34
35
36
    unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
}

37
Folder::Folder(DBConnection dbConnection, sqlite::Row& row )
38
39
    : m_dbConection( dbConnection )
{
40
41
42
43
    row >> m_id
        >> m_path
        >> m_parent
        >> m_lastModificationDate
44
        >> m_isBlacklisted
45
46
        >> m_deviceId
        >> m_isPresent;
47
48
}

49
Folder::Folder( const std::string& path, time_t lastModificationDate, unsigned int parent, unsigned int deviceId )
50
51
    : m_id( 0 )
    , m_path( path )
52
    , m_parent( parent )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53
    , m_lastModificationDate( lastModificationDate )
54
    , m_isBlacklisted( false )
55
    , m_deviceId( deviceId )
56
    , m_isPresent( true )
57
58
59
60
61
{
}

bool Folder::createTable(DBConnection connection)
{
62
63
64
65
66
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FolderTable::Name +
            "("
            "id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
            "path TEXT UNIQUE ON CONFLICT FAIL,"
            "id_parent UNSIGNED INTEGER,"
67
            "last_modification_date UNSIGNED INTEGER,"
68
            "is_blacklisted INTEGER,"
69
            "device_id UNSIGNED INTEGER,"
70
            "is_present BOOLEAN NOT NULL DEFAULT 1,"
71
            "FOREIGN KEY (id_parent) REFERENCES " + policy::FolderTable::Name +
72
73
74
            "(id_folder) ON DELETE CASCADE,"
            "FOREIGN KEY (device_id) REFERENCES " + policy::DeviceTable::Name +
            "(id_device) ON DELETE CASCADE"
75
            ")";
76
77
78
79
80
81
82
    std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_device_present AFTER UPDATE OF is_present ON "
            + policy::DeviceTable::Name +
            " BEGIN"
            " UPDATE " + policy::FolderTable::Name + " SET is_present = new.is_present WHERE device_id = new.id_device;"
            " END";
    return sqlite::Tools::executeRequest( connection, req ) &&
            sqlite::Tools::executeRequest( connection, triggerReq );
83
84
}

85
std::shared_ptr<Folder> Folder::create(DBConnection connection, const std::string& path, time_t lastModificationDate, unsigned int parentId, Device& device )
86
{
87
    auto self = std::make_shared<Folder>( path, lastModificationDate, parentId, device.id() );
88
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
89
            "(path, id_parent, last_modification_date, device_id) VALUES(?, ?, ?, ?)";
90
    if ( insert( connection, self, req, path, sqlite::ForeignKey( parentId ),
91
                         lastModificationDate, device.id() ) == false )
92
        return nullptr;
93
94
95
96
    self->m_dbConection = connection;
    return self;
}

97
98
99
100
101
102
103
bool Folder::blacklist( DBConnection connection, const std::string& path )
{
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
            "(path, id_parent, is_blacklisted) VALUES(?, ?, ?)";
    return sqlite::Tools::insert( connection, req, path, nullptr, true ) != 0;
}

104
105
std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string& path )
{
106
    const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND is_blacklisted IS NULL";
107
108
109
    return fetch( conn, req, path );
}

110
111
112
113
114
unsigned int Folder::id() const
{
    return m_id;
}

115
const std::string& Folder::path() const
116
117
118
119
{
    return m_path;
}

120
std::vector<MediaPtr> Folder::files()
121
{
122
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
123
        " WHERE folder_id = ?";
124
    return Media::fetchAll<IMedia>( m_dbConection, req, m_id );
125
}
126

127
std::vector<std::shared_ptr<Folder> > Folder::folders()
128
129
{
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
130
            " WHERE id_parent = ? AND is_blacklisted IS NULL";
131
    return fetchAll<Folder>( m_dbConection, req, m_id );
132
133
}

134
std::shared_ptr<Folder> Folder::parent()
135
{
136
    return fetch( m_dbConection, m_parent );
137
}
138
139
140
141
142

unsigned int Folder::lastModificationDate()
{
    return m_lastModificationDate;
}
143
144
145
146
147
148
149
150
151
152

bool Folder::setLastModificationDate( unsigned int lastModificationDate )
{
    static const std::string req = "UPDATE " + policy::FolderTable::Name +
            " SET last_modification_date = ? WHERE id_folder = ?";
    if ( sqlite::Tools::executeUpdate( m_dbConection, req, lastModificationDate, m_id ) == false )
        return false;
    m_lastModificationDate = lastModificationDate;
    return true;
}
153

154
unsigned int Folder::deviceId() const
155
{
156
    return m_deviceId;
157
}
158
159
160
161
162

bool Folder::isPresent() const
{
    return m_isPresent;
}