uriloader/exthandler/unix/nsMIMEInfoUnix.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/uriloader/exthandler/unix/nsMIMEInfoUnix.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifdef MOZ_WIDGET_QT
    1.11 +#include <QDesktopServices>
    1.12 +#include <QUrl>
    1.13 +#include <QString>
    1.14 +#if (MOZ_ENABLE_CONTENTACTION)
    1.15 +#include <contentaction/contentaction.h>
    1.16 +#include "nsContentHandlerApp.h"
    1.17 +#endif
    1.18 +#endif
    1.19 +
    1.20 +#include "nsMIMEInfoUnix.h"
    1.21 +#include "nsGNOMERegistry.h"
    1.22 +#include "nsIGIOService.h"
    1.23 +#include "nsNetCID.h"
    1.24 +#include "nsIIOService.h"
    1.25 +#include "nsIGnomeVFSService.h"
    1.26 +#include "nsAutoPtr.h"
    1.27 +#ifdef MOZ_ENABLE_DBUS
    1.28 +#include "nsDBusHandlerApp.h"
    1.29 +#endif
    1.30 +
    1.31 +nsresult
    1.32 +nsMIMEInfoUnix::LoadUriInternal(nsIURI * aURI)
    1.33 +{
    1.34 +  nsresult rv = nsGNOMERegistry::LoadURL(aURI);
    1.35 +
    1.36 +#ifdef MOZ_WIDGET_QT
    1.37 +  if (NS_FAILED(rv)) {
    1.38 +    nsAutoCString spec;
    1.39 +    aURI->GetAsciiSpec(spec);
    1.40 +    if (QDesktopServices::openUrl(QUrl(spec.get()))) {
    1.41 +      rv = NS_OK;
    1.42 +    }
    1.43 +  }
    1.44 +#endif
    1.45 +
    1.46 +  return rv;
    1.47 +}
    1.48 +
    1.49 +NS_IMETHODIMP
    1.50 +nsMIMEInfoUnix::GetHasDefaultHandler(bool *_retval)
    1.51 +{
    1.52 +  // if mDefaultApplication is set, it means the application has been set from
    1.53 +  // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
    1.54 +  // give the GNOME answer.
    1.55 +  if (mDefaultApplication)
    1.56 +    return nsMIMEInfoImpl::GetHasDefaultHandler(_retval);
    1.57 +
    1.58 +  *_retval = false;
    1.59 +
    1.60 +  if (mClass ==  eProtocolInfo) {
    1.61 +    *_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get());
    1.62 +  } else {
    1.63 +    nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromType(mSchemeOrType);
    1.64 +    if (!mimeInfo) {
    1.65 +      nsAutoCString ext;
    1.66 +      nsresult rv = GetPrimaryExtension(ext);
    1.67 +      if (NS_SUCCEEDED(rv)) {
    1.68 +        mimeInfo = nsGNOMERegistry::GetFromExtension(ext);
    1.69 +      }
    1.70 +    }
    1.71 +    if (mimeInfo)
    1.72 +      *_retval = true;
    1.73 +  }
    1.74 +
    1.75 +  if (*_retval)
    1.76 +    return NS_OK;
    1.77 +
    1.78 +#if defined(MOZ_ENABLE_CONTENTACTION)
    1.79 +  ContentAction::Action action = 
    1.80 +    ContentAction::Action::defaultActionForFile(QUrl(), QString(mSchemeOrType.get()));
    1.81 +  if (action.isValid()) {
    1.82 +    *_retval = true;
    1.83 +    return NS_OK;
    1.84 +  }
    1.85 +#endif
    1.86 +
    1.87 +  return NS_OK;
    1.88 +}
    1.89 +
    1.90 +nsresult
    1.91 +nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile *aFile)
    1.92 +{
    1.93 +  // if mDefaultApplication is set, it means the application has been set from
    1.94 +  // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
    1.95 +  // give the GNOME answer.
    1.96 +  if (mDefaultApplication)
    1.97 +    return nsMIMEInfoImpl::LaunchDefaultWithFile(aFile);
    1.98 +
    1.99 +  nsAutoCString nativePath;
   1.100 +  aFile->GetNativePath(nativePath);
   1.101 +
   1.102 +#if defined(MOZ_ENABLE_CONTENTACTION)
   1.103 +  QUrl uri = QUrl::fromLocalFile(QString::fromUtf8(nativePath.get()));
   1.104 +  ContentAction::Action action =
   1.105 +    ContentAction::Action::defaultActionForFile(uri, QString(mSchemeOrType.get()));
   1.106 +  if (action.isValid()) {
   1.107 +    action.trigger();
   1.108 +    return NS_OK;
   1.109 +  }
   1.110 +  return NS_ERROR_FAILURE;
   1.111 +#endif
   1.112 +
   1.113 +  nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
   1.114 +  nsAutoCString uriSpec;
   1.115 +  if (giovfs) {
   1.116 +    // nsGIOMimeApp->Launch wants a URI string instead of local file
   1.117 +    nsresult rv;
   1.118 +    nsCOMPtr<nsIIOService> ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
   1.119 +    NS_ENSURE_SUCCESS(rv, rv);
   1.120 +    nsCOMPtr<nsIURI> uri;
   1.121 +    rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
   1.122 +    NS_ENSURE_SUCCESS(rv, rv);
   1.123 +    uri->GetSpec(uriSpec);
   1.124 +  }
   1.125 +
   1.126 +  nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
   1.127 +  if (giovfs) {
   1.128 +    nsCOMPtr<nsIGIOMimeApp> app;
   1.129 +    if (NS_SUCCEEDED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app)
   1.130 +      return app->Launch(uriSpec);
   1.131 +  } else if (gnomevfs) {
   1.132 +    /* Fallback to GnomeVFS */
   1.133 +    nsCOMPtr<nsIGnomeVFSMimeApp> app;
   1.134 +    if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app)
   1.135 +      return app->Launch(nativePath);
   1.136 +  }
   1.137 +
   1.138 +  // If we haven't got an app we try to get a valid one by searching for the
   1.139 +  // extension mapped type
   1.140 +  nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromExtension(nativePath);
   1.141 +  if (mimeInfo) {
   1.142 +    nsAutoCString type;
   1.143 +    mimeInfo->GetType(type);
   1.144 +    if (giovfs) {
   1.145 +      nsCOMPtr<nsIGIOMimeApp> app;
   1.146 +      if (NS_SUCCEEDED(giovfs->GetAppForMimeType(type, getter_AddRefs(app))) && app)
   1.147 +        return app->Launch(uriSpec);
   1.148 +    } else if (gnomevfs) {
   1.149 +      nsCOMPtr<nsIGnomeVFSMimeApp> app;
   1.150 +      if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(type, getter_AddRefs(app))) && app)
   1.151 +        return app->Launch(nativePath);
   1.152 +    }
   1.153 +  }
   1.154 +
   1.155 +  if (!mDefaultApplication)
   1.156 +    return NS_ERROR_FILE_NOT_FOUND;
   1.157 +
   1.158 +  return LaunchWithIProcess(mDefaultApplication, nativePath);
   1.159 +}
   1.160 +
   1.161 +#if defined(MOZ_ENABLE_CONTENTACTION)
   1.162 +NS_IMETHODIMP
   1.163 +nsMIMEInfoUnix::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers)
   1.164 +{
   1.165 +  if (!mPossibleApplications) {
   1.166 +    mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID);
   1.167 +
   1.168 +    if (!mPossibleApplications)
   1.169 +      return NS_ERROR_OUT_OF_MEMORY;
   1.170 +
   1.171 +    QList<ContentAction::Action> actions =
   1.172 +      ContentAction::Action::actionsForFile(QUrl(), QString(mSchemeOrType.get()));
   1.173 +
   1.174 +    for (int i = 0; i < actions.size(); ++i) {
   1.175 +      nsContentHandlerApp* app =
   1.176 +        new nsContentHandlerApp(nsString((char16_t*)actions[i].name().data()), 
   1.177 +                                mSchemeOrType, actions[i]);
   1.178 +      mPossibleApplications->AppendElement(app, false);
   1.179 +    }
   1.180 +  }
   1.181 +
   1.182 +  *aPossibleAppHandlers = mPossibleApplications;
   1.183 +  NS_ADDREF(*aPossibleAppHandlers);
   1.184 +  return NS_OK;
   1.185 +}
   1.186 +#endif
   1.187 +

mercurial