addresslist.cpp
来自「Qtopia下的邮件处理程序」· C++ 代码 · 共 753 行 · 第 1/2 页
CPP
753 行
/******************************************************************************** Copyright (C) 2000-2006 TROLLTECH ASA. All rights reserved.**** This file is part of the Phone Edition of the Qtopia Toolkit.**** Licensees holding a valid license agreement from Trolltech or any of its** authorized distributors may use this file in accordance with** the License Agreement provided with the Licensed Software.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Trolltech's Commercial License Agreements.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.********** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "addresslist.h"#include <QLayout>#include <QFile>#include <QTextStream>#include <QTimer>#include <QStyle>#include <QKeyEvent>#include <QPainter>#include <qtopia/qtopiaglobal.h>#include <qtopia/qtopianamespace.h>// #include <qtopia/qcategoriesmanager.h>#ifdef QTOPIA_PHONE#include <qtopia/qsoftmenubar.h>#include <qtopia/qtopiaapplication.h>#endif#include <qtopia/mail/mailmessage.h>#include <qtopia/qcopservices.h>#ifdef QTOPIA_DESKTOP#include <qcopenvelope_qd.h>#else#include <qtopia/qcopenvelope.h>#include <qtopia/qcopservices.h>#endifstatic QIcon* pm_contacts=0;//static QPixmap* pm_email;static AddressList *alist=0;static void ensurePixmaps(){ if ( !pm_contacts ) pm_contacts = new QIcon(":icon/addressbook/AddressBook"); }/* Build a simple string format that the emailclient understands */static QString buildAddress(QString name, QString email){ QString str = MailMessage::quoteString(name) + " "; if ( email[0] == '<' ) { str += email; } else { str += "<" + email + ">"; } return str;}static QContact contact(const QString &name, const QString &email){ QContact c; QStringList l = name.split(" ",QString::SkipEmptyParts); QStringListIterator lit(l); if ( lit.hasNext() ) { c.setFirstName(lit.next()); } if ( lit.hasNext()) { c.setMiddleName(lit.next()); } if ( lit.hasNext() ) { c.setLastName( lit.next() ); } c.insertEmail( email ); return c;}AddressList::AddressList() : _contactModel(0){ loadEmailContacts(); _dirty = false; _contactsLoaded = false;}QContactModel* AddressList::contactModel(){ if ( !_contactModel ) { _contactModel = new QContactModel(this); connect( _contactModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),SLOT(contactsUpdated(const QModelIndex&,const QModelIndex&)) ); } return _contactModel;}AddressList::~AddressList(){ if ( _contactModel ) delete _contactModel; save();}void AddressList::load(){ loadContacts(); mergeLists();}void AddressList::contactsUpdated(const QModelIndex&, const QModelIndex&){ // we don't read the addressbook until we have to. Ignore if we haven't read it yet if ( _contactsLoaded ) { loadContacts(); mergeLists(); emit updated(); }}void AddressList::loadContacts(){ _contacts.clear(); for (int index = 0; index < contactModel()->count(); ++index) { QContact c(contactModel()->contact(index)); EMailAddress a; a.name = c.firstName(); //TODO fullName function here a.categories = c.categories(); a.fromContacts = true; QStringList list = c.emailList(); for (QStringList::Iterator lit = list.begin(); lit != list.end(); ++lit ) { a.email = *lit; _contacts.append(a); } } _contactsLoaded = true;}void AddressList::loadEmailContacts(){ QString file = Qtopia::applicationFileName("qtmail", "emailaddresslist.txt"); QFile f( file ); if ( !f.open( QIODevice::ReadOnly ) ) return; QStringList l = QString(f.readAll()).split("\n",QString::SkipEmptyParts); f.close(); for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it ) { EMailAddress a; if ( parseLine( *it, &a ) ) _emailContacts.append( a ); }}bool AddressList::parseLine(const QString &s, EMailAddress *a){ QString str = s.trimmed(); int pos; if ( (pos = str.indexOf(" ")) > -1 ) { QString action = s.left(pos); QString c = s.mid(pos).trimmed(); if ( action == "list") { // No tr } else if ( action == "deleted" ) { // No tr a->deleted = true; } else if ( action == "merged" ) { // No tr a->merged = true; } else return false; MailMessage::parseEmailAddress(c, a->name, a->email); return true; } return false;}/* Sync email addresslist and contacts.*/void AddressList::mergeLists(){// qDebug("merging email addressList and contact list"); for (QList<EMailAddress>::Iterator it = _emailContacts.begin(); it != _emailContacts.end(); ++it ) { // deleted stays deleted no matter what if ( !(*it).deleted ) { // if we've added it to the addressbook, set it as deleted if it no longer exists there if ( (*it).merged ) { bool removed = true; for ( QList<EMailAddress>::Iterator cit = _contacts.begin(); cit != _contacts.end(); ++cit) { if ( (*cit).email == (*it).email ) { removed = false; break; } } if ( removed ) { //qDebug("rec %s was merged and now removed", (*it).email.data() ); (*it).deleted = true; _dirty = true; } } else { bool added = false; for ( QList<EMailAddress>::Iterator cit = _contacts.begin(); cit != _contacts.end(); ++cit) { if ( (*cit).email == (*it).email ) { added = true; break; } } if ( added ) { //qDebug("rec %s was in email list only, but now exists in contacts as well", (*it).email.data() ); (*it).merged = true; _dirty = true; } } } }}void AddressList::save(){ if ( !_dirty ) return; QString file = Qtopia::applicationFileName("qtmail", "emailaddresslist.txt"); QFile f( file ); if ( !f.open( QIODevice::WriteOnly ) ) { qWarning("could not save email address list"); return; } QTextStream t(&f); t << "# This is the internal address list for the email application\n"; // No tr t << "# merged = contact is in addressbook. #deleted = deleted from internal list or addressbook\n"; // No tr t << "# list = believed to be in internal list only\n"; // No tr QString str; for (QList<EMailAddress>::Iterator it = _emailContacts.begin(); it != _emailContacts.end(); ++it) { if ( (*it).deleted ) { str = "deleted "; // No tr } else if ( (*it).merged ) { str = "merged "; // No tr } else { str = "list "; // No tr } str += buildAddress( (*it).name, (*it).email ); t << str << "\n"; } _dirty = false; f.close();}QList<EMailAddress> AddressList::contacts(){ return _contacts;}QList<EMailAddress> AddressList::emailContacts(){ return _emailContacts;}void AddressList::deleteAddress(EMailAddress adr){ if ( _emailContacts.contains(adr) ) { int index = _emailContacts.indexOf(adr); EMailAddress& eaddr = _emailContacts[index]; eaddr.deleted = true; _dirty = true; }}void AddressList::addAddress(const QString &mailAdr){ QString name, email; MailMessage::parseEmailAddress(mailAdr, name, email); addAddress(name, email);}bool AddressList::addToContacts(const QString &mailAdr){ QString name, email; MailMessage::parseEmailAddress(mailAdr, name, email); // add to internal list first it it isn't there addAddress(name, email); if ( !_contactsLoaded ) load(); for ( QList<EMailAddress>::Iterator cit = _contacts.begin(); cit != _contacts.end(); ++cit) { if ( (*cit).email == email ) return false; }#ifndef QT_NO_COP QContact c = contact(name, email); QCopServiceRequest e( "Contacts", "addAndEditContact(QContact)" ); e << c; e.send();#endif return true;}void AddressList::addAddress(const QString &name, const QString &email){ EMailAddress a; a.name = name; a.email = email; //should do a mergeitem first though if ( !_emailContacts.contains(a) ) { _emailContacts.append(a); _dirty = true; } else { // let's see if we're updated int index = _emailContacts.indexOf(a); EMailAddress eaddr = _emailContacts.at(index); if ( name.length() > 0 && eaddr.name != name ) { eaddr.name = name; _dirty = true; } }}void AddressList::addContact(const QContact &c){// qDebug("addressList adding %s, %s", c.fullName().data(), c.defaultEmail().data() );// qDebug("pimrec has uid %s", c.uid().toString().data() );#ifndef QT_NO_COP QCopServiceRequest e( "Contacts", "addContact(QContact)" ); e << c; e.send();#endif}AddressView::AddressView(QWidget *parent) : QTreeWidget(parent){}void AddressView::keyPressEvent(QKeyEvent* e){ switch( e->key() ) {#ifdef QTOPIA_PHONE case Qt::Key_Select:#endif case Qt::Key_Space: case Qt::Key_Return:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?