-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
louib
authored and
louib
committed
Sep 22, 2019
1 parent
77fcde8
commit 68674da
Showing
12 changed files
with
582 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <cstdlib> | ||
#include <stdio.h> | ||
|
||
#include "AddGroup.h" | ||
|
||
#include "cli/TextStream.h" | ||
#include "cli/Utils.h" | ||
#include "core/Database.h" | ||
#include "core/Entry.h" | ||
#include "core/Group.h" | ||
|
||
AddGroup::AddGroup() | ||
{ | ||
name = QString("mkdir"); | ||
description = QObject::tr("Adds a new group to a database."); | ||
positionalArguments.append({QString("group"), QObject::tr("Path of the group to add."), QString("")}); | ||
} | ||
|
||
AddGroup::~AddGroup() | ||
{ | ||
} | ||
|
||
int AddGroup::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser) | ||
{ | ||
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly); | ||
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly); | ||
|
||
const QStringList args = parser->positionalArguments(); | ||
const QString& databasePath = args.at(0); | ||
const QString& groupPath = args.at(1); | ||
|
||
QStringList pathParts = groupPath.split("/"); | ||
QString groupName = pathParts.takeLast(); | ||
QString parentGroupPath = pathParts.join("/"); | ||
|
||
Group* group = database->rootGroup()->findGroupByPath(groupPath); | ||
if (group) { | ||
errorTextStream << QObject::tr("Group %1 already exists!").arg(groupPath) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
Group* parentGroup = database->rootGroup()->findGroupByPath(parentGroupPath); | ||
if (!parentGroup) { | ||
errorTextStream << QObject::tr("Group %1 not found.").arg(parentGroupPath) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
Group* newGroup = new Group(); | ||
newGroup->setUuid(QUuid::createUuid()); | ||
newGroup->setName(groupName); | ||
newGroup->setParent(parentGroup); | ||
|
||
QString errorMessage; | ||
if (!database->save(databasePath, &errorMessage, true, false)) { | ||
errorTextStream << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (!parser->isSet(Command::QuietOption)) { | ||
outputTextStream << QObject::tr("Successfully added group %1.").arg(groupName) << endl; | ||
} | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef KEEPASSXC_ADDGROUP_H | ||
#define KEEPASSXC_ADDGROUP_H | ||
|
||
#include "DatabaseCommand.h" | ||
|
||
class AddGroup : public DatabaseCommand | ||
{ | ||
public: | ||
AddGroup(); | ||
~AddGroup(); | ||
|
||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser); | ||
}; | ||
|
||
#endif // KEEPASSXC_ADDGROUP_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <cstdlib> | ||
#include <stdio.h> | ||
|
||
#include "Move.h" | ||
|
||
#include "cli/TextStream.h" | ||
#include "cli/Utils.h" | ||
#include "core/Database.h" | ||
#include "core/Entry.h" | ||
#include "core/Group.h" | ||
|
||
Move::Move() | ||
{ | ||
name = QString("mv"); | ||
description = QObject::tr("Moves an entry to a new group."); | ||
positionalArguments.append({QString("entry"), QObject::tr("Path of the entry to move."), QString("")}); | ||
positionalArguments.append({QString("group"), QObject::tr("Path of the destination group."), QString("")}); | ||
} | ||
|
||
Move::~Move() | ||
{ | ||
} | ||
|
||
int Move::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser) | ||
{ | ||
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly); | ||
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly); | ||
|
||
const QStringList args = parser->positionalArguments(); | ||
const QString& databasePath = args.at(0); | ||
const QString& entryPath = args.at(1); | ||
const QString& destinationPath = args.at(2); | ||
|
||
Entry* entry = database->rootGroup()->findEntryByPath(entryPath); | ||
if (!entry) { | ||
errorTextStream << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
Group* destinationGroup = database->rootGroup()->findGroupByPath(destinationPath); | ||
if (!destinationGroup) { | ||
errorTextStream << QObject::tr("Could not find group with path %1.").arg(destinationPath) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (destinationGroup == entry->parent()) { | ||
errorTextStream << QObject::tr("Entry is already in group %1.").arg(destinationPath) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
entry->beginUpdate(); | ||
entry->setGroup(destinationGroup); | ||
entry->endUpdate(); | ||
|
||
QString errorMessage; | ||
if (!database->save(databasePath, &errorMessage, true, false)) { | ||
errorTextStream << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
outputTextStream << QObject::tr("Successfully moved entry %1 to group %2.").arg(entry->title(), destinationPath) | ||
<< endl; | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef KEEPASSXC_MOVE_H | ||
#define KEEPASSXC_MOVE_H | ||
|
||
#include "DatabaseCommand.h" | ||
|
||
class Move : public DatabaseCommand | ||
{ | ||
public: | ||
Move(); | ||
~Move(); | ||
|
||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser); | ||
}; | ||
|
||
#endif // KEEPASSXC_MOVE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 2 or (at your option) | ||
* version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <cstdlib> | ||
#include <stdio.h> | ||
|
||
#include "RemoveGroup.h" | ||
|
||
#include "cli/TextStream.h" | ||
#include "cli/Utils.h" | ||
#include "core/Database.h" | ||
#include "core/Entry.h" | ||
#include "core/Group.h" | ||
#include "core/Metadata.h" | ||
#include "core/Tools.h" | ||
|
||
RemoveGroup::RemoveGroup() | ||
{ | ||
name = QString("rmdir"); | ||
description = QString("Removes a group from a database."); | ||
positionalArguments.append({QString("group"), QObject::tr("Path of the group to remove."), QString("")}); | ||
} | ||
|
||
RemoveGroup::~RemoveGroup() | ||
{ | ||
} | ||
|
||
int RemoveGroup::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser) | ||
{ | ||
bool quiet = parser->isSet(Command::QuietOption); | ||
QString databasePath = parser->positionalArguments().at(0); | ||
QString groupPath = parser->positionalArguments().at(1); | ||
|
||
TextStream outputTextStream(quiet ? Utils::DEVNULL : Utils::STDOUT, QIODevice::WriteOnly); | ||
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly); | ||
|
||
// Recursive option means were looking for a group to remove. | ||
QPointer<Group> group = database->rootGroup()->findGroupByPath(groupPath); | ||
if (!group) { | ||
errorTextStream << QObject::tr("Group %1 not found.").arg(groupPath) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (group == database->rootGroup()) { | ||
errorTextStream << QObject::tr("Cannot remove root group from database.") << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
bool recycled = true; | ||
auto* recycleBin = database->metadata()->recycleBin(); | ||
if (!database->metadata()->recycleBinEnabled() || (recycleBin && recycleBin->findGroupByUuid(group->uuid()))) { | ||
delete group; | ||
recycled = false; | ||
} else { | ||
database->recycleGroup(group); | ||
}; | ||
|
||
QString errorMessage; | ||
if (!database->save(databasePath, &errorMessage, true, false)) { | ||
errorTextStream << QObject::tr("Unable to save database to file: %1").arg(errorMessage) << endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (recycled) { | ||
outputTextStream << QObject::tr("Successfully recycled group %1.").arg(groupPath) << endl; | ||
} else { | ||
outputTextStream << QObject::tr("Successfully deleted group %1.").arg(groupPath) << endl; | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
Oops, something went wrong.