1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2 
3    This file is part of the Trojita Qt IMAP e-mail client,
4    http://trojita.flaska.net/
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2 of
9    the License or (at your option) version 3 or any later version
10    accepted by the membership of KDE e.V. (or its successor approved
11    by the membership of KDE e.V.), which shall act as a proxy
12    defined in Section 14 of version 3 of the license.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 
24 #include "Fake_ListChildMailboxesTask.h"
25 #include "Imap/Model/Model.h"
26 #include "Imap/Model/MailboxTree.h"
27 #include "Imap/Model/TaskFactory.h"
28 #include "GetAnyConnectionTask.h"
29 
30 namespace Imap
31 {
32 namespace Mailbox
33 {
34 
35 
Fake_ListChildMailboxesTask(Model * model,const QModelIndex & mailbox)36 Fake_ListChildMailboxesTask::Fake_ListChildMailboxesTask(Model *model, const QModelIndex &mailbox):
37     ListChildMailboxesTask(model, mailbox)
38 {
39     Q_ASSERT(!mailbox.isValid() || dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(mailbox.internalPointer())));
40 }
41 
perform()42 void Fake_ListChildMailboxesTask::perform()
43 {
44     parser = conn->parser;
45     markAsActiveTask();
46 
47     IMAP_TASK_CHECK_ABORT_DIE;
48 
49     TreeItemMailbox *mailbox = dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(model->translatePtr(mailboxIndex)));
50     Q_ASSERT(mailbox);
51     parser = conn->parser;
52     QList<Responses::List> &listResponses = model->accessParser(parser).listResponses;
53     Q_ASSERT(listResponses.isEmpty());
54     TestingTaskFactory *factory = dynamic_cast<TestingTaskFactory *>(model->m_taskFactory.get());
55     Q_ASSERT(factory);
56     for (QMap<QString, QStringList>::const_iterator it = factory->fakeListChildMailboxesMap.constBegin();
57          it != factory->fakeListChildMailboxesMap.constEnd(); ++it) {
58         if (it.key() != mailbox->mailbox())
59             continue;
60         for (QStringList::const_iterator childIt = it->begin(); childIt != it->end(); ++childIt) {
61             QString childMailbox = mailbox->mailbox().isEmpty() ? *childIt : QStringLiteral("%1^%2").arg(mailbox->mailbox(), *childIt);
62             listResponses.append(Responses::List(Responses::LIST, QStringList(), QStringLiteral("^"), childMailbox, QMap<QByteArray, QVariant>()));
63         }
64     }
65     model->finalizeList(parser, mailbox);
66     _completed();
67 }
68 
handleStateHelper(const Imap::Responses::State * const resp)69 bool Fake_ListChildMailboxesTask::handleStateHelper(const Imap::Responses::State *const resp)
70 {
71     // This is a fake task inheriting from the "real one", so we have to reimplement functions which do real work with stubs
72     Q_UNUSED(resp);
73     return false;
74 }
75 
76 
77 }
78 }
79