added Chat Folders for Downloads

This commit is contained in:
danalec 2025-03-13 14:53:41 -03:00
parent 57aebdd300
commit 31a901cae1
4 changed files with 70 additions and 18 deletions

View file

@ -4094,6 +4094,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_translate_undo" = "Undo";
"lng_downloads_section" = "Downloads";
"lng_downloads_settings_save_to_chat_folders" = "Save files to chat folders";
"lng_downloads_settings_save_to_chat_folders_about" = "When enabled, files will be saved to separate folders named after their source chat.";
"lng_downloads_view_in_chat" = "View in chat";
"lng_downloads_view_in_section" = "View in downloads";
"lng_downloads_delete_sure_one" = "Do you want to delete this file?";

View file

@ -954,6 +954,9 @@ public:
void resetOnLastLogout();
[[nodiscard]] bool saveToFoldersByChat() const { return _saveToFoldersByChat; }
void setSaveToFoldersByChat(bool value) { _saveToFoldersByChat = value; }
private:
void resolveRecentEmoji() const;
@ -1094,7 +1097,7 @@ private:
QByteArray _photoEditorBrush;
bool _saveToFoldersByChat = false; // Default to disabled
};
} // namespace Core
} // namespace Core

View file

@ -210,11 +210,10 @@ QString FileNameForSave(
return result;
}
QString DocumentFileNameForSave(
not_null<const DocumentData*> data,
bool forceSavingAs,
const QString &already,
const QDir &dir) {
QString DocumentFileNameForSave(not_null<const DocumentData *> data,
bool forceSavingAs,
const QString &already,
const QDir &dir) {
auto alreadySavingFilename = data->loadingFilePath();
if (!alreadySavingFilename.isEmpty()) {
return alreadySavingFilename;
@ -253,20 +252,53 @@ QString DocumentFileNameForSave(
} else {
filter = mimeType.filterString() + u";;"_q + FileDialog::AllFilesFilter();
}
caption = data->isAudioFile()
? tr::lng_save_audio_file(tr::now)
: tr::lng_save_file(tr::now);
caption = data->isAudioFile() ? tr::lng_save_audio_file(tr::now) : tr::lng_save_file(tr::now);
prefix = u"doc"_q;
}
return FileNameForSave(
&data->session(),
caption,
filter,
prefix,
name,
forceSavingAs,
dir);
// Try to get chat information
QString chatFolder;
// Get window controller
if (const auto controller = data->session().windows().empty() ? nullptr : data->session().windows().front().get()) {
// Get the active chat history
if (const auto history = controller->activeChatCurrent().history()) {
if (const auto peer = history->peer) {
chatFolder = peer->name();
// Sanitize folder name
chatFolder.replace(QRegularExpression("[\\\\/:*?\"<>|]"), "_");
}
}
}
// Get base download path
auto path = [&]
{
const auto path = Core::App().settings().downloadPath();
if (path.isEmpty()) {
return File::DefaultDownloadPath(&data->session());
} else if (path == FileDialog::Tmp()) {
return data->session().local().tempDirectory();
} else {
return path;
}
}();
// Create chat subfolder if we have a chat name and setting is enabled
if (!chatFolder.isEmpty() && !path.isEmpty() && !forceSavingAs && Core::App().settings().saveToFoldersByChat()) {
// Create full path with chat subfolder
QDir baseDir(path);
if (!baseDir.exists(chatFolder)) {
baseDir.mkdir(chatFolder);
}
QDir chatDir(path + "/" + chatFolder);
// Use the chat directory instead of the base directory
return FileNameForSave(&data->session(), caption, filter, prefix, name, forceSavingAs, chatDir);
}
return FileNameForSave(&data->session(), caption, filter, prefix, name, forceSavingAs, dir);
}
Data::FileOrigin StickerData::setOrigin() const {

View file

@ -1096,6 +1096,21 @@ void SetupDataStorage(
const auto showpath = container->lifetime(
).make_state<rpl::event_stream<bool>>();
container->add(object_ptr<Ui::Checkbox>(
container,
tr::lng_downloads_settings_save_to_chat_folders(),
Core::App().settings().saveToFoldersByChat(),
st::settingsCheckbox),
st::settingsCheckboxPadding)->checkedChanges() |
rpl::start_with_next(
[=](bool checked) {
Core::App().settings().setSaveToFoldersByChat(checked);
Core::App().saveSettingsDelayed();
},
container->lifetime());
Ui::AddDividerText(container, tr::lng_downloads_settings_save_to_chat_folders_about());
const auto path = container->add(
object_ptr<Ui::SlideWrap<Button>>(
container,