File.h 4.99 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
#include "medialibrary/filesystem/IFile.h"
27 28
#include "database/DatabaseHelpers.h"
#include "database/SqliteConnection.h"
29
#include "parser/Parser.h"
30

31 32 33
namespace medialibrary
{

34 35 36
class File;
class Media;

37
class File : public IFile, public DatabaseHelpers<File>
38 39
{
public:
40 41 42 43 44 45
    struct Table
    {
        static const std::string Name;
        static const std::string PrimaryKeyColumn;
        static int64_t File::*const PrimaryKey;
    };
46
    File( MediaLibraryPtr ml, sqlite::Row& row );
47 48
    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 );
49
    virtual int64_t id() const override;
50
    virtual const std::string& mrl() const override;
51 52 53 54 55 56 57
    /**
     * @brief rawMrl returns the raw mrl, ie. the mrl from the mountpoint.
     * This is the same as MRL for files on non removable devices.
     * This is meant to be used when fiddling with the value stored in database
     * during a migration, but shouldn't be used otherwise, as it would be unusable
     */
    const std::string& rawMrl() const;
58
    void setMrl( const std::string& mrl );
59 60
    virtual Type type() const override;
    virtual unsigned int lastModificationDate() const override;
61
    virtual unsigned int size() const override;
62
    virtual bool isExternal() const override;
63
    bool updateFsInfo( uint32_t newLastModificationDate, uint32_t newSize );
64
    bool isRemovable() const;
65

66 67
    std::shared_ptr<Media> media() const;
    bool destroy();
68
    int64_t folderId();
69

70
    static void createTable( sqlite::Connection* dbConnection );
71
    static void createTriggers( sqlite::Connection* dbConnection );
72 73 74 75
    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 );
76 77 78 79

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

80
    /**
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
81
     * @brief fromPath  Attempts to fetch a file using its mrl
82
     * 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
83
     * @param path      The wanted file mrl
84 85
     * @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
86
    static std::shared_ptr<File> fromMrl( MediaLibraryPtr ml, const std::string& mrl );
87 88 89 90 91 92 93 94
    /**
     * @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 );
95

96 97 98 99 100 101
    /**
     * @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
102
    static std::shared_ptr<File> fromExternalMrl( MediaLibraryPtr ml, const std::string& mrl );
103

104
private:
105 106
    MediaLibraryPtr m_ml;

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

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

124
    friend File::Table;
125
};
126 127

}