File indexing completed on 2025-02-16 04:48:44
0001 /* 0002 * filedialog.cpp - file save dialogue, with append option & confirm overwrite 0003 * Program: kalarm 0004 * SPDX-FileCopyrightText: 2009 David Jarvie <djarvie@kde.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "filedialog.h" 0010 0011 #include "autoqpointer.h" 0012 #include "kalarm_debug.h" 0013 0014 #include <KFileWidget> 0015 #include <KLocalizedString> 0016 #include <KRecentDocument> 0017 0018 #include <QCheckBox> 0019 0020 0021 QCheckBox* FileDialog::mAppendCheck = nullptr; 0022 0023 0024 FileDialog::FileDialog(const QUrl& startDir, const QList<KFileFilter>& filters, QWidget* parent) 0025 : KFileCustomDialog(parent) 0026 { 0027 fileWidget()->setFilters(filters); 0028 fileWidget()->setStartDir(startDir); 0029 } 0030 0031 QString FileDialog::getSaveFileName(const QUrl& dir, const QList<KFileFilter>& filters, QWidget* parent, const QString& caption, bool* append) 0032 { 0033 bool defaultDir = dir.isEmpty(); 0034 bool specialDir = !defaultDir && dir.scheme() == QLatin1String("kfiledialog"); 0035 // Use AutoQPointer to guard against crash on application exit while 0036 // the dialogue is still open. It prevents double deletion (both on 0037 // deletion of parent, and on return from this function). 0038 AutoQPointer<FileDialog> dlg = new FileDialog(specialDir ? dir : QUrl(), filters, parent); 0039 if (!specialDir && !defaultDir) 0040 { 0041 if (!dir.isLocalFile()) 0042 qCWarning(KALARM_LOG) << "FileDialog::getSaveFileName called with non-local start dir " << dir; 0043 dlg->fileWidget()->setSelectedUrl(dir); // may also be a filename 0044 } 0045 dlg->setOperationMode(KFileWidget::Saving); 0046 dlg->fileWidget()->setMode(KFile::File | KFile::LocalOnly); 0047 dlg->fileWidget()->setConfirmOverwrite(true); 0048 if (!caption.isEmpty()) 0049 dlg->setWindowTitle(caption); 0050 mAppendCheck = nullptr; 0051 if (append) 0052 { 0053 // Show an 'append' option in the dialogue. 0054 // Note that the dialogue will take ownership of the QCheckBox. 0055 mAppendCheck = new QCheckBox(i18nc("@option:check", "Append to existing file"), nullptr); 0056 connect(mAppendCheck, &QCheckBox::toggled, dlg.data(), &FileDialog::appendToggled); 0057 dlg->setCustomWidget(mAppendCheck); 0058 *append = false; 0059 } 0060 dlg->setWindowModality(Qt::WindowModal); 0061 dlg->exec(); 0062 if (!dlg) 0063 return {}; // dialogue was deleted 0064 0065 QString filename = dlg->fileWidget()->selectedFile(); 0066 if (!filename.isEmpty()) 0067 { 0068 if (append) 0069 *append = mAppendCheck->isChecked(); 0070 KRecentDocument::add(QUrl::fromLocalFile(filename)); 0071 } 0072 return filename; 0073 } 0074 0075 void FileDialog::appendToggled(bool ticked) 0076 { 0077 fileWidget()->setConfirmOverwrite(!ticked); 0078 } 0079 0080 #include "moc_filedialog.cpp" 0081 0082 // vim: et sw=4: