xref: /MusicPlayer2/MusicPlayer2/LastFMDataArchive.cpp (revision 7a904577a34af07f5ae4438553a7e362aeb87006)
1 #include "stdafx.h"
2 #include "LastFMDataArchive.h"
3 #include "MusicPlayer2.h"
4 #include <time.h>
5 
Clear()6 void LastFMTrack::Clear() {
7     artist = L"";
8     track = L"";
9     timestamp = 0;
10     album = L"";
11     streamId = L"";
12     chosenByUser = true;
13     trackNumber = 0;
14     mbid = L"";
15     albumArtist = L"";
16     duration.fromInt(0);
17 }
18 
SaveDataTo(CArchive & ar)19 void LastFMTrack::SaveDataTo(CArchive& ar) {
20     ar << CString(artist.c_str());
21     ar << CString(track.c_str());
22     ar << timestamp;
23     ar << CString(album.c_str());
24     ar << CString(streamId.c_str());
25     ar << chosenByUser;
26     ar << trackNumber;
27     ar << CString(mbid.c_str());
28     ar << CString(albumArtist.c_str());
29     ar << (int32_t)duration.toInt();
30 }
31 
ReadDataFrom(CArchive & ar)32 void LastFMTrack::ReadDataFrom(CArchive& ar) {
33     CString temp;
34     ar >> temp;
35     artist = temp;
36     ar >> temp;
37     track = temp;
38     ar >> timestamp;
39     ar >> temp;
40     album = temp;
41     ar >> temp;
42     streamId = temp;
43     ar >> chosenByUser;
44     ar >> trackNumber;
45     ar >> temp;
46     mbid = temp;
47     ar >> temp;
48     albumArtist = temp;
49     int32_t d;
50     ar >> d;
51     duration.fromInt((int)d);
52 }
53 
ReadDataFrom(const SongInfo & info)54 void LastFMTrack::ReadDataFrom(const SongInfo& info) {
55     Clear();
56     if (!info.artist.empty()) {
57         artist = info.artist;
58     }
59     if (!info.title.empty()) {
60         track = info.title;
61     }
62     __time64_t tm;
63     _time64(&tm);
64     timestamp = tm;
65     if (!info.album.empty()) {
66         album = info.album;
67     }
68     trackNumber = info.track;
69     duration = info.length();
70     albumArtist = info.album_artist;
71 }
72 
operator ==(const LastFMTrack & track)73 bool LastFMTrack::operator==(const LastFMTrack& track) {
74     return artist == track.artist &&
75         this->track == track.track &&
76         album == track.album &&
77         trackNumber == track.trackNumber &&
78         albumArtist == track.albumArtist &&
79         duration == track.duration;
80 }
81 
operator ==(const SongInfo & info)82 bool LastFMTrack::operator==(const SongInfo& info) {
83     return artist == info.artist &&
84         track == info.title &&
85         album == info.album &&
86         trackNumber == info.track &&
87         duration == info.length();
88 }
89 
SaveData(wstring path)90 void LastFMDataArchive::SaveData(wstring path) {
91     CFile file;
92     BOOL bRet = file.Open(path.c_str(), CFile::modeCreate | CFile::modeWrite);
93     if (!bRet) {
94         return;
95     }
96     CArchive ar(&file, CArchive::store);
97     /// �汾��
98     ar << (uint16_t)1;
99     ar << CString(session_key.c_str());
100     ar << CString(user_name.c_str());
101     ar << current_played_time;
102     ar << is_pushed;
103     current_track.SaveDataTo(ar);
104     corrected_current_track.SaveDataTo(ar);
105     ar << (uint64_t)cached_tracks.size();
106     for (auto i = cached_tracks.begin(); i != cached_tracks.end(); i++) {
107         auto& track = *i;
108         track.SaveDataTo(ar);
109     }
110     ar.Close();
111     file.Close();
112 }
113 
LoadData(wstring path)114 void LastFMDataArchive::LoadData(wstring path) {
115     CFile file;
116     BOOL bRet = file.Open(path.c_str(), CFile::modeRead);
117     if (!bRet) {
118         return;
119     }
120     CArchive ar(&file, CArchive::load);
121     try {
122         uint16_t version;
123         ar >> version;
124         if (version > 1) {
125             return;
126         }
127         CString temp;
128         ar >> temp;
129         session_key = temp;
130         ar >> temp;
131         user_name = temp;
132         if (version > 0) {
133             ar >> current_played_time;
134         } else {
135             current_played_time = 0;
136         }
137         if (version > 0) {
138             ar >> is_pushed;
139         } else {
140             is_pushed = false;
141         }
142         current_track.ReadDataFrom(ar);
143         corrected_current_track.ReadDataFrom(ar);
144         uint64_t size;
145         ar >> size;
146         cached_tracks.clear();
147         for (uint64_t i = 0; i < size; i++) {
148             LastFMTrack track;
149             track.ReadDataFrom(ar);
150             cached_tracks.push_back(track);
151         }
152     } catch (CArchiveException* exception) {
153         wstring info = theApp.m_str_table.LoadTextFormat(L"MSG_SERIALIZE_ERROR", { path, exception->m_cause });
154         theApp.WriteLog(info);
155     }
156     ar.Close();
157     file.Close();
158 }
159