Line data Source code
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 0 : nsGNOMERegistry::HandlerExists(const char *aProtocolScheme)
15 : {
16 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
17 0 : if (!giovfs) {
18 0 : return false;
19 : }
20 :
21 0 : nsCOMPtr<nsIGIOMimeApp> app;
22 0 : return NS_SUCCEEDED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme),
23 : getter_AddRefs(app)));
24 : }
25 :
26 : // XXX Check HandlerExists() before calling LoadURL.
27 :
28 : /* static */ nsresult
29 0 : nsGNOMERegistry::LoadURL(nsIURI *aURL)
30 : {
31 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
32 0 : if (!giovfs) {
33 0 : return NS_ERROR_FAILURE;
34 : }
35 :
36 0 : return giovfs->ShowURI(aURL);
37 : }
38 :
39 : /* static */ void
40 0 : nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme,
41 : nsAString& aDesc)
42 : {
43 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
44 0 : if (!giovfs)
45 0 : return;
46 :
47 0 : nsAutoCString name;
48 0 : nsCOMPtr<nsIGIOMimeApp> app;
49 0 : if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app))))
50 0 : return;
51 :
52 0 : app->GetName(name);
53 :
54 0 : CopyUTF8toUTF16(name, aDesc);
55 : }
56 :
57 :
58 : /* static */ already_AddRefed<nsMIMEInfoBase>
59 0 : nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt)
60 : {
61 0 : nsAutoCString mimeType;
62 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
63 0 : if (!giovfs) {
64 0 : return nullptr;
65 : }
66 :
67 : // Get the MIME type from the extension, then call GetFromType to
68 : // fill in the MIMEInfo.
69 0 : if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
70 0 : mimeType.EqualsLiteral("application/octet-stream")) {
71 0 : return nullptr;
72 : }
73 :
74 0 : RefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType);
75 0 : if (mi) {
76 0 : mi->AppendExtension(aFileExt);
77 : }
78 :
79 0 : return mi.forget();
80 : }
81 :
82 : /* static */ already_AddRefed<nsMIMEInfoBase>
83 4 : nsGNOMERegistry::GetFromType(const nsACString& aMIMEType)
84 : {
85 8 : RefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType);
86 4 : NS_ENSURE_TRUE(mimeInfo, nullptr);
87 :
88 8 : nsAutoCString name;
89 8 : nsAutoCString description;
90 :
91 8 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
92 4 : if (!giovfs) {
93 0 : return nullptr;
94 : }
95 :
96 8 : nsCOMPtr<nsIGIOMimeApp> gioHandlerApp;
97 8 : if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) ||
98 4 : !gioHandlerApp) {
99 0 : return nullptr;
100 : }
101 4 : gioHandlerApp->GetName(name);
102 4 : giovfs->GetDescriptionForMimeType(aMIMEType, description);
103 :
104 4 : mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name));
105 4 : mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
106 4 : mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description));
107 :
108 4 : return mimeInfo.forget();
109 : }
|