File: XmlFonbook.cpp

package info (click to toggle)
vdr-plugin-fritzbox 1.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,012 kB
  • sloc: cpp: 23,764; makefile: 153; sh: 15
file content (177 lines) | stat: -rw-r--r-- 6,003 bytes parent folder | download | duplicates (5)
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
/*
 * libfritz++
 *
 * Copyright (C) 2007-2010 Joachim Wilke <libfritz@joachim-wilke.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "XmlFonbook.h"

#include <string>
#include <cstdlib>
#include <sstream>

#include "Config.h"
#include "Tools.h"
#include <liblog++/Log.h>
#include <libconv++/CharsetConverter.h>
#include <libconv++/EntityConverter.h>

namespace fritz {

XmlFonbook::XmlFonbook(std::string title, std::string techId, bool writeable)
: Fonbook{title, techId, writeable} {
}

XmlFonbook::~XmlFonbook() {
}


std::string XmlFonbook::extractXmlAttributeValue(std::string element, std::string attribute, std::string xml) {
	size_t posStart = xml.find('<'+element);
	if (posStart != std::string::npos) {
		posStart = xml.find(attribute+"=\"", posStart);
		if (posStart != std::string::npos) {
			size_t posEnd = xml.find("\"", posStart + attribute.length() + 2);
			if (posEnd != std::string::npos)
				return xml.substr(posStart + attribute.length() + 2, posEnd - posStart - attribute.length() - 2);
		}
	}
	return "";
}

std::string XmlFonbook::extractXmlElementValue(std::string element, std::string xml) {
	size_t posStart = xml.find('<'+element);
	if (posStart != std::string::npos) {
		posStart = xml.find(">", posStart);
		if (xml[posStart-1] == '/')
			return "";
		size_t posEnd   = xml.find("</"+element+'>');
		if (posEnd != std::string::npos)
			return xml.substr(posStart + 1, posEnd - posStart - 1);
	}
	return "";
}

void XmlFonbook::parseXmlFonbook(std::string *msg) {
	DBG("Parsing fonbook using xml parser.")
	// determine charset
	size_t pos, posStart, posEnd;
	posStart = msg->find("encoding=\"");
	if (posStart != std::string::npos) {
		posEnd = msg->find("\"", posStart + 10);
		if (posEnd != std::string::npos)
			charset = msg->substr(posStart + 10, posEnd - posStart - 10);
	}
	DBG("using charset " << charset);

	std::string msgConv = convert::CharsetConverter::ConvertToLocalEncoding(*msg, charset);

	pos = msgConv.find("<contact>");
	while (pos != std::string::npos) {
		std::string msgPart = msgConv.substr(pos, msgConv.find("</contact>", pos) - pos + 10);
		std::string category = extractXmlElementValue("category", msgPart);
		std::string name     = convert::EntityConverter::DecodeEntities(extractXmlElementValue("realName", msgPart));
		FonbookEntry fe(name, category == "1");
		size_t posNumber = msgPart.find("<number");
		while (posNumber != std::string::npos) {
			std::string msgPartofPart = msgPart.substr(posNumber, msgPart.find("</number>", posNumber) - posNumber + 9);
			std::string number    = extractXmlElementValue  ("number",              msgPartofPart);
			std::string typeStr   = extractXmlAttributeValue("number", "type",      msgPartofPart);
			std::string quickdial = extractXmlAttributeValue("number", "quickdial", msgPartofPart);
			std::string vanity    = extractXmlAttributeValue("number", "vanity",    msgPartofPart);
			std::string prio      = extractXmlAttributeValue("number", "prio",      msgPartofPart);

			if (number.size()) { // the xml may contain entries without a number!
				FonbookEntry::eType type = FonbookEntry::TYPE_NONE;
				if (typeStr == "home")
					type = FonbookEntry::TYPE_HOME;
				if (typeStr == "mobile")
					type = FonbookEntry::TYPE_MOBILE;
				if (typeStr == "work")
					type = FonbookEntry::TYPE_WORK;

				fe.addNumber(number, type, quickdial, vanity, atoi(prio.c_str()));
			}
			posNumber = msgPart.find("<number", posNumber+1);
		}
		addFonbookEntry(fe);
		pos = msgConv.find("<contact>", pos+1);
	}
}

std::string XmlFonbook::serializeToXml() {

	std::stringstream result;
	result << "<?xml version=\"1.0\" encoding=\"" << charset << "\"?>"
			  "<phonebooks>"
			  "<phonebook>";
	for (auto fe : getFonbookList()) {
		result << "<contact>"
			   << "<category>" << (fe.isImportant() ? "1" : "0") << "</category>"
			   << "<person>"
		       << "<realName>" << fe.getName() << "</realName>"
		       << "</person>"
		       << "<telephony>";
		for (size_t numberPos = 0; numberPos < fe.getSize(); numberPos++)
			if (fe.getNumber(numberPos).length() > 0) {  //just iterate over all numbers
				std::string typeName = "";
				switch (fe.getType(numberPos)) {
				case FonbookEntry::TYPE_NONE:
				case FonbookEntry::TYPE_HOME:
					typeName="home";
					break;
				case FonbookEntry::TYPE_MOBILE:
					typeName="mobile";
					break;
				case FonbookEntry::TYPE_WORK:
					typeName="work";
					break;
				default:
					// should not happen
					break;
				}
				result << "<number type=\"" << typeName << "\" "
						          "quickdial=\"" << fe.getQuickdial(numberPos) << "\" "
						          "vanity=\""    << fe.getVanity(numberPos)    << "\" "
						          "prio=\""      << fe.getPriority(numberPos)  << "\">"
				       << fe.getNumber(numberPos)
				       << "</number>";
			}
        //TODO: add <mod_time>1306951031</mod_time>
		result << "</telephony>"
			   << "<services/>"
               << "<setup/>"
               << "</contact>";
	}
	result << "</phonebook>"
			  "</phonebooks>";

	convert::CharsetConverter conv("", charset);
	std::string xmlData = conv.convert(result.str());

	// replace '&' with '&amp;'
	std::string::size_type pos = 0;
	while ((pos = xmlData.find('&', pos)) != std::string::npos) {
		xmlData.replace(pos, 1, "&amp;");
		pos += 5;
	}

	return xmlData;
}

}