29 #include <nsAutoPtr.h>
30 #include <nsIFileURL.h>
32 #include <nsServiceManagerUtils.h>
33 #include <nsNetUtil.h>
41 NS_INTERFACE_TABLE_BEGIN
44 NS_INTERFACE_TABLE_END
47 sbURIPropertyInfo::sbURIPropertyInfo()
48 : mURISchemeConstraintLock(nsnull)
49 , mIOServiceLock(nsnull)
51 mType = NS_LITERAL_STRING(
"uri");
53 mURISchemeConstraintLock = PR_NewLock();
54 NS_ASSERTION(mURISchemeConstraintLock,
55 "sbURIPropertyInfo::mURISchemeConstraintLock failed to create lock!");
57 mIOServiceLock = PR_NewLock();
58 NS_ASSERTION(mIOServiceLock,
59 "sbURIPropertyInfo::mIOServiceLock failed to create lock!");
64 if(mURISchemeConstraintLock) {
65 PR_DestroyLock(mURISchemeConstraintLock);
75 NS_ENSURE_SUCCESS(rv, rv);
77 rv = InitializeOperators();
78 NS_ENSURE_SUCCESS(rv, rv);
84 sbURIPropertyInfo::InitializeOperators()
88 nsRefPtr<sbPropertyOperator> propOp;
90 rv = sbPropertyInfo::GetOPERATOR_CONTAINS(op);
91 NS_ENSURE_SUCCESS(rv, rv);
93 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
95 NS_ENSURE_SUCCESS(rv, rv);
97 rv = sbPropertyInfo::GetOPERATOR_NOTCONTAINS(op);
98 NS_ENSURE_SUCCESS(rv, rv);
100 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
102 NS_ENSURE_SUCCESS(rv, rv);
104 rv = sbPropertyInfo::GetOPERATOR_EQUALS(op);
105 NS_ENSURE_SUCCESS(rv, rv);
107 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
109 NS_ENSURE_SUCCESS(rv, rv);
111 rv = sbPropertyInfo::GetOPERATOR_NOTEQUALS(op);
112 NS_ENSURE_SUCCESS(rv, rv);
114 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
116 NS_ENSURE_SUCCESS(rv, rv);
118 rv = sbPropertyInfo::GetOPERATOR_BEGINSWITH(op);
119 NS_ENSURE_SUCCESS(rv, rv);
121 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
123 NS_ENSURE_SUCCESS(rv, rv);
125 rv = sbPropertyInfo::GetOPERATOR_NOTBEGINSWITH(op);
126 NS_ENSURE_SUCCESS(rv, rv);
128 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
130 NS_ENSURE_SUCCESS(rv, rv);
132 rv = sbPropertyInfo::GetOPERATOR_ENDSWITH(op);
133 NS_ENSURE_SUCCESS(rv, rv);
135 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
137 NS_ENSURE_SUCCESS(rv, rv);
139 rv = sbPropertyInfo::GetOPERATOR_NOTENDSWITH(op);
140 NS_ENSURE_SUCCESS(rv, rv);
142 NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
144 NS_ENSURE_SUCCESS(rv, rv);
151 NS_ENSURE_ARG_POINTER(_retval);
155 NS_ENSURE_SUCCESS(rv, rv);
157 nsCOMPtr<nsIURI>
uri;
158 rv = NS_NewURI(getter_AddRefs(uri), aValue, nsnull, nsnull, mIOService);
165 if(!mURISchemeConstraint.IsEmpty()) {
166 NS_ConvertUTF16toUTF8 narrow(mURISchemeConstraint);
167 PRBool valid = PR_FALSE;
169 rv = uri->SchemeIs(narrow.get(), &valid);
170 NS_ENSURE_SUCCESS(rv, rv);
182 return NS_ERROR_NOT_IMPLEMENTED;
187 if (aValue.IsEmpty())
190 PRUint32
pos = aValue.FindChar(
':');
193 if (pos == aValue.Length()-1 ||
194 (pos == aValue.Length()-2 && aValue.CharAt(pos + 1) ==
'/') ||
195 (pos == aValue.Length()-3 && aValue.CharAt(pos + 1) ==
'/' && aValue.CharAt(pos + 2) ==
'/')) {
203 if (aValue.IsVoid()) {
209 nsCString escapedSpec;
213 NS_ENSURE_SUCCESS(rv, rv);
215 nsCOMPtr<nsIURI>
uri;
216 rv = NS_NewURI(getter_AddRefs(uri), aValue, nsnull, nsnull, mIOService);
217 NS_ENSURE_SUCCESS(rv, rv);
222 if (!mURISchemeConstraint.IsEmpty()) {
223 NS_ConvertUTF16toUTF8 narrow(mURISchemeConstraint);
224 PRBool valid = PR_FALSE;
226 rv = uri->SchemeIs(narrow.get(), &valid);
227 NS_ENSURE_SUCCESS(rv, rv);
230 return NS_ERROR_FAILURE;
235 nsCOMPtr<nsIFileURL>
url = do_QueryInterface(uri, &rv);
236 if (NS_SUCCEEDED(rv)) {
238 nsCOMPtr<nsIFile>
file;
239 rv = url->GetFile(getter_AddRefs(file));
240 NS_ENSURE_SUCCESS(rv, rv);
243 rv = file->GetPath(path);
244 NS_ENSURE_SUCCESS(rv, rv);
249 rv = uri->GetSpec(escapedSpec);
250 NS_ENSURE_SUCCESS(rv, rv);
253 CopyUTF16toUTF8(aValue, escapedSpec);
256 nsCOMPtr<nsINetUtil> netUtil =
257 do_GetService(
"@mozilla.org/network/util;1", &rv);
258 NS_ENSURE_SUCCESS(rv, rv);
261 nsCString unescapedSpec;
262 rv = netUtil->UnescapeString(escapedSpec,
263 nsINetUtil::ESCAPE_ALL,
265 NS_ENSURE_SUCCESS(rv, rv);
271 char buffer[] = {
'%',
'E',
'2',
'%',
'8',
'0',
'%',
'x',
'x'};
272 while ((offset = unescapedSpec.Find(NS_LITERAL_CSTRING(
"\xE2\x80"), offset)) != -1) {
273 switch ((PRUint8)unescapedSpec[offset + 2]) {
277 buffer[8] = unescapedSpec[offset + 2] - 0x48;
285 buffer[8] = unescapedSpec[offset + 2] - 0x5F;
291 unescapedSpec.Replace(offset, 3, buffer,
sizeof(buffer));
294 CopyUTF8toUTF16(unescapedSpec, _retval);
301 _retval = EmptyString();
314 NS_ENSURE_SUCCESS(rv, rv);
316 nsCOMPtr<nsIURI>
uri;
317 rv = NS_NewURI(getter_AddRefs(uri), aValue, nsnull, nsnull, mIOService);
318 NS_ENSURE_SUCCESS(rv, rv);
322 rv = uri->GetSpec(spec);
323 NS_ENSURE_SUCCESS(rv, rv);
325 nsCOMPtr<nsILocalFile> localFile =
326 do_CreateInstance(
"@mozilla.org/file/local;1", &rv);
327 NS_ENSURE_SUCCESS(rv, rv);
328 rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(spec));
329 if (NS_SUCCEEDED(rv)) {
333 nsCOMPtr<nsIFile>
file = do_QueryInterface(localFile, &rv);
334 NS_ENSURE_SUCCESS(rv, rv);
335 nsCOMPtr<nsIURI> fileUri;
336 rv = mIOService->NewFileURI(file, getter_AddRefs(fileUri));
337 NS_ENSURE_SUCCESS(rv, rv);
339 nsCAutoString escapedSpec;
340 rv = fileUri->GetSpec(escapedSpec);
341 NS_ENSURE_SUCCESS(rv, rv);
344 nsCOMPtr<nsINetUtil> netUtil =
345 do_GetService(
"@mozilla.org/network/util;1", &rv);
346 NS_ENSURE_SUCCESS(rv, rv);
347 rv = netUtil->UnescapeString(escapedSpec,
348 nsINetUtil::ESCAPE_ALL,
350 NS_ENSURE_SUCCESS(rv, rv);
353 NS_ConvertUTF8toUTF16 wide(spec);
362 NS_IMETHODIMP sbURIPropertyInfo::GetConstrainScheme(nsAString & aConstrainScheme)
365 aConstrainScheme = mURISchemeConstraint;
368 NS_IMETHODIMP sbURIPropertyInfo::SetConstrainScheme(
const nsAString & aConstrainScheme)
372 if(mURISchemeConstraint.IsEmpty()) {
373 mURISchemeConstraint = aConstrainScheme;
377 return NS_ERROR_ALREADY_INITIALIZED;
385 mIOService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
NS_IMETHOD Validate(const nsAString &aValue, PRBool *_retval)
The property information specific to URL/URI fields.
virtual ~sbURIPropertyInfo()
PRBool IsInvalidEmpty(const nsAString &aValue)
NS_IMETHOD Sanitize(const nsAString &aValue, nsAString &_retval)
NS_IMPL_ADDREF_INHERITED(sbURIPropertyInfo, sbPropertyInfo)
An interface used to describe a metadata property for use by the UI and other sbILibrary interfaces (...
NS_IMETHOD Format(const nsAString &aValue, nsAString &_retval)
NS_IMETHOD EnsureIOService()
NS_IMETHOD MakeSortable(const nsAString &aValue, nsAString &_retval)
NS_IMPL_RELEASE_INHERITED(sbURIPropertyInfo, sbPropertyInfo)
nsCOMArray< sbIPropertyOperator > mOperators
NS_IMETHOD MakeSearchable(const nsAString &aValue, nsAString &_retval)