Folder.h 4.03 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 24
#pragma once

25
#include "medialibrary/IFolder.h"
26
#include "medialibrary/filesystem/IDevice.h"
27
#include "database/DatabaseHelpers.h"
28
#include "utils/Cache.h"
29 30 31

#include <sqlite3.h>

32 33 34
namespace medialibrary
{

35
class File;
36
class Folder;
37
class Device;
38

39 40 41 42 43
namespace fs
{
    class IDirectory;
}

44 45 46 47 48
namespace policy
{
struct FolderTable
{
    static const std::string Name;
49
    static const std::string PrimaryKeyColumn;
50
    static int64_t Folder::*const PrimaryKey;
51 52 53
};
}

54 55 56
// 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
57
class Folder : public IFolder, public DatabaseHelpers<Folder, policy::FolderTable>
58 59
{
public:
60
    Folder( MediaLibraryPtr ml, sqlite::Row& row );
61
    Folder(MediaLibraryPtr ml, const std::string& path, int64_t parent , int64_t deviceId , bool isRemovable );
62

63
    static void createTable( sqlite::Connection* connection );
64
    static void createTriggers( sqlite::Connection* connection );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
65
    static std::shared_ptr<Folder> create( MediaLibraryPtr ml, const std::string& mrl, int64_t parentId, Device& device, fs::IDevice& deviceFs );
66
    static void excludeEntryFolder( MediaLibraryPtr ml, int64_t folderId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67
    static bool blacklist( MediaLibraryPtr ml, const std::string& mrl );
68
    static std::vector<std::shared_ptr<Folder>> fetchRootFolders( MediaLibraryPtr ml );
69

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70 71
    static std::shared_ptr<Folder> fromMrl(MediaLibraryPtr ml, const std::string& mrl );
    static std::shared_ptr<Folder> blacklistedFolder(MediaLibraryPtr ml, const std::string& mrl );
72

73
    virtual int64_t id() const override;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74
    virtual const std::string& mrl() const override;
75 76
    const std::string& rawMrl() const;
    void setMrl( std::string mrl );
77
    std::vector<std::shared_ptr<File>> files();
78 79
    std::vector<std::shared_ptr<Folder>> folders();
    std::shared_ptr<Folder> parent();
80
    int64_t deviceId() const;
81
    virtual bool isPresent() const override;
82
    virtual bool isBanned() const override;
83
    bool isRootFolder() const;
84

85 86 87 88 89 90 91
    enum class BannedType
    {
        Yes,    //< Only select banned folders
        No,     //< Only select unbanned folders
        Any,    //< Well... any of the above.
    };

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

94
private:
95
    MediaLibraryPtr m_ml;
96

97
    int64_t m_id;
98
    // This contains the path relative to the device mountpoint (ie. excluding it)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
99
    // or the full path (including mrl scheme) for folders on non removable devices
100
    std::string m_path;
101
    int64_t m_parent;
102
    bool m_isBlacklisted;
103
    int64_t m_deviceId;
104
    bool m_isRemovable;
105

106
    mutable Cache<std::string> m_deviceMountpoint;
107
    mutable Cache<std::shared_ptr<Device>> m_device;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
108 109
    // This contains the full path, including device mountpoint (and mrl scheme,
    // as its part of the mountpoint
110
    mutable std::string m_fullPath;
111

112
    friend struct policy::FolderTable;
113
};
114 115

}