3 /// Record parsing class for the phone call logs database.
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2013, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
24 #include "r_calllog.h"
25 #include "record-internal.h"
26 #include "protostructs.h"
32 #include "ios_state.h"
35 using namespace Barry::Protocol
;
39 #define MILLISECONDS_IN_A_SECOND 1000
41 time_t CallLog::GetTime() const
43 return (time_t)(Timestamp
/ MILLISECONDS_IN_A_SECOND
);
46 CallLog::DirectionFlagType
CallLog::DirectionProto2Rec(uint8_t s
)
48 return (DirectionFlagType
)s
;
51 uint8_t CallLog::DirectionRec2Proto(DirectionFlagType s
)
56 CallLog::PhoneTypeFlagType
CallLog::PhoneTypeProto2Rec(uint8_t s
)
58 return (PhoneTypeFlagType
)s
;
61 uint8_t CallLog::PhoneTypeRec2Proto(PhoneTypeFlagType s
)
67 ///////////////////////////////////////////////////////////////////////////////
70 // CallLog Field Codes
71 #define CLLFC_CALLLOG_TYPE 0x01
72 #define CLLFC_DIRECTION 0x02
73 #define CLLFC_DURATION 0x03
74 #define CLLFC_TIMESTAMP 0x04
75 #define CLLFC_STATUS 0x06
76 #define CLLFC_UNIQUEID 0x07
77 #define CLLFC_PHONE_TYPE 0x0b
78 #define CLLFC_PHONE_NUMBER 0x0c
79 #define CLLFC_PHONE_INFO 0x0d
80 #define CLLFC_CONTACT_NAME 0x1f
81 #define CLLFC_END 0xffff
83 static FieldLink
<CallLog
> CallLogFieldLinks
[] = {
84 { CLLFC_PHONE_NUMBER
, N_("Phone number"), 0, 0, &CallLog::PhoneNumber
, 0, 0, 0, 0, true },
85 { CLLFC_CONTACT_NAME
, N_("Contact name"), 0, 0, &CallLog::ContactName
, 0, 0, 0, 0, true },
86 { CLLFC_END
, N_("End of List"), 0, 0, 0, 0, 0, 0, 0, false }
98 const unsigned char* CallLog::ParseField(const unsigned char *begin
,
99 const unsigned char *end
,
100 const IConverter
*ic
)
102 const CommonField
*field
= (const CommonField
*) begin
;
104 // advance and check size
105 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
106 if( begin
> end
) // if begin==end, we are ok
109 if( !btohs(field
->size
) ) // if field has no size, something's up
112 if( field
->type
== CLLFC_CALLLOG_TYPE
) {
113 if( field
->u
.raw
[0] != 'p' ) {
114 throw Error( _("CallLog::ParseField: CallLogType is not 'p'") );
119 // this is always the same as the RecordID from the lower level
120 // protocol, so we throw this away for now
121 if( field
->type
== CLLFC_UNIQUEID
)
124 // cycle through the type table
125 for( FieldLink
<CallLog
> *b
= CallLogFieldLinks
;
126 b
->type
!= CLLFC_END
;
129 if( b
->type
== field
->type
) {
131 std::string
&s
= this->*(b
->strMember
);
132 s
= ParseFieldString(field
);
133 if( b
->iconvNeeded
&& ic
)
135 return begin
; // done!
137 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
138 TimeT
&t
= this->*(b
->timeMember
);
139 t
.Time
= min2time(field
->u
.min1900
);
145 // handle special cases
146 switch( field
->type
)
149 // single byte... size check above checks for non-zero already
150 switch (field
->u
.raw
[0]) {
152 StatusFlag
= Barry::CallLog::OK
;
155 StatusFlag
= Barry::CallLog::Busy
;
158 StatusFlag
= Barry::CallLog::NetError
;
161 StatusFlag
= Barry::CallLog::Unknown
;
165 case CLLFC_DIRECTION
:
166 if( field
->u
.raw
[0] > CLL_DIRECTION_RANGE_HIGH
) {
167 throw Error( _("CallLog::ParseField: direction field out of bounds") );
170 DirectionFlag
= DirectionProto2Rec(field
->u
.raw
[0]);
174 case CLLFC_PHONE_TYPE
:
175 if( field
->u
.raw
[0] > CLL_PHONETYPE_RANGE_HIGH
) {
176 PhoneTypeFlag
= Barry::CallLog::TypeUnknown
;
179 PhoneTypeFlag
= PhoneTypeProto2Rec(field
->u
.raw
[0]);
183 case CLLFC_PHONE_INFO
:
184 switch (field
->u
.raw
[0]) {
186 PhoneInfoFlag
= Barry::CallLog::InfoKnown
;
189 PhoneInfoFlag
= Barry::CallLog::InfoUnknown
;
192 PhoneInfoFlag
= Barry::CallLog::InfoPrivate
;
195 PhoneInfoFlag
= Barry::CallLog::InfoUndefined
;
200 if( btohs(field
->size
) >= sizeof(field
->u
.uint32
) ) {
201 Duration
= btohl(field
->u
.uint32
);
206 case CLLFC_TIMESTAMP
:
207 if( btohs(field
->size
) >= sizeof(field
->u
.timestamp
) ) {
208 Timestamp
= btohll(field
->u
.timestamp
);
214 // if still not handled, add to the Unknowns list
216 uf
.type
= field
->type
;
217 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
218 Unknowns
.push_back(uf
);
220 // return new pointer for next field
224 void CallLog::ParseHeader(const Data
&data
, size_t &offset
)
226 // no header in CallLog records
229 void CallLog::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
231 const unsigned char *finish
= ParseCommonFields(*this,
232 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
233 offset
+= finish
- (data
.GetData() + offset
);
236 void CallLog::Validate() const
240 void CallLog::BuildHeader(Data
&data
, size_t &offset
) const
242 // not yet implemented
245 void CallLog::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
247 // not yet implemented
250 void CallLog::Dump(std::ostream
&os
) const
252 ios_format_state
state(os
);
254 uint32_t timestamp
= Duration
;
255 int32_t days
, hours
, minutes
, secondes
;
257 static const char *DirectionName
[] = {
260 N_("Call Missing (Messagerie)"),
263 static const char *StatusName
[] = {
267 N_("Not supported by Barry")
269 static const char *PhoneInfoName
[] = {
271 N_("Known phone number"),
272 N_("Unknown phone number"),
273 N_("Private phone number")
275 static const char *PhoneTypeName
[] = {
280 N_("Not supported by Barry")
283 os
<< _("CallLog entry: ") << "0x" << setbase(16) << RecordId
284 << " (" << (unsigned int)RecType
<< ")\n";
286 time_t t
= GetTime();
287 os
<< _(" Timestamp: ") << ctime(&t
);
288 os
<< _(" Direction: ")
289 << gettext( DirectionName
[DirectionFlag
] ) << "\n";
291 << gettext( StatusName
[StatusFlag
] ) << "\n";
292 os
<< _(" Phone info: ")
293 << gettext( PhoneInfoName
[PhoneInfoFlag
] ) << "\n";
294 os
<< _(" Phone type: ")
295 << gettext( PhoneTypeName
[PhoneTypeFlag
] ) << "\n";
297 os
<< _(" Duration: ");
300 days
= (int) (timestamp
/ (60 * 60 * 24));
301 timestamp
= timestamp
- (days
* (60 * 60 * 24));
303 hours
= (int) (timestamp
/ (60 * 60));
304 timestamp
= timestamp
- (hours
* (60 * 60));
306 minutes
= (int) (timestamp
/ 60);
307 timestamp
= timestamp
- (minutes
* 60);
309 secondes
= timestamp
;
312 os
<< setbase(10) << days
<< _(" days ");
314 os
<< setbase(10) << days
<< _(" day ");
316 os
<< setfill ('0') << setw(2) << setbase(10) << hours
;
318 os
<< setfill ('0') << setw(2) << setbase(10) << minutes
;
320 os
<< setfill ('0') << setw(2) << setbase(10) << secondes
;
323 // cycle through the type table
324 for( const FieldLink
<CallLog
> *b
= CallLogFieldLinks
;
325 b
->type
!= CLLFC_END
;
329 const std::string
&s
= this->*(b
->strMember
);
331 os
<< " " << gettext(b
->name
) << ": " << s
<< "\n";
333 else if( b
->timeMember
) {
334 TimeT t
= this->*(b
->timeMember
);
336 os
<< " " << gettext(b
->name
) << ": " << t
<< "\n";
338 os
<< " " << gettext(b
->name
) << ": " << _("unknown") << "\n";
347 void CallLog::Clear()
349 RecType
= GetDefaultRecType();
358 DirectionFlag
= Barry::CallLog::Receiver
;
359 StatusFlag
= Barry::CallLog::Unknown
;
360 PhoneTypeFlag
= Barry::CallLog::TypeUnknown
;
361 PhoneInfoFlag
= Barry::CallLog::InfoUndefined
;
366 const FieldHandle
<CallLog
>::ListT
& CallLog::GetFieldHandles()
368 static FieldHandle
<CallLog
>::ListT fhv
;
373 #undef CONTAINER_OBJECT_NAME
374 #define CONTAINER_OBJECT_NAME fhv
376 #undef RECORD_CLASS_NAME
377 #define RECORD_CLASS_NAME CallLog
379 FHP(RecType
, _("Record Type Code"));
380 FHP(RecordId
, _("Unique Record ID"));
382 FHD(Duration
, _("Duration of Call in Seconds"), CLLFC_DURATION
, false);
383 FHD(Timestamp
, _("Timestamp of Call in Milliseconds"), CLLFC_TIMESTAMP
, false);
384 FHD(ContactName
, _("Contact Name"), CLLFC_CONTACT_NAME
, true);
385 FHD(PhoneNumber
, _("Phone Number"), CLLFC_PHONE_NUMBER
, true);
387 FHE(dft
, DirectionFlagType
, DirectionFlag
, _("Direction of Call"));
388 FHE_CONST(dft
, Receiver
, _("Received Call"));
389 FHE_CONST(dft
, Emitter
, _("Placed the Call"));
390 FHE_CONST(dft
, Failed
, _("Failed Call"));
391 FHE_CONST(dft
, Missing
, _("Missed Call"));
393 FHE(sft
, StatusFlagType
, StatusFlag
, _("Status of Call"));
394 FHE_CONST(sft
, OK
, _("OK"));
395 FHE_CONST(sft
, Busy
, _("Busy"));
396 FHE_CONST(sft
, NetError
, _("Network Error"));
397 FHE_CONST(sft
, Unknown
, _("Unsupported Status"));
399 FHE(ptf
, PhoneTypeFlagType
, PhoneTypeFlag
, _("Phone Type"));
400 FHE_CONST(ptf
, TypeUndefined
, _("Undefined"));
401 FHE_CONST(ptf
, TypeOffice
, _("Office"));
402 FHE_CONST(ptf
, TypeHome
, _("Home"));
403 FHE_CONST(ptf
, TypeMobile
, _("Mobile"));
404 FHE_CONST(ptf
, TypeUnknown
, _("Unknown"));
406 FHE(pif
, PhoneInfoFlagType
, PhoneInfoFlag
, _("Phone Info"));
407 FHE_CONST(pif
, InfoUndefined
, _("Undefined"));
408 FHE_CONST(pif
, InfoKnown
, _("Phone Number is Set"));
409 FHE_CONST(pif
, InfoUnknown
, _("Phone Number Not Set"));
410 FHE_CONST(pif
, InfoPrivate
, _("Phone Number is Private"));
412 FHP(Unknowns
, _("Unknown Fields"));
417 std::string
CallLog::GetDescription() const