Folder.h 8.07 KB
Newer Older
1
2
3
/*****************************************************************************
 * Media Library
 *****************************************************************************
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
4
 * Copyright (C) 2015-2019 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
5
 *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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
24
#pragma once

25
#include "medialibrary/IFolder.h"
26
#include "medialibrary/IMedia.h"
27
#include "database/DatabaseHelpers.h"
28

29
30
31
namespace medialibrary
{

32
class File;
33
class Device;
34

35
36
37
// This doesn't publicly expose the DatabaseHelper inheritance in order to force
// the user to go through Folder's overloads, as they take care of the device mountpoint
// fetching & path composition
38
class Folder : public IFolder, public DatabaseHelpers<Folder>
39
40
{
public:
41
42
43
44
45
46
    struct Table
    {
        static const std::string Name;
        static const std::string PrimaryKeyColumn;
        static int64_t Folder::*const PrimaryKey;
    };
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
47

48
49
50
51
    struct FtsTable
    {
        static const std::string Name;
    };
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52

53
    /* Deprecated since model 30 */
54
55
56
57
    struct ExcludedFolderTable
    {
        static const std::string Name;
    };
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
58

59
60
61
62
63
64
65
66
    enum class Triggers : uint8_t
    {
        InsertFts,
        DeleteFts,
        UpdateNbMediaOnIndex,
        UpdateNbMediaOnUpdate,
        UpdateNbMediaOnDelete,
    };
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67

68
69
70
71
72
    enum class Indexes : uint8_t
    {
        DeviceId,
        ParentId,
    };
73

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74
75
76
77
78
79
80
    enum class BannedType
    {
        Yes,    //< Only select banned folders
        No,     //< Only select unbanned folders
        Any,    //< Well... any of the above.
    };

81
82
83
84
85
86
87
88
89
90
91
92
93
94
    enum class RemovalBehavior
    {
        /*
         * The folder was removed from disk: remove it from the db and delete
         * the media it contained
         */
        RemovedFromDisk,
        /*
         * The entry point was explicitely removed. Flag it as such in database
         * but keep its media on disk
         */
        EntrypointRemoved,
    };

95
    Folder( MediaLibraryPtr ml, sqlite::Row& row );
96
    Folder(MediaLibraryPtr ml, const std::string& path, std::string name, int64_t parent,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
97
            int64_t deviceId , bool isRemovable );
98

99
    static void createTable( sqlite::Connection* connection );
100
101
    static void createTriggers( sqlite::Connection* connection );
    static void createIndexes( sqlite::Connection* connection );
102
    static std::string schema( const std::string& tableName, uint32_t dbModel );
103
    static std::string trigger( Triggers trigger, uint32_t dbModel );
104
    static std::string triggerName( Triggers trigger, uint32_t dbModel );
105
    static std::string index( Indexes index, uint32_t dbModel );
106
    static std::string indexName( Indexes index, uint32_t dbModel );
107
    static bool checkDbModel( MediaLibraryPtr ml );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
108
    static std::shared_ptr<Folder> create( MediaLibraryPtr ml, const std::string& mrl,
109
                                           int64_t parentId, const Device& device,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
110
                                           fs::IDevice& deviceFs );
111
    static bool ban( MediaLibraryPtr ml, const std::string& mrl );
112
    static std::vector<std::shared_ptr<Folder>> fetchRootFolders( MediaLibraryPtr ml );
113

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
114
115
116
117
    static std::shared_ptr<Folder> fromMrl( MediaLibraryPtr ml,
                                            const std::string& mrl );
    static std::shared_ptr<Folder> bannedFolder( MediaLibraryPtr ml,
                                                 const std::string& mrl );
118
119
120
121
122
    static Query<IFolder> withMedia( MediaLibraryPtr ml, IMedia::Type type,
                                     const QueryParameters* params );
    static Query<IFolder> searchWithMedia( MediaLibraryPtr ml,
                                           const std::string& pattern,
                                           IMedia::Type type,
123
                                           const QueryParameters* params );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
124
125
    static Query<IFolder> entryPoints( MediaLibraryPtr ml, bool banned,
                                       int64_t deviceId );
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    /**
     * @brief deleteFolder Mark a folder in database as removed
     * @param ml The media library instance
     * @param folder The folder to remove
     * @param behavior An enum member to drive the removal behavior
     * @return true in case of success, false otherwise.
     *
     * This will mark the folder as removed. If the folder was an entry point,
     * it will simply be removed from the database. If it was a sub folder of an
     * entry point, the folder will be marked as banned in order to not be
     * discovered again.
     * If the behavior passed is RemovedFromDisk, all the media that belonged to
     * that folder will be removed from the database. This also means that the
     * media will be removed from any media group or playlist they belonged to.
     * Otherwise, when behavior is EntrypointRemoved, the media will be converted
     * to external media, and will be kept in database.
     */
    static bool remove( MediaLibraryPtr ml, std::shared_ptr<Folder> folder, RemovalBehavior behavior );
144

145
    virtual int64_t id() const override;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
146
    virtual const std::string& mrl() const override;
147
    virtual const std::string& name() const override;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
148
    // Used for 13 -> 14 migration
149
    void setName( std::string name );
150
151
    const std::string& rawMrl() const;
    void setMrl( std::string mrl );
152
    std::vector<std::shared_ptr<File>> files();
153
154
    std::vector<std::shared_ptr<Folder>> folders();
    std::shared_ptr<Folder> parent();
155
    int64_t deviceId() const;
156
    virtual bool isRemovable() const override;
157
    virtual bool isPresent() const override;
158
    virtual bool isBanned() const override;
159
    bool isRootFolder() const;
160
161
    virtual Query<IMedia> media( IMedia::Type type,
                                 const QueryParameters* params ) const override;
162
163
164
    virtual Query<IMedia> searchMedia( const std::string& pattern,
                                       IMedia::Type type,
                                       const QueryParameters* params = nullptr ) const override;
165
    virtual Query<IFolder> subfolders( const QueryParameters* params ) const override;
166

167
168
169
170
    virtual uint32_t nbVideo() const override;
    virtual uint32_t nbAudio() const override;
    virtual uint32_t nbMedia() const override;

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
171
172
173
    static std::shared_ptr<Folder> fromMrl( MediaLibraryPtr ml,
                                            const std::string& mrl,
                                            BannedType bannedType );
174

175
176
private:
    static std::string sortRequest( const QueryParameters* params );
177
    static std::string filterByMediaType( IMedia::Type type );
178

179
    std::shared_ptr<Device> device() const;
180
    bool ban();
181

182
private:
183
    MediaLibraryPtr m_ml;
184

185
    int64_t m_id;
186
    // This contains the path relative to the device mountpoint (ie. excluding it)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
187
    // or the full path (including mrl scheme) for folders on non removable devices
188
    std::string m_path;
189
    mutable std::string m_name;
190
    const int64_t m_parent;
191
    const bool m_isBanned;
192
    const int64_t m_deviceId;
193
194
195
    // Can't be const anymore, but should be if we ever get to remove the
    // removable->non removable device fixup (introduced after vlc-android 3.1.0 rc3)
    bool m_isRemovable;
196
197
    uint32_t m_nbAudio;
    uint32_t m_nbVideo;
198

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
199
    mutable std::shared_ptr<Device> m_device;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
200
201
    // This contains the full path, including device mountpoint (and mrl scheme,
    // as its part of the mountpoint
202
    mutable std::string m_fullPath;
203

204
    friend struct Folder::Table;
205
};
206
207

}