summaryrefslogtreecommitdiff
path: root/src/cheese-fileutil.c
blob: f18e6a5cf471fa6e35fe2c7a000521be8ee416bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright © 2007,2008 daniel g. siegel <dgsiegel@gnome.org>
 * Copyright © 2007,2008 Jaap Haitsma <jaap@haitsma.org>
 * Copyright © 2008 Felix Kaser <f.kaser@gmx.net>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
  #include <cheese-config.h>
#endif

#include <glib.h>
#include <gio/gio.h>
#include <string.h>

#include "cheese-fileutil.h"
#include "cheese-gconf.h"

G_DEFINE_TYPE (CheeseFileUtil, cheese_fileutil, G_TYPE_OBJECT)

#define CHEESE_FILEUTIL_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), CHEESE_TYPE_FILEUTIL, CheeseFileUtilPrivate))

typedef struct
{
  gchar *video_path;
  gchar *photo_path;
  gchar *log_path;
} CheeseFileUtilPrivate;

gchar *
cheese_fileutil_get_video_path (CheeseFileUtil *fileutil)
{
  CheeseFileUtilPrivate *priv = CHEESE_FILEUTIL_GET_PRIVATE (fileutil);

  gchar *path;

  path = priv->video_path;

  return path;
}

gchar *
cheese_fileutil_get_photo_path (CheeseFileUtil *fileutil)
{
  CheeseFileUtilPrivate *priv = CHEESE_FILEUTIL_GET_PRIVATE (fileutil);

  gchar *path;

  path = priv->photo_path;

  return path;
}

gchar *
cheese_fileutil_get_path_before_224 (CheeseFileUtil *fileutil)
{
  return g_strjoin (G_DIR_SEPARATOR_S, g_get_home_dir (), ".gnome2", "cheese", "media", NULL);
}

gchar *
cheese_fileutil_get_log_path (CheeseFileUtil *fileutil)
{
  CheeseFileUtilPrivate *priv = CHEESE_FILEUTIL_GET_PRIVATE (fileutil);

  gchar *path;

  path = priv->log_path;

  return path;
}

gchar *
cheese_fileutil_get_new_media_filename (CheeseFileUtil *fileutil, CheeseMediaMode mode)
{
  struct tm *ptr;
  time_t     tm;
  char       date[21];
  gchar     *path;
  char      *filename;
  GFile     *file;
  int        num;

  tm  = time (NULL);
  ptr = localtime (&tm);
  strftime (date, 20, "%F-%H%M%S", ptr);

  if (mode == CHEESE_MEDIA_MODE_PHOTO)
    path = cheese_fileutil_get_photo_path (fileutil);
  else
    path = cheese_fileutil_get_video_path (fileutil);

  if (mode == CHEESE_MEDIA_MODE_PHOTO)
    filename = g_strdup_printf ("%s%s%s%s", path, G_DIR_SEPARATOR_S, date, PHOTO_NAME_SUFFIX);
  else
    filename = g_strdup_printf ("%s%s%s%s", path, G_DIR_SEPARATOR_S, date, VIDEO_NAME_SUFFIX);

  file = g_file_new_for_path (filename);

  if (g_file_query_exists (file, NULL))
  {
    num = 1;
    if (mode == CHEESE_MEDIA_MODE_PHOTO)
      filename = g_strdup_printf ("%s%s%s (%d)%s", path, G_DIR_SEPARATOR_S, date, num, PHOTO_NAME_SUFFIX);
    else
      filename = g_strdup_printf ("%s%s%s (%d)%s", path, G_DIR_SEPARATOR_S, date, num, VIDEO_NAME_SUFFIX);

    file = g_file_new_for_path (filename);

    while (g_file_query_exists (file, NULL))
    {
      num++;
      if (mode == CHEESE_MEDIA_MODE_PHOTO)
        filename = g_strdup_printf ("%s%s%s (%d)%s", path, G_DIR_SEPARATOR_S, date, num, PHOTO_NAME_SUFFIX);
      else
        filename = g_strdup_printf ("%s%s%s (%d)%s", path, G_DIR_SEPARATOR_S, date, num, VIDEO_NAME_SUFFIX);

      file = g_file_new_for_path (filename);
    }
  }

  return filename;
}

static void
cheese_fileutil_finalize (GObject *object)
{
  CheeseFileUtil *fileutil;

  fileutil = CHEESE_FILEUTIL (object);
  CheeseFileUtilPrivate *priv = CHEESE_FILEUTIL_GET_PRIVATE (fileutil);

  g_free (priv->video_path);
  g_free (priv->photo_path);
  g_free (priv->log_path);
  G_OBJECT_CLASS (cheese_fileutil_parent_class)->finalize (object);
}

static void
cheese_fileutil_class_init (CheeseFileUtilClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = cheese_fileutil_finalize;

  g_type_class_add_private (klass, sizeof (CheeseFileUtilPrivate));
}

static void
cheese_fileutil_init (CheeseFileUtil *fileutil)
{
  CheeseFileUtilPrivate *priv = CHEESE_FILEUTIL_GET_PRIVATE (fileutil);

  CheeseGConf *gconf;

  gconf = cheese_gconf_new ();

  g_object_get (gconf, "gconf_prop_video_path", &priv->video_path, NULL);
  g_object_get (gconf, "gconf_prop_photo_path", &priv->photo_path, NULL);

  /* get the video path from gconf, xdg or hardcoded */
  if (!priv->video_path || strcmp (priv->video_path, "") == 0)
  {
    /* get xdg */
    priv->video_path = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS), "Webcam", NULL);
    if (strcmp (priv->video_path, "") == 0)
    {
      /* get "~/.gnome2/cheese/media" */
      priv->video_path = cheese_fileutil_get_path_before_224 (fileutil);
    }
  }

  /* get the photo path from gconf, xdg or hardcoded */
  if (!priv->photo_path || strcmp (priv->photo_path, "") == 0)
  {
    /* get xdg */
    priv->photo_path = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES), "Webcam", NULL);
    if (strcmp (priv->photo_path, "") == 0)
    {
      /* get "~/.gnome2/cheese/media" */
      priv->photo_path = cheese_fileutil_get_path_before_224 (fileutil);
    }
  }

  /* FIXME: use the xdg log path */
  priv->log_path = g_strjoin (G_DIR_SEPARATOR_S, g_get_home_dir (), ".gnome2", "cheese", NULL);

  g_object_unref (gconf);
}

CheeseFileUtil *
cheese_fileutil_new ()
{
  CheeseFileUtil *fileutil;

  fileutil = g_object_new (CHEESE_TYPE_FILEUTIL, NULL);
  return fileutil;
}