2 * Copyright © 2007,2008 daniel g. siegel <dgsiegel@gnome.org>
3 * Copyright © 2007,2008 Jaap Haitsma <jaap@haitsma.org>
4 * Copyright © 2008 Felix Kaser <f.kaser@gmx.net>
6 * Licensed under the GNU General Public License Version 2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <glib/gi18n-lib.h>
31 #include "cheese-fileutil.h"
34 * SECTION:cheese-file-util
35 * @short_description: File utility functions for Cheese
36 * @stability: Unstable
37 * @include: cheese/cheese-fileutil.h
39 * #CheeseFileUtil provides some helpful utility functions for looking up paths
40 * for photos and videos.
48 gchar
*burst_raw_name
;
49 } CheeseFileUtilPrivate
;
51 G_DEFINE_TYPE_WITH_PRIVATE (CheeseFileUtil
, cheese_fileutil
, G_TYPE_OBJECT
)
54 cheese_fileutil_get_path_before_224 (CheeseFileUtil
*fileutil
);
57 * cheese_fileutil_get_video_path:
58 * @fileutil: a #CheeseFileUtil
60 * Get the path where Cheese video files are stored.
62 * Returns: (transfer none) (type filename): the Cheese video path
65 cheese_fileutil_get_video_path (CheeseFileUtil
*fileutil
)
67 CheeseFileUtilPrivate
*priv
;
69 g_return_val_if_fail (CHEESE_IS_FILEUTIL (fileutil
), NULL
);
71 priv
= cheese_fileutil_get_instance_private (fileutil
);
73 return priv
->video_path
;
77 * cheese_fileutil_get_photo_path:
78 * @fileutil: a #CheeseFileUtil
80 * Get the path where Cheese photo files are stored.
82 * Returns: (transfer none) (type filename): the Cheese photo path
85 cheese_fileutil_get_photo_path (CheeseFileUtil
*fileutil
)
87 CheeseFileUtilPrivate
*priv
;
89 g_return_val_if_fail (CHEESE_IS_FILEUTIL (fileutil
), NULL
);
91 priv
= cheese_fileutil_get_instance_private (fileutil
);
93 return priv
->photo_path
;
97 * cheese_fileutil_get_path_before_224:
98 * @fileutil: a #CheeseFileUtil
100 * Returns: (transfer full) (type filename): the photo path for Cheese versions before 2.24
103 cheese_fileutil_get_path_before_224 (CheeseFileUtil
*fileutil
)
105 return g_build_filename (g_get_home_dir (), ".gnome2", "cheese", "media", NULL
);
109 * cheese_fileutil_get_new_media_filename:
110 * @fileutil: a #CheeseFileUtil
111 * @mode: the type of media to create a filename for
113 * Creates a filename for one of the three media types: photo, photo burst or
114 * video. If a filename for a photo burst image was previously created, this
115 * function increments the burst count automatically. To start a new burst,
116 * first call cheese_fileutil_reset_burst().
118 * Returns: (transfer full) (type filename): a new filename
121 cheese_fileutil_get_new_media_filename (CheeseFileUtil
*fileutil
, CheeseMediaMode mode
)
129 CheeseFileUtilPrivate
*priv
;
131 g_return_val_if_fail (CHEESE_IS_FILEUTIL (fileutil
), NULL
);
133 priv
= cheese_fileutil_get_instance_private (fileutil
);
135 datetime
= g_date_time_new_now_local ();
137 g_assert (datetime
!= NULL
);
139 time_string
= g_date_time_format (datetime
, "%F-%H%M%S");
140 g_date_time_unref (datetime
);
142 g_assert (time_string
!= NULL
);
146 case CHEESE_MEDIA_MODE_PHOTO
:
147 case CHEESE_MEDIA_MODE_BURST
:
148 path
= cheese_fileutil_get_photo_path (fileutil
);
150 case CHEESE_MEDIA_MODE_VIDEO
:
151 path
= cheese_fileutil_get_video_path (fileutil
);
154 g_assert_not_reached ();
157 g_mkdir_with_parents (path
, 0775);
161 case CHEESE_MEDIA_MODE_PHOTO
:
162 filename
= g_strdup_printf ("%s%s%s%s", path
, G_DIR_SEPARATOR_S
, time_string
, CHEESE_PHOTO_NAME_SUFFIX
);
164 case CHEESE_MEDIA_MODE_BURST
:
166 if (strlen (priv
->burst_raw_name
) == 0)
168 g_free (priv
->burst_raw_name
);
169 priv
->burst_raw_name
= g_strdup_printf ("%s%s%s", path
, G_DIR_SEPARATOR_S
, time_string
);
172 filename
= g_strdup_printf ("%s_%d%s", priv
->burst_raw_name
, priv
->burst_count
, CHEESE_PHOTO_NAME_SUFFIX
);
174 case CHEESE_MEDIA_MODE_VIDEO
:
175 filename
= g_strdup_printf ("%s%s%s%s", path
, G_DIR_SEPARATOR_S
, time_string
, CHEESE_VIDEO_NAME_SUFFIX
);
178 g_assert_not_reached ();
181 file
= g_file_new_for_path (filename
);
184 while (g_file_query_exists (file
, NULL
))
188 g_object_unref (file
);
193 case CHEESE_MEDIA_MODE_PHOTO
:
194 filename
= g_strdup_printf ("%s%s%s (%d)%s", path
, G_DIR_SEPARATOR_S
, time_string
, num
, CHEESE_PHOTO_NAME_SUFFIX
);
196 case CHEESE_MEDIA_MODE_BURST
:
197 filename
= g_strdup_printf ("%s_%d (%d)%s", priv
->burst_raw_name
, priv
->burst_count
, num
, CHEESE_PHOTO_NAME_SUFFIX
);
199 case CHEESE_MEDIA_MODE_VIDEO
:
200 filename
= g_strdup_printf ("%s%s%s (%d)%s", path
, G_DIR_SEPARATOR_S
, time_string
, num
, CHEESE_VIDEO_NAME_SUFFIX
);
203 g_assert_not_reached ();
206 file
= g_file_new_for_path (filename
);
209 g_free (time_string
);
210 g_object_unref (file
);
216 * cheese_fileutil_reset_burst:
217 * @fileutil: a #CheeseFileUtil
219 * Resets the burst counter, so that calling
220 * cheese_fileutil_get_new_media_filename() with a photo burst starts a new
224 cheese_fileutil_reset_burst (CheeseFileUtil
*fileutil
)
226 CheeseFileUtilPrivate
*priv
;
228 g_return_if_fail (CHEESE_IS_FILEUTIL (fileutil
));
230 priv
= cheese_fileutil_get_instance_private (fileutil
);
232 priv
->burst_count
= 0;
233 g_free (priv
->burst_raw_name
);
234 priv
->burst_raw_name
= g_strdup ("");
238 cheese_fileutil_finalize (GObject
*object
)
240 CheeseFileUtil
*fileutil
= CHEESE_FILEUTIL (object
);
241 CheeseFileUtilPrivate
*priv
= cheese_fileutil_get_instance_private (fileutil
);;
243 g_free (priv
->video_path
);
244 g_free (priv
->photo_path
);
245 g_free (priv
->burst_raw_name
);
246 G_OBJECT_CLASS (cheese_fileutil_parent_class
)->finalize (object
);
250 cheese_fileutil_class_init (CheeseFileUtilClass
*klass
)
252 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
254 object_class
->finalize
= cheese_fileutil_finalize
;
258 cheese_fileutil_init (CheeseFileUtil
*fileutil
)
260 CheeseFileUtilPrivate
*priv
= cheese_fileutil_get_instance_private (fileutil
);
264 priv
->burst_count
= 0;
265 priv
->burst_raw_name
= g_strdup ("");
267 settings
= g_settings_new ("org.gnome.Cheese");
269 priv
->video_path
= g_settings_get_string (settings
, "video-path");
270 priv
->photo_path
= g_settings_get_string (settings
, "photo-path");
272 /* Get the video path from GSettings, XDG or hardcoded. */
273 if (!priv
->video_path
|| !*priv
->video_path
)
275 const gchar
*video_path
;
277 video_path
= g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS
);
278 g_free (priv
->video_path
);
283 priv
->video_path
= g_build_filename (video_path
, _("Webcam"), NULL
);
287 /* Get "~/.gnome2/cheese/media". */
288 priv
->video_path
= cheese_fileutil_get_path_before_224 (fileutil
);
292 /* Get the photo path from GSettings, XDG or hardcoded. */
293 if (!priv
->photo_path
|| !*priv
->photo_path
)
295 const gchar
*photo_path
;
297 photo_path
= g_get_user_special_dir (G_USER_DIRECTORY_PICTURES
);
298 g_free (priv
->photo_path
);
303 priv
->photo_path
= g_build_filename (photo_path
, _("Webcam"), NULL
);
307 /* Get "~/.gnome2/cheese/media". */
308 priv
->photo_path
= cheese_fileutil_get_path_before_224 (fileutil
);
312 g_object_unref (settings
);
316 * cheese_fileutil_new:
318 * Create a new #CheeseFileUtil object.
320 * Returns: a new #CheeseFileUtil
323 cheese_fileutil_new (void)
325 static CheeseFileUtil
*fileutil
= NULL
;
327 if (fileutil
!= NULL
)
328 return g_object_ref (fileutil
);
330 fileutil
= g_object_new (CHEESE_TYPE_FILEUTIL
, NULL
);
331 g_object_add_weak_pointer (G_OBJECT (fileutil
),
332 (gpointer
) & fileutil
);