KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > filetypes > internal > GnomeAssociationUtil


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.filetypes.internal;
22
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import org.jdesktop.jdic.filetypes.Action;
27
28
29 /**
30  * Utility class for accessing the system association info for Gnome.
31  */

32 public class GnomeAssociationUtil {
33     /* Description and icon_filename keys used on GNOME desktop */
34     public final static String JavaDoc GNOME_VFS_MIME_KEY_DESCRIPTION
35             = GnomeVfsWrapper.GNOME_VFS_MIME_KEY_DESCRIPTION;
36     public final static String JavaDoc GNOME_VFS_MIME_KEY_ICON_FILENAME
37             = GnomeVfsWrapper.GNOME_VFS_MIME_DEFAULT_KEY_ICON_FILENAME;
38
39     /**
40      * Suppress default constructor for noninstantiability.
41      */

42     private GnomeAssociationUtil() {}
43   
44     /**
45      * Returns the mime type associated with the given file extension.
46      * If the file extension doesn't exist in the MIME database, no mime type is returned.
47      *
48      */

49     public static String JavaDoc getMimeTypeByFileExt(String JavaDoc fileExt) {
50         String JavaDoc resultMimeType = null;
51         String JavaDoc[] allMimeTypes = GnomeVfsWrapper.gnome_vfs_get_registered_mime_types();
52
53         if (allMimeTypes == null) {
54             return null;
55         }
56
57         for (int i = 0; i < allMimeTypes.length; i++) {
58             String JavaDoc curMimeType = allMimeTypes[i];
59             String JavaDoc[] fileExtensions = GnomeVfsWrapper.gnome_vfs_mime_get_extensions_list(curMimeType);
60
61             if (fileExtensions != null) {
62                 for (int j = 0; j < fileExtensions.length; j++) {
63                     if (fileExtensions[j].equals(fileExt)) {
64                         resultMimeType = allMimeTypes[i];
65                         break;
66                     }
67                 }
68             }
69             if (resultMimeType != null) {
70                 break;
71             }
72         }
73
74         return resultMimeType;
75     }
76   
77     /**
78      * Returns the file extension list associated with the given mime type.
79      *
80      * @param mimeType Given mime type
81      */

82     public static List JavaDoc getFileExtListByMimeType(String JavaDoc mimeType) {
83         String JavaDoc[] fileExtensions = GnomeVfsWrapper.gnome_vfs_mime_get_extensions_list(mimeType);
84
85         if (fileExtensions == null) {
86             return null;
87         } else {
88             // All returned file extensions has no leading "." character.
89
List JavaDoc fileExtList = new ArrayList JavaDoc();
90             for (int index = 0; index < fileExtensions.length; index++) {
91                 fileExtList.add(fileExtensions[index]);
92             }
93             
94             return fileExtList;
95         }
96     }
97
98     /**
99      * Returns the icon file name associated with the given mime type.
100      *
101      */

102     public static String JavaDoc getIconFileNameByMimeType(String JavaDoc mimeType) {
103         return GnomeVfsWrapper.gnome_vfs_mime_get_icon(mimeType);
104     }
105   
106     /**
107      * Returns the description associated with the given mime type.
108      *
109      * @param mimeType Given mime type
110      * @return String
111      */

112     public static String JavaDoc getDescriptionByMimeType(String JavaDoc mimeType) {
113         return GnomeVfsWrapper.gnome_vfs_mime_get_description(mimeType);
114     }
115
116     /**
117      * Returns the action list associated with the given mime type.
118      */

119     public static List JavaDoc getActionListByMimeType(String JavaDoc mimeType) {
120         List JavaDoc actionList = new ArrayList JavaDoc();
121         Action oneAction = null;
122
123         // Check the keys associated with the mime type which may include below keys:
124
// short_list_application_ids_for_intermediate_user_level
125
// short_list_application_ids_for_novice_user_level
126
// short_list_application_ids_for_advanced_user_level
127
// short_list_component_iids_for_intermediate_user_level
128
// short_list_component_iids_for_novice_user_level
129
// short_list_component_iids
130
// short_list_component_iids_for_advanced_user_level
131
// default_action_type
132
// category
133
// default_application_id
134
String JavaDoc[] keys = GnomeVfsWrapper.gnome_vfs_mime_get_key_list(mimeType);
135         if (keys != null) {
136             String JavaDoc command = null;
137             for (int i = 0; i < keys.length; i++) {
138                 command = GnomeVfsWrapper.gnome_vfs_mime_get_value(mimeType, keys[i]);
139                 if (command != null) {
140                     oneAction = new Action(keys[i], command);
141                     actionList.add(oneAction);
142                 }
143             }
144         }
145         
146         // --- As from the spec for gnome-vfs---
147
// If the default application for a mime type is specified, it will always be used.
148
// and an error will be given if the application is not available. If the default application is not specified, the first
149
// *available* application on the short list of viewers will be used.
150

151         // Get the default/first available application's command to construct an "open" action.
152
// Which will be used by upper level launcher code.
153
String JavaDoc defaultCmd = GnomeVfsWrapper.gnome_vfs_mime_get_default_application_command(mimeType);
154         if (defaultCmd != null) {
155             actionList.add(new Action("open", defaultCmd));
156         }
157       
158         if (actionList.isEmpty()) {
159             return null;
160         } else {
161             return actionList;
162         }
163     }
164   
165     /**
166      * Returns the mime type associated with the given URL, by checking the content of the URL.
167      *
168      */

169     public static String JavaDoc getMimeTypeByURL(URL JavaDoc url) {
170         // The URL object specified by the user shouls be converted from an File object,
171
// such as file:/user/local/test.html, or file:///user/local/test.html.
172
return GnomeVfsWrapper.gnome_vfs_get_mime_type(url.toString());
173     }
174
175     /**
176      * Returns true if the given mime type exists in the GnomeVFS MIME database.
177      */

178     public static boolean isMimeTypeExist(String JavaDoc mimeType) {
179         // Check that the mime type is known and not deleted.
180
boolean isMimeTypeExist = false;
181         String JavaDoc[] allMimeTypes = GnomeVfsWrapper.gnome_vfs_get_registered_mime_types();
182
183         if (allMimeTypes == null) {
184             return false;
185         }
186
187         for (int i = 0; i < allMimeTypes.length; i++) {
188             if (mimeType.equals(allMimeTypes[i])) {
189                 isMimeTypeExist = true;
190                 break;
191             }
192         }
193
194         return isMimeTypeExist;
195 }
196
197     /**
198      * Returns true if the given file extension exists in the GnomeVFS MIME database.
199      */

200     public static boolean isFileExtExist(String JavaDoc fileExt) {
201         return (getMimeTypeByFileExt(fileExt) != null) ? true : false;
202     }
203
204     /**
205      * Returns the value of an environment variable.
206      * It's not related with GnomeVFS API or library.
207      */

208     public static String JavaDoc getEnv(String JavaDoc envName) {
209         return GnomeVfsWrapper.getenv(envName);
210     }
211 }
212
Popular Tags