22
#include "core/Group.h"
23
#include "core/PasswordGenerator.h"
25
#include <QCommandLineParser>
27
const QCommandLineOption Add::UsernameOption = QCommandLineOption(QStringList() << "u"
29
QObject::tr("Username for the entry."),
30
QObject::tr("username"));
32
const QCommandLineOption Add::UrlOption =
33
QCommandLineOption(QStringList() << "url", QObject::tr("URL for the entry."), QObject::tr("URL"));
35
const QCommandLineOption Add::NotesOption =
36
QCommandLineOption(QStringList() << "notes", QObject::tr("Notes for the entry."), QObject::tr("Notes"));
38
const QCommandLineOption Add::PasswordPromptOption =
39
QCommandLineOption(QStringList() << "p"
41
QObject::tr("Prompt for the entry's password."));
43
const QCommandLineOption Add::GenerateOption = QCommandLineOption(QStringList() << "g"
45
QObject::tr("Generate a password for the entry."));
49
name = QString("add");
50
description = QObject::tr("Add a new entry to a database.");
51
options.append(Add::UsernameOption);
52
options.append(Add::UrlOption);
53
options.append(Add::NotesOption);
54
options.append(Add::PasswordPromptOption);
55
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry to add."), QString("")});
58
options.append(Add::GenerateOption);
59
options.append(Generate::PasswordLengthOption);
60
options.append(Generate::LowerCaseOption);
61
options.append(Generate::UpperCaseOption);
62
options.append(Generate::NumbersOption);
63
options.append(Generate::SpecialCharsOption);
64
options.append(Generate::ExtendedAsciiOption);
65
options.append(Generate::ExcludeCharsOption);
66
options.append(Generate::ExcludeSimilarCharsOption);
67
options.append(Generate::IncludeEveryGroupOption);
68
options.append(Generate::CustomCharacterSetOption);
71
int Add::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
73
auto& out = Utils::STDOUT;
74
auto& err = Utils::STDERR;
76
const QStringList args = parser->positionalArguments();
77
auto& entryPath = args.at(1);
80
if (parser->isSet(Add::GenerateOption) && parser->isSet(Add::PasswordPromptOption)) {
81
err << QObject::tr("Cannot generate a password and prompt at the same time.") << endl;
87
QSharedPointer<PasswordGenerator> passwordGenerator;
88
if (parser->isSet(Add::GenerateOption)) {
89
passwordGenerator = Generate::createGenerator(parser);
90
if (passwordGenerator.isNull()) {
95
Entry* entry = database->rootGroup()->addEntryWithPath(entryPath);
97
err << QObject::tr("Could not create entry with path %1.").arg(entryPath) << endl;
101
if (!parser->value(Add::UsernameOption).isEmpty()) {
102
entry->setUsername(parser->value(Add::UsernameOption));
105
if (!parser->value(Add::UrlOption).isEmpty()) {
106
entry->setUrl(parser->value(Add::UrlOption));
109
if (!parser->value(Add::NotesOption).isEmpty()) {
110
entry->setNotes(parser->value(Add::NotesOption).replace("\\n", "\n"));
113
if (parser->isSet(Add::PasswordPromptOption)) {
114
if (!parser->isSet(Command::QuietOption)) {
115
out << QObject::tr("Enter password for new entry: ") << flush;
117
QString password = Utils::getPassword(parser->isSet(Command::QuietOption));
118
entry->setPassword(password);
119
} else if (parser->isSet(Add::GenerateOption)) {
120
QString password = passwordGenerator->generatePassword();
121
entry->setPassword(password);
124
QString errorMessage;
125
if (!database->save(Database::Atomic, {}, &errorMessage)) {
126
err << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;
130
if (!parser->isSet(Command::QuietOption)) {
131
out << QObject::tr("Successfully added entry %1.").arg(entry->title()) << endl;