Debian Codegraph @

plugins/wlx/preview/src/upreviewhandler.pas

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 Explorer preview handler
5
6 Copyright (C) 2021 Alexander Koblov ([email protected])
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 }
27
28 unit uPreviewHandler;
29
30 {$mode delphi}
31
32 interface
33
34 uses
35 Classes, SysUtils, Windows, ShlObj, ActiveX;
36
37 type
38 IPreviewHandler = interface(IUnknown)
39 ['{8895B1C6-B41F-4C1C-A562-0D564250836F}']
40 function SetWindow(hwnd: HWND; const prc: RECT): HRESULT; stdcall;
41 function SetRect(const prc: PRECT): HRESULT; stdcall;
42 function DoPreview(): HRESULT; stdcall;
43 function Unload(): HRESULT; stdcall;
44 function SetFocus(): HRESULT; stdcall;
45 function QueryFocus(out phwnd: HWND): HRESULT; stdcall;
46 function TranslateAccelerator(pmsg: PMSG): HRESULT; stdcall;
47 end;
48
49 function GetPreviewHandler(const FileName: UnicodeString): IPreviewHandler;
50
51 implementation
52
53 uses
54 ComObj, DCConvertEncoding, DCClassesUtf8;
55
56 type
57 IInitializeWithFile = interface(IUnknown)
58 ['{B7D14566-0509-4CCE-A71F-0A554233BD9B}']
59 function Initialize(pszFilePath: LPCWSTR; grfMode: DWORD): HRESULT; stdcall;
60 end;
61
62 IInitializeWithStream = interface(IUnknown)
63 ['{B824B49D-22AC-4161-AC8A-9916E8FA3F7F}']
64 function Initialize(const pstream: IStream; grfMode: DWORD): HRESULT; stdcall;
65 end;
66
67 IInitializeWithItem = interface(IUnknown)
68 ['{7F73BE3F-FB79-493C-A6C7-7EE14E245841}']
69 function Initialize(const psi: IShellItem; grfMode: DWORD): HRESULT; stdcall;
70 end;
71
72 var
73 SHCreateItemFromParsingName: function(pszPath: LPCWSTR; const pbc: IBindCtx;
74 const riid: TIID; out ppv): HRESULT; stdcall;
75
76 function AssocQueryStringW(flags: DWORD; str: DWORD; pszAssoc: LPCWSTR; pszExtra: LPCWSTR;
77 pszOut: LPWSTR; pcchOut: PDWORD): HRESULT; stdcall; external 'shlwapi.dll';
78
79 function GetShellClass(const FileExt: UnicodeString; interfaceID: TGUID): TGUID;
80 const
81 ASSOCSTR_SHELLEXTENSION = 16;
82 ASSOCF_INIT_DEFAULTTOSTAR = $00000004;
83 var
84 Res: HRESULT;
85 cchOut: DWORD = MAX_PATH;
86 ABuffer: array[0..MAX_PATH] of WideChar;
87 begin
88 Res := AssocQueryStringW(ASSOCF_INIT_DEFAULTTOSTAR, ASSOCSTR_SHELLEXTENSION, PWideChar(FileExt),
89 PWideChar(CeUtf8ToUtf16(GuidToString(interfaceID))), ABuffer, @cchOut);
90 if (Res <> S_OK) then Exit(Default(TGUID));
91 Res := CLSIDFromString(ABuffer, @Result);
92 if (Res <> NOERROR) then Exit(Default(TGUID));
93 end;
94
95 function GetPreviewHandler(const FileName: UnicodeString): IPreviewHandler;
96 var
97 Res: HRESULT;
98 ClassID: TGUID;
99 AStream: IStream;
100 AFile: TFileStreamEx;
101 AShellItem: IShellItem;
102 AInitializeWithFile: IInitializeWithFile;
103 AInitializeWithItem: IInitializeWithItem;
104 AInitializeWithStream: IInitializeWithStream;
105 begin
106 ClassID:= GetShellClass(ExtractFileExt(FileName), IPreviewHandler);
107 if IsEqualGUID(ClassID, Default(TGUID)) then Exit(nil);
108 Result:= CreateComObject(ClassID) as IPreviewHandler;
109 if Assigned(Result) then
110 begin
111 if Supports(Result, IInitializeWithFile, AInitializeWithFile) then
112 Res:= AInitializeWithFile.Initialize(PWideChar(FileName), STGM_READ)
113 else if Supports(Result, IInitializeWithStream, AInitializeWithStream) then
114 try
115 AFile:= TFileStreamEx.Create(CeUtf16ToUtf8(FileName), fmOpenRead or fmShareDenyNone);
116 AStream:= TStreamAdapter.Create(AFile, soOwned) as IStream;
117 Res:= AInitializeWithStream.Initialize(AStream, STGM_READ);
118 except
119 Res:= E_FAIL;
120 end
121 else if (Win32MajorVersion > 5) and Supports(Result, IInitializeWithItem, AInitializeWithItem) then
122 begin
123 Res:= SHCreateItemFromParsingName(PWideChar(FileName), nil, IShellItem, AShellItem);
124 if Succeeded(Res) then Res:= AInitializeWithItem.Initialize(AShellItem, STGM_READ);
125 end
126 else begin
127 Res:= E_FAIL;
128 end;
129 if not Succeeded(Res) then
130 begin
131 Result:= nil;
132 AStream:= nil;
133 end;
134 end;
135 end;
136
137 initialization
138 if (Win32MajorVersion > 5) then
139 SHCreateItemFromParsingName:= GetProcAddress(GetModuleHandle('shell32.dll'),
140 'SHCreateItemFromParsingName');
141
142 end.
143