author | llornkcor <llornkcor> | 2003-04-06 17:45:00 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2003-04-06 17:45:00 (UTC) |
commit | 70b1ff1a3f134d75d1539f269d52b34e6762684d (patch) (unidiff) | |
tree | 28c1b3d0c03e8c0188f014dfe094645d1ddfef5f /noncore/net/mailit/emailclient.cpp | |
parent | 75e8f29020e267d1013a79839831035073b4eeae (diff) | |
download | opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.zip opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.tar.gz opie-70b1ff1a3f134d75d1539f269d52b34e6762684d.tar.bz2 |
move malit to head again, since it is working
Diffstat (limited to 'noncore/net/mailit/emailclient.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mailit/emailclient.cpp | 858 |
1 files changed, 858 insertions, 0 deletions
diff --git a/noncore/net/mailit/emailclient.cpp b/noncore/net/mailit/emailclient.cpp new file mode 100644 index 0000000..7ba5c75 --- a/dev/null +++ b/noncore/net/mailit/emailclient.cpp | |||
@@ -0,0 +1,858 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qt Palmtop Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include <qapplication.h> | ||
21 | #include <qmessagebox.h> | ||
22 | #include <qfile.h> | ||
23 | #include <qcheckbox.h> | ||
24 | #include <qmenubar.h> | ||
25 | #include <qaction.h> | ||
26 | #include "resource.h" | ||
27 | #include "emailclient.h" | ||
28 | |||
29 | QCollection::Item AccountList::newItem(QCollection::Item d) | ||
30 | { | ||
31 | return dupl( (MailAccount *) d); | ||
32 | } | ||
33 | |||
34 | MailAccount* AccountList::dupl(MailAccount *in) | ||
35 | { | ||
36 | ac = new MailAccount(*in); | ||
37 | return ac; | ||
38 | } | ||
39 | |||
40 | EmailClient::EmailClient( QWidget* parent, const char* name, WFlags fl ) | ||
41 | : QMainWindow( parent, name, fl ) | ||
42 | { | ||
43 | emailHandler = new EmailHandler(); | ||
44 | addressList = new AddressList( getPath(FALSE) + "mail_adr"); | ||
45 | |||
46 | sending = FALSE; | ||
47 | receiving = FALSE; | ||
48 | previewingMail = FALSE; | ||
49 | mailIdCount = 1; | ||
50 | accountIdCount = 1; | ||
51 | allAccounts = FALSE; | ||
52 | |||
53 | init(); | ||
54 | |||
55 | connect(emailHandler, SIGNAL(mailSent()), this, SLOT(mailSent()) ); | ||
56 | |||
57 | connect(emailHandler, SIGNAL(smtpError(int)), this, | ||
58 | SLOT(smtpError(int)) ); | ||
59 | connect(emailHandler, SIGNAL(popError(int)), this, | ||
60 | SLOT(popError(int)) ); | ||
61 | |||
62 | connect(inboxView, SIGNAL(clicked(QListViewItem *)), this, SLOT(inboxItemSelected()) ); | ||
63 | connect(outboxView, SIGNAL(clicked(QListViewItem *)), this, SLOT(outboxItemSelected()) ); | ||
64 | |||
65 | connect(emailHandler, SIGNAL(mailArrived(const Email &, bool)), this, | ||
66 | SLOT(mailArrived(const Email &, bool)) ); | ||
67 | connect(emailHandler, SIGNAL(mailTransfered(int)), this, | ||
68 | SLOT(allMailArrived(int)) ); | ||
69 | |||
70 | mailconf = new Config("mailit"); | ||
71 | //In case Synchronize is not defined in settings.txt | ||
72 | |||
73 | readSettings(); | ||
74 | |||
75 | updateAccounts(); | ||
76 | |||
77 | lineShift = "\n"; | ||
78 | readMail(); | ||
79 | lineShift = "\r\n"; | ||
80 | |||
81 | } | ||
82 | |||
83 | |||
84 | EmailClient::~EmailClient() | ||
85 | { | ||
86 | //needs to be moved from destructor to closewindow event | ||
87 | saveMail(getPath(FALSE) + "inbox.txt", inboxView); | ||
88 | //does not currently work. Defining outbox in the same | ||
89 | //format as inbox is not a good solution as they have | ||
90 | //different properties | ||
91 | saveMail(getPath(FALSE) + "outbox.txt", outboxView); | ||
92 | saveSettings(); | ||
93 | |||
94 | mailconf->write(); | ||
95 | delete mailconf; | ||
96 | |||
97 | } | ||
98 | |||
99 | void EmailClient::init() | ||
100 | { | ||
101 | statusBar = new QStatusBar(this); | ||
102 | statusBar->setSizeGripEnabled(FALSE); | ||
103 | |||
104 | status1Label = new QLabel( tr("Idle"), statusBar); | ||
105 | status2Label = new QLabel("", statusBar); | ||
106 | connect(emailHandler, SIGNAL(updatePopStatus(const QString &)), | ||
107 | status2Label, SLOT(setText(const QString &)) ); | ||
108 | connect(emailHandler, SIGNAL(updateSmtpStatus(const QString &)), | ||
109 | status2Label, SLOT(setText(const QString &)) ); | ||
110 | |||
111 | progressBar = new QProgressBar(statusBar); | ||
112 | connect(emailHandler, SIGNAL(mailboxSize(int)), | ||
113 | this, SLOT(setTotalSize(int)) ); | ||
114 | connect(emailHandler, SIGNAL(currentMailSize(int)), | ||
115 | this, SLOT(setMailSize(int)) ); | ||
116 | connect(emailHandler, SIGNAL(downloadedSize(int)), | ||
117 | this, SLOT(setDownloadedSize(int)) ); | ||
118 | |||
119 | statusBar->addWidget(status1Label); | ||
120 | statusBar->addWidget(progressBar); | ||
121 | statusBar->addWidget(status2Label); | ||
122 | |||
123 | setToolBarsMovable(FALSE); | ||
124 | |||
125 | bar = new QToolBar(this); | ||
126 | bar->setHorizontalStretchable( TRUE ); | ||
127 | |||
128 | mb = new QMenuBar( bar ); | ||
129 | |||
130 | QPopupMenu *mail = new QPopupMenu(mb); | ||
131 | mb->insertItem( tr( "&Mail" ), mail); | ||
132 | |||
133 | QPopupMenu *configure = new QPopupMenu(mb); | ||
134 | mb->insertItem( tr( "Accounts" ), configure); | ||
135 | |||
136 | selectAccountMenu = new QPopupMenu(mb); | ||
137 | editAccountMenu = new QPopupMenu(mb); | ||
138 | deleteAccountMenu = new QPopupMenu(mb); | ||
139 | |||
140 | mail->insertItem(tr("Get Mail in"), selectAccountMenu); | ||
141 | configure->insertItem(tr("Edit account"), editAccountMenu); | ||
142 | configure->insertItem(tr("Delete account"), deleteAccountMenu); | ||
143 | |||
144 | bar = new QToolBar(this); | ||
145 | |||
146 | getMailButton = new QAction(tr("Get all mail"), QPixmap("getmail.xpm"), QString::null, 0, this, 0); | ||
147 | connect(getMailButton, SIGNAL(activated()), this, SLOT(getAllNewMail()) ); | ||
148 | getMailButton->addTo(bar); | ||
149 | getMailButton->addTo(mail); | ||
150 | |||
151 | sendMailButton = new QAction(tr("Send mail"), QPixmap("sendmail.xpm"), QString::null, 0, this, 0); | ||
152 | connect(sendMailButton, SIGNAL(activated()), this, SLOT(sendQuedMail()) ); | ||
153 | sendMailButton->addTo(bar); | ||
154 | sendMailButton->addTo(mail); | ||
155 | |||
156 | composeButton = new QAction(tr("Compose"), Resource::loadPixmap("new"), QString::null, 0, this, 0); | ||
157 | connect(composeButton, SIGNAL(activated()), this, SLOT(compose()) ); | ||
158 | composeButton->addTo(bar); | ||
159 | composeButton->addTo(mail); | ||
160 | |||
161 | cancelButton = new QAction(tr("Cancel transfer"), Resource::loadPixmap("reset"), QString::null, 0, this, 0); | ||
162 | connect(cancelButton, SIGNAL(activated()), this, SLOT(cancel()) ); | ||
163 | cancelButton->addTo(mail); | ||
164 | cancelButton->setEnabled(FALSE); | ||
165 | |||
166 | mailboxView = new QTabWidget( this, "mailboxView" ); | ||
167 | |||
168 | QWidget* widget = new QWidget( mailboxView, "widget" ); | ||
169 | grid_2 = new QGridLayout( widget ); | ||
170 | // grid_2->setSpacing(6); | ||
171 | // grid_2->setMargin( 11 ); | ||
172 | |||
173 | inboxView = new QListView( widget, "inboxView" ); | ||
174 | inboxView->addColumn( tr( "From" ) ); | ||
175 | inboxView->addColumn( tr( "Subject" ) ); | ||
176 | inboxView->addColumn( tr( "Date" ) ); | ||
177 | inboxView->setMinimumSize( QSize( 0, 0 ) ); | ||
178 | inboxView->setAllColumnsShowFocus(TRUE); | ||
179 | |||
180 | grid_2->addWidget( inboxView, 2, 0 ); | ||
181 | mailboxView->insertTab( widget, tr( "Inbox" ) ); | ||
182 | |||
183 | QWidget* widget_2 = new QWidget( mailboxView, "widget_2" ); | ||
184 | grid_3 = new QGridLayout( widget_2 ); | ||
185 | // grid_3->setSpacing(6); | ||
186 | // grid_3->setMargin( 11 ); | ||
187 | |||
188 | outboxView = new QListView( widget_2, "outboxView" ); | ||
189 | outboxView->addColumn( tr( "To" ) ); | ||
190 | outboxView->addColumn( tr( "Subject" ) ); | ||
191 | outboxView->setAllColumnsShowFocus(TRUE); | ||
192 | |||
193 | grid_3->addWidget( outboxView, 0, 0 ); | ||
194 | mailboxView->insertTab( widget_2, tr( "Outbox" ) ); | ||
195 | |||
196 | setCentralWidget(mailboxView); | ||
197 | } | ||
198 | |||
199 | void EmailClient::compose() | ||
200 | { | ||
201 | emit composeRequested(); | ||
202 | } | ||
203 | |||
204 | void EmailClient::cancel() | ||
205 | { | ||
206 | emailHandler->cancel(); | ||
207 | } | ||
208 | |||
209 | AddressList* EmailClient::getAdrListRef() | ||
210 | { | ||
211 | return addressList; | ||
212 | } | ||
213 | |||
214 | //this needs to be rewritten to syncronize with outboxView | ||
215 | void EmailClient::enqueMail(const Email &mail) | ||
216 | { | ||
217 | if (accountList.count() > 0) { | ||
218 | currentAccount = accountList.first(); | ||
219 | qWarning("using account " + currentAccount->name); | ||
220 | } | ||
221 | |||
222 | Email addMail = mail; | ||
223 | addMail.from = currentAccount->name; | ||
224 | addMail.fromMail = currentAccount->emailAddress; | ||
225 | addMail.rawMail.prepend("From: " + addMail.from + "<" + addMail.fromMail + ">\n"); | ||
226 | item = new EmailListItem(outboxView, addMail, false); | ||
227 | |||
228 | } | ||
229 | |||
230 | void EmailClient::sendQuedMail() | ||
231 | { | ||
232 | int count = 0; | ||
233 | |||
234 | if (accountList.count() == 0) { | ||
235 | QMessageBox::warning(qApp->activeWindow(), | ||
236 | "No account selected", "You must create an account", "OK\n"); | ||
237 | return; | ||
238 | } | ||
239 | //traverse listview, find messages to send | ||
240 | if (! sending) { | ||
241 | item = (EmailListItem *) outboxView->firstChild(); | ||
242 | if (item != NULL) { | ||
243 | while (item != NULL) { | ||
244 | quedMessages.append(item->getMail()); | ||
245 | item = (EmailListItem *) item->nextSibling(); | ||
246 | count++; | ||
247 | } | ||
248 | setMailAccount(); | ||
249 | emailHandler->sendMail(&quedMessages); | ||
250 | sending = TRUE; | ||
251 | sendMailButton->setEnabled(FALSE); | ||
252 | cancelButton->setEnabled(TRUE); | ||
253 | } else { | ||
254 | qWarning("sendQuedMail(): no messages to send"); | ||
255 | } | ||
256 | } | ||
257 | } | ||
258 | |||
259 | void EmailClient::setMailAccount() | ||
260 | { | ||
261 | emailHandler->setAccount(*currentAccount); | ||
262 | } | ||
263 | |||
264 | void EmailClient::mailSent() | ||
265 | { | ||
266 | sending = FALSE; | ||
267 | sendMailButton->setEnabled(TRUE); | ||
268 | |||
269 | quedMessages.clear(); | ||
270 | outboxView->clear(); //should be moved to an sentBox | ||
271 | } | ||
272 | |||
273 | void EmailClient::getNewMail() { | ||
274 | |||
275 | if (accountList.count() == 0) { | ||
276 | QMessageBox::warning(qApp->activeWindow(),"No account selected", | ||
277 | "You must create an account", "OK\n"); | ||
278 | return; | ||
279 | } | ||
280 | |||
281 | setMailAccount(); | ||
282 | |||
283 | receiving = TRUE; | ||
284 | previewingMail = TRUE; | ||
285 | getMailButton->setEnabled(FALSE); | ||
286 | cancelButton->setEnabled(TRUE); | ||
287 | selectAccountMenu->setEnabled(FALSE); | ||
288 | |||
289 | status1Label->setText(currentAccount->accountName + " headers"); | ||
290 | progressBar->reset(); | ||
291 | |||
292 | //get any previous mails not downloaded and add to queue | ||
293 | mailDownloadList.clear(); | ||
294 | Email *mailPtr; | ||
295 | item = (EmailListItem *) inboxView->firstChild(); | ||
296 | while (item != NULL) { | ||
297 | mailPtr = item->getMail(); | ||
298 | if ( (!mailPtr->downloaded) && (mailPtr->fromAccountId == currentAccount->id) ) { | ||
299 | mailDownloadList.sizeInsert(mailPtr->serverId, mailPtr->size); | ||
300 | } | ||
301 | item = (EmailListItem *) item->nextSibling(); | ||
302 | } | ||
303 | |||
304 | emailHandler->getMailHeaders(); | ||
305 | } | ||
306 | |||
307 | void EmailClient::getAllNewMail() | ||
308 | { | ||
309 | allAccounts = TRUE; | ||
310 | currentAccount = accountList.first(); | ||
311 | getNewMail(); | ||
312 | } | ||
313 | |||
314 | void EmailClient::mailArrived(const Email &mail, bool fromDisk) | ||
315 | { | ||
316 | Enclosure *ePtr; | ||
317 | Email newMail; | ||
318 | int thisMailId; | ||
319 | emailHandler->parse(mail.rawMail, lineShift, &newMail); | ||
320 | |||
321 | mailconf->setGroup(newMail.id); | ||
322 | |||
323 | if (fromDisk) { | ||
324 | newMail.downloaded = mailconf->readBoolEntry("downloaded"); | ||
325 | newMail.size = mailconf->readNumEntry("size"); | ||
326 | newMail.serverId = mailconf->readNumEntry("serverid"); | ||
327 | newMail.fromAccountId = mailconf->readNumEntry("fromaccountid"); | ||
328 | } else { //mail arrived from server | ||
329 | newMail.serverId = mail.serverId; | ||
330 | newMail.size = mail.size; | ||
331 | newMail.downloaded = mail.downloaded; | ||
332 | |||
333 | newMail.fromAccountId = currentAccount->id; | ||
334 | mailconf->writeEntry("fromaccountid", newMail.fromAccountId); | ||
335 | } | ||
336 | |||
337 | //add if read or not | ||
338 | newMail.read = mailconf->readBoolEntry("mailread"); | ||
339 | |||
340 | //check if new mail | ||
341 | if ( (thisMailId = mailconf->readNumEntry("internalmailid", -1)) == -1) { | ||
342 | thisMailId = mailIdCount; | ||
343 | mailIdCount++; | ||
344 | |||
345 | //set server count, so that if the user aborts, the new | ||
346 | //header is not reloaded | ||
347 | if (currentAccount->synchronize) | ||
348 | currentAccount->lastServerMailCount++; | ||
349 | |||
350 | mailconf->writeEntry("internalmailid", thisMailId); | ||
351 | mailconf->writeEntry("downloaded", newMail.downloaded); | ||
352 | mailconf->writeEntry("size", (int) newMail.size); | ||
353 | mailconf->writeEntry("serverid", newMail.serverId); | ||
354 | |||
355 | addressList->addContact(newMail.fromMail, newMail.from); | ||
356 | } else if (!fromDisk) { //body to header arrived | ||
357 | mailconf->writeEntry("downloaded", TRUE); | ||
358 | } | ||
359 | QString stringMailId; | ||
360 | stringMailId.setNum(thisMailId); | ||
361 | //se if any attatchments needs to be stored | ||
362 | for ( ePtr=newMail.files.first(); ePtr != 0; ePtr=newMail.files.next() ) { | ||
363 | QString stringId; | ||
364 | stringId.setNum(ePtr->id); | ||
365 | |||
366 | int id = mailconf->readNumEntry("enclosureid_" + stringId); | ||
367 | if (id != ePtr->id) { //new entry | ||
368 | mailconf->writeEntry("enclosureid_" + stringId, ePtr->id); | ||
369 | mailconf->writeEntry("name_" + stringId, ePtr->originalName); | ||
370 | mailconf->writeEntry("contenttype_" + stringId, ePtr->contentType); | ||
371 | mailconf->writeEntry("contentattribute_" + stringId, ePtr->contentAttribute); | ||
372 | mailconf->writeEntry("saved_" + stringId, ePtr->saved); | ||
373 | mailconf->writeEntry("installed_" + stringId, FALSE); | ||
374 | |||
375 | ePtr->name = stringMailId + "_" + stringId; | ||
376 | ePtr->path = getPath(TRUE); | ||
377 | if (emailHandler->getEnclosure(ePtr)) { //file saved | ||
378 | ePtr->saved = TRUE; | ||
379 | mailconf->writeEntry("saved_" + stringId, ePtr->saved); | ||
380 | mailconf->writeEntry("filename_" + stringId, ePtr->name); | ||
381 | mailconf->writeEntry("path_" + stringId, ePtr->path); | ||
382 | } else { | ||
383 | ePtr->saved = FALSE; | ||
384 | mailconf->writeEntry("saved_" + stringId, ePtr->saved); | ||
385 | } | ||
386 | } else { | ||
387 | ePtr->saved = mailconf->readBoolEntry("saved_" + stringId); | ||
388 | ePtr->installed = mailconf->readBoolEntry("installed_" + stringId); | ||
389 | if (ePtr->saved) { | ||
390 | ePtr->name = mailconf->readEntry("filename_" + stringId); | ||
391 | ePtr->path = mailconf->readEntry("path_" + stringId); | ||
392 | } | ||
393 | } | ||
394 | } | ||
395 | if (!previewingMail && !fromDisk) { | ||
396 | Email *mailPtr; | ||
397 | item = (EmailListItem *) inboxView->firstChild(); | ||
398 | while (item != NULL) { | ||
399 | mailPtr = item->getMail(); | ||
400 | if (mailPtr->id == newMail.id) { | ||
401 | item->setMail(newMail); | ||
402 | emit mailUpdated(item->getMail()); | ||
403 | } | ||
404 | item = (EmailListItem *) item->nextSibling(); | ||
405 | } | ||
406 | } else { | ||
407 | item = new EmailListItem(inboxView, newMail, TRUE); | ||
408 | if (!newMail.downloaded) | ||
409 | mailDownloadList.sizeInsert(newMail.serverId, newMail.size); | ||
410 | } | ||
411 | |||
412 | } | ||
413 | |||
414 | void EmailClient::allMailArrived(int count) | ||
415 | { | ||
416 | // not previewing means all mailtransfer has been done | ||
417 | if (!previewingMail) { | ||
418 | if ( (allAccounts) && ( (currentAccount = accountList.next()) !=0 ) ) { | ||
419 | emit newCaption("Mailit - " + currentAccount->accountName); | ||
420 | getNewMail(); | ||
421 | return; | ||
422 | } else { | ||
423 | allAccounts = FALSE; | ||
424 | receiving = FALSE; | ||
425 | getMailButton->setEnabled(TRUE); | ||
426 | cancelButton->setEnabled(FALSE); | ||
427 | selectAccountMenu->setEnabled(TRUE); | ||
428 | status1Label->setText("Idle"); | ||
429 | |||
430 | progressBar->reset(); | ||
431 | return; | ||
432 | } | ||
433 | } | ||
434 | |||
435 | // all headers downloaded from server, start downloading remaining mails | ||
436 | previewingMail = FALSE; | ||
437 | status1Label->setText(currentAccount->accountName); | ||
438 | progressBar->reset(); | ||
439 | |||
440 | emailHandler->getMailByList(&mailDownloadList); | ||
441 | } | ||
442 | |||
443 | void EmailClient::moveMailFront(Email *mailPtr) | ||
444 | { | ||
445 | if ( (receiving) && (mailPtr->fromAccountId == currentAccount->id) ) { | ||
446 | mailDownloadList.moveFront(mailPtr->serverId, mailPtr->size); | ||
447 | } | ||
448 | } | ||
449 | |||
450 | void EmailClient::smtpError(int code) | ||
451 | { | ||
452 | QString temp; | ||
453 | |||
454 | if (code == ErrUnknownResponse) | ||
455 | temp = "Unknown response from server"; | ||
456 | |||
457 | if (code == QSocket::ErrHostNotFound) | ||
458 | temp = "host not found"; | ||
459 | if (code == QSocket::ErrConnectionRefused) | ||
460 | temp = "connection refused"; | ||
461 | if (code == QSocket::ErrSocketRead) | ||
462 | temp = "socket packet error"; | ||
463 | |||
464 | if (code != ErrCancel) { | ||
465 | QMessageBox::warning(qApp->activeWindow(), "Sending error", temp, "OK\n"); | ||
466 | } else { | ||
467 | status2Label->setText("Aborted by user"); | ||
468 | } | ||
469 | |||
470 | sending = FALSE; | ||
471 | sendMailButton->setEnabled(TRUE); | ||
472 | cancelButton->setEnabled(FALSE); | ||
473 | quedMessages.clear(); | ||
474 | } | ||
475 | |||
476 | void EmailClient::popError(int code) | ||
477 | { | ||
478 | QString temp; | ||
479 | |||
480 | if (code == ErrUnknownResponse) | ||
481 | temp = "Unknown response from server"; | ||
482 | if (code == ErrLoginFailed) | ||
483 | temp = "Login failed\nCheck user name and password"; | ||
484 | |||
485 | if (code == QSocket::ErrHostNotFound) | ||
486 | temp = "host not found"; | ||
487 | if (code == QSocket::ErrConnectionRefused) | ||
488 | temp = "connection refused"; | ||
489 | if (code == QSocket::ErrSocketRead) | ||
490 | temp = "socket packet error"; | ||
491 | |||
492 | if (code != ErrCancel) { | ||
493 | QMessageBox::warning(qApp->activeWindow(), "Receiving error", temp, "OK\n"); | ||
494 | } else { | ||
495 | status2Label->setText("Aborted by user"); | ||
496 | } | ||
497 | |||
498 | receiving = FALSE; | ||
499 | getMailButton->setEnabled(TRUE); | ||
500 | cancelButton->setEnabled(FALSE); | ||
501 | selectAccountMenu->setEnabled(TRUE); | ||
502 | } | ||
503 | |||
504 | void EmailClient::inboxItemSelected() | ||
505 | { | ||
506 | item = (EmailListItem*) inboxView->selectedItem(); | ||
507 | if (item != NULL) { | ||
508 | emit viewEmail(inboxView, item->getMail()); | ||
509 | } | ||
510 | } | ||
511 | |||
512 | void EmailClient::outboxItemSelected() | ||
513 | { | ||
514 | item = (EmailListItem*) outboxView->selectedItem(); | ||
515 | if (item != NULL) { | ||
516 | emit viewEmail(outboxView, item->getMail()); | ||
517 | } | ||
518 | |||
519 | } | ||
520 | |||
521 | void EmailClient::readMail() | ||
522 | { | ||
523 | Email mail; | ||
524 | int start, stop; | ||
525 | QString s, del; | ||
526 | |||
527 | QFile f(getPath(FALSE) + "inbox.txt"); | ||
528 | // QFileInfo fi(f); | ||
529 | qDebug( f.name()); | ||
530 | |||
531 | if ( f.open(IO_ReadOnly) ) { // file opened successfully | ||
532 | QTextStream t( &f ); // use a text stream | ||
533 | s = t.read(); | ||
534 | f.close(); | ||
535 | |||
536 | start = 0; | ||
537 | del = "\n.\n"; | ||
538 | while ((uint) start < s.length()) { | ||
539 | stop = s.find(del, start); | ||
540 | if (stop == -1) | ||
541 | stop = s.length() - del.length(); | ||
542 | |||
543 | mail.rawMail = s.mid(start, stop + del.length() - start ); | ||
544 | start = stop + del.length(); | ||
545 | mailArrived(mail, TRUE); | ||
546 | } | ||
547 | } | ||
548 | |||
549 | QFile fo(getPath(FALSE) + "outbox.txt"); | ||
550 | if ( fo.open(IO_ReadOnly) ) { // file opened successfully | ||
551 | QTextStream t( &fo ); // use a text stream | ||
552 | s = t.read(); | ||
553 | fo.close(); | ||
554 | |||
555 | start = 0; | ||
556 | del = "\n.\n"; | ||
557 | while ((uint) start < s.length()) { | ||
558 | stop = s.find(del, start); | ||
559 | if (stop == -1) | ||
560 | stop = s.length() - del.length(); | ||
561 | |||
562 | mail.rawMail = s.mid(start, stop + del.length() - start ); | ||
563 | start = stop + del.length(); | ||
564 | emailHandler->parse(mail.rawMail, lineShift, &mail); | ||
565 | mail.sent = false; | ||
566 | mail.received = false; | ||
567 | enqueMail(mail); | ||
568 | |||
569 | } | ||
570 | } | ||
571 | } | ||
572 | |||
573 | void EmailClient::saveMail(QString fileName, QListView *view) | ||
574 | { | ||
575 | QFile f(fileName); | ||
576 | Email *mail; | ||
577 | |||
578 | if (! f.open(IO_WriteOnly) ) { | ||
579 | qWarning("could not open file"); | ||
580 | return; | ||
581 | } | ||
582 | item = (EmailListItem *) view->firstChild(); | ||
583 | qDebug (QString("Write : ") ); | ||
584 | QTextStream t(&f); | ||
585 | while (item != NULL) { | ||
586 | mail = item->getMail(); | ||
587 | qDebug(mail->rawMail); | ||
588 | qDebug(mail->recipients.first()); | ||
589 | t << mail->rawMail; | ||
590 | |||
591 | mailconf->setGroup(mail->id); | ||
592 | mailconf->writeEntry("mailread", mail->read); | ||
593 | |||
594 | item = (EmailListItem *) item->nextSibling(); | ||
595 | } | ||
596 | f.close(); | ||
597 | } | ||
598 | |||
599 | //paths for mailit, is settings, inbox, enclosures | ||
600 | QString EmailClient::getPath(bool enclosurePath) | ||
601 | { | ||
602 | QString basePath = "qtmail"; | ||
603 | QString enclosures = "enclosures"; | ||
604 | |||
605 | QDir dir = (QString(getenv("HOME")) + "/Applications/" + basePath); | ||
606 | if ( !dir.exists() ) | ||
607 | dir.mkdir( dir.path() ); | ||
608 | |||
609 | if (enclosurePath) { | ||
610 | dir = (QString(getenv("HOME")) + "/Applications/" + basePath + "/" + enclosures); | ||
611 | |||
612 | if ( !dir.exists() ) | ||
613 | dir.mkdir( dir.path() ); | ||
614 | |||
615 | return (dir.path() + "/"); | ||
616 | |||
617 | } | ||
618 | return (dir.path() + "/"); | ||
619 | } | ||
620 | |||
621 | void EmailClient::readSettings() | ||
622 | { | ||
623 | TextParser *p; | ||
624 | QString s; | ||
625 | int pos, accountPos, y; | ||
626 | QFile f( getPath(FALSE) + "settings.txt"); | ||
627 | |||
628 | if ( f.open(IO_ReadOnly) ) { // file opened successfully | ||
629 | QTextStream t( &f ); // use a text stream | ||
630 | s = t.read(); | ||
631 | f.close(); | ||
632 | |||
633 | p = new TextParser(s, "\n"); | ||
634 | |||
635 | accountPos = 0; | ||
636 | while ( (accountPos = p->find("ACCOUNTSTART",';', accountPos, TRUE)) != -1 ) { | ||
637 | accountPos++; | ||
638 | if ( (pos = p->find("ACCOUNTNAME",':', accountPos, TRUE)) != -1 ) | ||
639 | account.accountName = p->getString(& ++pos, 'z', TRUE); | ||
640 | if ( (pos = p->find("NAME",':', accountPos, TRUE)) != -1) | ||
641 | account.name = p->getString(& ++pos, 'z', TRUE); | ||
642 | if ( (pos = p->find("EMAIL",':', accountPos, TRUE)) != -1) | ||
643 | account.emailAddress = p->getString(& ++pos, 'z', TRUE); | ||
644 | if ( (pos = p->find("POPUSER",':', accountPos, TRUE)) != -1) | ||
645 | account.popUserName = p->getString(& ++pos, 'z', TRUE); | ||
646 | if ( (pos = p->find("POPPASSWORD",':', accountPos, TRUE)) != -1) | ||
647 | account.popPasswd = p->getString(& ++pos, 'z', TRUE); | ||
648 | if ( (pos = p->find("POPSERVER",':', accountPos, TRUE)) != -1) | ||
649 | account.popServer = p->getString(& ++pos, 'z', TRUE); | ||
650 | if ( (pos = p->find("SMTPSERVER",':', accountPos, TRUE)) != -1) | ||
651 | account.smtpServer = p->getString(& ++pos, 'z', TRUE); | ||
652 | if ( (pos = p->find("ACCOUNTID",':', accountPos, TRUE)) != -1) { | ||
653 | s = p->getString(& ++pos, 'z', TRUE); | ||
654 | account.id = s.toInt(); | ||
655 | } | ||
656 | |||
657 | account.lastServerMailCount = 0; | ||
658 | account.synchronize = FALSE; | ||
659 | if ( (pos = p->find("SYNCHRONIZE",':', accountPos, TRUE)) != -1) { | ||
660 | if (p->getString(& ++pos, 'z', TRUE).upper() == "YES") { | ||
661 | account.synchronize = TRUE; | ||
662 | if ( (pos = p->find("LASTSERVERMAILCOUNT",':', accountPos, TRUE)) != -1) { | ||
663 | s = p->getString(& ++pos, 'z', TRUE); | ||
664 | account.lastServerMailCount = s.toInt(); | ||
665 | } | ||
666 | } | ||
667 | } | ||
668 | accountList.append(&account); | ||
669 | } | ||
670 | delete p; | ||
671 | } | ||
672 | mailconf->setGroup("mailitglobal"); | ||
673 | if ( (y = mailconf->readNumEntry("mailidcount", -1)) != -1) { | ||
674 | mailIdCount = y; | ||
675 | } | ||
676 | if ( (y = mailconf->readNumEntry("accountidcount", -1)) != -1) { | ||
677 | accountIdCount = y; | ||
678 | } | ||
679 | } | ||
680 | |||
681 | void EmailClient::saveSettings() | ||
682 | { | ||
683 | QString temp; | ||
684 | QFile f( getPath(FALSE) + "settings.txt"); | ||
685 | MailAccount *accountPtr; | ||
686 | |||
687 | if (! f.open(IO_WriteOnly) ) { | ||
688 | qWarning("could not save settings file"); | ||
689 | return; | ||
690 | } | ||
691 | QTextStream t(&f); | ||
692 | t << "#Settings for QPE Mailit program\n"; | ||
693 | |||
694 | for (accountPtr = accountList.first(); accountPtr != 0; | ||
695 | accountPtr = accountList.next()) { | ||
696 | |||
697 | t << "accountStart;\n"; | ||
698 | t << "AccountName: " + accountPtr->accountName + "\n"; | ||
699 | t << "Name: " + accountPtr->name + "\n"; | ||
700 | t << "Email: " + accountPtr->emailAddress + "\n"; | ||
701 | t << "POPUser: " + accountPtr->popUserName + "\n"; | ||
702 | t << "POPPAssword: " + accountPtr->popPasswd + "\n"; | ||
703 | t << "POPServer: " + accountPtr->popServer + "\n"; | ||
704 | t << "SMTPServer: " + accountPtr->smtpServer + "\n"; | ||
705 | t << "AccountId: " << accountPtr->id << "\n"; | ||
706 | if (accountPtr->synchronize) { | ||
707 | t << "Synchronize: Yes\n"; | ||
708 | t << "LastServerMailCount: "; | ||
709 | t << accountPtr->lastServerMailCount << "\n"; | ||
710 | } else { | ||
711 | t << "Synchronize: No\n"; | ||
712 | } | ||
713 | t << "accountEnd;\n"; | ||
714 | } | ||
715 | f.close(); | ||
716 | |||
717 | mailconf->setGroup("mailitglobal"); | ||
718 | mailconf->writeEntry("mailidcount", mailIdCount); | ||
719 | mailconf->writeEntry("accountidcount", accountIdCount); | ||
720 | } | ||
721 | |||
722 | void EmailClient::selectAccount(int id) | ||
723 | { | ||
724 | if (accountList.count() > 0) { | ||
725 | currentAccount = accountList.at(id); | ||
726 | emit newCaption("Mailit - " + currentAccount->accountName); | ||
727 | getNewMail(); | ||
728 | } else { | ||
729 | emit newCaption("Mailit ! No account defined"); | ||
730 | } | ||
731 | } | ||
732 | |||
733 | void EmailClient::editAccount(int id) | ||
734 | { | ||
735 | MailAccount *newAccount; | ||
736 | |||
737 | editAccountView = new EditAccount(this, "account", TRUE); | ||
738 | if (id == newAccountId) { //new account | ||
739 | newAccount = new MailAccount; | ||
740 | editAccountView->setAccount(newAccount); | ||
741 | } else { | ||
742 | newAccount = accountList.at(id); | ||
743 | editAccountView->setAccount(newAccount, FALSE); | ||
744 | } | ||
745 | |||
746 | editAccountView->showMaximized(); | ||
747 | editAccountView->exec(); | ||
748 | |||
749 | if (editAccountView->result() == QDialog::Accepted) { | ||
750 | if (id == newAccountId) { | ||
751 | newAccount->id = accountIdCount; | ||
752 | accountIdCount++; | ||
753 | accountList.append(newAccount); | ||
754 | updateAccounts(); | ||
755 | } else { | ||
756 | updateAccounts(); | ||
757 | } | ||
758 | } | ||
759 | |||
760 | delete editAccountView; | ||
761 | } | ||
762 | |||
763 | void EmailClient::deleteAccount(int id) | ||
764 | { | ||
765 | MailAccount *newAccount; | ||
766 | QString message; | ||
767 | |||
768 | newAccount = accountList.at(id); | ||
769 | message = "Delete account:\n" + newAccount->accountName; | ||
770 | switch( QMessageBox::warning( this, "Mailit", message, | ||
771 | "Yes", "No", 0, 0, 1 ) ) { | ||
772 | |||
773 | case 0: accountList.remove(id); | ||
774 | updateAccounts(); | ||
775 | break; | ||
776 | case 1: | ||
777 | break; | ||
778 | } | ||
779 | } | ||
780 | |||
781 | void EmailClient::updateAccounts() | ||
782 | { | ||
783 | MailAccount *accountPtr; | ||
784 | |||
785 | //rebuild menus, clear all first | ||
786 | editAccountMenu->clear(); | ||
787 | selectAccountMenu->clear(); | ||
788 | deleteAccountMenu->clear(); | ||
789 | |||
790 | newAccountId = editAccountMenu->insertItem("New", this, | ||
791 | SLOT(editAccount(int)) ); | ||
792 | editAccountMenu->insertSeparator(); | ||
793 | |||
794 | idCount = 0; | ||
795 | for (accountPtr = accountList.first(); accountPtr != 0; | ||
796 | accountPtr = accountList.next()) { | ||
797 | |||
798 | editAccountMenu->insertItem(accountPtr->accountName, | ||
799 | this, SLOT(editAccount(int)), 0, idCount); | ||
800 | selectAccountMenu->insertItem(accountPtr->accountName, | ||
801 | this, SLOT(selectAccount(int)), 0, idCount); | ||
802 | deleteAccountMenu->insertItem(accountPtr->accountName, | ||
803 | this, SLOT(deleteAccount(int)), 0, idCount); | ||
804 | idCount++; | ||
805 | } | ||
806 | } | ||
807 | |||
808 | void EmailClient::deleteMail(EmailListItem *mailItem, bool &inbox) | ||
809 | { | ||
810 | Email *mPtr; | ||
811 | Enclosure *ePtr; | ||
812 | |||
813 | if (inbox) { | ||
814 | mPtr = mailItem->getMail(); | ||
815 | |||
816 | //if mail is in queue for download, remove it from | ||
817 | //queue if possible | ||
818 | if ( (receiving) && (mPtr->fromAccountId == currentAccount->id) ) { | ||
819 | if ( !mPtr->downloaded ) | ||
820 | mailDownloadList.remove(mPtr->serverId, mPtr->size); | ||
821 | } | ||
822 | |||
823 | mailconf->setGroup(mPtr->id); | ||
824 | mailconf->clearGroup(); | ||
825 | |||
826 | //delete any temporary attatchemnts storing | ||
827 | for ( ePtr=mPtr->files.first(); ePtr != 0; ePtr=mPtr->files.next() ) { | ||
828 | if (ePtr->saved) { | ||
829 | QFile::remove( (ePtr->path + ePtr->name) ); | ||
830 | } | ||
831 | } | ||
832 | inboxView->takeItem(mailItem); | ||
833 | } else { | ||
834 | outboxView->takeItem(mailItem); | ||
835 | } | ||
836 | } | ||
837 | |||
838 | void EmailClient::setMailSize(int size) | ||
839 | { | ||
840 | progressBar->reset(); | ||
841 | progressBar->setTotalSteps(size); | ||
842 | } | ||
843 | |||
844 | void EmailClient::setTotalSize(int size) | ||
845 | { | ||
846 | |||
847 | } | ||
848 | |||
849 | void EmailClient::setDownloadedSize(int size) | ||
850 | { | ||
851 | int total = progressBar->totalSteps(); | ||
852 | |||
853 | if (size < total) { | ||
854 | progressBar->setProgress(size); | ||
855 | } else { | ||
856 | progressBar->setProgress(total); | ||
857 | } | ||
858 | } | ||