|
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 "nsGnomeVFSService.h" |
|
7 #include "nsStringAPI.h" |
|
8 #include "nsIURI.h" |
|
9 #include "nsTArray.h" |
|
10 #include "nsIStringEnumerator.h" |
|
11 #include "nsAutoPtr.h" |
|
12 |
|
13 extern "C" { |
|
14 #include <libgnomevfs/gnome-vfs.h> |
|
15 #include <libgnomevfs/gnome-vfs-mime.h> |
|
16 #include <libgnomevfs/gnome-vfs-mime-handlers.h> |
|
17 } |
|
18 |
|
19 class nsGnomeVFSMimeApp MOZ_FINAL : public nsIGnomeVFSMimeApp |
|
20 { |
|
21 public: |
|
22 NS_DECL_ISUPPORTS |
|
23 NS_DECL_NSIGNOMEVFSMIMEAPP |
|
24 |
|
25 nsGnomeVFSMimeApp(GnomeVFSMimeApplication* aApp) : mApp(aApp) {} |
|
26 ~nsGnomeVFSMimeApp() { gnome_vfs_mime_application_free(mApp); } |
|
27 |
|
28 private: |
|
29 GnomeVFSMimeApplication *mApp; |
|
30 }; |
|
31 |
|
32 NS_IMPL_ISUPPORTS(nsGnomeVFSMimeApp, nsIGnomeVFSMimeApp) |
|
33 |
|
34 NS_IMETHODIMP |
|
35 nsGnomeVFSMimeApp::GetId(nsACString& aId) |
|
36 { |
|
37 aId.Assign(mApp->id); |
|
38 return NS_OK; |
|
39 } |
|
40 |
|
41 NS_IMETHODIMP |
|
42 nsGnomeVFSMimeApp::GetName(nsACString& aName) |
|
43 { |
|
44 aName.Assign(mApp->name); |
|
45 return NS_OK; |
|
46 } |
|
47 |
|
48 NS_IMETHODIMP |
|
49 nsGnomeVFSMimeApp::GetCommand(nsACString& aCommand) |
|
50 { |
|
51 aCommand.Assign(mApp->command); |
|
52 return NS_OK; |
|
53 } |
|
54 |
|
55 NS_IMETHODIMP |
|
56 nsGnomeVFSMimeApp::GetCanOpenMultipleFiles(bool* aCanOpen) |
|
57 { |
|
58 *aCanOpen = mApp->can_open_multiple_files; |
|
59 return NS_OK; |
|
60 } |
|
61 |
|
62 NS_IMETHODIMP |
|
63 nsGnomeVFSMimeApp::GetExpectsURIs(int32_t* aExpects) |
|
64 { |
|
65 *aExpects = mApp->expects_uris; |
|
66 return NS_OK; |
|
67 } |
|
68 |
|
69 NS_IMETHODIMP |
|
70 nsGnomeVFSMimeApp::Launch(const nsACString &aUri) |
|
71 { |
|
72 char *uri = gnome_vfs_make_uri_from_input(PromiseFlatCString(aUri).get()); |
|
73 |
|
74 if (! uri) |
|
75 return NS_ERROR_FAILURE; |
|
76 |
|
77 GList uris = { 0 }; |
|
78 uris.data = uri; |
|
79 |
|
80 GnomeVFSResult result = gnome_vfs_mime_application_launch(mApp, &uris); |
|
81 g_free(uri); |
|
82 |
|
83 if (result != GNOME_VFS_OK) |
|
84 return NS_ERROR_FAILURE; |
|
85 |
|
86 return NS_OK; |
|
87 } |
|
88 |
|
89 class UTF8StringEnumerator MOZ_FINAL : public nsIUTF8StringEnumerator |
|
90 { |
|
91 public: |
|
92 UTF8StringEnumerator() : mIndex(0) { } |
|
93 ~UTF8StringEnumerator() { } |
|
94 |
|
95 NS_DECL_ISUPPORTS |
|
96 NS_DECL_NSIUTF8STRINGENUMERATOR |
|
97 |
|
98 nsTArray<nsCString> mStrings; |
|
99 uint32_t mIndex; |
|
100 }; |
|
101 |
|
102 NS_IMPL_ISUPPORTS(UTF8StringEnumerator, nsIUTF8StringEnumerator) |
|
103 |
|
104 NS_IMETHODIMP |
|
105 UTF8StringEnumerator::HasMore(bool *aResult) |
|
106 { |
|
107 *aResult = mIndex < mStrings.Length(); |
|
108 return NS_OK; |
|
109 } |
|
110 |
|
111 NS_IMETHODIMP |
|
112 UTF8StringEnumerator::GetNext(nsACString& aResult) |
|
113 { |
|
114 if (mIndex >= mStrings.Length()) |
|
115 return NS_ERROR_UNEXPECTED; |
|
116 |
|
117 aResult.Assign(mStrings[mIndex]); |
|
118 ++mIndex; |
|
119 return NS_OK; |
|
120 } |
|
121 |
|
122 NS_IMETHODIMP |
|
123 nsGnomeVFSMimeApp::GetSupportedURISchemes(nsIUTF8StringEnumerator** aSchemes) |
|
124 { |
|
125 *aSchemes = nullptr; |
|
126 |
|
127 nsRefPtr<UTF8StringEnumerator> array = new UTF8StringEnumerator(); |
|
128 NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY); |
|
129 |
|
130 for (GList *list = mApp->supported_uri_schemes; list; list = list->next) { |
|
131 if (!array->mStrings.AppendElement((char*) list->data)) { |
|
132 return NS_ERROR_OUT_OF_MEMORY; |
|
133 } |
|
134 } |
|
135 |
|
136 NS_ADDREF(*aSchemes = array); |
|
137 return NS_OK; |
|
138 } |
|
139 |
|
140 NS_IMETHODIMP |
|
141 nsGnomeVFSMimeApp::GetRequiresTerminal(bool* aRequires) |
|
142 { |
|
143 *aRequires = mApp->requires_terminal; |
|
144 return NS_OK; |
|
145 } |
|
146 |
|
147 nsresult |
|
148 nsGnomeVFSService::Init() |
|
149 { |
|
150 return gnome_vfs_init() ? NS_OK : NS_ERROR_FAILURE; |
|
151 } |
|
152 |
|
153 NS_IMPL_ISUPPORTS(nsGnomeVFSService, nsIGnomeVFSService) |
|
154 |
|
155 NS_IMETHODIMP |
|
156 nsGnomeVFSService::GetMimeTypeFromExtension(const nsACString &aExtension, |
|
157 nsACString& aMimeType) |
|
158 { |
|
159 nsAutoCString fileExtToUse("."); |
|
160 fileExtToUse.Append(aExtension); |
|
161 |
|
162 const char *mimeType = gnome_vfs_mime_type_from_name(fileExtToUse.get()); |
|
163 aMimeType.Assign(mimeType); |
|
164 |
|
165 // |mimeType| points to internal gnome-vfs data, so don't free it. |
|
166 |
|
167 return NS_OK; |
|
168 } |
|
169 |
|
170 NS_IMETHODIMP |
|
171 nsGnomeVFSService::GetAppForMimeType(const nsACString &aMimeType, |
|
172 nsIGnomeVFSMimeApp** aApp) |
|
173 { |
|
174 *aApp = nullptr; |
|
175 GnomeVFSMimeApplication *app = |
|
176 gnome_vfs_mime_get_default_application(PromiseFlatCString(aMimeType).get()); |
|
177 |
|
178 if (app) { |
|
179 nsGnomeVFSMimeApp *mozApp = new nsGnomeVFSMimeApp(app); |
|
180 NS_ENSURE_TRUE(mozApp, NS_ERROR_OUT_OF_MEMORY); |
|
181 |
|
182 NS_ADDREF(*aApp = mozApp); |
|
183 } |
|
184 |
|
185 return NS_OK; |
|
186 } |
|
187 |
|
188 NS_IMETHODIMP |
|
189 nsGnomeVFSService::GetDescriptionForMimeType(const nsACString &aMimeType, |
|
190 nsACString& aDescription) |
|
191 { |
|
192 const char *desc = |
|
193 gnome_vfs_mime_get_description(PromiseFlatCString(aMimeType).get()); |
|
194 aDescription.Assign(desc); |
|
195 |
|
196 // |desc| points to internal gnome-vfs data, so don't free it. |
|
197 |
|
198 return NS_OK; |
|
199 } |
|
200 |
|
201 NS_IMETHODIMP |
|
202 nsGnomeVFSService::ShowURI(nsIURI *aURI) |
|
203 { |
|
204 nsAutoCString spec; |
|
205 aURI->GetSpec(spec); |
|
206 |
|
207 if (gnome_vfs_url_show_with_env(spec.get(), nullptr) == GNOME_VFS_OK) |
|
208 return NS_OK; |
|
209 |
|
210 return NS_ERROR_FAILURE; |
|
211 } |
|
212 |
|
213 NS_IMETHODIMP |
|
214 nsGnomeVFSService::ShowURIForInput(const nsACString &aUri) |
|
215 { |
|
216 char* spec = gnome_vfs_make_uri_from_input(PromiseFlatCString(aUri).get()); |
|
217 nsresult rv = NS_ERROR_FAILURE; |
|
218 |
|
219 if (gnome_vfs_url_show_with_env(spec, nullptr) == GNOME_VFS_OK) |
|
220 rv = NS_OK; |
|
221 |
|
222 g_free(spec); |
|
223 return rv; |
|
224 } |