1919
2020#include " FileDialog.h"
2121
22- #include " Application.h"
2322#include " Settings.h"
2423#include " dialogs/WarningDialog.h"
2524
@@ -47,6 +46,8 @@ class CPtr
4746 inline operator T*() const { return d; }
4847 inline T** operator &() { return &d; }
4948};
49+ #elif defined(Q_OS_MAC)
50+ #include < sys/xattr.h>
5051#endif
5152
5253#include < array>
@@ -61,7 +62,7 @@ QString FileDialog::createNewFileName(const QString &file, bool signature, QWidg
6162 const QFileInfo f (normalized (file));
6263 QString dir = defaultDir.isEmpty () ? f.absolutePath () : defaultDir;
6364 QString fileName = QDir::toNativeSeparators (dir + QDir::separator () + f.completeBaseName () + extension);
64- #ifndef Q_OS_OSX
65+ #ifndef Q_OS_MACOS
6566 // macOS App Sandbox restricts the rights of the application to write to the filesystem outside of
6667 // app sandbox; user must explicitly give permission to write data to the specific folders.
6768 if (!QFile::exists (fileName))
@@ -107,23 +108,6 @@ bool FileDialog::fileIsWritable( const QString &filename )
107108 return result;
108109}
109110
110- int FileDialog::fileZone (const QString &path)
111- {
112- #ifdef Q_OS_WIN
113- CPtr<IZoneIdentifier> spzi;
114- CPtr<IPersistFile> spf;
115- DWORD dwZone = 0 ;
116- if (SUCCEEDED (CoCreateInstance (CLSID_PersistentZoneIdentifier, nullptr , CLSCTX_INPROC, IID_PPV_ARGS (&spzi))) &&
117- SUCCEEDED (spzi->QueryInterface (&spf)) &&
118- SUCCEEDED (spf->Load (LPCWSTR (QDir::toNativeSeparators (path).utf16 ()), STGM_READ)) &&
119- SUCCEEDED (spzi->GetId (&dwZone)))
120- return int (dwZone);
121- #else
122- Q_UNUSED (path)
123- #endif
124- return -1 ;
125- }
126-
127111bool FileDialog::isSignedPDF (const QString &path)
128112{
129113 if (!path.endsWith (QLatin1String (" pdf" ), Qt::CaseInsensitive))
@@ -136,25 +120,33 @@ bool FileDialog::isSignedPDF(const QString &path)
136120 return std::any_of (list.begin (), list.end (), [&blob](const char *token) { return blob.indexOf (token) > 0 ; });
137121}
138122
139- void FileDialog::setFileZone (const QString &path, int zone )
123+ void FileDialog::setFileZone (const QString &target, const QString &source )
140124{
141- if (zone < 0 )
142- return ;
143125#ifdef Q_OS_WIN
144126 CPtr<IZoneIdentifier> spzi;
145127 CPtr<IPersistFile> spf;
146- if (SUCCEEDED (CoCreateInstance (CLSID_PersistentZoneIdentifier, nullptr , CLSCTX_INPROC, IID_PPV_ARGS (&spzi))) &&
147- SUCCEEDED (spzi->SetId (zone)) &&
148- SUCCEEDED (spzi->QueryInterface (&spf)))
149- spf->Save (LPCWSTR (QDir::toNativeSeparators (path).utf16 ()), TRUE );
128+ if (SUCCEEDED (CoCreateInstance (CLSID_PersistentZoneIdentifier, nullptr , CLSCTX_INPROC_SERVER, IID_PPV_ARGS (&spzi))) &&
129+ SUCCEEDED (spzi->QueryInterface (&spf)) &&
130+ SUCCEEDED (spf->Load (LPCWSTR (QDir::toNativeSeparators (source).utf16 ()), STGM_READ)))
131+ spf->Save (LPCWSTR (QDir::toNativeSeparators (target).utf16 ()), TRUE );
132+ #elif defined(Q_OS_MACOS)
133+ QByteArray p = QFile::encodeName (source);
134+ ssize_t n = getxattr (p.constData (), " com.apple.quarantine" , nullptr , 0 , 0 , 0 );
135+ if (n <= 0 )
136+ return ;
137+ QByteArray value (int (n), Qt::Uninitialized);
138+ n = getxattr (p.constData (), " com.apple.quarantine" , value.data (), value.size (), 0 , 0 );
139+ if (n > 0 )
140+ setxattr (QFile::encodeName (target).constData (), " com.apple.quarantine" , value.constData (), size_t (n), 0 , 0 );
150141#else
151- Q_UNUSED (path)
142+ Q_UNUSED (target)
143+ Q_UNUSED (source)
152144#endif
153145}
154146
155147void FileDialog::setReadOnly (const QString &path, bool readonly)
156148{
157- #if defined( Q_OS_WIN)
149+ #ifdef Q_OS_WIN
158150 ::SetFileAttributesW (LPCWSTR(path.utf16()), readonly ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL);
159151#else
160152 QFile::setPermissions (path, QFile::Permissions (QFile::Permission::ReadOwner)
@@ -164,7 +156,7 @@ void FileDialog::setReadOnly(const QString &path, bool readonly)
164156
165157QString FileDialog::getDir ( const QString &dir )
166158{
167- #ifdef Q_OS_OSX
159+ #ifdef Q_OS_MACOS
168160 Q_UNUSED (dir)
169161 QString path = QSettings ().value (QStringLiteral (" NSNavLastRootDirectory" )).toString ();
170162 path.replace (' ~' , QDir::homePath ());
@@ -266,14 +258,32 @@ QString FileDialog::getExistingDirectory( QWidget *parent, const QString &captio
266258 return result ( res );
267259}
268260
269- QString FileDialog::getSaveFileName ( QWidget *parent, const QString &caption,
270- const QString &dir, const QString &filter, QString *selectedFilter, Options options )
261+ QString FileDialog::getSaveFileName (QWidget *parent, const QString &caption, const QString &filename, QString filter)
271262{
263+ if (filename.endsWith (QLatin1String (" .adoc" ), Qt::CaseInsensitive))
264+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.adoc" ));
265+ else if (filename.endsWith (QLatin1String (" .asice" ), Qt::CaseInsensitive) ||
266+ filename.endsWith (QLatin1String (" .sce" ), Qt::CaseInsensitive))
267+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.asice *.sce" ));
268+ else if (filename.endsWith (QLatin1String (" .asics" ), Qt::CaseInsensitive) ||
269+ filename.endsWith (QLatin1String (" .scs" ), Qt::CaseInsensitive))
270+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.asics *.scs" ));
271+ else if (filename.endsWith (QLatin1String (" .bdoc" ), Qt::CaseInsensitive))
272+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.bdoc" ));
273+ else if (filename.endsWith (QLatin1String (" .cdoc" ), Qt::CaseInsensitive))
274+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.cdoc" ));
275+ else if (filename.endsWith (QLatin1String (" .cdoc2" ), Qt::CaseInsensitive))
276+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.cdoc2" ));
277+ else if (filename.endsWith (QLatin1String (" .ddoc" ), Qt::CaseInsensitive))
278+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.ddoc" ));
279+ else if (filename.endsWith (QLatin1String (" .edoc" ), Qt::CaseInsensitive))
280+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.edoc" ));
281+ else if (filename.endsWith (QLatin1String (" .pdf" ), Qt::CaseInsensitive))
282+ filter = tr (" Documents (%1)" ).arg (QLatin1String (" *.pdf" ));
272283 QString file;
273284 while ( true )
274285 {
275- file = QFileDialog::getSaveFileName (parent,
276- caption, normalized (dir), filter, selectedFilter, options);
286+ file = QFileDialog::getSaveFileName (parent, caption, normalized (filename), filter);
277287 if ( !file.isEmpty () && !fileIsWritable ( file ) )
278288 {
279289 WarningDialog::show (parent, tr ( " You don't have sufficient privileges to write this file into folder %1" ).arg ( file ));
@@ -301,7 +311,7 @@ QString FileDialog::normalized(const QString &data)
301311
302312QString FileDialog::result ( const QString &str )
303313{
304- #ifndef Q_OS_OSX
314+ #ifndef Q_OS_MACOS
305315 if (!str.isEmpty ())
306316 Settings::LAST_PATH = QFileInfo (str).absolutePath ();
307317#else
@@ -322,7 +332,7 @@ QString FileDialog::tempPath(const QString &file)
322332{
323333 QDir tmp = QDir::temp ();
324334 if (!tmp.exists (file))
325- return tmp.path () + " / " + file;
335+ return tmp.path () + ' / ' + file;
326336 QFileInfo info (file);
327337 int i = 0 ;
328338 while (tmp.exists (QStringLiteral (" %1_%2.%3" ).arg (info.baseName ()).arg (i).arg (info.suffix ())))
@@ -334,7 +344,7 @@ QString FileDialog::safeName(const QString &file)
334344{
335345 QFileInfo info (file);
336346 QString filename = info.fileName ();
337- #if defined( Q_OS_WIN)
347+ #ifdef Q_OS_WIN
338348 static const QStringList disabled { " CON" , " PRN" , " AUX" , " NUL" ,
339349 " COM1" , " COM2" , " COM3" , " COM4" , " COM5" , " COM6" , " COM7" , " COM8" , " COM9" ,
340350 " LPT1" , " LPT2" , " LPT3" , " LPT4" , " LPT5" , " LPT6" , " LPT7" , " LPT8" , " LPT9" };
0 commit comments