KIO

kencodingfiledialog.cpp
1// -*- c++ -*-
2/*
3 This file is part of the KDE libraries
4 SPDX-FileCopyrightText: 2003 Joseph Wenninger <jowenn@kde.org>
5 SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@freemail.hu>
6 SPDX-FileCopyrightText: 2013 Teo Mrnjavac <teo@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "kencodingfiledialog.h"
12
13#include "kfilewidget.h"
14
15#include <defaults-kfile.h>
16
17#include <KConfigGroup>
18#include <KLocalizedString>
19#include <KSharedConfig>
20#include <KWindowConfig>
21#include <krecentdocument.h>
22
23#include <QBoxLayout>
24#include <QComboBox>
25#include <QPushButton>
26#include <QStringDecoder>
27#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
28#include <QTextCodec>
29#endif
30
31struct KEncodingFileDialogPrivate {
32 KEncodingFileDialogPrivate()
33 : cfgGroup(KSharedConfig::openConfig(), ConfigGroup)
34 {
35 }
36
37 QComboBox *encoding;
38 KFileWidget *w;
39 KConfigGroup cfgGroup;
40};
41
42KEncodingFileDialog::KEncodingFileDialog(const QUrl &startDir,
43 const QString &encoding,
44 const QString &filter,
45 const QString &title,
47 QWidget *parent)
48 : QDialog(parent, Qt::Dialog)
49 , d(new KEncodingFileDialogPrivate)
50{
51 d->w = new KFileWidget(startDir, this);
52 d->w->setFilters(KFileFilter::fromFilterString(filter));
53 if (type == QFileDialog::AcceptOpen) {
54 d->w->setOperationMode(KFileWidget::Opening);
55 } else {
56 d->w->setOperationMode(KFileWidget::Saving);
57 }
58
59 setWindowTitle(title);
60 // ops->clearHistory();
61
63
64 QBoxLayout *mainLayout = new QVBoxLayout(this);
65 mainLayout->addWidget(d->w);
66
67 d->w->okButton()->show();
68 connect(d->w->okButton(), &QAbstractButton::clicked, this, &KEncodingFileDialog::slotOk);
69 d->w->cancelButton()->show();
70 connect(d->w->cancelButton(), &QAbstractButton::clicked, this, &KEncodingFileDialog::slotCancel);
71 connect(d->w, &KFileWidget::accepted, this, &KEncodingFileDialog::accept);
72
73 d->encoding = new QComboBox(this);
74 d->w->setCustomWidget(i18n("Encoding:"), d->encoding);
75
76 d->encoding->clear();
77 QByteArray sEncoding = encoding.toUtf8();
78#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
79 const auto systemEncoding = QTextCodec::codecForLocale()->name();
80#else
81 const auto systemEncoding = QStringDecoder(QStringDecoder::System).name();
82#endif
83 if (sEncoding.isEmpty() || sEncoding == "System") {
84 sEncoding = systemEncoding;
85 }
86
87#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
88 auto encodings = QTextCodec::availableCodecs();
89#else
90 auto encodings = QStringDecoder::availableCodecs();
91#endif
92 std::sort(encodings.begin(), encodings.end(), [](auto &a, auto &b) {
93 return (a.compare(b, Qt::CaseInsensitive) < 0);
94 });
95
96 int insert = 0;
97 int system = 0;
98 bool foundRequested = false;
99 for (const auto &encoding : encodings) {
100#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
101 QTextCodec *codecForEnc = QTextCodec::codecForName(encoding);
102
103 if (codecForEnc) {
104 d->encoding->addItem(QString::fromUtf8(encoding));
105 auto codecName = codecForEnc->name();
106 if ((codecName == sEncoding) || (encoding == sEncoding)) {
107 d->encoding->setCurrentIndex(insert);
108 foundRequested = true;
109 }
110
111 if ((codecName == systemEncoding) || (encoding == systemEncoding)) {
112 system = insert;
113 }
114 insert++;
115 }
116#else
117 d->encoding->addItem(encoding);
118 const auto codecName = QStringDecoder(encoding.toUtf8().constData()).name();
119 if ((codecName == sEncoding) || (encoding.toUtf8() == sEncoding)) {
120 d->encoding->setCurrentIndex(insert);
121 foundRequested = true;
122 }
123
124 if ((codecName == systemEncoding) || (encoding.toUtf8() == systemEncoding)) {
125 system = insert;
126 }
127 insert++;
128#endif
129 }
130
131 if (!foundRequested) {
132 d->encoding->setCurrentIndex(system);
133 }
134}
135
136KEncodingFileDialog::~KEncodingFileDialog() = default;
137
138QString KEncodingFileDialog::selectedEncoding() const
139{
140 if (d->encoding) {
141 return d->encoding->currentText();
142 } else {
143 return QString();
144 }
145}
146
147KEncodingFileDialog::Result
148KEncodingFileDialog::getOpenFileNameAndEncoding(const QString &encoding, const QUrl &startDir, const QString &filter, QWidget *parent, const QString &title)
149{
150 KEncodingFileDialog dlg(startDir, encoding, filter, title.isNull() ? i18n("Open") : title, QFileDialog::AcceptOpen, parent);
151
152 dlg.d->w->setMode(KFile::File | KFile::LocalOnly);
153 dlg.exec();
154
155 Result res;
156 res.fileNames << dlg.d->w->selectedFile();
157 res.encoding = dlg.selectedEncoding();
158 return res;
159}
160
161KEncodingFileDialog::Result
162KEncodingFileDialog::getOpenFileNamesAndEncoding(const QString &encoding, const QUrl &startDir, const QString &filter, QWidget *parent, const QString &title)
163{
164 KEncodingFileDialog dlg(startDir, encoding, filter, title.isNull() ? i18n("Open") : title, QFileDialog::AcceptOpen, parent);
165 dlg.d->w->setMode(KFile::Files | KFile::LocalOnly);
166 dlg.exec();
167
168 Result res;
169 res.fileNames = dlg.d->w->selectedFiles();
170 res.encoding = dlg.selectedEncoding();
171 return res;
172}
173
174KEncodingFileDialog::Result
175KEncodingFileDialog::getOpenUrlAndEncoding(const QString &encoding, const QUrl &startDir, const QString &filter, QWidget *parent, const QString &title)
176{
177 KEncodingFileDialog dlg(startDir, encoding, filter, title.isNull() ? i18n("Open") : title, QFileDialog::AcceptOpen, parent);
178
179 dlg.d->w->setMode(KFile::File);
180 dlg.exec();
181
182 Result res;
183 res.URLs << dlg.d->w->selectedUrl();
184 res.encoding = dlg.selectedEncoding();
185 return res;
186}
187
188KEncodingFileDialog::Result
189KEncodingFileDialog::getOpenUrlsAndEncoding(const QString &encoding, const QUrl &startDir, const QString &filter, QWidget *parent, const QString &title)
190{
191 KEncodingFileDialog dlg(startDir, encoding, filter, title.isNull() ? i18n("Open") : title, QFileDialog::AcceptOpen, parent);
192
193 dlg.d->w->setMode(KFile::Files);
194 dlg.exec();
195
196 Result res;
197 res.URLs = dlg.d->w->selectedUrls();
198 res.encoding = dlg.selectedEncoding();
199 return res;
200}
201
202KEncodingFileDialog::Result
203KEncodingFileDialog::getSaveFileNameAndEncoding(const QString &encoding, const QUrl &dir, const QString &filter, QWidget *parent, const QString &title)
204{
205 KEncodingFileDialog dlg(dir, encoding, filter, title.isNull() ? i18n("Save As") : title, QFileDialog::AcceptSave, parent);
206 dlg.d->w->setMode(KFile::File);
207 dlg.exec();
208
209 QString filename = dlg.d->w->selectedFile();
210 if (!filename.isEmpty()) {
212 }
213
214 Result res;
215 res.fileNames << filename;
216 res.encoding = dlg.selectedEncoding();
217 return res;
218}
219
220KEncodingFileDialog::Result
221KEncodingFileDialog::getSaveUrlAndEncoding(const QString &encoding, const QUrl &dir, const QString &filter, QWidget *parent, const QString &title)
222{
223 KEncodingFileDialog dlg(dir, encoding, filter, title.isNull() ? i18n("Save As") : title, QFileDialog::AcceptSave, parent);
224 dlg.d->w->setMode(KFile::File);
225
226 Result res;
227 if (dlg.exec() == QDialog::Accepted) {
228 QUrl url = dlg.d->w->selectedUrl();
229 if (url.isValid()) {
231 }
232 res.URLs << url;
233 res.encoding = dlg.selectedEncoding();
234 }
235 return res;
236}
237
238QSize KEncodingFileDialog::sizeHint() const
239{
240 return d->w->dialogSizeHint();
241}
242
243void KEncodingFileDialog::hideEvent(QHideEvent *e)
244{
246
248}
249
250void KEncodingFileDialog::accept()
251{
252 d->w->accept();
254}
255
256void KEncodingFileDialog::slotOk()
257{
258 d->w->slotOk();
259}
260
261void KEncodingFileDialog::slotCancel()
262{
263 d->w->slotCancel();
264 reject();
265}
266
267#include "moc_kencodingfiledialog.cpp"
Provides a user (and developer) friendly way to select files with support for choosing encoding.
static KIOFILEWIDGETS_EXPORT Result getOpenFileNamesAndEncoding(const QString &encoding=QString(), const QUrl &startDir=QUrl(), const QString &filter=QString(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal file dialog and returns the selected encoding and the selected filenames or an empty ...
static KIOFILEWIDGETS_EXPORT Result getOpenUrlAndEncoding(const QString &encoding=QString(), const QUrl &startDir=QUrl(), const QString &filter=QString(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal file dialog and returns the selected encoding and URL or an empty string if none was ...
static KIOFILEWIDGETS_EXPORT Result getOpenFileNameAndEncoding(const QString &encoding=QString(), const QUrl &startDir=QUrl(), const QString &filter=QString(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal file dialog and return the selected filename or an empty string if none was chosen ad...
static KIOFILEWIDGETS_EXPORT Result getOpenUrlsAndEncoding(const QString &encoding=QString(), const QUrl &startDir=QUrl(), const QString &filter=QString(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal file dialog and returns the selected encoding URLs or an empty list if none was chose...
static KIOFILEWIDGETS_EXPORT Result getSaveUrlAndEncoding(const QString &encoding=QString(), const QUrl &startDir=QUrl(), const QString &filter=QString(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal file dialog and returns the selected encoding and filename or an empty string if none...
static KIOFILEWIDGETS_EXPORT Result getSaveFileNameAndEncoding(const QString &encoding=QString(), const QUrl &startDir=QUrl(), const QString &filter=QString(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal file dialog and returns the selected encoding and filename or an empty string if none...
File selector widget.
Definition kfilewidget.h:49
void accepted()
Emitted by slotOk() (directly or asynchronously) once everything has been done.
static void add(const QUrl &url)
Add a new item to the Recent Document menu.
QString i18n(const char *text, const TYPE &arg...)
bool insert(Part *part, qint64 *insertId=nullptr)
QWindow * windowHandle(QObject *job)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void clicked(bool checked)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
const char * constData() const const
bool isEmpty() const const
virtual void accept()
virtual int exec()
virtual void reject()
QObject * parent() const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
bool isNull() const const
QByteArray toUtf8() const const
const char * name() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
bool isValid() const const
virtual void hideEvent(QHideEvent *event)
QWindow * windowHandle() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.