2017-01-15 21:43:46 +01:00
|
|
|
#include "./attachmentinfo.h"
|
|
|
|
|
|
|
|
#include <tagparser/abstractattachment.h>
|
2018-03-07 01:18:01 +01:00
|
|
|
#include <tagparser/abstractcontainer.h>
|
2021-02-04 23:22:43 +01:00
|
|
|
#include <tagparser/progressfeedback.h>
|
2017-01-15 21:43:46 +01:00
|
|
|
|
|
|
|
#include <c++utilities/conversion/conversionexception.h>
|
2018-03-06 02:04:35 +01:00
|
|
|
#include <c++utilities/conversion/stringbuilder.h>
|
2018-03-07 01:18:01 +01:00
|
|
|
#include <c++utilities/conversion/stringconversion.h>
|
2017-01-15 21:43:46 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-06-10 22:49:46 +02:00
|
|
|
using namespace CppUtilities;
|
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)
|
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!strncmp(denotation, "id=", 3)) {
|
2017-01-15 21:43:46 +01:00
|
|
|
try {
|
2019-03-13 19:07:51 +01:00
|
|
|
id = stringToNumber<std::uint64_t, string>(denotation + 3);
|
2017-01-15 21:43:46 +01:00
|
|
|
hasId = true;
|
2018-03-07 01:18:01 +01:00
|
|
|
} catch (const ConversionException &) {
|
2017-01-15 21:43:46 +01:00
|
|
|
cerr << "The specified attachment ID \"" << (denotation + 3) << "\" is invalid.";
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (!strncmp(denotation, "path=", 5)) {
|
2017-01-15 21:43:46 +01:00
|
|
|
path = denotation + 5;
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (!strncmp(denotation, "name=", 5)) {
|
2017-01-15 21:43:46 +01:00
|
|
|
name = denotation + 5;
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (!strncmp(denotation, "mime=", 5)) {
|
2017-01-15 21:43:46 +01:00
|
|
|
mime = denotation + 5;
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (!strncmp(denotation, "desc=", 5)) {
|
2017-01-15 21:43:46 +01:00
|
|
|
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;
|
2018-03-07 01:18:01 +01:00
|
|
|
switch (action) {
|
2017-01-15 21:43:46 +01:00
|
|
|
case AttachmentAction::Add:
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!path || !name) {
|
2017-01-15 21:43:46 +01:00
|
|
|
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:
|
2018-03-07 01:18:01 +01:00
|
|
|
if (hasId) {
|
|
|
|
for (size_t i = 0, count = container->attachmentCount(); i < count; ++i) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment = container->attachment(i);
|
2018-03-07 01:18:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!attachmentFound) {
|
|
|
|
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
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (name) {
|
|
|
|
for (size_t i = 0, count = container->attachmentCount(); i < count; ++i) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment = container->attachment(i);
|
2018-03-07 01:18:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!attachmentFound) {
|
|
|
|
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:
|
2018-03-07 01:18:01 +01:00
|
|
|
if (hasId) {
|
|
|
|
for (size_t i = 0, count = container->attachmentCount(); i < count; ++i) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment = container->attachment(i);
|
2018-03-07 01:18:01 +01:00
|
|
|
if (attachment->id() == id) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment->setIgnored(true);
|
|
|
|
attachmentFound = true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!attachmentFound) {
|
|
|
|
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
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
} else if (name) {
|
|
|
|
for (size_t i = 0, count = container->attachmentCount(); i < count; ++i) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment = container->attachment(i);
|
2018-03-07 01:18:01 +01:00
|
|
|
if (attachment->name() == name) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment->setIgnored(true);
|
|
|
|
attachmentFound = true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!attachmentFound) {
|
|
|
|
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
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
if (hasId) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment->setId(id);
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (path) {
|
2021-02-04 23:22:43 +01:00
|
|
|
AbortableProgressFeedback progress;
|
|
|
|
attachment->setFile(path, diag, progress);
|
2017-01-15 21:43:46 +01:00
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (name) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment->setName(name);
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (mime) {
|
2017-01-15 21:43:46 +01:00
|
|
|
attachment->setMimeType(mime);
|
|
|
|
}
|
2018-03-07 01:18:01 +01:00
|
|
|
if (desc) {
|
2017-01-15 21:43:46 +01:00
|
|
|
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
|
|
|
{
|
2018-03-07 01:18:01 +01:00
|
|
|
if (!id && !path && !name && !mime && !desc) {
|
2018-11-15 21:33:08 +01:00
|
|
|
// skip empty attachment info
|
2017-01-15 21:43:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-06 02:04:35 +01:00
|
|
|
apply(container, diag);
|
2017-01-15 21:43:46 +01:00
|
|
|
reset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:18:01 +01:00
|
|
|
} // namespace Cli
|