00001
00002 #include "accountview.h"
00003 #include "accountitem.h"
00004 #include "selectstore.h"
00005
00006 #include <libmailwrapper/settings.h>
00007 #include <libmailwrapper/mailwrapper.h>
00008 #include <libmailwrapper/mailtypes.h>
00009 #include <libmailwrapper/abstractmail.h>
00010
00011
00012 #include <opie2/odebug.h>
00013 #include <qpe/qpeapplication.h>
00014 #include <qpe/config.h>
00015
00016
00017 #include <qmessagebox.h>
00018 #include <qpopupmenu.h>
00019
00020 using namespace Opie::Core;
00021 AccountView::AccountView( QWidget *parent, const char *name, WFlags flags )
00022 : QListView( parent, name, flags )
00023 {
00024 setSorting(0);
00025 setSelectionMode(Single);
00026 m_rightPressed = false;
00027
00028 connect( this, SIGNAL( selectionChanged(QListViewItem*) ),
00029 SLOT( slotSelectionChanged(QListViewItem*) ) );
00030 connect( this, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ),this,
00031 SLOT( slotMouseButton(int,QListViewItem*,const QPoint&,int) ) );
00032 connect( this, SIGNAL(clicked(QListViewItem*) ),this,
00033 SLOT( slotMouseClicked(QListViewItem*) ) );
00034 m_currentItem = 0;
00035 readSettings();
00036 }
00037
00038 AccountView::~AccountView()
00039 {
00040 imapAccounts.clear();
00041 mhAccounts.clear();
00042 }
00043
00044 void AccountView::readSettings()
00045 {
00046 Config cfg("mail");
00047 cfg.setGroup( "Settings" );
00048 m_clickopens = cfg.readBoolEntry("clickOpensFolder",true);
00049 }
00050
00051 void AccountView::slotSelectionChanged(QListViewItem*item)
00052 {
00053 if (!item) {
00054 emit serverSelected(0);
00055 return;
00056 }
00057 AccountViewItem *view = static_cast<AccountViewItem *>(item);
00058 emit serverSelected(view->isServer());
00059 }
00060
00061 QMap<int,QString> AccountView::currentServerMenu()const
00062 {
00063 QMap<int,QString> smap;
00064 AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
00065 if (!view)return smap;
00066 smap = view->serverMenu();
00067 return smap;
00068 }
00069
00070 QMap<int,QString> AccountView::currentFolderMenu()const
00071 {
00072 QMap<int,QString> fmap;
00073 AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
00074 if (!view)return fmap;
00075 fmap = view->folderMenu();
00076 return fmap;
00077 }
00078
00079 void AccountView::slotContextMenu(int id)
00080 {
00081 AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
00082 if (!view) return;
00083 bool del = view->contextMenuSelected(id);
00084 if (!del && view->isServer()!=2) {
00085 emit refreshMenues(view->isServer());
00086 }
00087 }
00088
00089 void AccountView::slotRightButton(int, QListViewItem * item,const QPoint&,int)
00090 {
00091 m_rightPressed = true;
00092 if (!item) return;
00093 AccountViewItem *view = static_cast<AccountViewItem *>(item);
00094 QPopupMenu*m = view->getContextMenu();
00095 if (!m) return;
00096 connect(m,SIGNAL(activated(int)),this,SLOT(slotContextMenu(int)));
00097 m->setFocus();
00098 m->exec( QPoint( QCursor::pos().x(), QCursor::pos().y()) );
00099 delete m;
00100 }
00101
00102 void AccountView::slotLeftButton(int, QListViewItem *,const QPoint&,int)
00103 {
00104 m_rightPressed = false;
00105 }
00106
00107 void AccountView::slotMouseClicked(QListViewItem*item)
00108 {
00109 if (m_rightPressed) return;
00110 if (!item || m_currentItem == item||!m_clickopens) return;
00111
00112 m_currentItem = item;
00113 refresh(m_currentItem);
00114 }
00115
00116 void AccountView::slotMouseButton(int button, QListViewItem * item,const QPoint&pos,int column)
00117 {
00118 if (button==1) {
00119 slotLeftButton(button,item,pos,column);
00120 } else if (button==2) {
00121 slotRightButton(button,item,pos,column);
00122 }
00123 }
00124
00125 void AccountView::populate( QList<Account> list )
00126 {
00127 clear();
00128
00129 imapAccounts.clear();
00130 mhAccounts.clear();
00131 mhAccounts.append(new MHviewItem(AbstractMail::defaultLocalfolder(),this));
00132
00133 Account *it;
00134 for ( it = list.first(); it; it = list.next() ) {
00135 if ( it->getType() == MAILLIB::A_IMAP ) {
00136 IMAPaccount *imap = static_cast<IMAPaccount *>(it);
00137 odebug << "added IMAP " + imap->getAccountName() << oendl;
00138 imapAccounts.append(new IMAPviewItem( imap, this ));
00139 } else if ( it->getType() == MAILLIB::A_POP3 ) {
00140 POP3account *pop3 = static_cast<POP3account *>(it);
00141 odebug << "added POP3 " + pop3->getAccountName() << oendl;
00142
00143 (void) new POP3viewItem( pop3, this );
00144 } else if ( it->getType() == MAILLIB::A_NNTP ) {
00145 NNTPaccount *nntp = static_cast<NNTPaccount *>(it);
00146 odebug << "added NNTP " + nntp->getAccountName() << oendl;
00147
00148 (void) new NNTPviewItem( nntp, this );
00149 } else if ( it->getType() == MAILLIB::A_MH ) {
00150 }
00151 }
00152 }
00153
00154 void AccountView::refresh(QListViewItem *item)
00155 {
00156
00157 odebug << "AccountView refresh..." << oendl;
00158 if ( item )
00159 {
00160 m_currentItem = item;
00161 QValueList<RecMailP> headerlist;
00162 AccountViewItem *view = static_cast<AccountViewItem *>(item);
00163 view->refresh(headerlist);
00164 emit refreshMailview(headerlist);
00165 }
00166 }
00167
00168 void AccountView::refreshCurrent()
00169 {
00170 m_currentItem = currentItem();
00171 if ( !m_currentItem ) return;
00172 QValueList<RecMailP> headerlist;
00173 AccountViewItem *view = static_cast<AccountViewItem *>(m_currentItem);
00174 view->refresh(headerlist);
00175 emit refreshMailview(headerlist);
00176 }
00177
00178 void AccountView::refreshAll()
00179 {
00180 }
00181
00182 RecBodyP AccountView::fetchBody(const RecMailP&aMail)
00183 {
00184 QListViewItem*item = selectedItem ();
00185 if (!item) return new RecBody();
00186 AccountViewItem *view = static_cast<AccountViewItem *>(item);
00187 return view->fetchBody(aMail);
00188 }
00189
00190 void AccountView::setupFolderselect(Selectstore*sels)
00191 {
00192 QPEApplication::showDialog( sels );
00193 QStringList sFolders;
00194 unsigned int i = 0;
00195 for (i=0; i < mhAccounts.count();++i)
00196 {
00197 mhAccounts[i]->refresh(false);
00198 sFolders = mhAccounts[i]->subFolders();
00199 sels->addAccounts(mhAccounts[i]->getWrapper(),sFolders);
00200 }
00201 for (i=0; i < imapAccounts.count();++i)
00202 {
00203 if (imapAccounts[i]->offline())
00204 continue;
00205 imapAccounts[i]->refreshFolders(false);
00206 sels->addAccounts(imapAccounts[i]->getWrapper(),imapAccounts[i]->subFolders());
00207 }
00208 }
00209
00210 void AccountView::downloadMails(const FolderP&fromFolder,AbstractMail*fromWrapper)
00211 {
00212 AbstractMail*targetMail = 0;
00213 QString targetFolder = "";
00214 Selectstore sels;
00215 setupFolderselect(&sels);
00216 if (!sels.exec()) return;
00217 targetMail = sels.currentMail();
00218 targetFolder = sels.currentFolder();
00219 if ( (fromWrapper==targetMail && fromFolder->getName()==targetFolder) ||
00220 targetFolder.isEmpty())
00221 {
00222 return;
00223 }
00224 if (sels.newFolder() && !targetMail->createMbox(targetFolder))
00225 {
00226 QMessageBox::critical(0,tr("Error creating new Folder"),
00227 tr("<center>Error while creating<br>new folder - breaking.</center>"));
00228 return;
00229 }
00230 odebug << "Targetfolder: " << targetFolder.latin1() << "" << oendl;
00231 odebug << "Fromfolder: " << fromFolder->getName().latin1() << "" << oendl;
00232 fromWrapper->mvcpAllMails(fromFolder,targetFolder,targetMail,sels.moveMails());
00233 refreshCurrent();
00234 }
00235
00236 bool AccountView::currentisDraft()
00237 {
00238 AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
00239 if (!view) return false;
00240 return view->isDraftfolder();
00241 }