~dpniel/dekko/0.5

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
/* Copyright (C) 2014-2015 Dan Chapman <dpniel@ubuntu.com>

   This file is part of Dekko email client for Ubuntu Devices/

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License or (at your option) version 3

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "MailboxProxyModel.h"
#include "Imap/Model/SubtreeModel.h"
#include <QDebug>
namespace Dekko
{
namespace Models
{
MailboxProxyModel::MailboxProxyModel(QObject *parent) :
    RowsJoinerProxy(parent)
{
}

QHash<int, QByteArray> MailboxProxyModel::roleNames() const
{
    if (m_mboxModels.size() > 0)
        return m_mboxModels.at(0)->roleNames();
    else
        return QHash<int, QByteArray>();
}

QList<QAbstractItemModel *> MailboxProxyModel::mailboxModels() const
{
    return m_mboxModels;
}

void MailboxProxyModel::setMailboxModels(QList<QAbstractItemModel *> mboxList)
{
    if (mboxList.size() <= 0) {
        return;
    } else if (RowsJoinerProxy::models().size() > 0) {
        // We need to remove all these and basically do a resetModel
        // Since this is bound to the modelsChanged property binding in qml this will get reevaluated
        // after every models changed signal from the accountsmanager
        Q_FOREACH(QAbstractItemModel *model, RowsJoinerProxy::models()) {
            Q_ASSERT(model);
            removeSourceModel(model);
        }
    }
    m_mboxModels = mboxList;
    Q_FOREACH(QAbstractItemModel *aim, m_mboxModels) {
        Imap::Mailbox::SubtreeModelOfMailboxModel *model = qobject_cast<Imap::Mailbox::SubtreeModelOfMailboxModel *>(aim);
        Q_ASSERT(model);
        insertSourceModel(model);
        if (m_roles.isEmpty()) {
            m_roles = model->roleNames();
        }
    }
    emit modelsChanged();
}

QObject *MailboxProxyModel::get(int index) const
{
    return m_mboxModels.at(index);
}

QObject *MailboxProxyModel::getAccountForIndex(int index) const
{
    return getAccount(index);
}

QObject *MailboxProxyModel::getAccountAndSetMailbox(int index, const QString &mbox) const
{
    Accounts::Account *account = getAccount(index);
    // Now set the mailbox
    static_cast<Imap::Mailbox::MsgListModel *>(account->msgListModel())->setMailbox(mbox);
    return account;
}

Accounts::Account *MailboxProxyModel::getAccount(int index) const
{
    QObject *mboxModel = m_mboxModels.at(index);
    Accounts::Account *account = qobject_cast<Accounts::Account *>(mboxModel->parent());
    Q_ASSERT(account);
    return account;
}

}
}