QtPass  1.2.0
Multi-platform GUI for pass, the standard unix password manager.
realpass.cpp
Go to the documentation of this file.
1 #include "realpass.h"
2 #include "qtpasssettings.h"
3 
4 using namespace Enums;
5 
7 
11 void RealPass::GitInit() { executePass(GIT_INIT, {"git", "init"}); }
12 
18  exec.executeBlocking(QtPassSettings::getPassExecutable(), {"git", "pull"});
19 }
20 
24 void RealPass::GitPull() { executePass(GIT_PULL, {"git", "pull"}); }
25 
29 void RealPass::GitPush() { executePass(GIT_PUSH, {"git", "push"}); }
30 
40 void RealPass::Show(QString file) {
41  executePass(PASS_SHOW, {"show", file}, "", true);
42 }
43 
47 void RealPass::Insert(QString file, QString newValue, bool overwrite) {
48  QStringList args = {"insert", "-m"};
49  if (overwrite)
50  args.append("-f");
51  args.append(file);
52  executePass(PASS_INSERT, args, newValue);
53 }
54 
58 void RealPass::Remove(QString file, bool isDir) {
59  executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
60 }
61 
68 void RealPass::Init(QString path, const QList<UserInfo> &users) {
69  // remove the passStore directory otherwise,
70  // pass would create a passStore/passStore/dir
71  // but you want passStore/dir
72  QString dirWithoutPassdir =
73  path.remove(0, QtPassSettings::getPassStore().size());
74  QStringList args = {"init", "--path=" + dirWithoutPassdir};
75  foreach (const UserInfo &user, users) {
76  if (user.enabled)
77  args.append(user.key_id);
78  }
79  executePass(PASS_INIT, args);
80 }
81 
88 void RealPass::Move(const QString src, const QString dest, const bool force) {
89  QFileInfo srcFileInfo = QFileInfo(src);
90  QFileInfo destFileInfo = QFileInfo(dest);
91 
92  // force mode?
93  // pass uses always the force mode, when call from eg. QT. so we have to check
94  // if this are to files
95  // and the user didnt want to move force
96  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
97  return;
98  }
99 
100  QString passSrc = QDir(QtPassSettings::getPassStore())
101  .relativeFilePath(QDir(src).absolutePath());
102  QString passDest = QDir(QtPassSettings::getPassStore())
103  .relativeFilePath(QDir(dest).absolutePath());
104 
105  // remove the .gpg because pass will not work
106  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
107  passSrc.replace(QRegExp("\\.gpg$"), "");
108  }
109  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
110  passDest.replace(QRegExp("\\.gpg$"), "");
111  }
112 
113  QStringList args;
114  args << "mv";
115  if (force) {
116  args << "-f";
117  }
118  args << passSrc;
119  args << passDest;
120  executePass(PASS_MOVE, args);
121 }
122 
129 void RealPass::Copy(const QString src, const QString dest, const bool force) {
130  QFileInfo srcFileInfo = QFileInfo(src);
131  QFileInfo destFileInfo = QFileInfo(dest);
132  // force mode?
133  // pass uses always the force mode, when call from eg. QT. so we have to check
134  // if this are to files
135  // and the user didnt want to move force
136  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
137  return;
138  }
139 
140  QString passSrc = QDir(QtPassSettings::getPassStore())
141  .relativeFilePath(QDir(src).absolutePath());
142  QString passDest = QDir(QtPassSettings::getPassStore())
143  .relativeFilePath(QDir(dest).absolutePath());
144 
145  // remove the .gpg because pass will not work
146  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
147  passSrc.replace(QRegExp("\\.gpg$"), "");
148  }
149  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
150  passDest.replace(QRegExp("\\.gpg$"), "");
151  }
152  QStringList args;
153  args << "cp";
154  if (force) {
155  args << "-f";
156  }
157  args << passSrc;
158  args << passDest;
159  executePass(PASS_COPY, args);
160 }
161 
166 void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
167  bool readStdout, bool readStderr) {
168  executeWrapper(id, QtPassSettings::getPassExecutable(), args, input,
169  readStdout, readStderr);
170 }
virtual void Insert(QString file, QString value, bool overwrite=false) Q_DECL_OVERRIDE
RealPass::Insert pass insert.
Definition: realpass.cpp:47
RealPass()
Definition: realpass.cpp:6
void Move(const QString src, const QString dest, const bool force=false) Q_DECL_OVERRIDE
RealPass::Move move a file (or folder)
Definition: realpass.cpp:88
virtual void GitInit() Q_DECL_OVERRIDE
RealPass::GitInit pass git init wrapper.
Definition: realpass.cpp:11
PROCESS
Definition: enums.h:16
bool enabled
UserInfo::enabled.
Definition: datahelpers.h:85
QString key_id
UserInfo::key_id hexadecimal representation.
Definition: datahelpers.h:71
virtual void Show(QString file) Q_DECL_OVERRIDE
RealPass::Show pass show.
Definition: realpass.cpp:40
static QString getPassStore(const QString &defaultValue=QVariant().toString())
Enumerators for configuration and runtime items.
virtual void Init(QString path, const QList< UserInfo > &users) Q_DECL_OVERRIDE
RealPass::Init initialize pass repository.
Definition: realpass.cpp:68
static QString getPassExecutable(const QString &defaultValue=QVariant().toString())
virtual void GitPull() Q_DECL_OVERRIDE
RealPass::GitPull pass git pull wrapper.
Definition: realpass.cpp:24
Stores key info lines including validity, creation date and more.
Definition: datahelpers.h:46
virtual void GitPull_b() Q_DECL_OVERRIDE
RealPass::GitInit pass git pull wrapper which blocks until process finishes.
Definition: realpass.cpp:17
virtual void GitPush() Q_DECL_OVERRIDE
RealPass::GitPush pass git push wrapper.
Definition: realpass.cpp:29
void Copy(const QString src, const QString dest, const bool force=false) Q_DECL_OVERRIDE
RealPass::Copy copy a file (or folder)
Definition: realpass.cpp:129
virtual void Remove(QString file, bool isDir=false) Q_DECL_OVERRIDE
RealPass::Remove pass remove wrapper.
Definition: realpass.cpp:58