Menu

[r107]: / trunk / Src / UFileProtocol.pas  Maximize  Restore  History

Download this file

179 lines (144 with data), 6.0 kB

  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
{
* UFileProtocol.pas
*
* Implements a handler for the "file" URL protocol that displays a local file
* in the associated program.
*
* v1.0 of 30 Jun 2009 - Original version.
*
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UFileProtocol.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009 Peter
* Johnson. All Rights Reserved.
*
* ***** END LICENSE BLOCK *****
}
unit UFileProtocol;
interface
implementation
uses
// Delphi
SysUtils, StrUtils, ExtActns,
// Project
UBrowseProtocol, UProtocols, UUtils;
{
The file: protocol.
The file protocol is of the form file://<hostname>/<path>. <hostname> can be
omitted giving file:///<path> which is equivalent to file://localhost/<path>.
<path> must be absolute.
See http://equinox-project.org/spec/file-uri-spec.txt.
The IE web browser control strips the file:// component of the protocol and
converts the specified file into the OS file name. This means that by the time
TFileProtocol receives the URL (via a TWebBrowser event handler), the URL has
been converted into the actual OS file name.
IE also supports direct specification of the file name, without file://
prefix. UNC file names are also supported, with or without the file:// prefix.
According to Microsoft, http://msdn.microsoft.com/en-us/library/aa767731.aspx,
valid file paths understood by TWebBrowser are:
+ file:///C|/Dirs/FileName.ext (browses file name)
+ file:///C|/Dirs/ (browses directory)
+ C:\Dirs\ (browses directory)
+ \Dirs\ (directory on local primary drive)
Experiment shows that "|" can be replaced by ":".
Additionally UNC file names such as the following are valid and equivalent:
+ file://///servername/sharename/filename
+ file:///\\servername\sharename/filename
+ file://servername/sharename/filename
\\servername\sharename\filename
By the time TFileProtocol sees any of this it is in the form or either
+ C:\path\filename
or
+ \\servername\sharename\filename
So TFileProtocol checks to see if a URL is a file: protocol by checking the
URL to see if it conforms with one of the above.
NOTE TFileProtocol does not support the local primary drive format or
directories. The URL must specify a file and it must exist.
}
type
{
TFileProtocol:
Implements a handler for the file: protocol that causes a file to be
displayed by its associated program.
}
TFileProtocol = class sealed(TBrowseProtocol)
public
class function SupportsProtocol(const URL: string): Boolean; override;
{Checks if a URL uses the file: protocol.
@param URL [in] URL whose protocol is to be checked.
@return True if URL's protocol is file:, False if not.
}
function Execute: Boolean; override;
{Executes referenced file using shell to display URL in default browser.
@return True.
@except raises exception if the file doesn't exist or is a directory.
}
end;
{ TFileProtocol }
function TFileProtocol.Execute: Boolean;
{Executes referenced file using shell to display URL in default browser.
@return True.
@except raises exception if the file doesn't exist or is a directory.
}
resourcestring
// error message used if file does not exist
sBadFile = 'File "%s" does not exist';
sIsDir = '"%s" is a directory. File expected';
begin
// Perform checks: URL must not be a directory and must be absolute path
if IsDirectory(URL) then
raise EProtocol.CreateFmt(sIsDir, [URL]);
if not FileExists(URL) then
raise EProtocol.CreateFmt(sBadFile, [URL]);
// We execute the resource using an action
Result := inherited Execute;
end;
class function TFileProtocol.SupportsProtocol(const URL: string): Boolean;
{Checks if a URL uses the file: protocol.
@param URL [in] URL whose protocol is to be checked.
@return True if URL's protocol is file:, False if not.
}
// ---------------------------------------------------------------------------
function IsValidAbsoluteFileName(const FileName: string): Boolean;
{Checks if a filename is a valid, complete, absolute local file path.
@param FileName [in] File name to be checked.
@return True if file name is valid absolute file path, false if not.
}
begin
Result := (Length(FileName) > 3) and
(UpCase(FileName[1]) in ['A'..'Z']) and
(FileName[2] = ':') and (FileName[3] = '\');
end;
function IsValidUNCFileName(const FileName: string): Boolean;
{Checks if a filename is a valid, complete, UNC file name.
@param FileName [in] File name to be checked.
@return True if file name is valid UNC name, false if not.
}
begin
Result := (Length(FileName) > 5) and
AnsiStartsStr('\\', FileName) and
(PosEx('\', FileName, 4) >= 4);
end;
// ---------------------------------------------------------------------------
begin
Result := IsValidAbsoluteFileName(URL) or IsValidUNCFileName(URL);
end;
initialization
// Register the protocol with the protocol factory
TProtocolFactory.RegisterProtocol(TFileProtocol);
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.