📄 playlistmgr.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
///////////////////////////////////////////////////////////////////////////////
// File: PlaylistMgr.cpp
//
// Desc: This file implements a class which houses the Playlist Manager
//
///////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <windev.h>
#include "resource.h"
#include "PlaylistMgr.h"
TCHAR s_szBasePath[] = TEXT("\\My Documents");
static LPCTSTR s_pszExcludePath[] = { TEXT("\\Databases"),
TEXT("\\Release"),
// TEXT("\\Windows"),
TEXT("\\Recycled") };
static LPCTSTR s_pszIncludePath[] = { TEXT("\\"),
TEXT("\\Windows\\Desktop"),
TEXT("\\Windows\\Recent") };
static const int REGKEY_SIZE = 80;
const int s_iExcludePathLength = sizeof (s_pszExcludePath) / sizeof (LPCTSTR);
const int s_iIncludePathLength = sizeof (s_pszIncludePath) / sizeof (LPCTSTR);
extern HINSTANCE g_hInst;
extern DWORD g_cAudioTypes;
extern DWORD g_cVideoTypes;
extern TCHAR ** g_ppszAudioTypes;
extern TCHAR ** g_ppszVideoTypes;
CPlaylistMgr * CPlaylistMgr::ms_pManager = NULL;
CPlaylist * CPlaylistMgr::CurrentPlaylist()
{
list_t * pList = NULL;
int i = 0;
if (m_iCurrent >= 0)
{
pList = m_pPlaylists;
while (pList)
{
if (i == m_iCurrent) break;
pList = pList->pNext;
i++;
}
if (i != m_iCurrent)
{
m_iCurrent = -1;
pList = NULL;
}
}
return (pList ? pList->pPlaylist : NULL);
}
CPlaylist * CPlaylistMgr::LocalContent()
{
if (NULL == m_pLocalContent || 0 == m_pLocalContent->GetCount())
{
UpdateLocalContent();
}
return m_pLocalContent;
}
CPlaylist * CPlaylistMgr::GetPlaylist(int iIndex)
{
list_t * pLists = m_pPlaylists;
for (int i = 0; i < iIndex; i++)
{
if (NULL == pLists)
{
break;
}
pLists = pLists->pNext;
}
return (pLists ? pLists->pPlaylist : NULL);
}
CPlaylist * CPlaylistMgr::GetFavorites()
{
return m_pFavorites;
}
int CPlaylistMgr::MRUPlaylistCount()
{
int iCount;
for (iCount = 0; iCount < MAX_PLAYLIST_HISTORY
&& NULL != m_pszMRUPlaylist[iCount]; iCount++);
return iCount;
}
CPlaylist * CPlaylistMgr::MRUPlaylist(int i)
{
if (0 <= i && i < MAX_PLAYLIST_HISTORY
&& NULL != m_pszMRUPlaylist[i])
{
list_t * pTemp = m_pPlaylists;
while (pTemp)
{
CPlaylist * pPlaylist = pTemp->pPlaylist;
if (pPlaylist && pPlaylist->GetPath())
{
if (0 == _tcscmp(m_pszMRUPlaylist[i], pPlaylist->GetPath()))
{
return pPlaylist;
}
}
pTemp = pTemp->pNext;
}
//
// If the playlist couldn't be found, try loading it. GetPlaylist
// will return NULL if the playlist can not be found.
//
return GetPlaylist(AddPlaylist(m_pszMRUPlaylist[i], 0));
}
return NULL;
}
void CPlaylistMgr::MRURemove(int iIndex)
{
int i;
delete [] m_pszMRUPlaylist[iIndex];
for (i = iIndex; i < MAX_PLAYLIST_HISTORY - 1; i++)
{
m_pszMRUPlaylist[i] = m_pszMRUPlaylist[i + 1];
}
m_pszMRUPlaylist[MAX_PLAYLIST_HISTORY - 1] = NULL;
SaveRegState();
}
void CPlaylistMgr::MRURename(int iIndex, LPCTSTR pszPath)
{
ASSERT(0 <= iIndex && iIndex < MAX_PLAYLIST_HISTORY);
ASSERT(pszPath && m_pszMRUPlaylist[iIndex]);
if( pszPath == 0 )
{
return;
}
delete [] m_pszMRUPlaylist[iIndex];
UINT uLen = _tcslen(pszPath) + 1;
if (m_pszMRUPlaylist[iIndex] = new TCHAR[uLen])
{
m_pszMRUPlaylist[iIndex][uLen - 1] = 0;
_tcscpy(m_pszMRUPlaylist[iIndex], pszPath);
ASSERT(0 == m_pszMRUPlaylist[iIndex][uLen - 1]);
}
else ASSERT(FALSE);
SaveRegState();
}
bool CPlaylistMgr::SetCurrentPlaylist(LPCTSTR pszPath)
{
int iIndex = 0;
list_t * pItem = m_pPlaylists;
bool bResult = false;
while (pItem && !bResult)
{
if (0 == _tcsicmp(pszPath, pItem->pPlaylist->GetPath()))
{
m_iCurrent = iIndex;
bResult = true;
}
iIndex++;
pItem = pItem->pNext;
}
if (!bResult)
{
iIndex = AddPlaylist((LPTSTR)pszPath, iIndex);
if (iIndex >= 0)
{
m_iCurrent = iIndex;
bResult = true;
}
}
if (bResult)
{
UpdateMRUPlaylists();
}
return bResult;
}
bool CPlaylistMgr::SetCurrentPlaylist(int iIndex)
{
int iPos = 0;
list_t * pItem = m_pPlaylists;
bool bResult = false;
if (-1 == iIndex)
{
m_iCurrent = -1;
bResult = true;
}
while (pItem && !bResult)
{
if (iIndex == iPos)
{
m_iCurrent = iIndex;
bResult = true;
}
iPos++;
pItem = pItem->pNext;
}
if (bResult)
{
UpdateMRUPlaylists();
}
return bResult;
}
int CPlaylistMgr::GetCurrentPlaylistID()
{
return m_iCurrent;
}
int CPlaylistMgr::AddPlaylist(LPTSTR pszPath, int iIndex)
{
int iPos = 0;
int iResult = -1;
list_t * pIter = m_pPlaylists;
list_t * pCur = pIter;
list_t * pPrev = NULL;
list_t * pNew = NULL;
list_t * pDupName = NULL;
LPTSTR pszName = NULL;
bool fRet = CPlaylist::GetNameFromPath(pszPath, &pszName);
if (!fRet) return -1;
//
// We do SEVERAL things inside this while loop:
// 1. Make sure we aren't adding a duplicate playlist (where
// "duplicate playlist" is a new reference to an existing
// playlist).
// 2. Notice if new playlist has same name as an existing
// playlist, so we can mark them as having matching names
// later.
// 3. Find the playlist at iIndex so we can insert the new
// playlist there (unless the new playlist is a dup, see #1).
//
while (pIter)
{
LPCTSTR pszTemp = NULL;
if (pIter->pPlaylist
&& (pszTemp = pIter->pPlaylist->GetPath())
&& 0 == _tcscmp(pszPath, pszTemp))
{
// This playlist is already in the list
return iPos;
}
// if the path is different but the name is the
// same, make a note of it and we'll add this
// playlist to the dup names list farther down
if (!pDupName && pIter->pPlaylist
&& (pszTemp = pIter->pPlaylist->GetName())
&& 0 == _tcscmp(pszName, pszTemp))
{
pDupName = pIter;
}
pIter = pIter->pNext;
if (iPos < iIndex)
{
pPrev = pCur;
pCur = pCur->pNext;
ASSERT(pCur == pIter);
}
iPos++;
}
delete [] pszName;
pszName = NULL;
pNew = new list_t;
if (NULL == pNew)
return iResult;
pNew->pPlaylist = new CPlaylist(pszPath);
if (pPrev)
{
iResult = iIndex;
pNew->pNext = pPrev->pNext;
pNew->pPrev = pPrev;
pPrev->pNext = pNew;
if (pCur) pCur->pPrev = pNew;
}
else
{
iResult = 0;
if (m_pPlaylists) m_pPlaylists->pPrev = pNew;
pNew->pNext = m_pPlaylists;
m_pPlaylists = pNew;
}
// if this playlist has a duplicate name, add it to
// the dup names list
if (pDupName)
pNew->AddDupName(pDupName);
return iResult;
}
bool CPlaylistMgr::DeletePlaylist(int iIndex)
{
int iPos = 0;
CPlaylist * pPlaylist;
list_t * pCurr;
BOOL fResult;
bool bResult = false;
pCurr = m_pPlaylists;
while (pCurr && iPos != iIndex)
{
pCurr = pCurr->pNext;
iPos++;
}
if (pCurr && iPos == iIndex)
{
pPlaylist = pCurr->pPlaylist;
fResult = DeleteFile(pPlaylist->GetPath());
if (fResult)
{
LPCTSTR pszPath = pPlaylist->GetPath();
//
// If the playlist was the "current" playlist, make the current
// playlist invalid
//
if (m_iCurrent == iIndex)
{
m_iCurrent = -1;
}
//
// If necessary remove the playlist from the MRU playlist list
//
for (int i = 0; i < MAX_PLAYLIST_HISTORY; i++)
{
if (m_pszMRUPlaylist[i]
&& 0 == _tcscmp(m_pszMRUPlaylist[i], pszPath))
{
MRURemove(i);
break;
}
}
//
// Delete the playlist
//
list_t * pPrev = pCurr->pPrev;
list_t * pNext = pCurr->pNext;
if (pPrev) pPrev->pNext = pNext;
else m_pPlaylists = pNext;
if (pNext) pNext->pPrev = pPrev;
delete pCurr;
bResult = true;
}
}
return bResult;
}
bool CPlaylistMgr::RenamePlaylist(int iIndex, LPTSTR pszName)
{
int iPos = 0;
list_t * pCurr;
bool bResult = false;
TCHAR szOldPath[MAX_PATH];
pCurr = m_pPlaylists;
while (pCurr && iPos != iIndex)
{
pCurr = pCurr->pNext;
iPos++;
}
if (pCurr && iPos == iIndex)
{
_tcsncpy(szOldPath, pCurr->pPlaylist->GetPath(), MAX_PATH);
szOldPath[MAX_PATH-1] = TEXT('\0');
bResult = pCurr->pPlaylist->Rename(pszName);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -