xref: /MusicPlayer2/MusicPlayer2/PropertyDlgHelper.cpp (revision 26cefa3ce191eaff4754c23aa55c7bab36bd518b)
1 #include "stdafx.h"
2 #include "PropertyDlgHelper.h"
3 #include "AudioCommon.h"
4 #include "FilePathHelper.h"
5 #include "COSUPlayerHelper.h"
6 #include "AudioTag.h"
7 #include "SongInfoHelper.h"
8 #include "MusicPlayer2.h"
9 
CPropertyDlgHelper(const vector<SongInfo> & songs)10 CPropertyDlgHelper::CPropertyDlgHelper(const vector<SongInfo>& songs)
11     : m_song_info{ songs }
12 {
13 }
14 
~CPropertyDlgHelper()15 CPropertyDlgHelper::~CPropertyDlgHelper()
16 {
17 }
18 
GetMultiFileName()19 wstring CPropertyDlgHelper::GetMultiFileName()
20 {
21     return GetMultiValue([](const SongInfo& song)
22     {
23         return song.GetFileName();
24     }, m_song_info);
25 }
26 
GetMultiFilePath()27 wstring CPropertyDlgHelper::GetMultiFilePath()
28 {
29     return GetMultiValue([](const SongInfo& song)
30     {
31         return song.file_path;
32     }, m_song_info);
33 }
34 
GetMultiType()35 wstring CPropertyDlgHelper::GetMultiType()
36 {
37     return GetMultiValue([](const SongInfo& song)
38     {
39         wstring extension = CFilePathHelper(song.file_path).GetFileExtension();
40         return CAudioCommon::GetAudioDescriptionByExtension(extension);
41     }, m_song_info);
42 }
43 
GetMultiLength()44 wstring CPropertyDlgHelper::GetMultiLength()
45 {
46     wstring multi_length = GetMultiValue([](const SongInfo& song)
47     {
48         return song.length().toString2();
49     }, m_song_info);
50     return multi_length;
51 }
52 
GetMultiSize()53 wstring CPropertyDlgHelper::GetMultiSize()
54 {
55     return GetMultiValue([](const SongInfo& song)
56     {
57         size_t file_size = CCommon::GetFileSize(song.file_path);
58         return wstring(CCommon::DataSizeToString(file_size).GetString());
59     }, m_song_info);
60 }
61 
GetMultiBitrate()62 wstring CPropertyDlgHelper::GetMultiBitrate()
63 {
64     return GetMultiValue([](const SongInfo& song)
65     {
66         return wstring(CSongInfoHelper::GetBitrateString(song).GetString());
67     }, m_song_info);
68 }
69 
GetMultiChannels()70 wstring CPropertyDlgHelper::GetMultiChannels()
71 {
72     return GetMultiValue([](const SongInfo& song)
73         {
74             return CSongInfoHelper::GetChannelsString(song.channels);
75         }, m_song_info);
76 }
77 
GetMultiFreq()78 wstring CPropertyDlgHelper::GetMultiFreq()
79 {
80     return GetMultiValue([](const SongInfo& song)
81         {
82             return wstring(CSongInfoHelper::GetFreqString(song).GetString());
83         }, m_song_info);
84 }
85 
GetMultiBits()86 wstring CPropertyDlgHelper::GetMultiBits()
87 {
88     return GetMultiValue([](const SongInfo& song)
89         {
90             return wstring(CSongInfoHelper::GetBitsString(song).GetString());
91         }, m_song_info);
92 }
93 
GetMultiTitle()94 wstring CPropertyDlgHelper::GetMultiTitle()
95 {
96     return GetMultiValue([](const SongInfo& song)
97     {
98         return song.GetTitle();
99     }, m_song_info);
100 }
101 
GetMultiArtist()102 wstring CPropertyDlgHelper::GetMultiArtist()
103 {
104     return GetMultiValue([](const SongInfo& song)
105     {
106         return song.GetArtist();
107     }, m_song_info);
108 }
109 
GetMultiAlbum()110 wstring CPropertyDlgHelper::GetMultiAlbum()
111 {
112     return GetMultiValue([](const SongInfo& song)
113     {
114         return song.GetAlbum();
115     }, m_song_info);
116 }
117 
GetMultiTrack()118 wstring CPropertyDlgHelper::GetMultiTrack()
119 {
120     return GetMultiValue([](const SongInfo& song)
121     {
122         if (song.track == 0)
123             return wstring();
124         else
125             return std::to_wstring(song.track);
126     }, m_song_info);
127 }
128 
GetMultiYear()129 wstring CPropertyDlgHelper::GetMultiYear()
130 {
131     return GetMultiValue([](const SongInfo& song)
132     {
133         return song.GetYear();
134     }, m_song_info);
135 }
136 
GetMultiGenre()137 wstring CPropertyDlgHelper::GetMultiGenre()
138 {
139     return GetMultiValue([](const SongInfo& song)
140     {
141         return song.GetGenre();
142     }, m_song_info);
143 }
144 
GetMultiComment()145 wstring CPropertyDlgHelper::GetMultiComment()
146 {
147     return GetMultiValue([](const SongInfo& song)
148     {
149         return song.comment;
150     }, m_song_info);
151 }
152 
IsMultiWritable()153 bool CPropertyDlgHelper::IsMultiWritable()
154 {
155     wstring writable_str = GetMultiValue([](const SongInfo& song)
156     {
157         if (song.is_cue || (!COSUPlayerHelper::IsOsuFile(song.file_path) && CAudioTag::IsFileTypeTagWriteSupport(CFilePathHelper(song.file_path).GetFileExtension())))
158             return L"true";
159         else
160             return L"false";
161     }, m_song_info);
162     return writable_str != L"false";
163 }
164 
IsMultiCoverWritable()165 bool CPropertyDlgHelper::IsMultiCoverWritable()
166 {
167     wstring writable_str = GetMultiValue([](const SongInfo& song)
168     {
169         if (!song.is_cue && !COSUPlayerHelper::IsOsuFile(song.file_path) && CAudioTag::IsFileTypeCoverWriteSupport(CFilePathHelper(song.file_path).GetFileExtension()))
170             return L"true";
171         else
172             return L"false";
173     }, m_song_info);
174     return writable_str != L"false";
175 }
176 
IsTitleModified(const vector<SongInfo> & list_ori)177 bool CPropertyDlgHelper::IsTitleModified(const vector<SongInfo>& list_ori)
178 {
179     return IsValueModified([](const SongInfo& song)
180     {
181         return song.GetTitle();
182     }, list_ori);
183 }
184 
IsArtistModified(const vector<SongInfo> & list_ori)185 bool CPropertyDlgHelper::IsArtistModified(const vector<SongInfo>& list_ori)
186 {
187     return IsValueModified([](const SongInfo& song)
188     {
189         return song.GetArtist();
190     }, list_ori);
191 }
192 
IsAlbumModified(const vector<SongInfo> & list_ori)193 bool CPropertyDlgHelper::IsAlbumModified(const vector<SongInfo>& list_ori)
194 {
195     return IsValueModified([](const SongInfo& song)
196     {
197         return song.GetAlbum();
198     }, list_ori);
199 }
200 
IsTrackModified(const vector<SongInfo> & list_ori)201 bool CPropertyDlgHelper::IsTrackModified(const vector<SongInfo>& list_ori)
202 {
203     return IsValueModified([](const SongInfo& song)
204     {
205         return std::to_wstring(song.track);
206     }, list_ori);
207 }
208 
IsYearModified(const vector<SongInfo> & list_ori)209 bool CPropertyDlgHelper::IsYearModified(const vector<SongInfo>& list_ori)
210 {
211     return IsValueModified([](const SongInfo& song)
212     {
213         return song.GetYear();
214     }, list_ori);
215 }
216 
IsGenreModified(const vector<SongInfo> & list_ori)217 bool CPropertyDlgHelper::IsGenreModified(const vector<SongInfo>& list_ori)
218 {
219     return IsValueModified([](const SongInfo& song)
220     {
221         return song.GetGenre();
222     }, list_ori);
223 }
224 
IsCommentModified(const vector<SongInfo> & list_ori)225 bool CPropertyDlgHelper::IsCommentModified(const vector<SongInfo>& list_ori)
226 {
227     return IsValueModified([](const SongInfo& song)
228     {
229         return song.comment;
230     }, list_ori);
231 }
232 
GetMultiValue(std::function<wstring (const SongInfo &)> fun_get_value,const vector<SongInfo> & song_list)233 wstring CPropertyDlgHelper::GetMultiValue(std::function<wstring(const SongInfo&)> fun_get_value, const vector<SongInfo>& song_list)
234 {
235     if (!song_list.empty())
236     {
237         wstring value = fun_get_value(song_list.front());     //第一首歌曲的值
238         int num = static_cast<int>(song_list.size());
239         for (int i{ 1 }; i < num; i++)
240         {
241             if (value != fun_get_value(song_list[i]))         //有一首歌曲的值不同,则返回“多个数值”
242                 return theApp.m_str_table.LoadText(L"TXT_MULTI_VALUE");
243         }
244         return value;       //全部相同,则返回第一个值
245     }
246     else
247     {
248         return wstring();
249     }
250 }
251 
IsValueModified(std::function<wstring (const SongInfo &)> fun_get_value,const vector<SongInfo> & list_ori)252 bool CPropertyDlgHelper::IsValueModified(std::function<wstring(const SongInfo&)> fun_get_value, const vector<SongInfo>& list_ori)
253 {
254     for (size_t i{}; i < m_song_info.size() && i < list_ori.size(); i++)
255     {
256         wstring value_ori = fun_get_value(list_ori[i]);
257         wstring value_new = fun_get_value(m_song_info[i]);
258         if (value_new != value_ori)
259             return true;
260     }
261     return false;
262 }
263 
264