Show choose file box over the passport panel.

This commit is contained in:
John Preston 2018-04-17 19:19:34 +04:00
parent 6c2a39f1fc
commit 67ea175fc6
21 changed files with 170 additions and 47 deletions

View File

@ -83,20 +83,25 @@ void DownloadPathBox::radioChanged(Directory value) {
}
void DownloadPathBox::onEditPath() {
auto initialPath = [] {
const auto initialPath = [] {
if (!Global::DownloadPath().isEmpty() && Global::DownloadPath() != qstr("tmp")) {
return Global::DownloadPath().left(Global::DownloadPath().size() - (Global::DownloadPath().endsWith('/') ? 1 : 0));
}
return QString();
};
FileDialog::GetFolder(lang(lng_download_path_choose), initialPath(), base::lambda_guarded(this, [this](const QString &result) {
}();
const auto handleFolder = [=](const QString &result) {
if (!result.isEmpty()) {
_path = result + '/';
_pathBookmark = psDownloadPathBookmark(_path);
setPathText(QDir::toNativeSeparators(_path));
_group->setValue(Directory::Custom);
}
}));
};
FileDialog::GetFolder(
this,
lang(lng_download_path_choose),
initialPath,
base::lambda_guarded(this, handleFolder));
}
void DownloadPathBox::save() {

View File

@ -13,17 +13,38 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "messenger.h"
bool filedialogGetSaveFile(
QPointer<QWidget> parent,
QString &file,
const QString &caption,
const QString &filter,
const QString &initialPath) {
QStringList files;
QByteArray remoteContent;
bool result = Platform::FileDialog::Get(files, remoteContent, caption, filter, FileDialog::internal::Type::WriteFile, initialPath);
bool result = Platform::FileDialog::Get(
parent,
files,
remoteContent,
caption,
filter,
FileDialog::internal::Type::WriteFile,
initialPath);
file = files.isEmpty() ? QString() : files.at(0);
return result;
}
bool filedialogGetSaveFile(
QString &file,
const QString &caption,
const QString &filter,
const QString &initialPath) {
return filedialogGetSaveFile(
Messenger::Instance().getFileDialogParent(),
file,
caption,
filter,
initialPath);
}
QString filedialogDefaultName(
const QString &prefix,
const QString &extension,
@ -130,6 +151,7 @@ void UnsafeLaunchDefault(const QString &filepath) {
namespace FileDialog {
void GetOpenPath(
QPointer<QWidget> parent,
const QString &caption,
const QString &filter,
base::lambda<void(OpenResult &&result)> callback,
@ -138,6 +160,7 @@ void GetOpenPath(
auto files = QStringList();
auto remoteContent = QByteArray();
const auto success = Platform::FileDialog::Get(
parent,
files,
remoteContent,
caption,
@ -161,6 +184,7 @@ void GetOpenPath(
}
void GetOpenPaths(
QPointer<QWidget> parent,
const QString &caption,
const QString &filter,
base::lambda<void(OpenResult &&result)> callback,
@ -169,6 +193,7 @@ void GetOpenPaths(
auto files = QStringList();
auto remoteContent = QByteArray();
const auto success = Platform::FileDialog::Get(
parent,
files,
remoteContent,
caption,
@ -188,6 +213,7 @@ void GetOpenPaths(
}
void GetWritePath(
QPointer<QWidget> parent,
const QString &caption,
const QString &filter,
const QString &initialPath,
@ -195,7 +221,7 @@ void GetWritePath(
base::lambda<void()> failed) {
InvokeQueued(QApplication::instance(), [=] {
auto file = QString();
if (filedialogGetSaveFile(file, caption, filter, initialPath)) {
if (filedialogGetSaveFile(parent, file, caption, filter, initialPath)) {
if (callback) {
callback(std::move(file));
}
@ -206,6 +232,7 @@ void GetWritePath(
}
void GetFolder(
QPointer<QWidget> parent,
const QString &caption,
const QString &initialPath,
base::lambda<void(QString &&result)> callback,
@ -214,6 +241,7 @@ void GetFolder(
auto files = QStringList();
auto remoteContent = QByteArray();
const auto success = Platform::FileDialog::Get(
parent,
files,
remoteContent,
caption,
@ -244,7 +272,14 @@ void InitLastPathDefault() {
cSetDialogLastPath(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
}
bool GetDefault(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, FileDialog::internal::Type type, QString startFile = QString()) {
bool GetDefault(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
FileDialog::internal::Type type,
QString startFile = QString()) {
if (cDialogLastPath().isEmpty()) {
Platform::FileDialog::InitLastPath();
}

View File

@ -54,22 +54,26 @@ struct OpenResult {
QByteArray remoteContent;
};
void GetOpenPath(
QPointer<QWidget> parent,
const QString &caption,
const QString &filter,
base::lambda<void(OpenResult &&result)> callback,
base::lambda<void()> failed = base::lambda<void()>());
void GetOpenPaths(
QPointer<QWidget> parent,
const QString &caption,
const QString &filter,
base::lambda<void(OpenResult &&result)> callback,
base::lambda<void()> failed = base::lambda<void()>());
void GetWritePath(
QPointer<QWidget> parent,
const QString &caption,
const QString &filter,
const QString &initialPath,
base::lambda<void(QString &&result)> callback,
base::lambda<void()> failed = base::lambda<void()>());
void GetFolder(
QPointer<QWidget> parent,
const QString &caption,
const QString &initialPath,
base::lambda<void(QString &&result)> callback,
@ -89,6 +93,7 @@ enum class Type {
void InitLastPathDefault();
bool GetDefault(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,

View File

@ -1085,6 +1085,7 @@ void InnerWidget::savePhotoToFile(PhotoData *photo) {
auto filter = qsl("JPEG Image (*.jpg);;") + FileDialog::AllFilesFilter();
FileDialog::GetWritePath(
this,
lang(lng_save_photo),
filter,
filedialogDefaultName(qsl("photo"), qsl(".jpg")),

View File

@ -1738,6 +1738,7 @@ void HistoryInner::savePhotoToFile(not_null<PhotoData*> photo) {
auto filter = qsl("JPEG Image (*.jpg);;") + FileDialog::AllFilesFilter();
FileDialog::GetWritePath(
this,
lang(lng_save_photo),
filter,
filedialogDefaultName(

View File

@ -3221,7 +3221,7 @@ void HistoryWidget::chooseAttach() {
auto filter = FileDialog::AllFilesFilter() + qsl(";;Image files (*") + cImgExtensions().join(qsl(" *")) + qsl(")");
FileDialog::GetOpenPaths(lang(lng_choose_files), filter, base::lambda_guarded(this, [this](FileDialog::OpenResult &&result) {
FileDialog::GetOpenPaths(this, lang(lng_choose_files), filter, base::lambda_guarded(this, [this](FileDialog::OpenResult &&result) {
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
return;
}

View File

@ -49,6 +49,7 @@ void SavePhotoToFile(not_null<PhotoData*> photo) {
}
FileDialog::GetWritePath(
Messenger::Instance().getFileDialogParent(),
lang(lng_save_photo),
qsl("JPEG Image (*.jpg);;") + FileDialog::AllFilesFilter(),
filedialogDefaultName(qsl("photo"), qsl(".jpg")),

View File

@ -231,7 +231,7 @@ void CloudManager::switchToLanguage(QString id) {
void CloudManager::performSwitchToCustom() {
auto filter = qsl("Language files (*.strings)");
auto title = qsl("Choose language .strings file");
FileDialog::GetOpenPath(title, filter, [weak = base::make_weak(this)](const FileDialog::OpenResult &result) {
FileDialog::GetOpenPath(Messenger::Instance().getFileDialogParent(), title, filter, [weak = base::make_weak(this)](const FileDialog::OpenResult &result) {
if (!weak || result.paths.isEmpty()) {
return;
}

View File

@ -851,6 +851,7 @@ void MediaView::onSaveAs() {
psBringToBack(this);
auto filter = qsl("JPEG Image (*.jpg);;") + FileDialog::AllFilesFilter();
FileDialog::GetWritePath(
this,
lang(lng_save_photo),
filter,
filedialogDefaultName(

View File

@ -833,13 +833,11 @@ void PanelController::editWithUpload(int index, int documentIndex) {
Expects(documentIndex >= 0
&& documentIndex < _scopes[index].documents.size());
EditScans::ChooseScan(
base::lambda_guarded(_panel.get(),
[=](QByteArray &&content) {
base::take(_scopeDocumentTypeBox);
editScope(index, documentIndex);
uploadScan(std::move(content));
}));
EditScans::ChooseScan(_panel.get(), [=](QByteArray &&content) {
base::take(_scopeDocumentTypeBox);
editScope(index, documentIndex);
uploadScan(std::move(content));
});
}
void PanelController::editScope(int index, int documentIndex) {

View File

@ -423,22 +423,27 @@ void EditScans::chooseScan() {
_controller->showToast(lang(lng_passport_scans_limit_reached));
return;
}
ChooseScan(base::lambda_guarded(this, [=](QByteArray &&content) {
ChooseScan(this, [=](QByteArray &&content) {
_controller->uploadScan(std::move(content));
}));
});
}
void EditScans::chooseSelfie() {
ChooseScan(base::lambda_guarded(this, [=](QByteArray &&content) {
ChooseScan(this, [=](QByteArray &&content) {
_controller->uploadSelfie(std::move(content));
}));
});
}
void EditScans::ChooseScan(base::lambda<void(QByteArray&&)> callback) {
void EditScans::ChooseScan(
QPointer<QWidget> parent,
base::lambda<void(QByteArray&&)> callback) {
Expects(parent != nullptr);
const auto filter = FileDialog::AllFilesFilter()
+ qsl(";;Image files (*")
+ cImgExtensions().join(qsl(" *"))
+ qsl(")");
const auto guardedCallback = base::lambda_guarded(parent, callback);
const auto processFile = [=](FileDialog::OpenResult &&result) {
if (result.paths.size() == 1) {
auto content = [&] {
@ -449,13 +454,14 @@ void EditScans::ChooseScan(base::lambda<void(QByteArray&&)> callback) {
return f.readAll();
}();
if (!content.isEmpty()) {
callback(std::move(content));
guardedCallback(std::move(content));
}
} else if (!result.remoteContent.isEmpty()) {
callback(std::move(result.remoteContent));
guardedCallback(std::move(result.remoteContent));
}
};
FileDialog::GetOpenPath(
parent,
lang(lng_passport_choose_image),
filter,
processFile);

View File

@ -41,7 +41,9 @@ public:
base::optional<int> validateGetErrorTop();
static void ChooseScan(base::lambda<void(QByteArray&&)> callback);
static void ChooseScan(
QPointer<QWidget> parent,
base::lambda<void(QByteArray&&)> callback);
private:
void setupContent(const QString &header);

View File

@ -139,8 +139,14 @@ bool PreviewSupported() {
&& (Libs::gdk_pixbuf_new_from_file_at_size != nullptr);
}
bool GetNative(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, Type type, QString startFile) {
auto parent = Messenger::Instance().getFileDialogParent();
bool GetNative(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
Type type,
QString startFile) {
internal::GtkFileDialog dialog(parent, caption, QString(), filter);
dialog.setModal(true);
@ -185,13 +191,34 @@ bool GetNative(QStringList &files, QByteArray &remoteContent, const QString &cap
} // namespace
bool Get(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, Type type, QString startFile) {
bool Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
Type type,
QString startFile) {
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
if (NativeSupported()) {
return GetNative(files, remoteContent, caption, filter, type, startFile);
return GetNative(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
}
#endif // !TDESKTOP_DISABLE_GTK_INTEGRATION
return ::FileDialog::internal::GetDefault(files, remoteContent, caption, filter, type, startFile);
return ::FileDialog::internal::GetDefault(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
}
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION

View File

@ -27,8 +27,22 @@ inline void InitLastPath() {
::FileDialog::internal::InitLastPathDefault();
}
inline bool Get(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, ::FileDialog::internal::Type type, QString startFile) {
return ::FileDialog::internal::GetDefault(files, remoteContent, caption, filter, type, startFile);
inline bool Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
::FileDialog::internal::Type type,
QString startFile) {
return ::FileDialog::internal::GetDefault(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
}
} // namespace FileDialog

View File

@ -29,7 +29,14 @@ namespace FileDialog {
void InitLastPath();
bool Get(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, ::FileDialog::internal::Type type, QString startFile = QString());
bool Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
::FileDialog::internal::Type type,
QString startFile = QString());
} // namespace FileDialog
} // namespace Platform

View File

@ -333,7 +333,14 @@ void InitLastPath() {
}
}
bool Get(QStringList &files, QByteArray &remoteContent, const QString &caption, const QString &filter, ::FileDialog::internal::Type type, QString startFile) {
bool Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
::FileDialog::internal::Type type,
QString startFile) {
if (cDialogLastPath().isEmpty()) {
Platform::FileDialog::InitLastPath();
}
@ -344,7 +351,6 @@ bool Get(QStringList &files, QByteArray &remoteContent, const QString &caption,
// that forced file icon and maybe other properties being resolved and this was
// a blocking operation.
auto helperPath = cDialogHelperPathFinal();
auto parent = Messenger::Instance().getFileDialogParent();
QFileDialog dialog(parent, caption, helperPath, filter);
dialog.setModal(true);

View File

@ -231,7 +231,7 @@ void BackgroundWidget::onChooseFromFile() {
auto imgExtensions = cImgExtensions();
auto filters = QStringList(qsl("Theme files (*.tdesktop-theme *.tdesktop-palette *") + imgExtensions.join(qsl(" *")) + qsl(")"));
filters.push_back(FileDialog::AllFilesFilter());
FileDialog::GetOpenPath(lang(lng_choose_image), filters.join(qsl(";;")), base::lambda_guarded(this, [this](const FileDialog::OpenResult &result) {
const auto callback = [=](const FileDialog::OpenResult &result) {
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
return;
}
@ -263,7 +263,12 @@ void BackgroundWidget::onChooseFromFile() {
Window::Theme::Background()->setImage(Window::Theme::kCustomBackground, std::move(img));
_tile->setChecked(false);
_background->updateImage();
}));
};
FileDialog::GetOpenPath(
this,
lang(lng_choose_image),
filters.join(qsl(";;")),
base::lambda_guarded(this, callback));
}
void BackgroundWidget::onEditTheme() {

View File

@ -343,7 +343,7 @@ void CoverWidget::refreshStatusText() {
void CoverWidget::chooseNewPhoto() {
auto imageExtensions = cImgExtensions();
auto filter = qsl("Image files (*") + imageExtensions.join(qsl(" *")) + qsl(");;") + FileDialog::AllFilesFilter();
FileDialog::GetOpenPath(lang(lng_choose_image), filter, base::lambda_guarded(this, [this](const FileDialog::OpenResult &result) {
const auto callback = [=](const FileDialog::OpenResult &result) {
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
return;
}
@ -356,7 +356,12 @@ void CoverWidget::chooseNewPhoto() {
}
showSetPhotoBox(img);
}));
};
FileDialog::GetOpenPath(
this,
lang(lng_choose_image),
filter,
base::lambda_guarded(this, callback));
}
void CoverWidget::editName() {

View File

@ -90,7 +90,7 @@ void fillCodes() {
}
});
Codes.insert(qsl("loadcolors"), [] {
FileDialog::GetOpenPath("Open palette file", "Palette (*.tdesktop-palette)", [](const FileDialog::OpenResult &result) {
FileDialog::GetOpenPath(Messenger::Instance().getFileDialogParent(), "Open palette file", "Palette (*.tdesktop-palette)", [](const FileDialog::OpenResult &result) {
if (!result.paths.isEmpty()) {
Window::Theme::Apply(result.paths.front());
}
@ -108,7 +108,7 @@ void fillCodes() {
}));
});
Codes.insert(qsl("endpoints"), [] {
FileDialog::GetOpenPath("Open DC endpoints", "DC Endpoints (*.tdesktop-endpoints)", [](const FileDialog::OpenResult &result) {
FileDialog::GetOpenPath(Messenger::Instance().getFileDialogParent(), "Open DC endpoints", "DC Endpoints (*.tdesktop-endpoints)", [](const FileDialog::OpenResult &result) {
if (!result.paths.isEmpty()) {
if (!Messenger::Instance().mtp()->dcOptions()->loadFromFile(result.paths.front())) {
Ui::show(Box<InformBox>("Could not load endpoints :( Errors in 'log.txt'."));
@ -136,7 +136,7 @@ void fillCodes() {
return;
}
FileDialog::GetOpenPath("Open audio file", audioFilters, [key](const FileDialog::OpenResult &result) {
FileDialog::GetOpenPath(Messenger::Instance().getFileDialogParent(), "Open audio file", audioFilters, [key](const FileDialog::OpenResult &result) {
if (AuthSession::Exists() && !result.paths.isEmpty()) {
auto track = Media::Audio::Current().createTrack();
track->fillFromFile(result.paths.front());

View File

@ -95,7 +95,10 @@ void SuggestPhotoFile(
}
template <typename Callback>
void ShowChoosePhotoBox(PeerId peerForCrop, Callback &&callback) {
void ShowChoosePhotoBox(
QPointer<QWidget> parent,
PeerId peerForCrop,
Callback &&callback) {
auto imgExtensions = cImgExtensions();
auto filter = qsl("Image files (*")
+ imgExtensions.join(qsl(" *"))
@ -108,6 +111,7 @@ void ShowChoosePhotoBox(PeerId peerForCrop, Callback &&callback) {
SuggestPhotoFile(result, peerForCrop, std::move(callback));
};
FileDialog::GetOpenPath(
parent,
lang(lng_choose_image),
filter,
std::move(handleChosenPhoto));
@ -470,7 +474,7 @@ void UserpicButton::changePhotoLazy() {
auto callback = base::lambda_guarded(
this,
[this](QImage &&image) { setImage(std::move(image)); });
ShowChoosePhotoBox(_peerForCrop, std::move(callback));
ShowChoosePhotoBox(this, _peerForCrop, std::move(callback));
}
void UserpicButton::uploadNewPeerPhoto() {
@ -482,7 +486,7 @@ void UserpicButton::uploadNewPeerPhoto() {
_peer->id
);
});
ShowChoosePhotoBox(_peerForCrop, std::move(callback));
ShowChoosePhotoBox(this, _peerForCrop, std::move(callback));
}
void UserpicButton::openPeerPhoto() {

View File

@ -621,7 +621,7 @@ void ThemeExportBox::updateThumbnail() {
}
void ThemeExportBox::chooseBackgroundFromFile() {
FileDialog::GetOpenPath(lang(lng_theme_editor_choose_image), "Image files (*.jpeg *.jpg *.png)", base::lambda_guarded(this, [this](const FileDialog::OpenResult &result) {
FileDialog::GetOpenPath(this, lang(lng_theme_editor_choose_image), "Image files (*.jpeg *.jpg *.png)", base::lambda_guarded(this, [this](const FileDialog::OpenResult &result) {
auto content = result.remoteContent;
if (!result.paths.isEmpty()) {
QFile f(result.paths.front());
@ -651,7 +651,7 @@ void ThemeExportBox::exportTheme() {
auto caption = lang(lng_theme_editor_choose_name);
auto filter = "Themes (*.tdesktop-theme)";
auto name = "awesome.tdesktop-theme";
FileDialog::GetWritePath(caption, filter, name, base::lambda_guarded(this, [this](const QString &path) {
FileDialog::GetWritePath(this, caption, filter, name, base::lambda_guarded(this, [this](const QString &path) {
zlib::FileToWrite zip;
zip_fileinfo zfi = { { 0, 0, 0, 0, 0, 0 }, 0, 0, 0 };
@ -787,7 +787,7 @@ void Editor::paintEvent(QPaintEvent *e) {
void Editor::Start() {
auto palettePath = Local::themePaletteAbsolutePath();
if (palettePath.isEmpty()) {
FileDialog::GetWritePath(lang(lng_theme_editor_save_palette), "Palette (*.tdesktop-palette)", "colors.tdesktop-palette", [](const QString &path) {
FileDialog::GetWritePath(App::wnd(), lang(lng_theme_editor_save_palette), "Palette (*.tdesktop-palette)", "colors.tdesktop-palette", [](const QString &path) {
if (!Local::copyThemeColorsToPalette(path)) {
writeDefaultPalette(path);
}