Skip to content

Commit

Permalink
itemencrypted: Import newly generated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Jun 11, 2016
1 parent ccf28fd commit 5270d45
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions plugins/itemencrypted/itemencrypted.cpp
Expand Up @@ -78,20 +78,9 @@ QStringList getDefaultEncryptCommandArgumentsEscaped()
return args;
}

void startGpgProcess(QProcess *p, const QStringList &args, bool importPrivateKey = false)
void startGpgProcess(QProcess *p, const QStringList &args)
{
KeyPairPaths keys;

if (importPrivateKey) {
p->start("gpg", getDefaultEncryptCommandArguments(keys.pub) << "--import" << keys.sec);
if (!p->waitForFinished(5000)) {
p->terminate();
if ( !p->waitForFinished(5000) )
p->kill();
return;
}
}

p->start("gpg", getDefaultEncryptCommandArguments(keys.pub) + args);
}

Expand All @@ -110,6 +99,22 @@ bool verifyProcess(QProcess *p)
return true;
}

bool importGpgKey()
{
KeyPairPaths keys;

QProcess p;
p.start("gpg", getDefaultEncryptCommandArguments(keys.pub) << "--import" << keys.sec);
if (!p.waitForFinished(5000)) {
p.terminate();
if ( !p.waitForFinished(5000) )
p.kill();
return false;
}

return verifyProcess(&p);
}

QByteArray readGpgOutput(const QStringList &args, const QByteArray &input = QByteArray())
{
QProcess p;
Expand Down Expand Up @@ -302,8 +307,14 @@ bool ItemEncryptedLoader::loadItems(QAbstractItemModel *model, QFile *file)
return false;
}

if (!importGpgKey())
{
COPYQ_LOG("ItemEncrypted ERROR: Failte to import GPG key");
return false;
}

QProcess p;
startGpgProcess( &p, QStringList("--decrypt"), true );
startGpgProcess( &p, QStringList("--decrypt") );

char encryptedBytes[4096];

Expand Down Expand Up @@ -562,6 +573,7 @@ void ItemEncryptedLoader::onGpgProcessFinished(int exitCode, QProcess::ExitStatu
m_gpgProcessStatus = GpgNotRunning;

if ( oldStatus == GpgGeneratingKeys && error.isEmpty() ) {
importGpgKey();
setPassword();
} else {
updateUi();
Expand Down

0 comments on commit 5270d45

Please sign in to comment.