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; */
102 * @param filename the filename from save dialog
103 * @param tmpfile the tmpfile where the actual data is
104 * @param format format of the tmpfile
105 * @param newfile the file (filename + extension) where the data was saved is returned here
108 saveFile(const gchar *filename, const gchar *tmpfile, AudioFormat format, gchar **newfile)
111 GnomeVFSHandle *tmp_handle = NULL;
112 GnomeVFSHandle *to_handle = NULL;
115 if (!filename || !tmpfile)
118 ext = getExtension(format);
120 /* don't append extension if the filename already has it */
121 if (g_str_has_suffix(filename, ext))
122 newfile_tmp = g_strdup(filename);
124 newfile_tmp = g_strconcat(filename, ext, NULL);
126 g_assert(newfile_tmp);
128 if (strcmp(ext, EXTENSION_AU) == 0)
130 GnomeVFSFileSize len = 0;
131 guint32 encoding = 0;
133 GnomeVFSURI *uri = NULL;
137 len = getFileLength(tmpfile);
140 ULOG_WARN("%s: file length was zero", G_STRFUNC);
144 encoding = au_get_encoding(format);
148 /* open tmpfile(r) */
149 text_uri = g_strconcat("file://", tmpfile, NULL);
151 if (gnome_vfs_open(&tmp_handle, text_uri, GNOME_VFS_OPEN_READ) != GNOME_VFS_OK)
153 ULOG_WARN("%s: gnome_vfs_open() failed", G_STRFUNC);
161 /* open/create newfile for writing */
162 text_uri = g_strconcat("file://", newfile_tmp, NULL);
163 uri = gnome_vfs_uri_new(text_uri);
167 /* TODO: check for symlink and other dangerous stuff */
168 res = gnome_vfs_create_uri(&to_handle, uri,
169 GNOME_VFS_OPEN_WRITE,
171 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* perm */);
172 if (res != GNOME_VFS_OK)
174 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));
175 gnome_vfs_uri_unref(uri);
179 /* TODO: future work: get rate and channels from AudioFormatSpec */
180 written = au_write_copy(to_handle, encoding, DEFAULT_RATE, DEFAULT_CHANNELS, tmp_handle, len);
182 gnome_vfs_uri_unref(uri);
183 /* not very clever check but should catch most errors */
186 ULOG_WARN("%s: au_write_copy() failed", G_STRFUNC);
190 /* todo esta bien, close things and exit */
191 gnome_vfs_close(tmp_handle);
192 gnome_vfs_close(to_handle);
194 /* finally, place the filename into newfile */
195 *newfile = newfile_tmp;
201 /* everything but AU files are RAW, just rename */
202 if (moveFile(tmpfile, newfile_tmp))
204 *newfile = newfile_tmp;
213 gnome_vfs_close(tmp_handle);
216 gnome_vfs_close(to_handle);
221 /* don't touch newfile if we're returning false */
222 /* *newfile = NULL; */
227 /* openFile takes in the filename and returns:
228 * - whether it is supported format (the boolean return value)
229 * - audio format (FORMAT_XXX)
230 * - the tmpfile where the raw data is (or the same file name if the file was raw)
231 * TODO: add parameter (GError?) to report back an error string
233 gboolean openFile(const gchar *filename, AudioFormat *format, gchar **tmpfile)
235 if (g_strrstr(filename, EXTENSION_PCMA))
237 ULOG_INFO("%s() - file was %s, assuming raw %s, %d Hz, 1 ch", G_STRFUNC, EXTENSION_PCMA, GST_TYPE_PCMA, PCM_RATE);
238 *format = FORMAT_PCMA;
239 *tmpfile = g_strdup(filename);
241 else if (g_strrstr(filename, EXTENSION_PCMU))
243 ULOG_INFO("%s() - file was %s, assuming raw %s, %d Hz, 1 ch", G_STRFUNC, EXTENSION_PCMU, GST_TYPE_PCMU, PCM_RATE);
244 *format = FORMAT_PCMU;
245 *tmpfile = g_strdup(filename);
247 else if (g_strrstr(filename, EXTENSION_RAW))
249 ULOG_INFO("%s() - file was %s, assuming raw %s, %d Hz, 1 ch, 16-bit", G_STRFUNC, EXTENSION_RAW, GST_TYPE_PCM, PCM_RATE);
250 *format = FORMAT_PCM;
251 *tmpfile = g_strdup(filename);
253 else if (g_strrstr(filename, EXTENSION_MP3))
255 ULOG_INFO("%s() - file was %s", G_STRFUNC, EXTENSION_MP3);
256 *format = FORMAT_MP3;
257 *tmpfile = g_strdup(filename);
259 else if (g_strrstr(filename, EXTENSION_WAV))
261 ULOG_INFO("%s() - file was %s", G_STRFUNC, EXTENSION_WAV);
262 *format = FORMAT_WAV;
263 *tmpfile = g_strdup(filename);
265 else if (g_strrstr(filename, EXTENSION_ILBC))
267 ULOG_INFO("%s() - file was %s", G_STRFUNC, EXTENSION_ILBC);
268 *format = FORMAT_ILBC;
269 *tmpfile = g_strdup(filename);
271 else if (g_strrstr(filename, EXTENSION_AU) || g_strrstr(filename, EXTENSION_SND))
274 GnomeVFSHandle *from_handle, *tmp_handle;
276 guint32 fmt, rate, channels, data_size, data_offset;
281 ULOG_INFO("%s() - file was AU/SND", G_STRFUNC);
282 /* decode and extract raw AU data */
283 text_uri = g_strdup_printf("file://%s", filename);
284 if (gnome_vfs_open(&from_handle, text_uri, GNOME_VFS_OPEN_READ) != GNOME_VFS_OK)
286 ULOG_WARN("%s() - gnome_vfs_open() failed", G_STRFUNC);
292 if ((ret = au_get_info(from_handle, &fmt, &rate, &channels, &data_size, &data_offset)) <= 0)
294 ULOG_WARN("%s() - au_get_info() failed", G_STRFUNC);
295 gnome_vfs_close(from_handle);
299 ULOG_DEBUG("%s() - format: %u, rate: %u, channels: %u, data_size: %u, data_offset: %u", G_STRFUNC, fmt, rate, channels, data_size, data_offset);
300 if (rate != DEFAULT_RATE || channels != DEFAULT_CHANNELS || data_size == 0)
302 ULOG_WARN("%s() - unsupported format", G_STRFUNC);
303 gnome_vfs_close(from_handle);
310 text_uri = g_strdup_printf("file://%s/%s", get_default_dir(), DEFAULT_TMP_PCMA_FILE);
313 text_uri = g_strdup_printf("file://%s/%s", get_default_dir(), DEFAULT_TMP_PCMU_FILE);
317 text_uri = g_strdup_printf("file://%s/%s", get_default_dir(), DEFAULT_TMP_FILE);
321 uri = gnome_vfs_uri_new(text_uri);
325 /* TODO: check for symlink and other dangerous stuff */
326 res = gnome_vfs_create_uri(&tmp_handle, uri,
327 GNOME_VFS_OPEN_WRITE,
329 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /* perm */);
330 if (res != GNOME_VFS_OK)
332 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));
333 gnome_vfs_uri_unref(uri);
337 ret = au_copy_data(tmp_handle, from_handle, data_offset /* from_offset */);
338 gnome_vfs_close(from_handle);
341 ULOG_WARN("%s() - couldn't copy file data to %s", G_STRFUNC, gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE));
342 gnome_vfs_uri_unref(uri);
346 /* everything ok, tmpfile created */
349 *tmpfile = g_strdup(gnome_vfs_uri_get_path(uri));
350 ULOG_DEBUG("%s() - created tmpfile '%s'", G_STRFUNC, *tmpfile);
352 gnome_vfs_uri_unref(uri);
357 ULOG_WARN("%s() - non-matching file name", G_STRFUNC);
363 GnomeVFSFileSize getFileLength(const gchar *file)
365 GnomeVFSFileInfo *info;
368 GnomeVFSFileSize ret = 0;
373 text_uri = g_strdup_printf("file://%s", file);
374 info = gnome_vfs_file_info_new();
375 res = gnome_vfs_get_file_info(text_uri,
377 GNOME_VFS_FILE_INFO_DEFAULT);
380 if (res == GNOME_VFS_OK && (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE))
386 ULOG_WARN("%s: couldn't get file size", G_STRFUNC);
390 gnome_vfs_file_info_unref(info);