File.h 4.91 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 29
#include "database/DatabaseHelpers.h"
#include "database/SqliteConnection.h"
#include "utils/Cache.h"
30
#include "parser/Parser.h"
31

32 33 34
namespace medialibrary
{

35 36 37
class File;
class Media;

38
class File : public IFile, public DatabaseHelpers<File>
39 40
{
public:
41 42 43 44 45 46
    struct Table
    {
        static const std::string Name;
        static const std::string PrimaryKeyColumn;
        static int64_t File::*const PrimaryKey;
    };
47
    File( MediaLibraryPtr ml, sqlite::Row& row );
48 49
    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 );
50
    virtual int64_t id() const override;
51
    virtual const std::string& mrl() const override;
52 53 54 55 56 57 58
    /**
     * @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;
59
    void setMrl( const std::string& mrl );
60 61
    virtual Type type() const override;
    virtual unsigned int lastModificationDate() const override;
62
    virtual unsigned int size() const override;
63
    virtual bool isExternal() const override;
64

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

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

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

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

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

103
private:
104 105
    MediaLibraryPtr m_ml;

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

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

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

}