toolkit/system/gnome/nsGnomeVFSService.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial