2017-01-15 21:43:46 +01:00
# include "./attachmentinfo.h"
# include <tagparser/abstractcontainer.h>
# include <tagparser/abstractattachment.h>
# include <c++utilities/conversion/conversionexception.h>
# include <c++utilities/conversion/stringconversion.h>
2018-03-06 02:04:35 +01:00
# include <c++utilities/conversion/stringbuilder.h>
2017-01-15 21:43:46 +01:00
# include <cstring>
# include <iostream>
using namespace std ;
using namespace ConversionUtilities ;
2018-03-06 23:10:13 +01:00
using namespace TagParser ;
2017-01-15 21:43:46 +01:00
namespace Cli {
void AttachmentInfo : : parseDenotation ( const char * denotation )
{
if ( ! strncmp ( denotation , " id= " , 3 ) ) {
try {
id = stringToNumber < uint64 , string > ( denotation + 3 ) ;
hasId = true ;
} catch ( const ConversionException & ) {
cerr < < " The specified attachment ID \" " < < ( denotation + 3 ) < < " \" is invalid. " ;
}
} else if ( ! strncmp ( denotation , " path= " , 5 ) ) {
path = denotation + 5 ;
} else if ( ! strncmp ( denotation , " name= " , 5 ) ) {
name = denotation + 5 ;
} else if ( ! strncmp ( denotation , " mime= " , 5 ) ) {
mime = denotation + 5 ;
} else if ( ! strncmp ( denotation , " desc= " , 5 ) ) {
desc = denotation + 5 ;
} else {
cerr < < " The attachment specification \" " < < denotation < < " \" is invalid and will be ignored. " ;
}
}
2018-03-06 23:10:13 +01:00
void AttachmentInfo : : apply ( AbstractContainer * container , TagParser : : Diagnostics & diag )
2017-01-15 21:43:46 +01:00
{
static const string context ( " applying specified attachments " ) ;
AbstractAttachment * attachment = nullptr ;
bool attachmentFound = false ;
switch ( action ) {
case AttachmentAction : : Add :
if ( ! path | | ! name ) {
cerr < < " Argument --update-argument specified but no name/path provided. " < < endl ;
return ;
}
2018-03-06 02:04:35 +01:00
apply ( container - > createAttachment ( ) , diag ) ;
2017-01-15 21:43:46 +01:00
break ;
case AttachmentAction : : Update :
if ( hasId ) {
for ( size_t i = 0 , count = container - > attachmentCount ( ) ; i < count ; + + i ) {
attachment = container - > attachment ( i ) ;
if ( attachment - > id ( ) = = id ) {
2018-03-06 02:04:35 +01:00
apply ( attachment , diag ) ;
2017-01-15 21:43:46 +01:00
attachmentFound = true ;
}
}
if ( ! attachmentFound ) {
2018-03-06 02:04:35 +01:00
diag . emplace_back ( DiagLevel : : Critical , argsToString ( " Attachment with the specified ID \" " , id , " \" does not exist and hence can't be updated. " ) , context ) ;
2017-01-15 21:43:46 +01:00
}
} else if ( name ) {
for ( size_t i = 0 , count = container - > attachmentCount ( ) ; i < count ; + + i ) {
attachment = container - > attachment ( i ) ;
if ( attachment - > name ( ) = = name ) {
2018-03-06 02:04:35 +01:00
apply ( attachment , diag ) ;
2017-01-15 21:43:46 +01:00
attachmentFound = true ;
}
}
if ( ! attachmentFound ) {
2018-03-06 02:04:35 +01:00
diag . emplace_back ( DiagLevel : : Critical , argsToString ( " Attachment with the specified name \" " , name , " \" does not exist and hence can't be updated. " ) , context ) ;
2017-01-15 21:43:46 +01:00
}
} else {
cerr < < " Argument --update-argument specified but no ID/name provided. " < < endl ;
}
break ;
case AttachmentAction : : Remove :
if ( hasId ) {
for ( size_t i = 0 , count = container - > attachmentCount ( ) ; i < count ; + + i ) {
attachment = container - > attachment ( i ) ;
if ( attachment - > id ( ) = = id ) {
attachment - > setIgnored ( true ) ;
attachmentFound = true ;
}
}
if ( ! attachmentFound ) {
2018-03-06 02:04:35 +01:00
diag . emplace_back ( DiagLevel : : Critical , " Attachment with the specified ID \" " + numberToString ( id ) + " \" does not exist and hence can't be removed. " , context ) ;
2017-01-15 21:43:46 +01:00
}
} else if ( name ) {
for ( size_t i = 0 , count = container - > attachmentCount ( ) ; i < count ; + + i ) {
attachment = container - > attachment ( i ) ;
if ( attachment - > name ( ) = = name ) {
attachment - > setIgnored ( true ) ;
attachmentFound = true ;
}
}
if ( ! attachmentFound ) {
2018-03-06 02:04:35 +01:00
diag . emplace_back ( DiagLevel : : Critical , " Attachment with the specified name \" " + string ( name ) + " \" does not exist and hence can't be removed. " , context ) ;
2017-01-15 21:43:46 +01:00
}
} else {
cerr < < " Argument --remove-argument specified but no ID/name provided. " < < endl ;
}
break ;
}
}
2018-03-06 23:10:13 +01:00
void AttachmentInfo : : apply ( AbstractAttachment * attachment , TagParser : : Diagnostics & diag )
2017-01-15 21:43:46 +01:00
{
if ( hasId ) {
attachment - > setId ( id ) ;
}
if ( path ) {
2018-03-06 02:04:35 +01:00
attachment - > setFile ( path , diag ) ;
2017-01-15 21:43:46 +01:00
}
if ( name ) {
attachment - > setName ( name ) ;
}
if ( mime ) {
attachment - > setMimeType ( mime ) ;
}
if ( desc ) {
attachment - > setDescription ( desc ) ;
}
}
void AttachmentInfo : : reset ( )
{
action = AttachmentAction : : Add ;
id = 0 ;
hasId = false ;
path = name = mime = desc = nullptr ;
}
2018-03-06 23:10:13 +01:00
bool AttachmentInfo : : next ( AbstractContainer * container , TagParser : : Diagnostics & diag )
2017-01-15 21:43:46 +01:00
{
if ( ! id & & ! path & & ! name & & ! mime & & ! desc ) {
// skip empty attachment infos
return false ;
}
2018-03-06 02:04:35 +01:00
apply ( container , diag ) ;
2017-01-15 21:43:46 +01:00
reset ( ) ;
return true ;
}
}