xref: /MusicPlayer2/MusicPlayer2/UpdateHelper.cpp (revision 6b26495bd86af2683098541e5e1edbeadf657e88)
1 #include "stdafx.h"
2 #include "UpdateHelper.h"
3 #include "SimpleXML.h"
4 #include "InternetCommon.h"
5 
6 
CUpdateHelper()7 CUpdateHelper::CUpdateHelper()
8 {
9 }
10 
11 
~CUpdateHelper()12 CUpdateHelper::~CUpdateHelper()
13 {
14 }
15 
SetUpdateSource(UpdateSource update_source)16 void CUpdateHelper::SetUpdateSource(UpdateSource update_source)
17 {
18     m_update_source = update_source;
19 }
20 
CheckForUpdate()21 bool CUpdateHelper::CheckForUpdate()
22 {
23     wstring version_info;
24     //使用GitHub更新源
25     if (m_update_source == UpdateSource::GitHubSource)
26     {
27         if (CInternetCommon::GetURL(L"https://raw.githubusercontent.com/zhongyang219/MusicPlayer2/master/version.info", version_info))		//获取版本信息
28         {
29             m_row_data = true;
30         }
31         else if (CInternetCommon::GetURL(L"https://github.com/zhongyang219/MusicPlayer2/blob/master/version.info", version_info))		//获取版本信息
32         {
33             m_row_data = false;
34         }
35         else
36         {
37             return false;
38         }
39 
40         if (!m_row_data)
41         {
42             size_t index = version_info.find(L"<version>");
43             if (index != std::wstring::npos)
44                 version_info = version_info.substr(index);
45 
46             CString str_version_info = version_info.c_str();
47             str_version_info.Replace(L"&lt;", L"<");
48             str_version_info.Replace(L"&gt;", L">");
49 
50             version_info = str_version_info;
51         }
52     }
53     //使用Gitee更新源
54     else
55     {
56         if (!CInternetCommon::GetURL(L"https://gitee.com/zhongyang219/MusicPlayer2/raw/master/version.info", version_info))     //获取版本信息
57             return false;
58     }
59 
60     ParseUpdateInfo(version_info);
61 
62     return true;
63 }
64 
ParseUpdateInfo(wstring version_info)65 void CUpdateHelper::ParseUpdateInfo(wstring version_info)
66 {
67     CSimpleXML version_xml;
68     version_xml.LoadXMLContentDirect(version_info);
69 
70     m_version = version_xml.GetNode(L"version");
71     wstring str_source_tag = (m_update_source == UpdateSource::GitHubSource ? L"GitHub" : L"Gitee");
72     m_link64 = version_xml.GetNode(L"link_x64", str_source_tag.c_str());
73     m_link = version_xml.GetNode(L"link", str_source_tag.c_str());
74     CString contents_zh_cn = version_xml.GetNode(L"contents_zh_cn", L"update_contents").c_str();
75     CString contents_en = version_xml.GetNode(L"contents_en", L"update_contents").c_str();
76     contents_zh_cn.Replace(L"\\n", L"\r\n");
77     contents_en.Replace(L"\\n", L"\r\n");
78     m_contents_zh_cn = contents_zh_cn;
79     m_contents_en = contents_en;
80 }
81 
GetVersion() const82 const std::wstring& CUpdateHelper::GetVersion() const
83 {
84     return m_version;
85 }
86 
GetLink() const87 const std::wstring& CUpdateHelper::GetLink() const
88 {
89     return m_link;
90 }
91 
GetLink64() const92 const std::wstring& CUpdateHelper::GetLink64() const
93 {
94     return m_link64;
95 }
96 
GetContentsEn() const97 const std::wstring& CUpdateHelper::GetContentsEn() const
98 {
99     return m_contents_en;
100 }
101 
GetContentsZhCn() const102 const std::wstring& CUpdateHelper::GetContentsZhCn() const
103 {
104     return m_contents_zh_cn;
105 }
106 
IsRowData()107 bool CUpdateHelper::IsRowData()
108 {
109     return m_row_data;
110 }
111