toolkit/system/gnome/nsGnomeVFSService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/system/gnome/nsGnomeVFSService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,224 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsGnomeVFSService.h"
    1.10 +#include "nsStringAPI.h"
    1.11 +#include "nsIURI.h"
    1.12 +#include "nsTArray.h"
    1.13 +#include "nsIStringEnumerator.h"
    1.14 +#include "nsAutoPtr.h"
    1.15 +
    1.16 +extern "C" {
    1.17 +#include <libgnomevfs/gnome-vfs.h>
    1.18 +#include <libgnomevfs/gnome-vfs-mime.h>
    1.19 +#include <libgnomevfs/gnome-vfs-mime-handlers.h>
    1.20 +}
    1.21 +
    1.22 +class nsGnomeVFSMimeApp MOZ_FINAL : public nsIGnomeVFSMimeApp
    1.23 +{
    1.24 +public:
    1.25 +  NS_DECL_ISUPPORTS
    1.26 +  NS_DECL_NSIGNOMEVFSMIMEAPP
    1.27 +
    1.28 +  nsGnomeVFSMimeApp(GnomeVFSMimeApplication* aApp) : mApp(aApp) {}
    1.29 +  ~nsGnomeVFSMimeApp() { gnome_vfs_mime_application_free(mApp); }
    1.30 +
    1.31 +private:
    1.32 +  GnomeVFSMimeApplication *mApp;
    1.33 +};
    1.34 +
    1.35 +NS_IMPL_ISUPPORTS(nsGnomeVFSMimeApp, nsIGnomeVFSMimeApp)
    1.36 +
    1.37 +NS_IMETHODIMP
    1.38 +nsGnomeVFSMimeApp::GetId(nsACString& aId)
    1.39 +{
    1.40 +  aId.Assign(mApp->id);
    1.41 +  return NS_OK;
    1.42 +}
    1.43 +
    1.44 +NS_IMETHODIMP
    1.45 +nsGnomeVFSMimeApp::GetName(nsACString& aName)
    1.46 +{
    1.47 +  aName.Assign(mApp->name);
    1.48 +  return NS_OK;
    1.49 +}
    1.50 +
    1.51 +NS_IMETHODIMP
    1.52 +nsGnomeVFSMimeApp::GetCommand(nsACString& aCommand)
    1.53 +{
    1.54 +  aCommand.Assign(mApp->command);
    1.55 +  return NS_OK;
    1.56 +}
    1.57 +
    1.58 +NS_IMETHODIMP
    1.59 +nsGnomeVFSMimeApp::GetCanOpenMultipleFiles(bool* aCanOpen)
    1.60 +{
    1.61 +  *aCanOpen = mApp->can_open_multiple_files;
    1.62 +  return NS_OK;
    1.63 +}
    1.64 +
    1.65 +NS_IMETHODIMP
    1.66 +nsGnomeVFSMimeApp::GetExpectsURIs(int32_t* aExpects)
    1.67 +{
    1.68 +  *aExpects = mApp->expects_uris;
    1.69 +  return NS_OK;
    1.70 +}
    1.71 +
    1.72 +NS_IMETHODIMP
    1.73 +nsGnomeVFSMimeApp::Launch(const nsACString &aUri)
    1.74 +{
    1.75 +  char *uri = gnome_vfs_make_uri_from_input(PromiseFlatCString(aUri).get());
    1.76 +
    1.77 +  if (! uri)
    1.78 +    return NS_ERROR_FAILURE;
    1.79 +
    1.80 +  GList uris = { 0 };
    1.81 +  uris.data = uri;
    1.82 +
    1.83 +  GnomeVFSResult result = gnome_vfs_mime_application_launch(mApp, &uris);
    1.84 +  g_free(uri);
    1.85 +
    1.86 +  if (result != GNOME_VFS_OK)
    1.87 +    return NS_ERROR_FAILURE;
    1.88 +
    1.89 +  return NS_OK;
    1.90 +}
    1.91 +
    1.92 +class UTF8StringEnumerator MOZ_FINAL : public nsIUTF8StringEnumerator
    1.93 +{
    1.94 +public:
    1.95 +  UTF8StringEnumerator() : mIndex(0) { }
    1.96 +  ~UTF8StringEnumerator() { }
    1.97 +
    1.98 +  NS_DECL_ISUPPORTS
    1.99 +  NS_DECL_NSIUTF8STRINGENUMERATOR
   1.100 +
   1.101 +  nsTArray<nsCString> mStrings;
   1.102 +  uint32_t            mIndex;
   1.103 +};
   1.104 +
   1.105 +NS_IMPL_ISUPPORTS(UTF8StringEnumerator, nsIUTF8StringEnumerator)
   1.106 +
   1.107 +NS_IMETHODIMP
   1.108 +UTF8StringEnumerator::HasMore(bool *aResult)
   1.109 +{
   1.110 +  *aResult = mIndex < mStrings.Length();
   1.111 +  return NS_OK;
   1.112 +}
   1.113 +
   1.114 +NS_IMETHODIMP
   1.115 +UTF8StringEnumerator::GetNext(nsACString& aResult)
   1.116 +{
   1.117 +  if (mIndex >= mStrings.Length())
   1.118 +    return NS_ERROR_UNEXPECTED;
   1.119 +
   1.120 +  aResult.Assign(mStrings[mIndex]);
   1.121 +  ++mIndex;
   1.122 +  return NS_OK;
   1.123 +}
   1.124 +
   1.125 +NS_IMETHODIMP
   1.126 +nsGnomeVFSMimeApp::GetSupportedURISchemes(nsIUTF8StringEnumerator** aSchemes)
   1.127 +{
   1.128 +  *aSchemes = nullptr;
   1.129 +
   1.130 +  nsRefPtr<UTF8StringEnumerator> array = new UTF8StringEnumerator();
   1.131 +  NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
   1.132 +
   1.133 +  for (GList *list = mApp->supported_uri_schemes; list; list = list->next) {
   1.134 +    if (!array->mStrings.AppendElement((char*) list->data)) {
   1.135 +      return NS_ERROR_OUT_OF_MEMORY;
   1.136 +    }
   1.137 +  }
   1.138 +
   1.139 +  NS_ADDREF(*aSchemes = array);
   1.140 +  return NS_OK;
   1.141 +}
   1.142 +
   1.143 +NS_IMETHODIMP
   1.144 +nsGnomeVFSMimeApp::GetRequiresTerminal(bool* aRequires)
   1.145 +{
   1.146 +  *aRequires = mApp->requires_terminal;
   1.147 +  return NS_OK;
   1.148 +}
   1.149 +
   1.150 +nsresult
   1.151 +nsGnomeVFSService::Init()
   1.152 +{
   1.153 +  return gnome_vfs_init() ? NS_OK : NS_ERROR_FAILURE;
   1.154 +}
   1.155 +
   1.156 +NS_IMPL_ISUPPORTS(nsGnomeVFSService, nsIGnomeVFSService)
   1.157 +
   1.158 +NS_IMETHODIMP
   1.159 +nsGnomeVFSService::GetMimeTypeFromExtension(const nsACString &aExtension,
   1.160 +                                            nsACString& aMimeType)
   1.161 +{
   1.162 +  nsAutoCString fileExtToUse(".");
   1.163 +  fileExtToUse.Append(aExtension);
   1.164 +
   1.165 +  const char *mimeType = gnome_vfs_mime_type_from_name(fileExtToUse.get());
   1.166 +  aMimeType.Assign(mimeType);
   1.167 +
   1.168 +  // |mimeType| points to internal gnome-vfs data, so don't free it.
   1.169 +
   1.170 +  return NS_OK;
   1.171 +}
   1.172 +
   1.173 +NS_IMETHODIMP
   1.174 +nsGnomeVFSService::GetAppForMimeType(const nsACString &aMimeType,
   1.175 +                                     nsIGnomeVFSMimeApp** aApp)
   1.176 +{
   1.177 +  *aApp = nullptr;
   1.178 +  GnomeVFSMimeApplication *app =
   1.179 +   gnome_vfs_mime_get_default_application(PromiseFlatCString(aMimeType).get());
   1.180 +
   1.181 +  if (app) {
   1.182 +    nsGnomeVFSMimeApp *mozApp = new nsGnomeVFSMimeApp(app);
   1.183 +    NS_ENSURE_TRUE(mozApp, NS_ERROR_OUT_OF_MEMORY);
   1.184 +
   1.185 +    NS_ADDREF(*aApp = mozApp);
   1.186 +  }
   1.187 +
   1.188 +  return NS_OK;
   1.189 +}
   1.190 +
   1.191 +NS_IMETHODIMP
   1.192 +nsGnomeVFSService::GetDescriptionForMimeType(const nsACString &aMimeType,
   1.193 +                                             nsACString& aDescription)
   1.194 +{
   1.195 +  const char *desc =
   1.196 +    gnome_vfs_mime_get_description(PromiseFlatCString(aMimeType).get());
   1.197 +  aDescription.Assign(desc);
   1.198 +
   1.199 +  // |desc| points to internal gnome-vfs data, so don't free it.
   1.200 +
   1.201 +  return NS_OK;
   1.202 +}
   1.203 +
   1.204 +NS_IMETHODIMP
   1.205 +nsGnomeVFSService::ShowURI(nsIURI *aURI)
   1.206 +{
   1.207 +  nsAutoCString spec;
   1.208 +  aURI->GetSpec(spec);
   1.209 +
   1.210 +  if (gnome_vfs_url_show_with_env(spec.get(), nullptr) == GNOME_VFS_OK)
   1.211 +    return NS_OK;
   1.212 +
   1.213 +  return NS_ERROR_FAILURE;
   1.214 +}
   1.215 +
   1.216 +NS_IMETHODIMP
   1.217 +nsGnomeVFSService::ShowURIForInput(const nsACString &aUri)
   1.218 +{
   1.219 +  char* spec = gnome_vfs_make_uri_from_input(PromiseFlatCString(aUri).get());
   1.220 +  nsresult rv = NS_ERROR_FAILURE;
   1.221 +
   1.222 +  if (gnome_vfs_url_show_with_env(spec, nullptr) == GNOME_VFS_OK)
   1.223 +    rv = NS_OK;
   1.224 +
   1.225 +  g_free(spec);
   1.226 +  return rv;
   1.227 +}

mercurial