summaryrefslogtreecommitdiff
path: root/noncore/net/mail/opiemail.cpp
blob: abf93dcd61f341f4b97288d04c5b1dbe56348fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

#include "settingsdialog.h"
#include "opiemail.h"
#include "editaccounts.h"
#include "composemail.h"
#include "mailistviewitem.h"
#include "viewmail.h"
#include "selectstore.h"
#include "selectsmtp.h"

/* OPIE */
#include <libmailwrapper/smtpwrapper.h>
#include <libmailwrapper/mailtypes.h>
#include <libmailwrapper/abstractmail.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpe/resource.h>
#include <qpe/qpeapplication.h>

/* QT */
#include <qmessagebox.h>
#include <qaction.h>
#include <qapplication.h>

OpieMail::OpieMail( QWidget *parent, const char *name, WFlags flags )
        : MainWindow( parent, name, WStyle_ContextHelp )
{
    settings = new Settings();

    folderView->populate( settings->getAccounts() );
}

OpieMail::~OpieMail()
{
    if (settings) delete settings;
}

void OpieMail::appMessage(const QCString &msg, const QByteArray &data)
{
    // copied from old mail2
    if (msg == "writeMail(QString,QString)")
    {
        QDataStream stream(data,IO_ReadOnly);
        QString name, email;
        stream >> name >> email;
        // removing the whitespaces at beginning and end is needed!
        slotwriteMail(name.stripWhiteSpace(),email.stripWhiteSpace());
    }
    else if (msg == "newMail()")
    {
        slotComposeMail();
    }
}

void OpieMail::slotwriteMail(const QString&name,const QString&email)
{
    ComposeMail compose( settings, this, 0, true , WStyle_ContextHelp );
    if (!email.isEmpty())
    {
        if (!name.isEmpty())
        {
            compose.setTo("\"" + name + "\"" + " " + "<"+ email + ">");
        }
        else
        {
            compose.setTo(email);
        }
    }
    compose.slotAdjustColumns();
    QPEApplication::execDialog( &compose );
}

void OpieMail::slotComposeMail()
{
    qDebug( "Compose Mail" );
    slotwriteMail(0l,0l);
}

void OpieMail::slotSendQueued()
{
    qDebug( "Send Queued" );
    SMTPaccount *smtp = 0;

    QList<Account> list = settings->getAccounts();
    QList<SMTPaccount> smtpList;
    smtpList.setAutoDelete(false);
    Account *it;
    for ( it = list.first(); it; it = list.next() )
    {
        if ( it->getType().compare( "SMTP" ) == 0 )
        {
            smtp = static_cast<SMTPaccount *>(it);
            smtpList.append(smtp);
        }
    }
    if (smtpList.count()==0)
    {
        QMessageBox::information(0,tr("Info"),tr("Define a smtp account first"));
        return;
    }
    if (smtpList.count()==1)
    {
        smtp = smtpList.at(0);
    }
    else
    {
        smtp = 0;
        selectsmtp selsmtp;
        selsmtp.setSelectionlist(&smtpList);
        if ( QPEApplication::execDialog( &selsmtp ) == QDialog::Accepted )
        {
            smtp = selsmtp.selected_smtp();
        }
    }
    if (smtp)
    {
        SMTPwrapper * wrap = new SMTPwrapper(smtp);
        if ( wrap->flushOutbox() )
        {
            QMessageBox::information(0,tr("Info"),tr("Mail queue flushed"));
        }
        delete wrap;
    }
}

void OpieMail::slotSearchMails()
{
    qDebug( "Search Mails" );
}

void OpieMail::slotEditSettings()
{
    SettingsDialog settingsDialog( this,  0, true,  WStyle_ContextHelp );
    QPEApplication::execDialog( &settingsDialog );
}

void OpieMail::slotEditAccounts()
{
    qDebug( "Edit Accounts" );
    EditAccounts eaDialog( settings, this, 0, true,  WStyle_ContextHelp );
    eaDialog.slotAdjustColumns();
    QPEApplication::execDialog( &eaDialog );
    if ( settings ) delete settings;
    settings = new Settings();

    folderView->populate( settings->getAccounts() );
}

void OpieMail::displayMail()
{
    QListViewItem*item = mailView->currentItem();
    if (!item) return;
    RecMail mail = ((MailListViewItem*)item)->data();
    RecBody body = folderView->fetchBody(mail);
    ViewMail readMail( this,"", Qt::WType_Modal | WStyle_ContextHelp  );
    readMail.setBody( body );
    readMail.setMail( mail );
    readMail.showMaximized();
    readMail.exec();

    if (  readMail.deleted )
    {
        folderView->refreshCurrent();
    }
    else
    {
        ( (MailListViewItem*)item )->setPixmap( 0, Resource::loadPixmap( "" ) );
    }
}

void OpieMail::slotDeleteMail()
{
    if (!mailView->currentItem()) return;
    RecMail mail = ((MailListViewItem*)mailView->currentItem() )->data();
    if ( QMessageBox::warning(this, tr("Delete Mail"), QString( tr("<p>Do you really want to delete this mail? <br><br>" ) + mail.getFrom() + " - " + mail.getSubject() ) , QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
    {
        mail.Wrapper()->deleteMail( mail );
        folderView->refreshCurrent();
    }
}

void OpieMail::mailHold(int button, QListViewItem *item,const QPoint&,int  )
{
    /* just the RIGHT button - or hold on pda */
    if (button!=2) {return;}
    qDebug("Event right/hold");
    if (!item) return;
    QPopupMenu *m = new QPopupMenu(0);
    if (m)
    {
        m->insertItem(tr("Read this mail"),this,SLOT(displayMail()));
        m->insertItem(tr("Delete this mail"),this,SLOT(slotDeleteMail()));
        m->insertItem(tr("Copy/Move this mail"),this,SLOT(slotMoveCopyMail()));
        m->setFocus();
        m->exec( QPoint( QCursor::pos().x(), QCursor::pos().y()) );
        delete m;
    }
}

void OpieMail::slotShowFolders( bool show )
{
    qDebug( "Show Folders" );
    if ( show && folderView->isHidden() )
    {
        qDebug( "-> showing" );
        folderView->show();
    }
    else if ( !show && !folderView->isHidden() )
    {
        qDebug( "-> hiding" );
        folderView->hide();
    }
}

void OpieMail::refreshMailView(QList<RecMail>*list)
{
    MailListViewItem*item = 0;
    mailView->clear();
    for (unsigned int i = 0; i < list->count();++i)
    {
        item = new MailListViewItem(mailView,item);
        item->storeData(*(list->at(i)));
        item->showEntry();
    }
}

void OpieMail::mailLeftClicked(int button, QListViewItem *item,const QPoint&,int )
{
    /* just LEFT button - or tap with stylus on pda */
    if (button!=1) return;
    if (!item) return;
    displayMail();
}

void OpieMail::slotMoveCopyMail()
{
    if (!mailView->currentItem()) return;
    RecMail mail = ((MailListViewItem*)mailView->currentItem() )->data();
    AbstractMail*targetMail = 0;
    QString targetFolder = "";
    Selectstore sels;
    folderView->setupFolderselect(&sels);
    if (!sels.exec()) return;
    targetMail = sels.currentMail();
    targetFolder = sels.currentFolder();
    if ( (mail.Wrapper()==targetMail && mail.getMbox()==targetFolder) ||
            targetFolder.isEmpty())
    {
        return;
    }
    if (sels.newFolder() && !targetMail->createMbox(targetFolder))
    {
        QMessageBox::critical(0,tr("Error creating new Folder"),
                              tr("<center>Error while creating<br>new folder - breaking.</center>"));
        return;
    }
    mail.Wrapper()->mvcpMail(mail,targetFolder,targetMail,sels.moveMails());
    folderView->refreshCurrent();
}