File: vtodo.c

package info (click to toggle)
libgpevtype 0.50-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,636 kB
  • sloc: sh: 8,767; ansic: 1,245; xml: 208; makefile: 50
file content (253 lines) | stat: -rw-r--r-- 5,956 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* vtodo.c: Implementation of todo import and export functionality.
 * Copyright (C) 2004 Philip Blundell <philb@gnu.org>
 * Copyright (C) 2006 Neal H. Walfield <neal@gnu.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

/* XXX: This needs some serious love.  First of all, libtodo-db needs
   an overhaul.  We shouldn't be directly manipulating the DB but
   letting libtodo-db do that.  */

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <assert.h>

#include <sqlite.h>

#include <mimedir/mimedir-vtodo.h>
#include <gpe/todo-db.h>

#include "priv.h"

#define TODO_DB_NAME "/.gpe/todo"

struct tag_map
{
  GType type;
  gchar *tag;
  gchar *vc;
};

static struct tag_map map[] =
  {
    { G_TYPE_STRING, "summary", NULL },
    { G_TYPE_STRING, "description", NULL },
    { G_TYPE_STRING, "todoid", "uid" },
    { G_TYPE_INT, "priority", NULL },
    { G_TYPE_NONE, NULL, NULL }
  };

static gboolean
vtodo_interpret_tag (MIMEDirVTodo *todo, const char *tag, const char *value)
{
  struct tag_map *t = &map[0];
  while (t->tag)
    {
      if (!strcasecmp (t->tag, tag))
	{
	  if (t->type == G_TYPE_STRING)
	    g_object_set (G_OBJECT (todo), t->vc ? t->vc : t->tag, value, NULL);
	  else if (t->type == G_TYPE_INT) 
	    {
	      if (!strcasecmp (t->tag, "STATE"))
		    {
              gint val = atol(value);
              switch (val)
              {
                case NOT_STARTED:
		          g_object_set (G_OBJECT (todo), t->vc ? t->vc : t->tag, MIMEDIR_STATUS_NEEDS_ACTION, NULL);
                break;
                case IN_PROGRESS:
		          g_object_set (G_OBJECT (todo), t->vc ? t->vc : t->tag, MIMEDIR_STATUS_IN_PROCESS, NULL);
                break;
                case ABANDONED:
                case COMPLETED:
                  // Completed Task
		          g_object_set (G_OBJECT (todo), t->vc ? t->vc : t->tag, MIMEDIR_STATUS_COMPLETED, NULL);
		          g_object_set (G_OBJECT (todo), "dtcompleted", mimedir_datetime_new_from_time_t (time(NULL)), NULL);
                break;
                default:
		          g_object_set (G_OBJECT (todo), t->vc ? t->vc : t->tag, MIMEDIR_STATUS_NEEDS_ACTION, NULL);
                break;
              }
            }
          else
            g_object_set (G_OBJECT (todo), t->vc ? t->vc : t->tag, atoi (value), NULL);
	    } 
	  else
	    return FALSE;

	  return TRUE;
	}
      t++;
    }

  if (!strcasecmp (tag, "due"))
    {
      struct tm tm;

      memset (&tm, 0, sizeof (tm));
      if (strptime (value, "%F", &tm))
	{
	  MIMEDirDateTime *date;

	  date = mimedir_datetime_new_from_struct_tm (&tm);

	  g_object_set (G_OBJECT (todo), "due", date, NULL);
	}

      return TRUE;
    }
  if (!strcasecmp (tag, "modified"))
    {
      MIMEDirDateTime *date;

      date = mimedir_datetime_new_from_time_t (atoi (value));

      g_object_set (G_OBJECT (todo), "last-modified", date, NULL);
    }

  return FALSE;
}

MIMEDirVTodo *
vtodo_from_tags (GSList *tags)
{
  MIMEDirVTodo *vtodo = mimedir_vtodo_new ();

  while (tags)
    {
      gpe_tag_pair *p = tags->data;

      vtodo_interpret_tag (vtodo, p->tag, p->value);

      tags = tags->next;
    }

  return vtodo;
}

GSList *
vtodo_to_tags (MIMEDirVTodo *vtodo)
{
  GSList *data = NULL;
  struct tag_map *t = &map[0];

  while (t->tag)
    {
      if (t->type == G_TYPE_STRING)
	{
	  gchar *value;

	  g_object_get (G_OBJECT (vtodo), t->vc ? t->vc : t->tag, &value, NULL);

	  if (value)
	    data = gpe_tag_list_prepend (data, t->tag, g_strstrip (value));
	}
      else if (t->type == G_TYPE_INT)
	{
	  gint value;

	  g_object_get (G_OBJECT (vtodo), t->vc ? t->vc : t->tag, &value, NULL);

	  // Convert from MIMEDir's Status to GPE's state
	  if (! strcasecmp (t->tag, "STATE"))
        {
          switch (value)
            {
              case MIMEDIR_STATUS_COMPLETED:
                value = COMPLETED;
              break;
              case MIMEDIR_STATUS_IN_PROCESS:
                value = IN_PROGRESS;
              break;
              default:
                value = NOT_STARTED;
              break;
            }
        }
	  // The default priority of 0 seems to annoy GPE-Todo?
	  if (! strcasecmp (t->tag, "PRIORITY"))
	    value = (value >= 5) ? value : 5;
	  data = gpe_tag_list_prepend (data, t->tag, g_strdup_printf ("%d", value));
	}
      else
	abort ();

      t++;
    }

  /* DUE */
  MIMEDirDateTime *date;
  g_object_get (G_OBJECT (vtodo), "due", &date, NULL);
  if (date) {
    char d_buf[32];
    struct tm tm;
    mimedir_datetime_get_struct_tm(date, &tm);
    strftime (d_buf, sizeof (d_buf), "%F", &tm);
    data = gpe_tag_list_prepend (data, "due", g_strdup (d_buf));
  }


  return data;
}

gboolean
todo_import_from_vtodo (MIMEDirVTodo *todo, GError **error)
{
  sqlite *db;
  GSList *tags, *i;
  char *buf;
  const gchar *home;
  char *err = NULL;
  int id;

  home = g_get_home_dir ();
  
  buf = g_strdup_printf ("%s%s", home, TODO_DB_NAME);

  db = sqlite_open (buf, 0, &err);
  g_free (buf);

  if (db == NULL)
    {
      g_set_error (error, ERROR_DOMAIN (), 0, "%s", err);
      free (err);
      return FALSE;
    }
 
  if (sqlite_exec (db, "insert into todo_urn values (NULL)",
		   NULL, NULL, &err) != SQLITE_OK)
    {
      g_set_error (error, ERROR_DOMAIN (), 0, "%s", err);
      free (err);
      sqlite_close (db);
      return FALSE;
    }

  id = sqlite_last_insert_rowid (db);

  tags = vtodo_to_tags (todo);

  for (i = tags; i; i = i->next)
    {
      gpe_tag_pair *t = i->data;

      sqlite_exec_printf (db, "insert into todo values ('%d', '%q', '%q')", NULL, NULL, NULL,
			  id, t->tag, t->value);
    }
  
  gpe_tag_list_free (tags);

  sqlite_close (db);

  return TRUE;
}