// private methods
int SmartListCtrl::binSearch( int size, int begin, int end, const AssetInfo& assetInfo )
{
	if ( size == 0 || end == begin - 1 )
		return -1;	// this values can happen under normal circumstances

	if ( size < 0 || begin < 0 || end < 0 || end < begin - 1
		|| begin >= size || end >= size )
	{
		// border cases that should not happen
		WARNING_MSG( "SmartListCtrl::binSearch: bad parameters size (%d),"
			" begin (%d) and/or end (%d), searching for %s (%s)\n",
			size, begin, end, assetInfo.text().c_str(),
			assetInfo.longText().c_str() );
		return -1;
	}

	int index = (begin + end) / 2;

	if ( index < 0 || index >= size )
	{
		// this should never happen at this stage
		WARNING_MSG( "SmartListCtrl::binSearch: bad index %d searching for %s (%s)\n",
			index, assetInfo.text().c_str(), assetInfo.longText().c_str() );
		return -1;
	}

	AssetInfo inf = getAssetInfo( index );
	int cmp = _stricmp( inf.text().c_str(), assetInfo.text().c_str() );
	if ( cmp == 0 )
	{
		// found. loop backwards to find the first in case of duplicates
		int i = index;
		while( --i > 0 )
		{
			AssetInfo inf = getAssetInfo( i );
			if ( _stricmp( inf.text().c_str(), assetInfo.text().c_str() ) == 0 )
				--index;
			else
				break;
		}
		return index;
	}
	else if ( begin < end )
	{
		if ( cmp < 0 )
		{
			return binSearch( size, index+1, end, assetInfo );
		}
		else
		{
			return binSearch( size, begin, index-1, assetInfo );
		}
	}
	return -1;
}
Exemplo n.º 2
0
void AssetRequest::start() {
    if (QThread::currentThread() != thread()) {
        QMetaObject::invokeMethod(this, "start", Qt::AutoConnection);
        return;
    }

    if (_state != NOT_STARTED) {
        qCWarning(networking) << "AssetRequest already started.";
        return;
    }
    
    _state = WAITING_FOR_INFO;
    
    auto assetClient = DependencyManager::get<AssetClient>();
    assetClient->getAssetInfo(_hash, _extension, [this](AssetServerError error, AssetInfo info) {
        _info = info;
        _error = error;
        
        if (_error != NoError) {
            qCDebug(networking) << "Got error retrieving asset info for" << _hash;
            _state = FINISHED;
            emit finished(this);
            return;
        }
        
        _state = WAITING_FOR_DATA;
        _data.resize(info.size);
        
        qCDebug(networking) << "Got size of " << _hash << " : " << info.size << " bytes";
        
        int start = 0, end = _info.size;
        
        auto assetClient = DependencyManager::get<AssetClient>();
        assetClient->getAsset(_hash, _extension, start, end, [this, start, end](AssetServerError error,
                                                                                const QByteArray& data) {
            Q_ASSERT(data.size() == (end - start));
            
            _error = error;
            if (_error == NoError) {
                memcpy(_data.data() + start, data.constData(), data.size());
                _totalReceived += data.size();
                emit progress(_totalReceived, _info.size);
            } else {
                qCDebug(networking) << "Got error retrieving asset" << _hash;
            }
            
            _state = FINISHED;
            emit finished(this);
        });
    });
}
bool SmartListCtrl::showItem( const AssetInfo& assetInfo )
{
	int n = GetItemCount();
	int begin = binSearch( n, 0, n-1, assetInfo ); 
	if ( begin == -1 )
	{
		// binSearch didn't find it, so try from the beginning
		begin = 0;	
	}
	for( int i = begin; i < n; ++i )
	{
		AssetInfo inf = getAssetInfo( i );
		if ( _stricmp( inf.longText().c_str(), assetInfo.longText().c_str() ) == 0 )
		{
			SetItemState( -1, 0, LVIS_SELECTED );
			SetItemState( i, LVIS_SELECTED, LVIS_SELECTED );
			EnsureVisible( i, FALSE );
			return true;
		}
	}
	return false;
}
void SmartListCtrl::updateItem( int index, bool removeFromCache )
{
	updateItemInternal( index, getAssetInfo( index ), removeFromCache );
}