Skip to content

Commit

Permalink
Remove RTL Override symbols from filenames.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Nov 16, 2017
1 parent cb5c59c commit aec496d
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 68 deletions.
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ namespace {
versionChanged = convert->setRemoteVersion(version);
convert->setRemoteLocation(dc, access);
convert->date = date;
convert->mime = mime;
convert->setMimeString(mime);
if (!thumb->isNull() && (convert->thumb->isNull() || convert->thumb->width() < thumb->width() || convert->thumb->height() < thumb->height() || versionChanged)) {
updateImage(convert->thumb, thumb);
}
Expand Down Expand Up @@ -1648,7 +1648,7 @@ namespace {
} else {
result = DocumentData::create(document, dc, access, version, attributes);
result->date = date;
result->mime = mime;
result->setMimeString(mime);
result->thumb = thumb;
result->size = size;
result->recountIsImage();
Expand All @@ -1666,7 +1666,7 @@ namespace {
result->setRemoteLocation(dc, access);
}
result->date = date;
result->mime = mime;
result->setMimeString(mime);
if (!thumb->isNull() && (result->thumb->isNull() || result->thumb->width() < thumb->width() || result->thumb->height() < thumb->height() || versionChanged)) {
result->thumb = thumb;
}
Expand Down
30 changes: 21 additions & 9 deletions Telegram/SourceFiles/boxes/send_files_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,18 @@ void SendFilesBox::prepareDocumentLayout() {
}
}

auto nameString = DocumentData::composeNameString(filename, songTitle, songPerformer);
_nameText.setText(st::semiboldTextStyle, nameString, _textNameOptions);
auto nameString = DocumentData::ComposeNameString(
filename,
songTitle,
songPerformer);
_nameText.setText(
st::semiboldTextStyle,
nameString,
_textNameOptions);
_statusText = formatSizeText(fileinfo.size());
_statusWidth = qMax(_nameText.maxWidth(), st::normalFont->width(_statusText));
_statusWidth = qMax(
_nameText.maxWidth(),
st::normalFont->width(_statusText));
}
}

Expand Down Expand Up @@ -504,13 +512,17 @@ EditCaptionBox::EditCaptionBox(QWidget*, HistoryMedia *media, FullMsgId msgId) :
}

if (doc) {
if (doc->voice()) {
_name.setText(st::semiboldTextStyle, lang(lng_media_audio), _textNameOptions);
} else {
_name.setText(st::semiboldTextStyle, doc->composeNameString(), _textNameOptions);
}
auto nameString = doc->voice()
? lang(lng_media_audio)
: doc->composeNameString();
_name.setText(
st::semiboldTextStyle,
nameString,
_textNameOptions);
_status = formatSizeText(doc->size);
_statusw = qMax(_name.maxWidth(), st::normalFont->width(_status));
_statusw = qMax(
_name.maxWidth(),
st::normalFont->width(_status));
_isImage = doc->isImage();
_isAudio = (doc->voice() || doc->song());
}
Expand Down
27 changes: 20 additions & 7 deletions Telegram/SourceFiles/data/data_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@ QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = fals
}

QString name, filter, caption, prefix;
MimeType mimeType = mimeTypeForName(data->mime);
MimeType mimeType = mimeTypeForName(data->mimeString());
QStringList p = mimeType.globPatterns();
QString pattern = p.isEmpty() ? QString() : p.front();
if (data->voice()) {
bool mp3 = (data->mime == qstr("audio/mp3"));
auto mp3 = data->hasMimeType(qstr("audio/mp3"));
name = already.isEmpty() ? (mp3 ? qsl(".mp3") : qsl(".ogg")) : already;
filter = mp3 ? qsl("MP3 Audio (*.mp3);;") : qsl("OGG Opus Audio (*.ogg);;");
filter += FileDialog::AllFilesFilter();
caption = lang(lng_save_audio);
prefix = qsl("audio");
} else if (data->isVideo()) {
name = already.isEmpty() ? data->name : already;
name = already.isEmpty() ? data->filename() : already;
if (name.isEmpty()) {
name = pattern.isEmpty() ? qsl(".mov") : pattern.replace('*', QString());
}
Expand All @@ -182,7 +182,7 @@ QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = fals
caption = lang(lng_save_video);
prefix = qsl("video");
} else {
name = already.isEmpty() ? data->name : already;
name = already.isEmpty() ? data->filename() : already;
if (name.isEmpty()) {
name = pattern.isEmpty() ? qsl(".unknown") : pattern.replace('*', QString());
}
Expand Down Expand Up @@ -444,7 +444,17 @@ void DocumentData::setattributes(const QVector<MTPDocumentAttribute> &attributes
song()->performer = qs(d.vperformer);
}
} break;
case mtpc_documentAttributeFilename: name = qs(attributes[i].c_documentAttributeFilename().vfile_name); break;
case mtpc_documentAttributeFilename: {
auto &attribute = attributes[i];
auto remoteFileName = qs(
attribute.c_documentAttributeFilename().vfile_name);

// We don't want RTL Override characters in filenames,
// because they introduce a security issue, when a filename
// "Fil[RTLO]gepj.exe" looks like "Filexe.jpeg" being ".exe"
auto rtlOverride = QChar(0x202E);
_filename = std::move(remoteFileName).replace(rtlOverride, "");
} break;
}
}
if (type == StickerDocument) {
Expand Down Expand Up @@ -868,7 +878,7 @@ void DocumentData::recountIsImage() {
if (isAnimation() || isVideo()) {
return;
}
_duration = fileIsImage(name, mime) ? 1 : -1; // hack
_duration = fileIsImage(filename(), mimeString()) ? 1 : -1; // hack
}

bool DocumentData::setRemoteVersion(int32 version) {
Expand Down Expand Up @@ -928,7 +938,10 @@ DocumentData::~DocumentData() {
}
}

QString DocumentData::composeNameString(const QString &filename, const QString &songTitle, const QString &songPerformer) {
QString DocumentData::ComposeNameString(
const QString &filename,
const QString &songTitle,
const QString &songPerformer) {
if (songTitle.isEmpty() && songPerformer.isEmpty()) {
return filename.isEmpty() ? qsl("Unknown File") : filename;
}
Expand Down
37 changes: 26 additions & 11 deletions Telegram/SourceFiles/data/data_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,26 @@ class DocumentData {
bool isAnimation() const {
return (type == AnimatedDocument)
|| isRoundVideo()
|| !mime.compare(qstr("image/gif"), Qt::CaseInsensitive);
|| hasMimeType(qstr("image/gif"));
}
bool isGifv() const {
return (type == AnimatedDocument)
&& !mime.compare(qstr("video/mp4"), Qt::CaseInsensitive);
&& hasMimeType(qstr("video/mp4"));
}
bool isTheme() const {
return
name.endsWith(
_filename.endsWith(
qstr(".tdesktop-theme"),
Qt::CaseInsensitive)
|| name.endsWith(
|| _filename.endsWith(
qstr(".tdesktop-palette"),
Qt::CaseInsensitive);
}
bool tryPlaySong() const {
return (song() != nullptr)
|| mime.startsWith(qstr("audio/"), Qt::CaseInsensitive);
|| _mimeString.startsWith(
qstr("audio/"),
Qt::CaseInsensitive);
}
bool isMusic() const {
if (auto s = song()) {
Expand Down Expand Up @@ -247,14 +249,25 @@ class DocumentData {
// to (this) received from the server "same" document.
void collectLocalData(DocumentData *local);

QString filename() const {
return _filename;
}
QString mimeString() const {
return _mimeString;
}
bool hasMimeType(QLatin1String mime) const {
return !_mimeString.compare(mime, Qt::CaseInsensitive);
}
void setMimeString(const QString &mime) {
_mimeString = mime;
}

~DocumentData();

DocumentId id = 0;
DocumentType type = FileDocument;
QSize dimensions;
int32 date = 0;
QString name;
QString mime;
ImagePtr thumb, replyPreview;
int32 size = 0;

Expand All @@ -267,18 +280,18 @@ class DocumentData {
return ::mediaKey(locationType(), _dc, id, _version);
}

static QString composeNameString(
static QString ComposeNameString(
const QString &filename,
const QString &songTitle,
const QString &songPerformer);
QString composeNameString() const {
if (auto songData = song()) {
return composeNameString(
name,
return ComposeNameString(
_filename,
songData->title,
songData->performer);
}
return composeNameString(name, QString(), QString());
return ComposeNameString(_filename, QString(), QString());
}

private:
Expand All @@ -305,6 +318,8 @@ class DocumentData {
uint64 _access = 0;
int32 _version = 0;
QString _url;
QString _filename;
QString _mimeString;

FileLocation _location;
QByteArray _data;
Expand Down
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/history/history_media_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ void HistoryDocument::createComponents(bool caption) {
} else {
mask |= HistoryDocumentNamed::Bit();
if (!_data->song()
&& !documentIsExecutableName(_data->name)
&& !documentIsExecutableName(_data->filename())
&& !_data->thumb->isNull()
&& _data->thumb->width()
&& _data->thumb->height()) {
Expand All @@ -1128,8 +1128,8 @@ void HistoryDocument::createComponents(bool caption) {
}

void HistoryDocument::fillNamedFromData(HistoryDocumentNamed *named) {
auto name = named->_name = _data->composeNameString();
named->_namew = st::semiboldFont->width(name);
auto nameString = named->_name = _data->composeNameString();
named->_namew = st::semiboldFont->width(nameString);
}

void HistoryDocument::initDimensions() {
Expand Down
24 changes: 17 additions & 7 deletions Telegram/SourceFiles/history/history_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ ApiWrap::RequestMessageDataCallback replyEditMessageDataCallback() {
}

MTPVector<MTPDocumentAttribute> composeDocumentAttributes(DocumentData *document) {
QVector<MTPDocumentAttribute> attributes(1, MTP_documentAttributeFilename(MTP_string(document->name)));
auto filenameAttribute = MTP_documentAttributeFilename(
MTP_string(document->filename()));
auto attributes = QVector<MTPDocumentAttribute>(1, filenameAttribute);
if (document->dimensions.width() > 0 && document->dimensions.height() > 0) {
int32 duration = document->duration();
if (duration >= 0) {
Expand Down Expand Up @@ -4428,7 +4430,15 @@ void HistoryWidget::onDocumentUploaded(const FullMsgId &newId, bool silent, cons
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
}
auto caption = item->getMedia() ? item->getMedia()->getCaption() : TextWithEntities();
auto media = MTP_inputMediaUploadedDocument(MTP_flags(0), file, MTPInputFile(), MTP_string(document->mime), composeDocumentAttributes(document), MTP_string(caption.text), MTPVector<MTPInputDocument>(), MTP_int(0));
auto media = MTP_inputMediaUploadedDocument(
MTP_flags(0),
file,
MTPInputFile(),
MTP_string(document->mimeString()),
composeDocumentAttributes(document),
MTP_string(caption.text),
MTPVector<MTPInputDocument>(),
MTP_int(0));
hist->sendRequestId = MTP::send(MTPmessages_SendMedia(MTP_flags(sendFlags), item->history()->peer->input, MTP_int(replyTo), media, MTP_long(randomId), MTPnullMarkup), App::main()->rpcDone(&MainWidget::sentUpdatesReceived), App::main()->rpcFail(&MainWidget::sendMessageFail), 0, 0, hist->sendRequestId);
}
}
Expand All @@ -4453,7 +4463,7 @@ void HistoryWidget::onThumbDocumentUploaded(const FullMsgId &newId, bool silent,
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
}
auto caption = media ? media->getCaption() : TextWithEntities();
auto media = MTP_inputMediaUploadedDocument(MTP_flags(MTPDinputMediaUploadedDocument::Flag::f_thumb), file, thumb, MTP_string(document->mime), composeDocumentAttributes(document), MTP_string(caption.text), MTPVector<MTPInputDocument>(), MTP_int(0));
auto media = MTP_inputMediaUploadedDocument(MTP_flags(MTPDinputMediaUploadedDocument::Flag::f_thumb), file, thumb, MTP_string(document->mimeString()), composeDocumentAttributes(document), MTP_string(caption.text), MTPVector<MTPInputDocument>(), MTP_int(0));
hist->sendRequestId = MTP::send(MTPmessages_SendMedia(MTP_flags(sendFlags), item->history()->peer->input, MTP_int(replyTo), media, MTP_long(randomId), MTPnullMarkup), App::main()->rpcDone(&MainWidget::sentUpdatesReceived), App::main()->rpcFail(&MainWidget::sendMessageFail), 0, 0, hist->sendRequestId);
}
}
Expand Down Expand Up @@ -5888,18 +5898,18 @@ void HistoryWidget::updatePreview() {
if (_previewData->title.isEmpty()) {
if (_previewData->description.text.isEmpty()) {
title = _previewData->author;
desc = ((_previewData->document && !_previewData->document->name.isEmpty()) ? _previewData->document->name : _previewData->url);
desc = ((_previewData->document && !_previewData->document->filename().isEmpty()) ? _previewData->document->filename() : _previewData->url);
} else {
title = _previewData->description.text;
desc = _previewData->author.isEmpty() ? ((_previewData->document && !_previewData->document->name.isEmpty()) ? _previewData->document->name : _previewData->url) : _previewData->author;
desc = _previewData->author.isEmpty() ? ((_previewData->document && !_previewData->document->filename().isEmpty()) ? _previewData->document->filename() : _previewData->url) : _previewData->author;
}
} else {
title = _previewData->title;
desc = _previewData->description.text.isEmpty() ? (_previewData->author.isEmpty() ? ((_previewData->document && !_previewData->document->name.isEmpty()) ? _previewData->document->name : _previewData->url) : _previewData->author) : _previewData->description.text;
desc = _previewData->description.text.isEmpty() ? (_previewData->author.isEmpty() ? ((_previewData->document && !_previewData->document->filename().isEmpty()) ? _previewData->document->filename() : _previewData->url) : _previewData->author) : _previewData->description.text;
}
} else {
title = _previewData->siteName;
desc = _previewData->title.isEmpty() ? (_previewData->description.text.isEmpty() ? (_previewData->author.isEmpty() ? ((_previewData->document && !_previewData->document->name.isEmpty()) ? _previewData->document->name : _previewData->url) : _previewData->author) : _previewData->description.text) : _previewData->title;
desc = _previewData->title.isEmpty() ? (_previewData->description.text.isEmpty() ? (_previewData->author.isEmpty() ? ((_previewData->document && !_previewData->document->filename().isEmpty()) ? _previewData->document->filename() : _previewData->url) : _previewData->author) : _previewData->description.text) : _previewData->title;
}
if (title.isEmpty()) {
if (_previewData->document) {
Expand Down
40 changes: 26 additions & 14 deletions Telegram/SourceFiles/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,44 +152,56 @@ QString formatPlayedText(qint64 played, qint64 duration) {
}

int32 documentColorIndex(DocumentData *document, QString &ext) {
int32 colorIndex = 0;
auto colorIndex = 0;

QString name = document ? (document->name.isEmpty() ? (document->sticker() ? lang(lng_in_dlg_sticker) : qsl("Unknown File")) : document->name) : lang(lng_message_empty);
auto name = document
? (document->filename().isEmpty()
? (document->sticker()
? lang(lng_in_dlg_sticker)
: qsl("Unknown File"))
: document->filename())
: lang(lng_message_empty);
name = name.toLower();
int32 lastDot = name.lastIndexOf('.');
QString mime = document ? document->mime.toLower() : QString();
auto lastDot = name.lastIndexOf('.');
auto mime = document
? document->mimeString().toLower()
: QString();
if (name.endsWith(qstr(".doc")) ||
name.endsWith(qstr(".txt")) ||
name.endsWith(qstr(".psd")) ||
mime.startsWith(qstr("text/"))
) {
mime.startsWith(qstr("text/"))) {
colorIndex = 0;
} else if (
name.endsWith(qstr(".xls")) ||
name.endsWith(qstr(".csv"))
) {
name.endsWith(qstr(".csv"))) {
colorIndex = 1;
} else if (
name.endsWith(qstr(".pdf")) ||
name.endsWith(qstr(".ppt")) ||
name.endsWith(qstr(".key"))
) {
name.endsWith(qstr(".key"))) {
colorIndex = 2;
} else if (
name.endsWith(qstr(".zip")) ||
name.endsWith(qstr(".rar")) ||
name.endsWith(qstr(".ai")) ||
name.endsWith(qstr(".mp3")) ||
name.endsWith(qstr(".mov")) ||
name.endsWith(qstr(".avi"))
) {
name.endsWith(qstr(".avi"))) {
colorIndex = 3;
} else {
QChar ch = (lastDot >= 0 && lastDot + 1 < name.size()) ? name.at(lastDot + 1) : (name.isEmpty() ? (mime.isEmpty() ? '0' : mime.at(0)) : name.at(0));
auto ch = (lastDot >= 0 && lastDot + 1 < name.size())
? name.at(lastDot + 1)
: (name.isEmpty()
? (mime.isEmpty() ? '0' : mime.at(0))
: name.at(0));
colorIndex = (ch.unicode() % 4);
}

ext = document ? ((lastDot < 0 || lastDot + 2 > name.size()) ? name : name.mid(lastDot + 1)) : QString();
ext = document
? ((lastDot < 0 || lastDot + 2 > name.size())
? name
: name.mid(lastDot + 1))
: QString();

return colorIndex;
}
Expand Down
Loading

0 comments on commit aec496d

Please sign in to comment.