📄 abtable.cpp
字号:
QMap<QCString, int> identifierToKey = PimContact::identifierToKeyMap(); for ( QStringList::Iterator it = selectedFields.begin(); it != selectedFields.end(); ++it) { if ( *it == "Contact" ) { // No tr headerKeyFields.append( FREQ_CONTACT_FIELD ); } else { int field = identifierToKey[ (*it).data() ]; headerKeyFields.append( field ); } } setFields( headerKeyFields, sizeList ); }#endif if ( mSortColumn > -1 ) { reload(); horizontalHeader()->setSortIndicator(mSortColumn,!mAscending); } constructorDone = TRUE; fitToSize(); refresh();}void AbTable::drawContents( QPainter *p, int cx, int cy, int cw, int ch ){ //qDebug("AbTable::drawContents( %d, %d, %d, %d )", cx, cy, cw, ch ); QTable::drawContents( p, cx, cy, cw, ch );}void AbTable::saveSettings(){ QMap<int,QCString> keyToIdentifier = PimContact::keyToIdentifierMap(); QHeader *header = horizontalHeader(); QStringList fieldList, sizeList; for ( int i = 0; i < header->count(); i++) { if ( headerKeyFields[i] != FREQ_CONTACT_FIELD ) fieldList.append( keyToIdentifier[ headerKeyFields[i] ] ); else fieldList.append( "Contact" ); // No tr sizeList.append( QString::number(header->sectionSize(i)) ); }#ifdef QTOPIA_DESKTOP QSettings *settings = gQtopiaDesktopConfig->createQSettings(); settings->writeEntry( "/addressbook/fields", fieldList ); settings->writeEntry( "/addressbook/colwidths", sizeList ); settings->writeEntry( "/addressbook/sortcolumn", mSortColumn ); settings->writeEntry( "/addressbook/ascending", mAscending ); gQtopiaDesktopConfig->deleteQSettings();#else Config config( "addressbook" ); config.setGroup( "View" ); config.writeEntry("fields", fieldList, ',' ); config.writeEntry("colwidths", sizeList, ',' ); config.writeEntry("sortcolumn", mSortColumn ); config.writeEntry("ascending", mAscending );#endif}//------------------------------------------------------------------------------ContactSelector::ContactSelector( bool allowNew, ContactXmlIO *contacts, QWidget *parent, const char *name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ), mSelectedType( ContactSelector::NoSelection ){ setCaption( tr("Select Contacts") ); QVBoxLayout *l = new QVBoxLayout( this ); table = new AbTable( contacts, this, "selectorTable" ); table ->setSelectionMode( AbTable::Extended ); connect( table, SIGNAL(clicked()), this, SLOT(contactSelected()) ); table->reload(); l->addWidget( table ); if( allowNew ) {#ifndef QTOPIA_PHONE //TODO pda#else ContextMenu *menu = new ContextMenu( this ); QAction *actionNew = new QAction( tr( "New" ), Resource::loadIconSet( "new" ) , QString::null, 0, this, 0 ); connect( actionNew, SIGNAL(activated()), this, SLOT(newSelected()) ); actionNew->addTo( menu );#endif }#ifndef QTOPIA_DESKTOP QPEApplication::setMenuLike( this, TRUE );#endif}void ContactSelector::newSelected(){ setSelectedType( SelectedNew );}void ContactSelector::contactSelected(){ setSelectedType( SelectedContacts );}void ContactSelector::setSelectedType( SelectTypes t ){ mSelectedType = t; accept();}ContactSelector::SelectTypes ContactSelector::selectedType() const{ return mSelectedType;}void ContactSelector::accept(){ QDialog::accept();}QValueList<PimContact> ContactSelector::selected() const{ QValueList<PimContact> selCnts; if( selectedType() == SelectedContacts ) selCnts = table->selected(); return selCnts;}QSize ContactSelector::sizeHint() const{ return QSize( width(), qApp->desktop()->height() );}//------------------------------------------------------------------------------PhoneTypeSelector::PhoneTypeSelector( const PimContact &cnt, const QString &number, QWidget *parent, const char *name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ), mContact( cnt ){ setCaption(tr("Phone Type")); QVBoxLayout *l = new QVBoxLayout( this ); l->setAutoAdd( TRUE ); mLabel = new QLabel( this ); mPhoneType = new QListView( this ); mPhoneType->setHScrollBarMode( QScrollView::AlwaysOff ); mPhoneType->addColumn( tr("Type") ); mPhoneType->addColumn( tr("Current Value") ); mPhoneType->setColumnWidth( 1, mPhoneType->visibleWidth()-mPhoneType->columnWidth( 0 ) ); mPhoneType->setAllColumnsShowFocus( TRUE );#ifdef QTOPIA_PHONE QFont f = mPhoneType->font(); f.setPointSize( 12 ); mPhoneType->setFont( f );#endif mLabel->setText( "<qt>"+tr("Save '%1' as:").arg( number )+"</qt>" ); QString ab = "addressbook/"; QListViewItem *item = 0; item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.homePhone() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"homephone") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::HomePhone; mPhoneType->setSelected( item, TRUE ); item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.homeMobile() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"homemobile") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::HomeMobile; item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.homeFax() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"homefax") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::HomeFax; item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.businessPhone() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"businessphone") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::BusinessPhone; item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.businessMobile() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"businessmobile") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::BusinessMobile; item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.businessFax() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"businessfax") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::BusinessFax; item = new QListViewItem( mPhoneType, item, "", verboseIfEmpty( mContact.businessPager() ) ); item->setPixmap( 0, Resource::loadPixmap(ab+"businesspager") ); mPhoneType->insertItem( item ); mItemToPhoneType[item] = PimContact::BusinessPager; connect( mPhoneType, SIGNAL(returnPressed(QListViewItem *)), this, SLOT(accept()) ); mPhoneType->setFocus();#ifdef QTOPIA_PHONE if( !Global::mousePreferred() ) { if( mPhoneType->isModalEditing() ) mPhoneType->setModalEditing( TRUE ); }#endif}QString PhoneTypeSelector::verboseIfEmpty( const QString &number ){ if( number.isEmpty() ) return tr("(empty)"); return number;}int PhoneTypeSelector::selected() const{ QListViewItem *item = mPhoneType->currentItem(); if( !item ) return -1; return mItemToPhoneType[item];}void PhoneTypeSelector::accept(){ QDialog::accept(); if( selected() != -1 ) emit selected( (PimContact::ContactFields)selected() );}#ifdef QTOPIA_PHONE/* matches phone keypad input \a needle to string \a haystack, starting at \a offset, for n characters, returning how many matched consecutively */int AbUtil::phoneKeyMatchN(const QString& haystack, int offset, const QString& needle, int n){ // XXX should use PkIMMatcher static QString phonekey[10]; if ( phonekey[0].isNull() ) { phonekey[0] = " 0"; phonekey[1] = "1"; phonekey[2] = "2abcABC"; phonekey[3] = "3defDEF"; phonekey[4] = "4ghiGHI"; phonekey[5] = "5jklJKL"; phonekey[6] = "6mnoMNO"; phonekey[7] = "7pqrsQPRS"; phonekey[8] = "8tuvTUV"; phonekey[9] = "9wxyzWXYZ"; } for (int i=0; i<n; i++) { //does this key press match its corresponding characters? if( !needle[i].isDigit() ) { if( needle[i].lower() != haystack[offset+i].lower() ) //not a digit, has to match literally return i; //otherwise matches, fall through } else { int pk = needle[i].latin1() - '0'; if( pk < 0 || pk > 9 ) { qDebug("BUG : AbUtil::phoneKeyMatchN - Digit in needle doesn't translate to a valid index."); return i; } QChar ch = haystack[i+offset].lower(); if ( !phonekey[pk].contains(ch) ) return i; /*ch.unicode() - phonekey[pk][0].unicode();*/ } } return n; // match!}#endif#ifdef QTOPIA_PHONE#include <qtopia/pim/addressbookaccess.h>PimContact AbUtil::findContactByNumber( const QString& number, int *matchingField, AddressBookAccess *access ){ QString value; int bestMatchField = -1; PimContact matchingContact; int bestMatch = 0; int match; // Search the address book for a match. bool ownAccess = FALSE; if( !access ) { access = new AddressBookAccess(); ownAccess = TRUE; } AddressBookIterator iter(*access); for ( ; iter.current(); ++iter ) { for( int f = 0; f < numPhoneFields ; ++f ) { value = iter.current()->field( phoneFields[f] ); if ( value.length() > 0 ) { value = PhoneUtils::resolveLetters( value ); match = PhoneUtils::matchNumbers( number, value ); if ( match > bestMatch ) { bestMatch = match; bestMatchField = phoneFields[f]; matchingContact = *iter.current(); } } } } if( ownAccess ) delete access; *matchingField = bestMatchField; return matchingContact;}#endifbool isDelim( const QChar &delim ){ static const char *delims = " .-()\":"; static int numDelims = strlen(delims); for( int i = 0 ; i < numDelims ; ++i ) if( delim == delims[i] ) return TRUE; return FALSE;}int AbUtil::findPrefixInName( const QString &name, const QString &prefix ){ int nameLen = name.length(); int prefixLen = prefix.length(); if( prefixLen > nameLen || name.isEmpty() || prefix.isEmpty() ) return -1; int numMatchingChars; int matchingIndex = -1; int startingIndex; for( int i = 0 ; i < nameLen ; ++i ) { while( i < nameLen && isDelim(name[i]) ) ++i; if( i >= nameLen ) break; numMatchingChars = 0; startingIndex = i;#ifdef QTOPIA_PHONE if( phoneKeyMatchN( name, startingIndex, prefix, prefixLen ) == prefixLen )#else if( name.mid( startingIndex, prefix.length() ).lower() == prefix.lower() )#endif { matchingIndex = startingIndex; break; } else { //goto next token while( i < nameLen && !isDelim(name[i]) ) ++i; } } return matchingIndex;}#ifdef QTOPIA_PHONE//these keys are the custom fields used for sim integration with PimContactconst QString AbUtil::SIM_HP = "SIM_HP";const QString AbUtil::SIM_HF = "SIM_HF";const QString AbUtil::SIM_HM = "SIM_HM";const QString AbUtil::SIM_BP = "SIM_BP";const QString AbUtil::SIM_BF = "SIM_BF";const QString AbUtil::SIM_BM = "SIM_BM";const QString AbUtil::SIM_BPA = "SIM_BPA";//indicates the file as of a sim contact is only part of the person's actual name//due to the size constraints of the simcardconst QChar AbUtil::SIM_PARTIAL_INDICATOR = '+';//these are the phone types of a contactconst int AbUtil::phoneFields[] ={ PimContact::HomePhone, PimContact::HomeMobile, PimContact::BusinessPhone, PimContact::BusinessMobile, PimContact::BusinessFax, PimContact::HomeFax, PimContact::BusinessPager};const int AbUtil::numPhoneFields = 7;#include <qtopia/phone/phone.h>#include <qtopia/phone/phonebook.h>/* converts all the sim card entries specified by list into contacts. it integrates numbers with the same name, eg all entries on the sim with the text 'John Smith' will end up in the same contact. it tries to work out the kind of number for each entry, otherwise just shoves it into the next available phone field.*/QList<PimContact> AbUtil::phoneBookToContacts( const QValueList<PhoneBookEntry>& list){ QList<PimContact> simCnts; //convert the phone book entries in the list to contacts. QValueList<PhoneBookEntry>::ConstIterator it; for( it = list.begin() ; it != list.end() ; ++it ) { QString name = (*it).text().stripWhiteSpace(), number = (*it).number().stripWhiteSpace(); if( name.isEmpty() || number.isEmpty() ) continue; //do some parsing to determine the type of phone number int phoneType = -1; AbUtil::parseSimText( name, phoneType ); // is this sim entry part of an existing contact? PimContact *cnt = 0; bool existing = FALSE; for( int i = 0 ; i < (int)simCnts.count() ; ++i ) { if( simCnts.at(i)->fileAs() == name ) { /* it's part of an existing contact. if we haven't parsed a phone type, put it in the next available spot */ if( phoneType < 0 ) { phoneType = AbUtil::phoneFields[0]; for( int j = 0 ; j < AbUtil::numPhoneFields ; ++j ) if( simCnts.at(i)->field( AbUtil::phoneFields[j] ) .stripWhiteSpace().isEmpty() ) { phoneType = phoneFields[j]; break; } } simCnts.at(i)->setField( phoneType, number ); existing = TRUE; cnt = simCnts.at(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -