Skip to content

Commit

Permalink
Show warning when failed to open container (open-eid#1148)
Browse files Browse the repository at this point in the history
IB-7604

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored Feb 6, 2023
1 parent a523867 commit 978e403
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 68 deletions.
116 changes: 54 additions & 62 deletions client/CryptoDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ class CryptoDoc::Private final: public QThread
QByteArray readCDoc(QIODevice *cdoc, bool data);
void readDDoc(QIODevice *ddoc);
void run() final;
void setLastError(const QString &err);
void showError(const QString &err, const QString &details = {})
{
WarningDialog::show(qApp->mainWindow(), err, details);
}
QString size(const QString &size)
{
bool converted = false;
Expand Down Expand Up @@ -156,8 +159,8 @@ class CryptoDoc::Private final: public QThread
QHash<QString,QString> properties;
QList<CKey> keys;
QList<File> files;
bool encrypted = false;
CDocumentModel *documents = nullptr;
bool isEncrypted = false;
CDocumentModel *documents = new CDocumentModel(this);
QStringList tempFiles;
};

Expand Down Expand Up @@ -319,10 +322,10 @@ QByteArray CryptoDoc::Private::fromBase64(const QStringRef &data)
bool CryptoDoc::Private::isEncryptedWarning()
{
if( fileName.isEmpty() )
setLastError( CryptoDoc::tr("Container is not open") );
if( encrypted )
setLastError( CryptoDoc::tr("Container is encrypted") );
return fileName.isEmpty() || encrypted;
showError(CryptoDoc::tr("Container is not open"));
if(isEncrypted)
showError(CryptoDoc::tr("Container is encrypted"));
return fileName.isEmpty() || isEncrypted;
}

bool CryptoDoc::Private::opensslError(bool err)
Expand All @@ -338,7 +341,7 @@ bool CryptoDoc::Private::opensslError(bool err)

void CryptoDoc::Private::run()
{
if( !encrypted )
if(!isEncrypted)
{
qCDebug(CRYPTO) << "Encrypt" << fileName;
QBuffer data;
Expand Down Expand Up @@ -429,18 +432,13 @@ void CryptoDoc::Private::run()
f.mime = mime;
f.size = FileDialog::fileSize(quint64(result.size()));
f.data = result;
files << f;
files.append(std::move(f));
}
else
lastError = CryptoDoc::tr("Error parsing document");
}
}
encrypted = !encrypted;
}

void CryptoDoc::Private::setLastError( const QString &err )
{
qApp->showWarning(err);
isEncrypted = !isEncrypted;
}

QByteArray CryptoDoc::Private::readCDoc(QIODevice *cdoc, bool data)
Expand Down Expand Up @@ -502,7 +500,7 @@ QByteArray CryptoDoc::Private::readCDoc(QIODevice *cdoc, bool data)
file.size = size(fileparts.value(1));
file.mime = fileparts.value(2);
file.id = fileparts.value(3);
files << file;
files.append(std::move(file));
}
else
properties[attr.value().toString()] = xml.readElementText();
Expand Down Expand Up @@ -568,7 +566,7 @@ QByteArray CryptoDoc::Private::readCDoc(QIODevice *cdoc, bool data)
key.cipher = fromBase64(xml.text());
}
}
keys << key;
keys.append(std::move(key));
}
}
return {};
Expand Down Expand Up @@ -766,7 +764,7 @@ void CryptoDoc::Private::readDDoc(QIODevice *ddoc)
x.readNext();
file.data = fromBase64( x.text() );
file.size = FileDialog::fileSize(quint64(file.data.size()));
files << file;
files.append(std::move(file));
}
}
}
Expand Down Expand Up @@ -806,8 +804,6 @@ void CryptoDoc::Private::writeDDoc(QIODevice *ddoc)
CDocumentModel::CDocumentModel(CryptoDoc::Private *doc)
: d( doc )
{
const_cast<QLoggingCategory&>(CRYPTO()).setEnabled( QtDebugMsg,
QFile::exists(QStringLiteral("%1/%2.log").arg( QDir::tempPath(), qApp->applicationName())));
}

bool CDocumentModel::addFile(const QString &file, const QString &mime)
Expand Down Expand Up @@ -848,26 +844,24 @@ bool CDocumentModel::addFile(const QString &file, const QString &mime)
f.name = QFileInfo(file).fileName();
f.data = data.readAll();
f.size = FileDialog::fileSize(quint64(f.data.size()));
d->files << f;
emit added(FileDialog::normalized(f.name));
d->files.append(std::move(f));
emit added(FileDialog::normalized(d->files.last().name));
return true;
}

void CDocumentModel::addTempReference(const QString &file)
{
d->tempFiles << file;
d->tempFiles.append(file);
}

QString CDocumentModel::copy(int row, const QString &dst) const
{
const CryptoDoc::Private::File &file = d->files.at(row);
if( QFile::exists( dst ) )
QFile::remove( dst );

QFile f(dst);
if(!f.open(QFile::WriteOnly) || f.write(file.data) < 0)
if(QFile f(dst); !f.open(QFile::WriteOnly) || f.write(file.data) < 0)
{
d->setLastError( tr("Failed to save file '%1'").arg( dst ) );
d->showError(tr("Failed to save file '%1'").arg(dst));
return {};
}
return dst;
Expand All @@ -890,15 +884,15 @@ QString CDocumentModel::mime(int row) const

void CDocumentModel::open(int row)
{
if(d->encrypted)
if(d->isEncrypted)
return;
QString path = FileDialog::tempPath(FileDialog::safeName(data(row)));
if(!verifyFile(path))
return;
QFileInfo f(copy(row, path));
if( !f.exists() )
return;
d->tempFiles << f.absoluteFilePath();
d->tempFiles.append(f.absoluteFilePath());
#if defined(Q_OS_WIN)
::SetFileAttributesW(f.absoluteFilePath().toStdWString().c_str(), FILE_ATTRIBUTE_READONLY);
#else
Expand All @@ -917,7 +911,7 @@ bool CDocumentModel::removeRows(int row, int count)

if( d->files.isEmpty() || row >= d->files.size() )
{
d->setLastError( DocumentModel::tr("Internal error") );
d->showError(DocumentModel::tr("Internal error"));
return false;
}

Expand All @@ -936,7 +930,7 @@ int CDocumentModel::rowCount() const

QString CDocumentModel::save(int row, const QString &path) const
{
if(d->encrypted)
if(d->isEncrypted)
return {};

int zone = FileDialog::fileZone(d->fileName);
Expand Down Expand Up @@ -979,9 +973,9 @@ void CKey::setCert( const QSslCertificate &c )
CryptoDoc::CryptoDoc( QObject *parent )
: QObject(parent)
, d(new Private)
, containerState(UnencryptedContainer)
{
d->documents = new CDocumentModel( d );
const_cast<QLoggingCategory&>(CRYPTO()).setEnabled(QtDebugMsg,
QFile::exists(QStringLiteral("%1/%2.log").arg( QDir::tempPath(), qApp->applicationName())));
}

CryptoDoc::~CryptoDoc() { clear(); delete d; }
Expand All @@ -992,10 +986,10 @@ bool CryptoDoc::addKey( const CKey &key )
return false;
if( d->keys.contains( key ) )
{
d->setLastError( tr("Key already exists") );
d->showError(tr("Key already exists"));
return false;
}
d->keys << key;
d->keys.append(key);
return true;
}

Expand Down Expand Up @@ -1060,7 +1054,7 @@ void CryptoDoc::clear( const QString &file )
QFile::remove(f);
}
d->tempFiles.clear();
d->encrypted = false;
d->isEncrypted = false;
d->fileName = file;
d->files.clear();
d->keys.clear();
Expand All @@ -1071,17 +1065,17 @@ void CryptoDoc::clear( const QString &file )

ContainerState CryptoDoc::state() const
{
return containerState;
return d->isEncrypted ? EncryptedContainer : UnencryptedContainer;
}

bool CryptoDoc::decrypt()
{
if( d->fileName.isEmpty() )
{
d->setLastError( tr("Container is not open") );
d->showError(tr("Container is not open"));
return false;
}
if( !d->encrypted )
if(!d->isEncrypted)
return true;

CKey key;
Expand All @@ -1095,7 +1089,7 @@ bool CryptoDoc::decrypt()
}
if( key.cert.isNull() )
{
d->setLastError( tr("You do not have the key to decrypt this document") );
d->showError(tr("You do not have the key to decrypt this document"));
return false;
}

Expand Down Expand Up @@ -1125,11 +1119,9 @@ bool CryptoDoc::decrypt()

d->waitForFinished();
if( !d->lastError.isEmpty() )
d->setLastError( d->lastError );

containerState = d->encrypted ? EncryptedContainer : UnencryptedContainer;
d->showError(d->lastError);

return !d->encrypted;
return !d->isEncrypted;
}

DocumentModel* CryptoDoc::documentModel() const { return d->documents; }
Expand All @@ -1140,30 +1132,26 @@ bool CryptoDoc::encrypt( const QString &filename )
d->fileName = filename;
if( d->fileName.isEmpty() )
{
d->setLastError( tr("Container is not open") );
d->showError(tr("Container is not open"));
return false;
}
if( d->encrypted )
if(d->isEncrypted)
return true;
if( d->keys.isEmpty() )
{
d->setLastError( tr("No keys specified") );
d->showError(tr("No keys specified"));
return false;
}

d->waitForFinished();
if( !d->lastError.isEmpty() )
d->setLastError( d->lastError );
d->showError(d->lastError);
open(d->fileName);

containerState = d->encrypted ? EncryptedContainer : UnencryptedContainer;

return d->encrypted;
return d->isEncrypted;
}

QString CryptoDoc::fileName() const { return d->fileName; }
bool CryptoDoc::isEncrypted() const { return d->encrypted; }
bool CryptoDoc::isNull() const { return d->fileName.isEmpty(); }

QList<CKey> CryptoDoc::keys() const
{
Expand All @@ -1174,13 +1162,13 @@ QList<QString> CryptoDoc::files()
{
QList<QString> fileList;
for(const Private::File &f: d->files)
fileList << f.name;
fileList.append(f.name);
return fileList;
}

bool CryptoDoc::move(const QString &to)
{
if(containerState == ContainerState::UnencryptedContainer)
if(!d->isEncrypted)
{
d->fileName = to;
return true;
Expand All @@ -1192,22 +1180,26 @@ bool CryptoDoc::move(const QString &to)
bool CryptoDoc::open( const QString &file )
{
clear(file);
QFile cdoc(d->fileName);
cdoc.open(QFile::ReadOnly);
d->readCDoc(&cdoc, false);
cdoc.close();
if(QFile cdoc(d->fileName); cdoc.open(QFile::ReadOnly))
d->readCDoc(&cdoc, false);
if(d->keys.isEmpty())
{
d->showError(tr("Failed to open the container. "
"You need to update your ID-software in order to open CDOC2 containers. "
"Install new ID-software from <a href='https://www.id.ee/en/article/install-id-software/'>www.id.ee</a>."));
return false;
}

if(d->files.isEmpty() && d->properties.contains(QStringLiteral("Filename")))
{
Private::File f;
f.name = d->properties[QStringLiteral("Filename")];
f.mime = d->mime == Private::MIME_ZLIB ? d->properties[QStringLiteral("OriginalMimeType")] : d->mime;
f.size = d->size(d->properties[QStringLiteral("OriginalSize")]);
d->files << f;
d->files.append(std::move(f));
}

d->encrypted = true;
containerState = EncryptedContainer;
d->isEncrypted = true;
qApp->addRecent( file );
return !d->keys.isEmpty();
}
Expand Down
7 changes: 2 additions & 5 deletions client/CryptoDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ class CryptoDoc: public QObject

bool addKey( const CKey &key );
bool canDecrypt(const QSslCertificate &cert);
void clear( const QString &file = QString() );
void clear(const QString &file = {});
bool decrypt();
DocumentModel* documentModel() const;
bool encrypt( const QString &filename = QString() );
bool encrypt(const QString &filename = {});
QString fileName() const;
QList<QString> files();
bool isEncrypted() const;
bool isNull() const;
QList<CKey> keys() const;
bool move(const QString &to);
bool open( const QString &file );
Expand All @@ -69,7 +67,6 @@ class CryptoDoc: public QObject
private:
class Private;
Private *d;
ria::qdigidoc4::ContainerState containerState;

friend class CDocumentModel;
};
Expand Down
6 changes: 5 additions & 1 deletion client/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@
<source>Digi-ID</source>
<translation>Digi-ID</translation>
</message>
<message>
<source>Failed to open the container. You need to update your ID-software in order to open CDOC2 containers. Install new ID-software from &lt;a href=&apos;https://www.id.ee/en/article/install-id-software/&apos;&gt;www.id.ee&lt;/a&gt;.</source>
<translation>Failed to open the container. You need to update your ID-software in order to open CDOC2 containers. Install new ID-software from &lt;a href=&apos;https://www.id.ee/en/article/install-id-software/&apos;&gt;www.id.ee&lt;/a&gt;.</translation>
</message>
</context>
<context>
<name>Diagnostics</name>
Expand Down Expand Up @@ -2763,7 +2767,7 @@ Additional licenses and components</translation>
</message>
<message>
<source>Select SiVa server certificate</source>
<translation type="unfinished"></translation>
<translation>Select SiVa server certificate</translation>
</message>
<message>
<source>ADD CERTIFICATE</source>
Expand Down
4 changes: 4 additions & 0 deletions client/translations/et.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@
<source>Digi-ID</source>
<translation>Digi-ID</translation>
</message>
<message>
<source>Failed to open the container. You need to update your ID-software in order to open CDOC2 containers. Install new ID-software from &lt;a href=&apos;https://www.id.ee/en/article/install-id-software/&apos;&gt;www.id.ee&lt;/a&gt;.</source>
<translation>Ümbriku avamine ebaõnnestus. CDOC2 ümbriku avamiseks pead ID-tarkvara uuendama. Paigalda uus ID-tarkvara veebilehelt &lt;a href=&apos;https://www.id.ee/artikkel/paigalda-id-tarkvara/&apos;&gt;www.id.ee&lt;/a&gt;.</translation>
</message>
</context>
<context>
<name>Diagnostics</name>
Expand Down
Loading

0 comments on commit 978e403

Please sign in to comment.