Diffstat (limited to 'noncore/unsupported/mailit/addresslist.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/mailit/addresslist.cpp | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/noncore/unsupported/mailit/addresslist.cpp b/noncore/unsupported/mailit/addresslist.cpp new file mode 100644 index 0000000..1cf2562 --- a/dev/null +++ b/noncore/unsupported/mailit/addresslist.cpp | |||
@@ -0,0 +1,167 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qt Palmtop Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include <qfile.h> | ||
21 | #include <qtextstream.h> | ||
22 | |||
23 | #include "addresslist.h" | ||
24 | |||
25 | AddressList::AddressList(QString file) | ||
26 | { | ||
27 | addresses.setAutoDelete(TRUE); | ||
28 | filename = file; | ||
29 | read(); | ||
30 | dirty = FALSE; | ||
31 | } | ||
32 | |||
33 | AddressList::~AddressList() | ||
34 | { | ||
35 | write(); | ||
36 | addresses.clear(); | ||
37 | } | ||
38 | |||
39 | void AddressList::addContact(QString email, QString name) | ||
40 | { | ||
41 | //skip if not a valid email address, | ||
42 | if (email.find( '@') == -1) | ||
43 | return; | ||
44 | |||
45 | if ( ! containsEmail(email) ) { | ||
46 | Contact *in = new Contact; | ||
47 | in->email = email; | ||
48 | in->name = name; | ||
49 | addresses.append(in); | ||
50 | dirty = TRUE; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | bool AddressList::containsEmail(QString email) | ||
55 | { | ||
56 | return ( getEmailRef(email) != -1 ); | ||
57 | } | ||
58 | |||
59 | bool AddressList::containsName(QString name) | ||
60 | { | ||
61 | return ( getNameRef(name) != -1 ); | ||
62 | } | ||
63 | |||
64 | QString AddressList::getNameByEmail(QString email) | ||
65 | { | ||
66 | int pos = getEmailRef(email); | ||
67 | if (pos != -1) { | ||
68 | Contact *ptr = addresses.at(pos); | ||
69 | return ptr->name; | ||
70 | } | ||
71 | |||
72 | return NULL; | ||
73 | } | ||
74 | |||
75 | QString AddressList::getEmailByName(QString name) | ||
76 | { | ||
77 | int pos = getNameRef(name); | ||
78 | if (pos != -1) { | ||
79 | Contact *ptr = addresses.at(pos); | ||
80 | return ptr->email; | ||
81 | } | ||
82 | |||
83 | return NULL; | ||
84 | } | ||
85 | |||
86 | int AddressList::getEmailRef(QString email) | ||
87 | { | ||
88 | int pos = 0; | ||
89 | Contact *ptr; | ||
90 | |||
91 | for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) { | ||
92 | if (ptr->email == email) | ||
93 | return pos; | ||
94 | pos++; | ||
95 | } | ||
96 | return -1; | ||
97 | } | ||
98 | |||
99 | int AddressList::getNameRef(QString name) | ||
100 | { | ||
101 | int pos = 0; | ||
102 | Contact *ptr; | ||
103 | |||
104 | for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) { | ||
105 | if (ptr->name == name) | ||
106 | return pos; | ||
107 | pos++; | ||
108 | } | ||
109 | return -1; | ||
110 | } | ||
111 | |||
112 | QList<Contact>* AddressList::getContactList() | ||
113 | { | ||
114 | return &addresses; | ||
115 | } | ||
116 | |||
117 | void AddressList::read() | ||
118 | { | ||
119 | QFile f(filename); | ||
120 | QString lineEmail, lineName, email, name; | ||
121 | |||
122 | if (! f.open(IO_ReadOnly) ) | ||
123 | return; | ||
124 | |||
125 | QTextStream stream(&f); | ||
126 | |||
127 | while (! stream.atEnd() ) { | ||
128 | lineEmail = stream.readLine(); | ||
129 | if (! stream.atEnd() ) | ||
130 | lineName = stream.readLine(); | ||
131 | else return; | ||
132 | |||
133 | email = getRightString(lineEmail); | ||
134 | name = getRightString(lineName); | ||
135 | addContact(email, name); | ||
136 | } | ||
137 | f.close(); | ||
138 | } | ||
139 | |||
140 | QString AddressList::getRightString(QString in) | ||
141 | { | ||
142 | QString out = ""; | ||
143 | |||
144 | int pos = in.find('='); | ||
145 | if (pos != -1) { | ||
146 | out = in.mid(pos+1).stripWhiteSpace(); | ||
147 | } | ||
148 | return out; | ||
149 | } | ||
150 | |||
151 | void AddressList::write() | ||
152 | { | ||
153 | if ( (addresses.count() == 0) || (!dirty) ) | ||
154 | return; | ||
155 | |||
156 | QFile f(filename); | ||
157 | if (! f.open(IO_WriteOnly) ) | ||
158 | return; | ||
159 | |||
160 | QTextStream stream(&f); | ||
161 | Contact *ptr; | ||
162 | for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) { | ||
163 | stream << "email = " + ptr->email + "\n"; | ||
164 | stream << "name = " + ptr->name + "\n"; | ||
165 | } | ||
166 | f.close(); | ||
167 | } | ||