Skip to content

Commit 7a90685

Browse files
author
Janosch Knack
committed
move files
wrapper for gpg and git
1 parent 8b01b81 commit 7a90685

7 files changed

+179
-130
lines changed

imitatepass.cpp

+49-42
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@ ImitatePass::ImitatePass() {}
88
* @brief ImitatePass::GitInit git init wrapper
99
*/
1010
void ImitatePass::GitInit() {
11-
executeWrapper(QtPassSettings::getGitExecutable(),
12-
"init \"" + QtPassSettings::getPassStore() + '"');
11+
executeGit("init \"" + QtPassSettings::getPassStore() + '"');
1312
}
1413

1514
/**
1615
* @brief ImitatePass::GitPull git init wrapper
1716
*/
1817
void ImitatePass::GitPull() {
19-
executeWrapper(QtPassSettings::getGitExecutable(), "pull");
18+
executeGit("pull");
2019
}
2120

2221
/**
2322
* @brief ImitatePass::GitPush git init wrapper
2423
*/
2524
void ImitatePass::GitPush() {
26-
executeWrapper(QtPassSettings::getGitExecutable(), "push");
25+
if(QtPassSettings::isUseGit()){
26+
executeGit("push");
27+
}
2728
}
2829

2930
/**
@@ -32,8 +33,7 @@ void ImitatePass::GitPush() {
3233
QProcess::ExitStatus ImitatePass::Show(QString file, bool block) {
3334
// TODO(bezet): apparently not yet needed
3435
// file += ".gpg";
35-
executeWrapper(QtPassSettings::getGpgExecutable(),
36-
"-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
36+
executeGpg("-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
3737
file + '"');
3838
if (block)
3939
return waitForProcess();
@@ -59,17 +59,15 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
5959
return;
6060
}
6161
QString force(overwrite ? " --yes " : " ");
62-
executeWrapper(QtPassSettings::getGpgExecutable(),
63-
force + "--batch -eq --output \"" + file + "\" " + recipients +
62+
executeGpg(force + "--batch -eq --output \"" + file + "\" " + recipients +
6463
" -",
6564
newValue);
6665
if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
6766
if (!overwrite)
68-
executeWrapper(QtPassSettings::getGitExecutable(), "add \"" + file + '"');
67+
executeGit("add \"" + file + '"');
6968
QString path = QDir(QtPassSettings::getPassStore()).relativeFilePath(file);
7069
path.replace(QRegExp("\\.gpg$"), "");
71-
executeWrapper(QtPassSettings::getGitExecutable(),
72-
"commit \"" + file + "\" -m \"" +
70+
executeGit("commit \"" + file + "\" -m \"" +
7371
(overwrite ? "Edit" : "Add") + " for " + path +
7472
" using QtPass.\"");
7573
}
@@ -80,13 +78,11 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
8078
*/
8179
void ImitatePass::Remove(QString file, bool isDir) {
8280
if (QtPassSettings::isUseGit()) {
83-
executeWrapper(QtPassSettings::getGitExecutable(),
84-
QString("rm ") + (isDir ? "-rf " : "-f ") + '"' + file +
81+
executeGit(QString("rm ") + (isDir ? "-rf " : "-f ") + '"' + file +
8582
'"');
8683
// TODO(bezet): commit message used to have pass-like file name inside(ie.
8784
// getFile(file, true)
88-
executeWrapper(QtPassSettings::getGitExecutable(),
89-
"commit \"" + file + "\" -m \"Remove for " + file +
85+
executeGit("commit \"" + file + "\" -m \"Remove for " + file +
9086
" using QtPass.\"");
9187
} else {
9288
if (isDir) {
@@ -140,12 +136,10 @@ void ImitatePass::Init(QString path, const QList<UserInfo> &users) {
140136
if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit() &&
141137
!QtPassSettings::getGitExecutable().isEmpty()) {
142138
if (addFile)
143-
executeWrapper(QtPassSettings::getGitExecutable(),
144-
"add \"" + gpgIdFile + '"');
139+
executeGit("add \"" + gpgIdFile + '"');
145140
QString path = gpgIdFile;
146141
path.replace(QRegExp("\\.gpg$"), "");
147-
executeWrapper(QtPassSettings::getGitExecutable(),
148-
"commit \"" + gpgIdFile + "\" -m \"Added " + path +
142+
executeGit("commit \"" + gpgIdFile + "\" -m \"Added " + path +
149143
" using QtPass.\"");
150144
}
151145
reencryptPath(path);
@@ -206,8 +200,7 @@ void ImitatePass::reencryptPath(QString dir) {
206200
gpgId.sort();
207201
}
208202
process.waitForFinished();
209-
executeWrapper(QtPassSettings::getGpgExecutable(),
210-
"-v --no-secmem-warning "
203+
executeGpg( "-v --no-secmem-warning "
211204
"--no-permission-warning --list-only "
212205
"--keyid-format long " +
213206
fileName);
@@ -233,8 +226,7 @@ void ImitatePass::reencryptPath(QString dir) {
233226
qDebug() << "reencrypt " << fileName << " for " << gpgId;
234227
QString local_lastDecrypt = "Could not decrypt";
235228
emit lastDecrypt(local_lastDecrypt);
236-
executeWrapper(QtPassSettings::getGpgExecutable(),
237-
"-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
229+
executeGpg("-d --quiet --yes --no-encrypt-to --batch --use-agent \"" +
238230
fileName + '"');
239231
process.waitForFinished(30000); // long wait (passphrase stuff)
240232
local_lastDecrypt = process.readAllStandardOutput();
@@ -253,20 +245,18 @@ void ImitatePass::reencryptPath(QString dir) {
253245
"file missing or invalid."));
254246
return;
255247
}
256-
executeWrapper(QtPassSettings::getGpgExecutable(),
248+
executeGpg(
257249
"--yes --batch -eq --output \"" + fileName + "\" " +
258250
recipients + " -",
259251
local_lastDecrypt);
260252
process.waitForFinished(3000);
261253

262254
if (!QtPassSettings::isUseWebDav() && QtPassSettings::isUseGit()) {
263-
executeWrapper(QtPassSettings::getGitExecutable(),
264-
"add \"" + fileName + '"');
255+
executeGit("add \"" + fileName + '"');
265256
QString path =
266257
QDir(QtPassSettings::getPassStore()).relativeFilePath(fileName);
267258
path.replace(QRegExp("\\.gpg$"), "");
268-
executeWrapper(QtPassSettings::getGitExecutable(),
269-
"commit \"" + fileName + "\" -m \"" + "Edit for " +
259+
executeGit("commit \"" + fileName + "\" -m \"" + "Edit for " +
270260
path + " using QtPass.\"");
271261
process.waitForFinished(3000);
272262
}
@@ -283,24 +273,22 @@ void ImitatePass::reencryptPath(QString dir) {
283273
emit endReencryptPath();
284274
}
285275

286-
void ImitatePass::Move(QDir srcDir, QDir destDir, bool force)
276+
void ImitatePass::Move(const QString src, const QString dest, const bool force)
287277
{
288-
QString src = srcDir.absolutePath();
289-
QString dest= destDir.absolutePath();
290278
if (QtPassSettings::isUseGit()) {
291279
QString args = QString("mv %1 %2 %3");
292-
// do we need force mode?
280+
// force mode?
293281
if(force){
294282
args = args.arg("-f");
295283
}else{
296284
args = args.arg("");
297285
}
298286
args = args.arg(src).arg(dest);
299-
executeWrapper(QtPassSettings::getGitExecutable(),args);
287+
executeGit(args);
300288

301-
QString args1=QString("commit -m \"moved dir from %1 to %2 using QTPass.\"");
289+
QString args1=QString("commit -m \"moved from %1 to %2 using QTPass.\"");
302290
args1= args1.arg(src).arg(dest);
303-
executeWrapper(QtPassSettings::getGitExecutable(), args1);
291+
executeGit(args1);
304292

305293
if(QtPassSettings::isAutoPush()){
306294
GitPush();
@@ -315,14 +303,33 @@ void ImitatePass::Move(QDir srcDir, QDir destDir, bool force)
315303
}
316304
}
317305

318-
void ImitatePass::Move(QFile srcFile, QFile destFile, bool force)
319-
{
320-
}
321306

322-
void ImitatePass::Copy(QDir srcDir, QDir destDir, bool force)
307+
void ImitatePass::Copy(const QString src, const QString dest, const bool force)
323308
{
324-
}
309+
if (QtPassSettings::isUseGit()) {
310+
QString args = QString("cp %1 %2 %3");
311+
// do we need force mode?
312+
if(force){
313+
args = args.arg("-f");
314+
}else{
315+
args = args.arg("");
316+
}
317+
args = args.arg(src).arg(dest);
318+
executeGit(args);
325319

326-
void ImitatePass::Copy(QFile srcFile, QFile destFile, bool force)
327-
{
320+
QString args1=QString("commit -m \"copied from %1 to %2 using QTPass.\"");
321+
args1= args1.arg(src).arg(dest);
322+
executeGit(args1);
323+
324+
if(QtPassSettings::isAutoPush()){
325+
GitPush();
326+
}
327+
328+
} else {
329+
QDir qDir;
330+
if(force){
331+
qDir.remove(dest);
332+
}
333+
qDir.rename(src, dest);
334+
}
328335
}

imitatepass.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ class ImitatePass : public Pass {
2828

2929
// Pass interface
3030
public:
31-
void Move(QDir srcDir, QDir destDir, bool force = false);
32-
void Move(QFile srcFile, QFile destFile, bool force = false);
33-
void Copy(QDir srcDir, QDir destDir, bool force = false);
34-
void Copy(QFile srcFile, QFile destFile, bool force = false);
31+
void Move(const QString src, const QString dest, const bool force = false);
32+
void Copy(const QString src, const QString dest, const bool force = false);
3533
};
3634

3735
#endif // IMITATEPASS_H

pass.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,29 @@ QString Pass::getRecipientString(QString for_file, QString separator,
314314
recipients_str += separator + '"' + recipient + '"';
315315
return recipients_str;
316316
}
317+
318+
/**
319+
* @brief Pass::executePass easy wrapper for running pass
320+
* @param args
321+
*/
322+
void Pass::executePass(QString args, QString input) {
323+
executeWrapper(QtPassSettings::getPassExecutable(), args, input);
324+
}
325+
326+
/**
327+
* @brief Pass::executeGpg easy wrapper for running gpg commands
328+
* @param args
329+
*/
330+
void Pass::executeGpg(QString args, QString input)
331+
{
332+
executeWrapper(QtPassSettings::getGpgExecutable(), args, input);
333+
}
334+
/**
335+
* @brief Pass::executeGit easy wrapper for running git commands
336+
* @param args
337+
*/
338+
void Pass::executeGit(QString args, QString input) {
339+
executeWrapper(QtPassSettings::getGitExecutable(), args, input);
340+
}
341+
342+

pass.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class Pass : public QObject {
4040
bool wrapperRunning;
4141
QStringList env;
4242

43-
protected:
43+
private :
4444
void executeWrapper(QString, QString, QString = QString());
45+
protected:
4546
QProcess process;
4647

4748
public:
@@ -55,10 +56,8 @@ class Pass : public QObject {
5556
virtual QProcess::ExitStatus Show(QString file, bool block = false) = 0;
5657
virtual void Insert(QString file, QString value, bool force) = 0;
5758
virtual void Remove(QString file, bool isDir) = 0;
58-
virtual void Move(QDir srcDir, QDir destDir, bool force = false) = 0;
59-
virtual void Move(QFile srcFile, QFile destFile, bool force = false) = 0;
60-
virtual void Copy(QDir srcDir, QDir destDir, bool force = false) = 0;
61-
virtual void Copy(QFile srcFile, QFile destFile, bool force = false) = 0;
59+
virtual void Move(const QString srcDir, const QString dest, const bool force = false) = 0;
60+
virtual void Copy(const QString srcDir, const QString dest, const bool force = false) = 0;
6261
virtual void Init(QString path, const QList<UserInfo> &users) = 0;
6362
virtual QString Generate(int length, const QString &charset);
6463

@@ -77,6 +76,9 @@ class Pass : public QObject {
7776
static QString getRecipientString(QString for_file, QString separator = " ",
7877
int *count = NULL);
7978

79+
void executeGit(QString args, QString input = QString());
80+
void executePass(QString args, QString input = QString());
81+
void executeGpg(QString args, QString input = QString());
8082
private slots:
8183
void processFinished(int, QProcess::ExitStatus);
8284

realpass.cpp

+29-19
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33

44
RealPass::RealPass() {}
55

6-
/**
7-
* @brief RealPass::executePass easy wrapper for running pass
8-
* @param args
9-
*/
10-
void RealPass::executePass(QString args, QString input) {
11-
executeWrapper(QtPassSettings::getPassExecutable(), args, input);
12-
}
6+
137

148
/**
159
* @brief RealPass::GitInit git init wrapper
@@ -78,29 +72,45 @@ void RealPass::Init(QString path, const QList<UserInfo> &users) {
7872
path.remove(0, QtPassSettings::getPassStore().size());
7973
executePass("init --path=" + dirWithoutPassdir + " " + gpgIds);
8074
}
81-
void RealPass::Move(QDir srcDir, QDir destDir, bool force)
75+
void RealPass::Move(const QString src, const QString dest, const bool force)
8276
{
8377
QString args = QString("mv %1 %2 %3");
84-
// do we need force mode?
78+
// force mode?
8579
if(force){
8680
args = args.arg("-f");
8781
}else{
8882
args = args.arg("");
8983
}
90-
QString passSrcDir = QDir(QtPassSettings::getPassStore()).relativeFilePath(srcDir.absolutePath());
91-
QString passDestDir = QDir(QtPassSettings::getPassStore()).relativeFilePath(destDir.absolutePath());
92-
args = args.arg(passSrcDir).arg(passDestDir);
84+
QString passSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(src).absolutePath());
85+
QString passDest= QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(dest).absolutePath());
86+
QFileInfo srcFileInfo= QFileInfo(src);
87+
QFileInfo destFileInfo= QFileInfo(dest);
88+
89+
// remove the .gpg because pass will not work
90+
if(srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg"){
91+
passSrc.replace(QRegExp("\\.gpg$"), "");
92+
}
93+
if(destFileInfo.isFile() && destFileInfo.suffix() == "gpg"){
94+
passDest.replace(QRegExp("\\.gpg$"), "");
95+
}
96+
97+
args = args.arg(passSrc).arg(passDest);
9398
executePass(args);
9499
}
95100

96-
void RealPass::Move(QFile srcFile, QFile destFile, bool force)
97-
{
98-
}
99101

100-
void RealPass::Copy(QDir srcDir, QDir destDir, bool force)
102+
void RealPass::Copy(const QString src, const QString dest, const bool force)
101103
{
104+
QString args = QString("cp %1 %2 %3");
105+
// do we need force mode?
106+
if(force){
107+
args = args.arg("-f");
108+
}else{
109+
args = args.arg("");
110+
}
111+
QString passSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(src).absolutePath());
112+
QString passDest= QDir(QtPassSettings::getPassStore()).relativeFilePath(QDir(dest).absolutePath());
113+
args = args.arg(passSrc).arg(passDest);
114+
executePass(args);
102115
}
103116

104-
void RealPass::Copy(QFile srcFile, QFile destFile, bool force)
105-
{
106-
}

realpass.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include "pass.h"
55

66
class RealPass : public Pass {
7-
private:
8-
void executePass(QString args, QString input = QString());
9-
107
public:
118
RealPass();
129
virtual ~RealPass() {}
@@ -21,10 +18,8 @@ class RealPass : public Pass {
2118

2219
// Pass interface
2320
public:
24-
void Move(QDir srcDir, QDir destDir, bool force = false);
25-
void Move(QFile srcFile, QFile destFile, bool force = false);
26-
void Copy(QDir srcDir, QDir destDir, bool force = false);
27-
void Copy(QFile srcFile, QFile destFile, bool force = false);
21+
void Move(const QString src, const QString dest, const bool force = false);
22+
void Copy(const QString src, const QString dest, const bool force = false);
2823
};
2924

3025

0 commit comments

Comments
 (0)