Feature : Clip command using native programs. (#792)
* Adding a timeout option * Using native apps. * Renaming PasswordInput -> Utils
This commit is contained in:
@@ -15,8 +15,10 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <stdio.h>
|
||||
#include <thread>
|
||||
|
||||
#include "Clip.h"
|
||||
|
||||
@@ -25,11 +27,11 @@
|
||||
#include <QStringList>
|
||||
#include <QTextStream>
|
||||
|
||||
#include "gui/UnlockDatabaseDialog.h"
|
||||
#include "cli/Utils.h"
|
||||
#include "core/Database.h"
|
||||
#include "core/Entry.h"
|
||||
#include "core/Group.h"
|
||||
#include "gui/Clipboard.h"
|
||||
#include "gui/UnlockDatabaseDialog.h"
|
||||
|
||||
Clip::Clip()
|
||||
{
|
||||
@@ -50,6 +52,7 @@ int Clip::execute(int argc, char** argv)
|
||||
}
|
||||
|
||||
QTextStream out(stdout);
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(this->description);
|
||||
@@ -59,17 +62,19 @@ int Clip::execute(int argc, char** argv)
|
||||
QObject::tr("Use a GUI prompt unlocking the database."));
|
||||
parser.addOption(guiPrompt);
|
||||
parser.addPositionalArgument("entry", QObject::tr("Path of the entry to clip."));
|
||||
parser.addPositionalArgument(
|
||||
"timeout",
|
||||
QObject::tr("Timeout in seconds before clearing the clipboard."),
|
||||
QString("[timeout]"));
|
||||
parser.process(arguments);
|
||||
|
||||
const QStringList args = parser.positionalArguments();
|
||||
if (args.size() != 2) {
|
||||
QCoreApplication app(argc, argv);
|
||||
if (args.size() != 2 && args.size() != 3) {
|
||||
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Database* db = nullptr;
|
||||
QApplication app(argc, argv);
|
||||
if (parser.isSet("gui-prompt")) {
|
||||
db = UnlockDatabaseDialog::openDatabasePrompt(args.at(0));
|
||||
} else {
|
||||
@@ -79,12 +84,20 @@ int Clip::execute(int argc, char** argv)
|
||||
if (!db) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return this->clipEntry(db, args.at(1));
|
||||
return this->clipEntry(db, args.at(1), args.value(2));
|
||||
}
|
||||
|
||||
int Clip::clipEntry(Database* database, QString entryPath)
|
||||
int Clip::clipEntry(Database* database, QString entryPath, QString timeout)
|
||||
{
|
||||
|
||||
int timeoutSeconds = 0;
|
||||
if (!timeout.isEmpty() && !timeout.toInt()) {
|
||||
qCritical("Invalid timeout value %s.", qPrintable(timeout));
|
||||
return EXIT_FAILURE;
|
||||
} else if (!timeout.isEmpty()) {
|
||||
timeoutSeconds = timeout.toInt();
|
||||
}
|
||||
|
||||
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
|
||||
Entry* entry = database->rootGroup()->findEntry(entryPath);
|
||||
if (!entry) {
|
||||
@@ -92,7 +105,25 @@ int Clip::clipEntry(Database* database, QString entryPath)
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Clipboard::instance()->setText(entry->password());
|
||||
return EXIT_SUCCESS;
|
||||
int exitCode = Utils::clipText(entry->password());
|
||||
if (exitCode != EXIT_SUCCESS) {
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
outputTextStream << "Entry's password copied to the clipboard!" << endl;
|
||||
|
||||
if (!timeoutSeconds) {
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
while (timeoutSeconds > 0) {
|
||||
outputTextStream << "\rClearing the clipboard in " << timeoutSeconds << " seconds...";
|
||||
outputTextStream.flush();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
timeoutSeconds--;
|
||||
}
|
||||
Utils::clipText("");
|
||||
outputTextStream << "\nClipboard cleared!" << endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user