-rw-r--r-- | kmicromail/composemail.cpp | 357 |
1 files changed, 357 insertions, 0 deletions
diff --git a/kmicromail/composemail.cpp b/kmicromail/composemail.cpp new file mode 100644 index 0000000..2c2e279 --- a/dev/null +++ b/kmicromail/composemail.cpp | |||
@@ -0,0 +1,357 @@ | |||
1 | |||
2 | #include "composemail.h" | ||
3 | |||
4 | #include <libmailwrapper/smtpwrapper.h> | ||
5 | #include <libmailwrapper/storemail.h> | ||
6 | #include <libmailwrapper/abstractmail.h> | ||
7 | #include <libmailwrapper/mailtypes.h> | ||
8 | |||
9 | /* OPIE */ | ||
10 | //#include <opie2/ofiledialog.h> | ||
11 | //#include <opie2/odebug.h> | ||
12 | #include <kfiledialog.h> | ||
13 | //#include <qpe/resource.h> | ||
14 | #include <qpe/config.h> | ||
15 | #include <qpe/global.h> | ||
16 | //#include <qpe/contact.h> | ||
17 | |||
18 | |||
19 | #include <qcombobox.h> | ||
20 | #include <qcheckbox.h> | ||
21 | #include <qtimer.h> | ||
22 | #include <qmessagebox.h> | ||
23 | #include <qpushbutton.h> | ||
24 | #include <qmultilineedit.h> | ||
25 | #include <qlabel.h> | ||
26 | #include <qtabwidget.h> | ||
27 | #include <qlistview.h> | ||
28 | #include <kabc/addresseedialog.h> | ||
29 | #include <kabc/stdaddressbook.h> | ||
30 | #include <kabc/addressee.h> | ||
31 | |||
32 | |||
33 | |||
34 | //using namespace Opie::Core; | ||
35 | //using namespace Opie::Ui; | ||
36 | ComposeMail::ComposeMail( Settings *s, QWidget *parent, const char *name, bool modal, WFlags flags ) | ||
37 | : ComposeMailUI( parent, name, modal, flags ) | ||
38 | { | ||
39 | |||
40 | settings = s; | ||
41 | m_replyid = ""; | ||
42 | KABC::Addressee con = KABC::StdAddressBook::self()->whoAmI( ); | ||
43 | QStringList mails = con.emails(); | ||
44 | QString defmail = con.preferredEmail(); | ||
45 | if ( mails.count() == 0) | ||
46 | QMessageBox::information( parentWidget(), tr( "Hint" ), | ||
47 | tr( "Please apply\n\"Set Who Am I\"\nin KA/Pi to get the from\nfield automatically filled out!" ), | ||
48 | tr( "Ok" ) ); | ||
49 | if (defmail.length()!=0) { | ||
50 | fromBox->insertItem(defmail); | ||
51 | } | ||
52 | QStringList::ConstIterator sit = mails.begin(); | ||
53 | for (;sit!=mails.end();++sit) { | ||
54 | if ( (*sit)==defmail) | ||
55 | continue; | ||
56 | fromBox->insertItem((*sit)); | ||
57 | } | ||
58 | senderNameEdit->setText(con.formattedName()); | ||
59 | Config cfg( "mail" ); | ||
60 | cfg.setGroup( "Compose" ); | ||
61 | checkBoxLater->setChecked( cfg.readBoolEntry( "sendLater", false ) ); | ||
62 | |||
63 | attList->addColumn( tr( "Name" ) ); | ||
64 | attList->addColumn( tr( "Size" ) ); | ||
65 | |||
66 | QList<Account> accounts = settings->getAccounts(); | ||
67 | |||
68 | Account *it; | ||
69 | for ( it = accounts.first(); it; it = accounts.next() ) { | ||
70 | if ( it->getType()==MAILLIB::A_SMTP ) { | ||
71 | SMTPaccount *smtp = static_cast<SMTPaccount *>(it); | ||
72 | smtpAccountBox->insertItem( smtp->getAccountName() ); | ||
73 | smtpAccounts.append( smtp ); | ||
74 | } | ||
75 | } | ||
76 | if ( smtpAccounts.count() > 0 ) { | ||
77 | fillValues( smtpAccountBox->currentItem() ); | ||
78 | } else { | ||
79 | QMessageBox::information( parentWidget(), tr( "Problem" ), | ||
80 | tr( "Please create an SMTP account first.\nThe SMTP is needed for sending mail." ), | ||
81 | tr( "Ok" ) ); | ||
82 | return; | ||
83 | } | ||
84 | connect( smtpAccountBox, SIGNAL( activated(int) ), SLOT( fillValues(int) ) ); | ||
85 | connect( toButton, SIGNAL( clicked() ), SLOT( pickAddressTo() ) ); | ||
86 | connect( ccButton, SIGNAL( clicked() ), SLOT( pickAddressCC() ) ); | ||
87 | connect( bccButton, SIGNAL( clicked() ), SLOT( pickAddressBCC() ) ); | ||
88 | connect( replyButton, SIGNAL( clicked() ), SLOT( pickAddressReply() ) ); | ||
89 | connect( addButton, SIGNAL( clicked() ), SLOT( addAttachment() ) ); | ||
90 | connect( deleteButton, SIGNAL( clicked() ), SLOT( removeAttachment() ) ); | ||
91 | connect( SaveButton, SIGNAL( clicked() ), SLOT( saveAsDraft()) ); | ||
92 | mMail = 0; | ||
93 | warnAttach = true; | ||
94 | } | ||
95 | void ComposeMail::saveAsDraft() | ||
96 | { | ||
97 | |||
98 | Opie::Core::OSmartPointer<Mail> mail= new Mail(); | ||
99 | mail->setMail(fromBox->currentText()); | ||
100 | mail->setTo( toLine->text() ); | ||
101 | mail->setName(senderNameEdit->text()); | ||
102 | mail->setCC( ccLine->text() ); | ||
103 | mail->setBCC( bccLine->text() ); | ||
104 | mail->setReply( replyLine->text() ); | ||
105 | mail->setSubject( subjectLine->text() ); | ||
106 | if (!m_replyid.isEmpty()) { | ||
107 | QStringList ids; | ||
108 | ids.append(m_replyid); | ||
109 | mail->setInreply(ids); | ||
110 | } | ||
111 | QString txt = message->text(); | ||
112 | if ( !sigMultiLine->text().isEmpty() ) { | ||
113 | txt.append( "\n--\n" ); | ||
114 | txt.append( sigMultiLine->text() ); | ||
115 | } | ||
116 | mail->setMessage( txt ); | ||
117 | |||
118 | /* only use the default drafts folder name! */ | ||
119 | Storemail wrapper(AbstractMail::draftFolder()); | ||
120 | wrapper.storeMail(mail); | ||
121 | |||
122 | AttachViewItem *it = (AttachViewItem *) attList->firstChild(); | ||
123 | /* attachments we will ignore! */ | ||
124 | if ( it != NULL ) { | ||
125 | if ( warnAttach ) | ||
126 | QMessageBox::warning(0,tr("Store message"), | ||
127 | tr("<center>Attachments will not be stored in \"Draft\" folder</center>")); | ||
128 | warnAttach = false; | ||
129 | } | ||
130 | setStatus( tr("Mail saved as draft!") ); | ||
131 | } | ||
132 | void ComposeMail::clearStatus() | ||
133 | { | ||
134 | topLevelWidget()->setCaption( tr("Compose mail") ); | ||
135 | } | ||
136 | void ComposeMail::setStatus( QString status ) | ||
137 | { | ||
138 | topLevelWidget()->setCaption( status ); | ||
139 | QTimer::singleShot ( 5000, this, SLOT( clearStatus() ) ) ; | ||
140 | } | ||
141 | void ComposeMail::pickAddress( QLineEdit *line ) | ||
142 | { | ||
143 | //qDebug(" ComposeMail::pickAddress "); | ||
144 | QString names ;//= AddressPicker::getNames(); | ||
145 | |||
146 | KABC::Addressee::List list = KABC::AddresseeDialog::getAddressees(this); | ||
147 | uint i=0; | ||
148 | for (i=0; i < list.count(); i++) { | ||
149 | if ( !list[i].preferredEmail().isEmpty()) { | ||
150 | names+= "\""+list[i].realName() +"\"<" +list[i].preferredEmail() +">"; | ||
151 | if ( i < list.count() -1 ) | ||
152 | names+= ","; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | |||
157 | if ( line->text().isEmpty() ) { | ||
158 | line->setText( names ); | ||
159 | } else if ( !names.isEmpty() ) { | ||
160 | line->setText( line->text() + ", " + names ); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | |||
165 | void ComposeMail::setTo( const QString & to ) | ||
166 | { | ||
167 | toLine->setText( to ); | ||
168 | } | ||
169 | |||
170 | void ComposeMail::setSubject( const QString & subject ) | ||
171 | { | ||
172 | subjectLine->setText( subject ); | ||
173 | } | ||
174 | |||
175 | void ComposeMail::setInReplyTo( const QString & messageId ) | ||
176 | { | ||
177 | m_replyid = messageId; | ||
178 | } | ||
179 | |||
180 | void ComposeMail::setMessage( const QString & text ) | ||
181 | { | ||
182 | message->setText( text ); | ||
183 | } | ||
184 | |||
185 | |||
186 | void ComposeMail::pickAddressTo() | ||
187 | { | ||
188 | pickAddress( toLine ); | ||
189 | } | ||
190 | |||
191 | void ComposeMail::pickAddressCC() | ||
192 | { | ||
193 | pickAddress( ccLine ); | ||
194 | } | ||
195 | |||
196 | void ComposeMail::pickAddressBCC() | ||
197 | { | ||
198 | pickAddress( bccLine ); | ||
199 | } | ||
200 | |||
201 | void ComposeMail::pickAddressReply() | ||
202 | { | ||
203 | pickAddress( replyLine ); | ||
204 | } | ||
205 | |||
206 | void ComposeMail::fillValues( int ) | ||
207 | { | ||
208 | #if 0 | ||
209 | SMTPaccount *smtp = smtpAccounts.at( current ); | ||
210 | ccLine->clear(); | ||
211 | if ( smtp->getUseCC() ) { | ||
212 | ccLine->setText( smtp->getCC() ); | ||
213 | } | ||
214 | bccLine->clear(); | ||
215 | if ( smtp->getUseBCC() ) { | ||
216 | bccLine->setText( smtp->getBCC() ); | ||
217 | } | ||
218 | replyLine->clear(); | ||
219 | if ( smtp->getUseReply() ) { | ||
220 | replyLine->setText( smtp->getReply() ); | ||
221 | } | ||
222 | sigMultiLine->setText( smtp->getSignature() ); | ||
223 | #endif | ||
224 | } | ||
225 | |||
226 | void ComposeMail::slotAdjustColumns() | ||
227 | { | ||
228 | int currPage = tabWidget->currentPageIndex(); | ||
229 | |||
230 | tabWidget->showPage( attachTab ); | ||
231 | attList->setColumnWidth( 0, attList->visibleWidth() - 80 ); | ||
232 | attList->setColumnWidth( 1, 80 ); | ||
233 | |||
234 | tabWidget->setCurrentPage( currPage ); | ||
235 | } | ||
236 | |||
237 | void ComposeMail::addAttachment() | ||
238 | { | ||
239 | QString lnk = KFileDialog::getOpenFileName( "", "Add Attachment", this ); | ||
240 | if ( !lnk.isEmpty() ) { | ||
241 | Attachment *att = new Attachment( lnk ); | ||
242 | (void) new AttachViewItem( attList, att ); | ||
243 | } | ||
244 | } | ||
245 | |||
246 | void ComposeMail::removeAttachment() | ||
247 | { | ||
248 | if ( !attList->currentItem() ) { | ||
249 | QMessageBox::information( this, tr( "Error" ), | ||
250 | tr( "<p>Please select a File.</p>" ), | ||
251 | tr( "Ok" ) ); | ||
252 | } else { | ||
253 | attList->takeItem( attList->currentItem() ); | ||
254 | } | ||
255 | } | ||
256 | |||
257 | void ComposeMail::accept() | ||
258 | { | ||
259 | if (! checkBoxLater->isChecked() ) { | ||
260 | int yesno = QMessageBox::warning(0,tr("Stop editing message"), | ||
261 | tr("Send this message?"), | ||
262 | tr("Yes"), | ||
263 | tr("Cancel")); | ||
264 | |||
265 | if (yesno == 1) { | ||
266 | return; | ||
267 | } | ||
268 | } | ||
269 | #if 0 | ||
270 | odebug << "Sending Mail with " | ||
271 | << smtpAccounts.at( smtpAccountBox->currentItem() )->getAccountName() << oendl; | ||
272 | #endif | ||
273 | Opie::Core::OSmartPointer<Mail> mail=new Mail; | ||
274 | |||
275 | SMTPaccount *smtp = smtpAccounts.at( smtpAccountBox->currentItem() ); | ||
276 | mail->setMail(fromBox->currentText()); | ||
277 | |||
278 | if ( !toLine->text().isEmpty() ) { | ||
279 | mail->setTo( toLine->text() ); | ||
280 | } else { | ||
281 | QMessageBox::warning(0,tr("Sending mail"), | ||
282 | tr("No Receiver spezified" ) ); | ||
283 | return; | ||
284 | } | ||
285 | |||
286 | mail->setName(senderNameEdit->text()); | ||
287 | mail->setCC( ccLine->text() ); | ||
288 | mail->setBCC( bccLine->text() ); | ||
289 | mail->setReply( replyLine->text() ); | ||
290 | mail->setSubject( subjectLine->text() ); | ||
291 | if (!m_replyid.isEmpty()) { | ||
292 | QStringList ids; | ||
293 | ids.append(m_replyid); | ||
294 | mail->setInreply(ids); | ||
295 | } | ||
296 | QString txt = message->text(); | ||
297 | if ( !sigMultiLine->text().isEmpty() ) { | ||
298 | txt.append( "\n--\n" ); | ||
299 | txt.append( sigMultiLine->text() ); | ||
300 | } | ||
301 | mail->setMessage( txt ); | ||
302 | AttachViewItem *it = (AttachViewItem *) attList->firstChild(); | ||
303 | while ( it != NULL ) { | ||
304 | mail->addAttachment( it->getAttachment() ); | ||
305 | it = (AttachViewItem *) it->nextSibling(); | ||
306 | } | ||
307 | |||
308 | SMTPwrapper wrapper( smtp ); | ||
309 | wrapper.sendMail( mail,checkBoxLater->isChecked() ); | ||
310 | |||
311 | QDialog::accept(); | ||
312 | } | ||
313 | |||
314 | void ComposeMail::reject() | ||
315 | { | ||
316 | //qDebug("ComposeMail::reject() "); | ||
317 | int yesno = QMessageBox::warning(0,tr("Stop editing message"), | ||
318 | tr("Store message into drafts?"), | ||
319 | tr("Yes"), | ||
320 | tr("No")); | ||
321 | |||
322 | //qDebug("button %d ", yesno); | ||
323 | if (yesno == 0) { | ||
324 | saveAsDraft(); | ||
325 | } | ||
326 | if (yesno == 2) { | ||
327 | qDebug("return "); | ||
328 | return; | ||
329 | } | ||
330 | QDialog::reject(); | ||
331 | } | ||
332 | |||
333 | ComposeMail::~ComposeMail() | ||
334 | { | ||
335 | } | ||
336 | |||
337 | void ComposeMail::reEditMail(const RecMailP¤t) | ||
338 | { | ||
339 | RecMailP data = current; | ||
340 | message->setText(data->Wrapper()->fetchBody(current)->Bodytext()); | ||
341 | subjectLine->setText( data->getSubject()); | ||
342 | toLine->setText(data->To().join(",")); | ||
343 | ccLine->setText(data->CC().join(",")); | ||
344 | bccLine->setText(data->Bcc().join(",")); | ||
345 | replyLine->setText(data->Replyto()); | ||
346 | } | ||
347 | |||
348 | AttachViewItem::AttachViewItem( QListView *parent, Attachment *att ) | ||
349 | : QListViewItem( parent ) | ||
350 | { | ||
351 | attachment = att; | ||
352 | if ( !attachment->getPixmap().isNull() ) | ||
353 | setPixmap( 0,attachment->getPixmap() ); | ||
354 | setText( 0, att->getName().isEmpty() ? att->getFileName() : att->getName() ); | ||
355 | setText( 1, QString::number( att->getSize() ) ); | ||
356 | } | ||
357 | |||