summaryrefslogtreecommitdiff
path: root/noncore/net/mail/accountview.cpp
blob: 0e739e22ed0bda48633536f6a24f109448619da5 (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

#include "accountview.h"
#include "accountitem.h"
#include "selectstore.h"

#include <libmailwrapper/settings.h>
#include <libmailwrapper/mailwrapper.h>
#include <libmailwrapper/mailtypes.h>
#include <libmailwrapper/abstractmail.h>

/* OPIE */
#include <opie2/odebug.h>
#include <qpe/qpeapplication.h>
#include <qpe/config.h>

/* QT */
#include <qmessagebox.h>
#include <qpopupmenu.h>

using namespace Opie::Core;
AccountView::AccountView( QWidget *parent, const char *name, WFlags flags )
        : QListView( parent, name, flags )
{
    setSorting(0);
    setSelectionMode(Single);
    m_rightPressed = false;

    connect( this, SIGNAL( selectionChanged(QListViewItem*) ),
             SLOT( slotSelectionChanged(QListViewItem*) ) );
    connect( this, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ),this,
             SLOT( slotMouseButton(int,QListViewItem*,const QPoint&,int) ) );
    connect( this, SIGNAL(clicked(QListViewItem*) ),this,
             SLOT( slotMouseClicked(QListViewItem*) ) );
    m_currentItem = 0;
    readSettings();
}

AccountView::~AccountView()
{
    imapAccounts.clear();
    mhAccounts.clear();
}

void AccountView::readSettings()
{
    Config cfg("mail");
    cfg.setGroup( "Settings" );
    m_clickopens = cfg.readBoolEntry("clickOpensFolder",true);
}

void AccountView::slotSelectionChanged(QListViewItem*item)
{
    if (!item) {
        emit serverSelected(0);
        return;
    }
    AccountViewItem *view = static_cast<AccountViewItem *>(item);
    emit serverSelected(view->isServer());
}

QMap<int,QString> AccountView::currentServerMenu()const
{
    QMap<int,QString> smap;
    AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
    if (!view)return smap;
    smap = view->serverMenu();
    return smap;
}

QMap<int,QString> AccountView::currentFolderMenu()const
{
    QMap<int,QString> fmap;
    AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
    if (!view)return fmap;
    fmap = view->folderMenu();
    return fmap;
}

void AccountView::slotContextMenu(int id)
{
    AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
    if (!view) return;
    bool del = view->contextMenuSelected(id);
    if (!del && view->isServer()!=2) {
        emit refreshMenues(view->isServer());
    }
}

void AccountView::slotRightButton(int, QListViewItem * item,const QPoint&,int)
{
    m_rightPressed = true;
    if (!item) return;
    AccountViewItem *view = static_cast<AccountViewItem *>(item);
    QPopupMenu*m = view->getContextMenu();
    if (!m) return;
    connect(m,SIGNAL(activated(int)),this,SLOT(slotContextMenu(int)));
    m->setFocus();
    m->exec( QPoint( QCursor::pos().x(), QCursor::pos().y()) );
    delete m;
}

void AccountView::slotLeftButton(int, QListViewItem *,const QPoint&,int)
{
    m_rightPressed = false;
}

void AccountView::slotMouseClicked(QListViewItem*item)
{
    if (m_rightPressed) return;
    if (!item || m_currentItem == item||!m_clickopens) return;
    /* ### ToDo check settings if on single tab it should open */
    m_currentItem = item;
    refresh(m_currentItem);
}

void AccountView::slotMouseButton(int button, QListViewItem * item,const QPoint&pos,int column)
{
    if (button==1) {
        slotLeftButton(button,item,pos,column);
    } else if (button==2) {
        slotRightButton(button,item,pos,column);
    }
}

void AccountView::populate( QList<Account> list )
{
    clear();

    imapAccounts.clear();
    mhAccounts.clear();
    mhAccounts.append(new MHviewItem(AbstractMail::defaultLocalfolder(),this));

    Account *it;
    for ( it = list.first(); it; it = list.next() ) {
        if ( it->getType() == MAILLIB::A_IMAP ) {
            IMAPaccount *imap = static_cast<IMAPaccount *>(it);
            odebug << "added IMAP " + imap->getAccountName() << oendl;
            imapAccounts.append(new IMAPviewItem( imap, this ));
        } else if ( it->getType() == MAILLIB::A_POP3 ) {
            POP3account *pop3 = static_cast<POP3account *>(it);
            odebug << "added POP3 " + pop3->getAccountName() << oendl;
            /* must not be hold 'cause it isn't required */
            (void) new POP3viewItem( pop3, this );
        } else if ( it->getType() == MAILLIB::A_NNTP ) {
            NNTPaccount *nntp = static_cast<NNTPaccount *>(it);
            odebug << "added NNTP " + nntp->getAccountName() << oendl;
            /* must not be hold 'cause it isn't required */
            (void) new NNTPviewItem( nntp, this );
        } else if ( it->getType() == MAILLIB::A_MH ) {
        }
    }
}

void AccountView::refresh(QListViewItem *item)
{

    odebug << "AccountView refresh..." << oendl;
    if ( item )
    {
        m_currentItem = item;
        QValueList<RecMailP> headerlist;
        AccountViewItem *view = static_cast<AccountViewItem *>(item);
        view->refresh(headerlist);
        emit refreshMailview(headerlist);
    }
}

void AccountView::refreshCurrent()
{
    m_currentItem = currentItem();
    if ( !m_currentItem ) return;
    QValueList<RecMailP> headerlist;
    AccountViewItem *view = static_cast<AccountViewItem *>(m_currentItem);
    view->refresh(headerlist);
    emit refreshMailview(headerlist);
}

void AccountView::refreshAll()
{
}

RecBodyP AccountView::fetchBody(const RecMailP&aMail)
{
    QListViewItem*item = selectedItem ();
    if (!item) return new RecBody();
    AccountViewItem *view = static_cast<AccountViewItem *>(item);
    return view->fetchBody(aMail);
}

void AccountView::setupFolderselect(Selectstore*sels)
{
    QPEApplication::showDialog( sels );
    QStringList sFolders;
    unsigned int i = 0;
    for (i=0; i < mhAccounts.count();++i)
    {
        mhAccounts[i]->refresh(false);
        sFolders = mhAccounts[i]->subFolders();
        sels->addAccounts(mhAccounts[i]->getWrapper(),sFolders);
    }
    for (i=0; i < imapAccounts.count();++i)
    {
        if (imapAccounts[i]->offline())
            continue;
        imapAccounts[i]->refreshFolders(false);
        sels->addAccounts(imapAccounts[i]->getWrapper(),imapAccounts[i]->subFolders());
    }
}

void AccountView::downloadMails(const FolderP&fromFolder,AbstractMail*fromWrapper)
{
    AbstractMail*targetMail = 0;
    QString targetFolder = "";
    Selectstore sels;
    setupFolderselect(&sels);
    if (!sels.exec()) return;
    targetMail = sels.currentMail();
    targetFolder = sels.currentFolder();
    if ( (fromWrapper==targetMail && fromFolder->getName()==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;
    }
    odebug << "Targetfolder: " << targetFolder.latin1() << "" << oendl;
    odebug << "Fromfolder: " << fromFolder->getName().latin1() << "" << oendl;
    fromWrapper->mvcpAllMails(fromFolder,targetFolder,targetMail,sels.moveMails());
    refreshCurrent();
}

bool AccountView::currentisDraft()
{
    AccountViewItem *view = static_cast<AccountViewItem *>(currentItem());
    if (!view) return false;
    return view->isDraftfolder();
}