File: kab_netscape_io.cc

package info (click to toggle)
kdeutils 4%3A2.2.2-9.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,892 kB
  • ctags: 10,879
  • sloc: cpp: 82,942; sh: 11,754; ansic: 4,638; perl: 1,852; makefile: 706; python: 258
file content (312 lines) | stat: -rw-r--r-- 8,025 bytes parent folder | download
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
/*
	This file implements kab's importing functions for
	Netscape Messagers' exported address books in .ldif-format.

 	(C) Copyright 2001 by Helge Deller <deller@gmx.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., 675 Mass Ave, Cambridge, MA 02139, USA.

   	$Revision: 1.4 $

	Description:
	This modules can import .ldif-files, which can be generated in
	Netscape by exporting the local address book to a file.


	TODO:		- streetaddress IS CRYPTED !!!
			- change existing entry instead of adding a new one
			- export kab's address book in .ldif-format

	Enhancements:   - Nickname		(user1)
			- xmozillausehtmlmail   (user1)
			- pagerphone		(user1)
			- xmozillaanyphone	(user2)
			- modifytimestamp

	This is a sample Netscape .ldif file: (Ok means: will be imported)
	--------------------------------------------------------
	modifytimestamp: 20010322011327Z	Ok
	cn: Helge Deller			Ok
	xmozillanickname: spitzname		Ok  user1
	mail: deller@gmx.de			Ok
	xmozillausehtmlmail: FALSE		Ok  user1
	givenname: Helge			Ok
	sn: Deller				Ok
	xmozillauseconferenceserver: 0
	o: firma				Ok
	locality: ort				Ok
	st: bundesland				Ok
	description: bemerkungen stehen hier	Ok
	title: titl				Ok
	streetaddress:: YWRkcjEJ		Ok (encrypted!)
	postalcode: postleitzahl		Ok
	countryname: land			Ok
	telephonenumber: dienstl.		Ok
	homephone: privat			Ok
	facsimiletelephonenumber: faxnr		Ok
	ou: abteilung				Ok
	pagerphone: piepser			Ok
	cellphone: handynr			Ok
	homeurl: url				Ok
	xmozillaanyphone: dienstl.		Ok
	objectclass: top			Ok
	objectclass: person			Ok
*/


#include <qtextstream.h>
#include <qfile.h>
#include <kabapi.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include "kab_netscape_io.h"


Netscape_io::Netscape_io(KabAPI * _api, QWidget * parent):
KabAPI(parent)
{
    api = _api;
}


static bool StringStartsWith(QString & source, const char *part,
			     QString & remaining)
{
    int len = strlen(part);

    if (source.left(len) == part) {
	remaining = source.mid(len).simplifyWhiteSpace();
	return remaining.length() > 0;
    } else
	return false;
}

int Netscape_io::Netscape_ldif_Import()
{
    QString name;
    name = KFileDialog::getOpenFileName("::ldif",
					QString("*.ldif *.LDIF|") +
					i18n
					("Netscape Messenger addressbook export files (*.ldif)")
					+ "\n*|" + i18n("All files"), this,
					i18n
					("Import Netscape Messanger addressbook export files..."));

    if (name.isEmpty())
	return 0;

    QFile file(name);
    if (!file.exists() || !file.open(IO_ReadOnly)) {
	KMessageBox::information(this,
				 i18n("Unable to read the file %1.").
				 arg(name), i18n("Error"));
	return 0;
    }

    KabKey key;
    AddressBook::Entry * entry = NULL;
    AddressBook::Entry::Address * address = NULL;
    QString line, value, watermark;
    int no = 0;

    QTextStream t(&file);
    t.setEncoding(QTextStream::UnicodeUTF8);	/* Latin1 */

    watermark = i18n("Entry imported on %1 from '%2'")
	.arg(KGlobal::locale()->formatDate(QDate::currentDate(), true))
	.arg(name);

    while (!t.eof()) {
	line = t.readLine();
	/* loop until the first "dn:" value is found */
	if (t.eof() || !StringStartsWith(line, "dn:", value))
	    continue;

	/* create an empty entry */
	if (!entry)
	    entry = new AddressBook::Entry;
	if (!address)
	    address = new AddressBook::Entry::Address;
	address->headline = i18n("Main contact information");

	do {
	    line = t.readLine();
	    line.stripWhiteSpace();
	    if (line.isEmpty())
		break;

	    if (StringStartsWith(line, "objectclass:", value))	/* unused entries */
		continue;

	    if (StringStartsWith(line, "cn:", entry->fn))	/* formatted name */
		continue;

	    if (StringStartsWith(line, "givenname:", entry->firstname)) {
		int pos;
		pos = entry->firstname.find(' ');
		if (pos > 0) {	/* split name into first- and middlename */
		    entry->middlename = entry->firstname.mid(pos + 1);
		    entry->firstname.remove(pos, 255);
		}
		continue;
	    }

	    if (StringStartsWith(line, "sn:", entry->lastname))
		continue;

	    if (StringStartsWith(line, "mail:", value)) {
		entry->emails.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "homeurl:", value)) {
		entry->URLs.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "xmozillanickname:", value)) {
		entry->user1 += i18n("Nickname: %1").arg(value) + "\n";
		continue;
	    }

	    if (StringStartsWith(line, "xmozillausehtmlmail:", value)) {
		entry->user1 += ((value == "FALSE")
				 ? i18n("Mailformat: Text") :
				 i18n("Mailformat: HTML"))
		    + "\n";
		continue;
	    }

	    if (StringStartsWith(line, "modifytimestamp:", value)) {
		entry->user1 +=
		    i18n("Last modified: %1").arg(value) + "\n";
		continue;
	    }

	    if (StringStartsWith(line, "description:", entry->comment))
		continue;

	    if (StringStartsWith(line, "title:", entry->title))	/* or: rank ? */
		continue;

	    if (StringStartsWith(line, "telephonenumber:", value)) {
		entry->telephone.
		    append(QChar('0' + (int) AddressBook::Fixed - 1));
		entry->telephone.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "homephone:", value)) {
		entry->telephone.
		    append(QChar('0' + (int) AddressBook::Fixed - 1));
		entry->telephone.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "facsimiletelephonenumber:", value)) {
		entry->telephone.
		    append(QChar('0' + (int) AddressBook::Fax - 1));
		entry->telephone.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "pagerphone:", value)) {
		entry->telephone.
		    append(QChar('0' + (int) AddressBook::User1 - 1));
		entry->telephone.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "cellphone:", value)) {
		entry->telephone.
		    append(QChar('0' + (int) AddressBook::Mobile - 1));
		entry->telephone.append(value);
		continue;
	    }

	    if (StringStartsWith(line, "xmozillaanyphone:", value)) {
		entry->telephone.
		    append(QChar('0' + (int) AddressBook::User2 - 1));
		entry->telephone.append(value);
		continue;
	    }

	    /* Addresses */
	    if (StringStartsWith(line, "postalcode:", address->zip))
		continue;

	    if (StringStartsWith(line, "countryname:", address->country))
		continue;

	    if (StringStartsWith(line, "streetaddress:", address->address)) {
		/* FIXME: streetaddress could be encrypted !! */
		continue;
	    }

	    if (StringStartsWith(line, "locality:", address->town))
		continue;

	    if (StringStartsWith(line, "st:", address->state))
		continue;

	    if (StringStartsWith(line, "ou:", address->orgUnit))
		continue;

	    if (StringStartsWith(line, "o:", address->org))
		continue;

	} while (!t.eof());

	/* append this address to this entry */
	entry->addresses.push_front(*address);

	/* store a watermark */
	if (entry->user4.isEmpty())
	    entry->user4 = watermark;

	if (api->add(*entry, key, false) == AddressBook::NoError)
	    ++no;

	if (entry)
	    delete entry;
	entry = NULL;

	if (address)
	    delete address;
	address = NULL;
    }				/* while (!t.eof()) */
    file.close();

    if (no) {
	emit(setStatus(i18n("%1 addresses imported.").arg(no)));
    } else {
	emit(setStatus(i18n("No addresses imported.")));
    }

    return no;
}

/*
int Netscape_io::Netscape_ldif_Export()
{
    // TODO:
    KMessageBox::information(this,
			     i18n("Not yet implemented !"), i18n("Error"));
    return 0;
}
*/
#include "kab_netscape_io.moc"