Device.h 3.27 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
/*****************************************************************************
 * 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

#include "Types.h"
#include "database/DatabaseHelpers.h"
27
#include <chrono>
28

29 30 31
namespace medialibrary
{

32
class Device;
33

34
class Device : public DatabaseHelpers<Device>
35 36
{
public:
37 38 39 40 41 42
    struct Table
    {
        static const std::string Name;
        static const std::string PrimaryKeyColumn;
        static int64_t Device::*const PrimaryKey;
    };
43 44
    Device( MediaLibraryPtr ml, const std::string& uuid, const std::string& scheme,
            bool isRemovable, time_t insertionDate );
45
    Device( MediaLibraryPtr ml, sqlite::Row& row );
46
    int64_t id() const;
47 48
    const std::string& uuid() const;
    bool isRemovable() const;
49 50 51 52 53
    ///
    /// \brief forceNonRemovable This is used only to fix an invalid DB state
    /// \return
    ///
    bool forceNonRemovable();
54 55
    bool isPresent() const;
    void setPresent( bool value );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
56 57 58 59 60 61 62
    ///
    /// \brief scheme returns the scheme that was used for this device when it was
    /// originally created. This allows to use the apropriate IFileSystemFactory to find the
    /// recreate a IDevice based on its id or UUID
    /// \return
    ///
    const std::string& scheme() const;
63 64
    time_t lastSeen() const;
    void updateLastSeen();
65

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
66
    static std::shared_ptr<Device> create( MediaLibraryPtr ml, const std::string& uuid, const std::string& scheme, bool isRemovable );
67
    static void createTable( sqlite::Connection* connection );
68
    static std::shared_ptr<Device> fromUuid( MediaLibraryPtr ml, const std::string& uuid );
69
    static void removeOldDevices( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime );
70
    static std::vector<std::shared_ptr<Device>> fetchByScheme( MediaLibraryPtr ml, const std::string& scheme );
71 72

private:
73
    MediaLibraryPtr m_ml;
74
    // This is a database ID
75
    int64_t m_id;
76 77
    // This is a unique ID on the system side, in the /dev/disk/by-uuid sense.
    // It can be a name or what not, depending on the OS.
78 79
    const std::string m_uuid;
    const std::string m_scheme;
80 81 82
    // Can't be const anymore, but should be if we ever get to remove the
    // removable->non removable device fixup (introduced after vlc-android 3.1.0 rc3)
    bool m_isRemovable;
83
    bool m_isPresent;
84
    time_t m_lastSeen;
85

86
    friend struct Device::Table;
87
};
88 89

}