QtPass  1.2.1
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 
50 void RealPass::Insert(QString file, QString newValue, bool overwrite) {
51  QStringList args = {"insert", "-m"};
52  if (overwrite)
53  args.append("-f");
54  args.append(file);
55  executePass(PASS_INSERT, args, newValue);
56 }
57 
61 void RealPass::Remove(QString file, bool isDir) {
62  executePass(PASS_REMOVE, {"rm", (isDir ? "-rf" : "-f"), file});
63 }
64 
71 void RealPass::Init(QString path, const QList<UserInfo> &users) {
72  // remove the passStore directory otherwise,
73  // pass would create a passStore/passStore/dir
74  // but you want passStore/dir
75  QString dirWithoutPassdir =
76  path.remove(0, QtPassSettings::getPassStore().size());
77  QStringList args = {"init", "--path=" + dirWithoutPassdir};
78  foreach (const UserInfo &user, users) {
79  if (user.enabled)
80  args.append(user.key_id);
81  }
82  executePass(PASS_INIT, args);
83 }
84 
91 void RealPass::Move(const QString src, const QString dest, const bool force) {
92  QFileInfo srcFileInfo = QFileInfo(src);
93  QFileInfo destFileInfo = QFileInfo(dest);
94 
95  // force mode?
96  // pass uses always the force mode, when call from eg. QT. so we have to check
97  // if this are to files
98  // and the user didnt want to move force
99  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
100  return;
101  }
102 
103  QString passSrc = QDir(QtPassSettings::getPassStore())
104  .relativeFilePath(QDir(src).absolutePath());
105  QString passDest = QDir(QtPassSettings::getPassStore())
106  .relativeFilePath(QDir(dest).absolutePath());
107 
108  // remove the .gpg because pass will not work
109  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
110  passSrc.replace(QRegExp("\\.gpg$"), "");
111  }
112  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
113  passDest.replace(QRegExp("\\.gpg$"), "");
114  }
115 
116  QStringList args;
117  args << "mv";
118  if (force) {
119  args << "-f";
120  }
121  args << passSrc;
122  args << passDest;
123  executePass(PASS_MOVE, args);
124 }
125 
132 void RealPass::Copy(const QString src, const QString dest, const bool force) {
133  QFileInfo srcFileInfo = QFileInfo(src);
134  QFileInfo destFileInfo = QFileInfo(dest);
135  // force mode?
136  // pass uses always the force mode, when call from eg. QT. so we have to check
137  // if this are to files
138  // and the user didnt want to move force
139  if (force == false && srcFileInfo.isFile() && destFileInfo.isFile()) {
140  return;
141  }
142 
143  QString passSrc = QDir(QtPassSettings::getPassStore())
144  .relativeFilePath(QDir(src).absolutePath());
145  QString passDest = QDir(QtPassSettings::getPassStore())
146  .relativeFilePath(QDir(dest).absolutePath());
147 
148  // remove the .gpg because pass will not work
149  if (srcFileInfo.isFile() && srcFileInfo.suffix() == "gpg") {
150  passSrc.replace(QRegExp("\\.gpg$"), "");
151  }
152  if (destFileInfo.isFile() && destFileInfo.suffix() == "gpg") {
153  passDest.replace(QRegExp("\\.gpg$"), "");
154  }
155  QStringList args;
156  args << "cp";
157  if (force) {
158  args << "-f";
159  }
160  args << passSrc;
161  args << passDest;
162  executePass(PASS_COPY, args);
163 }
164 
169 void RealPass::executePass(PROCESS id, const QStringList &args, QString input,
170  bool readStdout, bool readStderr) {
171  executeWrapper(id, QtPassSettings::getPassExecutable(), args, input,
172  readStdout, readStderr);
173 }
virtual void Insert(QString file, QString value, bool overwrite=false) Q_DECL_OVERRIDE
RealPass::Insert pass insert.
Definition: realpass.cpp:50
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:91
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
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:71
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:132
virtual void Remove(QString file, bool isDir=false) Q_DECL_OVERRIDE
RealPass::Remove pass remove wrapper.
Definition: realpass.cpp:61