From 156c3d288c03919ecb694acd6209661ceddb554c Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 20 Jun 2018 19:12:47 +0100 Subject: [PATCH] Allow selecting export folder. --- Telegram/Resources/langs/lang.strings | 1 + .../SourceFiles/boxes/download_path_box.cpp | 2 +- Telegram/SourceFiles/core/file_utilities.cpp | 12 ++++++++---- Telegram/SourceFiles/core/launcher.cpp | 3 ++- .../SourceFiles/export/export_controller.cpp | 5 +++-- .../export/view/export_view_settings.cpp | 19 +++++++++++-------- .../export/view/export_view_settings.h | 3 ++- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index a88615956633f..55bbd26655398 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1696,6 +1696,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_export_finished" = "Export is finished."; "lng_export_total_files" = "Total files: {count}."; "lng_export_total_size" = "Total size: {size}."; +"lng_export_folder" = "Choose export folder"; // Wnd specific diff --git a/Telegram/SourceFiles/boxes/download_path_box.cpp b/Telegram/SourceFiles/boxes/download_path_box.cpp index 96eb358642a87..596591c6881df 100644 --- a/Telegram/SourceFiles/boxes/download_path_box.cpp +++ b/Telegram/SourceFiles/boxes/download_path_box.cpp @@ -91,7 +91,7 @@ void DownloadPathBox::onEditPath() { }(); const auto handleFolder = [=](const QString &result) { if (!result.isEmpty()) { - _path = result + '/'; + _path = result.endsWith('/') ? result : (result + '/'); _pathBookmark = psDownloadPathBookmark(_path); setPathText(QDir::toNativeSeparators(_path)); _group->setValue(Directory::Custom); diff --git a/Telegram/SourceFiles/core/file_utilities.cpp b/Telegram/SourceFiles/core/file_utilities.cpp index 5c8e197260c22..91b64cc9223b9 100644 --- a/Telegram/SourceFiles/core/file_utilities.cpp +++ b/Telegram/SourceFiles/core/file_utilities.cpp @@ -76,8 +76,10 @@ QString filedialogDefaultName( if (skipExistance) { name = base + extension; } else { - QDir dir(directoryPath); - QString nameBase = dir.absolutePath() + '/' + base; + QDir directory(directoryPath); + const auto dir = directory.absolutePath(); + const auto nameBase = (dir.endsWith('/') ? dir : (dir + '/')) + + base; name = nameBase + extension; for (int i = 0; QFileInfo(name).exists(); ++i) { name = nameBase + qsl(" (%1)").arg(i + 2) + extension; @@ -90,14 +92,16 @@ QString filedialogNextFilename( const QString &name, const QString &cur, const QString &path) { - QDir dir(path.isEmpty() ? cDialogLastPath() : path); + QDir directory(path.isEmpty() ? cDialogLastPath() : path); int32 extIndex = name.lastIndexOf('.'); QString prefix = name, extension; if (extIndex >= 0) { extension = name.mid(extIndex); prefix = name.mid(0, extIndex); } - QString nameBase = dir.absolutePath() + '/' + prefix, result = nameBase + extension; + const auto dir = directory.absolutePath(); + const auto nameBase = (dir.endsWith('/') ? dir : (dir + '/')) + prefix; + auto result = nameBase + extension; for (int i = 0; result.toLower() != cur.toLower() && QFileInfo(result).exists(); ++i) { result = nameBase + qsl(" (%1)").arg(i + 2) + extension; } diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index 5c97c40eb68b5..2078858980bb7 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -151,7 +151,8 @@ void Launcher::prepareSettings() { info = info.symLinkTarget(); } if (info.exists()) { - gExeDir = info.absoluteDir().absolutePath() + '/'; + const auto dir = info.absoluteDir().absolutePath(); + gExeDir = (dir.endsWith('/') ? dir : (dir + '/')); gExeName = info.fileName(); } } diff --git a/Telegram/SourceFiles/export/export_controller.cpp b/Telegram/SourceFiles/export/export_controller.cpp index 8b33a81eb1365..7c5bbfc8a1bb2 100644 --- a/Telegram/SourceFiles/export/export_controller.cpp +++ b/Telegram/SourceFiles/export/export_controller.cpp @@ -232,11 +232,12 @@ bool Controller::normalizePath() { }; QDir folder(_settings.path); const auto path = folder.absolutePath(); - _settings.path = path + '/'; + _settings.path = path.endsWith('/') ? path : (path + '/'); if (!folder.exists()) { return check(); } - const auto list = folder.entryInfoList(); + const auto mode = QDir::AllEntries | QDir::NoDotAndDotDot; + const auto list = folder.entryInfoList(mode); if (list.isEmpty()) { return true; } diff --git a/Telegram/SourceFiles/export/view/export_view_settings.cpp b/Telegram/SourceFiles/export/view/export_view_settings.cpp index ea4a4b7749c7c..b8fa90f80c24e 100644 --- a/Telegram/SourceFiles/export/view/export_view_settings.cpp +++ b/Telegram/SourceFiles/export/view/export_view_settings.cpp @@ -18,6 +18,7 @@ For license and copyright information please follow this link: #include "ui/wrap/slide_wrap.h" #include "ui/wrap/fade_wrap.h" #include "platform/platform_specific.h" +#include "core/file_utilities.h" #include "styles/style_widgets.h" #include "styles/style_export.h" #include "styles/style_boxes.h" @@ -311,7 +312,7 @@ void SettingsWidget::refreshButtons(not_null container) { : nullptr; if (start) { start->show(); - _startClicks = start->clicks(); + start->addClickHandler([=] { chooseFolder(); }); container->sizeValue( ) | rpl::start_with_next([=](QSize size) { @@ -339,14 +340,16 @@ void SettingsWidget::refreshButtons(not_null container) { }, cancel->lifetime()); } +void SettingsWidget::chooseFolder() { + const auto ready = [=](QString &&result) { + _data.path = result; + _startClicks.fire(base::duplicate(_data)); + }; + FileDialog::GetFolder(this, lang(lng_export_folder), _data.path, ready); +} + rpl::producer SettingsWidget::startClicks() const { - return _startClicks.value( - ) | rpl::map([](Wrap &&wrap) { - return std::move(wrap.value); - }) | rpl::flatten_latest( - ) | rpl::map([=] { - return _data; - }); + return _startClicks.events(); } rpl::producer<> SettingsWidget::cancelClicks() const { diff --git a/Telegram/SourceFiles/export/view/export_view_settings.h b/Telegram/SourceFiles/export/view/export_view_settings.h index e7c0ba79b25d9..d5ff669d94392 100644 --- a/Telegram/SourceFiles/export/view/export_view_settings.h +++ b/Telegram/SourceFiles/export/view/export_view_settings.h @@ -31,6 +31,7 @@ class SettingsWidget : public Ui::RpWidget { using MediaTypes = MediaSettings::Types; void setupContent(); + void chooseFolder(); void refreshButtons(not_null container); void createSizeSlider(not_null container); @@ -43,7 +44,7 @@ class SettingsWidget : public Ui::RpWidget { rpl::producer<> value; }; - rpl::variable _startClicks; + rpl::event_stream _startClicks; rpl::variable _cancelClicks; rpl::event_stream _dataTypesChanges;