kmail

kmacctmaildir.cpp
1 // kmacctmaildir.cpp
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include <tqfileinfo.h>
8 #include "kmacctmaildir.h"
9 #include "kmfoldermaildir.h"
10 #include "kmacctfolder.h"
11 #include "broadcaststatus.h"
12 using KPIM::BroadcastStatus;
13 #include "progressmanager.h"
14 using KPIM::ProgressManager;
15 
16 #include <tdeapplication.h>
17 #include <tdelocale.h>
18 #include <tdemessagebox.h>
19 #include <kdebug.h>
20 #include <tdeconfig.h>
21 
22 #include <tqstylesheet.h>
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <assert.h>
28 
29 #ifdef HAVE_PATHS_H
30 #include <paths.h> /* defines _PATH_MAILDIR */
31 #endif
32 
33 #undef None
34 
35 //-----------------------------------------------------------------------------
36 KMAcctMaildir::KMAcctMaildir(AccountManager* aOwner, const TQString& aAccountName, uint id):
37  KMAccount(aOwner, aAccountName, id)
38 {
39 }
40 
41 
42 //-----------------------------------------------------------------------------
43 KMAcctMaildir::~KMAcctMaildir()
44 {
45  mLocation = "";
46 }
47 
48 
49 //-----------------------------------------------------------------------------
50 TQString KMAcctMaildir::type(void) const
51 {
52  return "maildir";
53 }
54 
55 
56 //-----------------------------------------------------------------------------
57 void KMAcctMaildir::init() {
58  KMAccount::init();
59 
60  mLocation = getenv("MAIL");
61  if (mLocation.isNull()) {
62  mLocation = getenv("HOME");
63  mLocation += "/Maildir/";
64  }
65 }
66 
67 
68 //-----------------------------------------------------------------------------
69 void KMAcctMaildir::pseudoAssign( const KMAccount * a )
70 {
71  KMAccount::pseudoAssign( a );
72 
73  const KMAcctMaildir * m = dynamic_cast<const KMAcctMaildir*>( a );
74  if ( !m ) return;
75 
76  setLocation( m->location() );
77 }
78 
79 //-----------------------------------------------------------------------------
80 void KMAcctMaildir::processNewMail(bool)
81 {
82  TQTime t;
83  hasNewMail = false;
84 
85  if ( precommand().isEmpty() ) {
86  TQFileInfo fi( location() );
87  if ( !fi.exists() ) {
88  checkDone( hasNewMail, CheckOK );
89  BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, 0 );
90  return;
91  }
92  }
93 
94  KMFolder mailFolder(0, location(), KMFolderTypeMaildir,
95  false /* no index */, false /* don't export sernums */);
96 
97  long num = 0;
98  long i;
99  int rc;
100  KMMessage* msg;
101  bool addedOk;
102 
103  if (!mFolder) {
104  checkDone( hasNewMail, CheckError );
105  BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
106  return;
107  }
108 
109  BroadcastStatus::instance()->setStatusMsg(
110  i18n("Preparing transmission from \"%1\"...").arg(mName));
111 
112  Q_ASSERT( !mMailCheckProgressItem );
113  TQString escapedName = TQStyleSheet::escape( mName );
114  mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
115  "MailCheck" + mName,
116  escapedName,
117  i18n("Preparing transmission from \"%1\"...").arg( escapedName ),
118  false, // cannot be canceled
119  false ); // no tls/ssl
120 
121  // run the precommand
122  if (!runPrecommand(precommand()))
123  {
124  kdDebug(5006) << "cannot run precommand " << precommand() << endl;
125  checkDone( hasNewMail, CheckError );
126  BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
127  return;
128  }
129 
130  rc = mailFolder.open("acctmaildirMail");
131  if (rc)
132  {
133  TQString aStr = i18n("<qt>Cannot open folder <b>%1</b>.</qt>").arg( mailFolder.location() );
134  KMessageBox::sorry(0, aStr);
135  kdDebug(5006) << "cannot open folder " << mailFolder.location() << endl;
136  checkDone( hasNewMail, CheckError );
137  BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
138  return;
139  }
140 
141  mFolder->open("acctmaildirFold");
142 
143 
144  num = mailFolder.count();
145 
146  addedOk = true;
147  t.start();
148 
149  // prepare the static parts of the status message:
150  TQString statusMsgStub = i18n("Moving message %3 of %2 from %1.")
151  .arg(mailFolder.location()).arg(num);
152 
153  mMailCheckProgressItem->setTotalItems( num );
154 
155  for (i=0; i<num; i++)
156  {
157 
158  if( kmkernel->mailCheckAborted() ) {
159  BroadcastStatus::instance()->setStatusMsg( i18n("Transmission aborted.") );
160  num = i;
161  addedOk = false;
162  }
163  if (!addedOk) break;
164 
165  TQString statusMsg = statusMsgStub.arg(i);
166  mMailCheckProgressItem->incCompletedItems();
167  mMailCheckProgressItem->updateProgress();
168  mMailCheckProgressItem->setStatus( statusMsg );
169 
170  msg = mailFolder.take(0);
171  if (msg)
172  {
173  msg->setStatus(msg->headerField("Status").latin1(),
174  msg->headerField("X-Status").latin1());
175  msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0));
176  msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
177 
178  addedOk = processNewMsg(msg);
179  if (addedOk)
180  hasNewMail = true;
181  }
182 
183  if (t.elapsed() >= 200) { //hardwired constant
184  kapp->processEvents();
185  t.start();
186  }
187 
188  }
189 
190  if( mMailCheckProgressItem ) { // do this only once...
191  BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( num );
192  mMailCheckProgressItem->setStatus(
193  i18n( "Fetched 1 message from maildir folder %1.",
194  "Fetched %n messages from maildir folder %1.",
195  num ).arg(mailFolder.location() ) );
196 
197  mMailCheckProgressItem->setComplete();
198  mMailCheckProgressItem = 0;
199  }
200  if (addedOk)
201  {
202  BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, num );
203  }
204  // else warning is written already
205 
206  mailFolder.close("acctmaildirMail");
207  mFolder->close("acctmaildirFold");
208 
209  checkDone( hasNewMail, CheckOK );
210 
211  return;
212 }
213 
214 
215 //-----------------------------------------------------------------------------
216 void KMAcctMaildir::readConfig(TDEConfig& config)
217 {
218  KMAccount::readConfig(config);
219  mLocation = config.readPathEntry("Location", mLocation);
220 }
221 
222 
223 //-----------------------------------------------------------------------------
224 void KMAcctMaildir::writeConfig(TDEConfig& config)
225 {
226  KMAccount::writeConfig(config);
227  config.writePathEntry("Location", mLocation);
228 }
229 
230 //-----------------------------------------------------------------------------
231 void KMAcctMaildir::setLocation(const TQString& aLocation)
232 {
233  mLocation = aLocation;
234 }
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
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...