# class NetAccess

###

Module | kio
Namespace | KIO
Class | NetAccess
Inherits | QObject

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. This means that the GUI will not freeze.

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.

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

Provides an easy, synchronous interface to KIO file operations.

### enums

- StatSide { SourceSide, DestinationSide }

enum details

### methods

- NetAccess ( )

  returns ctor
  Not implemented

  Private constructor

- NetAccess ( )

  returns dtor
  Not implemented

  Private destructor

- copy (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl |
  target | KUrl |
  window | QWidget | 0

  @deprecated, use file_copy instead

- del (url, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  window | QWidget |

  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).

  url - The file or directory to delete.

  window - main 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.

- dircopy (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl |
  target | KUrl |
  window | QWidget |

  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.

  src - URL Referencing the file to upload.

  target - URL containing the final location of the file.

  window - main 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

- dircopy (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl.List |
  target | KUrl |
  window | QWidget | 0

  Overloaded method, which takes a list of source URLs

- download (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl |
  target | QString |
  window | QWidget |

  Downloads a file from an arbitrary URL (@p src) to a temporary file on the local filesystem (@p 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();
      }
      }

  src - URL Reference to the file to download.

  target - String 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.

  window - main 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()

- exists (url, source, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  source | bool |
  window | QWidget |

  Tests whether a URL exists.

  url - the URL we are testing

  source - if 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.

  window - main 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

- exists (url, statSide, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  statSide | KIO::NetAccess.StatSide |
  window | QWidget |

  Tests whether a URL exists.

  url - the URL we are testing

  statSide - determines if we want to read or write. IMPORTANT: see documentation for KIO.stat for more details about this.

  window - main 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

- file_copy (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl |
  target | KUrl |
  window | QWidget | 0

  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.

  src - URL Referencing the file to upload.

  target - URL containing the final location of the file.

  window - main 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

- fish_execute (url, command, window)

  returns QString
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  command | QString |
  window | QWidget |

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

  url - The 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.

  command - The command to be executed.

  window - main 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.

- lastError ( )

  returns int
  static

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

- lastErrorString ( )

  returns QString
  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()

- leaveModality ( )

  returns Nothing
  SIGNAL ("leaveModality ()")

- mimetype (url, window)

  returns QString
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  window | QWidget |

  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.

  url - The URL whose mimetype we are interested in.

  window - main 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.

- mkdir (url, window, permissions)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  window | QWidget |
  permissions | int | -1

  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).

  url - The directory to create.

  window - main 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.

  permissions - directory permissions. Returns true on success, false on failure.

- mostLocalUrl (url, window)

  returns KUrl
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  window | QWidget |

  Tries to map a local URL for the given URL.

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

  url - The URL we are testing.

  window - main 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 ressource than the original URL, or the original URL if no local URL can be mapped

- move (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl |
  target | KUrl |
  window | QWidget | 0

  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())

- move (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | KUrl.List |
  target | KUrl |
  window | QWidget | 0

  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())

- removeTempFile (name)

  returns Nothing
  static

  Argument | Type | Default
  --- | --- | ---
  name | QString |

  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.

  name - Path to temporary file to remove. May not be empty.

- stat (url, entry, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  url | KUrl |
  entry | KIO.UDSEntry |
  window | QWidget |

  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).

  url - The URL we are testing.

  entry - The result of the stat. Iterate over the list of atoms to get hold of name, type, size, etc., or use KFileItem.

  window - main 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

- synchronousRun (job, window, data, finalURL, metaData)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  job | KIO.Job |
  window | QWidget |
  data | QByteArray | 0
  finalURL | KUrl | 0
  metaData | QMap<QString,QString> | 0

  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;
      }

  job - job which the function will run. Note that after this function finishes running, job is deleted and you can't access it anymore!

  window - main 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.

  data - if passed and relevant to this job then it will contain the data that was fetched by the job

  finalURL - if passed will contain the final url of this job (it might differ from the one it was created with if there was a redirection)

  metaData - you 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.

- upload (src, target, window)

  returns bool
  static

  Argument | Type | Default
  --- | --- | ---
  src | QString |
  target | KUrl |
  window | QWidget |

  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.

  src - URL Referencing the file to upload.

  target - URL containing the final location of the file.

  window - main 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

### enums - detail

- StatSide { SourceSide, DestinationSide }

  | SourceSide | - | - |
  | DestinationSide | - | - |