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 
58 void RealPass::Insert(QString file, QString newValue, bool overwrite) {
59  QStringList args = {"insert", "-m"};
60  if (overwrite)
61  args.append("-f");
62  args.append(file);
63  executePass(PASS_INSERT, args, newValue);
64 }
65 
69 void RealPass::Remove(QString file, bool isDir) {
70  executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
71 }
72 
79 void RealPass::Init(QString path, const QList<UserInfo> &users) {
80  // remove the passStore directory otherwise,
81  // pass would create a passStore/passStore/dir
82  // but you want passStore/dir
83  QString dirWithoutPassdir =
84  path.remove(0, QtPassSettings::getPassStore().size());
85  QStringList args = {"init", "--path=" + dirWithoutPassdir};
86  foreach (const UserInfo &user, users) {
87  if (user.enabled)
88  args.append(user.key_id);
89  }
90  executePass(PASS_INIT, args);
91 }
92 
99 void RealPass::Move(const QString src, const QString dest, const bool force) {
100  QFileInfo srcFileInfo = QFileInfo(src);
101  QFileInfo destFileInfo = QFileInfo(dest);
102 
103  // force mode?
104  // pass uses always the force mode, when call from eg. QT. so we have to check
105  // if this are to files
106  // and the user didnt want to move force
107  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
108  return;
109  }
110 
111  QString passSrc = QDir(QtPassSettings::getPassStore())
112  .relativeFilePath(QDir(src).absolutePath());
113  QString passDest = QDir(QtPassSettings::getPassStore())
114  .relativeFilePath(QDir(dest).absolutePath());
115 
116  // remove the .gpg because pass will not work
117  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
118  passSrc.replace(QRegExp("\\.gpg$"), "");
119  }
120  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
121  passDest.replace(QRegExp("\\.gpg$"), "");
122  }
123 
124  QStringList args;
125  args << "mv";
126  if (force) {
127  args << "-f";
128  }
129  args << passSrc;
130  args << passDest;
131  executePass(PASS_MOVE, args);
132 }
133 
140 void RealPass::Copy(const QString src, const QString dest, const bool force) {
141  QFileInfo srcFileInfo = QFileInfo(src);
142  QFileInfo destFileInfo = QFileInfo(dest);
143  // force mode?
144  // pass uses always the force mode, when call from eg. QT. so we have to check
145  // if this are to files
146  // and the user didnt want to move force
147  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
148  return;
149  }
150 
151  QString passSrc = QDir(QtPassSettings::getPassStore())
152  .relativeFilePath(QDir(src).absolutePath());
153  QString passDest = QDir(QtPassSettings::getPassStore())
154  .relativeFilePath(QDir(dest).absolutePath());
155 
156  // remove the .gpg because pass will not work
157  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
158  passSrc.replace(QRegExp("\\.gpg$"), "");
159  }
160  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
161  passDest.replace(QRegExp("\\.gpg$"), "");
162  }
163  QStringList args;
164  args << "cp";
165  if (force) {
166  args << "-f";
167  }
168  args << passSrc;
169  args << passDest;
170  executePass(PASS_COPY, args);
171 }
172 
177 void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
178  bool readStdout, bool readStderr) {
179  executeWrapper(id, QtPassSettings::getPassExecutable(), args, input,
180  readStdout, readStderr);
181 }
virtual void Insert(QString file, QString value, bool overwrite=false) Q_DECL_OVERRIDE
RealPass::Insert pass insert.
Definition: realpass.cpp:58
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:99
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:79
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:140
virtual void Remove(QString file, bool isDir=false) Q_DECL_OVERRIDE
RealPass::Remove pass remove wrapper.
Definition: realpass.cpp:69