aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_playlists/pluginproc.cpp
blob: ed951d0b865d2c3e2ee6981049de85a6d814bef6 (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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include "main.h"
#include "resource.h"
#include <windows.h>
#include <commctrl.h>
#include "SendTo.h"
#include "ml_local/api_mldb.h"
#include "ml_pmp/pmp.h"
#include "replicant/nswasabi/ReferenceCounted.h"
#include "replicant/nx/win/nxstring.h"
#include "replicant/nu/AutoChar.h"
#include "nu/AutoCharFn.h"
#include "nu/menushortcuts.h"

#include <api/syscb/callbacks/syscb.h>
#include <api/syscb/callbacks/browsercb.h>

using namespace Nullsoft::Utility;
INT_PTR lastActiveID = 0;
SendToMenu treeViewSendTo;
HWND currentView = 0;
int playlists_ContextMenu(INT_PTR param1, HWND hHost, POINTS pts);

LRESULT pluginHandleIpcMessage(int msg, WPARAM param)
{
	return SendMessage(plugin.hwndLibraryParent, WM_ML_IPC, param, msg);
}

INT_PTR pluginMessageProc(int message_type, INT_PTR param1, INT_PTR param2, INT_PTR param3)
{
	switch (message_type)
	{
		case ML_MSG_NO_CONFIG:
			return TRUE;

		case ML_MSG_TREE_ONCREATEVIEW:
			if (param1 == playlistsTreeId)
			{
				return (INT_PTR)(currentView = WASABI_API_CREATEDIALOGW(IDD_VIEW_PLAYLISTS, (HWND)param2, view_playlistsDialogProc));
			}
			else // if param1 is a valid playlist
			{
				if (FindTreeItem(param1))
				{
					lastActiveID = param1;
					// TODO: what if it's an ifc_playlist provided from elsewhere instead of just a filename?
					return (INT_PTR)(currentView = WASABI_API_CREATEDIALOGPARAMW(IDD_VIEW_PLAYLIST, (HWND)param2, view_playlistDialogProc, lastActiveID));
				}
			}
			break;

		case ML_MSG_ONSENDTOBUILD:
			return playlists_BuildSendTo(param1, param2);

		case ML_MSG_ONSENDTOSELECT:
			return playlists_OnSendTo(param1, param2, param3);

		case ML_MSG_TREE_ONCLICK:
			return playlists_OnClick(param1, param2, (HWND)param3);

		case ML_MSG_NAVIGATION_CONTEXTMENU:
		{
			HNAVITEM hItem = (HNAVITEM)param1;
			HNAVITEM myItem = MLNavCtrl_FindItemById(plugin.hwndLibraryParent, playlistsTreeId);
			if (hItem == myItem)
			{
				return playlists_ContextMenu(param1, (HWND)param2, MAKEPOINTS(param3));
			}
			else
			{
				NAVITEM nvItem = {sizeof(NAVITEM),hItem,NIMF_ITEMID,};
				MLNavItem_GetInfo(plugin.hwndLibraryParent, &nvItem);
				if (FindTreeItem(nvItem.id))
				{
					HWND wnd = (HWND)param2;
					sendToIgnoreID = nvItem.id;
					HMENU menu = GetSubMenu(g_context_menus, 0),
						  sendToMenu = GetSubMenu(menu, 2);
					treeViewSendTo.AddHere(wnd, sendToMenu, ML_TYPE_FILENAMES, 1, (ML_TYPE_PLAYLIST+1)); // we're going to lie about the type for now
					// make sure that we call init on this otherwise the sendto menu will most likely fail
					treeViewSendTo.InitPopupMenu((WPARAM)sendToMenu);

					// tweaked to not fudge on the ml tree
					EnableMenuItem(menu, IDC_PLAY, MF_BYCOMMAND | MF_ENABLED);
					EnableMenuItem(menu, IDC_ENQUEUE, MF_BYCOMMAND | MF_ENABLED);
					EnableMenuItem(menu, IDC_DELETE, MF_BYCOMMAND | MF_ENABLED);
					EnableMenuItem(menu, ID_QUERYMENU_ADDNEWQUERY, MF_BYCOMMAND | MF_ENABLED);
					EnableMenuItem(menu, IDC_RENAME, MF_BYCOMMAND | MF_ENABLED);
					EnableMenuItem(menu, IDC_ENQUEUE, MF_BYCOMMAND | MF_ENABLED);
					EnableMenuItem(menu, 2, MF_BYPOSITION | MF_ENABLED);
					EnableMenuItem(menu, IDC_VIEWLIST, MF_BYCOMMAND | (nvItem.id != lastActiveID ? MF_ENABLED : MF_DISABLED));

					HMENU cloud_hmenu = (HMENU)0x666;
					size_t index = 0;
					if (playlists_CloudAvailable())
					{
						AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
						PlaylistInfo info;
						if (info.Associate(nvItem.id))
						{
							ReferenceCountedNXString uid;
							NXStringCreateWithFormatting(&uid, "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
														 (int)info.playlist_guid.Data1, (int)info.playlist_guid.Data2,
														 (int)info.playlist_guid.Data3, (int)info.playlist_guid.Data4[0],
														 (int)info.playlist_guid.Data4[1], (int)info.playlist_guid.Data4[2],
														 (int)info.playlist_guid.Data4[3], (int)info.playlist_guid.Data4[4],
														 (int)info.playlist_guid.Data4[5], (int)info.playlist_guid.Data4[6],
														 (int)info.playlist_guid.Data4[7]);

							index = info.GetIndex();

							WASABI_API_SYSCB->syscb_issueCallback(api_mldb::SYSCALLBACK, api_mldb::MLDB_FILE_GET_CLOUD_STATUS, (intptr_t)uid->string, (intptr_t)&cloud_hmenu);

							if (cloud_hmenu && cloud_hmenu != (HMENU)0x666)
							{
								MENUITEMINFOW m = {sizeof(m), MIIM_TYPE | MIIM_ID | MIIM_SUBMENU, MFT_SEPARATOR, 0};
								m.wID = CLOUD_SOURCE_MENUS - 1;
								InsertMenuItemW(menu, 3, TRUE, &m);

								wchar_t a[100] = {0};
								m.fType = MFT_STRING;
								m.dwTypeData = WASABI_API_LNGSTRINGW_BUF(IDS_CLOUD_SOURCES, a, 100);
								m.wID = CLOUD_SOURCE_MENUS;
								m.hSubMenu = cloud_hmenu;
								InsertMenuItemW(menu, 4, TRUE, &m);
							}
						}
					}

					bool swapPlayEnqueue=false;
					if (g_config->ReadInt(L"enqueuedef", 0) == 1)
					{
						SwapPlayEnqueueInMenu(menu);
						swapPlayEnqueue=true;
					}

					{
					HACCEL accel = WASABI_API_LOADACCELERATORSW(IDR_VIEW_PL_ACCELERATORS);
					int size = CopyAcceleratorTable(accel,0,0);
						AppendMenuShortcuts(menu, &accel, size, MSF_REPLACE);
					}

					if (swapPlayEnqueue)
						SwapPlayEnqueueInMenu(menu);

					POINT pt;
					POINTSTOPOINT(pt, MAKEPOINTS(param3));
					if (-1 == pt.x || -1 == pt.y)
					{
						NAVITEMGETRECT itemRect;
						itemRect.fItem = FALSE;
						itemRect.hItem = hItem;
						if (MLNavItem_GetRect(plugin.hwndLibraryParent, &itemRect))
						{
							MapWindowPoints(wnd, HWND_DESKTOP, (POINT*)&itemRect.rc, 2);
							pt.x = itemRect.rc.left + 2;
							pt.y = itemRect.rc.top + 2;
						}
					}

					int r = Menu_TrackPopup(plugin.hwndLibraryParent, menu, TPM_RETURNCMD | TPM_RIGHTBUTTON | TPM_LEFTBUTTON, pt.x, pt.y, wnd, NULL);
					switch (r)
					{
						case IDC_PLAY:
						{
							AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
							PlaylistInfo info;
							if (info.Associate(nvItem.id))
							{
								playlist_SaveGUID(info.playlist_guid);
								mediaLibrary.PlayFile(info.GetFilename());
							}
						}
						break;
						case IDC_ENQUEUE:
						{
							AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
							PlaylistInfo info;
							if (info.Associate(nvItem.id))
							{
								playlist_SaveGUID(info.playlist_guid);
								mediaLibrary.EnqueueFile(info.GetFilename());
							}
						}
						break;
						case IDC_NEWPLAYLIST:
							playlists_Add(wnd);
							break;
						case IDC_DELETE:
							DeletePlaylist(tree_to_guid_map[nvItem.id], wnd, true);
							break;
						case IDC_RENAME:
							RenamePlaylist(tree_to_guid_map[nvItem.id], wnd);
							break;
						case IDC_VIEWLIST:
							mediaLibrary.SelectTreeItem(nvItem.id);
							break;
						default:
						{
							if (treeViewSendTo.WasClicked(r))
							{
								AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
								PlaylistInfo info;
								if (info.Associate(nvItem.id))
								{
									playlist_SaveGUID(info.playlist_guid);

									info.Refresh();

									mlPlaylist sendToPlaylist;
									sendToPlaylist.filename = info.GetFilename();
									sendToPlaylist.title = info.GetName();
									sendToPlaylist.numItems = info.GetSize();
									sendToPlaylist.length = info.GetLength();

									if (treeViewSendTo.SendPlaylist(&sendToPlaylist) != 1)
									{
										// didn't like that
										// let's try this way
										wchar_t filenames[MAX_PATH + 1] = {0};
										lstrcpyn(filenames, info.GetFilename(), MAX_PATH);
										filenames[lstrlen(filenames) + 1] = 0;
										treeViewSendTo.SendFilenames(filenames);
									}
								}
							}
							else
							{
								if (r >= CLOUD_SOURCE_MENUS && r < CLOUD_SOURCE_MENUS_PL_UPPER)	// deals with cloud specific menus
								{
									// 0 = no change
									// 1 = adding to cloud
									// 2 = added locally
									// 4 = removed
									int mode = -(int)index;
									WASABI_API_SYSCB->syscb_issueCallback(api_mldb::SYSCALLBACK, api_mldb::MLDB_FILE_PROCESS_CLOUD_STATUS, (intptr_t)r, (intptr_t)&mode);
									if (mode > 0)
									{
										AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
										PlaylistInfo info;
										if (info.Associate(nvItem.id))
										{
											info.SetCloud((mode == 1 ? 1 : 0));
											AGAVE_API_PLAYLISTS->Flush();
											UpdatePlaylists();
										}
									}
								}
							}
						}
					}
					treeViewSendTo.Cleanup();
					sendToIgnoreID = 0;
					if (cloud_hmenu && cloud_hmenu != (HMENU)0x666)
					{
						DeleteMenu(menu, CLOUD_SOURCE_MENUS - 1, MF_BYCOMMAND);
						DeleteMenu(menu, CLOUD_SOURCE_MENUS, MF_BYCOMMAND);
						DestroyMenu(cloud_hmenu);
					}
					return TRUE;
				}
			}
		}
			return FALSE;

		case ML_MSG_TREE_ONKEYDOWN:
			return playlists_OnKeyDown(param1, (NMTVKEYDOWN *)param2, (HWND)param3);

		case ML_MSG_TREE_ONDRAG:
			return playlists_OnDrag(param1, (POINT *)param2, (int *)param3);

		case ML_MSG_TREE_ONDROP:
			return playlists_OnDrop(param1, (POINT *)param2, param3);

		case ML_MSG_TREE_ONDROPTARGET:
			return playlists_OnDropTarget(param1, param2, param3);

		case ML_MSG_PLAYING_FILE:
			lstrcpynW(current_playing, (wchar_t*)param1, FILENAME_SIZE);
			if (IsWindow(currentView)) PostMessage(currentView, WM_APP + 103, (WPARAM)current_playing, 0);
			return FALSE;

		case ML_MSG_WRITE_CONFIG:
			if (param1)
			{
				// only save the ml playlists if saving winamp.m3u/m3u8
				// else this will happen everytime the prefs are closed
				AGAVE_API_PLAYLISTS->Flush();
			}
			return FALSE;

		case ML_MSG_VIEW_PLAY_ENQUEUE_CHANGE:
		{
			enqueuedef = param1;
			groupBtn = param2;
			PostMessage(currentView, WM_APP + 104, param1, param2);
			return 0;
		}
	}
	return 0;
}

void myOpenURL(HWND hwnd, wchar_t *loc)
{
	if (loc)
	{
		bool override=false;
		WASABI_API_SYSCB->syscb_issueCallback(SysCallback::BROWSER, BrowserCallback::ONOPENURL, reinterpret_cast<intptr_t>(loc), reinterpret_cast<intptr_t>(&override));
		if (!override)
			ShellExecuteW(hwnd, L"open", loc, NULL, NULL, SW_SHOWNORMAL);
	}
}

int playlists_ContextMenu( INT_PTR param1, HWND hHost, POINTS pts )
{
	POINT pt;
	POINTSTOPOINT( pt, pts );
	if ( -1 == pt.x || -1 == pt.y )
	{
		HNAVITEM hItem = (HNAVITEM)param1;
		NAVITEMGETRECT itemRect;
		itemRect.fItem = FALSE;
		itemRect.hItem = hItem;
		if ( MLNavItem_GetRect( plugin.hwndLibraryParent, &itemRect ) )
		{
			MapWindowPoints( hHost, HWND_DESKTOP, (POINT *)&itemRect.rc, 2 );
			pt.x = itemRect.rc.left + 2;
			pt.y = itemRect.rc.top + 2;
		}
	}

	HMENU menu = GetSubMenu( g_context_menus, 1 );

	int r = Menu_TrackPopup( plugin.hwndLibraryParent, menu, TPM_RETURNCMD | TPM_RIGHTBUTTON | TPM_LEFTBUTTON | TPM_NONOTIFY, pt.x, pt.y, hHost, NULL );
	switch ( r )
	{
		case IDC_NEWPLAYLIST:
			playlists_Add( hHost );
			break;
		case IDC_IMPORT_PLAYLIST_FROM_FILE:
			Playlist_importFromFile( hHost );
			break;
		case IDC_IMPORT_WINAMP_PLAYLIST:
			Playlist_importFromWinamp();
			break;
		case ID_PLAYLISTSMENU_IMPORTPLAYLISTFROMFOLDERS:
			Playlist_importFromFolders( hHost );
			break;
		case ID_SORTPLAYLIST_TITLE_A_Z:
			playlists_Sort( SORT_TITLE_ASCENDING );
			break;
		case ID_SORTPLAYLIST_TITLE_Z_A:
			playlists_Sort( SORT_TITLE_DESCENDING );
			break;
		case ID_SORTPLAYLIST_NUMBEROFITEMSASCENDING:
			playlists_Sort( SORT_NUMBER_ASCENDING );
			break;
		case ID_SORTPLAYLIST_NUMBEROFITEMSDESCENDING:
			playlists_Sort( SORT_NUMBER_DESCENDING );
			break;
		case ID_PLAYLISTS_HELP:
			myOpenURL( hHost, L"https://help.winamp.com/hc/articles/8109547717268-Winamp-Playlists" );
			break;
	}

	Sleep( 100 );
	MSG msg;
	while ( PeekMessage( &msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE ) ); //eat return

	return TRUE;
}

// param1 = param of tree item, param2 = action type (below), param3 = HWND of main window
INT_PTR playlists_OnClick(INT_PTR treeId, int clickType, HWND wnd)
{
	switch (clickType)
	{
		case ML_ACTION_DBLCLICK:
		case ML_ACTION_ENTER:
		{
			if (FindTreeItem(treeId))
			{
				AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
				PlaylistInfo info;
				if (info.Associate(treeId))
				{
					playlist_SaveGUID(info.playlist_guid);
					bool flip = (clickType==ML_ACTION_ENTER) && (GetAsyncKeyState(VK_SHIFT)&0x8000);
			
					if ((!!(g_config->ReadInt(L"enqueuedef", 0) == 1)) ^ flip)
						mediaLibrary.EnqueueFile(info.GetFilename());
					else
						mediaLibrary.PlayFile(info.GetFilename());
				}
				return 1;
			}
		}
		break;
	}
	return 0;
}

int playlists_OnKeyDown(int treeId, NMTVKEYDOWN *p, HWND hwndDlg)
{
	int ctrl = (GetAsyncKeyState(VK_CONTROL)&0x8000);
	int shift = (GetAsyncKeyState(VK_SHIFT)&0x8000);

	if (treeId == playlistsTreeId)
	{
		switch (p->wVKey)
		{
			case VK_INSERT:
			{
				if (shift && !ctrl) playlists_Add(plugin.hwndLibraryParent); return 1;
			}
		}
	}
	else if (FindTreeItem(treeId))
	{
		switch (p->wVKey)
		{
			case VK_F2: if (!shift && !ctrl) RenamePlaylist(tree_to_guid_map[treeId], plugin.hwndLibraryParent);	return 1;
			case VK_INSERT: if (shift && !ctrl) playlists_Add(plugin.hwndLibraryParent); return 1;
			case VK_DELETE: if (!shift && !ctrl) DeletePlaylist(tree_to_guid_map[treeId], hwndDlg, true);  return 1;
		}
	}
	return 0;
}

int playlists_OnDrag(int treeId, POINT *pt, int *type)
{
	if (FindTreeItem(treeId))
	{
		*type = ML_TYPE_FILENAMES;
		return 1;
	}
	return 0;
}

int playlists_OnDrop(int treeId, POINT *pt, int destTreeId)
{
	if (FindTreeItem(treeId))
	{
		AutoLockT<api_playlists> lock (AGAVE_API_PLAYLISTS);
		PlaylistInfo info;
		if (info.Associate(treeId))
		{
			if (destTreeId == playlistsTreeId)
			{
				mediaLibrary.RemoveTreeItem(info.treeId);
				tree_to_guid_map.erase(info.treeId);
				AGAVE_API_PLAYLISTS->MoveBefore(info.GetIndex(), 0);

				// TODO: move most of this to PlaylistsCB
				// TODO use the more native code like in MakeTree(..)
				MLTREEITEMW src = {sizeof(MLTREEITEMW), };
				src.title = const_cast<wchar_t *>(info.GetName());
				src.hasChildren = 0;
				src.parentId = playlistsTreeId;
				src.id = destTreeId;
				src.imageIndex = (!info.GetCloud() ? imgPL : imgCloudPL);
				mediaLibrary.InsertTreeItem(src);
				info.treeId = src.id;
				tree_to_guid_map[info.treeId] = info.playlist_guid;
				mediaLibrary.SelectTreeItem(info.treeId);
			}
			else if (FindTreeItem(destTreeId))
			{
				PlaylistInfo dest;
				if (dest.Associate(destTreeId))
				{
					mediaLibrary.RemoveTreeItem(info.treeId);
					tree_to_guid_map.erase(info.treeId);
					AGAVE_API_PLAYLISTS->MoveBefore(info.GetIndex(), dest.GetIndex()+1);

					// TODO: move most of this to PlaylistsCB
					// TODO use the more native code like in MakeTree(..)
					MLTREEITEMW src = {sizeof(MLTREEITEMW), };
					src.title = const_cast<wchar_t *>(info.GetName());
					src.hasChildren = 0;
					src.parentId = playlistsTreeId;
					src.id = destTreeId;
					src.imageIndex = (!info.GetCloud() ? imgPL : imgCloudPL);
					mediaLibrary.InsertTreeItem(src);
					info.treeId = src.id;
					tree_to_guid_map[info.treeId] = info.playlist_guid;
					mediaLibrary.SelectTreeItem(info.treeId);
				}
				return 1;
			}
			else
			{
				playlist_SaveGUID(info.playlist_guid);

				info.Refresh();
				mlPlaylist pl;
				pl.filename = info.GetFilename();
				pl.length = info.GetLength();
				pl.numItems = info.GetSize();
				pl.title = info.GetName();

				mlDropItemStruct m = {0};
				m.p = *pt;
				m.flags = 0;
				m.type = ML_TYPE_PLAYLIST;
				m.result = 0;
				m.data = (void *) & pl;
				m.name = 0;
				pluginHandleIpcMessage(ML_IPC_HANDLEDROP, (WPARAM)&m);
				/* TODO: fall back to this is result fails?
				mlDropItemStruct m = {0};
				m.p = *pt;
				m.flags = ML_HANDLEDRAG_FLAG_NAME;
				m.type=ML_TYPE_FILENAMES;
				m.result = 0;
				char filename[1025] = {0};
				lstrcpynA(filename, AutoCharFn(playlists[index].filename), 1024);
				filename[lstrlenA(filename)+1]=0;
				m.data=(void *)filename;
				AutoChar charTitle(playlists[index].title);
				m.name = charTitle;
				pluginHandleIpcMessage(ML_IPC_HANDLEDROP, (WPARAM)&m);
				*/
			}
		}
	}
	return 0;
}