Commit 4413beec authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Implement files label removal

parent f78a615c
......@@ -15,12 +15,14 @@ class IFile
public:
virtual ~IFile() {}
virtual unsigned int id() const = 0;
virtual IAlbumTrack* albumTrack() = 0;
virtual unsigned int duration() = 0;
virtual IShowEpisode* showEpisode() = 0;
virtual int playCount() = 0;
virtual const std::string& mrl() = 0;
virtual ILabel* addLabel( const std::string& label ) = 0;
virtual bool removeLabel( const ILabel* label ) = 0;
virtual std::vector<ILabel*> labels() = 0;
};
......
......@@ -10,8 +10,11 @@ class ILabel
public:
virtual ~ILabel() {}
virtual unsigned int id() const = 0;
virtual const std::string& name() = 0;
virtual std::vector<IFile*> files() = 0;
virtual bool link( IFile* file ) = 0;
virtual bool unlink( IFile* file ) const = 0;
};
#endif // ILABEL_H
......@@ -123,6 +123,25 @@ ILabel* File::addLabel(const std::string& label)
return l;
}
bool File::removeLabel( const ILabel* label )
{
if ( m_labels != NULL )
{
std::vector<ILabel*>::iterator it = m_labels->begin();
std::vector<ILabel*>::iterator ite = m_labels->end();
while ( it != ite )
{
if ( (*it)->id() == label->id() )
break;
++it;
}
if ( it == ite )
return false;
m_labels->erase( it );
}
return label->unlink( this );
}
unsigned int File::id() const
{
return m_id;
......
......@@ -26,6 +26,7 @@ class File : public IFile
bool insert(sqlite3* dbConnection);
virtual unsigned int id() const;
virtual IAlbumTrack* albumTrack();
virtual unsigned int duration();
virtual IShowEpisode* showEpisode();
......@@ -33,8 +34,8 @@ class File : public IFile
virtual int playCount();
virtual const std::string& mrl();
virtual ILabel* addLabel( const std::string &label );
virtual bool removeLabel( const ILabel* label );
unsigned int id() const;
static bool createTable( sqlite3* connection );
private:
......
......@@ -22,6 +22,11 @@ Label::Label( const std::string& name )
{
}
unsigned int Label::id() const
{
return m_id;
}
const std::string& Label::name()
{
......@@ -77,7 +82,7 @@ bool Label::createTable(sqlite3* dbConnection)
return SqliteTools::createTable( dbConnection, req );
}
bool Label::link(File* file)
bool Label::link( IFile* file )
{
if ( m_dbConnection == NULL || m_id == 0 )
{
......@@ -97,3 +102,24 @@ bool Label::link(File* file)
sqlite3_finalize( stmt );
return res;
}
bool Label::unlink( IFile* file ) const
{
if ( m_dbConnection == NULL || m_id == 0 )
{
std::cerr << "Can't unlink a label not inserted in database" << std::endl;
return false;
}
const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_file = ?";
sqlite3_stmt* stmt;
if ( sqlite3_prepare_v2( m_dbConnection, req, -1, &stmt, NULL ) != SQLITE_OK )
{
std::cerr << "Failed to remove record: " << sqlite3_errmsg( m_dbConnection ) << std::endl;
return false;
}
sqlite3_bind_int( stmt, 1, m_id );
sqlite3_bind_int( stmt, 2, file->id() );
bool res = sqlite3_step( stmt ) == SQLITE_DONE;
sqlite3_finalize( stmt );
return res;
}
......@@ -14,12 +14,14 @@ class Label : public ILabel
Label( const std::string& name );
public:
virtual unsigned int id() const;
virtual const std::string& name();
virtual std::vector<IFile*> files();
bool insert( sqlite3* dbConnection );
static bool createTable( sqlite3* dbConnection );
bool link( File* file );
bool link( IFile* file );
bool unlink( IFile* file ) const;
private:
sqlite3* m_dbConnection;
unsigned int m_id;
......
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