1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 David Smith <dsmith@algonet.se>
4
5 This class was inspired by a previous KUrlCompletion by
6 SPDX-FileContributor: Henner Zeller <zeller@think.de>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#ifndef KURLCOMPLETION_H
12#define KURLCOMPLETION_H
13
14#include "kiowidgets_export.h"
15#include <kio/udsentry.h>
16
17#include <KCompletion>
18
19#include <QString>
20#include <QStringList>
21
22#include <memory>
23
24namespace KIO
25{
26class Job;
27}
28
29class KUrlCompletionPrivate;
30
31/*!
32 * \class KUrlCompletion
33 * \inmodule KIOWidgets
34 *
35 * \brief Completion of a single URL.
36 *
37 * This class does completion of URLs including user directories (~user)
38 * and environment variables. Remote URLs are passed to KIO.
39 */
40class KIOWIDGETS_EXPORT KUrlCompletion : public KCompletion
41{
42 Q_OBJECT
43
44public:
45 /*!
46 * Determines how completion is done.
47 * \value ExeCompletion executables in $PATH or with full path.
48 * \value FileCompletion all files with full path or in dir(), URLs
49 * are listed using KIO.
50 * \value DirCompletion Same as FileCompletion but only returns directories.
51 */
52 enum Mode {
53 ExeCompletion = 1,
54 FileCompletion,
55 DirCompletion
56 };
57
58 /*!
59 * Constructs a KUrlCompletion object in FileCompletion mode.
60 */
61 KUrlCompletion();
62 /*!
63 * This overloaded constructor allows you to set the Mode to ExeCompletion
64 * or FileCompletion without using setMode. Default is FileCompletion.
65 */
66 KUrlCompletion(Mode);
67
68 ~KUrlCompletion() override;
69
70 /*!
71 * Finds completions to the given text.
72 *
73 * Remote URLs are listed with KIO. For performance reasons, local files
74 * are listed with KIO only if KURLCOMPLETION_LOCAL_KIO is set.
75 * The completion is done asynchronously if KIO is used.
76 *
77 * Returns the first match for user, environment, and local dir completion
78 * and QString() for asynchronous completion (KIO or threaded).
79 *
80 * \a text the text to complete
81 *
82 * Returns the first match, or QString() if not found
83 */
84 QString makeCompletion(const QString &text) override;
85
86 /*!
87 * Sets the current directory (used as base for completion).
88 * Default = $HOME.
89 *
90 * \a dir the current directory, as a URL (use QUrl::fromLocalFile for local paths)
91 */
92 virtual void setDir(const QUrl &dir);
93
94 /*!
95 * Returns the current directory, as it was given in setDir, as a URL (use QUrl::toLocalFile for local paths)
96 */
97 virtual QUrl dir() const;
98
99 /*!
100 * Check whether asynchronous completion is in progress.
101 */
102 virtual bool isRunning() const;
103
104 /*!
105 * Stops asynchronous completion.
106 */
107 virtual void stop();
108
109 /*!
110 * Returns the completion mode: exe or file completion (default FileCompletion).
111 */
112 virtual Mode mode() const;
113
114 /*!
115 * Changes the completion mode: exe or file completion
116 *
117 * \a mode the new completion mode
118 */
119 virtual void setMode(Mode mode);
120
121 /*!
122 * Checks whether environment variables are completed and
123 * whether they are replaced internally while finding completions.
124 *
125 * Default is enabled.
126 *
127 * Returns true if environment variables will be replaced
128 */
129 virtual bool replaceEnv() const;
130
131 /*!
132 * Enables/disables completion and replacement (internally) of
133 * environment variables in URLs. Default is enabled.
134 *
135 * \a replace true to replace environment variables
136 */
137 virtual void setReplaceEnv(bool replace);
138
139 /*!
140 * Returns whether ~username is completed and whether ~username
141 * is replaced internally with the user's home directory while
142 * finding completions. Default is enabled.
143 *
144 * Returns true to replace tilde with the home directory
145 */
146 virtual bool replaceHome() const;
147
148 /*!
149 * Enables/disables completion of ~username and replacement
150 * (internally) of ~username with the user's home directory.
151 * Default is enabled.
152 *
153 * \a replace true to replace tilde with the home directory
154 */
155 virtual void setReplaceHome(bool replace);
156
157 /*!
158 * Replaces username and/or environment variables, depending on the
159 * current settings and returns the filtered url. Only works with
160 * local files, i.e. returns back the original string for non-local
161 * urls.
162 *
163 * \a text the text to process
164 *
165 * Returns the path or URL resulting from this operation. If you
166 * want to convert it to a QUrl, use QUrl::fromUserInput.
167 */
168 QString replacedPath(const QString &text) const;
169
170 /*!
171 * \internal I'll let ossi add a real one to KShell :)
172 */
173 static QString replacedPath(const QString &text, bool replaceHome, bool replaceEnv = true);
174
175 /*!
176 * Sets the MIME type filters for the file dialog.
177 * \sa QFileDialog::setMimeTypeFilters()
178 * \since 5.38
179 */
180 void setMimeTypeFilters(const QStringList &mimeTypes);
181
182 /*!
183 * Returns the MIME type filters for the file dialog.
184 * \sa QFileDialog::mimeTypeFilters()
185 * \since 5.38
186 */
187 QStringList mimeTypeFilters() const;
188
189protected:
190 // Called by KCompletion, adds '/' to directories
191 void postProcessMatch(QString *match) const override;
192 void postProcessMatches(QStringList *matches) const override;
193 void postProcessMatches(KCompletionMatches *matches) const override;
194
195private:
196 std::unique_ptr<KUrlCompletionPrivate> const d;
197};
198
199#endif // KURLCOMPLETION_H
200

source code of kio/src/widgets/kurlcompletion.h