summaryrefslogtreecommitdiff
path: root/noncore/unsupported/mailit/addresslist.cpp
authorllornkcor <llornkcor>2003-04-06 17:45:00 (UTC)
committer llornkcor <llornkcor>2003-04-06 17:45:00 (UTC)
commit70b1ff1a3f134d75d1539f269d52b34e6762684d (patch) (side-by-side diff)
tree28c1b3d0c03e8c0188f014dfe094645d1ddfef5f /noncore/unsupported/mailit/addresslist.cpp
parent75e8f29020e267d1013a79839831035073b4eeae (diff)
downloadopie-70b1ff1a3f134d75d1539f269d52b34e6762684d.zip
opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.tar.gz
opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.tar.bz2
move malit to head again, since it is working
Diffstat (limited to 'noncore/unsupported/mailit/addresslist.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/mailit/addresslist.cpp167
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 @@
+/**********************************************************************
+** Copyright (C) 2001 Trolltech AS. All rights reserved.
+**
+** This file is part of Qt Palmtop Environment.
+**
+** This file may be distributed and/or modified under the terms of the
+** GNU General Public License version 2 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.
+**
+** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+**
+** See http://www.trolltech.com/gpl/ for GPL licensing information.
+**
+** Contact info@trolltech.com if any conditions of this licensing are
+** not clear to you.
+**
+**********************************************************************/
+#include <qfile.h>
+#include <qtextstream.h>
+
+#include "addresslist.h"
+
+AddressList::AddressList(QString file)
+{
+ addresses.setAutoDelete(TRUE);
+ filename = file;
+ read();
+ dirty = FALSE;
+}
+
+AddressList::~AddressList()
+{
+ write();
+ addresses.clear();
+}
+
+void AddressList::addContact(QString email, QString name)
+{
+ //skip if not a valid email address,
+ if (email.find( '@') == -1)
+ return;
+
+ if ( ! containsEmail(email) ) {
+ Contact *in = new Contact;
+ in->email = email;
+ in->name = name;
+ addresses.append(in);
+ dirty = TRUE;
+ }
+}
+
+bool AddressList::containsEmail(QString email)
+{
+ return ( getEmailRef(email) != -1 );
+}
+
+bool AddressList::containsName(QString name)
+{
+ return ( getNameRef(name) != -1 );
+}
+
+QString AddressList::getNameByEmail(QString email)
+{
+ int pos = getEmailRef(email);
+ if (pos != -1) {
+ Contact *ptr = addresses.at(pos);
+ return ptr->name;
+ }
+
+ return NULL;
+}
+
+QString AddressList::getEmailByName(QString name)
+{
+ int pos = getNameRef(name);
+ if (pos != -1) {
+ Contact *ptr = addresses.at(pos);
+ return ptr->email;
+ }
+
+ return NULL;
+}
+
+int AddressList::getEmailRef(QString email)
+{
+ int pos = 0;
+ Contact *ptr;
+
+ for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) {
+ if (ptr->email == email)
+ return pos;
+ pos++;
+ }
+ return -1;
+}
+
+int AddressList::getNameRef(QString name)
+{
+ int pos = 0;
+ Contact *ptr;
+
+ for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) {
+ if (ptr->name == name)
+ return pos;
+ pos++;
+ }
+ return -1;
+}
+
+QList<Contact>* AddressList::getContactList()
+{
+ return &addresses;
+}
+
+void AddressList::read()
+{
+ QFile f(filename);
+ QString lineEmail, lineName, email, name;
+
+ if (! f.open(IO_ReadOnly) )
+ return;
+
+ QTextStream stream(&f);
+
+ while (! stream.atEnd() ) {
+ lineEmail = stream.readLine();
+ if (! stream.atEnd() )
+ lineName = stream.readLine();
+ else return;
+
+ email = getRightString(lineEmail);
+ name = getRightString(lineName);
+ addContact(email, name);
+ }
+ f.close();
+}
+
+QString AddressList::getRightString(QString in)
+{
+ QString out = "";
+
+ int pos = in.find('=');
+ if (pos != -1) {
+ out = in.mid(pos+1).stripWhiteSpace();
+ }
+ return out;
+}
+
+void AddressList::write()
+{
+ if ( (addresses.count() == 0) || (!dirty) )
+ return;
+
+ QFile f(filename);
+ if (! f.open(IO_WriteOnly) )
+ return;
+
+ QTextStream stream(&f);
+ Contact *ptr;
+ for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) {
+ stream << "email = " + ptr->email + "\n";
+ stream << "name = " + ptr->name + "\n";
+ }
+ f.close();
+}