Commit 074cf8f6 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Folder: Store the name in database

So we can use it to search & sort
ref #42
parent 457b501f
......@@ -41,6 +41,7 @@ public:
* @return The folder's mrl
*/
virtual const std::string& mrl() const = 0;
virtual const std::string& name() const = 0;
virtual bool isPresent() const = 0;
/**
* @brief isBanned Will return true if the folder was explicitely banned
......
......@@ -34,6 +34,7 @@
#include "medialibrary/filesystem/IDevice.h"
#include "medialibrary/filesystem/IFileSystemFactory.h"
#include "utils/Filename.h"
#include "utils/Url.h"
#include <unordered_map>
......@@ -48,18 +49,21 @@ Folder::Folder( MediaLibraryPtr ml, sqlite::Row& row )
: m_ml( ml )
, m_id( row.load<decltype(m_id)>( 0 ) )
, m_path( row.load<decltype(m_path)>( 1 ) )
, m_parent( row.load<decltype(m_parent)>( 2 ) )
, m_isBanned( row.load<decltype(m_isBanned)>( 3 ) )
, m_deviceId( row.load<decltype(m_deviceId)>( 4 ) )
, m_isRemovable( row.load<decltype(m_isRemovable)>( 5 ) )
, m_nbMedia( row.load<decltype(m_nbMedia)>( 6 ) )
, m_name( row.load<decltype(m_name)>( 2 ) )
, m_parent( row.load<decltype(m_parent)>( 3 ) )
, m_isBanned( row.load<decltype(m_isBanned)>( 4 ) )
, m_deviceId( row.load<decltype(m_deviceId)>( 5 ) )
, m_isRemovable( row.load<decltype(m_isRemovable)>( 6 ) )
, m_nbMedia( row.load<decltype(m_nbMedia)>( 7 ) )
{
}
Folder::Folder(MediaLibraryPtr ml, const std::string& path, int64_t parent, int64_t deviceId, bool isRemovable )
Folder::Folder(MediaLibraryPtr ml, const std::string& path,
int64_t parent, int64_t deviceId, bool isRemovable )
: m_ml( ml )
, m_id( 0 )
, m_path( path )
, m_name( utils::url::decode( utils::file::directoryName( path ) ) )
, m_parent( parent )
, m_isBanned( false )
, m_deviceId( deviceId )
......@@ -117,8 +121,8 @@ std::shared_ptr<Folder> Folder::create( MediaLibraryPtr ml, const std::string& m
path = mrl;
auto self = std::make_shared<Folder>( ml, path, parentId, device.id(), device.isRemovable() );
static const std::string req = "INSERT INTO " + Folder::Table::Name +
"(path, parent_id, device_id, is_removable) VALUES(?, ?, ?, ?)";
if ( insert( ml, self, req, path, sqlite::ForeignKey( parentId ), device.id(), device.isRemovable() ) == false )
"(path, name, parent_id, device_id, is_removable) VALUES(?, ?, ?, ?, ?)";
if ( insert( ml, self, req, path, self->m_name, sqlite::ForeignKey( parentId ), device.id(), device.isRemovable() ) == false )
return nullptr;
if ( device.isRemovable() == true )
{
......@@ -289,6 +293,21 @@ const std::string& Folder::mrl() const
return m_fullPath;
}
const std::string&Folder::name() const
{
return m_name;
}
void Folder::setName( std::string name )
{
assert( m_name.empty() == true );
static const std::string req = "UPDATE " + Table::Name +
" SET name = ? WHERE id_folder = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, name, m_id ) == false )
return;
m_name = std::move( name );
}
const std::string& Folder::rawMrl() const
{
return m_path;
......
......@@ -67,6 +67,9 @@ public:
virtual int64_t id() const override;
virtual const std::string& mrl() const override;
virtual const std::string& name() const override;
// Use for 13 -> 14 migration
void setName( std::string name );
const std::string& rawMrl() const;
void setMrl( std::string mrl );
std::vector<std::shared_ptr<File>> files();
......@@ -94,6 +97,7 @@ private:
// This contains the path relative to the device mountpoint (ie. excluding it)
// or the full path (including mrl scheme) for folders on non removable devices
std::string m_path;
std::string m_name;
const int64_t m_parent;
const bool m_isBanned;
const int64_t m_deviceId;
......
......@@ -1190,6 +1190,11 @@ void MediaLibrary::migrateModel13to14( uint32_t originalPreviousVersion )
LOG_INFO( "Converting ", m->fileName(), " to ", newFileName );
m->setFileName( std::move( newFileName ) );
}
auto folders = Folder::fetchAll<Folder>( this );
for ( const auto& f : folders )
{
f->setName( utils::file::directoryName( f->rawMrl() ) );
}
t->commit();
}
......
......@@ -2,6 +2,7 @@
"("
"id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
"path TEXT,"
"name TEXT,"
"parent_id UNSIGNED INTEGER,"
"is_banned BOOLEAN NOT NULL DEFAULT 0,"
"device_id UNSIGNED INTEGER,"
......
......@@ -31,6 +31,7 @@
#include "Folder.h"
#include "medialibrary/IMediaLibrary.h"
#include "utils/Filename.h"
#include "utils/Url.h"
#include "mocks/FileSystem.h"
#include "mocks/DiscovererCbMock.h"
......@@ -473,3 +474,21 @@ TEST_F( Folders, NbMedia )
ASSERT_EQ( 2u, root->nbMedia() );
ASSERT_EQ( 0u, subFolder->nbMedia() );
}
TEST_F( FoldersNoDiscover, Name )
{
auto newFolder = mock::FileSystemFactory::SubFolder + "folder%20with%20spaces/";
fsMock->addFolder( newFolder );
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto root = ml->folder( 1 );
auto subFolder = ml->folder( 2 );
auto spacesFolder = ml->folder( 3 );
ASSERT_EQ( "a", root->name() );
ASSERT_EQ( "folder", subFolder->name() );
ASSERT_EQ( "folder with spaces", spacesFolder->name() );
ASSERT_EQ( newFolder, spacesFolder->mrl() );
}
......@@ -219,6 +219,7 @@ TEST_F( DbModel, Upgrade13to14 )
auto folder = ml->folder( 1 );
ASSERT_NE( nullptr, folder );
ASSERT_EQ( 2u, folder->nbMedia() );
ASSERT_EQ( "folder", folder->name() );
CheckNbTriggers( 32 );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment