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
|
{
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<TAssocDocType>)
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('<document_type> requires non-empty "name" attribute');
if CharsPos(AllChars - ['a'..'z', 'A'..'Z', '0'..'9'], FName) <> 0 then
raise Exception.CreateFmt('<document_type> "name" attribute must contain only alphanumeric characters, for maximum portability: "%s"', [
FName
]);
FCaption := Element.AttributeString('caption');
if FCaption = '' then
raise Exception.Create('<document_type> 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 <document_type> must not start with a dot: "%s"', [S]);
Extensions.Add(S);
end;
finally FreeAndNil(ChildElements) end;
if Extensions.Count = 0 then
raise Exception.Create('<document_type> requires at least one <extension>');
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('<document_type> requires at least one <mime>');
end;
function TAssocDocType.ToPListBundleDocumentTypesSection(const ProjectQualifiedName, DefaultIcon: string): string;
var
FinalIcon: string;
begin
if FIcon <> '' then
FinalIcon := FIcon
else
FinalIcon := DefaultIcon;
Result :=
#9#9'<dict>' + NL +
#9#9#9'<key>CFBundleTypeIconFiles</key>' + NL +
#9#9#9'<array>' + NL +
#9#9#9#9'<string>' + FinalIcon + '</string>' + NL +
#9#9#9'</array>' + NL +
#9#9#9'<key>CFBundleTypeName</key>' + NL +
#9#9#9'<string>' + FCaption + '</string>' + NL +
#9#9#9'<key>CFBundleTypeRole</key>' + NL +
#9#9#9'<string>Viewer</string>' + NL +
#9#9#9'<key>LSHandlerRank</key>' + NL +
#9#9#9'<string>Owner</string>' + NL +
#9#9#9'<key>LSItemContentTypes</key>' + NL +
#9#9#9'<array>' + NL +
#9#9#9#9'<string>' + ProjectQualifiedName + '.' + FName + '</string>' + NL +
#9#9#9'</array>' + NL +
#9#9'</dict>' + NL;
end;
function TAssocDocType.ToPListExportedTypeDeclarationsSection(const ProjectQualifiedName: string): string;
var
S: String;
begin
Result :=
#9#9'<dict>' + NL +
#9#9#9'<key>UTTypeConformsTo</key>' + NL +
#9#9#9'<array>' + NL +
#9#9#9#9'<string>public.data</string>' + NL +
#9#9#9'</array>' + NL +
#9#9#9'<key>UTTypeDescription</key>' + NL +
#9#9#9'<string>' + FCaption + '</string>' + NL +
#9#9#9'<key>UTTypeIdentifier</key>' + NL +
#9#9#9'<string>' + ProjectQualifiedName + '.' + FName + '</string>' + NL +
#9#9#9'<key>UTTypeTagSpecification</key>' + NL +
#9#9#9'<dict>' + NL +
#9#9#9#9'<key>public.filename-extension</key>' + NL +
#9#9#9#9'<array>' + NL;
for S in Extensions do
Result := Result +
#9#9#9#9#9'<string>' + S + '</string>' + NL;
Result := Result +
#9#9#9#9'</array>' + NL +
#9#9#9#9'<key>public.mime-type</key>' + NL +
#9#9#9#9'<array>' + NL;
for S in MimeTypes do
Result := Result +
#9#9#9#9#9'<string>' + S + '</string>' + NL;
Result := Result +
#9#9#9#9'</array>' + NL +
#9#9#9'</dict>' + NL +
#9#9'</dict>' + NL;
end;
function TAssocDocType.ToIntentFilterPathPattern: string;
var
Ext: String;
begin
Result := '';
for Ext in Extensions do
begin
Result := Result +
#9#9#9#9'<data android:pathPattern=".*\\.' + Ext + '" />' + NL +
#9#9#9#9'<data android:pathPattern=".*\\..*\\.' + Ext + '" />' + NL + // to match dot in filename
#9#9#9#9'<data android:pathPattern=".*\\..*\\..*\\.' + Ext + '" />' + 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'<key>CFBundleDocumentTypes</key>' + NL +
#9'<array>' + NL +
#9#9'<dict>' + NL +
#9#9#9'<key>CFBundleTypeRole</key>' + NL +
#9#9#9'<string>Viewer</string>' + NL +
#9#9#9'<key>CFBundleTypeExtensions</key>' + NL +
#9#9#9'<array>' + NL +
#9#9#9#9'<string>*</string>' + NL +
#9#9#9'</array>' + NL +
#9#9#9'<key>CFBundleTypeOSTypes</key>' + NL +
#9#9#9'<array>' + NL +
#9#9#9#9'<string>fold</string>' + NL +
#9#9#9#9'<string>disk</string>' + NL +
#9#9#9#9'<string>****</string>' + NL +
#9#9#9'</array>' + NL +
#9#9'</dict>' + NL +
BundleDocumentTypes +
#9'</array>' + NL;
if ExportedTypeDeclarations <> '' then
Result := Result +
#9'<key>UTExportedTypeDeclarations</key>' + NL +
#9'<array>' + NL +
ExportedTypeDeclarations +
#9'</array>';
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'<intent-filter>' + NL +
#9#9#9#9'<action android:name="android.intent.action.VIEW" />' + NL +
#9#9#9#9'<category android:name="android.intent.category.DEFAULT" />' + NL +
#9#9#9#9'<category android:name="android.intent.category.BROWSABLE" />' + NL +
#9#9#9#9'<data android:scheme="content" />' + NL +
#9#9#9#9'<data android:scheme="file" />' + NL +
#9#9#9#9'<data android:mimeType="*/*" />' + NL +
PathPatterns +
#9#9#9'</intent-filter>' + NL +
#9#9#9'<intent-filter>' + NL +
#9#9#9#9'<action android:name="android.intent.action.VIEW" />' + NL +
#9#9#9#9'<category android:name="android.intent.category.DEFAULT" />' + NL +
#9#9#9#9'<category android:name="android.intent.category.BROWSABLE" />' + NL +
#9#9#9#9'<data android:scheme="http" />' + NL +
#9#9#9#9'<data android:scheme="https" />' + NL +
#9#9#9#9'<data android:scheme="ftp" />' + NL +
#9#9#9#9'<data android:host="*" />' + NL +
PathPatterns +
#9#9#9'</intent-filter>';
end;
end.
|