kaddressbook

pab_pablib.cpp
1/***************************************************************************
2 pablib.cpp - description
3 -------------------
4 begin : Tue Jul 4 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#define REC_OK PAB_REC_OK
21
22
23pab::pab(const char *_pabfile)
24{
25 pabfile=_pabfile;
26 in.setName(pabfile);
27 in.open(IO_ReadOnly);
28 cap=i18n("Import MS Exchange Personal Address Book (.PAB)");
29}
30
31
32pab::~pab()
33{
34 if (in.isOpen()) { in.close(); }
35}
36
38//
39// Main conversion
40//
42
43bool pab::convert(void)
44{
45adr_t A;
46bool ret;
47
48 if (!in.isOpen()) {TQString msg;
49 msg=i18n("Cannot open %1 for reading").arg(pabfile);
50 // info->alert(msg);
51 return false;
52 }
53 if (!knownPAB()) {
54 return false;
55 }
56
57/*
58 if (!f->openAddressBook(info)) {
59 return false;
60 }
61*/
62
63 A=go(INDEX_OF_INDEX);
64 ret=convert(A,0,0);
65
66 // f->closeAddressBook();
67
68return ret;
69}
70
71bool pab::convert(adr_t A,content_t ,content_t )
72{
73adr_t table;
74content_t start,stop,T;
75int n, N = 0;
76
77 go(A);
78 T=read();
79
80 // Now we have to decide if this is a distribution list
81 // or an addressbook container. If it is the last just
82 // jump directly to dotable().
83
84 //if (upper(T)==PAB_REC_OK) {
85 // dotable(A,strt,stp);
86 // return true;
87 //}
88
89 // OK, it's not an addressbook container,
90 // handle it like a distribution list
91
92 start=T;
93 while(start!=0) {
94 N+=1;
95 stop=read();
96 table=read();
97 start=read();
98 }
99 if (N==0) { N=1; }
100/* {char m[100];
101 sprintf(m,"%d",N);
102 info->alert("",m);
103 }*/
104
105 //A=go(INDEX_OF_INDEX);
106 //printf("IoI=%08lx\n",A);
107 go(A);
108 start=read();
109 n=0;
110 while(start!=0) {adr_t cp;
111 stop=read();
112 table=read();
113 cp=tell();
114 dotable(table,start,stop);
115 //convert(table,start,stop);
116 go(cp);
117 start=read();
118 n+=1;
119 // info->setOverall( 100 * n / N );
120 }
121
122return true;
123}
124
125
126void pab::dotable(adr_t T,content_t start,content_t stop)
127{
128adr_t REC,pREC,cp;
129content_t cmp,skip;
130int N,n;
131
132 go(T);
133 cp=tell();
134
135 REC=0xffffffff;
136 pREC=0;
137 cmp=read();
138 if (cmp!=start) {
139 // first try processing as if this was a record. I.e. at the stop thing
140 processRec(stop);
141 // Then exit
142 // info->setCurrent();
143 // info->setCurrent(100);
144 return;
145 } // This is not a table.
146
147 // info->setCurrent();
148 N=0;
149 while (cmp!=stop && REC!=pREC) {
150 pREC=REC;
151 REC=read();
152 if (REC!=pREC) {
153 skip=read();
154 cmp=read();
155 }
156 N+=1;
157 }
158
159 go(cp);
160 REC=0xffffffff;
161 pREC=0;
162
163 cmp=read();
164
165 n=0;
166 while(cmp!=stop && REC!=pREC) {adr_t cp;
167 pREC=REC;
168 REC=read();
169 if (REC!=pREC) {
170 skip=read();
171 cp=tell();
172 processRec(REC);
173 go(cp);
174 cmp=read();
175 }
176 n+=1;
177 // info->setCurrent(100 * n / N);
178 }
179
180 // info->setCurrent();
181 // info->setCurrent(100);
182}
183
184
185void pab::processRec(adr_t REC)
186{
187content_t hdr;
188
189 hdr=go(REC);
190 if (upper(hdr)==REC_OK) { // Now read a record and instantiate!
191 pabrec rec(*this);
192 pabfields_t fields(rec, NULL);
193
194 if (fields.isOK() && fields.isUsable()) {
195 // f->addContact( fields.get() );
196 }
197 }
198}
199
200void pab::prt(unsigned char *,pabrec &,pabrec_entry )
201{
202}
203
204#define PABREC_N (sizeof(pabrec)/sizeof(word_t))
205
206void pab::rdPabRec(pabrec & )
207{
208}
209
211//
212// Here's where we recognize the record types
213//
215
216bool pab::recUnknown(pabrec &)
217{
218return false;
219}
220
221bool pab::recNoFunction(pabrec & )
222{
223return false;
224}
225
226const char *pab::get(unsigned char *,pabrec_entry ,pabrec & )
227{
228return "";
229}
230
231void pab::getrange(pabrec & ,pabrec_entry ,word_t & ,word_t & )
232{
233}
234
236//
237// Here's where we recognize the PAB files
238//
240
241bool pab::knownPAB(void)
242{
243content_t id;
244 id=go(0);
245 if (id!=PAB_FILE_ID) {TQString msg;
246 msg=i18n("%1 has no PAB id that I know of, cannot convert this").arg(pabfile);
247 // info->alert(msg);
248 return false;
249 }
250return true;
251}
252
253
255//
256// Functions to do file reading/positioning
257//
259
260content_t pab::go(adr_t a)
261{
262content_t A;
263 in.at(a);
264 A=read();
265 in.at(a);
266return A;
267}
268
269content_t pab::read(void)
270{
271unsigned char mem[4];
272content_t A;
273 in.readBlock((char *) &mem, sizeof(A)); // WinTel unsigned long opslag
274 A=mem[3];
275 A<<=8;A|=mem[2];
276 A<<=8;A|=mem[1];
277 A<<=8;A|=mem[0];
278return A;
279}
280
281void pab::read(word_t & w)
282{
283unsigned char mem[2];
284 in.readBlock((char *) &mem, sizeof(w));
285 w=mem[1];
286 w<<=8;w|=mem[0];
287}
288
289content_t pab::relative(int words)
290{
291adr_t a;
292 a=in.at();
293return go(a+(words*sizeof(content_t)));
294}
295
296content_t pab::add(adr_t & A,int words)
297{
298 A+=(words*sizeof(content_t));
299return go(A);
300}
301
302pabsize_t pab::size(content_t A)
303{
304return A&0xFFFF;
305}
306
307word_t pab::lower(content_t A)
308{
309return A&0xFFFF;
310}
311
312word_t pab::upper(content_t A)
313{
314return A>>16;
315}
316
317void pab::size(content_t A,pabsize_t & s1,pabsize_t & s2)
318{
319 s1=A&0xFFFF;
320 s2>>=16;
321}
322
323byte_t pab::readbyte(void)
324{
325byte_t c;
326 c=in.getch();
327return c;
328}
329
330void pab::read(unsigned char *mem,content_t size)
331{
332 in.readBlock((char *) mem, size);
333}