Qt 4.8
Public Slots | Public Functions | Protected Variables | Private Functions | Properties | List of all members
QNetworkAccessFileBackend Class Reference

#include <qnetworkaccessfilebackend_p.h>

Inheritance diagram for QNetworkAccessFileBackend:
QNetworkAccessBackend QObject

Public Slots

void uploadReadyReadSlot ()
 
- Public Slots inherited from QNetworkAccessBackend
void writeDownstreamData (QIODevice *data)
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Public Functions

virtual void closeDownstreamChannel ()
 
virtual void downstreamReadyWrite ()
 
virtual void open ()
 
 QNetworkAccessFileBackend ()
 
virtual ~QNetworkAccessFileBackend ()
 
- Public Functions inherited from QNetworkAccessBackend
QVariant attribute (QNetworkRequest::Attribute code) const
 
virtual bool canResume () const
 
virtual void copyFinished (QIODevice *)
 
virtual void emitReadBufferFreed (qint64 size)
 
virtual QNetworkCacheMetaData fetchCacheMetaData (const QNetworkCacheMetaData &metaData) const
 
virtual void fetchSslConfiguration (QSslConfiguration &configuration) const
 
bool hasRawHeader (const QByteArray &headerName) const
 
QVariant header (QNetworkRequest::KnownHeaders header) const
 
virtual void ignoreSslErrors ()
 
virtual void ignoreSslErrors (const QList< QSslError > &errors)
 
bool isCachingEnabled () const
 
bool isSynchronous ()
 
virtual bool needsResetableUploadData ()
 
QAbstractNetworkCachenetworkCache () const
 
QNetworkAccessManager::Operation operation () const
 
virtual bool processRequestSynchronously ()
 
QList< QNetworkProxyproxyList () const
 
 QNetworkAccessBackend ()
 
QByteArray rawHeader (const QByteArray &headerName) const
 
QList< QByteArrayrawHeaderList () const
 
QNetworkRequest request () const
 
void setAttribute (QNetworkRequest::Attribute code, const QVariant &value)
 
void setCachingEnabled (bool enable)
 
virtual void setDownstreamLimited (bool b)
 
void setHeader (QNetworkRequest::KnownHeaders header, const QVariant &value)
 
void setRawHeader (const QByteArray &headerName, const QByteArray &value)
 
virtual void setReadBufferSize (qint64 size)
 
virtual void setResumeOffset (quint64 offset)
 
virtual void setSslConfiguration (const QSslConfiguration &configuration)
 
void setSynchronous (bool sync)
 
void setUrl (const QUrl &url)
 
virtual bool start ()
 Starts the backend. More...
 
QUrl url () const
 
virtual ~QNetworkAccessBackend ()
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Protected Variables

QNonContiguousByteDeviceuploadByteDevice
 
- Protected Variables inherited from QNetworkAccessBackend
QNetworkAccessManagerPrivatemanager
 
QNetworkReplyImplPrivatereply
 
QSharedPointer< QNonContiguousByteDeviceuploadByteDevice
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Private Functions

bool loadFileInfo ()
 
bool readMoreFromFile ()
 

Properties

QFile file
 
bool hasUploadFinished
 
qint64 totalBytes
 

Additional Inherited Members

- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Slots inherited from QNetworkAccessBackend
void authenticationRequired (QAuthenticator *auth)
 
void emitReplyUploadProgress (qint64 bytesSent, qint64 bytesTotal)
 
void error (QNetworkReply::NetworkError code, const QString &errorString)
 
void finished ()
 
void metaDataChanged ()
 
void proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *auth)
 
void redirectionRequested (const QUrl &destination)
 
void sslErrors (const QList< QSslError > &errors)
 
- Protected Functions inherited from QNetworkAccessBackend
QNonContiguousByteDevicecreateUploadByteDevice ()
 
char * getDownloadBuffer (qint64)
 
qint64 nextDownstreamBlockSize () const
 
void writeDownstreamData (QByteDataBuffer &list)
 
void writeDownstreamDataDownloadBuffer (qint64, qint64)
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 63 of file qnetworkaccessfilebackend_p.h.

Constructors and Destructors

◆ QNetworkAccessFileBackend()

QNetworkAccessFileBackend::QNetworkAccessFileBackend ( )

◆ ~QNetworkAccessFileBackend()

QNetworkAccessFileBackend::~QNetworkAccessFileBackend ( )
virtual

Definition at line 93 of file qnetworkaccessfilebackend.cpp.

94 {
95 }

Functions

◆ closeDownstreamChannel()

void QNetworkAccessFileBackend::closeDownstreamChannel ( )
virtual

Implements QNetworkAccessBackend.

Definition at line 207 of file qnetworkaccessfilebackend.cpp.

208 {
210  file.close();
211  }
212 }
QNetworkAccessManager::Operation operation() const
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680

◆ downstreamReadyWrite()

void QNetworkAccessFileBackend::downstreamReadyWrite ( )
virtual

Reimplemented from QNetworkAccessBackend.

Definition at line 214 of file qnetworkaccessfilebackend.cpp.

215 {
216  Q_ASSERT_X(operation() == QNetworkAccessManager::GetOperation, "QNetworkAccessFileBackend",
217  "We're being told to download data but operation isn't GET!");
218 
220 }
QNetworkAccessManager::Operation operation() const
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837

◆ loadFileInfo()

bool QNetworkAccessFileBackend::loadFileInfo ( )
private

Definition at line 222 of file qnetworkaccessfilebackend.cpp.

Referenced by open().

223 {
224  QFileInfo fi(file);
227 
228  // signal we're open
229  metaDataChanged();
230 
231  if (fi.isDir()) {
233  QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url().toString()));
234  finished();
235  return false;
236  }
237 
238  return true;
239 }
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
static QString toString(Register *reg, int type, bool *ok=0)
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
void error(QNetworkReply::NetworkError code, const QString &errorString)
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60

◆ open()

void QNetworkAccessFileBackend::open ( )
virtual

Implements QNetworkAccessBackend.

Definition at line 97 of file qnetworkaccessfilebackend.cpp.

98 {
99  QUrl url = this->url();
100 
101  if (url.host() == QLatin1String("localhost"))
102  url.setHost(QString());
103 #if !defined(Q_OS_WIN)
104  // do not allow UNC paths on Unix
105  if (!url.host().isEmpty()) {
106  // we handle only local files
108  QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString()));
109  finished();
110  return;
111  }
112 #endif // !defined(Q_OS_WIN)
113  if (url.path().isEmpty())
114  url.setPath(QLatin1String("/"));
115  setUrl(url);
116 
117  QString fileName = url.toLocalFile();
118  if (fileName.isEmpty()) {
119  if (url.scheme() == QLatin1String("qrc"))
120  fileName = QLatin1Char(':') + url.path();
121  else
123  }
124  file.setFileName(fileName);
125 
127  if (!loadFileInfo())
128  return;
129  }
130 
131  QIODevice::OpenMode mode;
132  switch (operation()) {
134  mode = QIODevice::ReadOnly;
135  break;
140  QMetaObject::invokeMethod(this, "uploadReadyReadSlot", Qt::QueuedConnection);
141  break;
142  default:
143  Q_ASSERT_X(false, "QNetworkAccessFileBackend::open",
144  "Got a request operation I cannot handle!!");
145  return;
146  }
147 
148  mode |= QIODevice::Unbuffered;
149  bool opened = file.open(mode);
150 
151  // could we open the file?
152  if (!opened) {
153  QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
154  .arg(this->url().toString(), file.errorString());
155 
156  // why couldn't we open the file?
157  // if we're opening for reading, either it doesn't exist, or it's access denied
158  // if we're opening for writing, not existing means it's access denied too
161  else
163  finished();
164  }
165 }
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
#define SLOT(a)
Definition: qobjectdefs.h:226
QString errorString() const
Returns a human-readable description of the last device error that occurred.
Definition: qiodevice.cpp:1671
void setPath(const QString &path)
Sets the path of the URL to path.
Definition: qurl.cpp:4960
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString host() const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4837
void setHost(const QString &host)
Sets the host of the URL to host.
Definition: qurl.cpp:4821
QString path() const
Returns the path of the URL.
Definition: qurl.cpp:4977
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static QString toString(Register *reg, int type, bool *ok=0)
QNonContiguousByteDevice * createUploadByteDevice()
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QNetworkAccessManager::Operation operation() const
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition: qurl.cpp:6412
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
void error(QNetworkReply::NetworkError code, const QString &errorString)
void setUrl(const QUrl &url)
QNonContiguousByteDevice * uploadByteDevice
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
static QString fileName(const QString &fileUrl)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void setFileName(const QString &name)
Sets the name of the file.
Definition: qfile.cpp:494

◆ readMoreFromFile()

bool QNetworkAccessFileBackend::readMoreFromFile ( )
private

Definition at line 241 of file qnetworkaccessfilebackend.cpp.

Referenced by downstreamReadyWrite().

242 {
243  qint64 wantToRead;
244  while ((wantToRead = nextDownstreamBlockSize()) > 0) {
245  // ### FIXME!!
246  // Obtain a pointer from the ringbuffer!
247  // Avoid extra copy
249  data.reserve(wantToRead);
250  qint64 actuallyRead = file.read(data.data(), wantToRead);
251  if (actuallyRead <= 0) {
252  // EOF or error
253  if (file.error() != QFile::NoError) {
254  QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Read error reading from %1: %2")
255  .arg(url().toString(), file.errorString());
257 
258  finished();
259  return false;
260  }
261 
262  finished();
263  return true;
264  }
265 
266  data.resize(actuallyRead);
267  totalBytes += actuallyRead;
268 
269  QByteDataBuffer list;
270  list.append(data);
271  data.clear(); // important because of implicit sharing!
272  writeDownstreamData(list);
273  }
274  return true;
275 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QString errorString() const
Returns a human-readable description of the last device error that occurred.
Definition: qiodevice.cpp:1671
void append(QByteDataBuffer &other)
Definition: qbytedata_p.h:77
The QString class provides a Unicode character string.
Definition: qstring.h:83
qint64 nextDownstreamBlockSize() const
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
static QString toString(Register *reg, int type, bool *ok=0)
static const char * data(const QByteArray &arr)
__int64 qint64
Definition: qglobal.h:942
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
void error(QNetworkReply::NetworkError code, const QString &errorString)
void resize(int size)
Sets the size of the byte array to size bytes.
void writeDownstreamData(QByteDataBuffer &list)
FileError error() const
Returns the file error status.
Definition: qfile.cpp:1984
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
void clear()
Clears the contents of the byte array and makes it empty.

◆ uploadReadyReadSlot

void QNetworkAccessFileBackend::uploadReadyReadSlot ( )
slot

Definition at line 167 of file qnetworkaccessfilebackend.cpp.

Referenced by open().

168 {
169  if (hasUploadFinished)
170  return;
171 
172  forever {
173  qint64 haveRead;
174  const char *readPointer = uploadByteDevice->readPointer(-1, haveRead);
175  if (haveRead == -1) {
176  // EOF
177  hasUploadFinished = true;
178  file.flush();
179  file.close();
180  finished();
181  break;
182  } else if (haveRead == 0 || readPointer == 0) {
183  // nothing to read right now, we will be called again later
184  break;
185  } else {
186  qint64 haveWritten;
187  haveWritten = file.write(readPointer, haveRead);
188 
189  if (haveWritten < 0) {
190  // write error!
191  QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Write error writing to %1: %2")
192  .arg(url().toString(), file.errorString());
194 
195  finished();
196  return;
197  } else {
198  uploadByteDevice->advanceReadPointer(haveWritten);
199  }
200 
201 
202  file.flush();
203  }
204  }
205 }
QString errorString() const
Returns a human-readable description of the last device error that occurred.
Definition: qiodevice.cpp:1671
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
virtual const char * readPointer(qint64 maximumLength, qint64 &len)=0
Return a byte pointer for at most maximumLength bytes of that device.
static QString toString(Register *reg, int type, bool *ok=0)
bool flush()
Flushes any buffered data to the file.
Definition: qfile.cpp:1645
__int64 qint64
Definition: qglobal.h:942
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
void error(QNetworkReply::NetworkError code, const QString &errorString)
virtual bool advanceReadPointer(qint64 amount)=0
The old readPointer is invalid after this call.
QNonContiguousByteDevice * uploadByteDevice
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
virtual void close()
Calls QFile::flush() and closes the file.
Definition: qfile.cpp:1680
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452

Properties

◆ file

QFile QNetworkAccessFileBackend::file
private

◆ hasUploadFinished

bool QNetworkAccessFileBackend::hasUploadFinished
private

Definition at line 82 of file qnetworkaccessfilebackend_p.h.

Referenced by uploadReadyReadSlot().

◆ totalBytes

qint64 QNetworkAccessFileBackend::totalBytes
private

Definition at line 81 of file qnetworkaccessfilebackend_p.h.

Referenced by readMoreFromFile().

◆ uploadByteDevice

QNonContiguousByteDevice* QNetworkAccessFileBackend::uploadByteDevice
protected

Definition at line 78 of file qnetworkaccessfilebackend_p.h.

Referenced by open(), and uploadReadyReadSlot().


The documentation for this class was generated from the following files: