• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

KIO

  • KIO
  • NetAccess
Public Types | Signals | Static Public Member Functions

KIO::NetAccess Class Reference

Net Transparency. More...

#include <netaccess.h>

Inheritance diagram for KIO::NetAccess:
Inheritance graph
[legend]

List of all members.

Public Types

enum  StatSide { SourceSide, DestinationSide }

Signals

void leaveModality ()

Static Public Member Functions

static bool copy (const KUrl &src, const KUrl &target, QWidget *window=0)
static bool del (const KUrl &url, QWidget *window)
static bool dircopy (const KUrl &src, const KUrl &target, QWidget *window)
static bool dircopy (const KUrl::List &src, const KUrl &target, QWidget *window=0L)
static bool download (const KUrl &src, QString &target, QWidget *window)
static bool exists (const KUrl &url, bool source, QWidget *window)
static bool exists (const KUrl &url, StatSide statSide, QWidget *window)
static bool file_copy (const KUrl &src, const KUrl &target, QWidget *window=0)
static QString fish_execute (const KUrl &url, const QString &command, QWidget *window)
static int lastError ()
static QString lastErrorString ()
static QString mimetype (const KUrl &url, QWidget *window)
static bool mkdir (const KUrl &url, QWidget *window, int permissions=-1)
static KUrl mostLocalUrl (const KUrl &url, QWidget *window)
static bool move (const KUrl::List &src, const KUrl &target, QWidget *window=0L)
static bool move (const KUrl &src, const KUrl &target, QWidget *window=0L)
static void removeTempFile (const QString &name)
static bool stat (const KUrl &url, KIO::UDSEntry &entry, QWidget *window)
static bool synchronousRun (Job *job, QWidget *window, QByteArray *data=0, KUrl *finalURL=0, QMap< QString, QString > *metaData=0)
static bool upload (const QString &src, const KUrl &target, QWidget *window)

Detailed Description

Net Transparency.

NetAccess allows you to do simple file operation (load, save, copy, delete...) without working with KIO::Job directly. Whereas a KIO::Job is asynchronous, meaning that the developer has to connect slots for it, KIO::NetAccess provides synchronous downloads and uploads, as well as temporary file creation and removal. The functions appear to be blocking, but the Qt event loop continues running while the operations are handled. More precisely, the GUI will still repaint, but no user interaction will be possible. If you can, please use async KIO jobs instead! See the documentation of KJob::exec() for more about the dangers of NetAccess.

This class isn't meant to be used as a class but only as a simple namespace for static functions, though an instance of the class is built for internal purposes. TODO KDE5: turn into namespace, and make the qobject class private.

Port to kio done by David Faure, faure@kde.org

Provides a blocking interface to KIO file operations.

Definition at line 67 of file netaccess.h.


Member Enumeration Documentation

enum KIO::NetAccess::StatSide
Enumerator:
SourceSide 
DestinationSide 

Definition at line 72 of file netaccess.h.


Member Function Documentation

bool NetAccess::copy ( const KUrl &  src,
const KUrl &  target,
QWidget *  window = 0 
) [static]
Deprecated:
, use file_copy instead

Definition at line 153 of file netaccess.cpp.

bool NetAccess::del ( const KUrl &  url,
QWidget *  window 
) [static]

Deletes a file or a directory in a synchronous way.

This is a convenience function for KIO::del (it saves creating a slot and testing for the job result).

Parameters:
urlThe file or directory to delete.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true on success, false on failure.

Definition at line 259 of file netaccess.cpp.

bool NetAccess::dircopy ( const KUrl &  src,
const KUrl &  target,
QWidget *  window 
) [static]

Alternative method for copying over the network.

This one takes two URLs and is a direct equivalent of KIO::copy!. This means that it can copy files and directories alike (it should have been named copy()).

This method will bring up a dialog if the destination already exists.

Parameters:
srcURL Referencing the file to upload.
targetURL containing the final location of the file.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if successful, false for failure

Definition at line 176 of file netaccess.cpp.

bool NetAccess::dircopy ( const KUrl::List &  src,
const KUrl &  target,
QWidget *  window = 0L 
) [static]

Overloaded method, which takes a list of source URLs.

Definition at line 183 of file netaccess.cpp.

bool NetAccess::download ( const KUrl &  src,
QString &  target,
QWidget *  window 
) [static]

Downloads a file from an arbitrary URL (src) to a temporary file on the local filesystem (target).

If the argument for target is an empty string, download will generate a unique temporary filename in /tmp. Since target is a reference to QString you can access this filename easily. Download will return true if the download was successful, otherwise false.

Special case: If the URL is of kind file:, then no downloading is processed but the full filename is returned in target. That means you have to take care about the target argument. (This is very easy to do, please see the example below.)

Download is synchronous. That means you can use it like this: (assuming your application has a loadFile() function)

 QString tmpFile;
 if( KIO::NetAccess::download(url, tmpFile, window)) {
     loadFile(tmpFile);
     KIO::NetAccess::removeTempFile(tmpFile);
 } else {
     KMessageBox::error(this, KIO::NetAccess::lastErrorString());
 }

Of course, your user interface will still process exposure/repaint events during the download.

If the download fails, lastError() and lastErrorString() will be set.

If the url is always remote, then you could also just write the more usual way:

 KTemporaryFile tmpFile;
 if (tmpFile.open()) {
     KIO::Job* getJob = KIO::file_copy(url, KUrl(tmpFile.fileName()), -1, KIO::Overwrite | KIO::HideProgressInfo);
     getJob->ui()->setWindow(window);
     if (KIO::NetAccess::synchronousRun(getJob, 0)) {
         loadFile(tmpFile.fileName());
     } else {
         getJob->ui()->showErrorMessage();
     }
 }
Parameters:
srcURL Reference to the file to download.
targetString containing the final local location of the file. If you insert an empty string, it will return a location in a temporary spot. Note: you are responsible for the removal of this file when you are finished reading it using removeTempFile.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if successful, false for failure. Use lastErrorString() to get the reason it failed.
See also:
lastErrorString()

Definition at line 95 of file netaccess.cpp.

bool NetAccess::exists ( const KUrl &  url,
bool  source,
QWidget *  window 
) [static]

Tests whether a URL exists.

Parameters:
urlthe URL we are testing
sourceif true, we want to read from that URL. If false, we want to write to it. IMPORTANT: see documentation for KIO::stat for more details about this.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if the URL exists and we can do the operation specified by source, false otherwise

Definition at line 208 of file netaccess.cpp.

bool NetAccess::exists ( const KUrl &  url,
StatSide  statSide,
QWidget *  window 
) [static]

Tests whether a URL exists.

Parameters:
urlthe URL we are testing
statSidedetermines if we want to read or write. IMPORTANT: see documentation for KIO::stat for more details about this.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if the URL exists and we can do the operation specified by source, false otherwise

Definition at line 218 of file netaccess.cpp.

bool NetAccess::file_copy ( const KUrl &  src,
const KUrl &  target,
QWidget *  window = 0 
) [static]

Alternative to upload for copying over the network.

Overwrite is false, so this will fail if target exists.

This one takes two URLs and is a direct equivalent of KIO::file_copy.

Parameters:
srcURL Referencing the file to upload.
targetURL containing the final location of the file.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if successful, false for failure

Definition at line 145 of file netaccess.cpp.

QString NetAccess::fish_execute ( const KUrl &  url,
const QString &  command,
QWidget *  window 
) [static]

Executes a remote process via the fish ioslave in a synchronous way.

Parameters:
urlThe remote machine where the command should be executed. e.g. fish://someuser@somehost:sshport/ some special cases exist. fish://someuser@localhost/ will use su instead of ssh to connect and execute the command. fish://someuser@localhost:port/ will use ssh to connect and execute the command.
commandThe command to be executed.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
The resulting output of the command that is executed.

Definition at line 271 of file netaccess.cpp.

int NetAccess::lastError ( ) [static]

Returns the error code for the last job, in case it failed.

Returns:
the last error code

Definition at line 303 of file netaccess.cpp.

QString NetAccess::lastErrorString ( ) [static]

Returns the error string for the last job, in case it failed.

Note that this is already translated.

Returns:
the last error string, or QString()

Definition at line 298 of file netaccess.cpp.

void KIO::NetAccess::leaveModality ( ) [signal]
QString NetAccess::mimetype ( const KUrl &  url,
QWidget *  window 
) [static]

Determines the mimetype of a given URL.

This is a convenience function for KIO::mimetype. You should call this only when really necessary. KMimeType::findByUrl can determine extension a lot faster, but less reliably for remote files. Only when findByUrl() returns unknown (application/octet-stream) then this one should be used.

Parameters:
urlThe URL whose mimetype we are interested in.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
The mimetype name.

Definition at line 292 of file netaccess.cpp.

bool NetAccess::mkdir ( const KUrl &  url,
QWidget *  window,
int  permissions = -1 
) [static]

Creates a directory in a synchronous way.

This is a convenience function for KIO::mkdir (it saves creating a slot and testing for the job result).

Parameters:
urlThe directory to create.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
permissionsdirectory permissions.
Returns:
true on success, false on failure.

Definition at line 265 of file netaccess.cpp.

KUrl NetAccess::mostLocalUrl ( const KUrl &  url,
QWidget *  window 
) [static]

Tries to map a local URL for the given URL.

This is a convenience function for KIO::stat + parsing the resulting UDSEntry.

Parameters:
urlThe URL we are testing.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
a local URL corresponding to the same resource than the original URL, or the original URL if no local URL can be mapped

Definition at line 235 of file netaccess.cpp.

bool NetAccess::move ( const KUrl &  src,
const KUrl &  target,
QWidget *  window = 0L 
) [static]

Full-fledged equivalent of KIO::move.

Moves or renames one file or directory.

Deprecated:
use KIO::move and then KIO::NetAccess::synchronousRun (or job->exec())

Definition at line 190 of file netaccess.cpp.

bool NetAccess::move ( const KUrl::List &  src,
const KUrl &  target,
QWidget *  window = 0L 
) [static]

Full-fledged equivalent of KIO::move.

Moves or renames a list of files or directories.

Deprecated:
use KIO::move and then KIO::NetAccess::synchronousRun (or job->exec())

Definition at line 200 of file netaccess.cpp.

void NetAccess::removeTempFile ( const QString &  name) [static]

Removes the specified file if and only if it was created by KIO::NetAccess as a temporary file for a former download.

Note: This means that if you created your temporary with KTempFile, use KTempFile::unlink() or KTempFile::setAutoDelete() to have it removed.

Parameters:
namePath to temporary file to remove. May not be empty.

Definition at line 308 of file netaccess.cpp.

bool NetAccess::stat ( const KUrl &  url,
KIO::UDSEntry &  entry,
QWidget *  window 
) [static]

Tests whether a URL exists and return information on it.

This is a convenience function for KIO::stat (it saves creating a slot and testing for the job result).

Parameters:
urlThe URL we are testing.
entryThe result of the stat. Iterate over the list of atoms to get hold of name, type, size, etc., or use KFileItem.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if successful, false for failure

Definition at line 226 of file netaccess.cpp.

bool NetAccess::synchronousRun ( Job *  job,
QWidget *  window,
QByteArray *  data = 0,
KUrl *  finalURL = 0,
QMap< QString, QString > *  metaData = 0 
) [static]

This function executes a job in a synchronous way.

If a job fetches some data, pass a QByteArray pointer as data parameter to this function and after the function returns it will contain all the data fetched by this job.

 KIO::Job *job = KIO::get( url );
 QMap<QString, QString> metaData;
 metaData.insert( "PropagateHttpHeader", "true" );
 if ( NetAccess::synchronousRun( job, 0, &data, &url, &metaData ) ) {
   QString responseHeaders = metaData[ "HTTP-Headers" ];
   kDebug()<<"Response header = "<< responseHeaders;
 }
Parameters:
jobjob which the function will run. Note that after this function finishes running, job is deleted and you can't access it anymore!
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
dataif passed and relevant to this job then it will contain the data that was fetched by the job
finalURLif passed will contain the final url of this job (it might differ from the one it was created with if there was a redirection)
metaDatayou can pass a pointer to the map with meta data you wish to set on the job. After the job finishes this map will hold all the meta data from the job.
Returns:
true on success, false on failure.

Definition at line 277 of file netaccess.cpp.

bool NetAccess::upload ( const QString &  src,
const KUrl &  target,
QWidget *  window 
) [static]

Uploads file src to URL target.

Both must be specified, unlike download. Note that this is assumed to be used for saving a file over the network, so overwriting is set to true. This is not the case with copy.

Parameters:
srcURL Referencing the file to upload.
targetURL containing the final location of the file.
windowmain window associated with this job. This is used to automatically cache and discard authentication information as needed. If NULL, authentication information will be cached only for a short duration after which the user will again be prompted for passwords as needed.
Returns:
true if successful, false for failure

Definition at line 128 of file netaccess.cpp.


The documentation for this class was generated from the following files:
  • netaccess.h
  • netaccess.cpp

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal