Back to index

lightning-sunbird  0.9+nobinonly
nsPluginsDirBeOS.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Musil
00024  *   Makoto Hamanaka <VYA04230@nifty.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041        nsPluginsDirBeOS.cpp
00042        
00043        BeOS implementation of the nsPluginsDir/nsPluginsFile classes.
00044        
00045        Based on nsPluginsDirUNIX.cpp r1.12 by Alex Musil
00046  */
00047 
00048 #include "nsPluginsDir.h"
00049 #include "prlink.h"
00050 #include "plstr.h"
00051 #include "prmem.h"
00052 #include "nsReadableUtils.h"
00053 #include "nsString.h"
00054 
00055 #include <File.h>
00056 #include <AppFileInfo.h>
00057 #include <Message.h>
00058 #include <String.h>
00059 
00060 //#define NS_PLUGIN_BEOS_DEBUG
00061 
00062 /* Local helper functions */
00063  
00064 static char* GetFileName(const char* pathname)
00065 {
00066         const char* filename = nsnull;
00067                 
00068         // this is most likely a path, so skip to the filename
00069         filename = PL_strrchr(pathname, '/');
00070         if(filename)
00071                 ++filename;
00072         else
00073                 filename = pathname;
00074 
00075         return PL_strdup(filename);
00076 }
00077 
00078 static nsresult GetMimeExtensions(const char *mimeType, char *extensions, int extLen)
00079 {
00080     // check variables
00081     if (!mimeType || !extensions || extLen < 1) return NS_ERROR_FAILURE;
00082     extensions[0] = '\0';
00083     
00084     // make mime object
00085     BMimeType mime(mimeType) ;
00086     if (mime.InitCheck() != B_OK)
00087         return NS_ERROR_FAILURE;
00088     
00089     // get extensions : comma separated (if multiple extensions in a mime-type)
00090     // ex) "jpg,jpeg"
00091     BString extStr("");
00092     BMessage extMsg;
00093     mime.GetFileExtensions(&extMsg);
00094     uint32 type;
00095     int32 types_num;
00096     if (extMsg.GetInfo("extensions", &type, &types_num) != B_OK
00097         || type != B_STRING_TYPE || types_num == 0)
00098         return NS_ERROR_FAILURE;
00099     
00100     for (int i = 0 ; i < types_num ; i ++) {
00101         const char *ext;
00102         if (extMsg.FindString("extensions", i, &ext) != B_OK) {
00103             break;
00104         }
00105         if (i > 0)
00106             extStr.Append(",");
00107         extStr.Append(ext);
00108     }
00109     PL_strncpyz(extensions, extStr.String(), extLen) ;
00110     
00111     return NS_OK;
00112 }
00113 
00115 
00116 /* nsPluginsDir implementation */
00117 
00118 PRBool nsPluginsDir::IsPluginFile(nsIFile* file)
00119 {
00120        return PR_TRUE;
00121 }
00122 
00124 
00125 /* nsPluginFile implementation */
00126 
00127 nsPluginFile::nsPluginFile(nsIFile* spec)
00128 :      mPlugin(spec)
00129 {
00130        // nada
00131 }
00132 
00133 nsPluginFile::~nsPluginFile()
00134 {
00135        // nada
00136 }
00137 
00142 nsresult nsPluginFile::LoadPlugin(PRLibrary* &outLibrary)
00143 {
00144         nsCAutoString path;
00145         nsresult rv = mPlugin->GetNativePath(path);
00146         if (NS_OK != rv) {
00147             return rv;
00148         }
00149         pLibrary = outLibrary = PR_LoadLibrary(path.get());
00150 
00151 #ifdef NS_DEBUG
00152         printf("LoadPlugin() %s returned %lx\n",path,(unsigned long)pLibrary);
00153 #endif
00154 
00155         return NS_OK;
00156 }
00157 
00158 typedef char* (*BeOS_Plugin_GetMIMEDescription)();
00159 
00160 
00164 nsresult nsPluginFile::GetPluginInfo(nsPluginInfo& info)
00165 {
00166     nsCAutoString fpath;
00167     nsresult rv = mPlugin->GetNativePath(fpath);
00168     if (NS_OK != rv) {
00169         return rv;
00170     }
00171     const char *path = fpath.get();
00172     int i;
00173 
00174 #ifdef NS_PLUGIN_BEOS_DEBUG
00175     printf("nsPluginFile::GetPluginInfo() an attempt to load MIME String\n");
00176     printf("path = <%s>\n", path);
00177 #endif
00178 
00179     // get supported mime types
00180     BFile file(path, B_READ_ONLY);
00181     if (file.InitCheck() != B_OK)
00182         return NS_ERROR_FAILURE;
00183 
00184     BAppFileInfo appinfo(&file);
00185     if (appinfo.InitCheck() != B_OK)
00186         return NS_ERROR_FAILURE;
00187 
00188     BMessage msg;
00189     if (appinfo.GetSupportedTypes(&msg) != B_OK)
00190         return NS_ERROR_FAILURE;
00191 
00192     uint32 type;
00193     int32 types_num;
00194     if (msg.GetInfo("types", &type, &types_num) != B_OK
00195         || type != B_STRING_TYPE)
00196         return NS_ERROR_FAILURE;
00197 
00198     // set mime types to plugin info
00199     info.fMimeTypeArray =(char **)PR_Malloc(types_num * sizeof(char *));
00200     info.fMimeDescriptionArray =(char **)PR_Malloc(types_num * sizeof(char *));
00201     info.fExtensionArray =(char **)PR_Malloc(types_num * sizeof(char *));
00202 
00203     for (i = 0 ; i < types_num ; i ++) {
00204         // get mime string
00205         const char *mtype;
00206         if (msg.FindString("types", i, &mtype) != B_OK) {
00207             types_num = i;
00208             break;
00209         }
00210         
00211         // get (short)description for the mime
00212         char desc[B_MIME_TYPE_LENGTH+1] = "";
00213         BMimeType mime(mtype) ;
00214         if (mime.InitCheck() == B_OK)
00215             mime.GetShortDescription(desc);
00216         
00217         // get file extensions for the mime
00218         char extensions[B_MIME_TYPE_LENGTH+1] = "";
00219         GetMimeExtensions(mtype, extensions, B_MIME_TYPE_LENGTH+1);
00220 
00221         #ifdef NS_PLUGIN_BEOS_DEBUG
00222             printf("  mime = %30s | %10s | %15s |\n", 
00223                 mtype, extensions, desc);
00224         #endif
00225         
00226         info.fMimeTypeArray[i] = PL_strdup( mtype ? mtype : (char *)"" ) ;
00227         info.fMimeDescriptionArray[i] = PL_strdup( desc ) ;
00228         info.fExtensionArray[i] = PL_strdup( extensions );
00229     }
00230 
00231     // get name and description of this plugin
00232     version_info vinfo;
00233     if (appinfo.GetVersionInfo(&vinfo, B_APP_VERSION_KIND) == B_OK
00234         && *vinfo.short_info) {
00235         // XXX convert UTF-8 2byte chars to 1 byte chars, to avoid string corruption
00236         info.fName = ToNewCString(NS_ConvertUTF8toUCS2(vinfo.short_info));
00237         info.fDescription = ToNewCString(NS_ConvertUTF8toUCS2(vinfo.long_info));
00238     } else {
00239         // use filename as its name
00240         info.fName = GetFileName(path);
00241         info.fDescription = PL_strdup("");
00242     }
00243 
00244     info.fVariantCount = types_num;
00245     info.fFileName = PL_strdup(path);
00246 
00247 
00248 #ifdef NS_PLUGIN_BEOS_DEBUG
00249     printf("info.fFileName = %s\n", info.fFileName);
00250     printf("info.fName = %s\n", info.fName);
00251     printf("info.fDescription = %s\n", info.fDescription);
00252 #endif
00253 
00254     return NS_OK;
00255 }
00256 
00257 nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
00258 {
00259     if (info.fName)
00260         PL_strfree(info.fName);
00261 
00262     if (info.fDescription)
00263         PL_strfree(info.fDescription);
00264 
00265     for (PRUint32 i = 0; i < info.fVariantCount; i++) {
00266         if (info.fMimeTypeArray[i])
00267             PL_strfree(info.fMimeTypeArray[i]);
00268 
00269         if (info.fMimeDescriptionArray[i])
00270             PL_strfree(info.fMimeDescriptionArray[i]);
00271 
00272         if (info.fExtensionArray[i])
00273             PL_strfree(info.fExtensionArray[i]);
00274     }
00275 
00276     PR_FREEIF(info.fMimeTypeArray);
00277     PR_FREEIF(info.fMimeDescriptionArray);
00278     PR_FREEIF(info.fExtensionArray);
00279 
00280     if (info.fFileName)
00281         PL_strfree(info.fFileName);
00282 
00283     return NS_OK;
00284 }