Device.h 2.98 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 49 50
    const std::string& uuid() const;
    bool isRemovable() const;
    bool isPresent() const;
    void setPresent( bool value );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
51 52 53 54 55 56 57
    ///
    /// \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;
58 59
    time_t lastSeen() const;
    void updateLastSeen();
60

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

private:
68
    MediaLibraryPtr m_ml;
69
    // This is a database ID
70
    int64_t m_id;
71 72
    // 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.
73 74 75
    const std::string m_uuid;
    const std::string m_scheme;
    const bool m_isRemovable;
76
    bool m_isPresent;
77
    time_t m_lastSeen;
78

79
    friend struct Device::Table;
80
};
81 82

}