Skip to content

Commit 61f50b3

Browse files
authored
Verify if argument is file before handling it (#1353)
IB-8591 Signed-off-by: Raul Metsma <[email protected]>
1 parent 2b48d57 commit 61f50b3

File tree

5 files changed

+44
-43
lines changed

5 files changed

+44
-43
lines changed

client/Application.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ using namespace std::chrono;
8787
const QStringList Application::CONTAINER_EXT {
8888
QStringLiteral("asice"), QStringLiteral("sce"),
8989
QStringLiteral("asics"), QStringLiteral("scs"),
90-
QStringLiteral("bdoc"), QStringLiteral("edoc"),
90+
QStringLiteral("bdoc"), QStringLiteral("edoc"), QStringLiteral("adoc"),
9191
};
9292

9393
class DigidocConf final: public digidoc::XmlConfCurrent
@@ -623,7 +623,7 @@ bool Application::event(QEvent *event)
623623
{
624624
case REOpenEvent::Type:
625625
if( !activeWindow() )
626-
parseArgs();
626+
showClient();
627627
return true;
628628
case QEvent::FileOpen:
629629
{
@@ -841,7 +841,7 @@ void Application::parseArgs(QStringList args)
841841
suffix.endsWith(QLatin1String(".cdoc"), Qt::CaseInsensitive) ||
842842
suffix.endsWith(QLatin1String(".cdoc2"), Qt::CaseInsensitive))
843843
crypto = true;
844-
showClient(args, crypto, sign, newWindow);
844+
showClient(std::move(args), crypto, sign, newWindow);
845845
}
846846

847847
uint Application::readTSLVersion(const QString &path)
@@ -869,19 +869,28 @@ int Application::run()
869869
return exec();
870870
}
871871

872-
void Application::showClient(const QStringList &params, bool crypto, bool sign, bool newWindow)
872+
void Application::showClient(QStringList files, bool crypto, bool sign, bool newWindow)
873873
{
874+
// Make sure all parameters are files
875+
for(auto i = files.begin(); i != files.end(); )
876+
{
877+
if(QFileInfo(*i).isFile())
878+
++i;
879+
else
880+
i = files.erase(i);
881+
}
882+
874883
if(sign)
875-
sign = params.size() != 1 || !CONTAINER_EXT.contains(QFileInfo(params.value(0)).suffix(), Qt::CaseInsensitive);
884+
sign = files.size() != 1 || !CONTAINER_EXT.contains(QFileInfo(files.value(0)).suffix(), Qt::CaseInsensitive);
885+
876886
MainWindow *w = nullptr;
877-
if(!newWindow && params.isEmpty())
878-
{
879-
// If no files selected (e.g. restoring minimized window), select first
887+
if(newWindow)
888+
w = nullptr;
889+
else if(files.isEmpty()) // If no files selected (e.g. restoring minimized window), select first
880890
w = qobject_cast<MainWindow*>(mainWindow());
881-
}
882-
else if(!newWindow)
891+
else
883892
{
884-
// else select first window with no open files
893+
// select first window with no open files
885894
for(auto *widget : topLevelWidgets())
886895
{
887896
if(auto *main = qobject_cast<MainWindow*>(widget);
@@ -923,12 +932,13 @@ void Application::showClient(const QStringList &params, bool crypto, bool sign,
923932
w->show();
924933
w->activateWindow();
925934
w->raise();
926-
if(!params.isEmpty())
927-
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
928-
QMetaObject::invokeMethod(w, [&] { w->open(params, crypto, sign); });
929-
#else
930-
QMetaObject::invokeMethod(w, &MainWindow::open, params, crypto, sign);
931-
#endif
935+
if(files.isEmpty())
936+
return;
937+
QMetaObject::invokeMethod(w, [&] {
938+
using enum ria::qdigidoc4::Pages;
939+
w->selectPage(crypto && !sign ? CryptoIntro : SignIntro);
940+
w->openFiles(files, false, sign);
941+
});
932942
}
933943

934944
void Application::showWarning( const QString &msg, const digidoc::Exception &e )

client/Application.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Application final: public BaseApplication
7373
static QWidget* mainWindow();
7474
static void openHelp();
7575
static uint readTSLVersion(const QString &path);
76-
static void showClient(const QStringList &params = {}, bool crypto = false, bool sign = false, bool newWindow = false);
76+
static void showClient(QStringList params = {}, bool crypto = false, bool sign = false, bool newWindow = false);
7777
static void updateTSLCache(const QDateTime &tslTime);
7878
#if defined(Q_OS_MAC)
7979
static QString groupContainerPath();

client/Application_mac.mm

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727

2828
using namespace Qt::StringLiterals;
2929

30+
static auto fetchPaths(NSPasteboard *pboard)
31+
{
32+
QStringList result;
33+
NSArray<NSURL *> *urls = [pboard readObjectsForClasses:@[[NSURL class]]
34+
options:@{ NSPasteboardURLReadingFileURLsOnlyKey : @YES }];
35+
for (NSURL *url in urls) {
36+
result.append(QString::fromNSString(url.path).normalized(QString::NormalizationForm_C));
37+
}
38+
return result;
39+
}
40+
3041
@implementation NSApplication (ApplicationObjC)
3142

3243
- (void)appReopen:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
@@ -40,30 +51,21 @@ - (void)openClient:(NSPasteboard *)pboard userData:(NSString *)data error:(NSStr
4051
{
4152
Q_UNUSED(data)
4253
Q_UNUSED(error)
43-
QStringList result;
44-
for( NSString *filename in [pboard propertyListForType:NSFilenamesPboardType] )
45-
result.append(QString::fromNSString(filename).normalized(QString::NormalizationForm_C));
46-
Application::showClient(result);
54+
Application::showClient(fetchPaths(pboard));
4755
}
4856

4957
- (void)signClient:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
5058
{
5159
Q_UNUSED(data)
5260
Q_UNUSED(error)
53-
QStringList result;
54-
for(NSString *filename in [pboard propertyListForType:NSFilenamesPboardType])
55-
result.append(QString::fromNSString(filename).normalized(QString::NormalizationForm_C));
56-
Application::showClient(result, false, true);
61+
Application::showClient(fetchPaths(pboard), false, true);
5762
}
5863

5964
- (void)openCrypto:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
6065
{
6166
Q_UNUSED(data)
6267
Q_UNUSED(error)
63-
QStringList result;
64-
for( NSString *filename in [pboard propertyListForType:NSFilenamesPboardType] )
65-
result.append(QString::fromNSString(filename).normalized(QString::NormalizationForm_C));
66-
Application::showClient(result, true);
68+
Application::showClient(fetchPaths(pboard), true);
6769
}
6870
@end
6971

client/MainWindow.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,16 +614,6 @@ void MainWindow::openFiles(const QStringList &files, bool addFile, bool forceCre
614614
navigateToPage(page, content, create);
615615
}
616616

617-
void MainWindow::open(const QStringList &files, bool crypto, bool sign)
618-
{
619-
if (crypto && !sign)
620-
selectPage(Pages::CryptoIntro);
621-
else
622-
selectPage(Pages::SignIntro);
623-
if(!files.isEmpty())
624-
openFiles(files, false, sign);
625-
}
626-
627617
void MainWindow::openContainer(bool signature)
628618
{
629619
QString filter = QFileDialog::tr("All Files (*)") + QStringLiteral(";;") + tr("Documents (%1)");

client/MainWindow.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class MainWindow final : public QWidget
4444
explicit MainWindow(QWidget *parent = nullptr);
4545
~MainWindow() final;
4646

47-
void open(const QStringList &files, bool crypto, bool sign);
47+
void openFiles(const QStringList &files, bool addFile = false, bool forceCreate = false);
48+
void selectPage(ria::qdigidoc4::Pages page);
4849
void showSettings(int page);
4950

5051
protected:
@@ -73,7 +74,6 @@ class MainWindow final : public QWidget
7374
void onCryptoAction(int action, const QString &id, const QString &phone);
7475
void onSignAction(int action, const QString &info1, const QString &info2);
7576
void openContainer(bool signature);
76-
void openFiles(const QStringList &files, bool addFile = false, bool forceCreate = false);
7777
void pageSelected(int page, bool checked = true);
7878
void pinUnblock(QSmartCardData::PinType type, bool isForgotPin);
7979
void pinPukChange( QSmartCardData::PinType type );
@@ -85,7 +85,6 @@ class MainWindow final : public QWidget
8585
void removeSignatureFile(int index);
8686
bool save(bool saveAs = false);
8787
QString selectFile( const QString &title, const QString &filename, bool fixedExt );
88-
void selectPage(ria::qdigidoc4::Pages page);
8988
template <typename F>
9089
void sign(F &&sign);
9190
void updateCardWarnings(const QSmartCardData &data);

0 commit comments

Comments
 (0)