{ Copyright 2014-2022 Michalis Kamburelis and Jan Adamec. This file is part of "Castle Game Engine". "Castle Game Engine" is free software; see the file COPYING.txt, included in this distribution, for details about the copyright. "Castle Game Engine" 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. ---------------------------------------------------------------------------- } { Associated documents types (file extensions) requested by the project. } unit ToolAssocDocTypes; interface uses SysUtils, Generics.Collections, DOM, Classes, CastleUtils, CastleStringUtils; type TAssocDocType = class private FCaption, FName, FIcon: String; Extensions, MimeTypes: TStringList; public constructor Create; destructor Destroy; override; procedure ReadCastleEngineManifest(const Element: TDOMElement); function ToPListBundleDocumentTypesSection(const ProjectQualifiedName, DefaultIcon: string): string; function ToPListExportedTypeDeclarationsSection(const ProjectQualifiedName: string): string; function ToIntentFilterPathPattern: string; end; TAssociatedDocTypeList = class({$ifdef FPC}specialize{$endif} TObjectList) public procedure ReadCastleEngineManifest(const Element: TDOMElement); function ToPListSection(const ProjectQualifiedName, DefaultIcon: string): string; function ToIntentFilter: string; end; implementation uses XMLRead, XMLWrite, CastleXmlUtils, CastleUriUtils, CastleFilesUtils; { TAssocDocType ------------------------------------------------------ } constructor TAssocDocType.Create; begin inherited; Extensions := TStringList.Create; MimeTypes := TStringList.Create; end; destructor TAssocDocType.Destroy; begin FreeAndNil(Extensions); FreeAndNil(MimeTypes); inherited; end; procedure TAssocDocType.ReadCastleEngineManifest(const Element: TDOMElement); var ChildElements: TXMLElementIterator; S: String; begin FName := Element.AttributeString('name'); if FName = '' then raise Exception.Create(' requires non-empty "name" attribute'); if CharsPos(AllChars - ['a'..'z', 'A'..'Z', '0'..'9'], FName) <> 0 then raise Exception.CreateFmt(' "name" attribute must contain only alphanumeric characters, for maximum portability: "%s"', [ FName ]); FCaption := Element.AttributeString('caption'); if FCaption = '' then raise Exception.Create(' requires non-empty "caption" attribute'); FIcon := Element.AttributeStringDef('icon', ''); Extensions.Clear; ChildElements := Element.ChildrenIterator('extension'); try while ChildElements.GetNext do begin S := Trim(ChildElements.Current.TextData); if SCharIs(S, 1, '.') then raise Exception.CreateFmt('Extensions in must not start with a dot: "%s"', [S]); Extensions.Add(S); end; finally FreeAndNil(ChildElements) end; if Extensions.Count = 0 then raise Exception.Create(' requires at least one '); MimeTypes.Clear; ChildElements := Element.ChildrenIterator('mime'); try while ChildElements.GetNext do begin S := Trim(ChildElements.Current.TextData); MimeTypes.Add(S); end; finally FreeAndNil(ChildElements) end; if MimeTypes.Count = 0 then raise Exception.Create(' requires at least one '); end; function TAssocDocType.ToPListBundleDocumentTypesSection(const ProjectQualifiedName, DefaultIcon: string): string; var FinalIcon: string; begin if FIcon <> '' then FinalIcon := FIcon else FinalIcon := DefaultIcon; Result := #9#9'' + NL + #9#9#9'CFBundleTypeIconFiles' + NL + #9#9#9'' + NL + #9#9#9#9'' + FinalIcon + '' + NL + #9#9#9'' + NL + #9#9#9'CFBundleTypeName' + NL + #9#9#9'' + FCaption + '' + NL + #9#9#9'CFBundleTypeRole' + NL + #9#9#9'Viewer' + NL + #9#9#9'LSHandlerRank' + NL + #9#9#9'Owner' + NL + #9#9#9'LSItemContentTypes' + NL + #9#9#9'' + NL + #9#9#9#9'' + ProjectQualifiedName + '.' + FName + '' + NL + #9#9#9'' + NL + #9#9'' + NL; end; function TAssocDocType.ToPListExportedTypeDeclarationsSection(const ProjectQualifiedName: string): string; var S: String; begin Result := #9#9'' + NL + #9#9#9'UTTypeConformsTo' + NL + #9#9#9'' + NL + #9#9#9#9'public.data' + NL + #9#9#9'' + NL + #9#9#9'UTTypeDescription' + NL + #9#9#9'' + FCaption + '' + NL + #9#9#9'UTTypeIdentifier' + NL + #9#9#9'' + ProjectQualifiedName + '.' + FName + '' + NL + #9#9#9'UTTypeTagSpecification' + NL + #9#9#9'' + NL + #9#9#9#9'public.filename-extension' + NL + #9#9#9#9'' + NL; for S in Extensions do Result := Result + #9#9#9#9#9'' + S + '' + NL; Result := Result + #9#9#9#9'' + NL + #9#9#9#9'public.mime-type' + NL + #9#9#9#9'' + NL; for S in MimeTypes do Result := Result + #9#9#9#9#9'' + S + '' + NL; Result := Result + #9#9#9#9'' + NL + #9#9#9'' + NL + #9#9'' + NL; end; function TAssocDocType.ToIntentFilterPathPattern: string; var Ext: String; begin Result := ''; for Ext in Extensions do begin Result := Result + #9#9#9#9'' + NL + #9#9#9#9'' + NL + // to match dot in filename #9#9#9#9'' + NL; end; end; { TAssociatedDocTypeList ------------------------------------------------------ } procedure TAssociatedDocTypeList.ReadCastleEngineManifest(const Element: TDOMElement); var ChildElements: TXMLElementIterator; ChildElement: TDOMElement; DocType: TAssocDocType; begin ChildElements := Element.ChildrenIterator('document_type'); try while ChildElements.GetNext do begin ChildElement := ChildElements.Current; DocType := TAssocDocType.Create; Add(DocType); DocType.ReadCastleEngineManifest(ChildElement); end; finally FreeAndNil(ChildElements) end; end; function TAssociatedDocTypeList.ToPListSection(const ProjectQualifiedName, DefaultIcon: string): string; var BundleDocumentTypes, ExportedTypeDeclarations: string; DocType: TAssocDocType; begin BundleDocumentTypes := ''; ExportedTypeDeclarations := ''; for DocType in Self do begin BundleDocumentTypes := BundleDocumentTypes + DocType.ToPListBundleDocumentTypesSection(ProjectQualifiedName, DefaultIcon); ExportedTypeDeclarations := ExportedTypeDeclarations + DocType.ToPListExportedTypeDeclarationsSection(ProjectQualifiedName); end; Result := #9'CFBundleDocumentTypes' + NL + #9'' + NL + #9#9'' + NL + #9#9#9'CFBundleTypeRole' + NL + #9#9#9'Viewer' + NL + #9#9#9'CFBundleTypeExtensions' + NL + #9#9#9'' + NL + #9#9#9#9'*' + NL + #9#9#9'' + NL + #9#9#9'CFBundleTypeOSTypes' + NL + #9#9#9'' + NL + #9#9#9#9'fold' + NL + #9#9#9#9'disk' + NL + #9#9#9#9'****' + NL + #9#9#9'' + NL + #9#9'' + NL + BundleDocumentTypes + #9'' + NL; if ExportedTypeDeclarations <> '' then Result := Result + #9'UTExportedTypeDeclarations' + NL + #9'' + NL + ExportedTypeDeclarations + #9''; end; // https://stackoverflow.com/questions/3760276/android-intent-filter-associate-app-with-file-extension function TAssociatedDocTypeList.ToIntentFilter: string; var PathPatterns: string; DocType: TAssocDocType; begin if Count = 0 then Exit(''); PathPatterns := ''; for DocType in Self do begin PathPatterns := PathPatterns + DocType.ToIntentFilterPathPattern; end; Result := #9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + PathPatterns + #9#9#9'' + NL + #9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + #9#9#9#9'' + NL + PathPatterns + #9#9#9''; end; end.