MediaGroup.h 6.23 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 25 26 27 28 29 30 31 32 33 34 35
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2019 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
 *
 * 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

#include "medialibrary/IMediaGroup.h"
#include "Types.h"
#include "database/SqliteTools.h"
#include "database/DatabaseHelpers.h"

namespace medialibrary
{

class MediaGroup : public IMediaGroup, public DatabaseHelpers<MediaGroup>
{
public:
36
    static constexpr auto AutomaticGroupPrefixSize = 6u;
37 38 39 40 41 42
    struct Table
    {
        static const std::string Name;
        static const std::string PrimaryKeyColumn;
        static int64_t MediaGroup::*const PrimaryKey;
    };
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43 44 45 46 47 48 49
    struct FtsTable
    {
        static const std::string Name;
    };
    enum class Triggers : uint8_t
    {
        InsertFts,
50
        DeleteFts,
51 52
        IncrementNbMediaOnGroupChange, // Deprecated in model 26
        DecrementNbMediaOnGroupChange, // Deprecated in model 26
53
        DecrementNbMediaOnDeletion,
54
        DeleteEmptyGroups,
55
        RenameForcedSingleton,
56 57
        UpdateDurationOnMediaChange,
        UpdateDurationOnMediaDeletion,
58
        UpdateNbMedia,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
    };
60 61
    enum class Indexes : uint8_t
    {
62
        ParentId, // Deprecated in model 25
63
        ForcedSingleton,
64 65 66
        Duration,
        CreationDate,
        LastModificationDate,
67
    };
68 69

    MediaGroup( MediaLibraryPtr ml, sqlite::Row& row );
70 71
    MediaGroup( MediaLibraryPtr ml, std::string name, bool userInitiated,
                bool forcedSingleton );
72
    MediaGroup( MediaLibraryPtr ml, std::string name );
73 74 75 76 77 78
    virtual int64_t id() const override;
    virtual const std::string& name() const override;
    virtual uint32_t nbMedia() const override;
    virtual uint32_t nbVideo() const override;
    virtual uint32_t nbAudio() const override;
    virtual uint32_t nbUnknown() const override;
79
    virtual int64_t duration() const override;
80 81
    virtual time_t creationDate() const override;
    virtual time_t lastModificationDate() const override;
82
    virtual bool userInteracted() const override;
83 84 85 86 87 88 89 90 91
    virtual bool add( IMedia& media ) override;
    virtual bool add(int64_t mediaId ) override;
    virtual bool remove( IMedia& media ) override;
    virtual bool remove( int64_t mediaId ) override;
    virtual Query<IMedia> media( IMedia::Type mediaType,
                                 const QueryParameters* params = nullptr ) override;
    virtual Query<IMedia> searchMedia( const std::string& pattern,
                                       IMedia::Type mediaType,
                                       const QueryParameters* params = nullptr ) override;
92
    virtual bool rename( std::string name ) override;
93 94
    bool rename( std::string name, bool userInitiated );

95 96
    bool isForcedSingleton() const;

97 98 99 100 101 102 103 104 105
    /**
     * @brief rename Rename a group
     * @param name The new name
     * @param userInitiated true if this comes from the public API
     *
     * If userInitiated is true, the has_been_renamed column will be set to true
     * Otherwise, this is assumed to be an internal change and the user will not
     * be notified
     */
106
    virtual bool destroy() override;
107

108 109
    static std::shared_ptr<MediaGroup> create( MediaLibraryPtr ml, std::string name,
                                               bool usedInitiated, bool isForcedSingleton );
110 111
    static std::shared_ptr<MediaGroup> create( MediaLibraryPtr ml,
                                               const std::vector<int64_t>& mediaIds );
112
    static Query<IMediaGroup> listAll( MediaLibraryPtr ml, const QueryParameters* params );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
113 114
    static Query<IMediaGroup> search( MediaLibraryPtr ml, const std::string& pattern,
                                      const QueryParameters* params );
115
    static void createTable( sqlite::Connection* connection );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
116
    static void createTriggers( sqlite::Connection* connection );
117
    static void createIndexes( sqlite::Connection* connection );
118
    static std::string schema( const std::string& name, uint32_t dbModel );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
119
    static std::string trigger( Triggers t, uint32_t dbModel );
120
    static std::string triggerName( Triggers t, uint32_t dbModel );
121
    static std::string index( Indexes i, uint32_t dbModel );
122
    static std::string indexName( Indexes i, uint32_t dbModel );
123

124 125
    static bool checkDbModel( MediaLibraryPtr ml );

126
    static bool assignToGroup( MediaLibraryPtr ml, Media& m );
127 128
    static std::string commonPattern( const std::string& groupName,
                                      const std::string& newTitle );
129
    static std::string prefix( const std::string& title );
130

131 132
private:
    static std::string orderBy( const QueryParameters* params );
133 134
    static std::vector<std::shared_ptr<MediaGroup> > fetchMatching( MediaLibraryPtr ml,
                                                  const std::string& prefix );
135

136 137 138
    bool add( IMedia& media, bool force );
    bool add( int64_t mediaId, bool force );

139 140 141 142 143 144 145
private:
    MediaLibraryPtr m_ml;
    int64_t m_id;
    std::string m_name;
    uint32_t m_nbVideo;
    uint32_t m_nbAudio;
    uint32_t m_nbUnknown;
146
    uint32_t m_nbMedia;
147
    int64_t m_duration;
148 149
    time_t m_creationDate;
    time_t m_lastModificationDate;
150
    /* Has the group been interacted with by the user */
151
    bool m_userInteracted;
152 153 154 155 156
    /*
     * Should this group be considered when automatically grouping.
     * This is true for groups we create to contain a single "ungrouped" media
     */
    bool m_forcedSingleton;
157 158 159
};

}