File: memo.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 (318 lines) | stat: -rw-r--r-- 7,960 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
/* memo.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 <ctype.h>
#include <pi-source.h>
#include <pi-socket.h>
#include <pi-memo.h>
#include <pi-dlp.h>
#include "memo.h"
#include "utils.h"
#include "log.h"
#include "prefs.h"
#include "libplugin.h"
#include "password.h"

#define MEMO_EOF 7

int memo_compare(const void *v1, const void *v2)
{
   MemoList **memol1, **memol2;
   char str1[52], str2[52];
   struct Memo *m1, *m2;
   int i;

   memol1=(MemoList **)v1;
   memol2=(MemoList **)v2;

   m1=&((*memol1)->mmemo.memo);
   m2=&((*memol2)->mmemo.memo);

   multibyte_safe_strncpy(str1, m1->text, 50);
   multibyte_safe_strncpy(str2, m2->text, 50);
   str1[50]='\0';
   str2[50]='\0';

   /* lower case the strings for a better compare */
   for (i=strlen(str1)-1; i >= 0; i--) {
      str1[i] = tolower(str1[i]);
   }
   for (i=strlen(str2)-1; i >= 0; i--) {
      str2[i] = tolower(str2[i]);
   }

   return strcoll(str2, str1);
}

int memo_sort(MemoList **memol, int sort_order)
{
   struct MemoAppInfo memo_ai;
   MemoList *temp_memol;
   MemoList **sort_memol;
   int count, i;

   if (sort_order==SORT_DESCENDING) {
      return 0;
   }

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

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

   /* Allocate an array to be qsorted */
   sort_memol = calloc(count, sizeof(MemoList *));
   if (!sort_memol) {
      jpilot_logf(LOG_WARN, "memo_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_memol=*memol; temp_memol; temp_memol=temp_memol->next, i++) {
      sort_memol[i] = temp_memol;
   }

   get_memo_app_info(&memo_ai);
   if (memo_ai.sortByAlpha==1) {
      /* qsort them */
      qsort(sort_memol, count, sizeof(MemoList *), memo_compare);
   }

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

   free(sort_memol);

   return 0;
}

int pc_memo_write(struct Memo *memo, PCRecType rt, unsigned char attrib,
		  unsigned int *unique_id)
{
   char record[65536];
   int rec_len;
   buf_rec br;
   long ivalue;
   long char_set;

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

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

   get_pref(PREF_MEMO32_MODE, &ivalue, NULL);
   if (ivalue) {
      jp_pc_write("Memo32DB", &br);
   } else {
      jp_pc_write("MemoDB", &br);
   }
   *unique_id = br.unique_id;

   return 0;
}

void free_MemoList(MemoList **memo)
{
   MemoList *temp_memo, *temp_memo_next;

   for (temp_memo = *memo; temp_memo; temp_memo=temp_memo_next) {
      free_Memo(&(temp_memo->mmemo.memo));
      temp_memo_next = temp_memo->next;
      free(temp_memo);
   }
   *memo = NULL;
}

int get_memo_app_info(struct MemoAppInfo *ai)
{
   int num,i;
   unsigned int rec_size;
   unsigned char *buf;
   long char_set;
   long ivalue;

   bzero(ai, sizeof(*ai));

   get_pref(PREF_MEMO32_MODE, &ivalue, NULL);
   if (ivalue) {
      jp_get_app_info("Memo32DB", &buf, &rec_size);
   } else {
      jp_get_app_info("MemoDB", &buf, &rec_size);
   }
   num = unpack_MemoAppInfo(ai, buf, rec_size);
   if (buf) {
      free(buf);
   }
   if (num <= 0) {
      jpilot_logf(LOG_WARN, _("Error reading"), "MemoDB.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_memos(MemoList **memo_list, int sort_order)
{
   return get_memos2(memo_list, sort_order, 1, 1, 1, CATEGORY_ALL);
}
/* 
 * sort_order: 0=descending, 1=ascending (memos are sorted if set in pdb file)
 * modified, deleted and private, 0 for no, 1 for yes, 2 for use prefs
 */
int get_memos2(MemoList **memo_list, int sort_order,
 	       int modified, int deleted, int privates, int category)
{
   GList *records;
   GList *temp_list;
   int recs_returned, i, num;
   struct Memo memo;
   MemoList *temp_memo_list;
   long keep_modified, keep_deleted;
   int keep_priv;
   long char_set;
   long ivalue;
   buf_rec *br;

   jpilot_logf(LOG_DEBUG, "get_memos2()\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;
   }

   *memo_list=NULL;
   recs_returned = 0;

   get_pref(PREF_MEMO32_MODE, &ivalue, NULL);
   if (ivalue) {
      num = jp_read_DB_files("Memo32DB", &records);
   } else {
      num = jp_read_DB_files("MemoDB", &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_Memo(&memo, br->buf, br->size);

      if (num <= 0) {
	 continue;
      }

      if ( ((br->attrib & 0x0F) != category) && category != CATEGORY_ALL) {
	 continue;
      }
      get_pref(PREF_CHAR_SET, &char_set, NULL);
      if (memo.text) charset_p2j(memo.text, strlen(memo.text)+1, char_set);

      temp_memo_list = malloc(sizeof(MemoList));
      if (!temp_memo_list) {
	 jpilot_logf(LOG_WARN, "get_memos2(): Out of memory\n");
	 break;
      }
      memcpy(&(temp_memo_list->mmemo.memo), &memo, sizeof(struct Memo));
      temp_memo_list->app_type = MEMO;
      temp_memo_list->mmemo.rt = br->rt;
      temp_memo_list->mmemo.attrib = br->attrib;
      temp_memo_list->mmemo.unique_id = br->unique_id;
      temp_memo_list->next = *memo_list;
      *memo_list = temp_memo_list;
      recs_returned++;
   }

   jp_free_DB_records(&records);

   memo_sort(memo_list, sort_order);

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

   return recs_returned;
}