kaddressbook

pab_mapihd.cpp
1 /***************************************************************************
2  mapihd.cpp - description
3  -------------------
4  begin : Tue Jul 25 2000
5  copyright : (C) 2000 by Hans Dijkema
6  email : kmailcvt@hum.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "pab_pablib.h"
19 
20 bool operator < (mapitag_t & a,mapitag_t & b) { return a._order<b._order; }
21 bool operator > (mapitag_t & a,mapitag_t & b) { return a._order>b._order; }
22 bool operator == (mapitag_t & a,mapitag_t & b) { return a._order==b._order; }
23 
24 static word_t
25 map_givenname[]=
26  { pr_givenname,
27  SET_MS_GIVEN_NAME,
28  0
29  },
30 map_email[]=
31  { pr_email,
32  SET_MS_EMAIL,
33  0
34  },
35 map_firstname[]=
36  { pr_firstname,
37  SET_MS_FIRSTNAME,
38  0
39  },
40 map_lastname[]=
41  { pr_lastname,
42  SET_MS_LASTNAME,
43  0
44  },
45 map_additionalname[]=
46  { pr_additionalname,
47  SET_MS_MIDDLENAME,
48  0
49  },
50 map_title[]=
51  { pr_title,
52  SET_MS_TITLE,
53  0
54  },
55 map_address[]=
56  { pr_address,
57  SET_MS_ADDRESS,
58  0
59  },
60 map_zip[]=
61  { pr_zip,
62  SET_MS_ZIP,
63  0
64  },
65 map_state[]=
66  { pr_state,
67  SET_MS_STATE,
68  0
69  },
70 map_town[]=
71  { pr_town,
72  SET_MS_TOWN,
73  0
74  },
75 map_country[]=
76  { pr_country,
77  SET_MS_COUNTRY,
78  0
79  },
80 map_tel[]=
81  { pr_tel,
82  SET_MS_TEL,
83  0
84  },
85 map_mobile[]=
86  { pr_mobile,
87  SET_MS_MOBILE,
88  0
89  },
90 map_fax[]=
91  { pr_fax,
92  SET_MS_FAX,
93  0
94  },
95 map_job[]=
96  { pr_job,
97  HP_OPENMAIL_JOB,
98  0
99  },
100 map_organization[]=
101  { pr_organization,
102  SET_MS_ORGANIZATION,
103  HP_OPENMAIL_ORGANIZATION,
104  0
105  },
106 map_department[]=
107  { pr_department,
108  SET_MS_DEPARTMENT,
109  HP_OPENMAIL_DEPARTMENT,
110  0
111  },
112 map_subdep[]=
113  { pr_subdep,
114  HP_OPENMAIL_SUBDEP,
115  0
116  },
117 map_notes[]=
118  { pr_notes,
119  SET_MS_COMMENT,
120  0
121  },
122 map_notused[]=
123  { pr_notused,
124  HP_OPENMAIL_LOCATION_OF_WORK, // location of work
125  SET_NOT_USED,
126  0
127  };
128 
129 
130 static word_t *mapi_map[]={ map_givenname, map_email,
131  map_firstname, map_lastname, map_additionalname,map_title,
132  map_address, map_town, map_zip, map_state, map_country,
133  map_tel, map_mobile, map_fax,
134  map_organization, map_department, map_subdep, map_job,
135  map_notes,
136  map_notused,
137  NULL
138  };
139 
140 pabrec_entry mapitag_t::matchTag(void)
141 {
142 int i,j;
143 pabrec_entry e=pr_unknown;
144 
145  for(i=0;mapi_map[i]!=NULL && e==pr_unknown;i++) {
146  for(j=1;mapi_map[i][j]!=0 && _tag!=mapi_map[i][j];j++);
147  if (mapi_map[i][j]!=0) {
148  e=(pabrec_entry) mapi_map[i][0];
149  }
150  }
151 return e;
152 }
153 
154 pabfields_t::pabfields_t(pabrec & R, TQWidget * /*parent*/)
155 {
156  // Skip the first two words, because they're always the
157  // same 000c 0014 ==> 0014 gives us the types, so we
158  // parse from 0014 till the next offset and order the tags.
159 
160  int mb,me;
161  uint i,k;
162  content_t _tag,_order;
163 
164  mb=R[1];
165  me=R[2];
166 
167  while (mb<me) {
168  _tag=R.read(mb);mb+=sizeof(_tag);
169  _order=R.read(mb);mb+=sizeof(_order);
170 
171  {mapitag_t mt(_tag,_order);
172  tags[tags.size()]=mt;
173  context_tags[context_tags.size()]=mt;
174  }
175  }
176  tags.sort();
177 
178  // get the right entries now
179 
180  for(i=2,k=0;i<R.N() && k<tags.size();i++,k++) {
181  if (!isUsed(k)) { i-=1; }
182  else {pabrec_entry e;
183  TQString E;
184 
185  e=isWhat(k);
186  E=R.getEntry(i);
187  { TQString s=E;
188  s=s.stripWhiteSpace();
189  E=s;
190  }
191 
192  /*
193  { char m[1024];
194  snprintf(m, sizeof(m), "%d %d %04x %08lx %d %s %d %d",i,k,literal(k),order(k),e,E.latin1(),E[0].latin1(),E.length());
195  info->addLog(m);
196  }
197  */
198 
199  if (!E.isEmpty()) {
200 
201  switch (e) {
202  case pr_givenname: givenName=E;
203  break;
204  case pr_email: email=E;
205  break;
206  case pr_firstname: firstName=E;
207  break;
208  case pr_additionalname: additionalName=E;
209  break;
210  case pr_lastname: lastName=E;
211  break;
212  case pr_title: title=E;
213  break;
214  case pr_address: address=E;
215  break;
216  case pr_town: town=E;
217  break;
218  case pr_state: state=E;
219  break;
220  case pr_zip: zip=E;
221  break;
222  case pr_country: country=E;
223  break;
224  case pr_organization: organization=E;
225  break;
226  case pr_department: department=E;
227  break;
228  case pr_subdep: subDep=E;
229  break;
230  case pr_job: job=E;
231  break;
232  case pr_tel: tel=E;
233  break;
234  case pr_fax: fax=E;
235  break;
236  case pr_modem: modem=E;
237  break;
238  case pr_mobile: mobile=E;
239  break;
240  case pr_url: homepage=E;
241  break;
242  case pr_talk: talk=E;
243  break;
244  case pr_notes: comment=E;
245  break;
246  case pr_birthday: birthday=E;
247  break;
248  case pr_notused:
249  break;
250  default: {/*char m[250];
251  snprintf(m,sizeof(m),"unknown tag '%x'",literal(k));
252  info->log(m);*/
253  }
254  break;
255  }
256  }
257  }
258  }
259 
260  if (!firstName.isEmpty() && !lastName.isEmpty()) {
261  givenName=lastName+", "+firstName;
262  }
263 
264  // Determine if the record is ok.
265 
266  OK=true;
267 }
268 
269 bool pabfields_t::isUsed(int k)
270 {
271 return tags[k].isUsed();
272 }
273 
274 pabrec_entry pabfields_t::isWhat(int k)
275 {
276 return tags[k].matchTag();
277 }
278 
279 word_t pabfields_t::literal(int k)
280 {
281 return tags[k].literal();
282 }
283 
284 content_t pabfields_t::order(int k)
285 {
286 return tags[k].order();
287 }
288 
289 TDEABC::Addressee pabfields_t::get() {
290  TDEABC::Addressee a;
291  if (!givenName.isEmpty()) a.setFormattedName(givenName);
292  if (!email.isEmpty()) a.insertEmail(email);
293  if (!title.isEmpty()) a.setTitle(title);
294  if (!firstName.isEmpty()) a.setName(firstName);
295  if (!additionalName.isEmpty()) a.setAdditionalName(additionalName);
296  if (!lastName.isEmpty()) a.setFamilyName(lastName);
297 
298  TDEABC::Address addr;
299  if (!address.isEmpty()) addr.setStreet(address);
300  if (!town.isEmpty()) addr.setLocality(town);
301  if (!zip.isEmpty()) addr.setPostalCode(zip);
302  if (!state.isEmpty()) addr.setRegion(state);
303  if (!country.isEmpty()) addr.setCountry(country);
304  a.insertAddress(addr);
305 
306  if (!organization.isEmpty()) a.setOrganization(organization);
307  if (!department.isEmpty()) a.setRole(department);
308  // Miss out department, subDep, job
309  if (!tel.isEmpty()) a.insertPhoneNumber( TDEABC::PhoneNumber( tel, TDEABC::PhoneNumber::Voice ) );
310  if (!fax.isEmpty()) a.insertPhoneNumber( TDEABC::PhoneNumber( fax, TDEABC::PhoneNumber::Fax ) );
311  if (!mobile.isEmpty()) a.insertPhoneNumber( TDEABC::PhoneNumber( mobile, TDEABC::PhoneNumber::Cell ) );
312  if (!modem.isEmpty()) a.insertPhoneNumber( TDEABC::PhoneNumber( modem, TDEABC::PhoneNumber::Modem ) );
313  if (!homepage.isEmpty()) a.setUrl(KURL( homepage ));
314  // Miss out talk
315  if (!comment.isEmpty()) a.setNote(comment);
316  // Miss out birthday
317  return a;
318 }
319 
320 
321 
322 
323 /* class pabrec {
324  private:
325  char entry[1024];
326  byte *_mem;
327  word_t *_N;
328  word_t *_w;
329  public:
330  pabrec(pab *); // expects record the begin at reading point (ftell).
331  ~pabrec();
332  public:
333  word_t N(void) { return _N[0]; }
334  word_t operator[](int i) { return _w[i]; }
335  const char *getEntry(int i);
336  public:
337  content_t read(word_t offset);
338  };
339 */
340 
341 pabrec::pabrec(pab & P)
342 {
343 adr_t A=P.tell();
344 content_t hdr;
345 word_t offset,size,dummy;
346 int i;
347 
348  hdr=P.go(A);
349  offset=P.lower(hdr);
350 
351  size=offset;
352  _mem=new byte[size];
353  P.read(_mem,size);
354 
355  P.go(A+offset);
356  P.read(m_N);
357  m_W=new word_t[m_N+1];
358 
359  P.read(dummy);
360  for(i=0;i<=m_N;i++) {
361  P.read(m_W[i]);
362  }
363 }
364 
365 pabrec::~pabrec()
366 {
367  delete _mem;
368  delete m_W;
369 }
370 
371 
372 content_t pabrec::read(word_t offset)
373 {
374 content_t R;
375  R=_mem[offset+3];
376  R<<=8;R+=_mem[offset+2];
377  R<<=8;R+=_mem[offset+1];
378  R<<=8;R+=_mem[offset];
379 return R;
380 }
381 
382 const char *pabrec::getEntry(int i)
383 {
384 int mb,me;
385 int k;
386  mb=m_W[i];me=m_W[i+1];
387  for(k=0;mb!=me;mb++) {
388  if (_mem[mb]>=' ' || _mem[mb]=='\n' || _mem[mb]==13 || _mem[mb]=='\t') {
389  if (_mem[mb]==13) { entry[k]='\n'; }
390  else { entry[k]=_mem[mb]; }
391  k+=1;
392  }
393  }
394  entry[k]='\0';
395 return (const char *) entry;
396 }