⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playlist.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    assert(item);

    if(item)
    {
        result = RemoveItem(IndexOf(item));
    }

    m_mutex.Release();
    return result;
}

Error PlaylistManager::RemoveItem(uint32 index)
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    index = CheckIndex(index);

    if(index != kInvalidIndex)
    {
        const PlaylistItem* currentItem = GetCurrentItem();

        PlaylistItem* item = (*m_activeList)[index];

        m_activeList->erase(m_activeList->begin() + index);

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
        {
            int32 shuffleIndex = InternalIndexOf(&m_shuffleList, item);

            m_shuffleList.erase(m_shuffleList.begin() + shuffleIndex);

            if(!m_activeList->size())
            {
               m_current = kInvalidIndex;
            }
        }

        /*UndoRemove* undoItem = new UndoRemove(this, item->URL(), index);

        m_undo.AddItem(undoItem);*/

        // 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;
        }

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
        {
            uint32 newIndex = IndexOf(currentItem);
            
            if(newIndex != kInvalidIndex)
            {
                SetCurrentIndex(newIndex);
            }
            else if(m_current != kInvalidIndex && m_current >= m_activeList->size())
            {
                SetCurrentIndex(m_activeList->size() - 1);
            }
            else
            {
                SetCurrentIndex(m_current);
            }
        }

        vector<PlaylistItem*> itemList;
        vector<uint32> indexList;

        itemList.push_back(item);
        indexList.push_back(index);

        m_context->target->AcceptEvent(new PlaylistItemRemovedEvent(&itemList, &indexList, this));

        result = kError_NoErr;
    }

    m_mutex.Release();
    return result;
}

Error PlaylistManager::RemoveItems(uint32 index, uint32 count)
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    index = CheckIndex(index);

    if(index != kInvalidIndex)
    {
        uint32 i;
        const PlaylistItem* currentItem = GetCurrentItem();
        vector<PlaylistItem*> itemList;
        vector<uint32> indexList;

        /*UndoMultiItem* multiItem= new UndoMultiItem();

        for(i = 0; i < count; i++)
        {
            PlaylistItem* item = (*m_activeList)[index + i];

            UndoRemove* undoItem = new UndoRemove(this, item->URL(), index + i);

            multiItem->AddItem(undoItem);
        }

        m_undo.AddItem(multiItem);*/

        for(i = 0; i < count; i++)
        {
            PlaylistItem* item = (*m_activeList)[index + i];

            if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
            {
                int32 shuffleIndex = InternalIndexOf(&m_shuffleList, item);

                m_shuffleList.erase(m_shuffleList.begin() + shuffleIndex);

                if(!m_activeList->size())
                {
                   m_current = kInvalidIndex;
                }
            }            

            // 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;
            }

            itemList.push_back(item);
            indexList.push_back(index + i);
        }

        m_context->target->AcceptEvent(new PlaylistItemRemovedEvent(&itemList, &indexList, this));

        m_activeList->erase(m_activeList->begin() + index, 
                            m_activeList->begin() + index + count);

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
        {
            uint32 newIndex = IndexOf(currentItem);
            
            if(newIndex != kInvalidIndex)
            {
                SetCurrentIndex(newIndex);
            }
            else if(m_current != kInvalidIndex && m_current >= m_activeList->size())
            {
                SetCurrentIndex(m_activeList->size() - 1);
            }
            else
            {
                SetCurrentIndex(m_current);
            }
        }

        result = kError_NoErr;
    }

    m_mutex.Release();
    return result;
}

Error PlaylistManager::RemoveItems(vector<PlaylistItem*>* items)
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    assert(items);

    if(items)
    {
        uint32 index = 0;
        uint32 size = 0;
        PlaylistItem* item = NULL;
        uint32 oldIndex;
        const PlaylistItem* currentItem = GetCurrentItem();
        vector<PlaylistItem*> itemList;
        vector<uint32> indexList;

        size = items->size();

        /*vector<PlaylistItem *>::iterator i = items->begin();

        UndoMultiItem* multiItem= new UndoMultiItem();

        for(; i != items->end(); i++)
        {
            UndoRemove* undoItem = new UndoRemove(this, (*i)->URL(), IndexOf(*i));

            multiItem->AddItem(undoItem);
        }

        m_undo.AddItem(multiItem);*/

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

            if(item)
            {
                oldIndex = IndexOf(item);

                m_activeList->erase(m_activeList->begin() + oldIndex);

                if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
                {
                    int32 shuffleIndex = InternalIndexOf(&m_shuffleList, item);

                    m_shuffleList.erase(m_shuffleList.begin() + shuffleIndex);
                }

                // 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;
                }

                itemList.push_back(item);
                indexList.push_back(oldIndex);                

                result = kError_NoErr;
            }
        }  

        m_context->target->AcceptEvent(new PlaylistItemRemovedEvent(&itemList, &indexList, this));

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
        {
            uint32 newIndex = IndexOf(currentItem);
            
            if(newIndex != kInvalidIndex)
            {
                SetCurrentIndex(newIndex);
            }
            else if(m_current != kInvalidIndex && m_current >= m_activeList->size())
            {
                SetCurrentIndex(m_activeList->size() - 1);
            }
            else
            {
                SetCurrentIndex(m_current);
            }
        }
    }

    m_mutex.Release();
    return result;
}

Error PlaylistManager::RemoveAll()
{
    m_mutex.Acquire();

    Error result = kError_InvalidParam;
    uint32 index = m_activeList->size() - 1;
    //uint32 size = m_activeList->size();
    PlaylistItem* item = NULL;
    vector<PlaylistItem*> itemList;
    vector<uint32> indexList;

    vector<PlaylistItem*>::reverse_iterator i;

    for(i = m_activeList->rbegin(); i !=  m_activeList->rend(); i++, index--)
    {
        item = (*i);

        if(item)
        {            
            /*UndoRemove* undoItem = new UndoRemove(this, item->URL(), IndexOf(item));

            m_undo.AddItem(undoItem);*/

            // 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;
            }          
            
            itemList.push_back(item);
            indexList.push_back(index);
        }
    }  

    m_context->target->AcceptEvent(new PlaylistItemRemovedEvent(&itemList, &indexList, this));

    m_activeList->clear();

    if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
    {
        m_shuffleList.clear();
        m_current = kInvalidIndex;
    }

    result = kError_NoErr;

    m_mutex.Release();
    return result;
}


// Functions for moving items around
Error PlaylistManager::SwapItems(PlaylistItem* item1, PlaylistItem* item2)
{
    return SwapItems(IndexOf(item1), IndexOf(item2));
}

Error PlaylistManager::SwapItems(uint32 index1, uint32 index2)
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    index1 = CheckIndex(index1);
    index2 = CheckIndex(index2);

    if(index1 != kInvalidIndex && index2 != kInvalidIndex)
    {
        PlaylistItem* temp;

        /*UndoMove* undoItem1 = new UndoMove(this, index2, index1);
        m_undo.AddItem(undoItem1);

        UndoMove* undoItem2 = new UndoMove(this, index1, index2);
        m_undo.AddItem(undoItem2);*/

        temp = (*m_activeList)[index1];
        (*m_activeList)[index1] = (*m_activeList)[index2];
        (*m_activeList)[index2] = temp;

        m_context->target->AcceptEvent(
                new PlaylistItemMovedEvent(index1, index2, (*m_activeList)[index2], this));

        // we add 1 to index2 bc it was shifted down by the above move
        m_context->target->AcceptEvent(
                new PlaylistItemMovedEvent(index2 + 1, index1, (*m_activeList)[index1], this));

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
        {
            if(m_current == index1)
                InternalSetCurrentIndex(index2);
            else if(m_current == index2)
                InternalSetCurrentIndex(index1);
        }        

        result = kError_NoErr;
    }

    m_mutex.Release();
    return result;
}

Error PlaylistManager::MoveItem(PlaylistItem* item, uint32 index)
{
    return MoveItem(IndexOf(item), index);
}

Error PlaylistManager::MoveItem(uint32 oldIndex, uint32 newIndex)
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    oldIndex = CheckIndex(oldIndex);
    newIndex = CheckIndex(newIndex);

    if(oldIndex != kInvalidIndex && newIndex != kInvalidIndex)
    {
        PlaylistItem* item = (*m_activeList)[oldIndex];

        /*UndoMove* undoItem = new UndoMove(this, newIndex, oldIndex);
        m_undo.AddItem(undoItem);*/

        m_activeList->erase(m_activeList->begin() + oldIndex);

        if(newIndex > m_activeList->size())
            newIndex = m_activeList->size();

        m_activeList->insert(m_activeList->begin() + newIndex,item);

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
        {
            if(oldIndex == m_current)
                InternalSetCurrentIndex(newIndex);
            else if(newIndex == m_current)
            {
                if(oldIndex > m_current)
                    InternalSetCurrentIndex(m_current + 1);
                else
                    InternalSetCurrentIndex(m_current - 1);
            }
        }

        m_context->target->AcceptEvent(
                new PlaylistItemMovedEvent(oldIndex, newIndex, item, this));

        result = kError_NoErr;
    }

    m_mutex.Release();
    return result;
}

Error PlaylistManager::MoveItems(vector<PlaylistItem*>* items, uint32 index)
{
    Error result = kError_InvalidParam;
    m_mutex.Acquire();

    assert(items);

    index = CheckIndex(index);

    const PlaylistItem* currentItem = GetCurrentItem();
    vector<uint32> oldIndices;

    if(items)
    {
        uint32 size = 0;
        uint32 i = 0;
        PlaylistItem* item = NULL;

        size = items->size();

        for(i = 0; i < size; i++)
        {
            item = (*items)[i];

            if(item)
            {
                uint32 oldIndex = IndexOf(item);

                if(oldIndex < index)
                    index--;

                oldIndices.push_back(oldIndex);
            }
        }

        for(i = 0; i < size; i++)
        {
            item = (*items)[i];

            if(item)
            {
                m_activeList->erase(m_activeList->begin() + IndexOf(item));
            }
        }  

        if(index >= m_activeList->size())
            index = m_activeList->size();

        m_activeList->insert(m_activeList->begin() + index,
                             items->begin(), 
                             items->end());

        if(kPlaylistKey_MasterPlaylist == GetActivePlaylist())
            SetCurrentIndex(IndexOf(currentItem));

        /*UndoMultiItem* multiItem= new UndoMultiItem();

        for(i = 0; i < size; i++)
        {
            UndoMove* undoItem = new UndoMove(this, index + i, oldIndices[i]);
            multiItem.AddItem(undoItem);
        }  

        m_undo.AddItem(multiItem);*/

        for(i = 0; i < size; i++)
        {
            item = (*items)[i];

            if(item)
            {
                m_context->target->AcceptEvent(
                    new PlaylistItemMovedEvent(oldIndices[i], index + i, item, this));
            }
        }

        result = kError_NoErr;
    }

    m_mutex.Release();

⌨️ 快捷键说明

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