~ubuntu-branches/debian/sid/bijiben/sid

« back to all changes in this revision

Viewing changes to src/libbiji/biji-note-id.c

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2013-03-26 21:19:36 UTC
  • Revision ID: package-import@ubuntu.com-20130326211936-tu8mpy82juohw8m2
Tags: upstream-3.8.0
ImportĀ upstreamĀ versionĀ 3.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* bjb-note-id.c
 
2
 * Copyright (C) Pierre-Yves LUYTEN 2012 <py@luyten.fr>
 
3
 * 
 
4
 * bijiben is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation, either version 3 of the License, or
 
7
 * (at your option) any later version.
 
8
 * 
 
9
 * bijiben is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
 * See the GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
#include "biji-note-id.h"
 
19
 
 
20
/* Properties */
 
21
enum {
 
22
  PROP_0,
 
23
  PROP_PATH,
 
24
  BIJI_ID_PROPERTIES
 
25
};
 
26
 
 
27
static GParamSpec *properties[BIJI_ID_PROPERTIES] = { NULL, };
 
28
 
 
29
struct _BijiNoteIDPrivate
 
30
{
 
31
  GFile * location;
 
32
  gchar * title ;
 
33
 
 
34
  GTimeVal last_change_date;
 
35
  GTimeVal last_metadata_change_date;
 
36
  GTimeVal create_date ;
 
37
};
 
38
 
 
39
#define NOTE_OBJ_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), NOTE_TYPE_OBJ, NoteObjPrivate))
 
40
 
 
41
G_DEFINE_TYPE (BijiNoteID, biji_note_id, G_TYPE_OBJECT);
 
42
 
 
43
static void
 
44
biji_note_id_init (BijiNoteID *self)
 
45
{
 
46
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, biji_note_id_get_type(),
 
47
                                            BijiNoteIDPrivate);
 
48
 
 
49
  self->priv->location = NULL;
 
50
  self->priv->title = NULL;
 
51
}
 
52
 
 
53
static void
 
54
biji_note_id_finalize (GObject *object)
 
55
{
 
56
  BijiNoteID *id = BIJI_NOTE_ID (object);
 
57
  BijiNoteIDPrivate *priv = id->priv;
 
58
 
 
59
  g_object_unref (priv->location);
 
60
  g_free (priv->title);
 
61
 
 
62
  G_OBJECT_CLASS (biji_note_id_parent_class)->finalize (object);
 
63
}
 
64
 
 
65
static void
 
66
biji_note_id_set_path (BijiNoteID *self, const gchar *path)
 
67
{
 
68
  g_warn_if_fail (!self->priv->location);
 
69
  self->priv->location = g_file_new_for_path (path);
 
70
}
 
71
 
 
72
static void
 
73
biji_note_id_set_property (GObject      *object,
 
74
                            guint         property_id,
 
75
                            const GValue *value,
 
76
                            GParamSpec   *pspec)
 
77
{
 
78
  BijiNoteID *self = BIJI_NOTE_ID (object);
 
79
 
 
80
 
 
81
  switch (property_id)
 
82
    {
 
83
    case PROP_PATH:
 
84
      biji_note_id_set_path (self,g_value_get_string (value));
 
85
      break;
 
86
    default:
 
87
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
88
      break;
 
89
    }
 
90
}
 
91
 
 
92
static void
 
93
biji_note_id_get_property (GObject    *object,
 
94
                            guint       property_id,
 
95
                            GValue     *value,
 
96
                            GParamSpec *pspec)
 
97
{
 
98
  BijiNoteID *self = BIJI_NOTE_ID (object);
 
99
 
 
100
  switch (property_id)
 
101
    {
 
102
    case PROP_PATH:
 
103
      g_value_set_object (value, g_file_get_basename (self->priv->location));
 
104
      break;
 
105
    default:
 
106
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
107
      break;
 
108
    }
 
109
}
 
110
 
 
111
static void
 
112
biji_note_id_class_init (BijiNoteIDClass *klass)
 
113
{
 
114
  GObjectClass* object_class = G_OBJECT_CLASS (klass);
 
115
 
 
116
  object_class->finalize = biji_note_id_finalize;
 
117
  object_class->get_property = biji_note_id_get_property;
 
118
  object_class->set_property = biji_note_id_set_property;
 
119
 
 
120
  properties[PROP_PATH] =
 
121
    g_param_spec_string("path",
 
122
                        "The note file",
 
123
                        "The location where the note is stored and saved",
 
124
                        NULL,
 
125
                        G_PARAM_READWRITE);
 
126
 
 
127
  g_object_class_install_properties (object_class, BIJI_ID_PROPERTIES, properties);
 
128
 
 
129
  g_type_class_add_private (klass, sizeof (BijiNoteIDPrivate));
 
130
}
 
131
 
 
132
gboolean
 
133
biji_note_id_equal (BijiNoteID *a, BijiNoteID *b)
 
134
{
 
135
  return g_file_equal (a->priv->location, b->priv->location);
 
136
}
 
137
 
 
138
gchar * 
 
139
biji_note_id_get_path (BijiNoteID* n)
 
140
{
 
141
  g_return_val_if_fail (BIJI_IS_NOTE_ID (n), NULL);
 
142
 
 
143
  return g_file_get_path (n->priv->location);
 
144
}
 
145
 
 
146
gchar *
 
147
biji_note_id_get_uuid (BijiNoteID *n)
 
148
{
 
149
  return g_file_get_basename (n->priv->location);
 
150
}
 
151
 
 
152
GFile *
 
153
biji_note_id_get_file (BijiNoteID *note)
 
154
{
 
155
  return note->priv->location;
 
156
}
 
157
 
 
158
void
 
159
biji_note_id_set_title  (BijiNoteID *n, gchar* title)
 
160
{
 
161
  if (n->priv->title)
 
162
    g_free (n->priv->title);
 
163
 
 
164
  n->priv->title = g_strdup (title);
 
165
}
 
166
 
 
167
gchar *
 
168
biji_note_id_get_title (BijiNoteID* n)
 
169
{
 
170
  return n->priv->title ;
 
171
}
 
172
 
 
173
static gboolean
 
174
set_date_from_string (gchar *iso8601, GTimeVal *date)
 
175
{
 
176
  g_return_val_if_fail (iso8601, FALSE);
 
177
  g_return_val_if_fail (date, FALSE);
 
178
 
 
179
  if (!g_time_val_from_iso8601 (iso8601, date))
 
180
  {
 
181
    g_get_current_time (date);
 
182
    return FALSE;
 
183
  }
 
184
 
 
185
  return TRUE;
 
186
}
 
187
 
 
188
gchar *
 
189
biji_note_id_get_last_change_date (BijiNoteID* n)
 
190
{
 
191
  g_return_val_if_fail (BIJI_IS_NOTE_ID (n), NULL);
 
192
 
 
193
  return g_time_val_to_iso8601 (&(n->priv->last_change_date));
 
194
}
 
195
 
 
196
void
 
197
biji_note_id_set_last_change_date_now (BijiNoteID *n)
 
198
{
 
199
  g_get_current_time(&(n->priv->last_change_date));
 
200
}
 
201
 
 
202
glong
 
203
biji_note_id_get_last_change_date_sec (BijiNoteID *n)
 
204
{
 
205
  g_return_val_if_fail (BIJI_IS_NOTE_ID (n), 0);
 
206
  
 
207
  return n->priv->last_change_date.tv_sec ;
 
208
}
 
209
 
 
210
gboolean
 
211
biji_note_id_set_last_change_date (BijiNoteID* n,gchar* date)
 
212
{
 
213
  return set_date_from_string(date,&(n->priv->last_change_date));
 
214
}
 
215
 
 
216
gchar *
 
217
biji_note_id_get_last_metadata_change_date(BijiNoteID* n)
 
218
{
 
219
  g_return_val_if_fail (BIJI_IS_NOTE_ID (n), NULL);
 
220
 
 
221
  return g_time_val_to_iso8601 (&n->priv->last_metadata_change_date);
 
222
}
 
223
 
 
224
gboolean
 
225
biji_note_id_set_last_metadata_change_date (BijiNoteID* n,gchar* date)
 
226
{
 
227
  return set_date_from_string(date,&(n->priv->last_metadata_change_date));
 
228
}
 
229
 
 
230
void
 
231
biji_note_id_set_last_metadata_change_date_now (BijiNoteID *n)
 
232
{
 
233
  g_get_current_time(&(n->priv->last_metadata_change_date));
 
234
}
 
235
 
 
236
gchar *
 
237
biji_note_id_get_create_date(BijiNoteID* n)
 
238
{
 
239
  g_return_val_if_fail (BIJI_IS_NOTE_ID (n), NULL);
 
240
 
 
241
  return g_time_val_to_iso8601 (&n->priv->create_date);
 
242
}
 
243
 
 
244
gboolean
 
245
biji_note_id_set_create_date (BijiNoteID* n,gchar* date)
 
246
{
 
247
  return set_date_from_string (date, &(n->priv->create_date));
 
248
}
 
249
 
 
250
void
 
251
biji_note_id_set_create_date_now (BijiNoteID* n)
 
252
{
 
253
  g_get_current_time (&(n->priv->create_date));
 
254
}