keepassxc

Форк
0
/
RemoveGroup.cpp 
76 строк · 2.6 Кб
1
/*
2
 *  Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "RemoveGroup.h"
19

20
#include "Utils.h"
21
#include "core/Group.h"
22
#include "core/Metadata.h"
23

24
#include <QCommandLineParser>
25

26
RemoveGroup::RemoveGroup()
27
{
28
    name = QString("rmdir");
29
    description = QString("Removes a group from a database.");
30
    positionalArguments.append({QString("group"), QObject::tr("Path of the group to remove."), QString("")});
31
}
32

33
RemoveGroup::~RemoveGroup() = default;
34

35
int RemoveGroup::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
36
{
37
    auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
38
    auto& err = Utils::STDERR;
39

40
    QString groupPath = parser->positionalArguments().at(1);
41

42
    // Recursive option means were looking for a group to remove.
43
    QPointer<Group> group = database->rootGroup()->findGroupByPath(groupPath);
44
    if (!group) {
45
        err << QObject::tr("Group %1 not found.").arg(groupPath) << endl;
46
        return EXIT_FAILURE;
47
    }
48

49
    if (group == database->rootGroup()) {
50
        err << QObject::tr("Cannot remove root group from database.") << endl;
51
        return EXIT_FAILURE;
52
    }
53

54
    bool recycled = true;
55
    auto* recycleBin = database->metadata()->recycleBin();
56
    if (!database->metadata()->recycleBinEnabled() || (recycleBin && recycleBin->findGroupByUuid(group->uuid()))) {
57
        delete group;
58
        recycled = false;
59
    } else {
60
        database->recycleGroup(group);
61
    };
62

63
    QString errorMessage;
64
    if (!database->save(Database::Atomic, {}, &errorMessage)) {
65
        err << QObject::tr("Unable to save database to file: %1").arg(errorMessage) << endl;
66
        return EXIT_FAILURE;
67
    }
68

69
    if (recycled) {
70
        out << QObject::tr("Successfully recycled group %1.").arg(groupPath) << endl;
71
    } else {
72
        out << QObject::tr("Successfully deleted group %1.").arg(groupPath) << endl;
73
    }
74

75
    return EXIT_SUCCESS;
76
}
77

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.