Folder.h 3.51 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 "database/DatabaseHelpers.h"
26
#include "factory/IFileSystem.h"
27
#include "utils/Cache.h"
28 29 30

#include <sqlite3.h>

31 32 33
namespace medialibrary
{

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

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

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

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

    static bool createTable( DBConnection connection );
63
    static std::shared_ptr<Folder> create( MediaLibraryPtr ml, const std::string& path, int64_t parentId, Device& device, fs::IDevice& deviceFs );
64
    static bool blacklist( MediaLibraryPtr ml, const std::string& fullPath );
65
    static std::vector<std::shared_ptr<Folder>> fetchAll( MediaLibraryPtr ml, int64_t parentFolderId );
66

67 68
    static std::shared_ptr<Folder> fromPath(MediaLibraryPtr ml, const std::string& fullPath );
    static std::shared_ptr<Folder> blacklistedFolder(MediaLibraryPtr ml, const std::string& fullPath );
69

70
    int64_t id() const;
71
    const std::string& path() const;
72
    std::vector<std::shared_ptr<File>> files();
73 74
    std::vector<std::shared_ptr<Folder>> folders();
    std::shared_ptr<Folder> parent();
75
    int64_t deviceId() const;
76
    bool isPresent() const;
77

78
private:
79 80 81 82 83 84 85 86
    enum class BannedType
    {
        Yes,    //< Only select banned folders
        No,     //< Only select unbanned folders
        Any,    //< Well... any of the above.
    };

    static std::shared_ptr<Folder> fromPath( MediaLibraryPtr ml, const std::string& fullPath, BannedType bannedType );
87

88
private:
89
    MediaLibraryPtr m_ml;
90

91
    int64_t m_id;
92
    // This contains the path relative to the device mountpoint (ie. excluding it)
93
    std::string m_path;
94
    int64_t m_parent;
95
    bool m_isBlacklisted;
96
    int64_t m_deviceId;
97
    bool m_isPresent;
98
    bool m_isRemovable;
99

100
    mutable Cache<std::string> m_deviceMountpoint;
101
    // This contains the full path, including device mountpoint.
102
    mutable std::string m_fullPath;
103

104
    friend struct policy::FolderTable;
105
};
106 107

}