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:
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include "fx_mixer.h"
|
||||
#include "fx_mixer_view.h"
|
||||
#include "ladspa_2_lmms.h"
|
||||
#include "local_resources_provider.h"
|
||||
#include "main_window.h"
|
||||
#include "mixer.h"
|
||||
#include "pattern.h"
|
||||
@@ -81,9 +82,11 @@ void engine::init( const bool _has_gui )
|
||||
s_projectJournal = new projectJournal;
|
||||
s_mixer = new mixer;
|
||||
s_song = new song;
|
||||
s_resourcesDB = new ResourcesDB( configManager::inst()->workingDir() +
|
||||
QDir::separator() +
|
||||
".resourcesdb.xml" );
|
||||
|
||||
LocalResourcesProvider * resProv =
|
||||
new LocalResourcesProvider( ResourcesItem::BaseWorkingDir, QString() );
|
||||
s_resourcesDB = resProv->database();
|
||||
|
||||
s_fxMixer = new fxMixer;
|
||||
s_bbTrackContainer = new bbTrackContainer;
|
||||
|
||||
|
||||
264
src/core/local_resources_provider.cpp
Normal file
264
src/core/local_resources_provider.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
* local_resources_provider.cpp - implementation of LocalResourcesProvider
|
||||
*
|
||||
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public
|
||||
* License along with this program (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <QtCore/QDir>
|
||||
|
||||
#include "local_resources_provider.h"
|
||||
#include "resources_db.h"
|
||||
|
||||
|
||||
LocalResourcesProvider::LocalResourcesProvider(
|
||||
ResourcesItem::BaseDirectory _baseDir,
|
||||
const QString & _dir ) :
|
||||
ResourcesProvider( ResourcesItem::getBaseDirectory( _baseDir ) ),
|
||||
m_baseDir( _baseDir ),
|
||||
m_dir( _dir ),
|
||||
m_watcher( this )
|
||||
{
|
||||
connect( &m_watcher, SIGNAL( directoryChanged( const QString & ) ),
|
||||
this, SLOT( reloadDirectory( const QString & ) ) );
|
||||
|
||||
connect( database(), SIGNAL( directoryItemAdded( const QString & ) ),
|
||||
this, SLOT( addDirectory( const QString & ) ) );
|
||||
connect( database(), SIGNAL( directoryItemRemoved( const QString & ) ),
|
||||
this, SLOT( removeDirectory( const QString & ) ) );
|
||||
|
||||
database()->init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LocalResourcesProvider::updateDatabase( void )
|
||||
{
|
||||
readDir( m_dir, database()->topLevelNode() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int LocalResourcesProvider::dataSize( const ResourcesItem * _item ) const
|
||||
{
|
||||
return QFileInfo( _item->fullName() ).size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
QByteArray LocalResourcesProvider::fetchData( const ResourcesItem * _item,
|
||||
int _maxSize ) const
|
||||
{
|
||||
QFile f( _item->fullName() );
|
||||
f.open( QFile::ReadOnly );
|
||||
|
||||
if( _maxSize == -1 )
|
||||
{
|
||||
return f.readAll();
|
||||
}
|
||||
|
||||
return f.read( _maxSize );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LocalResourcesProvider::addDirectory( const QString & _path )
|
||||
{
|
||||
if( QDir( _path ).exists() )
|
||||
{
|
||||
m_watcher.addPath( _path );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LocalResourcesProvider::removeDirectory( const QString & _path )
|
||||
{
|
||||
m_watcher.removePath( _path );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LocalResourcesProvider::reloadDirectory( const QString & _path )
|
||||
{
|
||||
ResourcesTreeItem * dirTreeItem = NULL;
|
||||
|
||||
foreach( ResourcesItem * it, database()->items() )
|
||||
{
|
||||
if( it->type() == ResourcesItem::TypeDirectory &&
|
||||
it->fullPath() == _path )
|
||||
{
|
||||
dirTreeItem = it->treeItem();
|
||||
}
|
||||
}
|
||||
|
||||
if( dirTreeItem )
|
||||
{
|
||||
ResourcesItem * dirItem = dirTreeItem->item();
|
||||
if( dirItem )
|
||||
{
|
||||
m_scannedFolders.clear();
|
||||
readDir( dirItem->path(), dirTreeItem->parent() );
|
||||
}
|
||||
}
|
||||
|
||||
emit itemsChanged();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void LocalResourcesProvider::readDir( const QString & _dir,
|
||||
ResourcesTreeItem * _parent )
|
||||
{
|
||||
#ifdef LMMS_BUILD_LINUX
|
||||
if( _dir.startsWith( "/dev" ) ||
|
||||
_dir.startsWith( "/sys" ) ||
|
||||
_dir.startsWith( "/proc" ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
QDir d( ResourcesItem::getBaseDirectory( m_baseDir ) + _dir );
|
||||
m_scannedFolders << d.canonicalPath();
|
||||
|
||||
ResourcesItem * parentItem;
|
||||
ResourcesTreeItem * curParent = _parent->findChild( d.dirName() +
|
||||
QDir::separator(),
|
||||
m_baseDir );
|
||||
printf("read dir: %s\n", d.canonicalPath().toAscii().constData() );
|
||||
if( curParent )
|
||||
{
|
||||
parentItem = curParent->item();
|
||||
foreachResourcesTreeItem( curParent->children() )
|
||||
{
|
||||
(*it)->setTemporaryMarker( false );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// create new item for current dir
|
||||
parentItem = new ResourcesItem( this,
|
||||
d.dirName(),
|
||||
ResourcesItem::TypeDirectory,
|
||||
m_baseDir,
|
||||
_parent->item() ?
|
||||
_parent->item()->path() + d.dirName() +
|
||||
QDir::separator() :
|
||||
QString::null );
|
||||
parentItem->setLastMod( QFileInfo(
|
||||
d.canonicalPath() ).lastModified() );
|
||||
database()->addItem( parentItem );
|
||||
curParent = new ResourcesTreeItem( _parent, parentItem );
|
||||
curParent->setTemporaryMarker( true );
|
||||
m_watcher.addPath( parentItem->fullPath() );
|
||||
}
|
||||
|
||||
|
||||
QFileInfoList list = d.entryInfoList( QDir::NoDotAndDotDot |
|
||||
QDir::Dirs | QDir::Files |
|
||||
QDir::Readable,
|
||||
QDir::Name | QDir::DirsFirst );
|
||||
foreach( QFileInfo f, list )
|
||||
{
|
||||
if( f.isSymLink() )
|
||||
{
|
||||
f = QFileInfo( f.symLinkTarget() );
|
||||
}
|
||||
|
||||
QString fname = f.fileName();
|
||||
if( f.isDir() )
|
||||
{
|
||||
fname += QDir::separator();
|
||||
}
|
||||
ResourcesTreeItem * curChild =
|
||||
curParent->findChild( fname, m_baseDir );
|
||||
if( curChild )
|
||||
{
|
||||
curChild->setTemporaryMarker( true );
|
||||
if( f.lastModified() > curChild->item()->lastMod() )
|
||||
{
|
||||
curChild->item()->setLastMod(
|
||||
f.lastModified() );
|
||||
if( curChild->item()->type() ==
|
||||
ResourcesItem::TypeDirectory )
|
||||
{
|
||||
readDir( _dir + fname, curParent );
|
||||
}
|
||||
else
|
||||
{
|
||||
curChild->item()->reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( f.isDir() &&
|
||||
!m_scannedFolders.contains(
|
||||
f.canonicalFilePath() ) )
|
||||
|
||||
{
|
||||
readDir( _dir + fname, curParent );
|
||||
}
|
||||
else if( f.isFile() )
|
||||
{
|
||||
ResourcesItem * newItem =
|
||||
new ResourcesItem( this,
|
||||
f.fileName(),
|
||||
ResourcesItem::TypeUnknown,
|
||||
m_baseDir, _dir );
|
||||
newItem->setLastMod( f.lastModified() );
|
||||
database()->addItem( newItem );
|
||||
ResourcesTreeItem * rti =
|
||||
new ResourcesTreeItem( curParent,
|
||||
newItem );
|
||||
rti->setTemporaryMarker( true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( ResourcesTreeItemList::Iterator it = curParent->children().begin();
|
||||
it != curParent->children().end(); )
|
||||
{
|
||||
if( (*it)->temporaryMarker() == false )
|
||||
{
|
||||
ResourcesTreeItem * item = *it;
|
||||
it = curParent->children().erase( it );
|
||||
database()->recursiveRemoveItems( item );
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include "moc_local_resources_provider.cxx"
|
||||
|
||||
@@ -23,32 +23,20 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <QtCore/QDir>
|
||||
#include <QtCore/QFileInfo>
|
||||
|
||||
#include "resources_db.h"
|
||||
#include "config_mgr.h"
|
||||
#include "lmms_basics.h"
|
||||
#include "resources_provider.h"
|
||||
#include "mmp.h"
|
||||
|
||||
|
||||
|
||||
ResourcesDB::ResourcesDB( const QString & _db_file ) :
|
||||
m_watcher( this ),
|
||||
m_dbFile( _db_file )
|
||||
ResourcesDB::ResourcesDB( ResourcesProvider * _provider ) :
|
||||
m_provider( _provider )
|
||||
{
|
||||
m_folders += qMakePair( ResourcesItem::BaseDataDir, QString() );
|
||||
m_folders += qMakePair( ResourcesItem::BaseWorkingDir, QString() );
|
||||
connect( m_provider, SIGNAL( itemsChanged() ),
|
||||
this, SIGNAL( itemsChanged() ) );
|
||||
|
||||
if( QFile::exists( m_dbFile ) )
|
||||
{
|
||||
load();
|
||||
}
|
||||
// (re-) scan directories
|
||||
scanResources();
|
||||
save();
|
||||
|
||||
connect( &m_watcher, SIGNAL( directoryChanged( const QString & ) ),
|
||||
this, SLOT( reloadDirectory( const QString & ) ) );
|
||||
}
|
||||
|
||||
|
||||
@@ -56,17 +44,32 @@ ResourcesDB::ResourcesDB( const QString & _db_file ) :
|
||||
|
||||
ResourcesDB::~ResourcesDB()
|
||||
{
|
||||
save();
|
||||
save( m_provider->localCatalogueFile() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::load( void )
|
||||
void ResourcesDB::init( void )
|
||||
{
|
||||
m_items.clear();
|
||||
if( QFileInfo( m_provider->localCatalogueFile() ).exists() )
|
||||
{
|
||||
load( m_provider->localCatalogueFile() );
|
||||
}
|
||||
|
||||
multimediaProject m( m_dbFile );
|
||||
m_provider->updateDatabase();
|
||||
|
||||
save( m_provider->localCatalogueFile() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::load( const QString & _file )
|
||||
{
|
||||
recursiveRemoveItems( topLevelNode(), false );
|
||||
|
||||
multimediaProject m( _file );
|
||||
|
||||
loadTreeItem( &m_topLevelNode, m.content() );
|
||||
}
|
||||
@@ -74,12 +77,13 @@ void ResourcesDB::load( void )
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::save( void )
|
||||
void ResourcesDB::save( const QString & _file )
|
||||
{
|
||||
multimediaProject m( multimediaProject::ResourcesDatabase );
|
||||
|
||||
saveTreeItem( &m_topLevelNode, m, m.content() );
|
||||
|
||||
m.writeFile( m_dbFile );
|
||||
m.writeFile( _file );
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +128,8 @@ void ResourcesDB::loadTreeItem( ResourcesTreeItem * _i, QDomElement & _de )
|
||||
const QString h = e.attribute( "hash" );
|
||||
if( !h.isEmpty() )
|
||||
{
|
||||
ResourcesItem * item = new ResourcesItem( e.attribute( "name" ),
|
||||
ResourcesItem * item = new ResourcesItem( m_provider,
|
||||
e.attribute( "name" ),
|
||||
static_cast<ResourcesItem::Type>( e.attribute( "type" ).toInt() ),
|
||||
static_cast<ResourcesItem::BaseDirectory>(
|
||||
e.attribute( "basedir" ).toInt() ),
|
||||
@@ -133,12 +138,11 @@ ResourcesItem * item = new ResourcesItem( e.attribute( "name" ),
|
||||
e.attribute( "tags" ),
|
||||
e.attribute( "size" ).toInt(),
|
||||
QDateTime::fromString( e.attribute( "lastmod" ), Qt::ISODate ) );
|
||||
replaceItem( item );
|
||||
addItem( item );
|
||||
ResourcesTreeItem * treeItem = new ResourcesTreeItem( _i, item );
|
||||
if( item->type() == ResourcesItem::TypeDirectory &&
|
||||
QFileInfo( item->fullPath() ).isDir() )
|
||||
if( item->type() == ResourcesItem::TypeDirectory )
|
||||
{
|
||||
m_watcher.addPath( item->fullPath() );
|
||||
emit directoryItemAdded( item->fullPath() );
|
||||
}
|
||||
loadTreeItem( treeItem, e );
|
||||
}
|
||||
@@ -150,18 +154,6 @@ loadTreeItem( treeItem, e );
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::scanResources( void )
|
||||
{
|
||||
for( FolderList::ConstIterator it = m_folders.begin();
|
||||
it != m_folders.end(); ++it )
|
||||
{
|
||||
readDir( it->second, &m_topLevelNode, it->first );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const ResourcesItem * ResourcesDB::nearestMatch( const ResourcesItem & _item )
|
||||
{
|
||||
if( !_item.hash().isEmpty() )
|
||||
@@ -193,38 +185,7 @@ const ResourcesItem * ResourcesDB::nearestMatch( const ResourcesItem & _item )
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::reloadDirectory( const QString & _path )
|
||||
{
|
||||
ResourcesTreeItem * dirTreeItem = NULL;
|
||||
|
||||
foreach( ResourcesItem * it, m_items )
|
||||
{
|
||||
if( it->type() == ResourcesItem::TypeDirectory &&
|
||||
it->fullPath() == _path )
|
||||
{
|
||||
dirTreeItem = it->treeItem();
|
||||
}
|
||||
}
|
||||
|
||||
if( dirTreeItem )
|
||||
{
|
||||
ResourcesItem * dirItem = dirTreeItem->item();
|
||||
if( dirItem )
|
||||
{
|
||||
m_scannedFolders.clear();
|
||||
readDir( dirItem->path(), dirTreeItem->parent(),
|
||||
dirItem->baseDir() );
|
||||
}
|
||||
}
|
||||
|
||||
emit itemsChanged();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::replaceItem( ResourcesItem * newItem )
|
||||
void ResourcesDB::addItem( ResourcesItem * newItem )
|
||||
{
|
||||
const QString hash = newItem->hash();
|
||||
ResourcesItem * oldItem = m_items[hash];
|
||||
@@ -238,7 +199,7 @@ void ResourcesDB::replaceItem( ResourcesItem * newItem )
|
||||
}
|
||||
if( oldItem->type() == ResourcesItem::TypeDirectory )
|
||||
{
|
||||
m_watcher.removePath( oldItem->fullPath() );
|
||||
emit directoryItemRemoved( oldItem->fullPath() );
|
||||
}
|
||||
m_items.remove( hash );
|
||||
delete oldItem;
|
||||
@@ -266,7 +227,7 @@ void ResourcesDB::recursiveRemoveItems( ResourcesTreeItem * parent,
|
||||
{
|
||||
if( parent->item()->type() == ResourcesItem::TypeDirectory )
|
||||
{
|
||||
m_watcher.removePath( parent->item()->fullPath() );
|
||||
emit directoryItemRemoved( parent->item()->fullPath() );
|
||||
}
|
||||
const QString & hash = parent->item()->hash();
|
||||
if( !hash.isEmpty() )
|
||||
@@ -280,132 +241,5 @@ void ResourcesDB::recursiveRemoveItems( ResourcesTreeItem * parent,
|
||||
|
||||
|
||||
|
||||
void ResourcesDB::readDir( const QString & _dir, ResourcesTreeItem * _parent,
|
||||
ResourcesItem::BaseDirectory _base_dir )
|
||||
{
|
||||
#ifdef LMMS_BUILD_LINUX
|
||||
if( _dir.startsWith( "/dev" ) ||
|
||||
_dir.startsWith( "/sys" ) ||
|
||||
_dir.startsWith( "/proc" ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
QDir d( ResourcesItem::getBaseDirectory( _base_dir ) + _dir );
|
||||
m_scannedFolders << d.canonicalPath();
|
||||
|
||||
ResourcesItem * parentItem;
|
||||
ResourcesTreeItem * curParent = _parent->findChild( d.dirName() +
|
||||
QDir::separator(),
|
||||
_base_dir );
|
||||
printf("read dir: %s\n", d.canonicalPath().toAscii().constData() );
|
||||
if( curParent )
|
||||
{
|
||||
parentItem = curParent->item();
|
||||
foreachResourcesTreeItem( curParent->children() )
|
||||
{
|
||||
(*it)->setTemporaryMarker( false );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// create new item for current dir
|
||||
parentItem = new ResourcesItem( d.dirName(),
|
||||
ResourcesItem::TypeDirectory,
|
||||
_base_dir,
|
||||
_parent->item() ?
|
||||
_parent->item()->path() + d.dirName() +
|
||||
QDir::separator() :
|
||||
QString::null );
|
||||
parentItem->setLastMod( QFileInfo(
|
||||
d.canonicalPath() ).lastModified() );
|
||||
replaceItem( parentItem );
|
||||
curParent = new ResourcesTreeItem( _parent, parentItem );
|
||||
curParent->setTemporaryMarker( true );
|
||||
m_watcher.addPath( parentItem->fullPath() );
|
||||
}
|
||||
|
||||
|
||||
QFileInfoList list = d.entryInfoList( QDir::NoDotAndDotDot |
|
||||
QDir::Dirs | QDir::Files |
|
||||
QDir::Readable,
|
||||
QDir::Name | QDir::DirsFirst );
|
||||
foreach( QFileInfo f, list )
|
||||
{
|
||||
if( f.isSymLink() )
|
||||
{
|
||||
f = QFileInfo( f.symLinkTarget() );
|
||||
}
|
||||
|
||||
QString fname = f.fileName();
|
||||
if( f.isDir() )
|
||||
{
|
||||
fname += QDir::separator();
|
||||
}
|
||||
ResourcesTreeItem * curChild =
|
||||
curParent->findChild( fname, _base_dir );
|
||||
if( curChild )
|
||||
{
|
||||
curChild->setTemporaryMarker( true );
|
||||
if( f.lastModified() > curChild->item()->lastMod() )
|
||||
{
|
||||
//printf("reload: %s\n", fname.toAscii().constData());
|
||||
curChild->item()->setLastMod(
|
||||
f.lastModified() );
|
||||
if( curChild->item()->type() ==
|
||||
ResourcesItem::TypeDirectory )
|
||||
{
|
||||
readDir( _dir + fname, curParent,
|
||||
_base_dir );
|
||||
}
|
||||
else
|
||||
{
|
||||
curChild->item()->reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( f.isDir() &&
|
||||
!m_scannedFolders.contains(
|
||||
f.canonicalFilePath() ) )
|
||||
|
||||
{
|
||||
readDir( _dir + fname, curParent, _base_dir );
|
||||
}
|
||||
else if( f.isFile() )
|
||||
{
|
||||
ResourcesItem * newItem =
|
||||
new ResourcesItem( f.fileName(),
|
||||
ResourcesItem::TypeUnknown,
|
||||
_base_dir, _dir );
|
||||
newItem->setLastMod( f.lastModified() );
|
||||
replaceItem( newItem );
|
||||
ResourcesTreeItem * ti =
|
||||
new ResourcesTreeItem( curParent,
|
||||
newItem );
|
||||
ti->setTemporaryMarker( true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( ResourcesTreeItemList::Iterator it = curParent->children().begin();
|
||||
it != curParent->children().end(); )
|
||||
{
|
||||
if( (*it)->temporaryMarker() == false )
|
||||
{
|
||||
//printf("removing %d %s\n", (*it)->item(), (*it)->item()->name().toAscii().constData() );
|
||||
recursiveRemoveItems( *it );
|
||||
it = curParent->children().erase( it );
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include "moc_resources_db.cxx"
|
||||
|
||||
@@ -28,29 +28,13 @@
|
||||
#include <QtCore/QDir>
|
||||
|
||||
#include "resources_item.h"
|
||||
#include "resources_provider.h"
|
||||
#include "config_mgr.h"
|
||||
|
||||
|
||||
|
||||
ResourcesItem::ResourcesItem() :
|
||||
m_name(),
|
||||
m_nameHash( 0 ),
|
||||
m_type( TypeUnknown ),
|
||||
m_baseDir( BaseRoot ),
|
||||
m_path(),
|
||||
m_hash(),
|
||||
m_size( -1 ),
|
||||
m_lastMod(),
|
||||
m_tags(),
|
||||
m_treeItem( NULL )
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ResourcesItem::ResourcesItem( const QString & _name,
|
||||
ResourcesItem::ResourcesItem( ResourcesProvider * _provider,
|
||||
const QString & _name,
|
||||
Type _type,
|
||||
BaseDirectory _base_dir,
|
||||
const QString & _path,
|
||||
@@ -58,6 +42,7 @@ ResourcesItem::ResourcesItem( const QString & _name,
|
||||
const QString & _tags,
|
||||
int _size,
|
||||
const QDateTime & _last_mod ) :
|
||||
m_provider( _provider ),
|
||||
m_name( _name ),
|
||||
m_nameHash( 0 ),
|
||||
m_type( _type ),
|
||||
@@ -238,21 +223,14 @@ void ResourcesItem::init( void )
|
||||
{
|
||||
if( m_size < 0 )
|
||||
{
|
||||
m_size = QFileInfo( fullName() ).size();
|
||||
m_size = realSize();
|
||||
}
|
||||
if( m_hash.isEmpty() )
|
||||
{
|
||||
QCryptographicHash h( QCryptographicHash::Sha1 );
|
||||
|
||||
QFile f( fullName() );
|
||||
f.open( QFile::ReadOnly );
|
||||
|
||||
const int chunkSize = 1024*1024; // 1 MB
|
||||
for( int i = 0; i < f.size() / chunkSize; ++i )
|
||||
{
|
||||
h.addData( f.read( chunkSize ) );
|
||||
}
|
||||
h.addData( f.readAll() );
|
||||
// fetch at most 1 MB for creating hash
|
||||
h.addData( fetchData( 1 * 1024 * 1024 ) );
|
||||
|
||||
m_hash = h.result().toHex();
|
||||
}
|
||||
@@ -272,12 +250,15 @@ QString ResourcesItem::getBaseDirectory( BaseDirectory _bd )
|
||||
case BaseRoot:
|
||||
d = QDir::rootPath();
|
||||
break;
|
||||
|
||||
case BaseWorkingDir:
|
||||
d = configManager::inst()->workingDir();
|
||||
break;
|
||||
|
||||
case BaseDataDir:
|
||||
d = configManager::inst()->dataDir();
|
||||
break;
|
||||
|
||||
case BaseHome:
|
||||
default:
|
||||
d = QDir::homePath();
|
||||
|
||||
70
src/core/resources_provider.cpp
Normal file
70
src/core/resources_provider.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* resources_provider.cpp - implementation of ResourcesProvider
|
||||
*
|
||||
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public
|
||||
* License along with this program (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <QtCore/QCryptographicHash>
|
||||
#include <QtCore/QDir>
|
||||
|
||||
#include "resources_provider.h"
|
||||
#include "resources_db.h"
|
||||
#include "config_mgr.h"
|
||||
|
||||
|
||||
ResourcesProvider::ResourcesProvider( const QString & _url ) :
|
||||
m_database( new ResourcesDB( this ) ),
|
||||
m_url( _url )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ResourcesProvider::~ResourcesProvider()
|
||||
{
|
||||
delete m_database;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
QString ResourcesProvider::localCatalogueFile( void ) const
|
||||
{
|
||||
const QString dir = configManager::inst()->workingDir() +
|
||||
"catalogs" + QDir::separator();
|
||||
if( !QDir( dir ).exists() )
|
||||
{
|
||||
QDir().mkpath( dir );
|
||||
}
|
||||
|
||||
QCryptographicHash h( QCryptographicHash::Md5 );
|
||||
|
||||
h.addData( QString( providerName() + url() ).toUtf8() );
|
||||
|
||||
return dir + h.result().toHex();
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include "moc_resources_provider.cxx"
|
||||
|
||||
Reference in New Issue
Block a user