KDELibs4Support

netaccess.h
1 /*
2  This file is part of the KDE libraries
3  Copyright (C) 1997 Torben Weis ([email protected])
4  Copyright (C) 1998 Matthias Ettrich ([email protected])
5  Copyright (C) 1999-2004 David Faure ([email protected])
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef KIO_NETACCESS_h
24 #define KIO_NETACCESS_h
25 
26 #include <QObject>
27 #include <QString>
28 #include <kdelibs4support_export.h>
29 #include <kio/global.h>
30 #include <kio/udsentry.h>
31 #include <kio/jobclasses.h> // for KIO::JobFlags
32 
33 class QStringList;
34 class QWidget;
35 
36 template<typename T, typename K> class QMap;
37 
38 class KJob;
39 namespace KIO
40 {
41 
42 class Job;
43 
44 class NetAccessPrivate;
45 /**
46  * Net Transparency.
47  *
48  * NetAccess allows you to do simple file operation (load, save,
49  * copy, delete...) without working with KIO::Job directly.
50  * Whereas a KIO::Job is asynchronous, meaning that the
51  * developer has to connect slots for it, KIO::NetAccess provides
52  * synchronous downloads and uploads, as well as temporary file
53  * creation and removal. The functions appear to be blocking,
54  * but the Qt event loop continues running while the operations
55  * are handled. More precisely, the GUI will still repaint, but no user
56  * interaction will be possible. If you can, please use async KIO jobs instead!
57  * See the documentation of KJob::exec() for more about the dangers of NetAccess.
58  *
59  * This class isn't meant to be used as a class but only as a simple
60  * namespace for static functions, though an instance of the class
61  * is built for internal purposes. TODO KDE5: turn into namespace,
62  * and make the qobject class private.
63  *
64  * Port to kio done by David Faure, [email protected]
65  *
66  * @short Provides a blocking interface to KIO file operations.
67  */
68 class KDELIBS4SUPPORT_DEPRECATED_EXPORT NetAccess : public QObject
69 {
70  Q_OBJECT
71 
72 public:
73  enum StatSide {
74  SourceSide,
75  DestinationSide
76  };
77 
78  /**
79  * Downloads a file from an arbitrary URL (@p src) to a
80  * temporary file on the local filesystem (@p target).
81  *
82  * If the argument
83  * for @p target is an empty string, download will generate a
84  * unique temporary filename in /tmp. Since @p target is a reference
85  * to QString you can access this filename easily. Download will
86  * return true if the download was successful, otherwise false.
87  *
88  * Special case:
89  * If the URL is of kind file:, then no downloading is
90  * processed but the full filename is returned in @p target.
91  * That means you @em have to take care about the @p target argument.
92  * (This is very easy to do, please see the example below.)
93  *
94  * Download is synchronous. That means you can use it like this:
95  * (assuming your application has a loadFile() function)
96  *
97  * \code
98  * QString tmpFile;
99  * if( KIO::NetAccess::download(url, tmpFile, window)) {
100  * loadFile(tmpFile);
101  * KIO::NetAccess::removeTempFile(tmpFile);
102  * } else {
103  * KMessageBox::error(this, KIO::NetAccess::lastErrorString());
104  * }
105  * \endcode
106  *
107  * Of course, your user interface will still process exposure/repaint
108  * events during the download.
109  *
110  * If the download fails, lastError() and lastErrorString() will be set.
111  *
112  * If the url is always remote, then you could also just write the more usual way:
113  * \code
114  * QTemporaryFile tmpFile;
115  * if (tmpFile.open()) {
116  * KIO::Job* getJob = KIO::file_copy(url, QUrl::fromLocalFile(tmpFile.fileName()), -1, KIO::Overwrite | KIO::HideProgressInfo);
117  * getJob->ui()->setWindow(window);
118  * if (KIO::NetAccess::synchronousRun(getJob, 0)) {
119  * loadFile(tmpFile.fileName());
120  * } else {
121  * getJob->ui()->showErrorMessage();
122  * }
123  * }
124  * \endcode
125  *
126  * @param src URL Reference to the file to download.
127  * @param target String containing the final local location of the
128  * file. If you insert an empty string, it will
129  * return a location in a temporary spot. <B>Note:</B>
130  * you are responsible for the removal of this file when
131  * you are finished reading it using removeTempFile.
132  * @param window main window associated with this job. This is used to
133  * automatically cache and discard authentication information
134  * as needed. If NULL, authentication information will be
135  * cached only for a short duration after which the user will
136  * again be prompted for passwords as needed.
137  * @return true if successful, false for failure. Use lastErrorString() to
138  * get the reason it failed.
139  *
140  * @see lastErrorString()
141  * @deprecated since 5.0, use KIO::storedGet + KJobWidgets::setWindow + job->exec() instead
142  */
143  static bool download(const QUrl &src, QString &target, QWidget *window);
144 
145  /**
146  * Removes the specified file if and only if it was created
147  * by KIO::NetAccess as a temporary file for a former download.
148  *
149  * Note: This means that if you created your temporary with KTempFile,
150  * use KTempFile::unlink() or KTempFile::setAutoDelete() to have
151  * it removed.
152  *
153  * @param name Path to temporary file to remove. May not be
154  * empty.
155  */
156  static void removeTempFile(const QString &name);
157 
158  /**
159  * Uploads file @p src to URL @p target.
160  *
161  * Both must be specified, unlike download.
162  * Note that this is assumed to be used for saving a file over
163  * the network, so overwriting is set to true. This is not the
164  * case with copy.
165  *
166  * @param src URL Referencing the file to upload.
167  * @param target URL containing the final location of the file.
168  * @param window main window associated with this job. This is used to
169  * automatically cache and discard authentication information
170  * as needed. If NULL, authentication information will be cached
171  * only for a short duration after which the user will again be
172  * prompted for passwords as needed.
173  *
174  * @return true if successful, false for failure
175  * @deprecated since 5.0, use KIO::storedPut or KIO::file_copy + KJobWidgets::setWindow + job->exec() instead
176  */
177  static bool upload(const QString &src, const QUrl &target, QWidget *window);
178 
179  /**
180  * Alternative to upload for copying over the network.
181  * Overwrite is false, so this will fail if @p target exists.
182  *
183  * This one takes two URLs and is a direct equivalent of KIO::file_copy.
184  *
185  * @param src URL Referencing the file to upload.
186  * @param target URL containing the final location of the file.
187  * @param window main window associated with this job. This is used to
188  * automatically cache and discard authentication information
189  * as needed. If NULL, authentication information will be cached
190  * only for a short duration after which the user will again be
191  * prompted for passwords as needed.
192  *
193  * @return true if successful, false for failure
194  * @deprecated since 5.0, use KIO::file_copy + job->ui()->setWindow() + job->exec() instead
195  */
196 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
197  static KDELIBS4SUPPORT_DEPRECATED bool file_copy(const QUrl &src, const QUrl &target, QWidget *window = nullptr);
198  static KDELIBS4SUPPORT_DEPRECATED bool copy(const QUrl &src, const QUrl &target,
199  QWidget *window = nullptr);
200 #endif
201 
202  /**
203  * Alternative method for copying over the network.
204  *
205  * This one takes two URLs and is a direct equivalent
206  * of KIO::copy!.
207  * This means that it can copy files and directories alike
208  * (it should have been named copy()).
209  *
210  * This method will bring up a dialog if the destination already exists.
211  *
212  * @param src URL Referencing the file to upload.
213  * @param target URL containing the final location of the
214  * file.
215  * @param window main window associated with this job. This is used to
216  * automatically cache and discard authentication information
217  * as needed. If NULL, authentication information will be cached
218  * only for a short duration after which the user will again be
219  * prompted for passwords as needed.
220  * @return true if successful, false for failure
221  * @deprecated since 5.0, use KIO::copy + job->ui()->setWindow() + job->exec() instead
222  */
223 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
224  static KDELIBS4SUPPORT_DEPRECATED bool dircopy(const QUrl &src, const QUrl &target, QWidget *window);
225 #endif
226 
227  /**
228  * Overloaded method, which takes a list of source URLs
229  * @deprecated since 5.0, use KIO::copy + job->ui()->setWindow() + job->exec() instead
230  */
231 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
232  static KDELIBS4SUPPORT_DEPRECATED bool dircopy(const QList<QUrl> &src, const QUrl &target, QWidget *window = nullptr);
233 #endif
234  /**
235  * Full-fledged equivalent of KIO::move.
236  * Moves or renames one file or directory.
237  * @deprecated since 5.0, use KIO::move + job->ui()->setWindow() + job->exec() instead
238  */
239 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
240  static KDELIBS4SUPPORT_DEPRECATED bool move(const QUrl &src, const QUrl &target, QWidget *window = nullptr);
241 #endif
242 
243  /**
244  * Full-fledged equivalent of KIO::move.
245  * Moves or renames a list of files or directories.
246  * @deprecated since 5.0, use KIO::move + job->ui()->setWindow() + job->exec() instead
247  */
248 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
249  static KDELIBS4SUPPORT_DEPRECATED bool move(const QList<QUrl> &src, const QUrl &target, QWidget *window = nullptr);
250 #endif
251 
252  /**
253  * Tests whether a URL exists.
254  *
255  * @param url the URL we are testing
256  * @param source if true, we want to read from that URL.
257  * If false, we want to write to it.
258  * IMPORTANT: see documentation for KIO::stat for more details about this.
259  * @param window main window associated with this job. This is used to
260  * automatically cache and discard authentication information
261  * as needed. If NULL, authentication information will be
262  * cached only for a short duration after which the user will
263  * again be prompted for passwords as needed.
264  * @return true if the URL exists and we can do the operation specified by
265  * @p source, false otherwise
266  *
267  * @deprecated use the StatSide enum instead of the bool source
268  */
269 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
270  static KDELIBS4SUPPORT_DEPRECATED bool exists(const QUrl &url, bool source, QWidget *window);
271 #endif
272 
273  /**
274  * Tests whether a URL exists.
275  *
276  * @param url the URL we are testing
277  * @param statSide determines if we want to read or write.
278  * IMPORTANT: see documentation for KIO::stat for more details about this.
279  * @param window main window associated with this job. This is used to
280  * automatically cache and discard authentication information
281  * as needed. If NULL, authentication information will be
282  * cached only for a short duration after which the user will
283  * again be prompted for passwords as needed.
284  * @return true if the URL exists and we can do the operation specified by
285  * @p source, false otherwise
286  * @deprecated since 5.0, use QFile::exists for local files and KIO::stat for transparency
287  */
288  static bool exists(const QUrl &url, StatSide statSide, QWidget *window);
289 
290  /**
291  * Tests whether a URL exists and return information on it.
292  *
293  * This is a convenience function for KIO::stat
294  * (it saves creating a slot and testing for the job result).
295  *
296  * @param url The URL we are testing.
297  * @param entry The result of the stat. Iterate over the list
298  * of atoms to get hold of name, type, size, etc., or use KFileItem.
299  * @param window main window associated with this job. This is used to
300  * automatically cache and discard authentication information
301  * as needed. If NULL, authentication information will be
302  * cached only for a short duration after which the user will
303  * again be prompted for passwords as needed.
304  * @return true if successful, false for failure
305  * @deprecated since 5.0, use KIO::stat + KJobWidgets::setWindow + job->exec() instead
306  */
307  static bool stat(const QUrl &url, KIO::UDSEntry &entry, QWidget *window);
308 
309  /**
310  * Tries to map a local URL for the given URL.
311  *
312  * This is a convenience function for KIO::stat + parsing the
313  * resulting UDSEntry.
314  *
315  * @param url The URL we are testing.
316  * @param window main window associated with this job. This is used to
317  * automatically cache and discard authentication information
318  * as needed. If NULL, authentication information will be
319  * cached only for a short duration after which the user will
320  * again be prompted for passwords as needed.
321  * @return a local URL corresponding to the same resource than the
322  * original URL, or the original URL if no local URL can be mapped
323  * @deprecated since 5.0, use KIO::mostLocalUrl + KJobWidgets::setWindow + job->exec() instead
324  */
325  static QUrl mostLocalUrl(const QUrl &url, QWidget *window);
326 
327  /**
328  * Deletes a file or a directory in a synchronous way.
329  *
330  * This is a convenience function for KIO::del
331  * (it saves creating a slot and testing for the job result).
332  *
333  * @param url The file or directory to delete.
334  * @param window main window associated with this job. This is used to
335  * automatically cache and discard authentication information
336  * as needed. If NULL, authentication information will be
337  * cached only for a short duration after which the user will
338  * again be prompted for passwords as needed.
339  * @return true on success, false on failure.
340  * @deprecated since 5.0, use KIO::del + job->ui()->setWindow() + job->exec() instead
341  */
342 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
343  static KDELIBS4SUPPORT_DEPRECATED bool del(const QUrl &url, QWidget *window);
344 #endif
345 
346  /**
347  * Creates a directory in a synchronous way.
348  *
349  * This is a convenience function for @p KIO::mkdir
350  * (it saves creating a slot and testing for the job result).
351  *
352  * @param url The directory to create.
353  * @param window main window associated with this job. This is used to
354  * automatically cache and discard authentication information
355  * as needed. If NULL, authentication information will be
356  * cached only for a short duration after which the user will
357  * again be prompted for passwords as needed.
358  * @param permissions directory permissions.
359  * @return true on success, false on failure.
360  * @deprecated since 5.0, use KIO::mkdir + job->ui()->setWindow() + job->exec() instead
361  */
362 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
363  static KDELIBS4SUPPORT_DEPRECATED bool mkdir(const QUrl &url, QWidget *window, int permissions = -1);
364 #endif
365 
366  /**
367  * Executes a remote process via the fish ioslave in a synchronous way.
368  *
369  * @param url The remote machine where the command should be executed.
370  * e.g. fish://someuser\@somehost:sshport/
371  * some special cases exist.
372  * fish://someuser\@localhost/
373  * will use su instead of ssh to connect and execute the command.
374  * fish://someuser\@localhost:port/
375  * will use ssh to connect and execute the command.
376  * @param command The command to be executed.
377  * @param window main window associated with this job. This is used to
378  * automatically cache and discard authentication information
379  * as needed. If NULL, authentication information will be
380  * cached only for a short duration after which the user will
381  * again be prompted for passwords as needed.
382  * @return The resulting output of the @p command that is executed.
383  */
384  static QString fish_execute(const QUrl &url, const QString &command, QWidget *window);
385 
386  /**
387  * This function executes a job in a synchronous way.
388  * If a job fetches some data, pass a QByteArray pointer as data parameter to this function
389  * and after the function returns it will contain all the data fetched by this job.
390  *
391  * @code
392  * KIO::Job *job = KIO::get( url );
393  * QMap<QString, QString> metaData;
394  * metaData.insert( "PropagateHttpHeader", "true" );
395  * if ( NetAccess::synchronousRun( job, 0, &data, &url, &metaData ) ) {
396  * QString responseHeaders = metaData[ "HTTP-Headers" ];
397  * qDebug()<<"Response header = "<< responseHeaders;
398  * }
399  * @endcode
400  *
401  * @param job job which the function will run. Note that after this function
402  * finishes running, job is deleted and you can't access it anymore!
403  * @param window main window associated with this job. This is used to
404  * automatically cache and discard authentication information
405  * as needed. If NULL, authentication information will be
406  * cached only for a short duration after which the user will
407  * again be prompted for passwords as needed.
408  * @param data if passed and relevant to this job then it will contain the data
409  * that was fetched by the job
410  * @param finalURL if passed will contain the final url of this job (it might differ
411  * from the one it was created with if there was a redirection)
412  * @param metaData you can pass a pointer to the map with meta data you wish to
413  * set on the job. After the job finishes this map will hold all the
414  * meta data from the job.
415  *
416  * @return true on success, false on failure.
417  * @deprecated since 5.0, KJobWidgets::setWindow + job->exec() instead
418  */
419  static bool synchronousRun(Job *job, QWidget *window, QByteArray *data = nullptr,
420  QUrl *finalURL = nullptr, QMap<QString, QString> *metaData = nullptr);
421 
422  /**
423  * Determines the mimetype of a given URL.
424  *
425  * This is a convenience function for KIO::mimetype. You
426  * should call this only when really necessary.
427  * QMimeDatabase::mimeTypeForUrl can determine the mimetype a lot faster, but
428  * less reliably for remote files. When mimeTypeForUrl() returns
429  * unknown (application/octet-stream) for a remote file, then this one
430  * should be used.
431  *
432  * @param url The URL whose mimetype we are interested in.
433  * @param window main window associated with this job. This is used to
434  * automatically cache and discard authentication information
435  * as needed. If NULL, authentication information will be
436  * cached only for a short duration after which the user will
437  * again be prompted for passwords as needed.
438  * @return The mimetype name.
439  * @deprecated since 5.0, use KIO::mimetype + KJobWidgets::setWindow + job->exec() instead
440  */
441  static QString mimetype(const QUrl &url, QWidget *window);
442 
443  /**
444  * Returns the error string for the last job, in case it failed.
445  * Note that this is already translated.
446  * @return the last error string, or QString()
447  */
448  static QString lastErrorString();
449 
450  /**
451  * Returns the error code for the last job, in case it failed.
452  * @return the last error code
453  */
454  static int lastError();
455 
456 Q_SIGNALS:
457  void leaveModality();
458 private:
459  /**
460  * Private constructor
461  */
462  NetAccess();
463 
464  /**
465  * Private destructor
466  */
467  ~NetAccess() override;
468 
469  /**
470  * Internal methods
471  */
472  bool filecopyInternal(const QUrl &src, const QUrl &target, int permissions,
473  KIO::JobFlags flags, QWidget *window, bool move);
474  bool dircopyInternal(const QList<QUrl> &src, const QUrl &target,
475  QWidget *window, bool move);
476  bool statInternal(const QUrl &url, int details, StatSide side, QWidget *window = nullptr);
477 
478  bool delInternal(const QUrl &url, QWidget *window = nullptr);
479  bool mkdirInternal(const QUrl &url, int permissions, QWidget *window = nullptr);
480  QString fish_executeInternal(const QUrl &url, const QString &command, QWidget *window = nullptr);
481  bool synchronousRunInternal(Job *job, QWidget *window, QByteArray *data,
482  QUrl *finalURL, QMap<QString, QString> *metaData);
483 
484  QString mimetypeInternal(const QUrl &url, QWidget *window = nullptr);
485  void enter_loop();
486 
487  friend class I_like_this_class;
488 
489 private Q_SLOTS:
490  void slotResult(KJob *job);
491  void slotMimetype(KIO::Job *job, const QString &type);
492  void slotData(KIO::Job *, const QByteArray &);
493  void slotRedirection(KIO::Job *, const QUrl &);
494 
495 private:
496  NetAccessPrivate *const d;
497 };
498 
499 }
500 
501 #endif
KIOCORE_EXPORT FileCopyJob * file_copy(const QUrl &src, const QUrl &dest, int permissions=-1, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
int mkdir(const QString &pathname, mode_t mode)
replacement for mkdir() to handle pathnames in a platform independent way
Definition: kde_file.h:195
Definition: netaccess.h:36
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Net Transparency.
Definition: netaccess.h:68
KIOCORE_EXPORT StatJob * mostLocalUrl(const QUrl &url, JobFlags flags=DefaultFlags)
int stat(const QString &path, KDE_struct_stat *buf)
replacement for stat()/::stat64() to handle filenames in a platform independent way
Definition: kde_file.h:207
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Dec 1 2023 03:59:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.