| 1 |
{ |
| 2 |
Copyright 2014-2022 Michalis Kamburelis and Jan Adamec. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Associated documents types (file extensions) requested by the project. } |
| 17 |
unit ToolAssocDocTypes; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, Generics.Collections, DOM, Classes, |
| 22 |
CastleUtils, CastleStringUtils; |
| 23 |
|
| 24 |
type |
| 25 |
TAssocDocType = class |
| 26 |
private |
| 27 |
FCaption, FName, FIcon: String; |
| 28 |
Extensions, MimeTypes: TStringList; |
| 29 |
public |
| 30 |
constructor Create; |
| 31 |
destructor Destroy; override; |
| 32 |
procedure ReadCastleEngineManifest(const Element: TDOMElement); |
| 33 |
function ToPListBundleDocumentTypesSection(const ProjectQualifiedName, DefaultIcon: string): string; |
| 34 |
function ToPListExportedTypeDeclarationsSection(const ProjectQualifiedName: string): string; |
| 35 |
function ToIntentFilterPathPattern: string; |
| 36 |
end; |
| 37 |
|
| 38 |
TAssociatedDocTypeList = class({$ifdef FPC}specialize{$endif} TObjectList<TAssocDocType>) |
| 39 |
public |
| 40 |
procedure ReadCastleEngineManifest(const Element: TDOMElement); |
| 41 |
function ToPListSection(const ProjectQualifiedName, DefaultIcon: string): string; |
| 42 |
function ToIntentFilter: string; |
| 43 |
end; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses XMLRead, XMLWrite, |
| 48 |
CastleXmlUtils, CastleUriUtils, CastleFilesUtils; |
| 49 |
|
| 50 |
{ TAssocDocType ------------------------------------------------------ } |
| 51 |
|
| 52 |
constructor TAssocDocType.Create; |
| 53 |
begin |
| 54 |
inherited; |
| 55 |
Extensions := TStringList.Create; |
| 56 |
MimeTypes := TStringList.Create; |
| 57 |
end; |
| 58 |
|
| 59 |
destructor TAssocDocType.Destroy; |
| 60 |
begin |
| 61 |
FreeAndNil(Extensions); |
| 62 |
FreeAndNil(MimeTypes); |
| 63 |
inherited; |
| 64 |
end; |
| 65 |
|
| 66 |
procedure TAssocDocType.ReadCastleEngineManifest(const Element: TDOMElement); |
| 67 |
var |
| 68 |
ChildElements: TXMLElementIterator; |
| 69 |
S: String; |
| 70 |
begin |
| 71 |
FName := Element.AttributeString('name'); |
| 72 |
if FName = '' then |
| 73 |
raise Exception.Create('<document_type> requires non-empty "name" attribute'); |
| 74 |
if CharsPos(AllChars - ['a'..'z', 'A'..'Z', '0'..'9'], FName) <> 0 then |
| 75 |
raise Exception.CreateFmt('<document_type> "name" attribute must contain only alphanumeric characters, for maximum portability: "%s"', [ |
| 76 |
FName |
| 77 |
]); |
| 78 |
|
| 79 |
FCaption := Element.AttributeString('caption'); |
| 80 |
if FCaption = '' then |
| 81 |
raise Exception.Create('<document_type> requires non-empty "caption" attribute'); |
| 82 |
|
| 83 |
FIcon := Element.AttributeStringDef('icon', ''); |
| 84 |
|
| 85 |
Extensions.Clear; |
| 86 |
ChildElements := Element.ChildrenIterator('extension'); |
| 87 |
try |
| 88 |
while ChildElements.GetNext do |
| 89 |
begin |
| 90 |
S := Trim(ChildElements.Current.TextData); |
| 91 |
if SCharIs(S, 1, '.') then |
| 92 |
raise Exception.CreateFmt('Extensions in <document_type> must not start with a dot: "%s"', [S]); |
| 93 |
Extensions.Add(S); |
| 94 |
end; |
| 95 |
finally FreeAndNil(ChildElements) end; |
| 96 |
if Extensions.Count = 0 then |
| 97 |
raise Exception.Create('<document_type> requires at least one <extension>'); |
| 98 |
|
| 99 |
MimeTypes.Clear; |
| 100 |
ChildElements := Element.ChildrenIterator('mime'); |
| 101 |
try |
| 102 |
while ChildElements.GetNext do |
| 103 |
begin |
| 104 |
S := Trim(ChildElements.Current.TextData); |
| 105 |
MimeTypes.Add(S); |
| 106 |
end; |
| 107 |
finally FreeAndNil(ChildElements) end; |
| 108 |
if MimeTypes.Count = 0 then |
| 109 |
raise Exception.Create('<document_type> requires at least one <mime>'); |
| 110 |
end; |
| 111 |
|
| 112 |
function TAssocDocType.ToPListBundleDocumentTypesSection(const ProjectQualifiedName, DefaultIcon: string): string; |
| 113 |
var |
| 114 |
FinalIcon: string; |
| 115 |
begin |
| 116 |
if FIcon <> '' then |
| 117 |
FinalIcon := FIcon |
| 118 |
else |
| 119 |
FinalIcon := DefaultIcon; |
| 120 |
|
| 121 |
Result := |
| 122 |
#9#9'<dict>' + NL + |
| 123 |
#9#9#9'<key>CFBundleTypeIconFiles</key>' + NL + |
| 124 |
#9#9#9'<array>' + NL + |
| 125 |
#9#9#9#9'<string>' + FinalIcon + '</string>' + NL + |
| 126 |
#9#9#9'</array>' + NL + |
| 127 |
#9#9#9'<key>CFBundleTypeName</key>' + NL + |
| 128 |
#9#9#9'<string>' + FCaption + '</string>' + NL + |
| 129 |
#9#9#9'<key>CFBundleTypeRole</key>' + NL + |
| 130 |
#9#9#9'<string>Viewer</string>' + NL + |
| 131 |
#9#9#9'<key>LSHandlerRank</key>' + NL + |
| 132 |
#9#9#9'<string>Owner</string>' + NL + |
| 133 |
#9#9#9'<key>LSItemContentTypes</key>' + NL + |
| 134 |
#9#9#9'<array>' + NL + |
| 135 |
#9#9#9#9'<string>' + ProjectQualifiedName + '.' + FName + '</string>' + NL + |
| 136 |
#9#9#9'</array>' + NL + |
| 137 |
#9#9'</dict>' + NL; |
| 138 |
end; |
| 139 |
|
| 140 |
function TAssocDocType.ToPListExportedTypeDeclarationsSection(const ProjectQualifiedName: string): string; |
| 141 |
var |
| 142 |
S: String; |
| 143 |
begin |
| 144 |
Result := |
| 145 |
#9#9'<dict>' + NL + |
| 146 |
#9#9#9'<key>UTTypeConformsTo</key>' + NL + |
| 147 |
#9#9#9'<array>' + NL + |
| 148 |
#9#9#9#9'<string>public.data</string>' + NL + |
| 149 |
#9#9#9'</array>' + NL + |
| 150 |
#9#9#9'<key>UTTypeDescription</key>' + NL + |
| 151 |
#9#9#9'<string>' + FCaption + '</string>' + NL + |
| 152 |
#9#9#9'<key>UTTypeIdentifier</key>' + NL + |
| 153 |
#9#9#9'<string>' + ProjectQualifiedName + '.' + FName + '</string>' + NL + |
| 154 |
#9#9#9'<key>UTTypeTagSpecification</key>' + NL + |
| 155 |
#9#9#9'<dict>' + NL + |
| 156 |
#9#9#9#9'<key>public.filename-extension</key>' + NL + |
| 157 |
#9#9#9#9'<array>' + NL; |
| 158 |
for S in Extensions do |
| 159 |
Result := Result + |
| 160 |
#9#9#9#9#9'<string>' + S + '</string>' + NL; |
| 161 |
Result := Result + |
| 162 |
#9#9#9#9'</array>' + NL + |
| 163 |
#9#9#9#9'<key>public.mime-type</key>' + NL + |
| 164 |
#9#9#9#9'<array>' + NL; |
| 165 |
for S in MimeTypes do |
| 166 |
Result := Result + |
| 167 |
#9#9#9#9#9'<string>' + S + '</string>' + NL; |
| 168 |
Result := Result + |
| 169 |
#9#9#9#9'</array>' + NL + |
| 170 |
#9#9#9'</dict>' + NL + |
| 171 |
#9#9'</dict>' + NL; |
| 172 |
end; |
| 173 |
|
| 174 |
function TAssocDocType.ToIntentFilterPathPattern: string; |
| 175 |
var |
| 176 |
Ext: String; |
| 177 |
begin |
| 178 |
Result := ''; |
| 179 |
for Ext in Extensions do |
| 180 |
begin |
| 181 |
Result := Result + |
| 182 |
#9#9#9#9'<data android:pathPattern=".*\\.' + Ext + '" />' + NL + |
| 183 |
#9#9#9#9'<data android:pathPattern=".*\\..*\\.' + Ext + '" />' + NL + // to match dot in filename |
| 184 |
#9#9#9#9'<data android:pathPattern=".*\\..*\\..*\\.' + Ext + '" />' + NL; |
| 185 |
end; |
| 186 |
end; |
| 187 |
|
| 188 |
{ TAssociatedDocTypeList ------------------------------------------------------ } |
| 189 |
|
| 190 |
procedure TAssociatedDocTypeList.ReadCastleEngineManifest(const Element: TDOMElement); |
| 191 |
var |
| 192 |
ChildElements: TXMLElementIterator; |
| 193 |
ChildElement: TDOMElement; |
| 194 |
DocType: TAssocDocType; |
| 195 |
begin |
| 196 |
ChildElements := Element.ChildrenIterator('document_type'); |
| 197 |
try |
| 198 |
while ChildElements.GetNext do |
| 199 |
begin |
| 200 |
ChildElement := ChildElements.Current; |
| 201 |
DocType := TAssocDocType.Create; |
| 202 |
Add(DocType); |
| 203 |
DocType.ReadCastleEngineManifest(ChildElement); |
| 204 |
end; |
| 205 |
finally FreeAndNil(ChildElements) end; |
| 206 |
end; |
| 207 |
|
| 208 |
function TAssociatedDocTypeList.ToPListSection(const ProjectQualifiedName, DefaultIcon: string): string; |
| 209 |
var |
| 210 |
BundleDocumentTypes, ExportedTypeDeclarations: string; |
| 211 |
DocType: TAssocDocType; |
| 212 |
begin |
| 213 |
BundleDocumentTypes := ''; |
| 214 |
ExportedTypeDeclarations := ''; |
| 215 |
for DocType in Self do |
| 216 |
begin |
| 217 |
BundleDocumentTypes := BundleDocumentTypes + |
| 218 |
DocType.ToPListBundleDocumentTypesSection(ProjectQualifiedName, DefaultIcon); |
| 219 |
ExportedTypeDeclarations := ExportedTypeDeclarations + |
| 220 |
DocType.ToPListExportedTypeDeclarationsSection(ProjectQualifiedName); |
| 221 |
end; |
| 222 |
|
| 223 |
Result := |
| 224 |
#9'<key>CFBundleDocumentTypes</key>' + NL + |
| 225 |
#9'<array>' + NL + |
| 226 |
#9#9'<dict>' + NL + |
| 227 |
#9#9#9'<key>CFBundleTypeRole</key>' + NL + |
| 228 |
#9#9#9'<string>Viewer</string>' + NL + |
| 229 |
#9#9#9'<key>CFBundleTypeExtensions</key>' + NL + |
| 230 |
#9#9#9'<array>' + NL + |
| 231 |
#9#9#9#9'<string>*</string>' + NL + |
| 232 |
#9#9#9'</array>' + NL + |
| 233 |
#9#9#9'<key>CFBundleTypeOSTypes</key>' + NL + |
| 234 |
#9#9#9'<array>' + NL + |
| 235 |
#9#9#9#9'<string>fold</string>' + NL + |
| 236 |
#9#9#9#9'<string>disk</string>' + NL + |
| 237 |
#9#9#9#9'<string>****</string>' + NL + |
| 238 |
#9#9#9'</array>' + NL + |
| 239 |
#9#9'</dict>' + NL + |
| 240 |
BundleDocumentTypes + |
| 241 |
#9'</array>' + NL; |
| 242 |
|
| 243 |
if ExportedTypeDeclarations <> '' then |
| 244 |
Result := Result + |
| 245 |
#9'<key>UTExportedTypeDeclarations</key>' + NL + |
| 246 |
#9'<array>' + NL + |
| 247 |
ExportedTypeDeclarations + |
| 248 |
#9'</array>'; |
| 249 |
end; |
| 250 |
|
| 251 |
// https://stackoverflow.com/questions/3760276/android-intent-filter-associate-app-with-file-extension |
| 252 |
function TAssociatedDocTypeList.ToIntentFilter: string; |
| 253 |
var |
| 254 |
PathPatterns: string; |
| 255 |
DocType: TAssocDocType; |
| 256 |
begin |
| 257 |
if Count = 0 then |
| 258 |
Exit(''); |
| 259 |
|
| 260 |
PathPatterns := ''; |
| 261 |
for DocType in Self do |
| 262 |
begin |
| 263 |
PathPatterns := PathPatterns + DocType.ToIntentFilterPathPattern; |
| 264 |
end; |
| 265 |
|
| 266 |
Result := |
| 267 |
#9#9#9'<intent-filter>' + NL + |
| 268 |
#9#9#9#9'<action android:name="android.intent.action.VIEW" />' + NL + |
| 269 |
#9#9#9#9'<category android:name="android.intent.category.DEFAULT" />' + NL + |
| 270 |
#9#9#9#9'<category android:name="android.intent.category.BROWSABLE" />' + NL + |
| 271 |
#9#9#9#9'<data android:scheme="content" />' + NL + |
| 272 |
#9#9#9#9'<data android:scheme="file" />' + NL + |
| 273 |
#9#9#9#9'<data android:mimeType="*/*" />' + NL + |
| 274 |
PathPatterns + |
| 275 |
#9#9#9'</intent-filter>' + NL + |
| 276 |
#9#9#9'<intent-filter>' + NL + |
| 277 |
#9#9#9#9'<action android:name="android.intent.action.VIEW" />' + NL + |
| 278 |
#9#9#9#9'<category android:name="android.intent.category.DEFAULT" />' + NL + |
| 279 |
#9#9#9#9'<category android:name="android.intent.category.BROWSABLE" />' + NL + |
| 280 |
#9#9#9#9'<data android:scheme="http" />' + NL + |
| 281 |
#9#9#9#9'<data android:scheme="https" />' + NL + |
| 282 |
#9#9#9#9'<data android:scheme="ftp" />' + NL + |
| 283 |
#9#9#9#9'<data android:host="*" />' + NL + |
| 284 |
PathPatterns + |
| 285 |
#9#9#9'</intent-filter>'; |
| 286 |
end; |
| 287 |
|
| 288 |
end. |