mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2026-07-25 06:54:43 +00:00
Added ability rename files from send files box.
This commit is contained in:
@@ -7600,4 +7600,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
|
||||
"lng_mac_hold_to_quit" = "Hold {text} to Quit";
|
||||
|
||||
"lng_rename_file" = "Rename file";
|
||||
|
||||
// Keys finished
|
||||
|
||||
@@ -3804,6 +3804,7 @@ void ApiWrap::editMedia(
|
||||
.album = nullptr,
|
||||
.forceFile = forceFile,
|
||||
.idOverride = 0,
|
||||
.displayName = file.displayName,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -3867,6 +3868,7 @@ void ApiWrap::sendFiles(
|
||||
.album = album,
|
||||
.forceFile = forceFile,
|
||||
.idOverride = 0,
|
||||
.displayName = file.displayName,
|
||||
}));
|
||||
caption = TextWithTags();
|
||||
}
|
||||
|
||||
@@ -62,12 +62,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "styles/style_boxes.h"
|
||||
#include "styles/style_chat_helpers.h"
|
||||
#include "styles/style_layers.h"
|
||||
#include "styles/style_settings.h"
|
||||
#include "styles/style_menu_icons.h"
|
||||
|
||||
#include <QtCore/QMimeData>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr auto kMaxMessageLength = 4096;
|
||||
constexpr auto kMaxDisplayNameLength = 64;
|
||||
|
||||
using Ui::SendFilesWay;
|
||||
|
||||
@@ -113,6 +116,55 @@ rpl::producer<QString> FieldPlaceholder(
|
||||
: tr::lng_photos_comment();
|
||||
}
|
||||
|
||||
void RenameFileBox(
|
||||
not_null<Ui::GenericBox*> box,
|
||||
const QString ¤tName,
|
||||
Fn<void(QString)> apply) {
|
||||
box->setTitle(tr::lng_rename_file());
|
||||
const auto field = box->addRow(object_ptr<Ui::InputField>(
|
||||
box,
|
||||
st::settingsDeviceName,
|
||||
rpl::single(QString()),
|
||||
currentName));
|
||||
const auto extension = [&] {
|
||||
const auto dot = currentName.lastIndexOf('.');
|
||||
return (dot >= 0) ? currentName.mid(dot) : QString();
|
||||
}();
|
||||
const auto nameWithoutExt = extension.isEmpty()
|
||||
? currentName
|
||||
: currentName.left(currentName.size() - extension.size());
|
||||
const auto maxNameLength = kMaxDisplayNameLength - extension.size();
|
||||
field->setMaxLength((maxNameLength > 0) ? maxNameLength : 0);
|
||||
field->setText(nameWithoutExt);
|
||||
field->selectAll();
|
||||
box->setFocusCallback([=] {
|
||||
field->setFocusFast();
|
||||
});
|
||||
const auto save = [=] {
|
||||
const auto newName = field->getLastText().trimmed();
|
||||
if (newName.isEmpty()) {
|
||||
field->showError();
|
||||
return;
|
||||
}
|
||||
if ((newName.size() + extension.size()) > kMaxDisplayNameLength) {
|
||||
field->showError();
|
||||
return;
|
||||
}
|
||||
const auto weak = base::make_weak(box);
|
||||
apply(newName + extension);
|
||||
if (const auto strong = weak.get()) {
|
||||
strong->closeBox();
|
||||
}
|
||||
};
|
||||
field->submits() | rpl::on_next([=] {
|
||||
save();
|
||||
}, box->lifetime());
|
||||
box->addButton(tr::lng_settings_save(), save);
|
||||
box->addButton(tr::lng_cancel(), [=] {
|
||||
box->closeBox();
|
||||
});
|
||||
}
|
||||
|
||||
void EditPriceBox(
|
||||
not_null<Ui::GenericBox*> box,
|
||||
not_null<Main::Session*> session,
|
||||
@@ -469,6 +521,16 @@ QImage SendFilesBox::Block::generatePriceTagBackground() const {
|
||||
return QImage();
|
||||
}
|
||||
|
||||
bool SendFilesBox::Block::setSingleFileDisplayName(
|
||||
const QString &displayName) {
|
||||
if (_isAlbum || _isSingleMedia) {
|
||||
return false;
|
||||
}
|
||||
const auto single = static_cast<Ui::SingleFilePreview*>(_preview.get());
|
||||
single->setDisplayName(displayName);
|
||||
return true;
|
||||
}
|
||||
|
||||
SendFilesBox::SendFilesBox(
|
||||
QWidget*,
|
||||
not_null<Window::SessionController*> controller,
|
||||
@@ -689,6 +751,18 @@ void SendFilesBox::refreshAllAfterChanges(int fromItem, Fn<void()> perform) {
|
||||
captionResized();
|
||||
}
|
||||
|
||||
bool SendFilesBox::setDisplayNameInSingleFilePreview(
|
||||
int fileIndex,
|
||||
const QString &displayName) {
|
||||
for (auto &block : _blocks) {
|
||||
if (fileIndex < block.fromIndex() || fileIndex >= block.tillIndex()) {
|
||||
continue;
|
||||
}
|
||||
return block.setSingleFileDisplayName(displayName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SendFilesBox::openDialogToAddFileToAlbum() {
|
||||
const auto show = uiShow();
|
||||
const auto checkResult = [=](const Ui::PreparedList &list) {
|
||||
@@ -1283,6 +1357,40 @@ void SendFilesBox::pushBlock(int from, int till) {
|
||||
_priceTag->update();
|
||||
}
|
||||
}, widget->lifetime());
|
||||
|
||||
struct State {
|
||||
base::unique_qptr<Ui::PopupMenu> menu;
|
||||
};
|
||||
const auto state = widget->lifetime().make_state<State>();
|
||||
base::install_event_filter(widget, [=, from = from, till = till](
|
||||
not_null<QEvent*> e) {
|
||||
if (e->type() == QEvent::ContextMenu) {
|
||||
const auto mouse = static_cast<QContextMenuEvent*>(e.get());
|
||||
if (from >= till || from >= _list.files.size()) {
|
||||
return base::EventFilterResult::Continue;
|
||||
}
|
||||
const auto fileIndex = from;
|
||||
state->menu = base::make_unique_q<Ui::PopupMenu>(
|
||||
widget,
|
||||
_st.tabbed.menu);
|
||||
state->menu->addAction(tr::lng_rename_file(tr::now), [=] {
|
||||
auto &file = _list.files[fileIndex];
|
||||
_show->show(Box(RenameFileBox, file.displayName, [=](
|
||||
QString newName) {
|
||||
const auto displayName = std::move(newName);
|
||||
_list.files[fileIndex].displayName = displayName;
|
||||
if (!setDisplayNameInSingleFilePreview(
|
||||
fileIndex,
|
||||
displayName)) {
|
||||
refreshAllAfterChanges(from);
|
||||
}
|
||||
}));
|
||||
}, &st::menuIconEdit);
|
||||
state->menu->popup(mouse->globalPos());
|
||||
return base::EventFilterResult::Cancel;
|
||||
}
|
||||
return base::EventFilterResult::Continue;
|
||||
}, widget->lifetime());
|
||||
}
|
||||
|
||||
void SendFilesBox::refreshControls(bool initial) {
|
||||
|
||||
@@ -177,6 +177,8 @@ private:
|
||||
void applyChanges();
|
||||
|
||||
[[nodiscard]] QImage generatePriceTagBackground() const;
|
||||
[[nodiscard]] bool setSingleFileDisplayName(
|
||||
const QString &displayName);
|
||||
|
||||
private:
|
||||
base::unique_qptr<Ui::RpWidget> _preview;
|
||||
@@ -243,6 +245,9 @@ private:
|
||||
|
||||
void openDialogToAddFileToAlbum();
|
||||
void refreshAllAfterChanges(int fromItem, Fn<void()> perform = nullptr);
|
||||
[[nodiscard]] bool setDisplayNameInSingleFilePreview(
|
||||
int fileIndex,
|
||||
const QString &displayName);
|
||||
|
||||
void enqueueNextPrepare();
|
||||
void addPreparedAsyncFile(Ui::PreparedFile &&file);
|
||||
|
||||
@@ -473,6 +473,7 @@ FileLoadTask::FileLoadTask(Args &&args)
|
||||
, _to(std::move(args.to))
|
||||
, _album(std::move(args.album))
|
||||
, _filepath(std::move(args.filepath))
|
||||
, _displayName(std::move(args.displayName))
|
||||
, _content(std::move(args.content))
|
||||
, _videoCover(std::move(args.videoCover))
|
||||
, _information(std::move(args.information))
|
||||
@@ -810,7 +811,11 @@ void FileLoadTask::process(ProcessArgs &&args) {
|
||||
QImage goodThumbnail;
|
||||
QByteArray goodThumbnailBytes;
|
||||
|
||||
QVector<MTPDocumentAttribute> attributes(1, MTP_documentAttributeFilename(MTP_string(filename)));
|
||||
auto attributes = QVector<MTPDocumentAttribute>(
|
||||
1,
|
||||
MTP_documentAttributeFilename(MTP_string(_displayName.isEmpty()
|
||||
? filename
|
||||
: _displayName)));
|
||||
|
||||
auto thumbnail = PreparedFileThumbnail();
|
||||
|
||||
|
||||
@@ -233,6 +233,7 @@ public:
|
||||
std::shared_ptr<SendingAlbum> album;
|
||||
bool forceFile = false;
|
||||
uint64 idOverride = 0;
|
||||
QString displayName;
|
||||
};
|
||||
|
||||
struct VoiceArgs {
|
||||
@@ -292,6 +293,7 @@ private:
|
||||
FileLoadTo _to;
|
||||
const std::shared_ptr<SendingAlbum> _album;
|
||||
QString _filepath;
|
||||
QString _displayName;
|
||||
QByteArray _content;
|
||||
std::unique_ptr<FileLoadTask> _videoCover;
|
||||
std::unique_ptr<Ui::PreparedFileInformation> _information;
|
||||
|
||||
@@ -68,6 +68,13 @@ rpl::producer<> AbstractSingleFilePreview::clearCoverRequests() const {
|
||||
return rpl::never<>();
|
||||
}
|
||||
|
||||
void AbstractSingleFilePreview::setDisplayName(const QString &displayName) {
|
||||
auto data = _data;
|
||||
data.name = displayName;
|
||||
setData(data);
|
||||
update();
|
||||
}
|
||||
|
||||
void AbstractSingleFilePreview::prepareThumbFor(
|
||||
Data &data,
|
||||
const QImage &preview) {
|
||||
|
||||
@@ -32,6 +32,7 @@ public:
|
||||
[[nodiscard]] rpl::producer<> modifyRequests() const override;
|
||||
[[nodiscard]] rpl::producer<> editCoverRequests() const override;
|
||||
[[nodiscard]] rpl::producer<> clearCoverRequests() const override;
|
||||
virtual void setDisplayName(const QString &displayName);
|
||||
|
||||
protected:
|
||||
struct Data {
|
||||
|
||||
@@ -15,6 +15,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "ui/ui_utility.h"
|
||||
#include "core/mime_type.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
|
||||
namespace Ui {
|
||||
namespace {
|
||||
|
||||
@@ -23,6 +25,8 @@ constexpr auto kMaxAlbumCount = 10;
|
||||
} // namespace
|
||||
|
||||
PreparedFile::PreparedFile(const QString &path) : path(path) {
|
||||
const auto fileInfo = QFileInfo(path);
|
||||
displayName = fileInfo.fileName();
|
||||
}
|
||||
|
||||
PreparedFile::PreparedFile(PreparedFile &&other) = default;
|
||||
|
||||
@@ -79,6 +79,7 @@ struct PreparedFile {
|
||||
[[nodiscard]] bool isGifv() const;
|
||||
|
||||
QString path;
|
||||
QString displayName;
|
||||
QByteArray content;
|
||||
int64 size = 0;
|
||||
std::unique_ptr<PreparedFileInformation> information;
|
||||
|
||||
@@ -27,6 +27,10 @@ SingleFilePreview::SingleFilePreview(
|
||||
preparePreview(file);
|
||||
}
|
||||
|
||||
void SingleFilePreview::setDisplayName(const QString &displayName) {
|
||||
AbstractSingleFilePreview::setDisplayName(displayName);
|
||||
}
|
||||
|
||||
void SingleFilePreview::preparePreview(const PreparedFile &file) {
|
||||
AbstractSingleFilePreview::Data data;
|
||||
|
||||
@@ -41,13 +45,18 @@ void SingleFilePreview::preparePreview(const PreparedFile &file) {
|
||||
prepareThumbFor(data, preview);
|
||||
const auto filepath = file.path;
|
||||
if (filepath.isEmpty()) {
|
||||
auto filename = "image.png";
|
||||
data.name = filename;
|
||||
const auto fallbackName = u"image.png"_q;
|
||||
const auto displayName = file.displayName.isEmpty()
|
||||
? fallbackName
|
||||
: file.displayName;
|
||||
data.name = displayName;
|
||||
data.statusText = FormatImageSizeText(file.originalDimensions);
|
||||
data.fileIsImage = true;
|
||||
} else {
|
||||
auto fileinfo = QFileInfo(filepath);
|
||||
auto filename = fileinfo.fileName();
|
||||
auto filename = file.displayName.isEmpty()
|
||||
? fileinfo.fileName()
|
||||
: file.displayName;
|
||||
data.fileIsImage = Core::FileIsImage(
|
||||
filename,
|
||||
Core::MimeTypeForFile(fileinfo).name());
|
||||
|
||||
@@ -20,6 +20,7 @@ public:
|
||||
const style::ComposeControls &st,
|
||||
const PreparedFile &file,
|
||||
AttachControls::Type type = AttachControls::Type::Full);
|
||||
void setDisplayName(const QString &displayName) override;
|
||||
|
||||
private:
|
||||
void preparePreview(const PreparedFile &file);
|
||||
|
||||
Reference in New Issue
Block a user