Folder.h 3.75 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
class File;
32
class Folder;
33
class Device;
34

35 36 37 38 39
namespace fs
{
    class IDirectory;
}

40 41 42 43 44
namespace policy
{
struct FolderTable
{
    static const std::string Name;
45
    static const std::string PrimaryKeyColumn;
46 47
    static unsigned int Folder::*const PrimaryKey;
};
48

49 50
}

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

    static bool createTable( DBConnection connection );
61 62 63
    static std::shared_ptr<Folder> create( MediaLibraryPtr ml, const std::string& path, unsigned int parentId, Device& device, fs::IDevice& deviceFs );
    static bool blacklist( MediaLibraryPtr ml, const std::string& fullPath );
    static std::vector<std::shared_ptr<Folder>> fetchAll(MediaLibraryPtr ml, unsigned int parentFolderId );
64 65 66 67 68 69
    ///
    /// \brief setFileSystemFactory Sets a file system factory to be used when building IDevices
    /// This is assumed to be called once, before any discovery/reloading process is launched.
    /// \param fsFactory The factory to be used
    ///
    static void setFileSystemFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
70

71 72
    static std::shared_ptr<Folder> fromPath(MediaLibraryPtr ml, const std::string& fullPath );
    static std::shared_ptr<Folder> blacklistedFolder(MediaLibraryPtr ml, const std::string& fullPath );
73

74 75
    unsigned int id() const;
    const std::string& path() const;
76
    std::vector<std::shared_ptr<File>> files();
77 78
    std::vector<std::shared_ptr<Folder>> folders();
    std::shared_ptr<Folder> parent();
79
    unsigned int deviceId() const;
80
    bool isPresent() const;
81

82 83
private:
    static std::shared_ptr<factory::IFileSystem> FsFactory;
84
    static std::shared_ptr<Folder> fromPath( MediaLibraryPtr ml, const std::string& fullPath, bool includeBlacklisted );
85

86
private:
87
    MediaLibraryPtr m_ml;
88 89

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

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

102
    friend struct policy::FolderTable;
103
};