Qt 4.8
Public Functions | Private Types | Private Slots | Private Functions | Properties | List of all members
QDeclarativeDataLoader Class Reference

The QDeclarativeDataLoader class abstracts loading files and their dependencies over the network. More...

#include <qdeclarativetypeloader_p.h>

Inheritance diagram for QDeclarativeDataLoader:
QObject QDeclarativeTypeLoader

Public Functions

QDeclarativeEngineengine () const
 Return the QDeclarativeEngine associated with this loader. More...
 
void load (QDeclarativeDataBlob *)
 Load the provided blob from the network or filesystem. More...
 
void loadWithStaticData (QDeclarativeDataBlob *, const QByteArray &)
 Load the provided blob with data. More...
 
 QDeclarativeDataLoader (QDeclarativeEngine *)
 Create a new QDeclarativeDataLoader for engine. More...
 
 ~QDeclarativeDataLoader ()
 
- 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...
 

Private Types

typedef QHash< QNetworkReply *, QDeclarativeDataBlob * > NetworkReplies
 

Private Slots

void networkReplyFinished ()
 
void networkReplyProgress (qint64, qint64)
 

Private Functions

void networkReplyFinished (QNetworkReply *)
 
void networkReplyProgress (QNetworkReply *, qint64, qint64)
 
void setData (QDeclarativeDataBlob *, const QByteArray &)
 

Properties

QDeclarativeEnginem_engine
 
NetworkReplies m_networkReplies
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- 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 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...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QDeclarativeDataLoader class abstracts loading files and their dependencies over the network.

Warning
This function is not part of the public interface.

The QDeclarativeDataLoader class is provided for the exclusive use of the QDeclarativeTypeLoader class.

Clients create QDeclarativeDataBlob instances and submit them to the QDeclarativeDataLoader class through the QDeclarativeDataLoader::load() or QDeclarativeDataLoader::loadWithStaticData() methods. The loader then fetches the data over the network or from the local file system in an efficient way. QDeclarativeDataBlob is an abstract class, so should always be specialized.

Once data is received, the QDeclarativeDataBlob::dataReceived() method is invoked on the blob. The derived class should use this callback to process the received data. Processing of the data can result in an error being set (QDeclarativeDataBlob::setError()), or one or more dependencies being created (QDeclarativeDataBlob::addDependency()). Dependencies are other QDeclarativeDataBlob's that are required before processing can fully complete.

To complete processing, the QDeclarativeDataBlob::done() callback is invoked. done() is called when one of these three preconditions are met.

  1. The QDeclarativeDataBlob has no dependencies.
  2. The QDeclarativeDataBlob has an error set.
  3. All the QDeclarativeDataBlob's dependencies are themselves "done()".

Thus QDeclarativeDataBlob::done() will always eventually be called, even if the blob has an error set.

Definition at line 157 of file qdeclarativetypeloader_p.h.

Typedefs

◆ NetworkReplies

Definition at line 179 of file qdeclarativetypeloader_p.h.

Constructors and Destructors

◆ QDeclarativeDataLoader()

QDeclarativeDataLoader::QDeclarativeDataLoader ( QDeclarativeEngine engine)

Create a new QDeclarativeDataLoader for engine.

Definition at line 506 of file qdeclarativetypeloader.cpp.

507 : m_engine(engine)
508 {
509 }

◆ ~QDeclarativeDataLoader()

QDeclarativeDataLoader::~QDeclarativeDataLoader ( )
Warning
This function is not part of the public interface.

Definition at line 512 of file qdeclarativetypeloader.cpp.

513 {
514  for (NetworkReplies::Iterator iter = m_networkReplies.begin(); iter != m_networkReplies.end(); ++iter)
515  (*iter)->release();
516 }
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
iterator Iterator
Qt-style synonym for QHash::iterator.
Definition: qhash.h:473

Functions

◆ engine()

QDeclarativeEngine * QDeclarativeDataLoader::engine ( ) const

Return the QDeclarativeEngine associated with this loader.

Definition at line 654 of file qdeclarativetypeloader.cpp.

Referenced by QDeclarativeTypeData::resolveTypes().

655 {
656  return m_engine;
657 }

◆ load()

void QDeclarativeDataLoader::load ( QDeclarativeDataBlob blob)

Load the provided blob from the network or filesystem.

Definition at line 521 of file qdeclarativetypeloader.cpp.

Referenced by QDeclarativeTypeLoader::get(), QDeclarativeTypeLoader::getQmldir(), and QDeclarativeTypeLoader::getScript().

522 {
524  Q_ASSERT(blob->m_manager == 0);
525 
527 
528  if (blob->m_url.isEmpty()) {
530  error.setDescription(QLatin1String("Invalid null URL"));
531  blob->setError(error);
532  return;
533  }
534 
536 
537  if (!lf.isEmpty()) {
540  error.setUrl(blob->m_url);
541  error.setDescription(QLatin1String("File name case mismatch"));
542  blob->setError(error);
543  return;
544  }
545  QFile file(lf);
546  if (file.open(QFile::ReadOnly)) {
547  QByteArray data = file.readAll();
548 
549  blob->m_progress = 1.;
550  blob->downloadProgressChanged(1.);
551 
552  setData(blob, data);
553  } else {
555  }
556 
557  } else {
558 
559  blob->m_manager = this;
561 
562  m_networkReplies.insert(reply, blob);
563  blob->addref();
564 
565  if (reply->isFinished()) {
566  // Short-circuit synchronous replies.
567  qint64 size = reply->size();
568  networkReplyProgress(reply, size, size);
569  networkReplyFinished(reply);
570  } else {
571  QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
573  QObject::connect(reply, SIGNAL(finished()),
574  this, SLOT(networkReplyFinished()));
575  }
576  }
577 }
static QString urlToLocalFileOrQrc(const QUrl &url)
virtual void downloadProgressChanged(qreal)
Called when the download progress of this blob changes.
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Definition: qiodevice.cpp:642
void setDescription(const QString &)
Sets the error description.
QDeclarativeDataLoader * m_manager
#define error(msg)
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Definition: qnetworkreply.h:65
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition: qurl.cpp:4317
#define SLOT(a)
Definition: qobjectdefs.h:226
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void networkReplyProgress(qint64, qint64)
void setData(QDeclarativeDataBlob *, const QByteArray &)
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
#define SIGNAL(a)
Definition: qobjectdefs.h:227
bool QDeclarative_isFileCaseCorrect(const QString &fileName)
Returns true if the case of fileName is equivalent to the file case of fileName on disk...
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
static const char * data(const QByteArray &arr)
The QDeclarativeError class encapsulates a QML error.
__int64 qint64
Definition: qglobal.h:942
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
Status status() const
Returns the blob&#39;s status.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
void setUrl(const QUrl &)
Sets the url for the file that caused this error.
bool isFinished() const
Returns true when the reply has finished or was aborted.
void setError(const QDeclarativeError &)
Mark this blob as having errors.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QNetworkAccessManager * networkAccessManager() const
Returns a common QNetworkAccessManager which can be used by any QML element instantiated by this engi...
virtual void networkError(QNetworkReply::NetworkError)
Invoked if there is a network error while fetching this blob.

◆ loadWithStaticData()

void QDeclarativeDataLoader::loadWithStaticData ( QDeclarativeDataBlob blob,
const QByteArray data 
)

Load the provided blob with data.

The blob's URL is not used by the data loader in this case.

Definition at line 641 of file qdeclarativetypeloader.cpp.

Referenced by QDeclarativeTypeLoader::get().

642 {
644  Q_ASSERT(blob->m_manager == 0);
645 
647 
648  setData(blob, data);
649 }
QDeclarativeDataLoader * m_manager
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void setData(QDeclarativeDataBlob *, const QByteArray &)
Status status() const
Returns the blob&#39;s status.

◆ networkReplyFinished [1/2]

void QDeclarativeDataLoader::networkReplyFinished ( )
privateslot

Definition at line 614 of file qdeclarativetypeloader.cpp.

Referenced by load(), and networkReplyFinished().

615 {
616  QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
617  networkReplyFinished(reply);
618 }
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Definition: qnetworkreply.h:65
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition: qobject.cpp:2327

◆ networkReplyFinished() [2/2]

void QDeclarativeDataLoader::networkReplyFinished ( QNetworkReply reply)
private

Definition at line 581 of file qdeclarativetypeloader.cpp.

582 {
583  reply->deleteLater();
584 
586 
587  Q_ASSERT(blob);
588 
589  blob->m_redirectCount++;
590 
593  if (redirect.isValid()) {
594  QUrl url = reply->url().resolved(redirect.toUrl());
595  blob->m_finalUrl = url;
596 
598  QObject::connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished()));
599  m_networkReplies.insert(reply, blob);
600  return;
601  }
602  }
603 
604  if (reply->error()) {
605  blob->networkError(reply->error());
606  } else {
607  QByteArray data = reply->readAll();
608  setData(blob, data);
609  }
610 
611  blob->release();
612 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDeclarativeDataBlob encapsulates a data request that can be issued to a QDeclarativeDataLoader.
NetworkError error() const
Returns the error that was found during the processing of this request.
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code code.
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Definition: qnetworkreply.h:65
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
Definition: qhash.h:807
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void setData(QDeclarativeDataBlob *, const QByteArray &)
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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
static const char * data(const QByteArray &arr)
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
#define DATALOADER_MAXIMUM_REDIRECT_RECURSION
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QUrl url() const
Returns the URL of the content downloaded or uploaded.
QNetworkAccessManager * networkAccessManager() const
Returns a common QNetworkAccessManager which can be used by any QML element instantiated by this engi...
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
Definition: qurl.cpp:5819
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
virtual void networkError(QNetworkReply::NetworkError)
Invoked if there is a network error while fetching this blob.

◆ networkReplyProgress [1/2]

void QDeclarativeDataLoader::networkReplyProgress ( qint64  bytesReceived,
qint64  bytesTotal 
)
privateslot

Definition at line 632 of file qdeclarativetypeloader.cpp.

Referenced by load().

633 {
634  QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
635  networkReplyProgress(reply, bytesReceived, bytesTotal);
636 }
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Definition: qnetworkreply.h:65
void networkReplyProgress(qint64, qint64)
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition: qobject.cpp:2327

◆ networkReplyProgress() [2/2]

void QDeclarativeDataLoader::networkReplyProgress ( QNetworkReply reply,
qint64  bytesReceived,
qint64  bytesTotal 
)
private

Definition at line 620 of file qdeclarativetypeloader.cpp.

621 {
623 
624  Q_ASSERT(blob);
625 
626  if (bytesTotal != 0) {
627  blob->m_progress = bytesReceived / bytesTotal;
628  blob->downloadProgressChanged(blob->m_progress);
629  }
630 }
The QDeclarativeDataBlob encapsulates a data request that can be issued to a QDeclarativeDataLoader.
virtual void downloadProgressChanged(qreal)
Called when the download progress of this blob changes.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606

◆ setData()

void QDeclarativeDataLoader::setData ( QDeclarativeDataBlob blob,
const QByteArray data 
)
private

Definition at line 659 of file qdeclarativetypeloader.cpp.

Referenced by load(), loadWithStaticData(), and networkReplyFinished().

660 {
661  blob->m_inCallback = true;
662 
663  blob->dataReceived(data);
664 
665  if (!blob->isError() && !blob->isWaiting())
666  blob->allDependenciesDone();
667 
668  if (blob->status() != QDeclarativeDataBlob::Error)
670 
671  blob->m_inCallback = false;
672 
673  blob->tryDone();
674 }
bool isError() const
Returns true if the status is Error.
virtual void allDependenciesDone()
Called when all blobs waited for have completed.
Status status() const
Returns the blob&#39;s status.
bool isWaiting() const
Returns true if the status is WaitingForDependencies.
virtual void dataReceived(const QByteArray &)=0
Invoked when data for the blob is received.

Properties

◆ m_engine

QDeclarativeEngine* QDeclarativeDataLoader::m_engine
private

Definition at line 178 of file qdeclarativetypeloader_p.h.

Referenced by engine(), load(), and networkReplyFinished().

◆ m_networkReplies

NetworkReplies QDeclarativeDataLoader::m_networkReplies
private

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