1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-08 15:16:34 +02:00

Use a richer internal model for attica resolvers, and store important things like version

This commit is contained in:
Leo Franchi
2011-09-15 19:04:21 -04:00
parent 22e69bbb8c
commit 95e1d8c5a3
3 changed files with 65 additions and 29 deletions

View File

@@ -65,8 +65,14 @@ AtticaManager::loadPixmapsFromCache()
{ {
// load all the pixmaps // load all the pixmaps
QFileInfo info( file ); QFileInfo info( file );
if ( !m_resolverStates.contains( info.baseName() ) )
{
tLog() << "Found resolver icon cached for resolver we no longer see in synchrotron repo:" << info.baseName();
continue;
}
QPixmap icon( cacheDir.absoluteFilePath( file ) ); QPixmap icon( cacheDir.absoluteFilePath( file ) );
m_resolversIconCache[ info.baseName() ] = icon; m_resolverStates[ info.baseName() ].pixmap = icon;
} }
} }
@@ -81,10 +87,10 @@ AtticaManager::savePixmapsToCache()
cacheDir.cd( "atticache" ); cacheDir.cd( "atticache" );
} }
foreach( const QString& id, m_resolversIconCache.keys() ) foreach( const QString& id, m_resolverStates.keys() )
{ {
const QString filename = cacheDir.absoluteFilePath( QString( "%1.png" ).arg( id ) ); const QString filename = cacheDir.absoluteFilePath( QString( "%1.png" ).arg( id ) );
if ( !m_resolversIconCache[ id ].save( filename ) ) if ( !m_resolverStates[ id ].pixmap.save( filename ) )
{ {
tLog() << "Failed to open cache file for writing:" << filename; tLog() << "Failed to open cache file for writing:" << filename;
continue; continue;
@@ -96,7 +102,7 @@ AtticaManager::savePixmapsToCache()
QPixmap QPixmap
AtticaManager::iconForResolver( const Content& resolver ) AtticaManager::iconForResolver( const Content& resolver )
{ {
return m_resolversIconCache.value( resolver.id(), QPixmap() ); return m_resolverStates.value( resolver.id() ).pixmap;
} }
@@ -115,7 +121,7 @@ AtticaManager::resolverState ( const Content& resolver ) const
return AtticaManager::Uninstalled; return AtticaManager::Uninstalled;
} }
return m_resolverStates[ resolver.id() ]; return m_resolverStates[ resolver.id() ].state;
} }
@@ -129,13 +135,10 @@ AtticaManager::resolversLoaded() const
QString QString
AtticaManager::pathFromId( const QString& resolverId ) const AtticaManager::pathFromId( const QString& resolverId ) const
{ {
foreach( const Content& content, m_resolvers ) if ( !m_resolverStates.contains( resolverId ) )
{
if ( content.id() == resolverId )
return QString( "%1/%2/contents/code/main.js" ).arg( TomahawkUtils::appDataDir().absolutePath() ).arg( QString( "atticaresolvers/%1" ).arg( resolverId ) );
}
return QString(); return QString();
return m_resolverStates.value( resolverId ).scriptPath;
} }
@@ -164,7 +167,10 @@ AtticaManager::resolversList( BaseJob* j )
// load icon cache from disk, and fetch any we are missing // load icon cache from disk, and fetch any we are missing
foreach ( Content resolver, m_resolvers ) foreach ( Content resolver, m_resolvers )
{ {
if ( !m_resolversIconCache.contains( resolver.id() ) && !resolver.icons().isEmpty() && !resolver.icons().first().url().isEmpty() ) if ( !m_resolverStates.contains( resolver.id() ) )
m_resolverStates.insert( resolver.id(), Resolver() );
if ( m_resolverStates.value( resolver.id() ).pixmap.isNull() && !resolver.icons().isEmpty() && !resolver.icons().first().url().isEmpty() )
{ {
QNetworkReply* fetch = TomahawkUtils::nam()->get( QNetworkRequest( resolver.icons().first().url() ) ); QNetworkReply* fetch = TomahawkUtils::nam()->get( QNetworkRequest( resolver.icons().first().url() ) );
fetch->setProperty( "resolverId", resolver.id() ); fetch->setProperty( "resolverId", resolver.id() );
@@ -172,6 +178,8 @@ AtticaManager::resolversList( BaseJob* j )
connect( fetch, SIGNAL( finished() ), this, SLOT( resolverIconFetched() ) ); connect( fetch, SIGNAL( finished() ), this, SLOT( resolverIconFetched() ) );
} }
} }
checkForUpdates();
} }
@@ -192,16 +200,24 @@ AtticaManager::resolverIconFetched()
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
QPixmap icon; QPixmap icon;
icon.loadFromData( data ); icon.loadFromData( data );
m_resolversIconCache[ resolverId ] = icon; m_resolverStates[ resolverId ].pixmap = icon;
} }
void
AtticaManager::checkForUpdates()
{
// look for any newever
}
void void
AtticaManager::installResolver( const Content& resolver ) AtticaManager::installResolver( const Content& resolver )
{ {
Q_ASSERT( !resolver.id().isNull() ); Q_ASSERT( !resolver.id().isNull() );
m_resolverStates[ resolver.id() ] = Installing; m_resolverStates[ resolver.id() ].state = Installing;
m_resolverStates[ resolver.id() ].scriptPath = resolver.attribute( "mainscript" );
m_resolverStates[ resolver.id() ].version = resolver.version();
emit resolverStateChanged( resolver.id() ); emit resolverStateChanged( resolver.id() );
ItemJob< DownloadItem >* job = m_resolverProvider.downloadLink( resolver.id() ); ItemJob< DownloadItem >* job = m_resolverProvider.downloadLink( resolver.id() );
@@ -252,13 +268,17 @@ AtticaManager::payloadFetched()
f.close(); f.close();
QString resolverId = reply->property( "resolverId" ).toString(); QString resolverId = reply->property( "resolverId" ).toString();
QString resolverPath = extractPayload( f.fileName(), resolverId ); QDir dir( extractPayload( f.fileName(), resolverId ) );
QString resolverPath = dir.absoluteFilePath( m_resolverStates[ resolverId ].scriptPath );
if ( !resolverPath.isEmpty() ) if ( !resolverPath.isEmpty() )
{ {
// update with absolute, not relative, path
m_resolverStates[ resolverId ].scriptPath = resolverPath;
// Do the install / add to tomahawk // Do the install / add to tomahawk
Tomahawk::Pipeline::instance()->addScriptResolver( resolverPath, true ); Tomahawk::Pipeline::instance()->addScriptResolver( resolverPath, true );
m_resolverStates[ resolverId ] = Installed; m_resolverStates[ resolverId ].state = Installed;
TomahawkSettings::instance()->setAtticaResolverState( resolverId, Installed ); TomahawkSettings::instance()->setAtticaResolverState( resolverId, Installed );
emit resolverInstalled( resolverId ); emit resolverInstalled( resolverId );
emit resolverStateChanged( resolverId ); emit resolverStateChanged( resolverId );
@@ -336,8 +356,7 @@ AtticaManager::extractPayload( const QString& filename, const QString& resolverI
} while ( zipFile.goToNextFile() ); } while ( zipFile.goToNextFile() );
// The path is *always* resovlerid/contents/code/main.js return resolverDir.absolutePath();
return QString( QFile( resolverDir.absolutePath() + "/contents/code/main.js" ).fileName() );
} }
@@ -355,7 +374,7 @@ AtticaManager::uninstallResolver( const QString& pathToResolver )
{ {
if ( resolver.id() == atticaId ) // this is the one if ( resolver.id() == atticaId ) // this is the one
{ {
m_resolverStates[ atticaId ] = Uninstalled; m_resolverStates[ atticaId ].state = Uninstalled;
TomahawkSettings::instance()->setAtticaResolverState( atticaId, Uninstalled ); TomahawkSettings::instance()->setAtticaResolverState( atticaId, Uninstalled );
doResolverRemove( atticaId ); doResolverRemove( atticaId );
@@ -372,7 +391,7 @@ AtticaManager::uninstallResolver( const Content& resolver )
emit resolverStateChanged( resolver.id() ); emit resolverStateChanged( resolver.id() );
Tomahawk::Pipeline::instance()->removeScriptResolver( pathFromId( resolver.id() ) ); Tomahawk::Pipeline::instance()->removeScriptResolver( pathFromId( resolver.id() ) );
m_resolverStates[ resolver.id() ] = Uninstalled; m_resolverStates[ resolver.id() ].state = Uninstalled;
TomahawkSettings::instance()->setAtticaResolverState( resolver.id(), Uninstalled ); TomahawkSettings::instance()->setAtticaResolverState( resolver.id(), Uninstalled );
doResolverRemove( resolver.id() ); doResolverRemove( resolver.id() );

View File

@@ -46,7 +46,17 @@ public:
Failed Failed
}; };
typedef QHash< QString, AtticaManager::ResolverState > StateHash; struct Resolver {
QString version, scriptPath;
ResolverState state;
QPixmap pixmap;
Resolver( const QString& v, const QString& path, ResolverState s )
: version( v ), scriptPath( path ), state( s ) {}
Resolver() : state( Uninstalled ) {}
};
typedef QHash< QString, AtticaManager::Resolver > StateHash;
static AtticaManager* instance() static AtticaManager* instance()
{ {
@@ -94,6 +104,8 @@ private slots:
void savePixmapsToCache(); void savePixmapsToCache();
void resolverIconFetched(); void resolverIconFetched();
void checkForUpdates();
private: private:
QString extractPayload( const QString& filename, const QString& resolverId ) const; QString extractPayload( const QString& filename, const QString& resolverId ) const;
void doResolverRemove( const QString& id ) const; void doResolverRemove( const QString& id ) const;
@@ -103,8 +115,6 @@ private:
Attica::Provider m_resolverProvider; Attica::Provider m_resolverProvider;
Attica::Content::List m_resolvers; Attica::Content::List m_resolvers;
QHash<QString, QPixmap> m_resolversIconCache;
StateHash m_resolverStates; StateHash m_resolverStates;
#endif #endif

View File

@@ -40,7 +40,10 @@ inline QDataStream& operator<<(QDataStream& out, const AtticaManager::StateHash&
{ {
out << (quint32)states.count(); out << (quint32)states.count();
foreach( const QString& key, states.keys() ) foreach( const QString& key, states.keys() )
out << key << (qint32)states[ key ]; {
AtticaManager::Resolver resolver = states[ key ];
out << key << resolver.version << resolver.scriptPath << (qint32)resolver.state;
}
return out; return out;
} }
@@ -50,11 +53,13 @@ inline QDataStream& operator>>(QDataStream& in, AtticaManager::StateHash& states
in >> count; in >> count;
for ( uint i = 0; i < count; i++ ) for ( uint i = 0; i < count; i++ )
{ {
QString key; QString key, version, scriptPath;
qint32 val; qint32 state;
in >> key; in >> key;
in >> val; in >> version;
states[ key ] = (AtticaManager::ResolverState)val; in >> scriptPath;
in >> state;
states[ key ] = AtticaManager::Resolver( version, scriptPath, (AtticaManager::ResolverState)state );
} }
return in; return in;
} }
@@ -856,7 +861,9 @@ void
TomahawkSettings::setAtticaResolverState( const QString& resolver, AtticaManager::ResolverState state ) TomahawkSettings::setAtticaResolverState( const QString& resolver, AtticaManager::ResolverState state )
{ {
AtticaManager::StateHash resolvers = value( "script/atticaresolverstates" ).value< AtticaManager::StateHash >(); AtticaManager::StateHash resolvers = value( "script/atticaresolverstates" ).value< AtticaManager::StateHash >();
resolvers.insert( resolver, state ); AtticaManager::Resolver r = resolvers.value( resolver );
r.state = state;
resolvers.insert( resolver, r );
setValue( "script/atticaresolverstates", QVariant::fromValue< AtticaManager::StateHash >( resolvers ) ); setValue( "script/atticaresolverstates", QVariant::fromValue< AtticaManager::StateHash >( resolvers ) );
sync(); sync();