Debian Codegraph @

plugins/wlx/MacPreview/src/MacPreview.lpr

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 MacOS preview plugin
5
6 Copyright (C) 2022-2024 Alexander Koblov ([email protected])
7 Copyright (C) 2022-2024 Rich Chang ([email protected])
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 }
22
23 library MacPreview;
24
25 {$mode objfpc}{$H+}
26 {$modeswitch objectivec1}
27
28 uses
29 SysUtils, WlxPlugin, QuickLookUI, CFPropertyList,
30 MacOSAll, CocoaAll, UniformTypeIdentifiers;
31
32 const
33 NSAppKitVersionNumber11_0 = 2022;
34
35 type
36 TQLPItem = objcclass(NSObject, QLPreviewItemProtocol)
37 function previewItemURL: NSURL;
38 private
39 url: NSURL;
40 public
41 ext: ShortString;
42 procedure initPath( path: pchar); message 'initPath:';
43 end;
44
45 DCQLPreview = objcclass(QLPreviewView)
46 function acceptsFirstResponder: ObjCBOOL; override;
47 end;
48
49 var
50 QLContentTypes: NSMutableArray;
51 QLContentUTTypes: NSMutableArray;
52
53 const
54 ExcludeList: array[0..2] of PAnsiChar =
55 (
56 'public.plain-text',
57 'public.json',
58 'public.xml'
59 );
60
61 // copy from uMyDarwin
62 function StringToNSString(const S: String): NSString;
63 begin
64 Result:= NSString(NSString.stringWithUTF8String(PAnsiChar(S)));
65 end;
66
67 // copy from DCStrUtils
68 function ExtractOnlyFileExt(const FileName: string): string;
69 var
70 I : LongInt;
71 SOF : Boolean;
72 EndSep : Set of Char;
73 begin
74 Result := EmptyStr;
75 I := Length(FileName);
76 EndSep:= AllowDirectorySeparators + AllowDriveSeparators + [ExtensionSeparator];
77 while (I > 0) and not (FileName[I] in EndSep) do Dec(I);
78 if (I > 0) and (FileName[I] = ExtensionSeparator) then
79 begin
80 SOF:= (I = 1) or (FileName[I - 1] in AllowDirectorySeparators);
81 if (not SOF) or FirstDotAtFileNameStartIsExtension then
82 Result := Copy(FileName, I + 1, MaxInt)
83 end;
84 end;
85
86 procedure AddContentType(AType: NSString);
87 var
88 anObject: id;
89 begin
90 QLContentTypes.addObject(AType);
91 if NSAppKitVersionNumber >= NSAppKitVersionNumber11_0 then begin
92 anObject:= UTType.typeWithIdentifier(AType);
93 if Assigned(anObject) then QLContentUTTypes.addObject(anObject);
94 end;
95 end;
96
97 function CheckContentType(const Name: String; FileType: NSString): Boolean;
98 var
99 Index: Integer;
100 begin
101 // Special case
102 if (Name = 'Text.qlgenerator') then
103 begin
104 for Index:= 0 to High(ExcludeList) do
105 begin
106 if StrComp(FileType.UTF8String, ExcludeList[Index]) = 0 then
107 Exit(False)
108 end;
109 end;
110 Result:= True;
111 end;
112
113 procedure ParseFile(const Path, Name: String);
114 var
115 Data: NSData;
116 I, J: Integer;
117 Dict: NSDictionary;
118 FileType: NSString;
119 DocumentTypes, ContentTypes: NSArray;
120 begin
121 Data:= NSData.dataWithContentsOfFile(StringToNSString(Path + Name + '/Contents/Info.plist'));
122 if Assigned(Data) then
123 begin
124 Dict:= NSDictionary(NSPropertyListSerialization.propertyListWithData_options_format_error(Data, NSPropertyListImmutable, nil, nil));
125 if Assigned(Dict) then
126 begin
127 DocumentTypes:= NSArray(Dict.valueForKey(StringToNSString('CFBundleDocumentTypes')));
128 if Assigned(DocumentTypes) then
129 begin
130 for I:= 0 to Integer(DocumentTypes.count) - 1 do
131 begin
132 Dict:= NSDictionary(DocumentTypes.objectAtIndex(I));
133 ContentTypes:= NSArray(Dict.valueForKey(StringToNSString('LSItemContentTypes')));
134 if Assigned(ContentTypes) then
135 begin
136 for J:= 0 to Integer(ContentTypes.count - 1) do
137 begin
138 FileType:= NSString(ContentTypes.objectAtIndex(J));
139 if CheckContentType(Name, FileType) then AddContentType(FileType);
140 end;
141 end;
142 end;
143 end;
144 end;
145 end;
146 end;
147
148 procedure ParseFolder(const Path: String);
149 var
150 FindData: TSearchRec;
151 begin
152 if FindFirst(Path + '*.qlgenerator', faDirectory, FindData) = 0 then
153 begin
154 repeat
155 ParseFile(Path, FindData.Name);
156 until FindNext(FindData) <> 0;
157 FindClose(FindData);
158 end;
159 end;
160
161 function CheckFile_oldMacOS(const FileName: String): Boolean;
162 var
163 Index: Integer;
164 QLType: NSString;
165 FileType: NSString;
166 begin
167 FileType:= NSWorkspace.sharedWorkspace.typeOfFile_error(StringToNSString(FileName), nil);
168 if (FileType = nil) then
169 begin
170 Result:= False;
171 end
172 else begin
173 for Index:= 0 to QLContentTypes.Count - 1 do
174 begin
175 QLType:= NSString(QLContentTypes.objectAtIndex(Index));
176 // Direct comparison
177 if (FileType.compare(QLType) = NSOrderedSame) then
178 Exit(True);
179 // Conforms checking
180 if UTTypeConformsTo(CFStringRef(FileType), CFStringRef(QLType)) then
181 Exit(True);
182 end;
183 Result:= False;
184 end;
185 end;
186
187 function CheckFile_newMacOS(const FileName: String): Boolean;
188 var
189 Index: Integer;
190 FileExt: String;
191 QLUTType: UTType;
192 QLType: NSString;
193 FileType: NSString;
194 FileUTType: UTType;
195 begin
196 FileExt:= ExtractOnlyFileExt(FileName);
197 FileUTType:= UTType.typeWithFilenameExtension(StringToNSString(FileExt));
198 if (FileUTType = nil) then
199 begin
200 Result:= False;
201 end
202 else begin
203 FileType:= FileUTType.identifier;
204 // Direct comparison
205 for Index:= 0 to QLContentTypes.Count - 1 do
206 begin
207 QLType:= NSString(QLContentTypes.objectAtIndex(Index));
208 if (FileType.compare(QLType) = NSOrderedSame) then
209 Exit(True);
210 end;
211 // Conforms checking
212 for Index:= 0 to QLContentUTTypes.Count - 1 do
213 begin
214 QLUTType:= UTType(QLContentUTTypes.objectAtIndex(Index));
215 if FileUTType.conformsToType(QLUTType) then
216 Exit(True);
217 end;
218 Result:= False;
219 end;
220 end;
221
222 function CheckFile(const FileName: String): Boolean;
223 begin
224 if NSAppKitVersionNumber >= NSAppKitVersionNumber11_0 then
225 Result:= CheckFile_newMacOS( FileName )
226 else
227 Result:= CheckFile_oldMacOS( FileName );
228 end;
229
230 function TQLPItem.previewItemURL: NSURL;
231 begin
232 Result:= url;
233 end;
234
235 procedure TQLPItem.initPath( path: pchar );
236 begin
237 url:= NSURL.fileURLWithPath( StringToNSString(path) );
238 ext:= UpperCase( ExtractOnlyFileExt(path) );
239 end;
240
241 function DCQLPreview.acceptsFirstResponder: ObjCBOOL;
242 begin
243 Result:= false;
244 end;
245
246 procedure setFilepath( view:QLPreviewView; filepath:String );
247 var
248 item: TQLPItem;
249 begin
250 if filepath=EmptyStr then begin
251 item:= nil;
252 end else begin
253 item:= TQLPItem.alloc.init;
254 item.initPath( pchar(filepath) );
255 end;
256 view.setPreviewItem( item );
257 item.release;
258 end;
259
260 function ListLoad( ParentWin:THandle; FileToLoad:pchar; {%H-}ShowFlags:integer):THandle; cdecl;
261 var
262 view: QLPreviewView;
263 begin
264 if not CheckFile(FileToLoad) then
265 Exit(wlxInvalidHandle);
266
267 view:= DCQLPreview.alloc.init;
268 view.setAutostarts( true );
269 view.setShouldCloseWithWindow( false );
270 NSView(ParentWin).addSubview( view );
271 setFilepath( view, FileToLoad );
272 Result:= THandle( view );
273 end;
274
275 function isExtChanged( view: QLPreviewView; FileToLoad:pchar ): boolean;
276 var
277 item: TQLPItem;
278 newExt: ShortString;
279 begin
280 item:= {%H-}TQLPItem( view.previewItem );
281 newExt:= upperCase( ExtractOnlyFileExt( FileToLoad ) );
282 Result:= item.ext<>newExt;
283 end;
284
285 function ListLoadNext( {%H-}ParentWin,PluginWin:THandle; FileToLoad:pchar; {%H-}ShowFlags:integer):integer; cdecl;
286 var
287 view: QLPreviewView;
288 begin
289 if not CheckFile(FileToLoad) then
290 Exit(LISTPLUGIN_ERROR);
291
292 view:= QLPreviewView(PluginWin);
293
294 // workaround for the bug of MacOS Quick Look:
295 // when previewing different types of files continuously, occasionally exceptions occur.
296 // such as previewing a large .pas file immediately after previewing a pdf file.
297 // empty the original preview file first can solve such problems.
298 if isExtChanged(view,FileToLoad) then setFilepath(view,EmptyStr);
299
300 setFilepath( view, FileToLoad );
301 Result:= LISTPLUGIN_OK;
302 end;
303
304 procedure ListCloseWindow(ListWin:THandle); cdecl;
305 begin
306 QLPreviewView(ListWin).close;
307 QLPreviewView(ListWin).removeFromSuperview;
308 end;
309
310 procedure ListSetDefaultParams(dps: PListDefaultParamStruct); cdecl;
311 begin
312 QLContentTypes:= NSMutableArray.alloc.init;
313 QLContentUTTypes:= NSMutableArray.alloc.init;
314 ParseFolder('/Library/QuickLook/');
315 ParseFolder('/System/Library/QuickLook/');
316 ParseFolder(IncludeTrailingBackslash(GetUserDir) + 'Library/QuickLook/');
317 end;
318
319 procedure ListGetDetectString(DetectString:pchar;maxlen:integer); cdecl;
320 begin
321 StrLCopy(DetectString, '(EXT!="")', MaxLen);
322 end;
323
324 exports
325 ListLoad,
326 ListLoadNext,
327 ListCloseWindow,
328 ListGetDetectString,
329 ListSetDefaultParams;
330
331 end.
332