/src/mozilla-central/uriloader/exthandler/unix/nsGNOMERegistry.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "nsGNOMERegistry.h" |
7 | | #include "nsString.h" |
8 | | #include "nsIComponentManager.h" |
9 | | #include "nsMIMEInfoUnix.h" |
10 | | #include "nsAutoPtr.h" |
11 | | #include "nsIGIOService.h" |
12 | | |
13 | | /* static */ bool |
14 | | nsGNOMERegistry::HandlerExists(const char *aProtocolScheme) |
15 | 0 | { |
16 | 0 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
17 | 0 | if (!giovfs) { |
18 | 0 | return false; |
19 | 0 | } |
20 | 0 | |
21 | 0 | nsCOMPtr<nsIHandlerApp> app; |
22 | 0 | return NS_SUCCEEDED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme), |
23 | 0 | getter_AddRefs(app))); |
24 | 0 | } |
25 | | |
26 | | // XXX Check HandlerExists() before calling LoadURL. |
27 | | |
28 | | /* static */ nsresult |
29 | | nsGNOMERegistry::LoadURL(nsIURI *aURL) |
30 | 0 | { |
31 | 0 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
32 | 0 | if (!giovfs) { |
33 | 0 | return NS_ERROR_FAILURE; |
34 | 0 | } |
35 | 0 | |
36 | 0 | return giovfs->ShowURI(aURL); |
37 | 0 | } |
38 | | |
39 | | /* static */ void |
40 | | nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme, |
41 | | nsAString& aDesc) |
42 | 0 | { |
43 | 0 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
44 | 0 | if (!giovfs) |
45 | 0 | return; |
46 | 0 | |
47 | 0 | nsCOMPtr<nsIHandlerApp> app; |
48 | 0 | if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app)))) |
49 | 0 | return; |
50 | 0 | |
51 | 0 | app->GetName(aDesc); |
52 | 0 | } |
53 | | |
54 | | |
55 | | /* static */ already_AddRefed<nsMIMEInfoBase> |
56 | | nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt) |
57 | 0 | { |
58 | 0 | nsAutoCString mimeType; |
59 | 0 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
60 | 0 | if (!giovfs) { |
61 | 0 | return nullptr; |
62 | 0 | } |
63 | 0 | |
64 | 0 | // Get the MIME type from the extension, then call GetFromType to |
65 | 0 | // fill in the MIMEInfo. |
66 | 0 | if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) || |
67 | 0 | mimeType.EqualsLiteral("application/octet-stream")) { |
68 | 0 | return nullptr; |
69 | 0 | } |
70 | 0 | |
71 | 0 | RefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType); |
72 | 0 | if (mi) { |
73 | 0 | mi->AppendExtension(aFileExt); |
74 | 0 | } |
75 | 0 |
|
76 | 0 | return mi.forget(); |
77 | 0 | } |
78 | | |
79 | | /* static */ already_AddRefed<nsMIMEInfoBase> |
80 | | nsGNOMERegistry::GetFromType(const nsACString& aMIMEType) |
81 | 0 | { |
82 | 0 | RefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType); |
83 | 0 | NS_ENSURE_TRUE(mimeInfo, nullptr); |
84 | 0 |
|
85 | 0 | nsAutoString name; |
86 | 0 | nsAutoCString description; |
87 | 0 |
|
88 | 0 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
89 | 0 | if (!giovfs) { |
90 | 0 | return nullptr; |
91 | 0 | } |
92 | 0 | |
93 | 0 | nsCOMPtr<nsIHandlerApp> handlerApp; |
94 | 0 | if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(handlerApp))) || |
95 | 0 | !handlerApp) { |
96 | 0 | return nullptr; |
97 | 0 | } |
98 | 0 | handlerApp->GetName(name); |
99 | 0 | giovfs->GetDescriptionForMimeType(aMIMEType, description); |
100 | 0 |
|
101 | 0 | mimeInfo->SetDefaultDescription(name); |
102 | 0 | mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault); |
103 | 0 | mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description)); |
104 | 0 |
|
105 | 0 | return mimeInfo.forget(); |
106 | 0 | } |