summaryrefslogtreecommitdiff
path: root/plugins/AssocMgr/src/assoclist.cpp
blob: 1a153b144edaae600ca6db3fb534c8634c185162 (plain)
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/*

'File Association Manager'-Plugin for Miranda IM

Copyright (C) 2005-2007 H. Herkenrath

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License,  or (at your option) any later version.

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program (AssocMgr-License.txt); if not,  write to the Free Software
Foundation,  Inc.,  59 Temple Place - Suite 330,  Boston,  MA  02111-1307,  USA.
*/

#include "stdafx.h"

// Options
extern HINSTANCE hInst;
static HANDLE hHookOptInit;

/************************* Assoc List *****************************/

typedef struct
{
	char *pszClassName;    // class name as used in registry and db
	wchar_t *pszDescription;
	HINSTANCE hInstance;   // allowed to be NULL for miranda32.exe
	WORD nIconResID;
	char *pszService;
	WORD flags;            // set of FTDF_* and UTDF_* flags
	char *pszFileExt;      // file type: NULL for url type
	char *pszMimeType;     // file type: allowed to be NULL
	wchar_t *pszVerbDesc;    // file type: allowed to be NULL
} ASSOCDATA;

static ASSOCDATA *pAssocList; // protected by csAssocList
static int nAssocListCount;   // protected by csAssocList
static mir_cs csAssocList;

/************************* Assoc Enabled **************************/

static BOOL IsAssocEnabled(const ASSOCDATA *assoc)
{
	char szSetting[MAXMODULELABELLENGTH];
	mir_snprintf(szSetting, "enabled_%s", assoc->pszClassName);
	return db_get_b(NULL, "AssocMgr", szSetting, (BYTE)!(assoc->flags&FTDF_DEFAULTDISABLED)) != 0;
}

static void SetAssocEnabled(const ASSOCDATA *assoc, BOOL fEnabled)
{
	char szSetting[MAXMODULELABELLENGTH];
	wchar_t szDLL[MAX_PATH], szBuf[MAX_PATH];
	mir_snprintf(szSetting, "enabled_%s", assoc->pszClassName);
	db_set_b(NULL, "AssocMgr", szSetting, (BYTE)fEnabled);
	// dll name for uninstall
	if (assoc->hInstance != nullptr && assoc->hInstance != hInst && assoc->hInstance != GetModuleHandle(nullptr))
		if (GetModuleFileName(assoc->hInstance, szBuf, _countof(szBuf)))
			if (PathToRelativeW(szBuf, szDLL)) {
				mir_snprintf(szSetting, "module_%s", assoc->pszClassName);
				db_set_ws(NULL, "AssocMgr", szSetting, szDLL);
			}
}

static void DeleteAssocEnabledSetting(const ASSOCDATA *assoc)
{
	char szSetting[MAXMODULELABELLENGTH];
	mir_snprintf(szSetting, "enabled_%s", assoc->pszClassName);
	db_unset(NULL, "AssocMgr", szSetting);
	// dll name for uninstall
	mir_snprintf(szSetting, "module_%s", assoc->pszClassName);
	db_unset(NULL, "AssocMgr", szSetting);
}

void CleanupAssocEnabledSettings(void)
{
	int nSettingsCount;
	char **ppszSettings, *pszSuffix;
	DBVARIANT dbv;
	int i;
	HANDLE hFile;
	wchar_t szDLL[MAX_PATH];
	char szSetting[MAXMODULELABELLENGTH];

	// delete old enabled_* settings if associated plugin no longer present
	if (EnumDbPrefixSettings("AssocMgr", "enabled_", &ppszSettings, &nSettingsCount)) {
		mir_cslock lck(csAssocList);
		for (i = 0; i < nSettingsCount; ++i) {
			pszSuffix = &ppszSettings[i][8];
			mir_snprintf(szSetting, "module_%s", pszSuffix);
			if (!db_get_ws(NULL, "AssocMgr", szSetting, &dbv)) {
				if (PathToAbsoluteW(dbv.ptszVal, szDLL)) {
					// file still exists?
					hFile = CreateFile(szDLL, 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
					if (hFile == INVALID_HANDLE_VALUE) {
						db_unset(NULL, "AssocMgr", ppszSettings[i]);
						db_unset(NULL, "AssocMgr", szSetting);
					}
					else CloseHandle(hFile);
				}
				mir_free(dbv.ptszVal);
			}
			mir_free(ppszSettings[i]);
		}
		mir_free(ppszSettings); // does NULL check
	}
}

/************************* Mime Reg *******************************/

static __inline void RememberMimeTypeAdded(const char *pszMimeType, const char *pszFileExt, BYTE fAdded)
{
	char szSetting[MAXMODULELABELLENGTH];
	mir_snprintf(szSetting, "mime_%s", pszMimeType);
	if (fAdded) db_set_s(NULL, "AssocMgr", szSetting, pszFileExt);
	else db_unset(NULL, "AssocMgr", szSetting);
}

static __inline BOOL WasMimeTypeAdded(const char *pszMimeType)
{
	char szSetting[MAXMODULELABELLENGTH];
	DBVARIANT dbv;
	BOOL fAdded = FALSE;
	mir_snprintf(szSetting, "mime_%s", pszMimeType);
	if (!db_get(NULL, "AssocMgr", szSetting, &dbv)) fAdded = TRUE;
	else db_free(&dbv);
	return fAdded;
}

void CleanupMimeTypeAddedSettings(void)
{
	int nSettingsCount;
	char **ppszSettings, *pszSuffix;
	DBVARIANT dbv;
	int i, j;

	// delete old mime_* settings and unregister the associated mime type
	if (EnumDbPrefixSettings("AssocMgr", "mime_", &ppszSettings, &nSettingsCount)) {
		mir_cslock lck(csAssocList);
		for (i = 0; i < nSettingsCount; ++i) {
			pszSuffix = &ppszSettings[i][5];
			for (j = 0; j < nAssocListCount; ++j)
				if (!mir_strcmp(pszSuffix, pAssocList[j].pszMimeType))
					break; // mime type in current list
			if (j == nAssocListCount) { // mime type not in current list
				if (!db_get(NULL, "AssocMgr", ppszSettings[i], &dbv)) {
					if (dbv.type == DBVT_ASCIIZ)
						RemoveRegMimeType(pszSuffix, dbv.pszVal);
					db_free(&dbv);
				}
				db_unset(NULL, "AssocMgr", ppszSettings[i]);
			}
			mir_free(ppszSettings[i]);
		}
		mir_free(ppszSettings);
	}
}

/************************* Shell Notify ***************************/

#define SHELLNOTIFY_DELAY  3000  // time for which assoc changes are buffered

static UINT nNotifyTimerID; // protected by csNotifyTimer
static mir_cs csNotifyTimer;

static void CALLBACK NotifyTimerProc(HWND hwnd, UINT, UINT_PTR nTimerID, DWORD)
{
	mir_cslock lck(csNotifyTimer);
	KillTimer(hwnd, nTimerID);
	if (nNotifyTimerID == nTimerID) // might be stopped previously
		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, nullptr, nullptr);
	nNotifyTimerID = 0;
}

static void NotifyAssocChange(BOOL fNow)
{
	mir_cslock lck(csNotifyTimer);
	if (fNow) {
		nNotifyTimerID = 0; // stop previous timer
		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSH, nullptr, nullptr);
	}
	else nNotifyTimerID = SetTimer(nullptr, nNotifyTimerID, SHELLNOTIFY_DELAY, NotifyTimerProc);
}

/************************* Assoc List Utils ***********************/

// this function assumes it has got the csAssocList mutex
static int FindAssocItem(const char *pszClassName)
{
	int i;
	for (i = 0; i < nAssocListCount; ++i)
		if (!mir_strcmp(pszClassName, pAssocList[i].pszClassName))
			return i;
	return -1;
}

BOOL IsRegisteredAssocItem(const char *pszClassName)
{
	int index;
	mir_cslock lck(csAssocList);
	index = FindAssocItem(pszClassName);
	return index != -1;
}


// this function assumes it has got the csAssocList mutex
static ASSOCDATA* CopyAssocItem(const ASSOCDATA *assoc)
{
	ASSOCDATA *assoc2;
	assoc2 = (ASSOCDATA*)mir_alloc(sizeof(ASSOCDATA));
	if (assoc2 == nullptr) return nullptr;
	assoc2->pszClassName = mir_strdup(assoc->pszClassName);
	assoc2->pszDescription = mir_wstrdup(assoc->pszDescription);
	assoc2->hInstance = assoc->hInstance;
	assoc2->nIconResID = assoc->nIconResID;
	assoc2->pszService = mir_strdup(assoc->pszService);
	assoc2->flags = assoc->flags;
	assoc2->pszFileExt = mir_strdup(assoc->pszFileExt);
	assoc2->pszMimeType = mir_strdup(assoc->pszMimeType);
	assoc2->pszVerbDesc = mir_wstrdup(assoc->pszVerbDesc);
	if (assoc2->pszClassName == nullptr || assoc2->pszDescription == nullptr ||
		(assoc2->pszFileExt == nullptr && assoc->pszFileExt != nullptr)) {
		mir_free(assoc2->pszClassName);   // does NULL check
		mir_free(assoc2->pszDescription); // does NULL check
		mir_free(assoc2->pszService);     // does NULL check
		mir_free(assoc2->pszFileExt);     // does NULL check
		mir_free(assoc2->pszMimeType);    // does NULL check
		mir_free(assoc2->pszVerbDesc);    // does NULL check
		mir_free(assoc2);
		return nullptr;
	}
	return assoc2;
}

// this function assumes it has got the csAssocList mutex
// this function assumes CoInitialize() has been called before
static int ReplaceImageListAssocIcon(HIMAGELIST himl, const ASSOCDATA *assoc, int iPrevIndex)
{
	HICON hIcon = nullptr;
	int index;
	if (himl == nullptr) return -1;

	// load icon
	hIcon = LoadRegClassSmallIcon(assoc->pszClassName);
	if (hIcon == nullptr) {
		SHFILEINFOA sfi;
		if (SHGetFileInfoA((assoc->pszFileExt != nullptr) ? assoc->pszFileExt : "", FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES))
			hIcon = sfi.hIcon; // WinXP: this icon is not updated until the process exits
	}
	// add icon
	if (hIcon == nullptr) return -1;
	index = ImageList_ReplaceIcon(himl, iPrevIndex, hIcon);
	DestroyIcon(hIcon);
	return index;
}

// the return value does not need to be freed
// this function assumes it has got the csAssocList mutex
static wchar_t* GetAssocTypeDesc(const ASSOCDATA *assoc)
{
	static wchar_t szDesc[32];
	if (assoc->pszFileExt == nullptr)
		mir_snwprintf(szDesc, L"%hs:", assoc->pszClassName);
	else
		mir_snwprintf(szDesc, TranslateT("%hs files"), assoc->pszFileExt);
	return szDesc;
}

// this function assumes it has got the csAssocList mutex
static BOOL IsAssocRegistered(const ASSOCDATA *assoc)
{
	BOOL fSuccess = FALSE, fIsUrl, fUseMainCmdLine;

	fIsUrl = (assoc->pszFileExt == nullptr);
	fUseMainCmdLine = (assoc->pszService == nullptr);

	// class
	wchar_t *pszRunCmd = MakeRunCommand(fUseMainCmdLine, !fUseMainCmdLine);
	if (pszRunCmd != nullptr)
		fSuccess = IsRegClass(assoc->pszClassName, pszRunCmd);
	mir_free(pszRunCmd); // does NULL check
	// file ext
	if (!fIsUrl)
		fSuccess = IsRegFileExt(assoc->pszFileExt, assoc->pszClassName);

	return fSuccess;
}

// this function assumes it has got the csAssocList mutex
// call GetLastError() on error to get more error details
static BOOL EnsureAssocRegistered(const ASSOCDATA *assoc)
{
	BOOL fSuccess = FALSE, fIsUrl, fUseMainCmdLine;
	wchar_t *pszIconLoc, *pszRunCmd, *pszDdeCmd, *pszAppFileName;

	fIsUrl = (assoc->pszFileExt == nullptr);
	fUseMainCmdLine = (assoc->pszService == nullptr);

	pszRunCmd = MakeRunCommand(fUseMainCmdLine, !fUseMainCmdLine);
	if (pszRunCmd != nullptr) {
		fSuccess = TRUE; // tentatively
		// do not overwrite user customized settings
		if (!IsRegClass(assoc->pszClassName, pszRunCmd)) {
			// class icon
			if (!assoc->nIconResID && fIsUrl) pszIconLoc = MakeIconLocation(nullptr, 0); // miranda logo
			else if (!assoc->nIconResID) pszIconLoc = MakeIconLocation(hInst, IDI_MIRANDAFILE); // generic file
			else pszIconLoc = MakeIconLocation(assoc->hInstance, assoc->nIconResID);
			// register class
			if (fUseMainCmdLine) pszDdeCmd = nullptr;
			else pszDdeCmd = fIsUrl ? DDEURLCMD : DDEFILECMD;
			fSuccess = AddRegClass(assoc->pszClassName, assoc->pszDescription, pszIconLoc, _A2W(MIRANDANAME), pszRunCmd, pszDdeCmd, DDEAPP, DDETOPIC, assoc->pszVerbDesc, assoc->flags&FTDF_BROWSERAUTOOPEN, fIsUrl, assoc->flags&FTDF_ISSHORTCUT);
			mir_free(pszIconLoc); // does NULL check
			// file type
			if (fSuccess && !fIsUrl) {
				// register mime type
				if (assoc->pszMimeType != nullptr)
					if (AddRegMimeType(assoc->pszMimeType, assoc->pszFileExt))
						RememberMimeTypeAdded(assoc->pszMimeType, assoc->pszFileExt, TRUE);
				// register file ext
				fSuccess = AddRegFileExt(assoc->pszFileExt, assoc->pszClassName, assoc->pszMimeType, assoc->flags&FTDF_ISTEXT);
				// register open-with
				pszAppFileName = MakeAppFileName(fUseMainCmdLine);
				if (pszAppFileName != nullptr)
					AddRegOpenWithExtEntry(pszAppFileName, assoc->pszFileExt, assoc->pszDescription);
				mir_free(pszAppFileName); // does NULL check
			}
		}
		mir_free(pszRunCmd);
	}
	else SetLastError(ERROR_OUTOFMEMORY);
	return fSuccess;
}

// this function assumes it has got the csAssocList mutex
// call GetLastError() on error to get more error details
static BOOL UnregisterAssoc(const ASSOCDATA *assoc)
{
	BOOL fIsUrl, fUseMainCmdLine;
	wchar_t *pszAppFileName;

	fIsUrl = (assoc->pszFileExt == nullptr);
	fUseMainCmdLine = (assoc->pszService == nullptr);

	// class might have been registered by another instance
	wchar_t *pszRunCmd = MakeRunCommand(fUseMainCmdLine, !fUseMainCmdLine);
	if (pszRunCmd != nullptr && !IsRegClass(assoc->pszClassName, pszRunCmd)) {
		mir_free(pszRunCmd);
		return TRUE; // succeed anyway
	}
	mir_free(pszRunCmd); // does NULL check

	// file type
	if (!fIsUrl) {
		// file extension
		RemoveRegFileExt(assoc->pszFileExt, assoc->pszClassName);
		// mime type
		if (assoc->pszMimeType != nullptr)
			if (WasMimeTypeAdded(assoc->pszMimeType)) {
				RemoveRegMimeType(assoc->pszMimeType, assoc->pszFileExt);
				RememberMimeTypeAdded(assoc->pszMimeType, assoc->pszFileExt, FALSE);
			}
		// open-with entry
		pszAppFileName = MakeAppFileName(fUseMainCmdLine);
		if (pszAppFileName != nullptr)
			RemoveRegOpenWithExtEntry(pszAppFileName, assoc->pszFileExt);
		mir_free(pszAppFileName); // does NULL check
	}
	return RemoveRegClass(assoc->pszClassName);
}

/************************* Assoc List Workers *********************/
// this structure represents the head of both
// * FILETYPEDESC and URLTYPEDESC structures.
// * the head is identical for both structures.

typedef struct
{
	int cbSize;  // either sizeof(FILETYPEDESC) or sizeof(URLTYPEDESC)
	const void *pszDescription;
	HINSTANCE hInstance;
	UINT nIconResID;
	const char *pszService;
	DWORD flags;
} TYPEDESCHEAD;

// ownership of pszClassName,  pszFileExt,  pszVerbDesc and pszMimeType is transfered
// to the storage list on success
static BOOL AddNewAssocItem_Worker(char *pszClassName, const TYPEDESCHEAD *tdh, char *pszFileExt, wchar_t *pszVerbDesc, char *pszMimeType)
{
	ASSOCDATA *pAssocListBuf, *assoc;

	// is already in list?
	mir_cslock lck(csAssocList);
	int index = FindAssocItem(pszClassName);
	if (index != -1) return FALSE;

	// resize storage array
	pAssocListBuf = (ASSOCDATA*)mir_realloc(pAssocList, (nAssocListCount + 1)*sizeof(ASSOCDATA));
	if (pAssocListBuf == nullptr)
		return FALSE;
	pAssocList = pAssocListBuf;

	// init new item
	assoc = &pAssocList[nAssocListCount];
	assoc->pszClassName = pszClassName; // no dup here
	assoc->pszDescription = s2t(tdh->pszDescription, tdh->flags&FTDF_UNICODE, TRUE); // does NULL check
	assoc->hInstance = tdh->hInstance; // hInstance is allowed to be NULL for miranda32.exe
	assoc->nIconResID = (WORD)tdh->nIconResID; // default icon selected later on
	assoc->pszService = mir_strdup(tdh->pszService); // does NULL check
	assoc->flags = (WORD)tdh->flags;
	assoc->pszFileExt = pszFileExt;
	assoc->pszMimeType = pszMimeType;
	assoc->pszVerbDesc = pszVerbDesc;

	// error check
	if (assoc->pszDescription == nullptr || (assoc->pszService == nullptr && tdh->pszService != nullptr)) {
		mir_free(assoc->pszService);     // does NULL check
		mir_free(assoc->pszDescription); // does NULL check
		return FALSE;
	}

	// add registry keys 
	if (IsAssocEnabled(assoc))
		EnsureAssocRegistered(assoc);

	++nAssocListCount;
	NotifyAssocChange(FALSE);
	return TRUE;
}

// ownership of pszClassName is *not* transferd to storage list
static BOOL RemoveAssocItem_Worker(const char *pszClassName)
{
	ASSOCDATA *pAssocListBuf, *assoc;

	// find index
	mir_cslock lck(csAssocList);
	int index = FindAssocItem(pszClassName);
	if (index == -1)
		return FALSE;
	assoc = &pAssocList[index];

	// delete registry keys and db setting
	UnregisterAssoc(assoc);
	if (assoc->pszMimeType != nullptr)
		RememberMimeTypeAdded(assoc->pszMimeType, assoc->pszFileExt, FALSE);
	DeleteAssocEnabledSetting(assoc);

	// free memory
	mir_free(assoc->pszClassName);
	mir_free(assoc->pszDescription);
	mir_free(assoc->pszService);
	mir_free(assoc->pszFileExt);  // does NULL check
	mir_free(assoc->pszVerbDesc); // does NULL check
	mir_free(assoc->pszMimeType); // does NULL check

	// resize storage array
	if ((index + 1) < nAssocListCount)
		memmove(assoc, &pAssocList[index + 1], ((nAssocListCount - index - 1)*sizeof(ASSOCDATA)));
	pAssocListBuf = (ASSOCDATA*)mir_realloc(pAssocList, (nAssocListCount - 1)*sizeof(ASSOCDATA));
	if (pAssocListBuf != nullptr) pAssocList = pAssocListBuf;
	--nAssocListCount;

	NotifyAssocChange(FALSE);
	return TRUE;
}

/************************* Services *******************************/

static INT_PTR ServiceAddNewFileType(WPARAM, LPARAM lParam)
{
	const FILETYPEDESC *ftd = (FILETYPEDESC*)lParam;
	if (ftd->cbSize < sizeof(FILETYPEDESC))
		return 1;
	if (ftd->pszFileExt == nullptr || ftd->pszFileExt[0] != '.')
		return 2;

	char *pszFileExt = mir_strdup(ftd->pszFileExt);
	char *pszClassName = MakeFileClassName(ftd->pszFileExt);
	if (pszFileExt != nullptr && pszClassName != nullptr) {
		wchar_t *pszVerbDesc = s2t(ftd->ptszVerbDesc, ftd->flags&FTDF_UNICODE, TRUE); // does NULL check
		char *pszMimeType = mir_strdup(ftd->pszMimeType); // does NULL check
		if (AddNewAssocItem_Worker(pszClassName, (TYPEDESCHEAD*)ftd, pszFileExt, pszVerbDesc, pszMimeType))
			// no need to free pszClassName,  pszFileExt, pszVerbDesc and pszMimeType, 
			// as their ownership got transfered to storage list
			return 0;
	}
	mir_free(pszClassName); // does NULL check
	mir_free(pszFileExt); // does NULL check
	return 3;
}

static INT_PTR ServiceRemoveFileType(WPARAM, LPARAM lParam)
{
	if ((char*)lParam == nullptr) return 2;
	char *pszClassName = MakeFileClassName((char*)lParam);
	if (pszClassName != nullptr)
		if (RemoveAssocItem_Worker(pszClassName)) {
			mir_free(pszClassName);
			return 0;
		}
	mir_free(pszClassName); // does NULL check
	return 3;
}

static INT_PTR ServiceAddNewUrlType(WPARAM, LPARAM lParam)
{
	const URLTYPEDESC *utd = (URLTYPEDESC*)lParam;

	if (utd->cbSize < sizeof(URLTYPEDESC))
		return 1;
	if (utd->pszService == nullptr)
		return 2;
	if (utd->pszProtoPrefix == nullptr || utd->pszProtoPrefix[mir_strlen(utd->pszProtoPrefix) - 1] != ':')
		return 2;

	char *pszClassName = MakeUrlClassName(utd->pszProtoPrefix);
	if (pszClassName != nullptr)
		if (AddNewAssocItem_Worker(pszClassName, (TYPEDESCHEAD*)utd, nullptr, nullptr, nullptr))
			// no need to free pszClassName, as its ownership got transferred to storage list
			return 0;
	mir_free(pszClassName); // does NULL check
	return 3;
}

static INT_PTR ServiceRemoveUrlType(WPARAM, LPARAM lParam)
{
	if ((char*)lParam == nullptr) return 2;
	char *pszClassName = MakeUrlClassName((char*)lParam);
	if (pszClassName != nullptr)
		if (RemoveAssocItem_Worker(pszClassName)) {
			mir_free(pszClassName);
			return 0;
		}
	mir_free(pszClassName); // does NULL check
	return 3;
}

/************************* Open Handler ***************************/

static BOOL InvokeHandler_Worker(const char *pszClassName, const wchar_t *pszParam, INT_PTR *res)
{
	void *pvParam;
	char *pszService;

	// find it in list
	mir_cslock lck(csAssocList);
	int index = FindAssocItem(pszClassName);
	if (index == -1)
		return FALSE;
	ASSOCDATA *assoc = &pAssocList[index];
	// no service specified? correct registry to use main commandline
	if (assoc->pszService == nullptr) {
		EnsureAssocRegistered(assoc);
		NotifyAssocChange(FALSE);
		// try main command line
		if ((INT_PTR)ShellExecute(nullptr, nullptr, pszParam, nullptr, nullptr, SW_SHOWNORMAL) >= 32)
			*res = 0; // success
		return TRUE;
	}
	// get params
	pszService = mir_strdup(assoc->pszService);
	pvParam = t2s(pszParam, assoc->flags&FTDF_UNICODE, FALSE);

	// call service
	if (pszService != nullptr && pvParam != nullptr)
		*res = CallService(pszService, 0, (LPARAM)pvParam);
	mir_free(pszService); // does NULL check
	mir_free(pvParam); // does NULL check
	return TRUE;
}

INT_PTR InvokeFileHandler(const wchar_t *pszFileName)
{
	char *pszClassName, *pszFileExt;
	INT_PTR res = CALLSERVICE_NOTFOUND;

	// find extension
	wchar_t *p = (wchar_t*)wcsrchr(pszFileName, '.');
	if (p != nullptr) {
		pszFileExt = t2a(p);
		if (pszFileExt != nullptr) {
			// class name
			pszClassName = MakeFileClassName(pszFileExt);
			if (pszClassName != nullptr)
				if (!InvokeHandler_Worker(pszClassName, pszFileName, &res)) {
					// correct registry on error (no longer in list)
					RemoveRegFileExt(pszFileExt, pszClassName);
					RemoveRegClass(pszClassName);
				}
			mir_free(pszClassName); // does NULL check
			mir_free(pszFileExt);
		}
	}
	return res;
}

INT_PTR InvokeUrlHandler(const wchar_t *pszUrl)
{
	char *pszClassName, *pszProtoPrefix, *p;
	INT_PTR res = CALLSERVICE_NOTFOUND;

	// find prefix
	pszProtoPrefix = t2a(pszUrl);
	if (pszProtoPrefix != nullptr) {
		p = strchr(pszProtoPrefix, ':');
		if (p != nullptr) {
			*(++p) = 0; // remove trailing :
			// class name
			pszClassName = MakeUrlClassName(pszProtoPrefix);
			if (pszClassName != nullptr)
				if (!InvokeHandler_Worker(pszClassName, pszUrl, &res))
					// correct registry on error (no longer in list)
					RemoveRegClass(pszClassName);
			mir_free(pszClassName); // does NULL check
		}
		mir_free(pszProtoPrefix);
	}
	return res;
}

/************************* Options ********************************/

static int CALLBACK ListViewSortDesc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	int cmp;
	if (((ASSOCDATA*)lParam1)->pszFileExt != nullptr && ((ASSOCDATA*)lParam2)->pszFileExt != nullptr)
		cmp = CompareStringA((LCID)lParamSort, 0, ((ASSOCDATA*)lParam1)->pszFileExt, -1, ((ASSOCDATA*)lParam2)->pszFileExt, -1);
	else if (((ASSOCDATA*)lParam1)->pszFileExt == ((ASSOCDATA*)lParam2)->pszFileExt) // both NULL
		cmp = CompareStringA((LCID)lParamSort, 0, ((ASSOCDATA*)lParam1)->pszClassName, -1, ((ASSOCDATA*)lParam2)->pszClassName, -1);
	else // different types,  incomparable
		cmp = (((ASSOCDATA*)lParam1)->pszFileExt == nullptr) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
	if (cmp == CSTR_EQUAL)
		cmp = CompareString((LCID)lParamSort, 0, ((ASSOCDATA*)lParam1)->pszDescription, -1, ((ASSOCDATA*)lParam2)->pszDescription, -1);
	if (cmp != 0) cmp -= 2; // maintain CRT conventions
	return cmp;
}

#define M_REFRESH_ICONS  (WM_APP+1)
static INT_PTR CALLBACK AssocListOptDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	LVITEM lvi;
	ASSOCDATA *assoc;

	switch (msg) {
	case WM_INITDIALOG:
		{
			TranslateDialogDefault(hwndDlg);
			CoInitialize(nullptr);
			HWND hwndList = GetDlgItem(hwndDlg, IDC_ASSOCLIST);

			ListView_SetUnicodeFormat(hwndList, TRUE);

			SendDlgItemMessage(hwndDlg, IDC_HEADERTEXT, WM_SETFONT, SendMessage(GetParent(hwndDlg), PSM_GETBOLDFONT, 0, 0), 0);
			// checkboxes won't show up on Win95 without IE3+ or 4.70 (plugin opts uses the same)
			ListView_SetExtendedListViewStyle(hwndList, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
			// columns
			{
				LVCOLUMN lvc;
				lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
				lvc.pszText = TranslateT("Type");
				lvc.cx = 170;
				ListView_InsertColumn(hwndList, lvc.iSubItem = 0, &lvc);
				lvc.pszText = TranslateT("Description");
				ListView_InsertColumn(hwndList, lvc.iSubItem = 1, &lvc);
			}
			// create image storage
			HIMAGELIST himl;
			mir_cslock lck(csAssocList);
			{
				HDC hdc = GetDC(hwndList);
				if (hdc != nullptr) { // BITSPIXEL is compatible with ILC_COLOR flags
					himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), GetDeviceCaps(hdc, BITSPIXEL) | ILC_MASK, nAssocListCount, 0);
					ReleaseDC(hwndList, hdc);
				}
				else himl = nullptr;
			}
			ListView_SetImageList(hwndList, himl, LVSIL_SMALL); // autodestroyed
			// enum assoc list
			lvi.iSubItem = 0;
			lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
			for (int i = 0; i < nAssocListCount; ++i) {
				assoc = &pAssocList[i];
				lvi.iItem = 0;
				lvi.lParam = (LPARAM)CopyAssocItem(assoc);
				lvi.pszText = GetAssocTypeDesc(assoc);
				lvi.iImage = ReplaceImageListAssocIcon(himl, assoc, -1);
				lvi.iItem = ListView_InsertItem(hwndList, &lvi);
				if (lvi.iItem != -1) {
					ListView_SetItemText(hwndList, lvi.iItem, 1, assoc->pszDescription);
					ListView_SetCheckState(hwndList, lvi.iItem, IsAssocEnabled(assoc) && IsAssocRegistered(assoc));
				}
			}
			// sort items (before moving to groups)
			ListView_SortItems(hwndList, ListViewSortDesc, Langpack_GetDefaultLocale());
			// groups
			if (ListView_EnableGroupView(hwndList, TRUE) == 1) { // returns 0 on pre WinXP or if commctls6 are disabled
				LVGROUP lvg;
				int iItem;
				// dummy item for group
				lvi.iItem = ListView_GetItemCount(hwndList) - 1;
				lvi.iSubItem = 0;
				lvi.mask = LVIF_PARAM | LVIF_IMAGE;
				lvi.iImage = -1;
				lvi.lParam = 0;
				// insert groups
				lvg.cbSize = sizeof(lvg);
				lvg.mask = LVGF_HEADER | LVGF_GROUPID;
				lvg.iGroupId = 2;
				lvg.pszHeader = TranslateT("URLs on websites");
				lvi.iItem = ListView_InsertItem(hwndList, &lvi);
				if (lvi.iItem != -1) {
					ListView_InsertGroup(hwndList, lvi.iItem, &lvg);
					lvg.iGroupId = 1;
					lvg.pszHeader = TranslateT("File types");
					iItem = lvi.iItem = ListView_InsertItem(hwndList, &lvi);
					if (lvi.iItem != -1)
						ListView_InsertGroup(hwndList, lvi.iItem, &lvg);
					else ListView_DeleteItem(hwndList, iItem);
				}
				// move to group
				lvi.iSubItem = 0;
				lvi.mask = LVIF_PARAM | LVIF_GROUPID;
				for (lvi.iItem = 0; ListView_GetItem(hwndList, &lvi); ++lvi.iItem) {
					assoc = (ASSOCDATA*)lvi.lParam;
					if (assoc == nullptr) continue; // groups
					lvi.iGroupId = (assoc->pszFileExt == nullptr) + 1;
					ListView_SetItem(hwndList, &lvi);
				}
			}
			lvi.iItem = ListView_GetTopIndex(hwndList);
			ListView_SetItemState(hwndList, lvi.iItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
			ListView_SetColumnWidth(hwndList, 1, LVSCW_AUTOSIZE_USEHEADER); // size to fit window
			// only while running
			CheckDlgButton(hwndDlg, IDC_ONLYWHILERUNNING, (BOOL)db_get_b(NULL, "AssocMgr", "OnlyWhileRunning", SETTING_ONLYWHILERUNNING_DEFAULT) ? BST_CHECKED : BST_UNCHECKED);

			// autostart
			wchar_t *pszRunCmd = MakeRunCommand(TRUE, TRUE);
			if (pszRunCmd != nullptr) {
				CheckDlgButton(hwndDlg, IDC_AUTOSTART, IsRegRunEntry(L"MirandaNG", pszRunCmd) ? BST_CHECKED : BST_UNCHECKED);
				mir_free(pszRunCmd);
			}
		}
		return TRUE;

	case WM_SETTINGCHANGE:
	case M_REFRESH_ICONS:
		{
			HWND hwndList = GetDlgItem(hwndDlg, IDC_ASSOCLIST);
			HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL);
			// enum items
			lvi.iSubItem = 0;
			lvi.mask = LVIF_PARAM | LVIF_IMAGE;
			for (lvi.iItem = 0; ListView_GetItem(hwndList, &lvi); ++lvi.iItem) {
				assoc = (ASSOCDATA*)lvi.lParam;
				if (assoc == nullptr) continue; // groups
				lvi.iImage = ReplaceImageListAssocIcon(himl, assoc, lvi.iImage);
				ListView_SetItem(hwndList, &lvi);
			}
			if (lvi.iItem) { // ListView_Update() blinks
				ListView_RedrawItems(hwndList, 0, lvi.iItem - 1);
				UpdateWindow(hwndList);
			}
		}
		return TRUE;

	case WM_CTLCOLORSTATIC:
		// use same text color for header as for group boxes (WinXP+)
		if (GetDlgCtrlID((HWND)lParam) == IDC_HEADERTEXT) {
			lParam = (LPARAM)GetDlgItem(hwndDlg, IDC_MISCLABEL);
			HBRUSH hBrush = (HBRUSH)SendMessage(hwndDlg, msg, wParam, lParam);
			COLORREF clr;
			HTHEME hTheme = GetWindowTheme((HWND)lParam);
			if (hTheme != nullptr && !GetThemeColor(hTheme, BP_GROUPBOX, GBS_NORMAL, TMT_TEXTCOLOR, &clr)) {
				SetBkMode((HDC)wParam, TRANSPARENT);
				SetTextColor((HDC)wParam, clr);
			}
			return (INT_PTR)hBrush;
		}
		break;

	case WM_NCDESTROY:
		CoUninitialize();
		return TRUE;

	case WM_COMMAND:
		// enable apply
		PostMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
		break;

	case WM_NOTIFYFORMAT:
		SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, NFR_UNICODE);
		return TRUE;

	case WM_NOTIFY:
		NMHDR *nmhdr = (NMHDR*)lParam;
		switch (nmhdr->idFrom) {
		case IDC_ASSOCLIST:
			switch (nmhdr->code) {
			case LVN_DELETEITEM: // also called on WM_DESTROY
				lvi.mask = LVIF_PARAM;
				lvi.iSubItem = 0;
				lvi.iItem = ((NMLISTVIEW*)lParam)->iItem;
				// free memory
				if (ListView_GetItem(nmhdr->hwndFrom, &lvi))
					mir_free((ASSOCDATA*)lvi.lParam); // does NULL check
				return TRUE;

			case LVN_ITEMCHANGED:
				// enable apply (not while loading)
				if (IsWindowVisible(nmhdr->hwndFrom))
					PostMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
				return TRUE;
			
			case LVN_KEYDOWN:
				// workaround for WinXP (ListView with groups):
				// eat keyboard navigation that goes beyond the first item in list
				// as it would scroll out of scope in this case
				// bug should not be present using WinVista and higher
				switch (((NMLVKEYDOWN*)lParam)->wVKey) {
				case VK_UP:
					lvi.iSubItem = 0;
					lvi.mask = LVIF_PARAM;
					lvi.iItem = ListView_GetNextItem(nmhdr->hwndFrom, -1, LVNI_FOCUSED);
					lvi.iItem = ListView_GetNextItem(nmhdr->hwndFrom, lvi.iItem, LVNI_ABOVE);
					if (lvi.iItem != -1)
						if (ListView_GetItem(nmhdr->hwndFrom, &lvi))
							if ((ASSOCDATA*)lvi.lParam == nullptr) // groups
								lvi.iItem = -1;
					if (lvi.iItem == -1) {
						SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, TRUE); // eat it
						return TRUE;
					}
					break;

				case VK_PRIOR:
					lvi.iSubItem = 0;
					lvi.mask = LVIF_PARAM;
					lvi.iItem = ListView_GetNextItem(nmhdr->hwndFrom, -1, LVNI_FOCUSED);
					lvi.iItem -= ListView_GetCountPerPage(nmhdr->hwndFrom);
					if (lvi.iItem >= 0)
						if (ListView_GetItem(nmhdr->hwndFrom, &lvi))
							if ((ASSOCDATA*)lvi.lParam == nullptr) // groups
								lvi.iItem = -1;
					if (lvi.iItem < 0) {
						ListView_SetItemState(nmhdr->hwndFrom, 0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
						SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, TRUE); // eat it
						return TRUE;
					}
					break;
				}
				break;
			}
			break;

		case 0:
			switch (nmhdr->code) {
			case PSN_APPLY:
				BOOL fEnabled, fRegFailed = FALSE;

				// only while running
				db_set_b(NULL, "AssocMgr", "OnlyWhileRunning", (BYTE)(IsDlgButtonChecked(hwndDlg, IDC_ONLYWHILERUNNING) != 0));

				// save enabled assoc items
				HWND hwndList = GetDlgItem(hwndDlg, IDC_ASSOCLIST);
				lvi.iSubItem = 0;
				lvi.mask = LVIF_PARAM;
				mir_cslock lck(csAssocList);
				for (lvi.iItem = 0; ListView_GetItem(hwndList, &lvi); ++lvi.iItem) {
					assoc = (ASSOCDATA*)lvi.lParam;
					if (assoc == nullptr) continue; // groups
					fEnabled = ListView_GetCheckState(hwndList, lvi.iItem);
					SetAssocEnabled(assoc, fEnabled);

					// re-register registery keys
					if (fEnabled ? !EnsureAssocRegistered(assoc) : !UnregisterAssoc(assoc)) {
						char *pszErr = GetWinErrorDescription(GetLastError());
						ShowInfoMessage(NIIF_ERROR, Translate("File association error"), Translate("There was an error writing to the registry to modify the file/url associations.\nReason: %s"), (pszErr != nullptr) ? pszErr : Translate("Unknown"));
						mir_free(pszErr); // does NULL check
						fRegFailed = TRUE; // just show one time
					}
				}
				NotifyAssocChange(TRUE);
				PostMessage(hwndDlg, M_REFRESH_ICONS, 0, 0);

				// autostart
				wchar_t *pszRunCmd = MakeRunCommand(TRUE, TRUE);
				fRegFailed = FALSE;
				if (pszRunCmd != nullptr) {
					fEnabled = IsDlgButtonChecked(hwndDlg, IDC_AUTOSTART);
					if (fEnabled ? !AddRegRunEntry(L"MirandaNG", pszRunCmd) : !RemoveRegRunEntry(L"MirandaNG", pszRunCmd)) {
						char *pszErr;
						pszErr = GetWinErrorDescription(GetLastError());
						ShowInfoMessage(NIIF_ERROR, Translate("Autostart error"), Translate("There was an error writing to the registry to modify the autostart list.\n\nReason: %s"), (pszErr != nullptr) ? pszErr : Translate("Unknown"));
						mir_free(pszErr); // does NULL check
						fRegFailed = TRUE; // just show one time
					}
					mir_free(pszRunCmd);
				}
				return TRUE;
			}
		}
		break;
	}
	return FALSE;
}

static int AssocListOptInit(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.hInstance = hInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_ASSOCLIST);
	odp.position = 900000100; // network opts  =  900000000
	odp.szGroup.a = LPGEN("Services"); // autotranslated
	odp.szTitle.a = LPGEN("Associations"); // autotranslated
	odp.flags = ODPF_BOLDGROUPS;
	odp.pfnDlgProc = AssocListOptDlgProc;
	Options_AddPage(wParam, &odp);
	return 0;
}

/************************* Misc ***********************************/

void InitAssocList(void)
{
	// Options
	INITCOMMONCONTROLSEX icc;
	icc.dwSize = sizeof(icc);
	icc.dwICC = ICC_LISTVIEW_CLASSES;
	InitCommonControlsEx(&icc);
	hHookOptInit = HookEvent(ME_OPT_INITIALISE, AssocListOptInit);

	// Assoc List
	pAssocList = nullptr;
	nAssocListCount = 0;

	// Services
	CreateServiceFunction(MS_ASSOCMGR_ADDNEWFILETYPE, ServiceAddNewFileType);
	CreateServiceFunction(MS_ASSOCMGR_REMOVEFILETYPE, ServiceRemoveFileType);
	CreateServiceFunction(MS_ASSOCMGR_ADDNEWURLTYPE, ServiceAddNewUrlType);
	CreateServiceFunction(MS_ASSOCMGR_REMOVEURLTYPE, ServiceRemoveUrlType);

	// Notify Shell
	nNotifyTimerID = 0;

	// register open-with app
	{
		wchar_t *pszAppFileName, *pszIconLoc, *pszRunCmd;
		pszIconLoc = MakeIconLocation(nullptr, 0);

		// miranda32.exe
		pszAppFileName = MakeAppFileName(TRUE);
		pszRunCmd = MakeRunCommand(TRUE, FALSE);
		if (pszAppFileName != nullptr && pszRunCmd != nullptr)
			AddRegOpenWith(pszAppFileName, FALSE, _A2W(MIRANDANAME), pszIconLoc, pszRunCmd, nullptr, nullptr, nullptr);
		mir_free(pszRunCmd); // does NULL check
		mir_free(pszAppFileName); // does NULL check
		// assocmgr.dll
		pszAppFileName = MakeAppFileName(FALSE);
		pszRunCmd = MakeRunCommand(FALSE, TRUE);
		if (pszAppFileName != nullptr && pszRunCmd != nullptr)
			AddRegOpenWith(pszAppFileName, TRUE, _A2W(MIRANDANAME), pszIconLoc, pszRunCmd, DDEFILECMD, DDEAPP, DDETOPIC);
		mir_free(pszRunCmd); // does NULL check
		mir_free(pszAppFileName); // does NULL check

		mir_free(pszIconLoc); // does NULL check
	}

	// default items
	{
		FILETYPEDESC ftd;
		ftd.cbSize = sizeof(FILETYPEDESC);
		ftd.pszFileExt = ".dat";
		ftd.pszMimeType = nullptr;
		ftd.pwszDescription = TranslateT("Miranda NG database");
		ftd.hInstance = hInst;
		ftd.nIconResID = IDI_MIRANDAFILE;
		ftd.ptszVerbDesc = nullptr;
		ftd.pszService = nullptr;
		ftd.flags = FTDF_DEFAULTDISABLED | FTDF_TCHAR;
		ServiceAddNewFileType(0, (LPARAM)&ftd);
	}
}

void UninitAssocList(void)
{
	// Options
	UnhookEvent(hHookOptInit);

	// Assoc List
	BYTE fOnlyWhileRunning = db_get_b(NULL, "AssocMgr", "OnlyWhileRunning", SETTING_ONLYWHILERUNNING_DEFAULT);
	for (int i = 0; i < nAssocListCount; ++i) {
		ASSOCDATA *assoc = &pAssocList[i];

		// remove registry keys
		if (fOnlyWhileRunning)
			UnregisterAssoc(assoc);

		mir_free(assoc->pszClassName);
		mir_free(assoc->pszDescription);
		mir_free(assoc->pszService);
		mir_free(assoc->pszFileExt);
		mir_free(assoc->pszVerbDesc);
		mir_free(assoc->pszMimeType);
	}
	mir_free(pAssocList);

	// Notify Shell
	if (fOnlyWhileRunning && nAssocListCount)
		NotifyAssocChange(TRUE);

	// unregister open-with app
	if (fOnlyWhileRunning) {
		// miranda32.exe
		ptrW pszAppFileName(MakeAppFileName(TRUE));
		if (pszAppFileName != NULL)
			RemoveRegOpenWith(pszAppFileName);
		
		// assocmgr.dll
		pszAppFileName = MakeAppFileName(FALSE);
		if (pszAppFileName != NULL)
			RemoveRegOpenWith(pszAppFileName);
	}
}