kmail

kmacctlocal.cpp
1 // kmacctlocal.cpp
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include "kmacctlocal.h"
8 #include "kmfoldermbox.h"
9 #include "kmacctfolder.h"
10 #include "broadcaststatus.h"
11 using KPIM::BroadcastStatus;
12 #include "progressmanager.h"
13 using KPIM::ProgressManager;
14 
15 #include "kmfoldermgr.h"
16 
17 #include <tdeapplication.h>
18 #include <tdelocale.h>
19 #include <tdemessagebox.h>
20 #include <kdebug.h>
21 #include <tdeconfig.h>
22 
23 #include <tqfileinfo.h>
24 #include <tqstylesheet.h>
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <assert.h>
30 
31 //-----------------------------------------------------------------------------
32 KMAcctLocal::KMAcctLocal(AccountManager* aOwner, const TQString& aAccountName, uint id):
33  KMAccount(aOwner, aAccountName, id), mHasNewMail( false ),
34  mAddedOk( true ), mNumMsgs( 0 ),
35  mMsgsFetched( 0 ), mMailFolder( 0 )
36 {
37  mLock = procmail_lockfile;
38 }
39 
40 
41 //-----------------------------------------------------------------------------
42 KMAcctLocal::~KMAcctLocal()
43 {
44 }
45 
46 
47 //-----------------------------------------------------------------------------
48 TQString KMAcctLocal::type(void) const
49 {
50  return "local";
51 }
52 
53 
54 //-----------------------------------------------------------------------------
55 void KMAcctLocal::init() {
56  KMAccount::init();
57 }
58 
59 
60 //-----------------------------------------------------------------------------
61 void KMAcctLocal::pseudoAssign( const KMAccount * a )
62 {
63  KMAccount::pseudoAssign( a );
64 
65  const KMAcctLocal * l = dynamic_cast<const KMAcctLocal*>( a );
66  if ( !l ) return;
67 
68  setLocation( l->location() );
69  setLockType( l->lockType() );
70  setProcmailLockFileName( l->procmailLockFileName() );
71 }
72 
73 //-----------------------------------------------------------------------------
74 void KMAcctLocal::processNewMail(bool)
75 {
76  mHasNewMail = false;
77 
78  if ( !preProcess() ) {
79  return;
80  }
81 
82  TQTime t;
83  t.start();
84 
85  for ( mMsgsFetched = 0; mMsgsFetched < mNumMsgs; ++mMsgsFetched )
86  {
87  if ( !fetchMsg() )
88  break;
89 
90  if (t.elapsed() >= 200) { //hardwired constant
91  kapp->processEvents();
92  t.start();
93  }
94  }
95 
96  postProcess();
97 }
98 
99 
100 //-----------------------------------------------------------------------------
101 bool KMAcctLocal::preProcess()
102 {
103  if ( precommand().isEmpty() ) {
104  TQFileInfo fi( location() );
105  if ( fi.size() == 0 ) {
106  BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, 0 );
107  checkDone( mHasNewMail, CheckOK );
108  return false;
109  }
110  }
111 
112  mMailFolder = new KMFolder( 0, location(), KMFolderTypeMbox,
113  false /* no index */, false /* don't export sernums */ );
114  KMFolderMbox* mboxStorage =
115  static_cast<KMFolderMbox*>(mMailFolder->storage());
116  mboxStorage->setLockType( mLock );
117  if ( mLock == procmail_lockfile)
118  mboxStorage->setProcmailLockFileName( mProcmailLockFileName );
119 
120  if (!mFolder) {
121  checkDone( mHasNewMail, CheckError );
122  BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
123  return false;
124  }
125 
126  //BroadcastStatus::instance()->reset();
127  BroadcastStatus::instance()->setStatusMsg(
128  i18n("Preparing transmission from \"%1\"...").arg(mName));
129 
130 
131  Q_ASSERT( !mMailCheckProgressItem );
132  TQString escapedName = TQStyleSheet::escape( mName );
133  mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
134  "MailCheck" + mName,
135  escapedName,
136  i18n("Preparing transmission from \"%1\"...").arg( escapedName ),
137  false, // cannot be canceled
138  false ); // no tls/ssl
139 
140  // run the precommand
141  if (!runPrecommand(precommand()))
142  {
143  kdDebug(5006) << "cannot run precommand " << precommand() << endl;
144  checkDone( mHasNewMail, CheckError );
145  BroadcastStatus::instance()->setStatusMsg( i18n( "Running precommand failed." ));
146  return false;
147  }
148 
149  const int rc = mMailFolder->open("acctlocalMail");
150  if ( rc != 0 ) {
151  TQString aStr;
152  aStr = i18n("Cannot open file:");
153  aStr += mMailFolder->path()+"/"+mMailFolder->name();
154  KMessageBox::sorry(0, aStr);
155  kdDebug(5006) << "cannot open file " << mMailFolder->path() << "/"
156  << mMailFolder->name() << endl;
157  checkDone( mHasNewMail, CheckError );
158  BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
159  return false;
160  }
161 
162  if (!mboxStorage->isLocked()) {
163  kdDebug(5006) << "mailFolder could not be locked" << endl;
164  mMailFolder->close("acctlocalMail");
165  checkDone( mHasNewMail, CheckError );
166  TQString errMsg = i18n( "Transmission failed: Could not lock %1." )
167  .arg( mMailFolder->location() );
168  BroadcastStatus::instance()->setStatusMsg( errMsg );
169  return false;
170  }
171 
172  mFolder->open("acctlocalFold");
173 
174  mNumMsgs = mMailFolder->count();
175 
176  mMailCheckProgressItem->setTotalItems( mNumMsgs );
177 
178  // prepare the static parts of the status message:
179  mStatusMsgStub = i18n("Moving message %3 of %2 from %1.")
180  .arg(mMailFolder->location()).arg( mNumMsgs );
181 
182  //BroadcastStatus::instance()->setStatusProgressEnable( "L" + mName, true );
183  return true;
184 }
185 
186 
187 //-----------------------------------------------------------------------------
188 bool KMAcctLocal::fetchMsg()
189 {
190  KMMessage* msg;
191 
192  /* This causes mail eating
193  if (kmkernel->mailCheckAborted()) break; */
194 
195  const TQString statusMsg = mStatusMsgStub.arg( mMsgsFetched );
196  //BroadcastStatus::instance()->setStatusMsg( statusMsg );
197  mMailCheckProgressItem->incCompletedItems();
198  mMailCheckProgressItem->updateProgress();
199  mMailCheckProgressItem->setStatus( statusMsg );
200 
201  msg = mMailFolder->take(0);
202  if (msg)
203  {
204 #if 0
205  // debug code, don't remove
206  TQFile fileD0( "testdat_xx-0-0" );
207  if( fileD0.open( IO_WriteOnly ) ) {
208  TQCString s = msg->asString();
209  uint l = s.length();
210  if ( l > 0 ) {
211  TQDataStream ds( &fileD0 );
212  ds.writeRawBytes( s.data(), l );
213  }
214  fileD0.close(); // If data is 0 we just create a zero length file.
215  }
216 #endif
217  msg->setStatus(msg->headerField("Status").latin1(),
218  msg->headerField("X-Status").latin1());
219  msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0) );
220  msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
221  msg->setComplete(true);
222  msg->updateAttachmentState();
223  msg->updateInvitationState();
224 
225  mAddedOk = processNewMsg(msg);
226 
227  if (mAddedOk)
228  mHasNewMail = true;
229 
230  return mAddedOk;
231  }
232  return true;
233 }
234 
235 
236 //-----------------------------------------------------------------------------
237 void KMAcctLocal::postProcess()
238 {
239  if (mAddedOk)
240  {
241  kmkernel->folderMgr()->syncAllFolders();
242  const int rc = mMailFolder->expunge();
243  if ( rc != 0 ) {
244  KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
245  i18n( "<qt>Cannot remove mail from "
246  "mailbox <b>%1</b>:<br>%2</qt>" )
247  .arg( mMailFolder->location() )
248  .arg( strerror( rc ) ) );
249  }
250 
251  if( mMailCheckProgressItem ) { // do this only once...
252  BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, mNumMsgs );
253  mMailCheckProgressItem->setStatus(
254  i18n( "Fetched 1 message from mailbox %1.",
255  "Fetched %n messages from mailbox %1.",
256  mNumMsgs ).arg( mMailFolder->location() ) );
257  mMailCheckProgressItem->setComplete();
258  mMailCheckProgressItem = 0;
259  }
260  }
261  // else warning is written already
262 
263  mMailFolder->close("acctlocalMail");
264  delete mMailFolder; mMailFolder = 0;
265 
266  mFolder->close("acctlocalFold");
267 
268  checkDone( mHasNewMail, CheckOK );
269 }
270 
271 
272 //-----------------------------------------------------------------------------
273 void KMAcctLocal::readConfig(TDEConfig& config)
274 {
275  KMAccount::readConfig(config);
276  mLocation = config.readPathEntry("Location", mLocation);
277  TQString locktype = config.readEntry("LockType", "procmail_lockfile" );
278 
279  if( locktype == "procmail_lockfile" ) {
280  mLock = procmail_lockfile;
281  mProcmailLockFileName = config.readEntry("ProcmailLockFile",
282  mLocation + ".lock");
283  } else if( locktype == "mutt_dotlock" )
284  mLock = mutt_dotlock;
285  else if( locktype == "mutt_dotlock_privileged" )
286  mLock = mutt_dotlock_privileged;
287  else if( locktype == "none" )
288  mLock = lock_none;
289  else mLock = FCNTL;
290 }
291 
292 
293 //-----------------------------------------------------------------------------
294 void KMAcctLocal::writeConfig(TDEConfig& config)
295 {
296  KMAccount::writeConfig(config);
297 
298  config.writePathEntry("Location", mLocation);
299 
300  TQString st = "fcntl";
301  if (mLock == procmail_lockfile) st = "procmail_lockfile";
302  else if (mLock == mutt_dotlock) st = "mutt_dotlock";
303  else if (mLock == mutt_dotlock_privileged) st = "mutt_dotlock_privileged";
304  else if (mLock == lock_none) st = "none";
305  config.writeEntry("LockType", st);
306 
307  if (mLock == procmail_lockfile) {
308  config.writeEntry("ProcmailLockFile", mProcmailLockFileName);
309  }
310 
311 }
312 
313 
314 //-----------------------------------------------------------------------------
315 void KMAcctLocal::setLocation(const TQString& aLocation)
316 {
317  mLocation = aLocation;
318 }
319 
320 void KMAcctLocal::setProcmailLockFileName(const TQString& s)
321 {
322  mProcmailLockFileName = s;
323 }
Mail folder.
Definition: kmfolder.h:69
This is a Mime Message.
Definition: kmmessage.h:68
void setStatus(const KMMsgStatus status, int idx=-1)
Set status and mark dirty.
Definition: kmmessage.cpp:4153
TQCString asString() const
Return the entire message contents as a string.
Definition: kmmessage.cpp:314
void setComplete(bool v)
Set if the message is a complete message.
Definition: kmmessage.h:869
TQString headerField(const TQCString &name) const
Returns the value of a header field with the given name.
Definition: kmmessage.cpp:2289
The account manager is responsible for creating accounts of various types via the factory method crea...