Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with renaming passwords and moving folders #532

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions src/imitatepass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ void ImitatePass::Insert(QString file, QString newValue, bool overwrite) {
* @param msg
*/
void ImitatePass::GitCommit(const QString &file, const QString &msg) {
executeGit(GIT_COMMIT, {"commit", "-m", msg, "--", pgit(file)});
if (file.isEmpty())
executeGit(GIT_COMMIT, {"commit", "-m", msg});
else
executeGit(GIT_COMMIT, {"commit", "-m", msg, "--", pgit(file)});
}

/**
Expand Down Expand Up @@ -342,39 +345,75 @@ void ImitatePass::reencryptPath(const QString &dir) {

void ImitatePass::Move(const QString src, const QString dest,
const bool force) {
QFileInfo destFileInfo(dest);
transactionHelper trans(this, PASS_MOVE);
QFileInfo srcFileInfo(src);
QFileInfo destFileInfo(dest);
QString destFile;

if (srcFileInfo.isFile()) {
if (destFileInfo.isFile()) {
if (!force) {
#ifdef QT_DEBUG
dbg() << "Destination file already exists";
#endif
return;
}
} else if (destFileInfo.isDir()) {
destFile = QDir(dest).filePath(srcFileInfo.baseName());
} else {
destFile = dest;
}

if (!destFile.endsWith(".gpg"))
destFile.append(".gpg");

} else if (srcFileInfo.isDir()) {
if (destFileInfo.isDir()) {
destFile = QDir(dest).filePath(srcFileInfo.baseName());
} else if (destFileInfo.isFile()) {
#ifdef QT_DEBUG
dbg() << "Destination is a file";
#endif
return;
} else {
destFile = dest;
}

} else {
#ifdef QT_DEBUG
dbg() << "Source file does not exist";
#endif
return;
}

#ifdef QT_DEBUG
dbg() << "Move Source: " << src;
dbg() << "Move Destination: " << destFile;
#endif

if (QtPassSettings::isUseGit()) {
QStringList args;
args << "mv";
if (force) {
args << "-f";
}
args << pgit(src);
args << pgit(dest);
args << pgit(destFile);
executeGit(GIT_MOVE, args);

QString message = QString("moved from %1 to %2 using QTPass.");
message = message.arg(src).arg(dest);
QString relSrc = QDir(QtPassSettings::getPassStore()).relativeFilePath(src);
relSrc.replace(QRegExp("\\.gpg$"), "");
QString relDest = QDir(QtPassSettings::getPassStore()).relativeFilePath(destFile);
relDest.replace(QRegExp("\\.gpg$"), "");
QString message = QString("Moved for %1 to %2 using QtPass.");
message = message.arg(relSrc).arg(relDest);
GitCommit("", message);
} else {
QDir qDir;
QFileInfo srcFileInfo(src);
QString destCopy = dest;
if (srcFileInfo.isFile() && destFileInfo.isDir()) {
destCopy = destFileInfo.absoluteFilePath() + QDir::separator() +
srcFileInfo.fileName();
}
if (force) {
qDir.remove(destCopy);
qDir.remove(destFile);
}
qDir.rename(src, destCopy);
}
// reecrypt all files under the new folder
if (destFileInfo.isDir()) {
reencryptPath(destFileInfo.absoluteFilePath());
} else if (destFileInfo.isFile()) {
reencryptPath(destFileInfo.dir().path());
qDir.rename(src, destFile);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,15 +1016,14 @@ void MainWindow::editPassword(const QString &file) {
void MainWindow::renamePassword() {
bool ok;
QString file = getFile(ui->treeView->currentIndex(), false);
QString filePath = QFileInfo(file).path();
QString fileName = QFileInfo(file).baseName();
QString newName =
QInputDialog::getText(this, tr("Rename file"), tr("Rename File To: "),
QLineEdit::Normal, fileName, &ok);
if (!ok || newName.isEmpty())
return;
QString newFile = file;
newFile.replace(file.lastIndexOf(fileName), fileName.length(), newName);
newFile.replace(QRegExp("\\.gpg$"), "");
QString newFile = QDir(filePath).filePath(newName);
QtPassSettings::getPass()->Move(file, newFile);
}

Expand Down