This repository was archived by the owner on Feb 9, 2021. It is now read-only.
forked from hypfvieh/foo_subsonic
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathui.cpp
More file actions
executable file
·464 lines (361 loc) · 15.5 KB
/
ui.cpp
File metadata and controls
executable file
·464 lines (361 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "foo_subsonic.h"
#include "ui.h"
#include "subsoniclibraryscanner.h"
#include "albumQueryThread.h"
#include "playlistQueryThread.h"
#include "artistUpdateThread.h"
#include "sqliteCacheDb.h"
#include "playlistupdater.h"
#include "search.h"
#include "artist.h"
using namespace foo_subsonic;
CSubsonicUi::CSubsonicUi(ui_element_config::ptr config, ui_element_instance_callback_ptr p_callback) : m_callback(p_callback), m_config(config) {
AtlInitCommonControls(ICC_TREEVIEW_CLASSES);
}
LRESULT CSubsonicUi::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/) {
LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
CTreeViewCtrlEx::SetDlgCtrlID(IDC_TREEVIEWCTRL);
CTreeViewCtrlEx::SetTextColor(m_callback->query_std_color(ui_color_text));
CTreeViewCtrlEx::SetBkColor(m_callback->query_std_color(ui_color_background));
// show +/- and the connecting lines
CTreeViewCtrlEx::ModifyStyle(1, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
//CTreeViewCtrlEx::SetExtendedStyle()
if (Preferences::load_album_cache_on_startup) {
// Read cached album catalog
SendMessage(m_hWnd, ID_CONTEXT_UPDATECATALOG_DONE, HIWORD(0), LOWORD(0));
}
if (Preferences::load_playlist_cache_on_startup) {
// Read cached playlist
SendMessage(m_hWnd, ID_CONTEXT_UPDATEPLAYLIST_DONE, HIWORD(0), LOWORD(0));
}
return lRet;
}
LRESULT CSubsonicUi::OnReloadCache(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
SqliteCacheDb::getInstance()->reloadCache();
// Read cached album catalog
SendMessage(m_hWnd, ID_CONTEXT_UPDATECATALOG_DONE, HIWORD(0), LOWORD(0));
// Read cached playlist
SendMessage(m_hWnd, ID_CONTEXT_UPDATEPLAYLIST_DONE, HIWORD(0), LOWORD(0));
return 0;
}
LRESULT CSubsonicUi::OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &)
{
LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
return lRet;
}
LRESULT CSubsonicUi::OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&) {
LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
HMENU hMenu = ::CreatePopupMenu();
if (NULL != hMenu) {
HTREEITEM selected = GetSelectedItem();
if ((selected != NULL)) {
DWORD_PTR ptr = GetItemData(selected);
if (ptr != NULL) {
CoreEntity* coreType = reinterpret_cast<CoreEntity*>(ptr);
if (coreType->get_type() == ENTRY_TYPE_ARTIST) { // Artist
::AppendMenu(hMenu, MF_STRING, ID_CONTEXT_UPDATEARTIST, _T("Update selected Artist"));
::AppendMenu(hMenu, MF_SEPARATOR, ID_CONTEXT_NOTHING, _T(""));
}
}
}
::AppendMenu(hMenu, MF_STRING, ID_CONTEXT_SEARCHDIALOG, _T("Search"));
::AppendMenu(hMenu, MF_SEPARATOR, ID_CONTEXT_NOTHING, _T(""));
::AppendMenu(hMenu, MF_STRING, ID_CONTEXT_UPDATECATALOG, _T("Retrieve/Update Subsonic Catalog"));
::AppendMenu(hMenu, MF_STRING, ID_CONTEXT_UPDATEPLAYLIST, _T("Retrieve/Update Subsonic Playlists"));
::AppendMenu(hMenu, MF_SEPARATOR, ID_CONTEXT_NOTHING, _T(""));
::AppendMenu(hMenu, MF_STRING, ID_CONTEXT_RELOADCACHE, _T("Reload Cache"));
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
CPoint p = CPoint(xPos, yPos);
ClientToScreen(&p);
int sel = ::TrackPopupMenuEx(hMenu,
TPM_CENTERALIGN | TPM_RIGHTBUTTON,
p.x,
p.y,
m_hWnd,
NULL);
::DestroyMenu(hMenu);
}
return lRet;
}
LRESULT CSubsonicUi::OnLButtonDblClick(UINT, WPARAM, LPARAM, BOOL&) {
HTREEITEM selected = GetSelectedItem();
if ((selected != NULL)) {
DWORD_PTR ptr = GetItemData(selected);
if (ptr == NULL) {
return 0;
}
CoreEntity* coreType = reinterpret_cast<CoreEntity*>(ptr);
if (coreType->get_type() == ENTRY_TYPE_TRACK) { // Track
if (ptr != NULL) {
Track* track = reinterpret_cast<Track*>(ptr);
uDebugLog() << "Got Track=" << track->get_title() << ", Artist=" << track->get_artist();
const char* url = track->get_streamUrl().c_str();
console::printf("Adding URL: %s", track->get_streamUrl());
static_api_ptr_t<playlist_incoming_item_filter_v2>()->process_locations_async(
pfc::list_single_ref_t<const char*>(url),
playlist_incoming_item_filter_v2::op_flag_background,
NULL,
NULL,
m_hWnd,
p_notify
);
}
}
else { // Album or Playlist
if (ptr != NULL) {
std::list<Track*>* trackList;
if (coreType->get_type() == ENTRY_TYPE_ALBUM) {
Album* album = reinterpret_cast<Album*>(ptr);
console::formatter() << "Got Album=" << album->get_title() << ", Artist=" << album->get_artist();
trackList = album->getTracks();
}
else if (coreType->get_type() == ENTRY_TYPE_PLAYLIST) {
Playlist* playlist = reinterpret_cast<Playlist*>(ptr);
console::formatter() << "Got Playlist=" << playlist->get_name() << ", Entries=" << playlist->getTracks()->size();
trackList = playlist->getTracks();
}
else {
return 0;
}
if (trackList->size() > 0) {
std::list<Track*>::iterator trackIterator;
pfc::list_t<const char*> data;
for (trackIterator = trackList->begin(); trackIterator != trackList->end(); trackIterator++) {
data.add_item((*trackIterator)->get_streamUrl());
}
static_api_ptr_t<playlist_incoming_item_filter_v2>()->process_locations_async(
data,
playlist_incoming_item_filter_v2::op_flag_background,
NULL,
NULL,
m_hWnd,
p_notify
);
}
}
}
}
return 0;
}
LRESULT CSubsonicUi::OnContextCatalogUpdate(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
const int result = MessageBox(L"Are you sure you want to update your local subsonic album list?", L"Update album list", MB_YESNO | MB_ICONQUESTION);
if (result == IDYES) {
threaded_process::g_run_modeless(new service_impl_t<foo_subsonic::AlbumQueryThread>(&scanner, m_hWnd),
threaded_process::flag_show_progress_dual | threaded_process::flag_show_item | threaded_process::flag_show_abort, m_hWnd, "Querying album catalog from Subsonic Server");
}
return 0;
}
LRESULT CSubsonicUi::OnContextPlaylistUpdate(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
const int result = MessageBox(L"Are you sure you want to update your local subsonic playlists?", L"Update playlists", MB_YESNO | MB_ICONQUESTION);
if (result == IDYES) {
threaded_process::g_run_modeless(new service_impl_t<foo_subsonic::PlaylistQueryThread>(&scanner, m_hWnd),
threaded_process::flag_show_progress_dual | threaded_process::flag_show_item | threaded_process::flag_show_abort, m_hWnd, "Querying playlist data from Subsonic Server");
}
return 0;
}
LRESULT CSubsonicUi::OnContextArtistUpdate(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
const int result = MessageBox(L"Are you sure you want to update the selected artist and all albums/tracks?", L"Update artist", MB_YESNO | MB_ICONQUESTION);
if (result == IDYES) {
HTREEITEM selected = GetSelectedItem();
if ((selected != NULL)) {
DWORD_PTR ptr = GetItemData(selected);
if (ptr == NULL) {
return 0;
}
CoreEntity* coreType = reinterpret_cast<CoreEntity*>(ptr);
if (coreType->get_type() == ENTRY_TYPE_ARTIST) { // Artist
if (!coreType->get_id().is_empty()) {
threaded_process::g_run_modeless(new service_impl_t<foo_subsonic::ArtistUpdateThread>(&scanner, m_hWnd, coreType->get_id()),
threaded_process::flag_show_progress_dual | threaded_process::flag_show_item | threaded_process::flag_show_abort, m_hWnd, "Querying artist data from Subsonic Server");
}
}
}
}
return 0;
}
LRESULT CSubsonicUi::OnSearchDialogShow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
new SearchDialog(m_callback->query_std_color(ui_color_text), m_callback->query_std_color(ui_color_background));
return 0;
}
HTREEITEM CSubsonicUi::getRootTreeNodeForArtist(wchar_t bgnLetter) {
std::wstring alpha = _T("#ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for (unsigned int i = 0; i < alpha.length(); i++) {
wchar_t c = alpha[i];
if (c == bgnLetter) {
return catalogRootNodes[i];
}
}
return catalogRootNodes[0]; // should match for everything which is not beginning with a letter
}
void CSubsonicUi::addTracksToTreeNode(std::list<Track*>* trackList, HTREEITEM albumNode, bool withTrackNumber, bool withArtistName) {
std::list<Track*>::iterator trackIterator;
for (trackIterator = trackList->begin(); trackIterator != trackList->end(); trackIterator++) {
pfc::stringcvt::string_wide_from_utf8 trackName((*trackIterator)->get_title());
pfc::stringcvt::string_wide_from_utf8 artistName((*trackIterator)->get_artist());
pfc::stringcvt::string_wide_from_utf8 trackNumber((*trackIterator)->get_tracknumber());
wchar_t track[250];
if (withTrackNumber && withArtistName) {
swprintf(track, sizeof(track), L"%s) %s - %s", trackNumber.get_ptr(), artistName.get_ptr(), trackName.get_ptr());
}
else if (withTrackNumber) {
swprintf(track, sizeof(track), L"%s) %s", trackNumber.get_ptr(), trackName.get_ptr());
}
else if (withArtistName) {
swprintf(track, sizeof(track), L"%s - %s", artistName.get_ptr(), trackName.get_ptr());
}
else {
swprintf(track, sizeof(track), L"%s", trackName.get_ptr());
}
Track* store = *trackIterator;
HTREEITEM titleNode = InsertItem(track, albumNode, TVI_LAST);
SetItemData(titleNode, (DWORD_PTR)store); // attach track meta data to node, so we can use this as shortcut for adding tracks to playlist
}
}
void CSubsonicUi::populateTreeWithAlbums(std::list<Album>* albumList) {
std::list<Album>::iterator it;
if (rootNodes[TREE_ROOT_CATALOG] != NULL) {
CTreeViewCtrlEx::DeleteItem(rootNodes[TREE_ROOT_CATALOG]); // remove old catalog node and all childs
}
rootNodes[TREE_ROOT_CATALOG] = CTreeViewCtrlEx::InsertItem(L"Remote Catalog", NULL, TVI_ROOT); // create new catalog node
std::wstring alpha = _T("#ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for (unsigned int i = 0; i < alpha.length(); i++) {
wchar_t name[2] = { alpha[i] ,'\0' };
catalogRootNodes[i] = CTreeViewCtrlEx::InsertItem(name, rootNodes[TREE_ROOT_CATALOG], TVI_LAST);
}
for (it = albumList->begin(); it != albumList->end(); it++) {
pfc::stringcvt::string_wide_from_utf8 artistName(it->get_artist());
pfc::stringcvt::string_wide_from_utf8 albumName(it->get_title());
HTREEITEM rootNode = getRootTreeNodeForArtist(artistName[0]);
bool needNewNode = true;
HTREEITEM artistNode = nullptr;
if (ItemHasChildren(rootNode)) { // there are already some artists
for (HTREEITEM j = GetNextItem(rootNode, TVGN_CHILD); j; j = GetNextItem(j, TVGN_NEXT)) { // iterate all artists for grouping same names
CString str;
if (GetItemText(j, str)) {
if (str == artistName) {
needNewNode = false;
artistNode = j;
break;
}
}
}
if (artistNode != nullptr) {
HTREEITEM albumNode = InsertItem(albumName, artistNode, TVI_LAST);
std::list<Track*>* trackList = it->getTracks();
addTracksToTreeNode(trackList, albumNode, true, false);
Album* store = &*it;
SetItemData(albumNode, (DWORD_PTR)store); // attach album details
}
}
if (needNewNode) {
HTREEITEM artistRoot = InsertItem(artistName, rootNode, TVI_LAST); // add artist as new entry
HTREEITEM albumNode = InsertItem(albumName, artistRoot, TVI_LAST); // add the current album as entry to artist
Artist *artist = new Artist(it->get_artistid());
SetItemData(artistRoot, (DWORD_PTR)artist); // attach artist details
std::list<Track*>* trackList = it->getTracks();
addTracksToTreeNode(trackList, albumNode, true, false);
Album* store = &*it;
SetItemData(albumNode, (DWORD_PTR)store); // attach album details
}
}
CTreeViewCtrlEx::Expand(rootNodes[TREE_ROOT_CATALOG]);
}
void CSubsonicUi::populateTreeWithPlaylists(std::list<Playlist>* playlists) {
std::list<Playlist>::iterator it;
if (rootNodes[TREE_ROOT_PLAYLISTS] != NULL) {
CTreeViewCtrlEx::DeleteItem(rootNodes[TREE_ROOT_PLAYLISTS]); // remove old playlists node and all childs
}
rootNodes[TREE_ROOT_PLAYLISTS] = CTreeViewCtrlEx::InsertItem(L"Remote Playlists", NULL, TVI_ROOT); // create playlist node
for (it = playlists->begin(); it != playlists->end(); it++) {
char playtime[20];
int hours = it->get_duration() / 3600;
int remaining = it->get_duration() % 3600;
int minutes = remaining / 60;
int seconds = remaining % 60;
snprintf(playtime, 20, "%02d:%02d:%02d", hours, minutes, seconds);
pfc::string8 tmp = it->get_name();
tmp << " <Len: " << playtime << ">";
pfc::stringcvt::string_wide_from_utf8 playlistName(tmp);
HTREEITEM rootNode = rootNodes[TREE_ROOT_PLAYLISTS];
HTREEITEM playlistNode = InsertItem(playlistName, rootNode, TVI_LAST); // add the current playlist as entry to artist
Playlist* store = &*it;
SetItemData(playlistNode, (DWORD_PTR)store); // attach album details
std::list<Track*>* tracks = it->getTracks();
addTracksToTreeNode(tracks, playlistNode, false, true);
}
}
LRESULT CSubsonicUi::OnContextCatalogUpdateDone(UINT, WPARAM, LPARAM, BOOL&) {
populateTreeWithAlbums(SqliteCacheDb::getInstance()->getAllAlbums());
return 0;
}
LRESULT CSubsonicUi::OnContextPlaylistUpdateDone(UINT, WPARAM, LPARAM, BOOL &) {
populateTreeWithPlaylists(SqliteCacheDb::getInstance()->getAllPlaylists());
return 0;
}
LRESULT CSubsonicUi::OnContextArtistUpdateDone(UINT, WPARAM, LPARAM, BOOL &) {
populateTreeWithAlbums(SqliteCacheDb::getInstance()->getAllAlbums());
return 0;
}
LRESULT CSubsonicUi::OnDrag(int idCtrl, LPNMHDR pnmh, BOOL& bHandled) {
HTREEITEM selected = GetSelectedItem();
if (selected) {
// CIDropSource* pdsrc = new CIDropSource;
// CIDataObject* pdobj = new CIDataObject(pdsrc);
// Init the supported format
FORMATETC fmtetc = { 0 };
fmtetc.cfFormat = CF_TEXT;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
// Init the medium used
STGMEDIUM medium = { 0 };
medium.tymed = TYMED_HGLOBAL;
// medium.hGlobal = init to something
// Add it to DataObject
// pdobj->SetData(&fmtetc, &medium, TRUE); // Release the medium for me
// add more formats and medium if needed
// Initiate the Drag & Drop
// ::DoDragDrop(pdobj, pdsrc, DROPEFFECT_COPY, &dwEffect);
}
return 0;
}
LRESULT CSubsonicUi::OnBeginDrag(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/) {
if (uMsg == TVN_BEGINDRAG) {
HTREEITEM selected = GetSelectedItem();
if (selected) {
m_dragging = true;
POINT pt = { 0 };
m_dragImage = &CTreeViewCtrlEx::CreateDragImage(selected);
m_dragImage->BeginDrag(0, 0, 0);
::ClientToScreen(m_hWnd, &pt);
m_dragImage->DragEnter(NULL, pt);
SetCapture();
return 0;
}
}
return 0;
}
void CSubsonicUi::OnMouseMove(UINT nFlags, CPoint point) {
if (m_dragging) {
POINT pt = point;
ClientToScreen(&pt);
CImageList::DragMove(pt);
}
}
void CSubsonicUi::OnLButtonUp(UINT nFlags, CPoint point) {
if (m_dragging) {
m_dragging = false;
CImageList::DragLeave(*this);
CImageList::EndDrag();
ReleaseCapture();
// move the dragged item
}
}
void CSubsonicUi::notify(const GUID & p_what, t_size p_param1, const void * p_param2, t_size p_param2size) {
if (p_what == ui_element_notify_colors_changed || p_what == ui_element_notify_font_changed) {
CTreeViewCtrlEx::SetTextColor(m_callback->query_std_color(ui_color_text));
CTreeViewCtrlEx::SetBkColor(m_callback->query_std_color(ui_color_background));
Invalidate();
}
}