File: todo.c

package info (click to toggle)
jpilot 0.99.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,808 kB
  • ctags: 1,967
  • sloc: ansic: 29,655; sh: 8,387; makefile: 352; sed: 93
file content (422 lines) | stat: -rw-r--r-- 10,676 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* todo.c
 * A module of J-Pilot http://jpilot.org
 * 
 * Copyright (C) 1999-2001 by Judd Montgomery
 *
 * 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; version 2 of the License.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"
#include "i18n.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pi-source.h>
#include <pi-socket.h>
#include <pi-todo.h>
#include <pi-dlp.h>
#include "utils.h"
#include "log.h"
#include "todo.h"
#include "prefs.h"
#include "libplugin.h"
#include "password.h"

#define TODO_EOF 7

static struct ToDoAppInfo *glob_Ptodo_app_info;
/*
 * sort by:
 * priority, due date
 * due date, priority
 * category, priority
 * category, due date
 */
int todo_compare(const void *v1, const void *v2)
{
   time_t t1, t2;
   int r;
   int cat1, cat2;
   ToDoList **todol1, **todol2;
   struct ToDo *todo1, *todo2;
   int sort_by_priority;

   todol1=(ToDoList **)v1;
   todol2=(ToDoList **)v2;

   todo1=&((*todol1)->mtodo.todo);
   todo2=&((*todol2)->mtodo.todo);

   sort_by_priority = glob_Ptodo_app_info->sortByPriority;

   cat1 = (*todol1)->mtodo.attrib & 0x0F;
   cat2 = (*todol2)->mtodo.attrib & 0x0F;

   if (sort_by_priority == 0) {
      /* due date, priority */
      if ( !(todo1->indefinite) && (todo2->indefinite) ) {
	 return 1;
      }
      if ( (todo1->indefinite) && !(todo2->indefinite) ) {
	 return -1;
      }
      if ( !(todo1->indefinite) && !(todo2->indefinite) ) {
	 t1 = mktime(&(todo1->due));
	 t2 = mktime(&(todo2->due));
	 if ( t1 < t2 ) {
	    return 1;
	 }
	 if ( t1 > t2 ) {
	    return -1;
	 }
      }
      if ( (todo1->priority) < (todo2->priority) ) {
	 return 1;
      }
      if ( (todo1->priority) > (todo2->priority) ) {
	 return -1;
      }
      /* If all else fails sort alphabetically */
      if ((todo1->description) && (todo2->description)) {
	 return -(strcoll(todo1->description,todo2->description));
      }
   }

   if (sort_by_priority == 1) {
      /* priority, due date */
      if ( (todo1->priority) < (todo2->priority) ) {
	 return 1;
      }
      if ( (todo1->priority) > (todo2->priority) ) {
	 return -1;
      }
      if ( (todo1->indefinite) && (todo2->indefinite) ) {
	 return 0;
      }
      if ( !(todo1->indefinite) && (todo2->indefinite) ) {
	 return 1;
      }
      if ( (todo1->indefinite) && !(todo2->indefinite) ) {
	 return -1;
      }
      t1 = mktime(&(todo1->due));
      t2 = mktime(&(todo2->due));
      if ( t1 < t2 ) {
	 return 1;
      }
      if ( t1 > t2 ) {
	 return -1;
      }
      /* If all else fails sort alphabetically */
      if ((todo1->description) && (todo2->description)) {
	 return -(strcoll(todo1->description,todo2->description));
      }
   }

   if (sort_by_priority == 2) {
      /* category, priority */
      r = strcoll(glob_Ptodo_app_info->category.name[cat1],
		 glob_Ptodo_app_info->category.name[cat2]);
      if (r) {
	 return -r;
      }
      if ( (todo1->priority) < (todo2->priority) ) {
	 return 1;
      }
      if ( (todo1->priority) > (todo2->priority) ) {
	 return -1;
      }
      /* If all else fails sort alphabetically */
      if ((todo1->description) && (todo2->description)) {
	 return -(strcoll(todo1->description,todo2->description));
      }
   }

   if (sort_by_priority == 3) {
      /* category, due date */
      r = strcoll(glob_Ptodo_app_info->category.name[cat1],
		 glob_Ptodo_app_info->category.name[cat2]);
      if (r) {
	 return -r;
      }
      if ( (todo1->indefinite) && (todo2->indefinite) ) {
	 return 0;
      }
      if ( !(todo1->indefinite) && (todo2->indefinite) ) {
	 return 1;
      }
      if ( (todo1->indefinite) && !(todo2->indefinite) ) {
	 return -1;
      }
      t1 = mktime(&(todo1->due));
      t2 = mktime(&(todo2->due));
      if ( t1 < t2 ) {
	 return 1;
      }
      if ( t1 > t2 ) {
	 return -1;
      }
      /* If all else fails sort alphabetically */
      if ((todo1->description) && (todo2->description)) {
	 return -(strcoll(todo1->description,todo2->description));
      }
   }

   return 0;
}

int todo_sort(ToDoList **todol, int sort_order)
{
   ToDoList *temp_todol;
   ToDoList **sort_todol;
   struct ToDoAppInfo ai;
   int count, i;

   /* Count the entries in the list */
   for (count=0, temp_todol=*todol; temp_todol; temp_todol=temp_todol->next, count++) {
      ;
   }

   if (count<2) {
      /* We don't have to sort less than 2 items */
      return 0;
   }

   get_todo_app_info(&ai);

   glob_Ptodo_app_info = &ai;

   /* Allocate an array to be qsorted */
   sort_todol = calloc(count, sizeof(ToDoList *));
   if (!sort_todol) {
      jpilot_logf(LOG_WARN, "todo_sort(): Out of Memory\n");
      return 0;
   }

   /* Set our array to be a list of pointers to the nodes in the linked list */
   for (i=0, temp_todol=*todol; temp_todol; temp_todol=temp_todol->next, i++) {
      sort_todol[i] = temp_todol;
   }

   /* qsort them */
   qsort(sort_todol, count, sizeof(ToDoList *), todo_compare);

   /* Put the linked list in the order of the array */
   if (sort_order==SORT_ASCENDING) {
      for (i=count-1; i>0; i--) {
	 sort_todol[i]->next=sort_todol[i-1];
      }
      sort_todol[0]->next = NULL;
      *todol = sort_todol[count-1];
   } else {
      /* Descending order */
      sort_todol[count-1]->next = NULL;
      for (i=count-1; i; i--) {
	 sort_todol[i-1]->next=sort_todol[i];
      }
      *todol = sort_todol[0];
   }

   free(sort_todol);

   return 0;
}

int pc_todo_write(struct ToDo *todo, PCRecType rt, unsigned char attrib,
		  unsigned int *unique_id)
{
   char record[65536];
   int rec_len;
   buf_rec br;
   long char_set;

   get_pref(PREF_CHAR_SET, &char_set, NULL);
   if (char_set != CHAR_SET_ENGLISH) {
      if (todo->description) charset_j2p(todo->description, strlen(todo->description)+1, char_set);
      if (todo->note) charset_j2p(todo->note, strlen(todo->note)+1, char_set);
   }

   rec_len = pack_ToDo(todo, record, 65535);
   if (!rec_len) {
      PRINT_FILE_LINE;
      jpilot_logf(LOG_WARN, "pack_ToDo %s\n", _("error"));
      return -1;
   }
   br.rt=rt;
   br.attrib = attrib;
   br.buf = record;
   br.size = rec_len;

   jp_pc_write("ToDoDB", &br);
   *unique_id = br.unique_id;

   return 0;
}

void free_ToDoList(ToDoList **todo)
{
   ToDoList *temp_todo, *temp_todo_next;

   for (temp_todo = *todo; temp_todo; temp_todo=temp_todo_next) {
      free_ToDo(&(temp_todo->mtodo.todo));
      temp_todo_next = temp_todo->next;
      free(temp_todo);
   }
   *todo = NULL;
}

int get_todo_app_info(struct ToDoAppInfo *ai)
{
   int num,i;
   unsigned int rec_size;
   unsigned char *buf;
   long char_set;

   bzero(ai, sizeof(*ai));

   jp_get_app_info("ToDoDB", &buf, &rec_size);
   num = unpack_ToDoAppInfo(ai, buf, rec_size);
   if (buf) {
      free(buf);
   }
   if (num <= 0) {
      jpilot_logf(LOG_WARN, _("Error reading"), "ToDoDB.pdb");
      return -1;
   }

   get_pref(PREF_CHAR_SET, &char_set, NULL);
   if (char_set != CHAR_SET_ENGLISH) {
      for (i = 0; i < 16; i++) {
	 if (ai->category.name[i][0] != '\0') {
	    charset_p2j(ai->category.name[i], 16, char_set);
	 }
      }
   }

   return 0;
}

int get_todos(ToDoList **todo_list, int sort_order)
{
   return get_todos2(todo_list, sort_order, 1, 1, 1, 1, CATEGORY_ALL);
}
/* 
 * sort_order: 0=descending,  1=ascending
 * modified, deleted and private, completed:
 *  0 for no, 1 for yes, 2 for use prefs
 */
int get_todos2(ToDoList **todo_list, int sort_order,
	       int modified, int deleted, int privates, int completed,
	       int category)
{
   GList *records;
   GList *temp_list;
   int recs_returned, i, num;
   struct ToDo todo;
   ToDoList *temp_todo_list;
   long keep_modified, keep_deleted, hide_completed;
   int keep_priv;
   buf_rec *br;
   long char_set;

   jpilot_logf(LOG_DEBUG, "get_todos2()\n");
   if (modified==2) {
      get_pref(PREF_SHOW_MODIFIED, &keep_modified, NULL);
   } else {
      keep_modified = modified;
   }
   if (deleted==2) {
      get_pref(PREF_SHOW_DELETED, &keep_deleted, NULL);
   } else {
      keep_deleted = deleted;
   }
   if (privates==2) {
      keep_priv = show_privates(GET_PRIVATES, NULL);
   } else {
      keep_priv = privates;
   }
   if (completed==2) {
      get_pref(PREF_HIDE_COMPLETED, &hide_completed, NULL);
   } else {
      hide_completed = !completed;
   }

   *todo_list=NULL;
   recs_returned = 0;

   num = jp_read_DB_files("ToDoDB", &records);
   /* Go to first entry in the list */
   for (temp_list = records; temp_list; temp_list = temp_list->prev) {
      records = temp_list;
   }
   for (i=0, temp_list = records; temp_list; temp_list = temp_list->next, i++) {
      if (temp_list->data) {
	 br=temp_list->data;
      } else {
	 continue;
      }
      if (!br->buf) {
	 continue;
      }

      if ( ((br->rt==DELETED_PALM_REC) && (!keep_deleted)) ||
	  ((br->rt==MODIFIED_PALM_REC) && (!keep_modified)) ) {
	 continue;
      }
      if ((keep_priv != SHOW_PRIVATES) && 
	  (br->attrib & dlpRecAttrSecret)) {
	 continue;
      }

      num = unpack_ToDo(&todo, br->buf, br->size);

      if (num <= 0) {
	 continue;
      }

      if ( ((br->attrib & 0x0F) != category) && category != CATEGORY_ALL) {
	 continue;
      }

      if (hide_completed && todo.complete) {
	 continue;
      }

      get_pref(PREF_CHAR_SET, &char_set, NULL);
      if (todo.description) charset_p2j(todo.description, strlen(todo.description)+1, char_set);
      if (todo.note) charset_p2j(todo.note, strlen(todo.note)+1, char_set);

      temp_todo_list = malloc(sizeof(ToDoList));
      if (!temp_todo_list) {
	 jpilot_logf(LOG_WARN, "get_todos2(): Out of memory\n");
	 break;
      }
      memcpy(&(temp_todo_list->mtodo.todo), &todo, sizeof(struct ToDo));
      temp_todo_list->app_type = TODO;
      temp_todo_list->mtodo.rt = br->rt;
      temp_todo_list->mtodo.attrib = br->attrib;
      temp_todo_list->mtodo.unique_id = br->unique_id;
      temp_todo_list->next = *todo_list;
      *todo_list = temp_todo_list;
      recs_returned++;
   }

   jp_free_DB_records(&records);

   todo_sort(todo_list, sort_order);

   jpilot_logf(LOG_DEBUG, "Leaving get_todos2()\n");

   return recs_returned;
}