00001 /********************************************************************** 00002 ** Copyright (C) 2001 Trolltech AS. All rights reserved. 00003 ** 00004 ** This file is part of Qt Palmtop Environment. 00005 ** 00006 ** This file may be distributed and/or modified under the terms of the 00007 ** GNU General Public License version 2 as published by the Free Software 00008 ** Foundation and appearing in the file LICENSE.GPL included in the 00009 ** packaging of this file. 00010 ** 00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 ** 00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 00015 ** 00016 ** Contact info@trolltech.com if any conditions of this licensing are 00017 ** not clear to you. 00018 ** 00019 **********************************************************************/ 00020 #include <qfile.h> 00021 #include <qtextstream.h> 00022 #include <opie/ocontactaccess.h> 00023 #include <opie/ocontact.h> 00024 00025 #include "addresslist.h" 00026 00027 AddressList::AddressList() 00028 { 00029 addresses.setAutoDelete(TRUE); 00030 read(); 00031 dirty = FALSE; 00032 } 00033 00034 AddressList::~AddressList() 00035 { 00036 addresses.clear(); 00037 } 00038 00039 void AddressList::addContact(const QString &email, const QString &name) 00040 { 00041 //skip if not a valid email address, 00042 if (email.find( '@') == -1) 00043 return; 00044 00045 if ( ! containsEmail(email) ) { 00046 AContact *in = new AContact; 00047 in->email = email; 00048 in->name = name; 00049 addresses.append(in); 00050 dirty = TRUE; 00051 } 00052 } 00053 00054 bool AddressList::containsEmail(const QString &email) 00055 { 00056 return ( getEmailRef(email) != -1 ); 00057 } 00058 00059 bool AddressList::containsName(const QString &name) 00060 { 00061 return ( getNameRef(name) != -1 ); 00062 } 00063 00064 QString AddressList::getNameByEmail(const QString &email) 00065 { 00066 int pos = getEmailRef(email); 00067 if (pos != -1) { 00068 AContact *ptr = addresses.at(pos); 00069 return ptr->name; 00070 } 00071 00072 return QString::null; 00073 } 00074 00075 QString AddressList::getEmailByName(const QString &name) 00076 { 00077 int pos = getNameRef(name); 00078 if (pos != -1) { 00079 AContact *ptr = addresses.at(pos); 00080 return ptr->email; 00081 } 00082 00083 return QString::null; 00084 } 00085 00086 int AddressList::getEmailRef(const QString &email) 00087 { 00088 int pos = 0; 00089 AContact *ptr; 00090 00091 for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) { 00092 if (ptr->email == email) 00093 return pos; 00094 pos++; 00095 } 00096 return -1; 00097 } 00098 00099 int AddressList::getNameRef(const QString &name) 00100 { 00101 int pos = 0; 00102 AContact *ptr; 00103 00104 for (ptr = addresses.first(); ptr != 0; ptr = addresses.next() ) { 00105 if (ptr->name == name) 00106 return pos; 00107 pos++; 00108 } 00109 return -1; 00110 } 00111 00112 QList<AContact>* AddressList::getContactList() 00113 { 00114 return &addresses; 00115 } 00116 00117 void AddressList::read() 00118 { 00119 OContactAccess::List::Iterator it; 00120 00121 QString lineEmail, lineName, email, name; 00122 OContactAccess m_contactdb("mailit"); 00123 OContactAccess::List m_list = m_contactdb.sorted( true, 0, 0, 0 ); 00124 //OContact* oc;(*it).defaultEmail() 00125 00126 for ( it = m_list.begin(); it != m_list.end(); ++it ) 00127 { 00128 //oc=(OContact*) it; 00129 if ((*it).defaultEmail().length()!=0) 00130 addContact((*it).defaultEmail(),(*it).fileAs()); 00131 } 00132 00133 /*if (! f.open(IO_ReadOnly) ) 00134 return; 00135 00136 QTextStream stream(&f); 00137 00138 while (! stream.atEnd() ) { 00139 lineEmail = stream.readLine(); 00140 if (! stream.atEnd() ) 00141 lineName = stream.readLine(); 00142 else return; 00143 00144 email = getRightString(lineEmail); 00145 name = getRightString(lineName); 00146 addContact(email, name); 00147 } 00148 f.close();*/ 00149 } 00150 00151 QString AddressList::getRightString(const QString &in) 00152 { 00153 QString out = ""; 00154 00155 int pos = in.find('='); 00156 if (pos != -1) { 00157 out = in.mid(pos+1).stripWhiteSpace(); 00158 } 00159 return out; 00160 } 00161
1.4.2