Refactored resources framework to use the new LocalResourcesProvider for
scanning local directories and filling its ResourcesDB. The ResourcesDB is responsible for writing local catalogue files. It automatically loads back the appropriate catalogue file (depending on the provider that operates on the ResourcesDB object) at initialization. Will be useful for WebResourcesProvider as well to cache metadata of online resources. All you have to do now is to create an according ResourcesProvider which will automatically setup a ResourcesDB that can be operated on by the ResourcesTreeModel.
This commit is contained in:
@@ -25,24 +25,50 @@
|
||||
#ifndef _LOCAL_RESOURCES_PROVIDER_H
|
||||
#define _LOCAL_RESOURCES_PROVIDER_H
|
||||
|
||||
#include <QtCore/QFileSystemWatcher>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
#include "resources_provider.h"
|
||||
#include "resources_item.h"
|
||||
|
||||
|
||||
class LocalResourcesProvider : public ResourcesProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
LocalResourcesProvider( const QString & _url,
|
||||
ResourcesItem::BaseDirectory _baseDir );
|
||||
LocalResourcesProvider( ResourcesItem::BaseDirectory _baseDir,
|
||||
const QString & _dir );
|
||||
virtual ~LocalResourcesProvider()
|
||||
{
|
||||
}
|
||||
|
||||
virtual ResourcesDB * createResourcesDB( void );
|
||||
virtual QByteArray fetchData( const ResourcesItem * _item );
|
||||
virtual QString providerName( void ) const
|
||||
{
|
||||
return "LocalResourcesProvider";
|
||||
}
|
||||
|
||||
virtual void updateDatabase( void );
|
||||
|
||||
virtual int dataSize( const ResourcesItem * _item ) const;
|
||||
virtual QByteArray fetchData( const ResourcesItem * _item,
|
||||
int _maxSize = -1 ) const;
|
||||
|
||||
|
||||
private slots:
|
||||
void addDirectory( const QString & _path );
|
||||
void removeDirectory( const QString & _path );
|
||||
void reloadDirectory( const QString & _path );
|
||||
|
||||
|
||||
private:
|
||||
void readDir( const QString & _dir, ResourcesTreeItem * _parent );
|
||||
|
||||
ResourcesItem::BaseDirectory m_baseDir;
|
||||
const QString m_dir;
|
||||
|
||||
QStringList m_scannedFolders;
|
||||
|
||||
QFileSystemWatcher m_watcher;
|
||||
|
||||
} ;
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#define _RESOURCES_DB_H
|
||||
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QFileSystemWatcher>
|
||||
#include <QtCore/QHash>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtXml/QDomDocument>
|
||||
@@ -41,12 +40,13 @@ public:
|
||||
typedef QHash<QString, ResourcesItem *> ItemList;
|
||||
|
||||
|
||||
ResourcesDB( const QString & _db_file );
|
||||
ResourcesDB( ResourcesProvider * _provider );
|
||||
~ResourcesDB();
|
||||
|
||||
void scanResources( void );
|
||||
void load( void );
|
||||
void save( void );
|
||||
void init( void );
|
||||
|
||||
void load( const QString & _file );
|
||||
void save( const QString & _file );
|
||||
|
||||
inline const ItemList & items( void ) const
|
||||
{
|
||||
@@ -60,37 +60,27 @@ public:
|
||||
|
||||
const ResourcesItem * nearestMatch( const ResourcesItem & _item );
|
||||
|
||||
void addItem( ResourcesItem * newItem );
|
||||
|
||||
private slots:
|
||||
void reloadDirectory( const QString & _path );
|
||||
|
||||
|
||||
private:
|
||||
void readDir( const QString & _dir, ResourcesTreeItem * _parent,
|
||||
ResourcesItem::BaseDirectory _base_dir );
|
||||
void replaceItem( ResourcesItem * newItem );
|
||||
void recursiveRemoveItems( ResourcesTreeItem * parent,
|
||||
bool removeTopLevelParent = true );
|
||||
|
||||
|
||||
private:
|
||||
void saveTreeItem( const ResourcesTreeItem * _i, QDomDocument & _doc,
|
||||
QDomElement & _de );
|
||||
void loadTreeItem( ResourcesTreeItem * _i, QDomElement & _de );
|
||||
|
||||
|
||||
typedef QList<QPair<ResourcesItem::BaseDirectories, QString> >
|
||||
FolderList;
|
||||
FolderList m_folders;
|
||||
QStringList m_scannedFolders;
|
||||
QFileSystemWatcher m_watcher;
|
||||
|
||||
QString m_dbFile;
|
||||
|
||||
ResourcesProvider * m_provider;
|
||||
ItemList m_items;
|
||||
ResourcesTreeItem m_topLevelNode;
|
||||
|
||||
|
||||
signals:
|
||||
void itemsChanged( void );
|
||||
void directoryItemAdded( const QString & _path );
|
||||
void directoryItemRemoved( const QString & _path );
|
||||
|
||||
} ;
|
||||
|
||||
|
||||
@@ -25,11 +25,14 @@
|
||||
#ifndef _RESOURCES_ITEM_H
|
||||
#define _RESOURCES_ITEM_H
|
||||
|
||||
#include <QtCore/QByteArray>
|
||||
#include <QtCore/QDateTime>
|
||||
|
||||
#include "resources_provider.h"
|
||||
|
||||
class ResourcesTreeItem;
|
||||
|
||||
|
||||
class ResourcesItem
|
||||
{
|
||||
public:
|
||||
@@ -59,8 +62,8 @@ public:
|
||||
} ;
|
||||
typedef Types Type;
|
||||
|
||||
ResourcesItem();
|
||||
ResourcesItem( const QString & _name,
|
||||
ResourcesItem( ResourcesProvider * _provider,
|
||||
const QString & _name,
|
||||
Type _type,
|
||||
BaseDirectory _base_dir = BaseWorkingDir,
|
||||
const QString & _path = QString::null,
|
||||
@@ -70,6 +73,11 @@ public:
|
||||
const QDateTime & _last_mod = QDateTime() );
|
||||
|
||||
|
||||
const ResourcesProvider * provider( void ) const
|
||||
{
|
||||
return m_provider;
|
||||
}
|
||||
|
||||
const QString & name( void ) const
|
||||
{
|
||||
return m_name;
|
||||
@@ -154,6 +162,16 @@ public:
|
||||
m_lastMod = _date;
|
||||
}
|
||||
|
||||
int realSize( void ) const
|
||||
{
|
||||
return m_provider->dataSize( this );
|
||||
}
|
||||
|
||||
QByteArray fetchData( int _maxSize = -1 ) const
|
||||
{
|
||||
return m_provider->fetchData( this );
|
||||
}
|
||||
|
||||
void reload( void );
|
||||
|
||||
bool operator==( const ResourcesItem & _other ) const;
|
||||
@@ -169,6 +187,7 @@ public:
|
||||
private:
|
||||
void init( void );
|
||||
|
||||
ResourcesProvider * m_provider;
|
||||
QString m_name;
|
||||
int m_nameHash;
|
||||
Type m_type;
|
||||
|
||||
@@ -26,31 +26,47 @@
|
||||
#define _RESOURCES_PROVIDER_H
|
||||
|
||||
#include <QtCore/QByteArray>
|
||||
|
||||
#include "resources_item.h"
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QString>
|
||||
|
||||
class ResourcesDB;
|
||||
class ResourcesItem;
|
||||
|
||||
|
||||
class ResourcesProvider
|
||||
class ResourcesProvider : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ResourcesProvider( const QString & url ) :
|
||||
m_url( url )
|
||||
{
|
||||
}
|
||||
ResourcesProvider( const QString & _url );
|
||||
virtual ~ResourcesProvider();
|
||||
|
||||
virtual ResourcesDB * createResourcesDB( void ) = 0;
|
||||
virtual QByteArray fetchData( const ResourcesItem * item ) = 0;
|
||||
virtual QString providerName( void ) const = 0;
|
||||
virtual void updateDatabase( void ) = 0;
|
||||
virtual int dataSize( const ResourcesItem * _item ) const = 0;
|
||||
virtual QByteArray fetchData( const ResourcesItem * _item,
|
||||
int _maxSize = -1 ) const = 0;
|
||||
|
||||
inline const QString & url( void ) const
|
||||
{
|
||||
return m_url;
|
||||
}
|
||||
|
||||
QString localCatalogueFile( void ) const;
|
||||
|
||||
ResourcesDB * database( void )
|
||||
{
|
||||
return m_database;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
ResourcesDB * m_database;
|
||||
QString m_url;
|
||||
|
||||
|
||||
signals:
|
||||
void itemsChanged( void );
|
||||
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
@@ -28,18 +28,26 @@
|
||||
#include <QtCore/QByteArray>
|
||||
|
||||
#include "resources_provider.h"
|
||||
#include "resources_item.h"
|
||||
|
||||
|
||||
class WebResourcesProvider : public ResourcesProvider
|
||||
{
|
||||
public:
|
||||
WebResourcesProvider( const QString & url );
|
||||
WebResourcesProvider( const QString & _url );
|
||||
virtual ~WebResourcesProvider()
|
||||
{
|
||||
}
|
||||
|
||||
virtual ResourcesDB * createResourcesDB( void );
|
||||
virtual QByteArray fetchData( const ResourcesItem * item );
|
||||
virtual int dataSize( const ResourcesItem * _item ) const
|
||||
{
|
||||
// asume that the size we have set before from the web
|
||||
// catalogue is correct
|
||||
return _item->size();
|
||||
}
|
||||
virtual QByteArray fetchData( const ResourcesItem * _item,
|
||||
int _maxSize = -1 ) const;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user