ml_local.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. // needed for handling the nde failed load error catching
  2. #pragma warning(disable:4530)
  3. #include "main.h"
  4. #include "ml_local.h"
  5. #include <windowsx.h>
  6. #include <time.h>
  7. #include <rpc.h>
  8. #include <assert.h>
  9. #include "resource.h"
  10. #include "..\..\General\gen_ml/config.h"
  11. #include "..\..\General\gen_ml/gaystring.h"
  12. #include "..\..\General\gen_ml/ml.h"
  13. #include "..\..\General\gen_ml/ml_ipc.h"
  14. #include "../nde/nde.h"
  15. #include "../nde/nde_c.h"
  16. #include "..\..\General\gen_hotkeys/wa_hotkeys.h"
  17. #include "..\..\General\gen_ml/MediaLibraryCOM.h"
  18. #include "api__ml_local.h"
  19. #include "../replicant/nu/AutoWide.h"
  20. #include "./scanfolderbrowser.h"
  21. #include "../replicant/nu/AutoChar.h"
  22. #include "api_mldb.h"
  23. int IPC_GET_ML_HMENU = -1, IPC_GET_CLOUD_HINST = -1, IPC_GET_CLOUD_ACTIVE = -1;
  24. HMENU wa_playlists_cmdmenu = NULL;
  25. HMENU wa_play_menu = 0;
  26. wchar_t *fieldTagFunc(wchar_t * tag, void * p); //return 0 if not found
  27. #define WA_MENUITEM_ID 23123
  28. WNDPROC wa_oldWndProc;
  29. BOOL myMenu = FALSE;
  30. HCURSOR hDragNDropCursor;
  31. C_Config *g_config;
  32. embedWindowState myWindowState;
  33. char g_burner_list[32] = "";
  34. int asked_for_playcount = 0;
  35. int g_bgrescan_int = 120, g_bgrescan_do = 0, g_bgrescan_force = 0, g_autochannel_do = 0;
  36. int g_guessifany = 0;
  37. int g_querydelay = 250;
  38. int g_viewnotplay = 0;
  39. wchar_t g_path[MAX_PATH] = {0};
  40. wchar_t g_tableDir[MAX_PATH] = {0};
  41. wchar_t g_viewsDir[MAX_PATH] = {0};
  42. //DB db;
  43. nde_database_t g_db=0;
  44. nde_table_t g_table=0;
  45. int g_table_dirty;
  46. CRITICAL_SECTION g_db_cs;
  47. HMENU g_context_menus = NULL, g_context_menus2 = NULL;
  48. HWND m_curview_hwnd = NULL;
  49. wchar_t *m_query = L"";
  50. int m_query_mode;
  51. C_Config *g_view_metaconf = NULL;
  52. static int m_query_moving;
  53. static HTREEITEM m_query_moving_dragplace;
  54. static HTREEITEM m_query_moving_item, m_query_moving_lastdest;
  55. static int m_query_moving_dragplaceisbelow;
  56. int m_query_tree;
  57. QueryList m_query_list;
  58. //xp theme disabling shit
  59. static HMODULE m_uxdll;
  60. HRESULT(__stdcall *SetWindowTheme)(HWND hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList);
  61. HRESULT(__stdcall *IsAppThemed)(void);
  62. void db_setFieldStringW( nde_scanner_t s, unsigned char id, const wchar_t *data )
  63. {
  64. nde_field_t f = NDE_Scanner_GetFieldByID( s, id );
  65. if ( !f )
  66. f = NDE_Scanner_NewFieldByID( s, id );
  67. NDE_StringField_SetString( f, data );
  68. }
  69. void db_setFieldInt( nde_scanner_t s, unsigned char id, int data )
  70. {
  71. nde_field_t f = NDE_Scanner_GetFieldByID( s, id );
  72. if ( !f )
  73. f = NDE_Scanner_NewFieldByID( s, id );
  74. NDE_IntegerField_SetValue( f, data );
  75. }
  76. int db_getFieldInt( nde_scanner_t s, unsigned char id, int defaultVal )
  77. {
  78. nde_field_t f = NDE_Scanner_GetFieldByID( s, id );
  79. if ( f )
  80. return NDE_IntegerField_GetValue( f );
  81. else
  82. return defaultVal;
  83. }
  84. void db_setFieldInt64( nde_scanner_t s, unsigned char id, __int64 data )
  85. {
  86. nde_field_t f = NDE_Scanner_GetFieldByID( s, id );
  87. if ( !f )
  88. f = NDE_Scanner_NewFieldByID( s, id );
  89. NDE_Int64Field_SetValue( f, data );
  90. }
  91. __int64 db_getFieldInt64( nde_scanner_t s, unsigned char id, __int64 defaultVal )
  92. {
  93. nde_field_t f = NDE_Scanner_GetFieldByID( s, id );
  94. if ( f )
  95. return NDE_Int64Field_GetValue( f );
  96. else
  97. return defaultVal;
  98. }
  99. void db_removeField( nde_scanner_t s, unsigned char id )
  100. {
  101. nde_field_t f = NDE_Scanner_GetFieldByID( s, id );
  102. if ( f )
  103. {
  104. NDE_Scanner_DeleteField( s, f );
  105. }
  106. }
  107. int pluginHandleIpcMessage(int msg, int param)
  108. {
  109. return SendMessage(plugin.hwndLibraryParent, WM_ML_IPC, param, msg);
  110. }
  111. void TAG_FMT_EXT(const wchar_t *filename, void *f, void *ff, void *p, wchar_t *out, int out_len, int extended)
  112. {
  113. waFormatTitleExtended fmt;
  114. fmt.filename = filename;
  115. fmt.useExtendedInfo = extended;
  116. fmt.out = out;
  117. fmt.out_len = out_len;
  118. fmt.p = p;
  119. fmt.spec = 0;
  120. *(void **)&fmt.TAGFUNC = f;
  121. *(void **)&fmt.TAGFREEFUNC = ff;
  122. *out = 0;
  123. int oldCallingGetFileInfo = m_calling_getfileinfo;
  124. m_calling_getfileinfo = 1;
  125. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&fmt, IPC_FORMAT_TITLE_EXTENDED);
  126. m_calling_getfileinfo = oldCallingGetFileInfo;
  127. }
  128. void main_playItemRecordList(itemRecordListW *obj, int enqueue, int startplaying)
  129. {
  130. assert(enqueue != -1); // benski> i'm pretty sure this isn't used anymore
  131. if (obj->Size && !enqueue)
  132. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_DELETE);
  133. int x;
  134. for (x = 0; x < obj->Size; x ++)
  135. {
  136. if (obj->Items[x].filename && *obj->Items[x].filename)
  137. {
  138. wchar_t title[2048] = {0};
  139. TAG_FMT_EXT(obj->Items[x].filename, itemrecordWTagFunc, ndeTagFuncFree, (void*)&obj->Items[x], title, 2047, 0);
  140. enqueueFileWithMetaStructW s;
  141. s.filename = obj->Items[x].filename;
  142. s.title = title;
  143. s.ext = NULL;
  144. s.length = obj->Items[x].length;
  145. #ifndef _DEBUG
  146. ndestring_retain(obj->Items[x].filename);
  147. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&s, IPC_PLAYFILEW_NDE);
  148. #else
  149. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&s, IPC_PLAYFILEW);
  150. #endif
  151. }
  152. }
  153. if (obj->Size && !enqueue && startplaying) SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_STARTPLAY);
  154. }
  155. void main_playQuery(C_Config *viewconf, const wchar_t *query, int enqueue, int startplaying)
  156. {
  157. // if enqueue is -1, we do it to the playlist
  158. if (!g_table) return ;
  159. EnterCriticalSection(&g_db_cs);
  160. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  161. NDE_Scanner_Query(s, query);
  162. itemRecordListW obj = {0, };
  163. // no need to have this provide compatible kb
  164. // based filesizes as we never use the values
  165. saveQueryToListW(viewconf, s, &obj, 0, 0, 0);
  166. NDE_Table_DestroyScanner(g_table, s);
  167. LeaveCriticalSection(&g_db_cs);
  168. main_playItemRecordList(&obj, enqueue, startplaying);
  169. freeRecordList(&obj);
  170. }
  171. time_t g_bgscan_last_rescan;
  172. int g_bgscan_scanning;
  173. HWND updateCurrentView(HWND hwndDlg)
  174. {
  175. if (m_curview_hwnd) DestroyWindow(m_curview_hwnd);
  176. m_curview_hwnd = NULL;
  177. delete g_view_metaconf;
  178. g_view_metaconf = 0;
  179. int id = -1;
  180. DLGPROC proc = 0;
  181. if (!g_table)
  182. {
  183. // try to show something better than a blank view
  184. id = IDD_VIEW_DB_ERROR;
  185. proc = view_errorinfoDialogProc;
  186. }
  187. else
  188. {
  189. switch (m_query_mode)
  190. {
  191. case 0:
  192. id = IDD_VIEW_MEDIA; proc = view_mediaDialogProc;
  193. break;
  194. default:
  195. id = IDD_VIEW_AUDIO; proc = view_audioDialogProc;
  196. break;
  197. }
  198. }
  199. if (id == -1)
  200. proc = NULL;
  201. if (id != -1)
  202. {
  203. wchar_t configDir[MAX_PATH] = {0};
  204. PathCombineW(configDir, g_viewsDir, m_query_metafile);
  205. g_view_metaconf = new C_Config(configDir);
  206. LPARAM lParam = 0;
  207. INT_PTR parms[2] = {0};
  208. if (g_config->ReadInt(L"useminiinfo2", 0) && (proc == view_audioDialogProc || proc == view_mediaDialogProc))
  209. {
  210. parms[0] = (INT_PTR)proc;
  211. parms[1] = (INT_PTR)id;
  212. lParam = (LPARAM) & parms;
  213. id = IDD_VIEW_MINIINFO;
  214. proc = view_miniinfoDialogProc;
  215. }
  216. if (proc)
  217. m_curview_hwnd = WASABI_API_CREATEDIALOGPARAMW(id, hwndDlg, proc, lParam);
  218. }
  219. return m_curview_hwnd;
  220. }
  221. void makemetafn(wchar_t *filename, wchar_t **out)
  222. {
  223. int x = 0;
  224. for (;;)
  225. {
  226. GetTempFileNameW(g_viewsDir, L"meta", GetTickCount() + x*4050, filename);
  227. if (wcslen(filename) > 4) wcscpy(filename + wcslen(filename) - 4, L".vmd");
  228. HANDLE h = CreateFileW(filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_NEW, 0, 0);
  229. if (h != INVALID_HANDLE_VALUE)
  230. {
  231. CloseHandle(h);
  232. *out = _wcsdup(filename);
  233. PathStripPathW(*out);
  234. return ;
  235. }
  236. if (++x > 4096)
  237. {
  238. *out = _wcsdup(L"meta-error.vmd");
  239. filename[0] = 0;
  240. return ;
  241. }
  242. }
  243. }
  244. int ImageGuessFilter( int mode, const wchar_t *val, int index )
  245. {
  246. if ( index != -1 )
  247. return index;
  248. if ( !lstrcmpiW( val, L"lastupd > [3 days ago]" ) )
  249. return TREE_IMAGE_LOCAL_RECENTLYMODIFIED;
  250. if ( !lstrcmpiW( val, L"dateadded > [3 days ago]" ) )
  251. return TREE_IMAGE_LOCAL_RECENTLYADDED;
  252. if ( !lstrcmpiW( val, L"playcount > 0" ) )
  253. return TREE_IMAGE_LOCAL_MOSTPLAYED;
  254. if ( !lstrcmpiW( val, L"rating >= 3" ) )
  255. return TREE_IMAGE_LOCAL_TOPRATED;
  256. if ( !lstrcmpiW( val, L"playcount = 0 | playcount isempty" ) )
  257. return TREE_IMAGE_LOCAL_NEVERPLAYED;
  258. if ( !lstrcmpiW( val, L"lastplay > [2 weeks ago]" ) )
  259. return TREE_IMAGE_LOCAL_RECENTLYPLAYED;
  260. if ( !lstrcmpiW( val, L"type = 0" ) )
  261. return TREE_IMAGE_LOCAL_AUDIO;
  262. if ( !lstrcmpiW( val, L"type = 1" ) )
  263. return TREE_IMAGE_LOCAL_VIDEO;
  264. return index;
  265. }
  266. int addQueryItem( const wchar_t *name, const wchar_t *val, int mode, int select, const wchar_t *metafn, int imageIndex, int num )
  267. {
  268. MLTREEITEMW newItem = { 0 };
  269. imageIndex = ImageGuessFilter( mode, val, imageIndex );
  270. newItem.size = sizeof( newItem );
  271. newItem.parentId = m_query_tree;
  272. newItem.title = const_cast<wchar_t *>( name );
  273. newItem.hasChildren = 0;
  274. newItem.id = 0;
  275. newItem.imageIndex = imageIndex;
  276. if ( num <= 0 )
  277. mediaLibrary.AddTreeItem( newItem );
  278. else
  279. {
  280. for ( QueryList::iterator iter = m_query_list.begin(); iter != m_query_list.end(); iter++ )
  281. {
  282. if ( iter->second && iter->second->index == num - 1 )
  283. newItem.id = iter->first; break;
  284. }
  285. mediaLibrary.InsertTreeItem( newItem );
  286. }
  287. queryItem *qi = (queryItem *)calloc( 1, sizeof( queryItem ) );
  288. qi->name = _wcsdup( name );
  289. qi->query = _wcsdup( val );
  290. qi->mode = mode;
  291. qi->imgIndex = imageIndex;
  292. qi->index = m_query_list.size();
  293. if ( !metafn || !metafn[ 0 ] )
  294. {
  295. wchar_t filename[ 1024 + 256 ] = { 0 };
  296. makemetafn( filename, &qi->metafn );
  297. }
  298. else qi->metafn = _wcsdup( metafn );
  299. m_query_list.insert({ newItem.id, qi });
  300. return newItem.id;
  301. }
  302. void replaceQueryItem(int n, const wchar_t *name, const wchar_t *val, int mode, int imageIndex)
  303. {
  304. queryItem *qi;
  305. if ( mode == 32 )
  306. return;
  307. qi = m_query_list[n];
  308. free(qi->name);
  309. qi->name = _wcsdup(name);
  310. if (val)
  311. {
  312. free(qi->query);
  313. qi->query = _wcsdup(val);
  314. }
  315. qi->mode = mode;
  316. qi->imgIndex = imageIndex;
  317. MLTREEITEMW item;
  318. item.hasChildren = 0;
  319. item.id = n;
  320. item.title = const_cast<wchar_t *>(name);
  321. item.parentId = m_query_tree;
  322. item.imageIndex = imageIndex;
  323. mediaLibrary.SetTreeItem(item);
  324. mediaLibrary.SelectTreeItem(n - 1);
  325. mediaLibrary.SelectTreeItem(n);
  326. }
  327. wchar_t *def_names[] = { L"Audio",
  328. L"Video",
  329. L"Most Played",
  330. L"Recently Added",
  331. L"Recently Played",
  332. L"Never Played",
  333. L"Top Rated",
  334. L"Recently Modified", };
  335. int def_str_ids[] = {IDS_AUDIO, IDS_VIDEO, IDS_MOST_PLAYED, IDS_RECENTLY_ADDED,
  336. IDS_RECENTLY_PLAYED, IDS_NEVER_PLAYED, IDS_TOP_RATED,
  337. IDS_RECENTLY_MODIFIED};
  338. void loadQueryTree()
  339. {
  340. int meta_add_dirty = 0;
  341. int nb = g_config->ReadInt(L"query_num", 0);
  342. int fix = g_config->ReadInt(L"query_fix", 0);
  343. int mig = g_config->ReadInt(L"query_mig", 0);
  344. g_config->WriteInt(L"query_fix", 1);
  345. g_config->WriteInt(L"query_mig", 1);
  346. // helps to migrate existing vmd files to plugins\ml\views
  347. wchar_t metafnold[MAX_PATH] = {0}, metafnnew[MAX_PATH] = {0};
  348. if (!mig)
  349. {
  350. PathCombineW(metafnold, g_tableDir, L"default.vmd");
  351. PathCombineW(metafnnew, g_viewsDir, L"default.vmd");
  352. if (!PathFileExistsW(metafnnew) && PathFileExistsW(metafnold))
  353. {
  354. MoveFileW(metafnold, metafnnew);
  355. }
  356. }
  357. for (int i = 0; i < nb; i++)
  358. {
  359. wchar_t qm[128] = {0}, qbmp[128] = {0}, qmet[128] = {0};
  360. char qn[128] = {0}, qv[128] = {0}, name[1024] = {0}, val[1024] = {0};
  361. wsprintfA(qn, "query%i_name", i + 1);
  362. UINT codePage = CP_ACP;
  363. if (!g_config->ReadString(qn, NULL, name, 1024) || !*name)
  364. {
  365. wsprintfA(qn, "query%i_name_utf8", i + 1);
  366. g_config->ReadString(qn, NULL, name, 1024);
  367. codePage = CP_UTF8;
  368. if (!name)
  369. continue;
  370. }
  371. wchar_t unicodeNameLoc[256] = {0};
  372. AutoWide unicodeName(name, codePage);
  373. wsprintfA(qv, "query%i_val", i + 1);
  374. codePage = CP_ACP;
  375. if (!g_config->ReadString(qv, NULL, val, 1024) || !*val)
  376. {
  377. wsprintfA(qv, "query%i_val_utf8", i + 1);
  378. g_config->ReadString(qv, NULL, val, 1024);
  379. codePage = CP_UTF8;
  380. }
  381. // this will convert 'lastupd > [3 days ago]' to 'dateadded > [3 days ago]'
  382. // on older client installs so we're making use of the new dateadded column
  383. if (!fix && val[0])
  384. {
  385. if (!_stricmp("lastupd > [3 days ago]", val))
  386. {
  387. lstrcpynA(val, "dateadded > [3 days ago]", sizeof(val));
  388. g_config->WriteString(qv, val);
  389. }
  390. }
  391. AutoWide unicodeVal(val, codePage);
  392. wsprintfW(qm, L"query%i_mode", i + 1);
  393. wsprintfW(qmet, L"query%i_meta", i + 1);
  394. wsprintfW(qbmp, L"query%i_image", i + 1);
  395. int mode = g_config->ReadInt(qm, -1);
  396. if (mode == 32 || mode == -1)
  397. continue; // old playlist or empty
  398. wchar_t metafn[MAX_PATH] = {0};
  399. g_config->ReadString(qmet, L"", metafn, MAX_PATH);
  400. // helps to migrate existing vmd files to plugins\ml\views
  401. if (!mig)
  402. {
  403. metafnold[0] = metafnnew[0] = 0;
  404. PathCombineW(metafnold, g_tableDir, metafn);
  405. PathCombineW(metafnnew, g_viewsDir, metafn);
  406. if (!PathFileExistsW(metafnnew) && PathFileExistsW(metafnold))
  407. {
  408. MoveFileW(metafnold, metafnnew);
  409. }
  410. }
  411. // see if we've got a name match to one of the defaults...
  412. for(int j = 0; j < sizeof(def_names)/sizeof(def_names[0]); j++)
  413. {
  414. if(!lstrcmpiW(unicodeName, def_names[j]))
  415. {
  416. WASABI_API_LNGSTRINGW_BUF(def_str_ids[j], unicodeNameLoc, 256);
  417. break;
  418. }
  419. }
  420. addQueryItem((unicodeNameLoc[0]?unicodeNameLoc:unicodeName), unicodeVal, mode, 0, metafn, max(g_config->ReadInt(qbmp, -1), -1));
  421. }
  422. int aapos = g_config->ReadInt(L"view_autoadd_pos", 0);
  423. if (aapos < 1)
  424. {
  425. if (!nb) // lame defaults added
  426. {
  427. meta_add_dirty = 1;
  428. addQueryItem( WASABI_API_LNGSTRINGW( IDS_AUDIO ), L"type = 0", 1, 0, L"", TREE_IMAGE_LOCAL_AUDIO ); // new defaults
  429. }
  430. typedef struct
  431. {
  432. int title;
  433. wchar_t *query;
  434. char sort_by;
  435. char sort_dir;
  436. char *columns; //xff terminated list :)
  437. int imageIndex;
  438. }
  439. addstruct;
  440. addstruct m[] =
  441. {
  442. {IDS_VIDEO, L"type = 1", 10, 0, "\x7\1\5\x1E\6\3\x20\x8\x9\xA\xff", TREE_IMAGE_LOCAL_VIDEO},
  443. {IDS_MOST_PLAYED, L"playcount > 0", 9, 0, "\x9\0\1\2\3\xA\xff", TREE_IMAGE_LOCAL_MOSTPLAYED},
  444. {IDS_RECENTLY_ADDED, L"dateadded > [3 days ago]", 33, 0, "\x21\0\1\2\3\xff", TREE_IMAGE_LOCAL_RECENTLYADDED},
  445. {IDS_RECENTLY_MODIFIED, L"lastupd > [3 days ago]", 11, 0, "\xB\0\1\2\3\xff", TREE_IMAGE_LOCAL_RECENTLYMODIFIED},
  446. {IDS_RECENTLY_PLAYED, L"lastplay > [2 weeks ago]", 10, 0, "\xA\x9\0\1\2\3\xff", TREE_IMAGE_LOCAL_RECENTLYPLAYED},
  447. {IDS_NEVER_PLAYED, L"playcount = 0 | playcount isempty", 0, 0, "\0\1\2\3\xff", TREE_IMAGE_LOCAL_NEVERPLAYED},
  448. {IDS_TOP_RATED, L"rating >= 3", 8, 0, "\x8\x9\0\1\2\3\xff", TREE_IMAGE_LOCAL_TOPRATED},
  449. };
  450. if (aapos < 1)
  451. {
  452. int x;
  453. for (x = 0; x < sizeof(m) / sizeof(m[0]); x++)
  454. {
  455. if (!x && nb) continue;
  456. wchar_t filename[1024 + 256] = {0}, *ptr = 0;
  457. makemetafn(filename, &ptr);
  458. if (filename[0])
  459. {
  460. C_Config foo(filename);
  461. foo.WriteInt(L"mv_sort_by", m[x].sort_by);
  462. foo.WriteInt(L"mv_sort_dir", m[x].sort_dir);
  463. int cnt = 0;
  464. while ((unsigned char)m[x].columns[cnt] != 0xff)
  465. {
  466. wchar_t buf[32] = {0};
  467. StringCchPrintfW(buf, 32, L"column%d", cnt);
  468. foo.WriteInt(buf, (unsigned char)m[x].columns[cnt]);
  469. cnt++;
  470. }
  471. foo.WriteInt(L"nbcolumns", cnt);
  472. meta_add_dirty = 1;
  473. addQueryItem(WASABI_API_LNGSTRINGW(m[x].title), m[x].query, 0, 0, ptr, m[x].imageIndex);
  474. }
  475. free(ptr);
  476. }
  477. }
  478. g_config->WriteInt(L"view_autoadd_pos", 1);
  479. }
  480. if (meta_add_dirty)
  481. saveQueryTree();
  482. }
  483. void saveQueryTree()
  484. {
  485. int nb = g_config->ReadInt( L"query_num", 0 );
  486. QueryList::iterator iter;
  487. int i = 1;
  488. wchar_t qm[ 128 ] = { 0 }, qmet[ 128 ] = { 0 }, qbmp[ 128 ] = { 0 };
  489. char qn[ 128 ] = { 0 }, qv[ 128 ] = { 0 };
  490. for ( int curId = mediaLibrary.GetChildId( m_query_tree ); curId != 0; curId = mediaLibrary.GetNextId( curId ), i++ )
  491. {
  492. iter = m_query_list.find( curId );
  493. if ( iter == m_query_list.end() )
  494. continue;
  495. if ( i <= nb )
  496. {
  497. do
  498. {
  499. wsprintfW( qm, L"query%i_mode", i );
  500. } while ( g_config->ReadInt( qm, -1 ) == 32 && i++ );
  501. }
  502. wsprintfA( qn, "query%i_name", i );
  503. wsprintfA( qv, "query%i_val", i );
  504. g_config->WriteString( qn, 0 ); // erase these old config items
  505. g_config->WriteString( qv, 0 ); // erase these old config items
  506. wsprintfA( qn, "query%i_name_utf8", i );
  507. wsprintfA( qv, "query%i_val_utf8", i );
  508. wsprintfW( qm, L"query%i_mode", i );
  509. wsprintfW( qmet, L"query%i_meta", i );
  510. wsprintfW( qbmp, L"query%i_image", i );
  511. queryItem *thisitem = iter->second;
  512. if ( thisitem == NULL ) continue;
  513. wchar_t charNameLoc[ 256 ] = { 0 };
  514. // see if we've got a name match to one of the defaults...
  515. for ( int j = 0; j < sizeof( def_names ) / sizeof( def_names[ 0 ] ); j++ )
  516. {
  517. if ( !lstrcmpiW( thisitem->name, WASABI_API_LNGSTRINGW( def_str_ids[ j ] ) ) )
  518. {
  519. lstrcpyn( charNameLoc, def_names[ j ], ARRAYSIZE( charNameLoc ) );
  520. break;
  521. }
  522. }
  523. g_config->WriteString( qn, ( charNameLoc[ 0 ] ? charNameLoc : AutoChar( thisitem->name, CP_UTF8 ) ) );
  524. g_config->WriteString( qv, AutoChar( thisitem->query, CP_UTF8 ) );
  525. g_config->WriteInt( qm, thisitem->mode );
  526. g_config->WriteString( qmet, thisitem->metafn );
  527. g_config->WriteInt( qbmp, max( thisitem->imgIndex, -1 ) );
  528. }
  529. --i;
  530. if ( i < nb )
  531. {
  532. for ( int k = i + 1; k <= nb; k++ )
  533. {
  534. wsprintfW( qm, L"query%i_mode", k );
  535. int mode = g_config->ReadInt( qm, -1 );
  536. if ( mode == 32 )
  537. i++;
  538. else
  539. {
  540. wsprintfA( qn, "query%i_name", k );
  541. wsprintfA( qv, "query%i_val", k );
  542. wsprintfW( qm, L"query%i_mode", k );
  543. wsprintfW( qmet, L"query%i_meta", k );
  544. wsprintfW( qbmp, L"query%i_image", k );
  545. g_config->WriteString( qn, NULL );
  546. g_config->WriteString( qv, NULL );
  547. g_config->WriteString( qm, NULL );
  548. g_config->WriteString( qmet, NULL );
  549. g_config->WriteString( qbmp, NULL );
  550. }
  551. }
  552. }
  553. g_config->WriteInt( L"query_num", i );
  554. }
  555. HTREEITEM g_treedrag_lastSel;
  556. HWND onTreeViewSelectChange(HWND hwnd)
  557. {
  558. if ( !g_table )
  559. openDb();
  560. bgQuery_Stop();
  561. int par = mediaLibrary.GetSelectedTreeItem();
  562. // defaults
  563. m_query_mode = par;
  564. m_query = L"";
  565. m_query_metafile = L"";
  566. m_query_mode = 0;
  567. if (par == m_query_tree) // set up default media view
  568. {
  569. m_query_metafile = L"default.vmd";
  570. }
  571. else
  572. {
  573. QueryList::iterator iter;
  574. iter = m_query_list.find(par);
  575. if (iter != m_query_list.end())
  576. {
  577. m_query = iter->second->query;
  578. m_query_mode = iter->second->mode;
  579. m_query_metafile = iter->second->metafn;
  580. }
  581. else
  582. {
  583. m_query_metafile = L"default.vmd";
  584. }
  585. }
  586. return updateCurrentView(hwnd);
  587. }
  588. void add_pledit_to_library()
  589. {
  590. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_WRITEPLAYLIST);
  591. wchar_t *m3udir = (wchar_t *) SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETM3UDIRECTORYW);
  592. wchar_t filename[MAX_PATH] = {0};
  593. PathCombineW(filename, m3udir, L"winamp.m3u8");
  594. PLCallBackW plCB;
  595. if (AGAVE_API_PLAYLISTMANAGER && PLAYLISTMANAGER_SUCCESS != AGAVE_API_PLAYLISTMANAGER->Load(filename, &plCB))
  596. {
  597. mediaLibrary.AddToMediaLibrary(filename);
  598. }
  599. }
  600. void add_to_library(HWND wndparent)
  601. {
  602. ScanFolderBrowser browser;
  603. browser.SetBckScanChecked(g_config->ReadInt(L"addinbg", 0));
  604. if (browser.Browse(wndparent))
  605. {
  606. wchar_t path[MAX_PATH] = {0};
  607. g_config->WriteInt(L"addinbg", browser.GetBckScanChecked());
  608. SHGetPathFromIDListW(browser.GetPIDL(), path);
  609. int guess = -1, meta = -1, rec = 1;
  610. autoscan_add_directory(path, &guess, &meta, &rec, 0);
  611. if (guess == -1)
  612. guess = g_config->ReadInt(L"guessmode", 0);
  613. if (meta == -1)
  614. meta = g_config->ReadInt(L"usemetadata", 1);
  615. if (g_config->ReadInt(L"addinbg", 0))
  616. {
  617. Scan_ScanFolderBackground(path, guess, meta, rec); // add our dir to scan :)
  618. }
  619. else
  620. {
  621. Scan_ScanFolder(wndparent, path, guess, meta, rec);
  622. int par = mediaLibrary.GetSelectedTreeItem();
  623. mediaLibrary.SelectTreeItem(par - 1);
  624. mediaLibrary.SelectTreeItem(par);
  625. if (IsWindow(m_curview_hwnd))
  626. SendMessage(m_curview_hwnd, WM_APP + 1, 0, 0); //update current view
  627. }
  628. }
  629. }
  630. void nukeLibrary(HWND hwndDlg)
  631. {
  632. wchar_t titleStr[32] = {0};
  633. if (MessageBoxW(hwndDlg, WASABI_API_LNGSTRINGW(IDS_REMOVE_ALL_ITEMS_IN_LIBRARY),
  634. WASABI_API_LNGSTRINGW_BUF(IDS_CONFIRMATION,titleStr,32),
  635. MB_YESNO | MB_ICONQUESTION) == IDYES)
  636. {
  637. wchar_t *last_query = NULL;
  638. EnterCriticalSection(&g_db_cs);
  639. if (m_media_scanner)
  640. {
  641. const wchar_t *lq = NDE_Scanner_GetLastQuery(m_media_scanner);
  642. if (lq) last_query = _wcsdup(lq);
  643. NDE_Table_DestroyScanner(g_table, m_media_scanner);
  644. }
  645. LeaveCriticalSection(&g_db_cs);
  646. bgQuery_Stop();
  647. Scan_Cancel();
  648. int count = 0;
  649. wchar_t **filenames = 0;
  650. nde_scanner_t clearedScanner = NDE_Table_CreateScanner(g_table);
  651. if (clearedScanner)
  652. {
  653. filenames = new wchar_t * [(count = NDE_Scanner_GetRecordsCount(clearedScanner))];
  654. int i = 0;
  655. for (NDE_Scanner_First(clearedScanner); !NDE_Scanner_EOF(clearedScanner); NDE_Scanner_Next(clearedScanner))
  656. {
  657. nde_field_t fileName = NDE_Scanner_GetFieldByID(clearedScanner, MAINTABLE_ID_FILENAME);
  658. if (fileName)
  659. {
  660. filenames[i] = NDE_StringField_GetString(fileName);
  661. ndestring_retain(filenames[i]);
  662. i++;
  663. }
  664. }
  665. }
  666. NDE_Table_DestroyScanner(g_table, clearedScanner);
  667. closeDb();
  668. wchar_t tmp[MAX_PATH] = {0};
  669. StringCchPrintfW(tmp, MAX_PATH, L"%s\\main.dat", g_tableDir);
  670. DeleteFileW(tmp);
  671. StringCchPrintfW(tmp, MAX_PATH, L"%s\\main.idx", g_tableDir);
  672. DeleteFileW(tmp);
  673. openDb();
  674. EnterCriticalSection(&g_db_cs);
  675. if (m_media_scanner)
  676. {
  677. m_media_scanner = NDE_Table_CreateScanner(g_table);
  678. if (last_query != NULL)
  679. {
  680. NDE_Scanner_Query(m_media_scanner, last_query);
  681. free(last_query);
  682. }
  683. }
  684. LeaveCriticalSection(&g_db_cs);
  685. DumpArtCache();
  686. if (IsWindow(m_curview_hwnd))
  687. SendMessage(m_curview_hwnd, WM_APP + 1, 0, 0); //update current view
  688. // Wasabi event callback when the media library is cleared
  689. WASABI_API_SYSCB->syscb_issueCallback(api_mldb::SYSCALLBACK, api_mldb::MLDB_CLEARED, (size_t)filenames, count);
  690. if (filenames) delete[] filenames;
  691. // trigger a refresh of the current view
  692. PostMessage(plugin.hwndLibraryParent, WM_USER + 30, 0, 0);
  693. }
  694. }
  695. extern int main_sendto_mode;
  696. extern HMENU main_sendto_hmenu;
  697. wchar_t *itemrecordWTagFunc(wchar_t *tag, void * p) //return 0 if not found
  698. {
  699. // TODO we can put more tags in here
  700. itemRecordW *t = (itemRecordW *)p;
  701. //bool copy=false;
  702. wchar_t buf[128] = {0};
  703. wchar_t *value = NULL;
  704. if ( !_wcsicmp( tag, DB_FIELDNAME_artist ) )
  705. value = t->artist;
  706. else if ( !_wcsicmp( tag, DB_FIELDNAME_album ) )
  707. value = t->album;
  708. else if ( !_wcsicmp( tag, DB_FIELDNAME_filename ) )
  709. value = t->filename;
  710. else if ( !_wcsicmp( tag, DB_FIELDNAME_title ) )
  711. value = t->title;
  712. else if ( !_wcsicmp( tag, DB_FIELDNAME_year ) )
  713. {
  714. if ( t->year > 0 )
  715. {
  716. wsprintfW( buf, L"%04d", t->year );
  717. value = buf;
  718. }
  719. }
  720. else if ( !_wcsicmp( tag, DB_FIELDNAME_genre ) )
  721. value = t->genre;
  722. else if ( !_wcsicmp( tag, DB_FIELDNAME_comment ) )
  723. value = t->comment;
  724. else if ( !_wcsicmp( tag, DB_FIELDNAME_tracknumber ) || !_wcsicmp( tag, DB_FIELDNAME_track ) )
  725. {
  726. if ( t->track > 0 )
  727. {
  728. if ( t->tracks > 0 )
  729. wsprintfW( buf, L"%02d/%02d", t->track, t->tracks );
  730. else
  731. wsprintfW( buf, L"%02d", t->track );
  732. value = buf;
  733. }
  734. }
  735. else if ( !_wcsicmp( tag, DB_FIELDNAME_disc ) )
  736. {
  737. if ( t->disc > 0 )
  738. {
  739. if ( t->discs > 0 )
  740. wsprintfW( buf, L"%d/%d", t->disc, t->discs );
  741. else
  742. wsprintfW( buf, L"%d", t->disc );
  743. value = buf;
  744. }
  745. }
  746. else if ( !_wcsicmp( tag, DB_FIELDNAME_rating ) )
  747. {
  748. if ( t->rating > 0 )
  749. {
  750. wsprintfW( buf, L"%d", t->rating );
  751. value = buf;
  752. }
  753. }
  754. else if ( !_wcsicmp( tag, DB_FIELDNAME_playcount ) )
  755. {
  756. if ( t->playcount > 0 )
  757. {
  758. wsprintfW( buf, L"%d", t->playcount );
  759. value = buf;
  760. }
  761. }
  762. else if ( !_wcsicmp( tag, DB_FIELDNAME_bitrate ) )
  763. {
  764. if ( t->bitrate > 0 )
  765. {
  766. wsprintfW( buf, L"%d", t->bitrate );
  767. value = buf;
  768. }
  769. }
  770. else if ( !_wcsicmp( tag, DB_FIELDNAME_bpm ) )
  771. {
  772. if ( t->bpm > 0 )
  773. {
  774. wsprintfW( buf, L"%d", t->bpm );
  775. value = buf;
  776. }
  777. }
  778. else if ( !_wcsicmp( tag, DB_FIELDNAME_albumartist ) )
  779. value = t->albumartist;
  780. else if ( !_wcsicmp( tag, DB_FIELDNAME_publisher ) )
  781. value = t->publisher;
  782. else if ( !_wcsicmp( tag, DB_FIELDNAME_composer ) )
  783. value = t->composer;
  784. else if ( !_wcsicmp( tag, DB_FIELDNAME_replaygain_album_gain ) )
  785. value = t->replaygain_album_gain;
  786. else if ( !_wcsicmp( tag, DB_FIELDNAME_replaygain_track_gain ) )
  787. value = t->replaygain_track_gain;
  788. else if ( !_wcsicmp( tag, DB_FIELDNAME_GracenoteFileID ) )
  789. value = getRecordExtendedItem_fast( t, extended_fields.GracenoteFileID );
  790. else if ( !_wcsicmp( tag, DB_FIELDNAME_GracenoteExtData ) )
  791. value = getRecordExtendedItem_fast( t, extended_fields.GracenoteExtData );
  792. else
  793. return 0;
  794. if (!value)
  795. return reinterpret_cast<wchar_t *>(-1);
  796. else
  797. {
  798. if (/*copy || */value == buf)
  799. return ndestring_wcsdup(value);
  800. else
  801. {
  802. ndestring_retain(value);
  803. return value;
  804. }
  805. }
  806. }
  807. static bool TagNameToFieldID(const wchar_t *tag, int *id)
  808. {
  809. if ( !_wcsicmp( tag, DB_FIELDNAME_artist ) )
  810. *id = MAINTABLE_ID_ARTIST;
  811. else if ( !_wcsicmp( tag, DB_FIELDNAME_album ) )
  812. *id = MAINTABLE_ID_ALBUM;
  813. else if ( !_wcsicmp( tag, DB_FIELDNAME_filename ) )
  814. *id = MAINTABLE_ID_FILENAME;
  815. else if ( !_wcsicmp( tag, DB_FIELDNAME_title ) )
  816. *id = MAINTABLE_ID_TITLE;
  817. else if ( !_wcsicmp( tag, DB_FIELDNAME_year ) )
  818. *id = MAINTABLE_ID_YEAR;
  819. else if ( !_wcsicmp( tag, DB_FIELDNAME_genre ) )
  820. *id = MAINTABLE_ID_GENRE;
  821. else if ( !_wcsicmp( tag, DB_FIELDNAME_comment ) )
  822. *id = MAINTABLE_ID_COMMENT;
  823. else if ( !_wcsicmp( tag, DB_FIELDNAME_tracknumber ) || !_wcsicmp( tag, DB_FIELDNAME_track ) )
  824. *id = MAINTABLE_ID_TRACKNB;
  825. else if ( !_wcsicmp( tag, DB_FIELDNAME_rating ) )
  826. *id = MAINTABLE_ID_RATING;
  827. else if ( !_wcsicmp( tag, DB_FIELDNAME_playcount ) )
  828. *id = MAINTABLE_ID_PLAYCOUNT;
  829. else if ( !_wcsicmp( tag, DB_FIELDNAME_bitrate ) )
  830. *id = MAINTABLE_ID_BITRATE;
  831. else if ( !_wcsicmp( tag, DB_FIELDNAME_disc ) )
  832. *id = MAINTABLE_ID_DISC;
  833. else if ( !_wcsicmp( tag, DB_FIELDNAME_bpm ) )
  834. *id = MAINTABLE_ID_BPM;
  835. else if ( !_wcsicmp( tag, DB_FIELDNAME_albumartist ) )
  836. *id = MAINTABLE_ID_ALBUMARTIST;
  837. else if ( !_wcsicmp( tag, DB_FIELDNAME_publisher ) )
  838. *id = MAINTABLE_ID_PUBLISHER;
  839. else if ( !_wcsicmp( tag, DB_FIELDNAME_composer ) )
  840. *id = MAINTABLE_ID_COMPOSER;
  841. else if ( !_wcsicmp( tag, DB_FIELDNAME_replaygain_album_gain ) )
  842. *id = MAINTABLE_ID_ALBUMGAIN;
  843. else if ( !_wcsicmp( tag, DB_FIELDNAME_replaygain_track_gain ) )
  844. *id = MAINTABLE_ID_TRACKGAIN;
  845. else if ( !_wcsicmp( tag, DB_FIELDNAME_GracenoteFileID ) )
  846. *id = MAINTABLE_ID_GRACENOTEFILEID;
  847. else if ( !_wcsicmp( tag, DB_FIELDNAME_GracenoteExtData ) )
  848. *id = MAINTABLE_ID_GRACENOTEEXTDATA;
  849. //else if (!_wcsicmp(tag, DB_FIELDNAME_lossless)) *id = MAINTABLE_ID_LOSSLESS;
  850. else
  851. return false;
  852. return true;
  853. }
  854. wchar_t *fieldTagFunc(wchar_t * tag, void * p) //return 0 if not found
  855. {
  856. nde_scanner_t s = (nde_scanner_t)p;
  857. int id = -1;
  858. if (!TagNameToFieldID(tag, &id))
  859. return 0;
  860. if (id >= 0)
  861. {
  862. nde_field_t f = NDE_Scanner_GetFieldByID(s, id);
  863. if (f)
  864. switch (id)
  865. {
  866. case MAINTABLE_ID_YEAR:
  867. {
  868. wchar_t buf[32] = {0};
  869. int l = NDE_IntegerField_GetValue(f);
  870. if (l < 0) return reinterpret_cast<wchar_t *>(-1);
  871. wsprintfW(buf, L"%04d", l);
  872. return ndestring_wcsdup(buf);
  873. }
  874. case MAINTABLE_ID_TRACKNB:
  875. {
  876. wchar_t buf[32] = {0};
  877. int l = NDE_IntegerField_GetValue(f);
  878. if (l < 0) return reinterpret_cast<wchar_t *>(-1);
  879. int tracks = db_getFieldInt(s, MAINTABLE_ID_TRACKS, -1);
  880. if (tracks > 0)
  881. wsprintfW(buf, L"%02d/%02d", l, tracks);
  882. else
  883. wsprintfW(buf, L"%02d", l);
  884. return ndestring_wcsdup(buf);
  885. }
  886. case MAINTABLE_ID_DISC:
  887. {
  888. wchar_t buf[32] = {0};
  889. int l = NDE_IntegerField_GetValue(f);
  890. if (l < 0) return reinterpret_cast<wchar_t *>(-1);
  891. int discs = db_getFieldInt(s, MAINTABLE_ID_DISCS, -1);
  892. if (discs > 0)
  893. wsprintfW(buf, L"%d/%d", l, discs);
  894. else
  895. wsprintfW(buf, L"%d", l);
  896. return ndestring_wcsdup(buf);
  897. }
  898. case MAINTABLE_ID_PLAYCOUNT:
  899. asked_for_playcount = 1;
  900. // fall through :)
  901. case MAINTABLE_ID_BPM:
  902. case MAINTABLE_ID_RATING:
  903. case MAINTABLE_ID_BITRATE:
  904. {
  905. wchar_t buf[32] = {0};
  906. int l = NDE_IntegerField_GetValue(f);
  907. if (l < 0)
  908. return reinterpret_cast<wchar_t *>(-1);
  909. wsprintfW(buf, L"%d", l);
  910. return ndestring_wcsdup(buf);
  911. }
  912. default:
  913. {
  914. wchar_t *p = NDE_StringField_GetString(f);
  915. if (!p || !*p)
  916. return reinterpret_cast<wchar_t *>(-1);;
  917. ndestring_retain(p);
  918. return p;
  919. }
  920. }
  921. }
  922. return 0;
  923. }
  924. void ndeTagFuncFree(wchar_t * tag, void * p)
  925. {
  926. ndestring_release(tag);
  927. }
  928. void RetypeFilename(nde_table_t table);
  929. void RefreshFileSizeAndDateAddedTable(nde_table_t table);
  930. void ReindexTable(nde_table_t table);
  931. static void CreateFields( nde_table_t table )
  932. {
  933. // create defaults
  934. NDE_Table_NewColumnW( table, MAINTABLE_ID_FILENAME, DB_FIELDNAME_filename, FIELD_FILENAME );
  935. NDE_Table_NewColumnW( table, MAINTABLE_ID_TITLE, DB_FIELDNAME_title, FIELD_STRING );
  936. NDE_Table_NewColumnW( table, MAINTABLE_ID_ARTIST, DB_FIELDNAME_artist, FIELD_STRING );
  937. NDE_Table_NewColumnW( table, MAINTABLE_ID_ALBUM, DB_FIELDNAME_album, FIELD_STRING );
  938. NDE_Table_NewColumnW( table, MAINTABLE_ID_YEAR, DB_FIELDNAME_year, FIELD_INTEGER );
  939. NDE_Table_NewColumnW( table, MAINTABLE_ID_GENRE, DB_FIELDNAME_genre, FIELD_STRING );
  940. NDE_Table_NewColumnW( table, MAINTABLE_ID_COMMENT, DB_FIELDNAME_comment, FIELD_STRING );
  941. NDE_Table_NewColumnW( table, MAINTABLE_ID_TRACKNB, DB_FIELDNAME_trackno, FIELD_INTEGER );
  942. NDE_Table_NewColumnW( table, MAINTABLE_ID_LENGTH, DB_FIELDNAME_length, FIELD_LENGTH );
  943. NDE_Table_NewColumnW( table, MAINTABLE_ID_TYPE, DB_FIELDNAME_type, FIELD_INTEGER );
  944. NDE_Table_NewColumnW( table, MAINTABLE_ID_LASTUPDTIME, DB_FIELDNAME_lastupd, FIELD_DATETIME );
  945. NDE_Table_NewColumnW( table, MAINTABLE_ID_LASTPLAY, DB_FIELDNAME_lastplay, FIELD_DATETIME );
  946. NDE_Table_NewColumnW( table, MAINTABLE_ID_RATING, DB_FIELDNAME_rating, FIELD_INTEGER );
  947. NDE_Table_NewColumnW( table, MAINTABLE_ID_GRACENOTE_ID, DB_FIELDNAME_tuid2, FIELD_STRING );
  948. NDE_Table_NewColumnW( table, MAINTABLE_ID_PLAYCOUNT, DB_FIELDNAME_playcount, FIELD_INTEGER );
  949. NDE_Table_NewColumnW( table, MAINTABLE_ID_FILETIME, DB_FIELDNAME_filetime, FIELD_DATETIME );
  950. NDE_Table_NewColumnW( table, MAINTABLE_ID_FILESIZE, DB_FIELDNAME_filesize, FIELD_INT64 );
  951. NDE_Table_NewColumnW( table, MAINTABLE_ID_BITRATE, DB_FIELDNAME_bitrate, FIELD_INTEGER );
  952. NDE_Table_NewColumnW( table, MAINTABLE_ID_DISC, DB_FIELDNAME_disc, FIELD_INTEGER );
  953. NDE_Table_NewColumnW( table, MAINTABLE_ID_ALBUMARTIST, DB_FIELDNAME_albumartist, FIELD_STRING );
  954. NDE_Table_NewColumnW( table, MAINTABLE_ID_ALBUMGAIN, DB_FIELDNAME_replaygain_album_gain, FIELD_STRING );
  955. NDE_Table_NewColumnW( table, MAINTABLE_ID_TRACKGAIN, DB_FIELDNAME_replaygain_track_gain, FIELD_STRING );
  956. NDE_Table_NewColumnW( table, MAINTABLE_ID_PUBLISHER, DB_FIELDNAME_publisher, FIELD_STRING );
  957. NDE_Table_NewColumnW( table, MAINTABLE_ID_COMPOSER, DB_FIELDNAME_composer, FIELD_STRING );
  958. NDE_Table_NewColumnW( table, MAINTABLE_ID_BPM, DB_FIELDNAME_bpm, FIELD_INTEGER );
  959. NDE_Table_NewColumnW( table, MAINTABLE_ID_DISCS, DB_FIELDNAME_discs, FIELD_INTEGER );
  960. NDE_Table_NewColumnW( table, MAINTABLE_ID_TRACKS, DB_FIELDNAME_tracks, FIELD_INTEGER );
  961. NDE_Table_NewColumnW( table, MAINTABLE_ID_ISPODCAST, DB_FIELDNAME_ispodcast, FIELD_INTEGER );
  962. NDE_Table_NewColumnW( table, MAINTABLE_ID_PODCASTCHANNEL, DB_FIELDNAME_podcastchannel, FIELD_STRING );
  963. NDE_Table_NewColumnW( table, MAINTABLE_ID_PODCASTPUBDATE, DB_FIELDNAME_podcastpubdate, FIELD_DATETIME );
  964. NDE_Table_NewColumnW( table, MAINTABLE_ID_GRACENOTEFILEID, DB_FIELDNAME_GracenoteFileID, FIELD_STRING );
  965. NDE_Table_NewColumnW( table, MAINTABLE_ID_GRACENOTEEXTDATA, DB_FIELDNAME_GracenoteExtData, FIELD_STRING );
  966. NDE_Table_NewColumnW( table, MAINTABLE_ID_LOSSLESS, DB_FIELDNAME_lossless, FIELD_INTEGER );
  967. NDE_Table_NewColumnW( table, MAINTABLE_ID_CATEGORY, DB_FIELDNAME_category, FIELD_STRING );
  968. NDE_Table_NewColumnW( table, MAINTABLE_ID_CODEC, DB_FIELDNAME_codec, FIELD_STRING );
  969. NDE_Table_NewColumnW( table, MAINTABLE_ID_DIRECTOR, DB_FIELDNAME_director, FIELD_STRING );
  970. NDE_Table_NewColumnW( table, MAINTABLE_ID_PRODUCER, DB_FIELDNAME_producer, FIELD_STRING );
  971. NDE_Table_NewColumnW( table, MAINTABLE_ID_WIDTH, DB_FIELDNAME_width, FIELD_INTEGER );
  972. NDE_Table_NewColumnW( table, MAINTABLE_ID_HEIGHT, DB_FIELDNAME_height, FIELD_INTEGER );
  973. NDE_Table_NewColumnW( table, MAINTABLE_ID_MIMETYPE, DB_FIELDNAME_mimetype, FIELD_STRING );
  974. NDE_Table_NewColumnW( table, MAINTABLE_ID_DATEADDED, DB_FIELDNAME_dateadded, FIELD_DATETIME );
  975. NDE_Table_PostColumns(table);
  976. NDE_Table_AddIndexByIDW(table, MAINTABLE_ID_FILENAME, DB_FIELDNAME_filename );
  977. }
  978. int openDb()
  979. {
  980. // TODO: fix!! this is a Double-Checked Lock Pattern and can have strange results
  981. // in weird conditions because g_table is assigned before fully initialized
  982. if ( g_table )
  983. return 0;
  984. EnterCriticalSection(&g_db_cs);
  985. // benski> i know this looks redundant, but we might have sat and blocked at the above Critical Section for a while
  986. if (g_table)
  987. {
  988. LeaveCriticalSection(&g_db_cs);
  989. return 0;
  990. }
  991. if ( !g_db )
  992. {
  993. __try
  994. {
  995. g_db = NDE_CreateDatabase( plugin.hDllInstance );
  996. }
  997. __except ( EXCEPTION_EXECUTE_HANDLER )
  998. {
  999. g_db = NULL;
  1000. LeaveCriticalSection( &g_db_cs );
  1001. return 0;
  1002. }
  1003. }
  1004. const wchar_t *inidir = WASABI_API_APP->path_getUserSettingsPath();
  1005. wchar_t tableName[MAX_PATH] = {0}, indexName[MAX_PATH] = {0};
  1006. PathCombineW(indexName, inidir, L"Plugins\\ml");
  1007. PathCombineW(tableName, indexName, L"main.dat");
  1008. PathAppendW(indexName, L"main.idx");
  1009. g_table = NDE_Database_OpenTable(g_db, tableName, indexName, NDE_OPEN_ALWAYS, NDE_CACHE);
  1010. if (g_table)
  1011. {
  1012. ((Table *)g_table)->EnableRowCache(); // TODO: don't use c++ NDE API
  1013. CreateFields(g_table);
  1014. RetypeFilename(g_table);
  1015. #define REINDEX_KEY L"reindex_561"
  1016. if ( !g_config->ReadInt( REINDEX_KEY, 0 ) ) // do we need to reindex?
  1017. ReindexTable( g_table );
  1018. g_config->WriteInt(REINDEX_KEY, 1);
  1019. #undef REINDEX_KEY
  1020. #define REINDEX_KEY L"reindex_564"
  1021. if ( g_config->ReadInt( REINDEX_KEY, 0 ) != 2 ) // do we need to update the filesizes and date added?
  1022. RefreshFileSizeAndDateAddedTable( g_table );
  1023. g_config->WriteInt(REINDEX_KEY, 2);
  1024. PostMessage(plugin.hwndWinampParent, WM_WA_IPC, NDE_Table_GetRecordsCount(g_table), IPC_STATS_LIBRARY_ITEMCNT);
  1025. }
  1026. LeaveCriticalSection(&g_db_cs);
  1027. return (g_table != 0);
  1028. }
  1029. // TODO make sure we're only ever saving if there was an actual change!!
  1030. void closeDb()
  1031. {
  1032. if ( g_db )
  1033. {
  1034. __try
  1035. {
  1036. if ( g_table )
  1037. {
  1038. if ( g_table_dirty )
  1039. NDE_Table_Sync( g_table );
  1040. NDE_Database_CloseTable( g_db, g_table );
  1041. }
  1042. NDE_DestroyDatabase( g_db );
  1043. }
  1044. __except ( EXCEPTION_EXECUTE_HANDLER )
  1045. {
  1046. }
  1047. }
  1048. g_db = NULL;
  1049. g_table = NULL;
  1050. }
  1051. LPCWSTR WINAMP_INI = NULL;
  1052. WNDPROC ml_oldWndProc = NULL;
  1053. LARGE_INTEGER freq;
  1054. int init()
  1055. {
  1056. QueryPerformanceFrequency(&freq);
  1057. g_table = NULL;
  1058. g_db = NULL;
  1059. g_bgscan_last_rescan = time( NULL );
  1060. LPCWSTR dir = (LPCWSTR )SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETINIDIRECTORYW);
  1061. if ( (INT_PTR)( dir ) < 65536 )
  1062. return 1;
  1063. PathCombineW(g_path, dir, L"Plugins");
  1064. CreateDirectoryW(g_path, NULL);
  1065. WINAMP_INI = (LPCWSTR)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETINIFILEW);
  1066. wchar_t configName[MAX_PATH] = {0};
  1067. PathCombineW(configName, g_path, L"gen_ml.ini");
  1068. g_config = new C_Config(configName);
  1069. g_bgrescan_int = g_config->ReadInt(L"bgrescan_int", g_bgrescan_int);
  1070. g_bgrescan_do = g_config->ReadInt(L"bgrescan_do", g_bgrescan_do);
  1071. g_bgrescan_force = g_config->ReadInt(L"bgrescan_startup", 0); // temporarily used
  1072. g_guessifany = g_config->ReadInt(L"guessifany", g_guessifany);
  1073. g_viewnotplay = g_config->ReadInt(L"viewnotplay", g_viewnotplay);
  1074. // this allows an override of the delay from making a change in the search box
  1075. // in the views to when the search will be run - sometimes needs tweaking for
  1076. // either older machines or some of the less powerful 'portable' type machines
  1077. g_querydelay = g_config->ReadInt(L"querydelay", g_querydelay);
  1078. if ( g_querydelay < 1 || g_querydelay > 5000 )
  1079. g_querydelay = 250;
  1080. PathCombineW(g_tableDir, g_path, L"ml");
  1081. PathCombineW(g_viewsDir, g_tableDir, L"views");
  1082. if (!g_config->ReadInt(L"artdbmig", 0))
  1083. {
  1084. MigrateArtCache();
  1085. g_config->WriteInt(L"artdbmig", 1);
  1086. }
  1087. wa_oldWndProc = (WNDPROC) SetWindowLongPtrW(plugin.hwndWinampParent, GWLP_WNDPROC, (LONG_PTR)wa_newWndProc);
  1088. if ( g_bgrescan_force || g_config->ReadInt( L"dbloadatstart", 1 ) )
  1089. openDb();
  1090. HMENU wa_plcontext_menu = GetSubMenu((HMENU)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)-1, IPC_GET_HMENU), 2);
  1091. if ( wa_plcontext_menu )
  1092. wa_playlists_cmdmenu = GetSubMenu( wa_plcontext_menu, 4 );
  1093. wa_play_menu = GetSubMenu((HMENU)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)0, IPC_GET_HMENU), 2);
  1094. // lets extend menu that called on button press
  1095. IPC_GET_ML_HMENU = (int)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&"LibraryGetHmenu", IPC_REGISTER_WINAMP_IPCMESSAGE);
  1096. g_context_menus = WASABI_API_LOADMENU(IDR_CONTEXTMENUS);
  1097. g_context_menus2 = WASABI_API_LOADMENU(IDR_CONTEXTMENUS);
  1098. HMENU rate_hmenu = GetSubMenu(GetSubMenu(g_context_menus,1),4);
  1099. ConvertRatingMenuStar(rate_hmenu, ID_RATE_5);
  1100. ConvertRatingMenuStar(rate_hmenu, ID_RATE_4);
  1101. ConvertRatingMenuStar(rate_hmenu, ID_RATE_3);
  1102. ConvertRatingMenuStar(rate_hmenu, ID_RATE_2);
  1103. ConvertRatingMenuStar(rate_hmenu, ID_RATE_1);
  1104. HMENU context_menu = (HMENU) SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GET_ML_HMENU);
  1105. if (context_menu)
  1106. {
  1107. HMENU btnMenu = GetSubMenu(context_menu, 0);
  1108. if (btnMenu)
  1109. {
  1110. MENUITEMINFOW mii = {sizeof(MENUITEMINFOW)};
  1111. mii.fMask = MIIM_FTYPE;
  1112. mii.fType = MFT_SEPARATOR;
  1113. mii.fState = MFS_ENABLED;
  1114. InsertMenuItemW(btnMenu, 0, TRUE, &mii);
  1115. mii.fMask = MIIM_TYPE | MIIM_ID;
  1116. mii.fType = MFT_STRING;
  1117. mii.dwTypeData = WASABI_API_LNGSTRINGW(IDS_NEW_SMART_VIEW);
  1118. mii.cch = (unsigned int) wcslen(mii.dwTypeData);
  1119. mii.wID = IDM_DOSHITMENU_ADDNEWVIEW;
  1120. InsertMenuItemW(btnMenu, 1, TRUE, &mii);
  1121. mii.dwTypeData = WASABI_API_LNGSTRINGW(IDS_RESCAN_WATCH_FOLDERS);
  1122. mii.cch = (unsigned int) wcslen(mii.dwTypeData);
  1123. mii.wID = IDM_RESCANFOLDERSNOW;
  1124. InsertMenuItemW(btnMenu, 0, TRUE, &mii);
  1125. mii.dwTypeData = WASABI_API_LNGSTRINGW(IDS_ADD_PLEDIT_TO_LOCAL_MEDIA);
  1126. mii.cch = (unsigned int) wcslen(mii.dwTypeData);
  1127. mii.wID = IDM_ADD_PLEDIT;
  1128. InsertMenuItemW(btnMenu, 0, TRUE, &mii);
  1129. mii.dwTypeData = WASABI_API_LNGSTRINGW(IDS_ADD_MEDIA_TO_LIBRARY);
  1130. mii.cch = (unsigned int) wcslen(mii.dwTypeData);
  1131. mii.wID = IDM_ADD_DIRS;
  1132. InsertMenuItemW(btnMenu, 0, TRUE, &mii);
  1133. mii.dwTypeData = WASABI_API_LNGSTRINGW(IDS_REMOVE_MISSING_FILES_FROM_ML);
  1134. mii.cch = (unsigned int) wcslen(mii.dwTypeData);
  1135. mii.wID = IDM_REMOVE_UNUSED_FILES;
  1136. InsertMenuItemW(btnMenu, 0, TRUE, &mii);
  1137. }
  1138. }
  1139. IPC_GET_CLOUD_HINST = (INT)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&"WinampCloud", IPC_REGISTER_WINAMP_IPCMESSAGE);
  1140. IPC_GET_CLOUD_ACTIVE = (INT)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&"WinampCloudActive", IPC_REGISTER_WINAMP_IPCMESSAGE);
  1141. ml_oldWndProc = (WNDPROC) SetWindowLongPtrW(plugin.hwndLibraryParent, GWLP_WNDPROC, (LONG_PTR)ml_newWndProc);
  1142. HookPlaylistEditor();
  1143. hDragNDropCursor = LoadCursor(GetModuleHandle(L"gen_ml.dll"), MAKEINTRESOURCE(ML_IDC_DRAGDROP));
  1144. // rescan timer
  1145. SetTimer( plugin.hwndLibraryParent, 200, 1000, NULL );
  1146. return 0;
  1147. }
  1148. int OnLocalMediaItemClick( int action, int item, HWND parent )
  1149. {
  1150. switch ( action )
  1151. {
  1152. case ML_ACTION_ENTER:
  1153. case ML_ACTION_DBLCLICK:
  1154. {
  1155. queryItem *qitem = m_query_list[ item ];
  1156. if ( qitem != NULL )
  1157. {
  1158. wchar_t configDir[ MAX_PATH ] = { 0 };
  1159. PathCombineW( configDir, g_viewsDir, qitem->metafn );
  1160. C_Config viewconf( configDir );
  1161. main_playQuery( &viewconf, qitem->query, ( ( !!( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) ) ^ ( !!g_config->ReadInt( L"enqueuedef", 0 ) ) ) );
  1162. }
  1163. }
  1164. return 1;
  1165. }
  1166. return 0;
  1167. }
  1168. int OnLocalMediaClick(int action, HWND parent)
  1169. {
  1170. switch (action)
  1171. {
  1172. case ML_ACTION_ENTER:
  1173. case ML_ACTION_DBLCLICK:
  1174. return 1;
  1175. }
  1176. return 0;
  1177. }