Back to index

lightning-sunbird  0.9+nobinonly
nsGnomeVFSService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla GNOME integration code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsGnomeVFSService.h"
00040 #include "nsStringEnumerator.h"
00041 #include "nsVoidArray.h"
00042 #include "nsString.h"
00043 #include "nsIURI.h"
00044 
00045 extern "C" {
00046 #include <libgnomevfs/gnome-vfs-application-registry.h>
00047 #include <libgnomevfs/gnome-vfs-init.h>
00048 #include <libgnomevfs/gnome-vfs-mime.h>
00049 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
00050 #include <libgnomevfs/gnome-vfs-mime-info.h>
00051 #include <libgnome/gnome-url.h>
00052 }
00053 
00054 class nsGnomeVFSMimeApp : public nsIGnomeVFSMimeApp
00055 {
00056 public:
00057   NS_DECL_ISUPPORTS
00058   NS_DECL_NSIGNOMEVFSMIMEAPP
00059 
00060   nsGnomeVFSMimeApp(GnomeVFSMimeApplication* aApp) : mApp(aApp) {}
00061   ~nsGnomeVFSMimeApp() { gnome_vfs_mime_application_free(mApp); }
00062 
00063 private:
00064   GnomeVFSMimeApplication *mApp;
00065 };
00066 
00067 NS_IMPL_ISUPPORTS1(nsGnomeVFSMimeApp, nsIGnomeVFSMimeApp)
00068 
00069 NS_IMETHODIMP
00070 nsGnomeVFSMimeApp::GetId(nsACString& aId)
00071 {
00072   aId.Assign(mApp->id);
00073   return NS_OK;
00074 }
00075 
00076 NS_IMETHODIMP
00077 nsGnomeVFSMimeApp::GetName(nsACString& aName)
00078 {
00079   aName.Assign(mApp->name);
00080   return NS_OK;
00081 }
00082 
00083 NS_IMETHODIMP
00084 nsGnomeVFSMimeApp::GetCommand(nsACString& aCommand)
00085 {
00086   aCommand.Assign(mApp->command);
00087   return NS_OK;
00088 }
00089 
00090 NS_IMETHODIMP
00091 nsGnomeVFSMimeApp::GetCanOpenMultipleFiles(PRBool* aCanOpen)
00092 {
00093   *aCanOpen = mApp->can_open_multiple_files;
00094   return NS_OK;
00095 }
00096 
00097 NS_IMETHODIMP
00098 nsGnomeVFSMimeApp::GetExpectsURIs(PRInt32* aExpects)
00099 {
00100   *aExpects = mApp->expects_uris;
00101   return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP
00105 nsGnomeVFSMimeApp::GetSupportedURISchemes(nsIUTF8StringEnumerator** aSchemes)
00106 {
00107   *aSchemes = nsnull;
00108 
00109   nsCStringArray *array = new nsCStringArray();
00110   NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
00111 
00112   for (GList *list = mApp->supported_uri_schemes; list; list = list->next) {
00113     if (!array->AppendCString(nsDependentCString((char*) list->data))) {
00114       delete array;
00115       return NS_ERROR_OUT_OF_MEMORY;
00116     }
00117   }
00118 
00119   return NS_NewAdoptingUTF8StringEnumerator(aSchemes, array);
00120 }
00121 
00122 NS_IMETHODIMP
00123 nsGnomeVFSMimeApp::GetRequiresTerminal(PRBool* aRequires)
00124 {
00125   *aRequires = mApp->requires_terminal;
00126   return NS_OK;
00127 }
00128 
00129 nsresult
00130 nsGnomeVFSService::Init()
00131 {
00132   return gnome_vfs_init() ? NS_OK : NS_ERROR_FAILURE;
00133 }
00134 
00135 NS_IMPL_ISUPPORTS1(nsGnomeVFSService, nsIGnomeVFSService)
00136 
00137 NS_IMETHODIMP
00138 nsGnomeVFSService::GetMimeTypeFromExtension(const nsACString &aExtension,
00139                                             nsACString& aMimeType)
00140 {
00141   nsCAutoString fileExtToUse(NS_LITERAL_CSTRING(".") + aExtension);
00142 
00143   const char *mimeType = gnome_vfs_mime_type_from_name(fileExtToUse.get());
00144   aMimeType.Assign(mimeType);
00145 
00146   // |mimeType| points to internal gnome-vfs data, so don't free it.
00147 
00148   return NS_OK;
00149 }
00150 
00151 NS_IMETHODIMP
00152 nsGnomeVFSService::GetAppForMimeType(const nsACString &aMimeType,
00153                                      nsIGnomeVFSMimeApp** aApp)
00154 {
00155   *aApp = nsnull;
00156   GnomeVFSMimeApplication *app =
00157    gnome_vfs_mime_get_default_application(PromiseFlatCString(aMimeType).get());
00158 
00159   if (app) {
00160     nsGnomeVFSMimeApp *mozApp = new nsGnomeVFSMimeApp(app);
00161     NS_ENSURE_TRUE(mozApp, NS_ERROR_OUT_OF_MEMORY);
00162 
00163     NS_ADDREF(*aApp = mozApp);
00164   }
00165 
00166   return NS_OK;
00167 }
00168 
00169 NS_IMETHODIMP
00170 nsGnomeVFSService::SetAppForMimeType(const nsACString &aMimeType,
00171                                      const nsACString &aId)
00172 {
00173   gnome_vfs_mime_set_default_application(PromiseFlatCString(aMimeType).get(),
00174                                          PromiseFlatCString(aId).get());
00175   return NS_OK;
00176 }
00177 
00178 NS_IMETHODIMP
00179 nsGnomeVFSService::SetIconForMimeType(const nsACString &aMimeType,
00180                                       const nsACString &aIconName)
00181 {
00182   gnome_vfs_mime_set_icon(PromiseFlatCString(aMimeType).get(),
00183                           PromiseFlatCString(aIconName).get());
00184   return NS_OK;
00185 }
00186 
00187 NS_IMETHODIMP
00188 nsGnomeVFSService::GetDescriptionForMimeType(const nsACString &aMimeType,
00189                                              nsACString& aDescription)
00190 {
00191   const char *desc =
00192     gnome_vfs_mime_get_description(PromiseFlatCString(aMimeType).get());
00193   aDescription.Assign(desc);
00194 
00195   // |desc| points to internal gnome-vfs data, so don't free it.
00196 
00197   return NS_OK;
00198 }
00199 
00200 NS_IMETHODIMP
00201 nsGnomeVFSService::ShowURI(nsIURI *aURI)
00202 {
00203   nsCAutoString spec;
00204   aURI->GetSpec(spec);
00205 
00206   if (gnome_url_show(spec.get(), NULL))
00207     return NS_OK;
00208 
00209   return NS_ERROR_FAILURE;
00210 }
00211 
00212 NS_IMETHODIMP
00213 nsGnomeVFSService::SetAppStringKey(const nsACString &aID,
00214                                    PRInt32           aKey,
00215                                    const nsACString &aValue)
00216 {
00217   const char *key;
00218 
00219   if (aKey == APP_KEY_COMMAND)
00220     key = GNOME_VFS_APPLICATION_REGISTRY_COMMAND;
00221   else if (aKey == APP_KEY_NAME)
00222     key = GNOME_VFS_APPLICATION_REGISTRY_NAME;
00223   else if (aKey == APP_KEY_SUPPORTED_URI_SCHEMES)
00224     key = "supported_uri_schemes";
00225   else if (aKey == APP_KEY_EXPECTS_URIS)
00226     key = "expects_uris";
00227   else
00228     return NS_ERROR_NOT_AVAILABLE;
00229 
00230   gnome_vfs_application_registry_set_value(PromiseFlatCString(aID).get(), key,
00231                                            PromiseFlatCString(aValue).get());
00232   return NS_OK;
00233 }
00234 
00235 NS_IMETHODIMP
00236 nsGnomeVFSService::SetAppBoolKey(const nsACString &aID,
00237                                  PRInt32          aKey,
00238                                  PRBool           aValue)
00239 {
00240   const char *key;
00241 
00242   if (aKey == APP_KEY_CAN_OPEN_MULTIPLE)
00243     key = GNOME_VFS_APPLICATION_REGISTRY_CAN_OPEN_MULTIPLE_FILES;
00244   else if (aKey == APP_KEY_REQUIRES_TERMINAL)
00245     key = GNOME_VFS_APPLICATION_REGISTRY_REQUIRES_TERMINAL;
00246   else
00247     return NS_ERROR_NOT_AVAILABLE;
00248 
00249   gnome_vfs_application_registry_set_bool_value(PromiseFlatCString(aID).get(),
00250                                                 key, aValue);
00251   return NS_OK;
00252 }
00253 
00254 NS_IMETHODIMP
00255 nsGnomeVFSService::AddMimeType(const nsACString &aID, const nsACString &aType)
00256 {
00257   gnome_vfs_application_registry_add_mime_type(PromiseFlatCString(aID).get(),
00258                                               PromiseFlatCString(aType).get());
00259   return NS_OK;
00260 }
00261 
00262 NS_IMETHODIMP
00263 nsGnomeVFSService::SyncAppRegistry()
00264 {
00265   gnome_vfs_application_registry_sync();
00266   return NS_OK;
00267 }
00268 
00269 NS_IMETHODIMP
00270 nsGnomeVFSService::SetMimeExtensions(const nsACString &aMimeType,
00271                                      const nsACString &aExtensionsList)
00272 {
00273   GnomeVFSResult res =
00274     gnome_vfs_mime_set_extensions_list(PromiseFlatCString(aMimeType).get(),
00275                                     PromiseFlatCString(aExtensionsList).get());
00276   return (res == GNOME_VFS_OK) ? NS_OK : NS_ERROR_FAILURE;
00277 }