QtPass  1.2.3
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 #include <QDir>
5 #include <QFileInfo>
6 
7 using namespace Enums;
8 
10 
14 void RealPass::GitInit() { executePass(GIT_INIT, {"git", "init"}); }
15 
21  exec.executeBlocking(QtPassSettings::getPassExecutable(), {"git", "pull"});
22 }
23 
27 void RealPass::GitPull() { executePass(GIT_PULL, {"git", "pull"}); }
28 
32 void RealPass::GitPush() { executePass(GIT_PUSH, {"git", "push"}); }
33 
43 void RealPass::Show(QString file) {
44  executePass(PASS_SHOW, {"show", file}, "", true);
45 }
46 
51 void RealPass::OtpGenerate(QString file) {
52  executePass(PASS_OTP_GENERATE, {"otp", file}, "", true);
53 }
54 
55 
59 void RealPass::Insert(QString file, QString newValue, bool overwrite) {
60  QStringList args = {"insert", "-m"};
61  if (overwrite)
62  args.append("-f");
63  args.append(file);
64  executePass(PASS_INSERT, args, newValue);
65 }
66 
70 void RealPass::Remove(QString file, bool isDir) {
71  executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
72 }
73 
80 void RealPass::Init(QString path, const QList<UserInfo> &users) {
81  // remove the passStore directory otherwise,
82  // pass would create a passStore/passStore/dir
83  // but you want passStore/dir
84  QString dirWithoutPassdir =
85  path.remove(0, QtPassSettings::getPassStore().size());
86  QStringList args = {"init", "--path=" + dirWithoutPassdir};
87  foreach (const UserInfo &user, users) {
88  if (user.enabled)
89  args.append(user.key_id);
90  }
91  executePass(PASS_INIT, args);
92 }
93 
100 void RealPass::Move(const QString src, const QString dest, const bool force) {
101  QFileInfo srcFileInfo = QFileInfo(src);
102  QFileInfo destFileInfo = QFileInfo(dest);
103 
104  // force mode?
105  // pass uses always the force mode, when call from eg. QT. so we have to check
106  // if this are to files
107  // and the user didnt want to move force
108  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
109  return;
110  }
111 
112  QString passSrc = QDir(QtPassSettings::getPassStore())
113  .relativeFilePath(QDir(src).absolutePath());
114  QString passDest = QDir(QtPassSettings::getPassStore())
115  .relativeFilePath(QDir(dest).absolutePath());
116 
117  // remove the .gpg because pass will not work
118  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
119  passSrc.replace(QRegExp("\\.gpg$"), "");
120  }
121  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
122  passDest.replace(QRegExp("\\.gpg$"), "");
123  }
124 
125  QStringList args;
126  args << "mv";
127  if (force) {
128  args << "-f";
129  }
130  args << passSrc;
131  args << passDest;
132  executePass(PASS_MOVE, args);
133 }
134 
141 void RealPass::Copy(const QString src, const QString dest, const bool force) {
142  QFileInfo srcFileInfo = QFileInfo(src);
143  QFileInfo destFileInfo = QFileInfo(dest);
144  // force mode?
145  // pass uses always the force mode, when call from eg. QT. so we have to check
146  // if this are to files
147  // and the user didnt want to move force
148  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
149  return;
150  }
151 
152  QString passSrc = QDir(QtPassSettings::getPassStore())
153  .relativeFilePath(QDir(src).absolutePath());
154  QString passDest = QDir(QtPassSettings::getPassStore())
155  .relativeFilePath(QDir(dest).absolutePath());
156 
157  // remove the .gpg because pass will not work
158  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
159  passSrc.replace(QRegExp("\\.gpg$"), "");
160  }
161  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
162  passDest.replace(QRegExp("\\.gpg$"), "");
163  }
164  QStringList args;
165  args << "cp";
166  if (force) {
167  args << "-f";
168  }
169  args << passSrc;
170  args << passDest;
171  executePass(PASS_COPY, args);
172 }
173 
178 void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
179  bool readStdout, bool readStderr) {
180  executeWrapper(id, QtPassSettings::getPassExecutable(), args, input,
181  readStdout, readStderr);
182 }
virtual void Insert(QString file, QString value, bool overwrite=false) Q_DECL_OVERRIDE
RealPass::Insert pass insert.
Definition: realpass.cpp:59
RealPass()
Definition: realpass.cpp:9
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:100
virtual void GitInit() Q_DECL_OVERRIDE
RealPass::GitInit pass git init wrapper.
Definition: realpass.cpp:14
PROCESS
Definition: enums.h:16
bool enabled
UserInfo::enabled.
Definition: userinfo.h:50
virtual void OtpGenerate(QString file) Q_DECL_OVERRIDE
RealPass::OtpGenerate pass otp.
Definition: realpass.cpp:51
QString key_id
UserInfo::key_id hexadecimal representation.
Definition: userinfo.h:36
virtual void Show(QString file) Q_DECL_OVERRIDE
RealPass::Show pass show.
Definition: realpass.cpp:43
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:80
static QString getPassExecutable(const QString &defaultValue=QVariant().toString())
virtual void GitPull() Q_DECL_OVERRIDE
RealPass::GitPull pass git pull wrapper.
Definition: realpass.cpp:27
Stores key info lines including validity, creation date and more.
Definition: userinfo.h:11
virtual void GitPull_b() Q_DECL_OVERRIDE
RealPass::GitInit pass git pull wrapper which blocks until process finishes.
Definition: realpass.cpp:20
virtual void GitPush() Q_DECL_OVERRIDE
RealPass::GitPush pass git push wrapper.
Definition: realpass.cpp:32
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:141
virtual void Remove(QString file, bool isDir=false) Q_DECL_OVERRIDE
RealPass::Remove pass remove wrapper.
Definition: realpass.cpp:70