xref: /MusicPlayer2/MusicPlayer2/MusicPlayerCmdHelper.h (revision 800a68ca4bdb8c4896c14aa157f6b72b26c482d9)
1 #pragma once
2 #include "ListItem.h"
3 #include "FormatConvertDlg.h"
4 
5 class CMusicPlayerCmdHelper
6 {
7 public:
8     CMusicPlayerCmdHelper(CWnd* pOwner = nullptr);
9     ~CMusicPlayerCmdHelper();
10 
11     void VeiwOnline(SongInfo& song);
12 
13     //执行“在线查看”的线程函数,lpParam为SongInfo类型的指针,必须确保线程结束前指针不会被析构
14     static UINT ViewOnlineThreadFunc(LPVOID lpParam);
15 
16     void FormatConvert(const std::vector<SongInfo>& songs);
17 
18     //执行添加到新建播放列表命令,成功返回true
19     //get_song_list: 获取要添加的文件列表的回调函数,函数原型为 void Func(std::vector<SongInfo>&)
20     //playlist_path: 接收新播放列表的路径
21     //default_name:新播放列表的默认名称
22     bool OnAddToNewPlaylist(std::function<void(std::vector<SongInfo>&)> get_song_list, std::wstring& playlist_path, const std::wstring& default_name = L"");
23 
24     bool OnAddToPlaylistCommand(std::function<void(std::vector<SongInfo>&)> get_song_list, DWORD command);
25 
26     //从磁盘删除歌曲
27     bool DeleteSongsFromDisk(const std::vector<SongInfo>& files);
28 
29     //查找可能匹配的所有歌词文件
30     void SearchLyricFiles(const wstring& lyric_name, const wstring& cur_dir, std::vector<std::wstring>& result, bool fuzzy_match);
31     //查找最佳匹配的歌词文件并返回路径
32     std::wstring SearchLyricFile(const SongInfo& song, bool fuzzy_match);
33 
34     //查找匹配的外部专辑封面
35     std::wstring SearchAlbumCover(const SongInfo& song);
36 
37     //响应歌曲分级命令
38     //(分级数据会写入到音频文件和song_data.dat文件中。如果文件写入失败,则返回false,否则返回true。
39     //但是如果此文件格式不支持将分级,则只会写入到song_data.dat文件中,函数仍然返回true)
40     bool OnRating(const SongInfo& song, DWORD command);
41 
42     //更新媒体库,返回新增的歌曲数。(此函数执行时间可能会较长,应该在后台线程中执行)
43     static int UpdateMediaLib();
44 
45     //清理数据,函数对象fun_condition用来判断文件是否要被清理,如果是则返回true
46     static int CleanUpSongData(std::function<bool(const SongInfo&)> fun_condition = [&](const SongInfo& song) { return !CCommon::FileExist(song.file_path); });
47 
48     // 清除最近播放文件夹中的无效条目
49     static int CleanUpRecentFolders();
50 
51     enum eMediaLibTab
52     {
53         ML_FOLDER = 0,
54         ML_PLAYLIST = 1,
55         ML_ARTIST,
56         ML_ALBUM,
57         ML_GENRE,
58         ML_YEAR,
59         ML_FILE_TYPE,
60         ML_BITRATE,
61         ML_RATING,
62         ML_ALL
63     };
64 
65     std::wstring GetMediaLibTabName(eMediaLibTab tab);
66 
67     //显示媒体库对话框
68     //cur_tab: 打开对话框后要切换的标签,如果为-1,则保持上次打开的标签
69     //tab_force_show: 要强制显示的标签,使用int中的各个bit表示要显示的标签,每个bit参见枚举 MediaLibDisplayItem 的声明
70     void ShowMediaLib(int cur_tab = -1, int tab_force_show = 0);
71 
72     //刷新媒体库指定标签页,0刷新文件夹,1刷新播放列表
73     static void RefreshMediaTabData(eMediaLibTab tab);
74 
75     //在媒体库中查看
76     void OnViewInMediaLib(eMediaLibTab tab, const std::wstring name);
77     void OnViewInMediaLib(const ListItem& list_item);
78 
79     //查看艺术家
80     void OnViewArtist(const SongInfo& song_info);
81 
82     //查看唱片集
83     void OnViewAlbum(const SongInfo& song_info);
84 
85     //修正播放列表中的错误的路径
86     int FixPlaylistPathError(const std::wstring& path) const;
87 
88     //如果file_path不存在,则从媒体库中查找文件名相同的最佳匹配的项目,并将file_path更改为正确的路径
89     //file_name_map 保存媒体库目录下音频文件文件名和文件路径的对应关系
90     bool FixWrongFilePath(wstring& file_path, const std::unordered_map<std::wstring, std::set<std::wstring>>& file_name_map) const;
91 
92     // 播放一个ListItem,play为true用于直接双击ListItem列表及一系列等价情况
93     // play为false用于快捷切换列表菜单命令处理
94     // 不设置播放特定曲目,list_item为空时直接返回
95     void OnListItemSelected(const ListItem& list_item, bool play = false);
96 
97     bool OnRenamePlaylist(const ListItem& list_item);
98     // 询问新播放列表名称后创建新播放列表,返回新播放列表的路径
99     // 提供源播放列表copy_from时执行创建播放列表副本
100     wstring OnNewPlaylist(const wstring& copy_from_playlist = {});
101     void OnPlaylistSaveAs(const std::wstring& playlist_path);
102     bool OnPlaylistFixPathError(const std::wstring& playlist_path);
103 
104     // 询问后执行删除最近播放项目操作,需要刷新显示时返回true
105     // 目前仅能用于LT_FOLDER/LT_PLAYLIST
106     bool OnDeleteRecentListItem(const ListItem& list_item);
107 
108     bool OnOpenFolder();
109     bool OnOpenFolder(std::wstring folder_path, bool include_sub_dir, bool play);
110 
111     //从播放列表中移除
112     bool OnRemoveFromPlaylist(const ListItem& list_item, const std::vector<SongInfo>& songs);
113     //从正在播放的播放列表中移除
114     bool OnRemoveFromCurrentPlaylist(const std::vector<int>& indexs);
115 
116     void OnPlayMyFavourite(const SongKey& song_key);
117     void OnPlayMyFavourite();
118     void OnPlayAllTrack(const SongInfo& song);
119 
120     void OnPlayTrack(int track);
121 
122     bool OnAddRemoveFromFavourite(int track);
123     bool OnAddRemoveFromFavourite(const SongInfo& song);
124     bool OnAddToFavourite();
125 
126 protected:
127     void AddToPlaylist(const std::vector<SongInfo>& songs, const std::wstring& playlist_path);
128 
129 private:
130     CWnd* GetOwner();
131 
132 private:
133     CWnd* m_pOwner{};
134 };
135