1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla GNOME integration code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * IBM Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2004
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Brian Ryner <bryner@brianryner.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsGnomeVFSService.h"
40 : #include "nsStringAPI.h"
41 : #include "nsIURI.h"
42 : #include "nsTArray.h"
43 : #include "nsIStringEnumerator.h"
44 : #include "nsAutoPtr.h"
45 :
46 : extern "C" {
47 : #include <libgnomevfs/gnome-vfs.h>
48 : #include <libgnomevfs/gnome-vfs-mime.h>
49 : #include <libgnomevfs/gnome-vfs-mime-handlers.h>
50 : }
51 :
52 : class nsGnomeVFSMimeApp : public nsIGnomeVFSMimeApp
53 : {
54 : public:
55 : NS_DECL_ISUPPORTS
56 : NS_DECL_NSIGNOMEVFSMIMEAPP
57 :
58 42 : nsGnomeVFSMimeApp(GnomeVFSMimeApplication* aApp) : mApp(aApp) {}
59 42 : ~nsGnomeVFSMimeApp() { gnome_vfs_mime_application_free(mApp); }
60 :
61 : private:
62 : GnomeVFSMimeApplication *mApp;
63 : };
64 :
65 252 : NS_IMPL_ISUPPORTS1(nsGnomeVFSMimeApp, nsIGnomeVFSMimeApp)
66 :
67 : NS_IMETHODIMP
68 0 : nsGnomeVFSMimeApp::GetId(nsACString& aId)
69 : {
70 0 : aId.Assign(mApp->id);
71 0 : return NS_OK;
72 : }
73 :
74 : NS_IMETHODIMP
75 42 : nsGnomeVFSMimeApp::GetName(nsACString& aName)
76 : {
77 42 : aName.Assign(mApp->name);
78 42 : return NS_OK;
79 : }
80 :
81 : NS_IMETHODIMP
82 0 : nsGnomeVFSMimeApp::GetCommand(nsACString& aCommand)
83 : {
84 0 : aCommand.Assign(mApp->command);
85 0 : return NS_OK;
86 : }
87 :
88 : NS_IMETHODIMP
89 0 : nsGnomeVFSMimeApp::GetCanOpenMultipleFiles(bool* aCanOpen)
90 : {
91 0 : *aCanOpen = mApp->can_open_multiple_files;
92 0 : return NS_OK;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : nsGnomeVFSMimeApp::GetExpectsURIs(PRInt32* aExpects)
97 : {
98 0 : *aExpects = mApp->expects_uris;
99 0 : return NS_OK;
100 : }
101 :
102 : NS_IMETHODIMP
103 0 : nsGnomeVFSMimeApp::Launch(const nsACString &aUri)
104 : {
105 0 : char *uri = gnome_vfs_make_uri_from_input(PromiseFlatCString(aUri).get());
106 :
107 0 : if (! uri)
108 0 : return NS_ERROR_FAILURE;
109 :
110 0 : GList uris = { 0 };
111 0 : uris.data = uri;
112 :
113 0 : GnomeVFSResult result = gnome_vfs_mime_application_launch(mApp, &uris);
114 0 : g_free(uri);
115 :
116 0 : if (result != GNOME_VFS_OK)
117 0 : return NS_ERROR_FAILURE;
118 :
119 0 : return NS_OK;
120 : }
121 :
122 : class UTF8StringEnumerator : public nsIUTF8StringEnumerator
123 : {
124 : public:
125 0 : UTF8StringEnumerator() : mIndex(0) { }
126 0 : ~UTF8StringEnumerator() { }
127 :
128 : NS_DECL_ISUPPORTS
129 : NS_DECL_NSIUTF8STRINGENUMERATOR
130 :
131 : nsTArray<nsCString> mStrings;
132 : PRUint32 mIndex;
133 : };
134 :
135 0 : NS_IMPL_ISUPPORTS1(UTF8StringEnumerator, nsIUTF8StringEnumerator)
136 :
137 : NS_IMETHODIMP
138 0 : UTF8StringEnumerator::HasMore(bool *aResult)
139 : {
140 0 : *aResult = mIndex < mStrings.Length();
141 0 : return NS_OK;
142 : }
143 :
144 : NS_IMETHODIMP
145 0 : UTF8StringEnumerator::GetNext(nsACString& aResult)
146 : {
147 0 : if (mIndex >= mStrings.Length())
148 0 : return NS_ERROR_UNEXPECTED;
149 :
150 0 : aResult.Assign(mStrings[mIndex]);
151 0 : ++mIndex;
152 0 : return NS_OK;
153 : }
154 :
155 : NS_IMETHODIMP
156 0 : nsGnomeVFSMimeApp::GetSupportedURISchemes(nsIUTF8StringEnumerator** aSchemes)
157 : {
158 0 : *aSchemes = nsnull;
159 :
160 0 : nsRefPtr<UTF8StringEnumerator> array = new UTF8StringEnumerator();
161 0 : NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
162 :
163 0 : for (GList *list = mApp->supported_uri_schemes; list; list = list->next) {
164 0 : if (!array->mStrings.AppendElement((char*) list->data)) {
165 0 : return NS_ERROR_OUT_OF_MEMORY;
166 : }
167 : }
168 :
169 0 : NS_ADDREF(*aSchemes = array);
170 0 : return NS_OK;
171 : }
172 :
173 : NS_IMETHODIMP
174 0 : nsGnomeVFSMimeApp::GetRequiresTerminal(bool* aRequires)
175 : {
176 0 : *aRequires = mApp->requires_terminal;
177 0 : return NS_OK;
178 : }
179 :
180 : nsresult
181 29 : nsGnomeVFSService::Init()
182 : {
183 29 : return gnome_vfs_init() ? NS_OK : NS_ERROR_FAILURE;
184 : }
185 :
186 755 : NS_IMPL_ISUPPORTS1(nsGnomeVFSService, nsIGnomeVFSService)
187 :
188 : NS_IMETHODIMP
189 62 : nsGnomeVFSService::GetMimeTypeFromExtension(const nsACString &aExtension,
190 : nsACString& aMimeType)
191 : {
192 124 : nsCAutoString fileExtToUse(".");
193 62 : fileExtToUse.Append(aExtension);
194 :
195 62 : const char *mimeType = gnome_vfs_mime_type_from_name(fileExtToUse.get());
196 62 : aMimeType.Assign(mimeType);
197 :
198 : // |mimeType| points to internal gnome-vfs data, so don't free it.
199 :
200 62 : return NS_OK;
201 : }
202 :
203 : NS_IMETHODIMP
204 60 : nsGnomeVFSService::GetAppForMimeType(const nsACString &aMimeType,
205 : nsIGnomeVFSMimeApp** aApp)
206 : {
207 60 : *aApp = nsnull;
208 : GnomeVFSMimeApplication *app =
209 60 : gnome_vfs_mime_get_default_application(PromiseFlatCString(aMimeType).get());
210 :
211 60 : if (app) {
212 42 : nsGnomeVFSMimeApp *mozApp = new nsGnomeVFSMimeApp(app);
213 42 : NS_ENSURE_TRUE(mozApp, NS_ERROR_OUT_OF_MEMORY);
214 :
215 42 : NS_ADDREF(*aApp = mozApp);
216 : }
217 :
218 60 : return NS_OK;
219 : }
220 :
221 : NS_IMETHODIMP
222 42 : nsGnomeVFSService::GetDescriptionForMimeType(const nsACString &aMimeType,
223 : nsACString& aDescription)
224 : {
225 : const char *desc =
226 42 : gnome_vfs_mime_get_description(PromiseFlatCString(aMimeType).get());
227 42 : aDescription.Assign(desc);
228 :
229 : // |desc| points to internal gnome-vfs data, so don't free it.
230 :
231 42 : return NS_OK;
232 : }
233 :
234 : NS_IMETHODIMP
235 0 : nsGnomeVFSService::ShowURI(nsIURI *aURI)
236 : {
237 0 : nsCAutoString spec;
238 0 : aURI->GetSpec(spec);
239 :
240 0 : if (gnome_vfs_url_show_with_env(spec.get(), NULL) == GNOME_VFS_OK)
241 0 : return NS_OK;
242 :
243 0 : return NS_ERROR_FAILURE;
244 : }
245 :
246 : NS_IMETHODIMP
247 0 : nsGnomeVFSService::ShowURIForInput(const nsACString &aUri)
248 : {
249 0 : char* spec = gnome_vfs_make_uri_from_input(PromiseFlatCString(aUri).get());
250 0 : nsresult rv = NS_ERROR_FAILURE;
251 :
252 0 : if (gnome_vfs_url_show_with_env(spec, NULL) == GNOME_VFS_OK)
253 0 : rv = NS_OK;
254 :
255 0 : g_free(spec);
256 0 : return rv;
257 : }
|