summaryrefslogtreecommitdiffstats
path: root/kpilot/lib/pilotAddress.cc
blob: f12e9cc50f9c144f77fcea7831736ac87882d6da (plain)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* KPilot
**
** Copyright (C) 1998-2001 by Dan Pilone
** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
** Copyright (C) 2007 by Adriaan de Groot <groot@kde.org>
**
** This is a C++ wrapper for the pilot's address database structures.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/


#include "options.h"


#include <stdlib.h>
#include <assert.h>

#include <tqnamespace.h>
#include <tqstringlist.h>

#include "pilotAddress.h"

static const char *default_address_category_names[] = {
	"Unfiled",
	"Business",
	"Personal",
	"Quicklist",
	0L
} ;

static const char *default_address_field_labels[] = {
	"Last name",
	"First name",
	"Company",
	"Work",
	"Home",
	"Fax",
	"Other",
	"E-mail",
	"Addr(W)",
	"City",
	"State",
	"Zip Code",
	"Country",
	"Title",
	"Custom 1",
	"Custom 2",
	"Custom 3",
	"Custom 4",
	"Note",
	0L
} ;

void PilotAddressInfo::resetToDefault()
{
	FUNCTIONSETUP;
	// Reset to all 0s
	memset(&fInfo,0,sizeof(fInfo));
	// Fill up default categories
	for (unsigned int i=0; (i<4) && default_address_category_names[i]; ++i)
	{
		strncpy(fInfo.category.name[i],default_address_category_names[i],sizeof(fInfo.category.name[0]));
	}
	// Weird hack, looks like there's an extra copy of Unfiled
	strncpy(fInfo.category.name[15],default_address_category_names[0],sizeof(fInfo.category.name[0]));

	// And fill up the default labels.
	for (unsigned int i=0; (i<19) && default_address_field_labels[i]; ++i)
	{
		strncpy(fInfo.labels[i],default_address_field_labels[i],sizeof(fInfo.labels[0]));
	}
}

TQString PilotAddressInfo::phoneLabel(EPhoneType i) const
{
	if (i<=eMobile)
	{
		return Pilot::fromPilot(info()->phoneLabels[i]);
	}
	else
	{
		return TQString();
	}
}

PhoneSlot::PhoneSlot( const int v )
{
	i = entryPhone1;
	operator=(v);
}

const PhoneSlot &PhoneSlot::operator=( const int &v )
{
	if ( (entryPhone1 <= v) && (v <= entryPhone5) )
	{
		i = v;
	}
	else
	{
		i = invalid;
	}
	return *this;
}

const PhoneSlot &PhoneSlot::operator++()
{
	if ( (i!=invalid) && (i<entryPhone5) )
	{
		++i;
	}
	else
	{
		i = invalid;
	}
	return *this;
}

/* static */ const PhoneSlot PhoneSlot::begin()
{
	return PhoneSlot( entryPhone1 );
}

/* static */ const PhoneSlot PhoneSlot::end()
{
	return PhoneSlot( invalid );
}

unsigned int PhoneSlot::toOffset() const
{
	if ( isValid() )
	{
		return i-entryPhone1;
	}
	else
	{
		return 0;
	}
}

unsigned int PhoneSlot::toField() const
{
	if ( isValid() )
	{
		return i;
	}
	else
	{
		return entryPhone1;
	}
}

PhoneSlot::operator TQString() const
{
	return TQString("%1,%2").arg(toOffset()).arg(toField());
}

#define MAXFIELDS 19

PilotAddress::PilotAddress(PilotRecord *rec) :
	PilotRecordBase(rec),
	fAddressInfo()
{
	FUNCTIONSETUPL(4);
	memset(&fAddressInfo,0,sizeof(fAddressInfo));

	if (rec)
	{
		pi_buffer_t b;
		b.data = (unsigned char *) rec->data();
		b.allocated = b.used = rec->size();
		unpack_Address(&fAddressInfo, &b, address_v1);
	}
	else
	{
		fAddressInfo.phoneLabel[0] = (int) PilotAddressInfo::eWork;
		fAddressInfo.phoneLabel[1] = (int) PilotAddressInfo::eHome;
		fAddressInfo.phoneLabel[2] = (int) PilotAddressInfo::eOther;
		fAddressInfo.phoneLabel[3] = (int) PilotAddressInfo::eMobile;
		fAddressInfo.phoneLabel[4] = (int) PilotAddressInfo::eEmail;
	}
}

PilotAddress::PilotAddress(const PilotAddress & copyFrom) :
	PilotRecordBase(copyFrom),
	fAddressInfo()
{
	FUNCTIONSETUPL(4);
	_copyAddressInfo(copyFrom.fAddressInfo);
}

PilotAddress & PilotAddress::operator = (const PilotAddress & copyFrom)
{
	FUNCTIONSETUPL(4);
	PilotRecordBase::operator = (copyFrom);
	_copyAddressInfo(copyFrom.fAddressInfo);
	return *this;
}

bool PilotAddress::operator==(const PilotAddress &compareTo)
{
	FUNCTIONSETUPL(4);

	// now compare all the fields stored in the fAddressInfo.entry array of char*[19]
	for (int i=0; i<MAXFIELDS; i++) {
		// if one is NULL, and the other non-empty, they are not equal for sure
		if ( !getFieldP(i) && compareTo.getFieldP(i))
		{
			return false;
		}
		if ( getFieldP(i) && !compareTo.getFieldP(i))
		{
			return false;
		}

		// test for getField(i)!=... to prevent strcmp or NULL strings!  None or both can be zero, but not a single one.
		if ( (getFieldP(i) != compareTo.getFieldP(i)) && ( strcmp(getFieldP(i), compareTo.getFieldP(i)) ) )
		{
			return false;
		}
	}
	return true;
}


void PilotAddress::_copyAddressInfo(const struct Address &copyFrom)
{
	FUNCTIONSETUPL(4);
	fAddressInfo.showPhone = copyFrom.showPhone;

	for (int labelLp = 0; labelLp < 5; labelLp++)
	{
		fAddressInfo.phoneLabel[labelLp] =
			copyFrom.phoneLabel[labelLp];
	}

	for (unsigned int i = 0; i< MAXFIELDS; ++i)
	{
		if (copyFrom.entry[i])
		{
			fAddressInfo.entry[i] = qstrdup(copyFrom.entry[i]);
		}
		else
		{
			fAddressInfo.entry[i] = 0L;
		}
	}
}


PilotAddress::~PilotAddress()
{
	FUNCTIONSETUPL(4);
	free_Address(&fAddressInfo);
}

TQString PilotAddress::getTextRepresentation(const PilotAddressInfo *info, Qt::TextFormat richText) const
{
	TQString text, tmp;

	TQString par = (richText==Qt::RichText) ?CSL1("<p>"): TQString();
	TQString ps = (richText==Qt::RichText) ?CSL1("</p>"):CSL1("\n");
	TQString br = (richText==Qt::RichText) ?CSL1("<br/>"):CSL1("\n");

	// title + name
	text += par;
	if (!getField(entryTitle).isEmpty())
	{
		text += rtExpand(getField(entryTitle), richText);
		text += CSL1(" ");
	}

	tmp = richText ? CSL1("<b><big>%1 %2</big></b>") : CSL1("%1 %2");
	TQString firstName = getField(entryFirstname);
	if (firstName.isEmpty())
	{
		// So replace placeholder for first name (%1) with empty
		tmp = tmp.arg(TQString());
	}
	else
	{
		tmp = tmp.arg(rtExpand(firstName,richText));
	}
	tmp=tmp.arg(rtExpand(getField(entryLastname), richText));
	text += tmp;
	text += ps;

	// company
	if (!getField(entryCompany).isEmpty())
	{
		text += par;
		text += rtExpand(getField(entryCompany), richText);
		text += ps;
	}

	// phone numbers (+ labels)
	text += par;
	for ( PhoneSlot i = PhoneSlot::begin(); i.isValid(); ++i )
	{
		if (!getField(i.toField()).isEmpty())
		{
			if (richText)
			{
				if (getShownPhone() == i)
				{
					tmp=CSL1("<small>%1: </small><b>%2</b>");
				}
				else
				{
					tmp=CSL1("<small>%1: </small>%2");
				}
			}
			else
			{
				tmp=CSL1("%1: %2");
			}
			if (info)
			{
				tmp=tmp.arg(info->phoneLabel( getPhoneType( i ) ));
			}
			else
			{
				tmp=tmp.arg(CSL1("Contact: "));
			}
			tmp=tmp.arg(rtExpand(getField(i.toField()), richText));
			text += tmp;
			text += br;
		}
	}
	text += ps;

	// address, city, state, country
	text += par;
	if (!getField(entryAddress).isEmpty())
	{
		text += rtExpand(getField(entryAddress), richText);
		text += br;
	}
	if (!getField(entryCity).isEmpty())
	{
		text += rtExpand(getField(entryCity), richText);
		text += CSL1(" ");
	}
	if (!getField(entryState).isEmpty())
	{
		text += rtExpand(getField(entryState), richText);
		text += CSL1(" ");
	}
	if (!getField(entryZip).isEmpty())
	{
		text += rtExpand(getField(entryZip), richText);
	}
	text += br;
	if (!getField(entryCountry).isEmpty())
	{
		text += rtExpand(getField(entryCountry), richText);
		text += br;
	}
	text += ps;

	// custom fields
	text += par;
	for (int i = entryCustom1; i <= entryCustom4; i++)
	{
		if (!getField(i).isEmpty())
		{
			text += rtExpand(getField(i), richText);
			text += br;
		}
	}
	text += ps;

	// category
	if (info)
	{
		TQString categoryName = info->categoryName( category() );
		if (!categoryName.isEmpty())
		{
			text += par;
			text += rtExpand(categoryName, richText);
			text += ps;
		}
	}

	// note
	if (!getField(entryNote).isEmpty())
	{
		text += richText?CSL1("<hr/>"):CSL1("-----------------------------\n");
		text += par;
		text += rtExpand(getField(entryNote), richText);
		text += ps;
	}

	return text;
}

TQStringList PilotAddress::getEmails() const
{
	TQStringList list;

	for ( PhoneSlot i = PhoneSlot::begin(); i.isValid(); ++i)
	{
		PilotAddressInfo::EPhoneType t = getPhoneType( i );
		if ( t == PilotAddressInfo::eEmail )
		{
			TQString s = getField(i.toField());
			if (!s.isEmpty())
			{
				list.append(s);
			}
		}
	}

	return list;
}

void PilotAddress::setEmails(const TQStringList &list)
{
	FUNCTIONSETUPL(4);
	TQString test;

	// clear all e-mails first
	for ( PhoneSlot i = PhoneSlot::begin(); i.isValid(); ++i )
	{
		PilotAddressInfo::EPhoneType t = getPhoneType( i );
		if (t == PilotAddressInfo::eEmail)
		{
			setField(i.toField(), TQString() );
		}
	}

	for(TQStringList::ConstIterator listIter = list.begin();
		   listIter != list.end(); ++listIter)
	{
		TQString email = *listIter;
		if (!setPhoneField(PilotAddressInfo::eEmail, email, NoFlags).isValid())
		{
			WARNINGKPILOT << "Email accounts overflowed, silently dropped." << endl;
		}
	}
}

TQString PilotAddress::getField(int field) const
{
	if ( (entryLastname <= field) && (field <= entryNote) )
	{
		return Pilot::fromPilot(fAddressInfo.entry[field]);
	}
	else
	{
		return TQString();
	}
}

PhoneSlot PilotAddress::_getNextEmptyPhoneSlot() const
{
	FUNCTIONSETUPL(4);
	for (PhoneSlot i = PhoneSlot::begin(); i.isValid(); ++i)
	{
		const char *phoneField = getFieldP(i.toField());

		if (!phoneField || !phoneField[0])
		{
			return i;
		}
	}
	return PhoneSlot();
}

PhoneSlot PilotAddress::setPhoneField(PilotAddressInfo::EPhoneType type,
	const TQString &field,
	PhoneHandlingFlags flags)
{
	FUNCTIONSETUPL(4);

	const bool overwriteExisting = (flags == Replace);
	PhoneSlot fieldSlot;
	if (overwriteExisting)
	{
		fieldSlot = _findPhoneFieldSlot(type);
	}

	if ( !fieldSlot.isValid() )
	{
		fieldSlot = _getNextEmptyPhoneSlot();
	}

	// store the overflow phone
	if ( !fieldSlot.isValid() )
	{
		DEBUGKPILOT << fname << ": Phone would overflow." << endl;
	}
	else			// phone field 1 - 5; straight forward storage
	{
		setField(fieldSlot.toField(), field);
		fAddressInfo.phoneLabel[fieldSlot.toOffset()] = (int) type;
	}
	return fieldSlot;
}

PhoneSlot PilotAddress::_findPhoneFieldSlot(PilotAddressInfo::EPhoneType t) const
{
	FUNCTIONSETUPL(4);
	for ( PhoneSlot i = PhoneSlot::begin(); i.isValid(); ++i )
	{
		if ( getPhoneType(i) == t )
		{
			return i;
		}
	}

	return PhoneSlot();
}

TQString PilotAddress::getPhoneField(PilotAddressInfo::EPhoneType type) const
{
	FUNCTIONSETUPL(4);
	PhoneSlot fieldSlot = _findPhoneFieldSlot(type);

	if (fieldSlot.isValid())
	{
		return getField(fieldSlot.toField());
	}

	return TQString();
}

PhoneSlot PilotAddress::getShownPhone() const
{
	// The slot is stored as an offset
	return PhoneSlot(entryPhone1 + fAddressInfo.showPhone);
}

const PhoneSlot &PilotAddress::setShownPhone( const PhoneSlot &v )
{
	FUNCTIONSETUPL(4);
	if (v.isValid())
	{
		fAddressInfo.showPhone = v.toOffset();
	}
	return v;
}

PhoneSlot PilotAddress::setShownPhone(PilotAddressInfo::EPhoneType type)
{
	FUNCTIONSETUPL(4);
	PhoneSlot fieldSlot = _findPhoneFieldSlot(type);

	// Did we find a slot with the requested type?
	if (!fieldSlot.isValid())
	{
		// No, so look for first non-empty phone slot
		for ( fieldSlot = PhoneSlot::begin(); fieldSlot.isValid(); ++fieldSlot )
		{
			const char *p = getFieldP(fieldSlot.toField());
			if (p && p[0])
			{
				break;
			}
		}
		// If all of them are empty, then use first slot instead
		if (!fieldSlot.isValid())
		{
			fieldSlot = PhoneSlot::begin();
		}
	}
	setShownPhone(fieldSlot);
	return fieldSlot;
}

PilotAddressInfo::EPhoneType PilotAddress::getPhoneType( const PhoneSlot &field ) const
{
	if ( field.isValid() )
	{
		return (PilotAddressInfo::EPhoneType) fAddressInfo.phoneLabel[field.toOffset()];
	}
	else
	{
		return PilotAddressInfo::eNone;
	}
}

void PilotAddress::setField(int field, const TQString &text)
{
	FUNCTIONSETUPL(4);
	// This will have either been created with unpack_Address, and/or will
	// be released with free_Address, so use malloc/free here:
	if (fAddressInfo.entry[field])
	{
		free(fAddressInfo.entry[field]);
		fAddressInfo.entry[field]=0L;
	}
	if (!text.isEmpty())
	{
		fAddressInfo.entry[field] = (char *) malloc(text.length() + 1);
		Pilot::toPilot(text, fAddressInfo.entry[field], text.length()+1);
	}
	else
	{
		fAddressInfo.entry[field] = 0L;
	}
}

PilotRecord *PilotAddress::pack() const
{
	FUNCTIONSETUPL(4);
	int i;

	pi_buffer_t *b = pi_buffer_new( sizeof(fAddressInfo) );
	i = pack_Address(const_cast<Address_t *>(&fAddressInfo), b, address_v1);
	if (i<0)
	{
		return 0L;
	}
	// pack_Address sets b->used
	return new PilotRecord( b, this );
}