File.h 4.53 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 23 24
/*****************************************************************************
 * 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.
 *****************************************************************************/

#pragma once

25
#include "medialibrary/IFile.h"
26 27 28 29
#include "database/DatabaseHelpers.h"
#include "database/SqliteConnection.h"
#include "filesystem/IFile.h"
#include "utils/Cache.h"
30
#include "parser/Parser.h"
31

32 33 34
namespace medialibrary
{

35 36 37 38 39 40 41 42 43
class File;
class Media;

namespace policy
{
struct FileTable
{
    static const std::string Name;
    static const std::string PrimaryKeyColumn;
44
    static int64_t File::*const PrimaryKey;
45 46 47
};
}

48
class File : public IFile, public DatabaseHelpers<File, policy::FileTable>
49 50
{
public:
51

52
    File( MediaLibraryPtr ml, sqlite::Row& row );
53 54
    File( MediaLibraryPtr ml, int64_t mediaId, int64_t playlistId, Type type, const fs::IFile& file, int64_t folderId, bool isRemovable );
    File( MediaLibraryPtr ml, int64_t mediaId, int64_t playlistId, Type type, const std::string& mrl );
55
    virtual int64_t id() const override;
56 57 58
    virtual const std::string& mrl() const override;
    virtual Type type() const override;
    virtual unsigned int lastModificationDate() const override;
59
    virtual unsigned int size() const override;
60
    virtual bool isExternal() const override;
61

62 63
    std::shared_ptr<Media> media() const;
    bool destroy();
64
    int64_t folderId();
65

66
    static void createTable( sqlite::Connection* dbConnection );
67
    static void createTriggers( sqlite::Connection* dbConnection );
68 69 70 71
    static std::shared_ptr<File> createFromMedia( MediaLibraryPtr ml, int64_t mediaId, Type type,
                                                  const fs::IFile& file, int64_t folderId, bool isRemovable );
    static std::shared_ptr<File> createFromMedia( MediaLibraryPtr ml, int64_t mediaId, Type type,
                                                  const std::string& mrl );
72 73 74 75

    static std::shared_ptr<File> createFromPlaylist( MediaLibraryPtr ml, int64_t playlistId, const fs::IFile& file,
                                                     int64_t folderId, bool isRemovable );

76
    /**
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
77
     * @brief fromPath  Attempts to fetch a file using its mrl
78
     * This will only work if the file was stored on a non removable device
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
79
     * @param path      The wanted file mrl
80 81
     * @return          A pointer to the wanted file, or nullptr if it wasn't found
     */
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
82
    static std::shared_ptr<File> fromMrl( MediaLibraryPtr ml, const std::string& mrl );
83 84 85 86 87 88 89 90
    /**
     * @brief fromFileName  Attemps to fetch a file based on its filename and folder id
     * @param ml
     * @param fileName
     * @param folderId
     * @return
     */
    static std::shared_ptr<File> fromFileName( MediaLibraryPtr ml, const std::string& fileName, int64_t folderId );
91

92 93 94 95 96 97
    /**
     * @brief fromMrl Attempts to find an external stream (ie. it was added with MediaLibrary::addMedia,
     * and not discovered through any IDiscoverer)
     * This implies the folder_id is null
     * @return
     */
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
98
    static std::shared_ptr<File> fromExternalMrl( MediaLibraryPtr ml, const std::string& mrl );
99

100
private:
101 102
    MediaLibraryPtr m_ml;

103 104
    int64_t m_id;
    int64_t m_mediaId;
105
    int64_t m_playlistId;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
106 107
    // Contains the path relative to the containing folder for files contained in a removable folder
    // or the full file MRL for non removable ones
108 109
    std::string m_mrl;
    Type m_type;
110
    std::time_t m_lastModificationDate;
111
    unsigned int m_size;
112
    int64_t m_folderId;
113 114
    bool m_isPresent;
    bool m_isRemovable;
115
    bool m_isExternal;
116

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
117
    // Contains the full path as a MRL
118
    mutable Cache<std::string> m_fullPath;
119
    mutable Cache<std::weak_ptr<Media>> m_media;
120

121
    friend policy::FileTable;
122
};
123 124

}