欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

playlist.cpp

这是一个mp3的源代码
CPP
第 1 页 / 共 5 页
字号:
        if(!items)
        {
            items = new vector<PlaylistItem*>;
            addToActiveList = true;
        }

        result = device->GetRef()->ReadPlaylist(device, 
                                                items,
                                                function, 
                                                cookie);

        if(addToActiveList)
        {
            AddItems(items);
            delete items;
        }
    }

    return result;
}

#if 0
Error PlaylistManager::ReadPortablePlaylist(DeviceInfo* device,
                                            PLMCallBackFunction function,
                                            void* cookie)
{
    Error result = kError_InvalidParam;

    assert(device);

    if(device)
    {
        // first delete old playlist
        uint32 index = 0;
        uint32 numItems = 0;
        PlaylistItem* item = NULL;

        numItems = m_portableList.size();

        for(index = 0; index < numItems; index++)
        {
            item = m_portableList[index];

            if(item)
            {
                // if the metadata thread is still accessing this item
                // we don't wanna delete the item  out from under it.
                // instead we set a flag and let the metadata thread
                // clean up when it returns.
                if(item->GetState() == kPlaylistItemState_RetrievingMetaData)
                {
                    item->SetState(kPlaylistItemState_Delete);  
                }
                else
                {
                    delete item;
                }
            }
        }

        m_portableList.clear();

        result = device->GetRef()->ReadPlaylist(device, 
                                                &m_portableList,
                                                function, 
                                                cookie);

        vector<PlaylistItem *>::iterator i = m_portableList.begin();
        for(; i != m_portableList.end(); i++)
            m_context->target->AcceptEvent(new PlaylistItemAddedEvent(*i, this));
    }

    return result;
}

#endif

Error PlaylistManager::SyncPortablePlaylist(DeviceInfo* device,
                                            PLMCallBackFunction function,
                                            void* cookie)
{
    Error result = kError_InvalidParam;

    assert(device);

    if(device)
    {
        result = device->GetRef()->WritePlaylist(device, 
                                                &m_externalList,
                                                function, 
                                                cookie);
    }

    return result;
}

Error PlaylistManager::DownloadItemFromPortable(DeviceInfo* device,
                                                PlaylistItem* item,
                                                const char* url,
                                                PLMCallBackFunction function,
                                                void* cookie)
{
    Error result = kError_InvalidParam;

    assert(device);
    assert(item);
    assert(url);

    if(device && item && url)
    {
        result = device->GetRef()->DownloadSong(device, 
                                                item,
                                                url,
                                                function, 
                                                cookie);
    }

    return result;
}

// Utility Functions
bool PlaylistManager::IsEmpty()
{
    bool result;
    m_mutex.Acquire();

    result = m_activeList->empty();

    m_mutex.Release();
    return result;
}

uint32 PlaylistManager::CountItems()
{
    uint32 result;
    m_mutex.Acquire();

    result = m_activeList->size();

    m_mutex.Release();
    return result;
}

PlaylistItem* PlaylistManager::ItemAt(uint32 index)
{
    PlaylistItem* result = NULL;
    m_mutex.Acquire();
    
    index = CheckIndex(index);

    if(index != kInvalidIndex)
    {
        result = (*m_activeList)[index];
    }
    
    m_mutex.Release();
    return result;
}

uint32 PlaylistManager::IndexOf(const PlaylistItem* item)
{
    return InternalIndexOf(m_activeList, item);
}

uint32 PlaylistManager::InternalIndexOf(vector<PlaylistItem*>* list,
                                        const PlaylistItem* item)
{
    uint32 result = kInvalidIndex;
    uint32 index = 0;
    uint32 size = 0;

    assert(list);
    assert(item);

    if(list && item)
    {
        vector<PlaylistItem*>::iterator i = list->begin();
        size = list->size();

        for(index = 0; i != list->end();i++,  index++)
        {
            if(item == (*i))
            {
                result = index;
                break;
            }
        }
    }
    
    return result;
}

bool PlaylistManager::HasItem(const PlaylistItem* item)
{
    return (IndexOf(item) != kInvalidIndex);
}


bool PlaylistManager::CanUndo()
{
    m_mutex.Acquire();
    
    bool result = m_undo.CanUndo();
    
    m_mutex.Release();

    return result;
}

bool PlaylistManager::CanRedo()
{
    m_mutex.Acquire();

    bool result = m_undo.CanRedo();
    
    m_mutex.Release();

    return result;    
}

void PlaylistManager::Undo()
{
    m_mutex.Acquire();
    
    m_undo.Undo();
    
    m_mutex.Release();
}

void PlaylistManager::Redo()
{
    m_mutex.Acquire();
    
    m_undo.Redo();
    
    m_mutex.Release();
}

// Internal functions

inline uint32 PlaylistManager::CheckIndex(uint32 index)
{
	// If we're dealing with a bogus index then set it to -1.
	if(index >= CountItems())
    {
		index = kInvalidIndex;
    }

	return index;
}

void PlaylistManager::AddItemToShuffleList(PlaylistItem* item)
{
    m_shuffleList.push_back(item);
}

void PlaylistManager::AddItemsToShuffleList(vector<PlaylistItem*>* list)
{
    pointer_to_unary_function<int, int> lRand = 
                   pointer_to_unary_function<int, int>(my_rand);

    random_shuffle(list->begin(), list->end(), lRand);

    m_shuffleList.insert(m_shuffleList.end(),
                         list->begin(), 
                         list->end());
}

void
PlaylistManager::
MetaDataThreadFunction(vector<PlaylistItem*>* list)
{
    assert(list);

    if(list)
    {
        uint32 index = 0;
        uint32 numItems = 0;
        PlaylistItem* item = NULL;

        numItems = list->size();

        for(index = 0; index < numItems; index++)
        {
            item = (*list)[index];

            if(item)
            {
                MetaData metadata;
                MetaData* dbData = NULL;

                // is there any metadata stored for this item in the music db?
                dbData = m_context->catalog->ReadMetaDataFromDatabase(item->URL().c_str());

                if(dbData)
                {
                    metadata = *dbData;
                    delete dbData;
                }
                else // if not look to see if the file contains metadata
                {
                    metadata = item->GetMetaData();
                    MetaDataFormat* mdf = NULL;
                    uint32 numFormats;

                    numFormats = m_metadataFormats.size();

                    for(uint32 i = 0; i < numFormats; i++)
                    {
                        mdf = m_metadataFormats[i];

                        if(mdf)
                        {
                            mdf->ReadMetaData(item->URL().c_str(), &metadata);
                        }
                    }
                }

                // check to see if the song length is unknown
                // and if so calculate it.
                if(metadata.Time() == 0)
                {

                }

                m_mutex.Acquire();

                if(item->GetState() == kPlaylistItemState_Delete)
                {
                    delete item;
                }
                else
                {
                    item->SetMetaData(&metadata);
                    item->SetState(kPlaylistItemState_Normal);

                    m_context->target->AcceptEvent(new PlaylistItemUpdatedEvent(item, this));
                }

                m_mutex.Release();
            }
        }

        delete list;
    }
}

typedef struct MetaDataThreadStruct{
    PlaylistManager* plm;
    vector<PlaylistItem*>* items;
    Thread* thread;
} MetaDataThreadStruct;

void 
PlaylistManager::
metadata_thread_function(void* arg)
{
    MetaDataThreadStruct* mts = (MetaDataThreadStruct*)arg;

    mts->plm->MetaDataThreadFunction(mts->items);

    delete mts->thread;
    delete mts;
}

void PlaylistManager::RetrieveMetaData(PlaylistItem* item)
{
    assert(item);

    if(item)
    {
        vector<PlaylistItem*>* items = new vector<PlaylistItem*>;

        if(items)
        {
            items->push_back(item);
    
            RetrieveMetaData(items);
        }
    }
}

void PlaylistManager::RetrieveMetaData(vector<PlaylistItem*>* list)
{
    uint32 index = 0;
    uint32 size = 0;
    PlaylistItem* item = NULL;

    assert(list);

    if(list)
    {
        size = list->size();

        for(index = 0; index < size; index++)
        {
            item = (*list)[index];

            if(item && item->GetState() == kPlaylistItemState_Normal)
            {
                item->SetState(kPlaylistItemState_RetrievingMetaData);
            }
        }

        Thread* thread = Thread::CreateThread();

        if(thread)
        {
            MetaDataThreadStruct* mts = new MetaDataThreadStruct;

            mts->plm = this;
            mts->items = list;
            mts->thread = thread;

            thread->Create(metadata_thread_function, mts);
        }
    }
}


UndoAdd::UndoAdd(PlaylistManager* plm, const string& url, uint32 index):
    m_plm(plm), m_url(url), m_index(index)
{
    
}

UndoAdd::~UndoAdd()
{

}

void UndoAdd::Undo()
{
    m_plm->RemoveItem(m_index);
}

void UndoAdd::Redo()
{
    m_plm->AddItem(m_url, m_index);
}

UndoAddMulti::UndoAddMulti(PlaylistManager* plm, vector<string>& urls, uint32 index):
    m_plm(plm), m_urls(urls), m_index(index)
{
    
}

UndoAddMulti::~UndoAddMulti()
{

}

void UndoAddMulti::Undo()
{
    m_plm->RemoveItems(m_index, m_urls.size());
}

void UndoAddMulti::Redo()
{
    m_plm->AddItems(m_urls, m_index);
}

UndoRemove::UndoRemove(PlaylistManager* plm, const string& url, uint32 index):
    m_plm(plm), m_url(url), m_index(index)
{

}

UndoRemove::~UndoRemove()
{

}

void UndoRemove::Undo()
{
    m_plm->AddItem(m_url, m_index);
}

void UndoRemove::Redo()
{
    m_plm->RemoveItem(m_index);
}

UndoMove::UndoMove(PlaylistManager* plm, uint32 newIndex, uint32 oldIndex):
    m_plm(plm), m_newIndex(newIndex), m_oldIndex(oldIndex)
{

}

UndoMove::~UndoMove()
{
    
}

void UndoMove::Undo()
{
    m_plm->MoveItem(m_newIndex, m_oldIndex);
}

void UndoMove::Redo()
{
    m_plm->MoveItem(m_oldIndex, m_newIndex);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -