1 /* vim: set ts=4 sw=4 et: */
3 * maemo-recorder-file.c
4 * File-related operations
6 * Copyright (C) 2006 Nokia Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <libgnomevfs/gnome-vfs.h>
29 #include "maemo-recorder.h"
30 #include "maemo-recorder-file.h"
31 #include "maemo-recorder-au.h"
34 static gboolean moveFile(const gchar *src, const gchar *dst);
39 doSave(const gchar *current, const gchar *target, AudioFormat format)
41 if (!current|| !target)
57 return moveFile(current, target);
60 return moveFile(current, target);
65 moveFile(const gchar *src, const gchar *dst)
71 tmpcmd = g_strdup_printf("mv '%s' '%s'", src, dst);
82 getExtension(gint format)
87 return EXTENSION_ILBC;
89 /* return EXTENSION_PCMA; */
92 /* return EXTENSION_PCMU; */
104 * @param filename the filename from save dialog
105 * @param tmpfile the tmpfile where the actual data is
106 * @param format format of the tmpfile
107 * @param newfile the file (filename + extension) where the data was saved is returned here
110 saveFile(const gchar *filename, const gchar *tmpfile, AudioFormat format, gchar **newfile)
113 GnomeVFSHandle *tmp_handle = NULL;
114 GnomeVFSHandle *to_handle = NULL;
117 if (!filename || !tmpfile)
120 ext = getExtension(format);
122 /* don't append extension if the filename already has it */
123 if (g_str_has_suffix(filename, ext))
124 newfile_tmp = g_strdup(filename);
126 newfile_tmp = g_strconcat(filename, ext, NULL);
128 g_assert(newfile_tmp);
130 if (strcmp(ext, EXTENSION_AU) == 0)
132 GnomeVFSFileSize len = 0;
133 guint32 encoding = 0;
135 GnomeVFSURI *uri = NULL;
139 len = getFileLength(tmpfile);
142 ULOG_WARN("%s: file length was zero", G_STRFUNC);
146 encoding = au_get_encoding(format);
150 /* open tmpfile(r) */
151 text_uri = g_strconcat("file://", tmpfile, NULL);
153 if (gnome_vfs_open(&tmp_handle, text_uri, GNOME_VFS_OPEN_READ) != GNOME_VFS_OK)
155 ULOG_WARN("%s: gnome_vfs_open() failed", G_STRFUNC);
163 /* open/create newfile for writing */
164 text_uri = g_strconcat("file://", newfile_tmp, NULL);
165 uri = gnome_vfs_uri_new(text_uri);
169 /* TODO: check for symlink and other dangerous stuff */
170 res = gnome_vfs_create_uri(&to_handle, uri,
171 GNOME_VFS_OPEN_WRITE,
173 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* perm */);
174 if (res != GNOME_VFS_OK)
176 ULOG_WARN("%s: gnome_vfs_create_uri() failed for '%s': %s", G_STRFUNC, gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE), gnome_vfs_result_to_string(res));
177 gnome_vfs_uri_unref(uri);
181 /* TODO: future work: get rate and channels from AudioFormatSpec */
182 written = au_write_copy(to_handle, encoding, DEFAULT_RATE, DEFAULT_CHANNELS, tmp_handle, len);
184 gnome_vfs_uri_unref(uri);
185 /* not very clever check but should catch most errors */
188 ULOG_WARN("%s: au_write_copy() failed", G_STRFUNC);
192 /* todo esta bien, close things and exit */
193 gnome_vfs_close(tmp_handle);
194 gnome_vfs_close(to_handle);
196 /* finally, place the filename into newfile */
197 *newfile = newfile_tmp;
203 /* everything but AU files are RAW, just rename */
204 if (moveFile(tmpfile, newfile_tmp))
206 *newfile = newfile_tmp;
215 gnome_vfs_close(tmp_handle);
218 gnome_vfs_close(to_handle);
223 /* don't touch newfile if we're returning false */
224 /* *newfile = NULL; */
229 /* openFile takes in the filename and returns:
230 * - whether it is supported format (the boolean return value)
231 * - audio format (FORMAT_XXX)
232 * - the tmpfile where the raw data is (or the same file name if the file was raw)
233 * TODO: add parameter (GError?) to report back an error string
235 gboolean openFile(const gchar *filename, AudioFormat *format, gchar **tmpfile)
237 if (g_strrstr(filename, EXTENSION_PCMA))
239 ULOG_INFO("%s() - file was %s, assuming raw %s, %d Hz, 1 ch", G_STRFUNC, EXTENSION_PCMA, GST_TYPE_PCMA, PCM_RATE);
240 *format = FORMAT_PCMA;
241 *tmpfile = g_strdup(filename);
243 else if (g_strrstr(filename, EXTENSION_PCMU))
245 ULOG_INFO("%s() - file was %s, assuming raw %s, %d Hz, 1 ch", G_STRFUNC, EXTENSION_PCMU, GST_TYPE_PCMU, PCM_RATE);
246 *format = FORMAT_PCMU;
247 *tmpfile = g_strdup(filename);
249 else if (g_strrstr(filename, EXTENSION_RAW))
251 ULOG_INFO("%s() - file was %s, assuming raw %s, %d Hz, 1 ch, 16-bit", G_STRFUNC, EXTENSION_RAW, GST_TYPE_PCM, PCM_RATE);
252 *format = FORMAT_PCM;
253 *tmpfile = g_strdup(filename);
255 else if (g_strrstr(filename, EXTENSION_MP3))
257 ULOG_INFO("%s() - file was %s", G_STRFUNC, EXTENSION_MP3);
258 *format = FORMAT_MP3;
259 *tmpfile = g_strdup(filename);
261 else if (g_strrstr(filename, EXTENSION_WAV))
263 ULOG_INFO("%s() - file was %s", G_STRFUNC, EXTENSION_WAV);
264 *format = FORMAT_WAV;
265 *tmpfile = g_strdup(filename);
267 else if (g_strrstr(filename, EXTENSION_ILBC))
269 ULOG_INFO("%s() - file was %s", G_STRFUNC, EXTENSION_ILBC);
270 *format = FORMAT_ILBC;
271 *tmpfile = g_strdup(filename);
273 else if (g_strrstr(filename, EXTENSION_AU) || g_strrstr(filename, EXTENSION_SND))
276 GnomeVFSHandle *from_handle, *tmp_handle;
278 guint32 fmt, rate, channels, data_size, data_offset;
283 ULOG_INFO("%s() - file was AU/SND", G_STRFUNC);
284 /* decode and extract raw AU data */
285 text_uri = g_strdup_printf("file://%s", filename);
286 if (gnome_vfs_open(&from_handle, text_uri, GNOME_VFS_OPEN_READ) != GNOME_VFS_OK)
288 ULOG_WARN("%s() - gnome_vfs_open() failed", G_STRFUNC);
294 if ((ret = au_get_info(from_handle, &fmt, &rate, &channels, &data_size, &data_offset)) <= 0)
296 ULOG_WARN("%s() - au_get_info() failed", G_STRFUNC);
297 gnome_vfs_close(from_handle);
301 ULOG_DEBUG("%s() - format: %u, rate: %u, channels: %u, data_size: %u, data_offset: %u", G_STRFUNC, fmt, rate, channels, data_size, data_offset);
302 if (rate != DEFAULT_RATE || channels != DEFAULT_CHANNELS || data_size == 0)
304 ULOG_WARN("%s() - unsupported format", G_STRFUNC);
305 gnome_vfs_close(from_handle);
312 text_uri = g_strdup_printf("file://%s/%s", get_default_dir(), DEFAULT_TMP_PCMA_FILE);
315 text_uri = g_strdup_printf("file://%s/%s", get_default_dir(), DEFAULT_TMP_PCMU_FILE);
319 text_uri = g_strdup_printf("file://%s/%s", get_default_dir(), DEFAULT_TMP_FILE);
323 uri = gnome_vfs_uri_new(text_uri);
327 /* TODO: check for symlink and other dangerous stuff */
328 res = gnome_vfs_create_uri(&tmp_handle, uri,
329 GNOME_VFS_OPEN_WRITE,
331 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* perm */);
332 if (res != GNOME_VFS_OK)
334 ULOG_WARN("%s() - gnome_vfs_create() failed for '%s': %s", G_STRFUNC, gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE), gnome_vfs_result_to_string(res));
335 gnome_vfs_uri_unref(uri);
339 ret = au_copy_data(tmp_handle, from_handle, data_offset /* from_offset */);
340 gnome_vfs_close(from_handle);
343 ULOG_WARN("%s() - couldn't copy file data to %s", G_STRFUNC, gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE));
344 gnome_vfs_uri_unref(uri);
348 /* everything ok, tmpfile created */
351 *tmpfile = g_strdup(gnome_vfs_uri_get_path(uri));
352 ULOG_DEBUG("%s() - created tmpfile '%s'", G_STRFUNC, *tmpfile);
354 gnome_vfs_uri_unref(uri);
359 ULOG_WARN("%s() - non-matching file name", G_STRFUNC);
365 GnomeVFSFileSize getFileLength(const gchar *file)
367 GnomeVFSFileInfo *info;
370 GnomeVFSFileSize ret = 0;
375 text_uri = g_strdup_printf("file://%s", file);
376 info = gnome_vfs_file_info_new();
377 res = gnome_vfs_get_file_info(text_uri,
379 GNOME_VFS_FILE_INFO_DEFAULT);
382 if (res == GNOME_VFS_OK && (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE))
388 ULOG_WARN("%s: couldn't get file size", G_STRFUNC);
392 gnome_vfs_file_info_unref(info);