/** * kmacctimap.cpp * * Copyright (c) 2000 Michael Haeckel * * This file is based on kmacctexppop.cpp by Don Sanders * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "kmacctimap.moc" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kmacctimap.h" #include "kmglobal.h" #include "kbusyptr.h" #include "kmacctfolder.h" #include "kmbroadcaststatus.h" #include "kmfoldertree.h" #include #include #include #include #include #include //----------------------------------------------------------------------------- KMAcctImap::KMAcctImap(KMAcctMgr* aOwner, const QString& aAccountName): KMAcctImapInherited(aOwner, aAccountName) { init(); mSlave = NULL; mTotal = 0; connect(KMBroadcastStatus::instance(), SIGNAL(signalAbortRequested()), this, SLOT(slotAbortRequested())); connect(&mIdleTimer, SIGNAL(timeout()), SLOT(slotIdleTimeout())); KIO::Scheduler::connect( SIGNAL(slaveError(KIO::Slave *, int, const QString &)), this, SLOT(slotSlaveError(KIO::Slave *, int, const QString &))); } //----------------------------------------------------------------------------- KMAcctImap::~KMAcctImap() { killAllJobs(); if (mSlave) KIO::Scheduler::disconnectSlave(mSlave); emit deleted(this); } //----------------------------------------------------------------------------- const char* KMAcctImap::type(void) const { return "imap"; } //----------------------------------------------------------------------------- void KMAcctImap::init(void) { mHost = ""; struct servent *serv = getservbyname("imap-4", "tcp"); if (serv) { mPort = ntohs(serv->s_port); } else { mPort = 143; } mLogin = ""; mPasswd = ""; mAuth = "*"; mStorePasswd = FALSE; mProgressEnabled = FALSE; mPrefix = "/"; mAutoExpunge = TRUE; mHiddenFolders = FALSE; mUseSSL = FALSE; mUseTLS = FALSE; mIdle = TRUE; } //----------------------------------------------------------------------------- void KMAcctImap::pseudoAssign(KMAccount* account) { assert(account->type() == "imap"); KMAcctImap *acct = static_cast(account); setName(acct->name()); setCheckInterval( 0 ); setCheckExclude( TRUE ); setFolder(acct->folder()); setHost(acct->host()); setPort(acct->port()); setPrefix(acct->prefix()); setLogin(acct->login()); setAuth(acct->auth()); setAutoExpunge(acct->autoExpunge()); setHiddenFolders(acct->hiddenFolders()); setStorePasswd(acct->storePasswd()); setPasswd(acct->passwd(), acct->storePasswd()); setUseSSL(acct->useSSL()); setUseTLS(acct->useTLS()); } //----------------------------------------------------------------------------- void KMAcctImap::readConfig(KConfig& config) { KMAcctImapInherited::readConfig(config); mLogin = config.readEntry("login", ""); mStorePasswd = config.readNumEntry("store-passwd", FALSE); if (mStorePasswd) mPasswd = config.readEntry("passwd"); else mPasswd = ""; mHost = config.readEntry("host"); mPort = config.readNumEntry("port"); mAuth = config.readEntry("auth", "*"); mPrefix = config.readEntry("prefix", "/"); mAutoExpunge = config.readBoolEntry("auto-expunge", TRUE); mHiddenFolders = config.readBoolEntry("hidden-folders", FALSE); mUseSSL = config.readBoolEntry("use-ssl", FALSE); mUseTLS = config.readBoolEntry("use-tls", FALSE); } //----------------------------------------------------------------------------- void KMAcctImap::writeConfig(KConfig& config) { KMAcctImapInherited::writeConfig(config); config.writeEntry("login", mLogin); config.writeEntry("store-passwd", mStorePasswd); if (mStorePasswd) config.writeEntry("passwd", mPasswd); else config.writeEntry("passwd", ""); config.writeEntry("host", mHost); config.writeEntry("port", static_cast(mPort)); config.writeEntry("auth", mAuth); config.writeEntry("prefix", mPrefix); config.writeEntry("auto-expunge", mAutoExpunge); config.writeEntry("hidden-folders", mHiddenFolders); config.writeEntry("use-ssl", mUseSSL); config.writeEntry("use-tls", mUseTLS); } //----------------------------------------------------------------------------- QString KMAcctImap::encryptStr(const QString &aStr) const { unsigned int i, val; unsigned int len = aStr.length(); QCString result; result.resize(len+1); for (i=0; isetStatusProgressEnable( mProgressEnabled ); } mIdle = FALSE; if (mapJobData.isEmpty()) mIdleTimer.start(15000); else mIdleTimer.stop(); int total = 0, done = 0; for (QMap::Iterator it = mapJobData.begin(); it != mapJobData.end(); it++) { total += (*it).total; done += (*it).done; } if (total == 0) { mTotal = 0; return; } if (total > mTotal) mTotal = total; done += mTotal - total; KMBroadcastStatus::instance()->setStatusProgressPercent( 100*done / mTotal ); } //----------------------------------------------------------------------------- void KMAcctImap::slotIdleTimeout() { if (mIdle) { if (mSlave) KIO::Scheduler::disconnectSlave(mSlave); mSlave = NULL; mIdleTimer.stop(); } else { if (mSlave) { KIO::SimpleJob *job = KIO::special(getUrl(), QCString("NOOP"), FALSE); KIO::Scheduler::assignJobToSlave(mSlave, job); connect(job, SIGNAL(result(KIO::Job *)), this, SLOT(slotSimpleResult(KIO::Job *))); } else mIdleTimer.stop(); } } //----------------------------------------------------------------------------- void KMAcctImap::slotAbortRequested() { killAllJobs(); } //----------------------------------------------------------------------------- void KMAcctImap::killAllJobs() { QMap::Iterator it = mapJobData.begin(); for (it = mapJobData.begin(); it != mapJobData.end(); it++) if ((*it).parent) { KMFolderImap *fld = static_cast((*it).parent->folder); (*it).parent->mImapState = KMFolderTreeItem::imapFinished; fld->setUidNext(""); fld->sendFolderComplete((*it).parent, FALSE); (*it).parent->folder->quiet(FALSE); } if (mapJobData.begin() != mapJobData.end()) { mSlave->kill(); mSlave = NULL; } mapJobData.clear(); mJobList.setAutoDelete(true); mJobList.clear(); mJobList.setAutoDelete(false); displayProgress(); } //----------------------------------------------------------------------------- void KMAcctImap::killJobsForItem(KMFolderTreeItem * fti) { QMap::Iterator it = mapJobData.begin(); while (it != mapJobData.end()) { if (it.data().parent == fti) { killAllJobs(); break; } else it++; } } //----------------------------------------------------------------------------- void KMAcctImap::slotSimpleResult(KIO::Job * job) { QMap::Iterator it = mapJobData.find(job); if (it != mapJobData.end()) mapJobData.remove(it); if (job->error()) { job->showErrorDialog(); if (job->error() == KIO::ERR_SLAVE_DIED) mSlave = NULL; } displayProgress(); } //============================================================================= // // Class KMImapPasswdDialog // //============================================================================= KMImapPasswdDialog::KMImapPasswdDialog(QWidget *parent, const char *name, KMAcctImap *account, const QString &caption, const QString &login, const QString &passwd) :QDialog(parent,name,true) { // This function pops up a little dialog which asks you // for a new username and password if one of them was wrong or not set. QLabel *l; kernel->kbp()->idle(); act = account; KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon()); if (!caption.isNull()) setCaption(caption); QGridLayout *gl = new QGridLayout(this, 5, 2, 10); QPixmap pix(locate("data", QString::fromLatin1("kdeui/pics/keys.png"))); if(!pix.isNull()) { l = new QLabel(this); l->setPixmap(pix); l->setFixedSize(l->sizeHint()); gl->addWidget(l, 0, 0); } l = new QLabel(i18n("You need to supply a username and a\n" "password to access this mailbox."), this); l->setFixedSize(l->sizeHint()); gl->addWidget(l, 0, 1); l = new QLabel(i18n("Server:"), this); l->setMinimumSize(l->sizeHint()); gl->addWidget(l, 1, 0); l = new QLabel(act->host(), this); l->setMinimumSize(l->sizeHint()); gl->addWidget(l, 1, 1); l = new QLabel(i18n("Login Name:"), this); l->setMinimumSize(l->sizeHint()); gl->addWidget(l, 2, 0); usernameLEdit = new QLineEdit(login, this); usernameLEdit->setFixedHeight(usernameLEdit->sizeHint().height()); usernameLEdit->setMinimumWidth(usernameLEdit->sizeHint().width()); gl->addWidget(usernameLEdit, 2, 1); l = new QLabel(i18n("Password:"), this); l->setMinimumSize(l->sizeHint()); gl->addWidget(l, 3, 0); passwdLEdit = new QLineEdit(this,"NULL"); passwdLEdit->setEchoMode(QLineEdit::Password); passwdLEdit->setText(passwd); passwdLEdit->setFixedHeight(passwdLEdit->sizeHint().height()); passwdLEdit->setMinimumWidth(passwdLEdit->sizeHint().width()); gl->addWidget(passwdLEdit, 3, 1); connect(passwdLEdit, SIGNAL(returnPressed()), SLOT(slotOkPressed())); KButtonBox *bbox = new KButtonBox(this); bbox->addStretch(1); ok = bbox->addButton(i18n("OK")); ok->setDefault(true); cancel = bbox->addButton(i18n("Cancel")); bbox->layout(); gl->addMultiCellWidget(bbox, 4, 4, 0, 1); connect(ok, SIGNAL(pressed()), this, SLOT(slotOkPressed())); connect(cancel, SIGNAL(pressed()), this, SLOT(slotCancelPressed())); if(!login.isEmpty()) passwdLEdit->setFocus(); else usernameLEdit->setFocus(); gl->activate(); } //----------------------------------------------------------------------------- void KMImapPasswdDialog::slotOkPressed() { act->setLogin(usernameLEdit->text()); act->setPasswd(passwdLEdit->text(), act->storePasswd()); done(1); } //----------------------------------------------------------------------------- void KMImapPasswdDialog::slotCancelPressed() { done(0); }