xref: /haiku/src/kits/storage/Mime.cpp (revision d7e489f80a82a0dc5974df1e780d7a908129bab4)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //---------------------------------------------------------------------
5 /*!
6 	\file Mime.cpp
7 	Mime type C functions implementation.
8 */
9 
10 #include <fs_attr.h>
11 #include <fs_info.h>
12 #include <Drivers.h>
13 #include <Entry.h>
14 #include <Mime.h>
15 #include <MimeType.h>
16 #include <mime/database_access.h>
17 #include <Node.h>
18 #include <RegistrarDefs.h>
19 #include <Roster.h>
20 
21 #include <unistd.h>
22 
23 enum {
24 	NOT_IMPLEMENTED	= B_ERROR,
25 };
26 
27 // do_mime_update
28 //! Helper function that contacts the registrar for mime update calls
29 status_t do_mime_update(int32 what, const char *path, int recursive,
30 	int synchronous, int force)
31 {
32 	BEntry root;
33 	entry_ref ref;
34 
35 	status_t err = root.SetTo(path ? path : "/");
36 	if (!err)
37 		err = root.GetRef(&ref);
38 	if (!err) {
39 		BMessage msg(what);
40 		BMessage reply;
41 		status_t result;
42 
43 		// Build and send the message, read the reply
44 		if (!err)
45 			err = msg.AddRef("entry", &ref);
46 		if (!err)
47 			err = msg.AddBool("recursive", recursive);
48 		if (!err)
49 			err = msg.AddBool("synchronous", synchronous);
50 		if (!err)
51 			err = msg.AddBool("force", force);
52 		if (!err)
53 			err = _send_to_roster_(&msg, &reply, true);
54 		if (!err)
55 			err = reply.what == B_REG_RESULT ? B_OK : B_BAD_VALUE;
56 		if (!err)
57 			err = reply.FindInt32("result", &result);
58 		if (!err)
59 			err = result;
60 	}
61 	return err;
62 }
63 
64 // update_mime_info
65 /*!	\brief Updates the MIME information (i.e MIME type) for one or more files.
66 	If \a path points to a file, the MIME information for this file are
67 	updated only. If it points to a directory and \a recursive is non-null,
68 	the information for all the files in the given directory tree are updated.
69 	If path is \c NULL all files are considered; \a recursive is ignored in
70 	this case.
71 	\param path The path to a file or directory, or \c NULL.
72 	\param recursive Non-null to trigger recursive behavior.
73 	\param synchronous If non-null update_mime_info() waits until the
74 		   operation is finished, otherwise it returns immediately and the
75 		   update is done asynchronously.
76 	\param force If non-null, also the information for files are updated that
77 		   have already been updated.
78 	\return
79 	- \c B_OK: Everything went fine.
80 	- An error code otherwise.
81 */
82 int
83 update_mime_info(const char *path, int recursive, int synchronous, int force)
84 {
85 	// Force recursion when given a NULL path
86 	if (!path)
87 		recursive = true;
88 
89 	return do_mime_update(B_REG_MIME_UPDATE_MIME_INFO, path, recursive,
90 		synchronous, force);
91 }
92 
93 // create_app_meta_mime
94 /*!	Creates a MIME database entry for one or more applications.
95 	\a path should either point to an application file or should be \c NULL.
96 	In the first case a MIME database entry for that application is created,
97 	in the second case entries for all applications are created.
98 	\param path The path to an application file, or \c NULL.
99 	\param recursive Currently unused.
100 	\param synchronous If non-null create_app_meta_mime() waits until the
101 		   operation is finished, otherwise it returns immediately and the
102 		   operation is done asynchronously.
103 	\param force If non-null, entries are created even if they do already
104 		   exist.
105 	\return
106 	- \c B_OK: Everything went fine.
107 	- An error code otherwise.
108 */
109 status_t
110 create_app_meta_mime(const char *path, int recursive, int synchronous,
111 					 int force)
112 {
113 	// If path is NULL, we are recursive, otherwise no.
114 	recursive = !path;
115 
116 	return do_mime_update(B_REG_MIME_CREATE_APP_META_MIME, path, recursive,
117 		synchronous, force);
118 }
119 
120 // get_device_icon
121 /*!	Retrieves an icon associated with a given device.
122 	\param dev The path to the device.
123 	\param icon A pointer to a buffer the icon data shall be written to.
124 	\param size The size of the icon. Currently the sizes 16 (small, i.e
125 	            \c B_MINI_ICON) and 32 (large, 	i.e. \c B_LARGE_ICON) are
126 	            supported.
127 
128 	\todo The mounted directories for volumes can also have META:X:STD_ICON
129 		  attributes. Should those attributes override the icon returned
130 		  by ioctl(,B_GET_ICON,)?
131 
132 	\return
133 	- \c B_OK: Everything went fine.
134 	- An error code otherwise.
135 */
136 status_t
137 get_device_icon(const char *dev, void *icon, int32 size)
138 {
139 	status_t err = dev && icon
140 				     && (size == B_LARGE_ICON || size == B_MINI_ICON)
141 				       ? B_OK : B_BAD_VALUE;
142 
143 	int fd = -1;
144 
145 	if (!err) {
146 		fd = open(dev, O_RDONLY);
147 		err = fd != -1 ? B_OK : B_BAD_VALUE;
148 	}
149 	if (!err) {
150 		device_icon iconData = { size, icon };
151 		err = ioctl(fd, B_GET_ICON, &iconData);
152 	}
153 	if (fd != -1) {
154 		// If the file descriptor was open()ed, we need to close it
155 		// regardless. Only if we haven't yet encountered any errors
156 		// do we make note close()'s return value, however.
157 		status_t error = close(fd);
158 		if (!err)
159 			err = error;
160 	}
161 	return err;
162 }
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187