diff --git a/src/core/core.h b/src/core/core.h index b56627018..626d4bc41 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -184,6 +184,7 @@ signals: void fileTransferInfo(ToxFile file); void fileTransferRemotePausedUnpaused(ToxFile file, bool paused); void fileTransferBrokenUnbroken(ToxFile file, bool broken); + void fileNameChanged(); void fileSendFailed(uint32_t friendId, const QString& fname); diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index 210c8d674..768956b2f 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -21,12 +21,14 @@ #include "core.h" #include "corefile.h" #include "toxfile.h" +#include "toxstring.h" #include "src/persistence/profile.h" #include "src/persistence/settings.h" #include #include #include #include +#include #include /** @@ -273,11 +275,21 @@ void CoreFile::removeFile(uint32_t friendId, uint32_t fileId) fileMap.remove(key); } +QString CoreFile::getCleanFileName(QString filename) +{ + QRegularExpression regex("[<>:\"/\\|?*]"); + filename.replace(regex, "_"); + + return filename; +} + void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, uint32_t kind, uint64_t filesize, const uint8_t* fname, size_t fnameLen, void* vCore) { Core* core = static_cast(vCore); + auto filename = ToxString(fname, fnameLen); + const auto cleanFileName = CoreFile::getCleanFileName(filename.getQString()); if (kind == TOX_FILE_KIND_AVATAR) { const ToxPk friendPk = core->getFriendPublicKey(friendId); @@ -314,7 +326,15 @@ void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, u qDebug() << QString("Received file request %1:%2 kind %3").arg(friendId).arg(fileId).arg(kind); } - ToxFile file{fileId, friendId, QByteArray((char*)fname, fnameLen), "", ToxFile::RECEIVING}; + if (cleanFileName != filename.getQString()) { + qDebug() << QStringLiteral("Cleaned filename from %1 to %2").arg(filename.getQString()).arg(cleanFileName); + filename = ToxString(cleanFileName); + emit core->fileNameChanged(); + } else { + qDebug() << QStringLiteral("cleanFileName: filename already clean"); + } + + ToxFile file{fileId, friendId, filename.getBytes(), "", ToxFile::RECEIVING}; file.filesize = filesize; file.fileKind = kind; file.resumeFileId.resize(TOX_FILE_ID_LENGTH); diff --git a/src/core/corefile.h b/src/core/corefile.h index bc5c618c3..759cd2876 100644 --- a/src/core/corefile.h +++ b/src/core/corefile.h @@ -77,6 +77,7 @@ private: private: static QMutex fileSendMutex; static QHash fileMap; + static QString getCleanFileName(QString filename); }; #endif // COREFILE_H diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index f57875f16..a6bc1b7d2 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -166,6 +166,7 @@ ChatForm::ChatForm(Friend* chatFriend, History* history) connect(core, &Core::friendMessageReceived, this, &ChatForm::onFriendMessageReceived); connect(core, &Core::friendTypingChanged, this, &ChatForm::onFriendTypingChanged); connect(core, &Core::friendStatusChanged, this, &ChatForm::onFriendStatusChanged); + connect(core, &Core::fileNameChanged, this, &ChatForm::onFileNameChanged); const CoreAV* av = core->getAv(); @@ -232,6 +233,12 @@ void ChatForm::onSendTriggered() SendMessageStr(msgEdit->toPlainText()); msgEdit->clear(); } +void ChatForm::onFileNameChanged() +{ + QMessageBox::warning(this, tr("Filename contained illegal characters"), + tr("Illegal characters have been changed to _ \n" + "so you can save the file on windows.")); +} void ChatForm::onTextEditChanged() { diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index c5860f95c..a6f1942f2 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -74,6 +74,7 @@ public slots: void onAvEnd(uint32_t friendId, bool error); void onAvatarChange(uint32_t friendId, const QPixmap& pic); void onAvatarRemoved(uint32_t friendId); + void onFileNameChanged(); protected slots: void onSearchUp(const QString& phrase) override;