summaryrefslogtreecommitdiffstats
path: root/kpilot/lib/pilotRecord.h
blob: a3812b333c9700e3cca2f0f1c1f44e872c3a7a6e (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
#ifndef _KPILOT_PILOTRECORD_H
#define _KPILOT_PILOTRECORD_H
/* KPilot
**
** Copyright (C) 1998-2001 by Dan Pilone
** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
** Copyright (C) 2006 Adriaan de Groot <groot@kde.org>
**
*/

/*
** 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 "pilot.h"

/**
* @file This file defines the lowest- denominator representation(s)
*of the bits used in a Pilot-based database record.
*/


/**
* All entries in the Handheld -- whether interpreted or binary blobs --
* have some common characteristics, viz. an ID number, a category,
* and some attributes defined by the handheld. PilotRecordBase is
* a common base class collecting methods to manipulate those
* common characteristics.
*/
class KDE_EXPORT PilotRecordBase
{
public:
	/** Constructor. Initialize the characteristics to the
	*   given values.
	*
	* @param attrib Attributes (bitfield) for this entry.
	* @param cat Category for this entry. Should be in the
	*        range 0 <= cat < Pilot::CATEGORY_COUNT . Using an
	*        invalid category means 0 (unfiled) is used.
	* @param id Unique ID for this entry. May be 0 (non-unique) as well.
	*/
	PilotRecordBase(int attrib=0, int cat=0, recordid_t id=0) :
		fAttrib(attrib),fCat(0),fID(id)
	{
		setCategory(cat);
	}

	/** Constructor. Initializes the characteristics from
	 *  the values the given record @p b has. When @p b is
	 *  NULL (which is allowed), everything is assumed zero.
	 *  No ownership is transferred.
	 *
	 *  @param b Record to take characteristics from.
	 */
	PilotRecordBase( const PilotRecordBase *b ) :
		fAttrib( b ? b->attributes() : 0 ),
		fCat( 0 ),
		fID( b ? b->id() : 0 )
	{
		if (b)
		{
			setCategory( b->category() );
		}
	}

	/** Destructor. Nothing to do for it. */
	virtual ~PilotRecordBase() { } ;

	/** Attributes of this record (deleted, secret, ...);
	* it's a bitfield.
	*/
	inline int attributes() const
	{
		return fAttrib;
	}

	/** Set the attributes of this record. */
	inline void  setAttributes(int attrib)
	{
		fAttrib = attrib;
	}

	/** Returns the category number [ 0 .. Pilot::CATEGORY_COUNT-1]
	* of this record.
	*/
	inline int   category() const
	{
		return fCat;
	}

	/** Sets the category number [ 0 .. Pilot::CATEGORY_COUNT-1]
	* of this record.
	* Trying to set an illegal category number files this one under
	* "Unfiled" (which is 0).
	*/
	inline void  setCategory(int cat)
	{
		if ( (cat<0) || (cat>=(int)Pilot::CATEGORY_COUNT))
		{
			cat=0;
		}
		fCat = cat;
	}

	/** Sets the category number by looking up the string @p label
	* in the category table @p info . Leaves the category unchanged
	* if no match is found and returns @c false.
	*
	* @param info AppInfo structure containing the labels (in handheld
	*        native encoding).
	* @param label The label to look for.
	*
	* @return @c true on success, @c false on failure
	*/
	bool setCategory(const struct CategoryAppInfo *info, const QString &label)
	{
		if (!info)
		{
			return false;
		}

		int cat = Pilot::findCategory( info, label, false );
		if ( (cat<0) || (cat>=(int)Pilot::CATEGORY_COUNT) )
		{
			return false;
		}
		else
		{
			setCategory( cat );
			return true;
		}
	}

	/** Returns the record ID for this record. Record IDs are unique for a given
	* handheld and database.
	*/
	inline recordid_t id() const
	{
		return fID;
	}

	/** Sets the record ID for this record. Use with caution -- you ca confuse
	* the handheld by doing weird things here.
	*/
	void setID(recordid_t id)
	{
		fID = id;
	}

	/** Accessor for one bit of the record's attributes. Is this record marked
	* deleted (on the handheld) ? Deleted records are not removed from the
	* database until a HotSync is done (which normally calls purge deleted
	* or so to really get rid of the records from storage.
	*/
	inline bool isDeleted() const
	{
		return fAttrib & dlpRecAttrDeleted;
	}

	/** Accessor for one bit of the record's attributes. Is this record secret?
	* Secret records are not displayed on the desktop by default.
	*/
	inline bool isSecret() const
	{
		return fAttrib & dlpRecAttrSecret;
	}

	/** Accessor for one bit of the record's attributes. Is this record a
	* to-be-archived record? When a record is deleted, it may be marked
	* as "archive on PC" which means the PC should keep a copy. The
	* PC data correspondng to an archived-but-deleted record must not
	* be deleted.
	*/
	inline bool isArchived() const
	{
		return fAttrib & dlpRecAttrArchived;
	}

	/** Accessor for one bit of the record's attributes. Is this record modified?
	* Modified records are those that have been modified since the last HotSync.
	*/
	inline bool isModified() const
	{
		return fAttrib & dlpRecAttrDirty;
	}

#define SETTER(a) {\
		if (d) { fAttrib |= a; } \
		else   { fAttrib &= ~a; } }

	/** Mark a record as deleted (or not).*/
	inline void setDeleted(bool d=true) SETTER(dlpRecAttrDeleted)

	/** Mark a record as secret (or not). */
	inline void setSecret(bool d=true) SETTER(dlpRecAttrSecret)

	/** Mark a record as archived (or not). */
	inline void setArchived(bool d=true) SETTER(dlpRecAttrArchived)

	/** Mark a record as modified (or not). */
	inline void setModified(bool d=true) SETTER(dlpRecAttrDirty)

#undef SETTER

	/** Returns a text representation of this record. */
	virtual QString textRepresentation() const;

private:
	int fAttrib, fCat;
	recordid_t fID;
} ;

/** An "uninterpreted" representation of the bits comprising a HH record.
* This binary blob only exposes the data via the data() and size() functions,
* and also exposes the common characteristics of all entries.
*/
class KDE_EXPORT PilotRecord : public PilotRecordBase
{
public:
	/** Constructor. Using the given @p data and @p length, create
	* a record. Give it the additional attributes and category numbers;
	* the UID is a HH unique ID for identifying records.
	*
	* This constructor makes a copy of the data buffer (and owns that buffer).
	*/
	PilotRecord(void* data, int length, int attrib, int cat, recordid_t uid) KDE_DEPRECATED;

	/** Constructor. Using the given buffer @p buf (which carries its
	* own data and length), create a record. Otherwise much like the
	* above constructor @em except that this record assumes ownership
	* of the buffer, and doesn't make an additional copy
	* (In practice, this just saves copying around extra buffers).
	*/
	PilotRecord(pi_buffer_t *buf, int attrib, int cat, recordid_t uid) :
		PilotRecordBase(attrib,cat,uid),
		fData((char *)buf->data),
		fLen(buf->used),
		fBuffer(buf)
	{
		fAllocated++;
	}

	/** Constructor. Like the above, only take the attributes, category
	* and id from the given @p entry.
	*/
	PilotRecord( pi_buffer_t *buf, const PilotRecordBase *entry ) :
		PilotRecordBase( entry ),
		fData((char *)buf->data),
		fLen(buf->used),
		fBuffer(buf)
	{
		fAllocated++;
	}

	/** Destructor. Dispose of the buffers in the right form. */
	virtual ~PilotRecord()
	{
		if (fBuffer)
		{
			pi_buffer_free(fBuffer);
		}
		else
		{
			delete [] fData;
		}
		fDeleted++;
	}

	/** Constructor. Copies the data from the @p orig record. */
	PilotRecord(PilotRecord* orig);

	/** Retrieve the data buffer for this record. Note that trying
	* to change this data is fraught with peril -- especially trying
	* to lengthen it.
	*
	* @see setData
	*/
	char *data() const
	{
		if (fBuffer)
		{
			return (char *)(fBuffer->data);
		}
		else
		{
			return fData;
		}
	}

	/** Returns the size of the data for this record. */
	int size() const
	{
		if (fBuffer) return fBuffer->used; else
		return fLen;
	}

	/** Returns the data buffer associated with this record. */
	const pi_buffer_t *buffer() const { return fBuffer; }

	/** Set the data for this record. Frees old data. Assumes
	* ownership of the passed in buffer @p b.
	*/
	void setData(pi_buffer_t *b)
	{
		if (fBuffer) { pi_buffer_free(fBuffer); }
		else { delete[] fData; } ;
		fData = (char *)b->data;
		fLen = b->used;
		fBuffer = b;
	}

	/** Assignment operator. Makes a copy of the @p orig record. */
	PilotRecord& operator=(PilotRecord& orig);

	/** Sets the data for this record. Makes a copy of the data buffer. */
	void setData(const char* data, int len);

	/** Returns a text representation of this record. */
	virtual QString textRepresentation() const;

private:
	char* fData;
	int   fLen;
	pi_buffer_t *fBuffer;

public:
	/**
	* This is an interface for tracking down memory leaks
	* in the use of PilotRecords (for those without valgrind).
	* Count the number of allocations and deallocations.
	*/
	static void allocationInfo();
private:
	static int fAllocated,fDeleted;
};

#endif