File: Addressbook.cc

package info (click to toggle)
postman 2.0-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,036 kB
  • ctags: 3,078
  • sloc: cpp: 31,131; ansic: 1,948; sh: 787; makefile: 285
file content (384 lines) | stat: -rw-r--r-- 9,466 bytes parent folder | download | duplicates (3)
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
/*
Addressbook.cc
*/

#include "Addressbook.h"
#include "Utils.h"

extern Language *L;

/*
NOTE: The field Fcc will be for the addressbook name,
      in Pine is used for other thing.
*/


Addressbook::Addressbook (const char *fn, UserOptions *auo)
  {        
  MODIFIED = false;
  xstrncpy (fnaddressbook, MAXLENGTHLINE, fn);
  uo = auo;
  sortby = uo->getSort_ab();
  readAddressbook ();
  }
  
Addressbook::~Addressbook ()
  {
  if (MODIFIED) SaveAddressbook ();
  }
  
void Addressbook::setSortby (int asortby)
  {
  if (asortby != -1) sortby = asortby;
  uo->setSort_ab (sortby);  
  }  
  
void Addressbook::initEntry (TEntry *aentry)
  {
  aentry->Nickname   = ""; 
  aentry->Fullname   = ""; 
  aentry->Addresses  = "";    
  aentry->Fcc        = ""; 
  aentry->Comment    = ""; 
  }  

//minickname	mifullname	(miaddresses,otra)	mifcc	micomment 
void Addressbook::readAddressbook ()
  {
  agenda.Clear();
  if (!FileExists(fnaddressbook)) return;
  XString XS;
  FILE *FR;
  TBuffer linea, token;
  TEntry aentry;
  FR = fopen (fnaddressbook, "r");
  XS = "";
  while (fgets (linea, CMAXBUFFER, FR) != NULL)
    {
    QuitaRetornoCarroDeLinea (linea);
    if (linea[0] == ' ')
      {
      trim (linea);
      XS += linea;
      }
    else
      {
      if (!IsEmpty(XS.cstr()) || (strcmp (XS.cstr(), "\t\t\t\t") == 0)) 
        {
        aentry.Nickname   = getToken (XS.cstr(), 1, token); 
        aentry.Fullname   = getToken (XS.cstr(), 2, token); 
        aentry.Addresses  = getToken (XS.cstr(), 3, token); 
        if ((aentry.Addresses.startsWith("(") == true) && (aentry.Addresses.endsWith(")") == true))
          {
          aentry.Addresses  = aentry.Addresses.substring (1, aentry.Addresses.length() - 1);
          }
        aentry.Fcc        = getToken (XS.cstr(), 4, token); 
        aentry.Comment    = getToken (XS.cstr(), 5, token); 
        agenda.Add (aentry);
        }
      XS = linea;
      }
    }
  fclose (FR);  
  if (XS.length() > 0) 
    {
    if (!IsEmpty(XS.cstr()) || (strcmp (XS.cstr(), "\t\t\t\t") == 0)) 
      {
      aentry.Nickname   = getToken (XS.cstr(), 1, token); 
      aentry.Fullname   = getToken (XS.cstr(), 2, token); 
      aentry.Addresses  = getToken (XS.cstr(), 3, token); 
      if ((aentry.Addresses.startsWith("(") == true) && (aentry.Addresses.endsWith(")") == true))
        {
        aentry.Addresses  = aentry.Addresses.substring (1, aentry.Addresses.length() - 1);
        }
      aentry.Fcc        = getToken (XS.cstr(), 4, token); 
      aentry.Comment    = getToken (XS.cstr(), 5, token); 
      agenda.Add (aentry);    
      }
    }
  }    

char *Addressbook::getToken (const char *linea, int numtoken, TBuffer token)
  {
  char c;
  int numtab, poschar;
  numtab = 0;
  poschar = 0;
  token [0] = '\0';
  for (xuint L = 0; L < strlen(linea); ++L)
    {
    c = linea[L];
    if (c == TAB) 
      {
      ++numtab;
      if (numtab == numtoken)
        {
        trim (token);
        return token;
        } 
      else 
        {
        poschar = 0;
        token [0] = '\0'; 
        }
      }
    else 
      {
      token[poschar] = c;
      token[poschar + 1] = '\0';
      ++poschar;
      }
    }
  if ((strlen (token) > 0) && (numtab == numtoken - 1))
    {
    trim (token);
    return token;
    }
  return token;
  }

bool Addressbook::SaveAddressbook ()
  {
  return (SaveAddressbookAs (fnaddressbook));
  }  
  
bool Addressbook::SaveAddressbookAs (const char *fn)
  {
  XString XS;
  FILE *FW;
  TFileName fntmp;
  bool b;
  
  MakeTmpName (fntmp, CMAXFILENAME);
  FW = fopen (fntmp, "w");
  if (FW == NULL) return false;
  for (int i = 0; i < agenda.Count (); ++i)
    {  
    if (agenda[i].Addresses.indexOf(',') > -1) 
      {
      XS = "("; XS += agenda[i].Addresses; XS += (")");
      }
    else XS = agenda[i].Addresses;
    fprintf (FW, "%s\t%s\t%s\t%s\t%s\n", agenda[i].Nickname.cstr(),  
                                         agenda[i].Fullname.cstr(),
                                         XS.cstr(), 
                                         agenda[i].Fcc.cstr(),
                                         agenda[i].Comment.cstr());    
    }  
  fclose (FW);  
  b = CopyFileOverWrite (fntmp, fn);
  if (b == false)
    {
    LOG ("Error copying the tmp addressbook '%s' to '%s'", fntmp, fn);
    return false;
    }
  else
    {
    DeleteFile (fntmp);
    return true;
    }
  }  
  
void Addressbook::Clear ()
  {
  agenda.Clear(); 
  MODIFIED = true; 
  }

int Addressbook::Count (void)
  {
  return agenda.Count();
  }    

const char *Addressbook::getNickname (int pos)
  {
  return agenda[pos].Nickname.cstr();
  }

const char *Addressbook::getFullname (int pos)
  {
  return agenda[pos].Fullname.cstr();
  }  

const char *Addressbook::getAddresses (int pos)
  {
  return agenda[pos].Addresses.cstr();
  }  

char *Addressbook::getMultiAddresses (const char *positions)
  {
  StringList *SL;
  TBuffer abuff;
  UINT ppos;
  
  SL = new StringList ();
  initStr (abuff);
  SeparaTokensEnStringList (positions, ',', SL);
  if (SL->Count() > 0)
    {
    for (int i=0; i < SL->Count(); ++i)
      {
      ppos = xatoidef (SL->getString(i).cstr(), 0);
      if (ppos > 0) 
        {
        xstrncat (abuff, CMAXBUFFER, agenda[ppos - 1].Addresses.cstr()); 
        }
      if (i < SL->Count() - 1) 
        {
        xstrncat (abuff, CMAXBUFFER, ",");
        }
      }
    }
  delete SL;
  return xstrdup(abuff);
  }  

bool Addressbook::DeleteEntries (const char *positions)
  {
  StringList *SL;
  UINT ppos;
  SL = new StringList ();
  SeparaTokensEnStringList (positions, ',', SL);
  if (SL->Count() <= 0) {delete SL; return false;}
  MODIFIED = true;
  for (int i=SL->Count() - 1; i>=0; --i)
    {
    ppos = xatoidef (SL->getString(i).cstr(), 0);    
    agenda.removeElementAt (ppos);
    }
  delete SL;
  return true;
  } 
    
const char *Addressbook::getFcc (int pos)
  {                    
  return agenda[pos].Fcc.cstr();
  }  

const char *Addressbook::getComment (int pos)
  {
  return agenda[pos].Comment.cstr();
  }  
 
void Addressbook::setNickname (int pos, char *value)
  {
  agenda[pos].Nickname = value;
  }

void Addressbook::setFullname (int pos, char *value)
  {
  agenda[pos].Fullname = value;
  }  

void Addressbook::setAddresses (int pos, char *value)
  {
  agenda[pos].Addresses = value;
  }  

void Addressbook::setFcc (int pos, char *value)
  {                    
  agenda[pos].Fcc = value;
  }  

void Addressbook::setComment (int pos, char *value)
  {
  agenda[pos].Comment = value;
  }    

void Addressbook::UpdateEntryFromFile (int numentry, char *fn)
  {
  if (numentry == -1) return;
  TBuffer linea;
  int cont;
  FILE *FR;
  FR = fopen (fn, "r");
  if (FR == NULL) return;
  cont = 0;
  while (fgets (linea, MAXLENGTHLINE, FR) != NULL) 
    {
    ++cont;
    QuitaRetornoCarroDeLinea (linea); 
    switch (cont)
      {
      case 1: setNickname  (numentry, linea); break;
      case 2: setFullname  (numentry, linea); break;
      case 3: setAddresses (numentry, linea); break;
      case 4: setFcc       (numentry, linea); break;
      case 5: setComment   (numentry, linea); break;
      }    
    } 
  fclose (FR);
  DeleteFile (fn);
  MODIFIED = true;
  }

int Addressbook::AddNewEntry (void)
  {
  TEntry aentry;
  initEntry (&aentry);
  agenda.addElement (aentry);
  MODIFIED = true;
  return (agenda.Count () - 1);
  }

int Addressbook::AddNewEntry (const char *addresses)
  {
  TEntry aentry;
  initEntry (&aentry);
  aentry.Addresses = addresses;
  agenda.addElement (aentry);
  MODIFIED = true;
  return (agenda.Count () - 1);
  }

void FlipEntries (const void *e1, const void *e2)
  {
  TEntry temp;
  temp.Nickname  = PEntry (e1)->Nickname;
  temp.Fullname  = PEntry (e1)->Fullname;  
  temp.Addresses = PEntry (e1)->Addresses;
  temp.Fcc       = PEntry (e1)->Fcc;
  temp.Comment   = PEntry (e1)->Comment;
  PEntry (e1)->Nickname  = PEntry (e2)->Nickname;  PEntry (e2)->Nickname  = temp.Nickname;
  PEntry (e1)->Fullname  = PEntry (e2)->Fullname;  PEntry (e2)->Fullname  = temp.Fullname;  
  PEntry (e1)->Addresses = PEntry (e2)->Addresses; PEntry (e2)->Addresses = temp.Addresses;  
  PEntry (e1)->Fcc       = PEntry (e2)->Fcc;       PEntry (e2)->Fcc       = temp.Fcc;  
  PEntry (e1)->Comment   = PEntry (e2)->Comment;   PEntry (e2)->Comment   = temp.Comment;  
  }
   
int CompareEntriesByNickname (const void *e1, const void *e2)
  {
  return (strcasecmp (PEntry (e1)->Nickname.cstr(), PEntry(e2)->Nickname.cstr()));
  }

int CompareEntriesByFullname (const void *e1, const void *e2)
  {
  return (strcasecmp (PEntry (e1)->Fullname.cstr(), PEntry(e2)->Fullname.cstr()));
  }

int CompareEntriesByFcc (const void *e1, const void *e2)
  {
  return (strcasecmp (PEntry (e1)->Fcc.cstr(), PEntry(e2)->Fcc.cstr()));
  }
   
int CompareEntriesByAddresses (const void *e1, const void *e2)
  {
  return (strcasecmp (PEntry (e1)->Addresses.cstr(), PEntry(e2)->Addresses.cstr()));
  }   
   
void Addressbook::SORT (int asortby)            
  { 
  setSortby (asortby);
  switch (sortby)
    {
    case 0:  agenda.SORT (CompareEntriesByNickname,  FlipEntries); break;
    case 1:  agenda.SORT (CompareEntriesByFullname,  FlipEntries); break;
    case 2:  agenda.SORT (CompareEntriesByAddresses, FlipEntries); break;
    case 3:  agenda.SORT (CompareEntriesByFcc, FlipEntries); break;
    default: agenda.SORT (CompareEntriesByNickname,  FlipEntries); break;
    }
  MODIFIED = true; 
  }