File indexing completed on 2024-11-24 04:53:09

0001 /*  Copyright (C) 2006 - 2016 Jan Kundrát <jkt@kde.org>
0002     Certain enhancements (www.xtuple.com/trojita-enhancements)
0003     are copyright © 2010 by OpenMFG LLC, dba xTuple.  All rights reserved.
0004 
0005     Redistribution and use in source and binary forms, with or without
0006     modification, are permitted provided that the following conditions are met:
0007 
0008     - Redistributions of source code must retain the above copyright notice, this
0009     list of conditions and the following disclaimer.
0010     - Redistributions in binary form must reproduce the above copyright notice,
0011     this list of conditions and the following disclaimer in the documentation
0012     and/or other materials provided with the distribution.
0013     - Neither the name of xTuple nor the names of its contributors may be used to
0014     endorse or promote products derived from this software without specific prior
0015     written permission.
0016 
0017     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0020     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
0021     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0022     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0023     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
0024     ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0026     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 
0028 */
0029 
0030 #include "Common/InvokeMethod.h"
0031 #include "Imap/Model/ItemRoles.h"
0032 #include "Imap/Model/MailboxFinder.h"
0033 #include "Imap/Model/Model.h"
0034 
0035 namespace Imap {
0036 
0037 namespace Mailbox {
0038 
0039 MailboxFinder::MailboxFinder(QObject *parent, QAbstractItemModel *model)
0040     : QObject(parent)
0041     , m_model(model)
0042 {
0043     Q_ASSERT(m_model);
0044     Q_ASSERT_X(!qobject_cast<Imap::Mailbox::Model *>(model),
0045                "MailboxFinder", "This requires a proxy model which exports a filtered view, not the original Model");
0046     connect(m_model, &QAbstractItemModel::layoutChanged, this, &MailboxFinder::checkArrivals);
0047     connect(m_model, &QAbstractItemModel::rowsInserted, this, &MailboxFinder::checkArrivals);
0048 }
0049 
0050 void MailboxFinder::addMailbox(const QString &mailbox)
0051 {
0052     m_pending.insert(mailbox);
0053     EMIT_LATER_NOARG(this, checkArrivals);
0054 }
0055 
0056 void MailboxFinder::checkArrivals()
0057 {
0058     Q_FOREACH(const QString &mailbox, m_pending) {
0059         QModelIndex root;
0060         bool cont = false;
0061 
0062         // Simply use the MVC API to find an interesting object
0063         do {
0064             cont = false;
0065             int rowCount = m_model->rowCount(root);
0066 
0067             for (int i = 0; i < rowCount; ++i) {
0068                 const QModelIndex index = m_model->index(i, 0, root);
0069                 const QString possibleName = m_model->data(index, Imap::Mailbox::RoleMailboxName).toString();
0070                 const QString separator = m_model->data(index, Imap::Mailbox::RoleMailboxSeparator).toString();
0071 
0072                 if (possibleName.isEmpty() && separator.isEmpty()) {
0073                     // This shouldn't really happen
0074                     qDebug() << "Skipping a mailbox with no name and no separator";
0075                     continue;
0076                 }
0077 
0078                 if (possibleName == mailbox) {
0079                     // found it
0080                     m_pending.remove(mailbox);
0081                     emit mailboxFound(mailbox, index);
0082                     break;
0083                 } else if (mailbox.startsWith(possibleName + separator)) {
0084                     // we know where to go
0085                     root = index;
0086                     cont = true;
0087                     break;
0088                 }
0089             }
0090         } while (cont);
0091     }
0092 }
0093 
0094 }
0095 }