⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 folderlistview.cpp

📁 Qtopia下的邮件处理程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** 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 "folderlistview.h"#include <qtopia/qtopiaapplication.h>#include "searchview.h"#include "accountlist.h"#include "emailfolderlist.h"#include "common.h"#include <qmessagebox.h>#include <qcursor.h>#include <qpainter.h>#include <qdesktopwidget.h>#include <qevent.h>#include <QHeaderView>using namespace QtMail;FolderListItem::FolderListItem(QTreeWidget *parent, Folder *in)    : QTreeWidgetItem( parent ){    _folder = in;    _statusText = "";    _highlight = false;    QString pixmap = "folder"; // No tr    if ( _folder->folderType() == FolderTypeSearch ) {	pixmap = "find"; // No tr    } else if ( _folder->folderType() == FolderTypeSystem ) {	if ( ((SystemFolder *) _folder)->systemType() == SystemTypeSearch )	    pixmap = "find"; // No tr    }    setText(0, _folder->displayName() );    int extent = qApp->style()->pixelMetric(QStyle::PM_SmallIconSize);    setIcon( 0, QIcon( ":icon/"+pixmap ).pixmap(extent));}FolderListItem::FolderListItem(QTreeWidgetItem *parent, Folder *in)    : QTreeWidgetItem(parent){    _folder = in;    _statusText = "";    _highlight = false;    QString pixmap = "folder"; // No tr    if ( _folder->folderType() == FolderTypeSearch ) {	pixmap = "find"; // No tr    } else if ( _folder->folderType() == FolderTypeSystem ) {	if ( ((SystemFolder *) _folder)->systemType() == SystemTypeSearch )	    pixmap = "find"; // No tr    }    setText(0, _folder->displayName() );    int extent = qApp->style()->pixelMetric(QStyle::PM_SmallIconSize);    setIcon( 0, QIcon( ":icon/"+pixmap ).pixmap(extent));}Folder* FolderListItem::folder(){    return _folder;}void FolderListItem::setStatusText( const QString &str, bool highlight, const QColor &col ){    _statusText = str;    _highlight = highlight;    _col = col;}void FolderListItem::statusText( QString *str, bool *highlight, QColor *col ){    *str = _statusText;    *highlight = _highlight;    *col = _col;}int FolderListItem::depth(){    int count = 0;    QTreeWidgetItem *item = this;    while (item->parent()) {	++count;	item = item->parent();    }    return count;}#ifdef QTOPIA4_TODOvoid FolderListItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int alignment ){    QColorGroup _cg( cg );    QString t = text( column );    QTreeWidget *lv = listView();    int r = lv ? lv->itemMargin() : 1;    const QPixmap * icon = pixmap( column );    p->fillRect( 0, 0, width, height(), cg.brush( QColorGroup::Base ) );    int marg = r;    if ( isSelected() &&	 (column==0 || listView()->allColumnsShowFocus()) ) {	p->fillRect( r - marg, 0, width - r + marg, height(),		     cg.brush( QColorGroup::Highlight ) );	p->setPen( cg.highlightedText() );    } else {	p->setPen( cg.text() );    }    if ( icon ) {	p->drawPixmap( r, (height()-icon->height())/2, *icon );	r += icon->width() + listView()->itemMargin();    }    if ( !t.isEmpty() ) {	if ( ! (alignment & AlignTop | alignment & AlignBottom) )            alignment |= AlignVCenter;		if ( !_statusText.isEmpty() ) {	    if ( _highlight ) {		QFont font = p->font();		font.setBold( true );		p->setFont( font );	    }		    QRect rBound;		    p->drawText( r, 0, width-marg-r, height(), alignment, t, -1, &rBound );	    r += rBound.width();		    if ( !isSelected() && _highlight ) {		_cg.setColor( QColorGroup::Text, _col );		p->setPen( _cg.text() );	    }		    p->drawText( r, 0, width-marg-r, height(), alignment, _statusText);	} else {	    p->drawText( r, 0, width-marg-r, height(), alignment, t );	}	    }    _cg.setColor( QColorGroup::Text, column );}int FolderListItem::width( const QFontMetrics& fm,  const QTreeWidget* lv, int c ) const{    int w;    if ( !_statusText.isEmpty() ) {	QFont font = lv->font();	font.setBold( _highlight );	QFontMetrics fm2( font );	    	w = fm2.width( text( c ) + _statusText ) + lv->itemMargin() * 2;    } else {    	w = fm.width( text( c ) ) + lv->itemMargin() * 2;    }    const QPixmap * pm = pixmap( c );    if ( pm )	w += pm->width() + lv->itemMargin(); // ### correct margin stuff?	    return qMax( w, QApplication::globalStrut().width() );}#endif/*  We want a particular layout, use sort to enforce it  */QString FolderListItem::key(int c, bool) const{  if ( c != 0 )    return QString();  int type = _folder->folderType();  switch( type ) {    case FolderTypeSystem:    {        // system search folder(last search) has a mailbox equal to one of the other real mailboxes,        // but we still want it to appear last though.      if ( ((SystemFolder *) _folder)->isSearch() )        return ( "77" );          QChar i = '7';      QString s = _folder->mailbox();      if ( s == InboxString)        i = '1';      else if ( s == OutboxString )        i = '2';      else if ( s == DraftsString )        i = '3';      else if ( s == SentString )        i = '4';      else if ( s == TrashString )        i = '5';              return QString::number( type ) + i;    }    case FolderTypeAccount:    {      return "2" + text(0);    }    case FolderTypeMailbox:    {      return "3" + text(0);    }    default:    //folderTypeSearch    {      return "4" + text(0);    }  }}/* Folder list view  */FolderListView::FolderListView(MailboxList *list, QWidget *parent, const char *name)    : QTreeWidget( parent ){    setObjectName( name );    if ( qApp->desktop()->width() < TabWidthTrigger )	setFrameStyle( NoFrame );    sortItems(0, Qt::AscendingOrder );    _mailboxList = list;    searchFolder = new SystemFolder(SystemTypeSearch, LastSearchString);    FolderListItemDelegate *delegate = new FolderListItemDelegate( this );    setItemDelegate( delegate );    connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),	    this, SLOT(folderChanged(QTreeWidgetItem*)) );    header()->hide();    connect( &menuTimer, SIGNAL(timeout()), SLOT(showFolderMenu()) );    connect( this, SIGNAL(itemSelectionChanged()), SLOT(cancelMenuTimer()) );#ifdef QTOPIA_PHONE    connect( this, SIGNAL(itemClicked(QTreeWidgetItem*,int)),	     this, SLOT(itemClicked(QTreeWidgetItem*)) );#endif}FolderListView::~FolderListView(){}Folder* FolderListView::currentFolder(){    FolderListItem *item = (FolderListItem *) currentItem();    if ( item != NULL )	return item->folder();    return NULL;}MailAccount* FolderListView::currentAccount(){    FolderListItem *item = (FolderListItem*)currentItem();    MailAccount *account = 0;    while (item &&	   item->folder() &&	   item->folder()->folderType() != FolderTypeAccount)	item = (FolderListItem*)item->parent();    if (item &&	item->folder() &&	item->folder()->folderType() == FolderTypeAccount)	account = (MailAccount*)item->folder();    return account;}void FolderListView::keyPressEvent( QKeyEvent *e ){    switch( e->key() ) {#ifdef QTOPIA_PHONE        case Qt::Key_Select:#endif        case Qt::Key_Space:        case Qt::Key_Return:        case Qt::Key_Enter:        {	    emit viewMessageList();	}        break;        default:  QTreeWidget::keyPressEvent( e );    }}QSize FolderListView::sizeHint() const{    return QSize(-1,-1);}QSize FolderListView::minimumSizeHint() const{    return QSize(-1,-1);}void FolderListView::setupFolders(AccountList *list){    clear();    //build system folders    QStringList mboxList = _mailboxList->mailboxes();    for (QStringList::Iterator it = mboxList.begin(); it != mboxList.end(); ++it) {	new FolderListItem(this, new SystemFolder(SystemTypeMailbox, *it ) );    }    new FolderListItem(this, searchFolder);    //build inbox    QListIterator<MailAccount*> itAccount = list->accountIterator();    FolderListItem* inbox = (FolderListItem*)topLevelItem(0);    //build all other accounts    while (itAccount.hasNext()) {           MailAccount* accountInfo = itAccount.next();        FolderListItem* accountItem = new FolderListItem(inbox, accountInfo);        buildImapFolder(accountItem,accountInfo);    }    setItemExpanded(inbox, true);}QModelIndex FolderListView::next(QModelIndex mi, bool nextParent){    if (mi.child(0,0).isValid() && !nextParent)      return mi.child(0,0);    if (mi.sibling( mi.row() + 1, mi.column() ).isValid())	return mi.sibling( mi.row() + 1, mi.column() );    if (mi.parent().isValid())	 return next( mi.parent(),true);    return QModelIndex(); // invalid e.g. correct}QTreeWidgetItem* FolderListView::next(QTreeWidgetItem *item){  QModelIndex m = indexFromItem(item);  QModelIndex nextIndex = next(m);  if(!nextIndex.isValid())    return 0;  else    return itemFromIndex(nextIndex);}// receives an account, finds it in the listview and makes// any necessary updatesvoid FolderListView::updateAccountFolder(MailAccount *account){        QModelIndex index = model()->index( 0, 0 ); //inbox    FolderListItem *inbox_item = (FolderListItem *)itemFromIndex( index );    FolderListItem *item = (FolderListItem *) inbox_item->child(0);    Folder *folder;    while (item != NULL) {	folder = item->folder();	if (folder->folderType() == FolderTypeAccount ) {	    if ( folder == account ) {		bool selected = selectedChildOf(item);		QString statusText;		bool statusHighlight;		QColor statusCol;		item->statusText( &statusText, &statusHighlight, &statusCol );        buildImapFolder(item,account); //rebuild imap subtree		item->setStatusText( statusText, statusHighlight, statusCol );		    		if ( selected ) {		    setCurrentItem( item );		    setItemExpanded(item, true);		}		folderChanged( currentItem() );		return;	    }	}	item = (FolderListItem *)next( item );    }    //if we get here, it was a new folder    item = new FolderListItem(inbox_item,account);    buildImapFolder(item,account);}void FolderListView::deleteAccountFolder(MailAccount *account){    Folder *folder;        QModelIndex rootindex = model()->index( 0, 0 );    QModelIndex index = next( rootindex);    if(index == rootindex)      return;    for ( ; index.isValid(); index = next( index ) ) {	folder = ( (FolderListItem *) itemFromIndex( index ) )->folder();	if ( folder->folderType() == FolderTypeAccount ) {	    //make updates	    if ( folder == account ) {		EmailFolderList *inbox = _mailboxList->mailbox(InboxString);		QListIterator<Email*> it2 = inbox->entryIterator();		while ( it2.hasNext() ) {		    Email *mail = it2.next();		    if ( mail->fromAccount() == account->id() ) {			inbox->removeMail( mail->uuid(), true );		    }		}		delete itemFromIndex( index );		folderChanged( currentItem() );		return;	    }	}    }}bool FolderListView::selectedChildOf(FolderListItem *folder){   FolderListItem *selected = (FolderListItem *) currentItem();   if (selected == NULL)       return false;    if (folder == selected)        return true;    while ( ( selected = (FolderListItem *) selected->parent() ) != NULL ) {        if (folder == selected)            return true;    }    return false;}void FolderListView::changeToSystemFolder(const QString &str){    Folder *folder;    bool search = (str == LastSearchString);        QModelIndex index = model()->index( 0, 0 ); //inbox    for ( ; index.isValid(); index = next( index ) ) {	FolderListItem *item = (FolderListItem *) itemFromIndex( index );	folder = item->folder();	if ( folder->folderType() == FolderTypeSystem ) {	    if ( search && ((SystemFolder *) folder)->isSearch() ) {		setCurrentItem( item );		break;	    } else if ( folder->mailbox() == str ) {		setCurrentItem( item );		break;	    }	}    }}void FolderListView::updateFolderStatus(const QString &mailbox, const QString &txt, bool highlight, const QColor &col){    Folder *folder;    QModelIndex index = model()->index( 0, 0 );    for ( ; index.isValid(); index = next( index ) ) {	FolderListItem *item = (FolderListItem *) itemFromIndex( index );	folder = item->folder();	if ( folder->folderType() == FolderTypeSystem ) {	    if ( ( (SystemFolder *) folder )->mailbox() == mailbox ) {		item->setStatusText( txt, highlight, col );		dataChanged( index, index );		return;	    }	}    }}void FolderListView::updateAccountStatus(const Folder *account, const QString &txt, bool highlight, const QColor &col){    Folder *folder;    QModelIndex index = model()->index( 0, 0 );    for ( ; index.isValid(); index = next( index ) ) {	FolderListItem *item = (FolderListItem *) itemFromIndex( index );	folder = item->folder();	if ( folder == account ) {	    item->setStatusText( txt, highlight, col );	    dataChanged( index, index );	    return;	}    }}void FolderListView::setLastSearch(Search *search){    searchFolder->setSearch( search );}Search* FolderListView::lastSearch(){    return searchFolder->search();}void FolderListView::folderChanged(QTreeWidgetItem *folder){    QString str;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -