aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_playlists/PlaylistsCB.cpp
blob: 8a1a3b01e029a3f00a9a32d9ee2afd5361fdda13 (plain) (blame)
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
#include "main.h"
#include "PlaylistsCB.h"
using namespace Nullsoft::Utility;

static UINT_PTR refreshTimer;
void CALLBACK RefreshPlaylistsCallback(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	if (idEvent == 777)
	{
		KillTimer(plugin.hwndWinampParent, idEvent);
		refreshTimer = 0;
		RefreshPlaylistsList();
	}
}

int PlaylistsCB::notify(int msg, intptr_t param1, intptr_t param2)
{
	switch (msg)
	{
		case api_playlists::PLAYLIST_ADDED:
		{
			AutoLockT<api_playlists> lock(AGAVE_API_PLAYLISTS);  // should already be locked, but can't hurt
			size_t newIndex = static_cast<size_t>(param1);
			PlaylistInfo playlist(newIndex);
			if (playlist.Valid())
			{
				// TODO: check where newIndex is, so we can insert into the middle of the list if necessary
				/* TODO: maybe stuff this in Set/GetInfo somewhere
				if (makeTree)
				*/
				MakeTree(playlist);
				if (refreshTimer) KillTimer(plugin.hwndWinampParent, refreshTimer);
				refreshTimer = SetTimer(plugin.hwndWinampParent, 777, 250, RefreshPlaylistsCallback);
				if (!param2) AGAVE_API_PLAYLISTS->Flush(); // REVIEW: save immediately? or only at the end?
			}
			return 1;
		}
		break;
		case api_playlists::PLAYLIST_REMOVED_PRE:
		{
			AutoLockT<api_playlists> lock(AGAVE_API_PLAYLISTS);  // should already be locked, but can't hurt
			size_t index = static_cast<size_t>(param1),
				   count = AGAVE_API_PLAYLISTS->GetCount();
			PlaylistInfo info(index);
			if (info.Valid())
			{
				// is a number of cases where this just seems to fail and so we revert to manually
				// parsing the playlists tree inorder to find and remove the expected playlist by
				// the index of the playlist (since we don't alter the order, this should be safe)
				if (!info.treeId)
				{
					INT_PTR id = mediaLibrary.GetChildId(playlistsTreeId);
					if (id)
					{
						for (size_t i = 0; i < count; i++)
						{
							if (i == index)
							{
								mediaLibrary.RemoveTreeItem(id);
								break;
							}

							if (!(id = mediaLibrary.GetNextId(id))) break;
						}
					}
				}
				else
					mediaLibrary.RemoveTreeItem(info.treeId);

				tree_to_guid_map.erase(info.treeId);
			}
			return 1;
		}
		break;
		case api_playlists::PLAYLIST_REMOVED_POST:
		{
			if (refreshTimer) KillTimer(plugin.hwndWinampParent, refreshTimer);
			refreshTimer = SetTimer(plugin.hwndWinampParent, 777, 250, RefreshPlaylistsCallback);
			return 1;
		}
		break;
		case api_playlists::PLAYLIST_RENAMED:
		{
			AutoLockT<api_playlists> lock(AGAVE_API_PLAYLISTS);  // should already be locked, but can't hurt
			// tell the media library to rename the treeview node
			size_t index = static_cast<size_t>(param1);
			PlaylistInfo playlist(index);
			if (playlist.Valid())
			{
				mediaLibrary.RenameTreeId(playlist.treeId, playlist.GetName());
			}
			return 1;
		}
		break;
		case api_playlists::PLAYLIST_SAVED:
			if (param2 != (intptr_t)&uniqueAddress)
			{
				AutoLockT<api_playlists> lock(AGAVE_API_PLAYLISTS);  // should already be locked, but can't hurt		
				size_t index = static_cast<size_t>(param1);
				playlist_ReloadGUID(AGAVE_API_PLAYLISTS->GetGUID(index));		
			}
			break;
		case api_playlists::PLAYLIST_FLUSH_REQUEST:
			if (param2 != (intptr_t)&uniqueAddress)
			{
				AutoLockT<api_playlists> lock(AGAVE_API_PLAYLISTS);  // should already be locked, but can't hurt
				size_t index = static_cast<size_t>(param1);
				playlist_SaveGUID(AGAVE_API_PLAYLISTS->GetGUID(index));
			}
			break;
	}
	return 0;
}

#define CBCLASS PlaylistsCB
START_DISPATCH;
CB(SYSCALLBACK_GETEVENTTYPE, getEventType);
CB(SYSCALLBACK_NOTIFY, notify);
END_DISPATCH;
#undef CBCLASS