void SubscribeUnsubscribeTask::perform()
{
    parser = conn->parser;
    markAsActiveTask();

    IMAP_TASK_CHECK_ABORT_DIE;

    if (! mailboxIndex.isValid()) {
        // FIXME: add proper fix
        log("Mailbox vanished before we could ask for number of messages inside");
        _completed();
        return;
    }
    TreeItemMailbox *mailbox = dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(mailboxIndex.internalPointer()));
    Q_ASSERT(mailbox);

    switch (operation) {
    case SUBSCRIBE:
        tag = parser->subscribe(mailbox->mailbox());
        break;
    case UNSUBSCRIBE:
        tag = parser->unSubscribe(mailbox->mailbox());
        break;
    default:
        Q_ASSERT(false);
    }
}
Example #2
0
QMimeData *MsgListModel::mimeData(const QModelIndexList &indexes) const
{
    if (indexes.isEmpty())
        return 0;

    QMimeData *res = new QMimeData();
    QByteArray encodedData;
    QDataStream stream(&encodedData, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_4_6);

    TreeItemMailbox *mailbox = dynamic_cast<TreeItemMailbox *>(Model::realTreeItem(
                                   indexes.front())->parent()->parent());
    Q_ASSERT(mailbox);
    stream << mailbox->mailbox() << mailbox->syncState.uidValidity();

    QList<uint> uids;
    for (QModelIndexList::const_iterator it = indexes.begin(); it != indexes.end(); ++it) {
        TreeItemMessage *message = dynamic_cast<TreeItemMessage *>(Model::realTreeItem(*it));
        Q_ASSERT(message);
        Q_ASSERT(message->parent()->parent() == mailbox);
        Q_ASSERT(message->uid() > 0);
        uids << message->uid();
    }
    uids = uids.toSet().toList();
    stream << uids;
    res->setData(QLatin1String("application/x-trojita-message-list"), encodedData);
    return res;
}
Example #3
0
void MsgListModel::setMailbox(const QModelIndex &index)
{
    if (!index.isValid() || !index.data(Imap::Mailbox::RoleMailboxIsSelectable).toBool())
        return;

    waitingForMessages = true;

    const Model *model = 0;
    TreeItemMailbox *mbox = dynamic_cast<TreeItemMailbox *>(Model::realTreeItem(index, &model));
    Q_ASSERT(mbox);
    TreeItemMsgList *newList = dynamic_cast<TreeItemMsgList *>(
                                   mbox->child(0, const_cast<Model *>(model)));
    Q_ASSERT(newList);
    checkPersistentIndex();
    if (newList != msgListPtr) {
        msgListPtr = newList;
        msgList = msgListPtr->toIndex(const_cast<Model*>(model));
        msgListPtr->resetWasUnreadState();
        RESET_MODEL;
        emit mailboxChanged(index);
    }

    // We want to tell the Model that it should consider starting the IDLE command.
    // This shall happen regardless on what this model's idea about a "current mailbox" is because the IMAP connection might have
    // switched to another mailbox behind the scenes.
    const_cast<Model *>(model)->switchToMailbox(index);
}
bool SubscribeUnsubscribeTask::handleStateHelper(const Imap::Responses::State *const resp)
{
    if (resp->tag.isEmpty())
        return false;

    if (resp->tag == tag) {
        if (resp->kind == Responses::OK) {
            TreeItemMailbox *mailbox = dynamic_cast<TreeItemMailbox *>(model->findMailboxByName(mailboxName));
            QString subscribed = QStringLiteral("\\SUBSCRIBED");
            switch (operation) {
            case SUBSCRIBE:
                if (mailbox && !mailbox->m_metadata.flags.contains(subscribed)) {
                    mailbox->m_metadata.flags.append(subscribed);
                }
                break;
            case UNSUBSCRIBE:
                if (mailbox) {
                    mailbox->m_metadata.flags.removeOne(subscribed);
                }
            }
            if (mailbox) {
                auto index = mailbox->toIndex(model);
                emit model->dataChanged(index, index);
            }
            _completed();
        } else {
            _failed(tr("SUBSCRIBE/UNSUBSCRIBE has failed"));
            // FIXME: error handling
        }
        return true;
    } else {
        return false;
    }
}
Example #5
0
/** @short Change mailbox to the one specified by its name */
void MsgListModel::setMailbox(const QString &mailboxName)
{
    Model *model = dynamic_cast<Model*>(sourceModel());
    Q_ASSERT(model);
    TreeItemMailbox *mailboxPtr = model->findMailboxByName(mailboxName);
    if (mailboxPtr)
        setMailbox(mailboxPtr->toIndex(model));
}
QString SubscribeUnsubscribeTask::debugIdentification() const
{
    if (! mailboxIndex.isValid())
        return QLatin1String("[invalid mailboxIndex]");

    TreeItemMailbox *mailbox = dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(mailboxIndex.internalPointer()));
    Q_ASSERT(mailbox);
    return QString::fromUtf8("attached to %1").arg(mailbox->mailbox());
}
Example #7
0
QVariant MailboxModel::data(const QModelIndex &proxyIndex, int role) const
{
    if (! proxyIndex.isValid() || proxyIndex.model() != this)
        return QVariant();

    if (proxyIndex.column() != 0)
        return QVariant();

    TreeItemMailbox *mbox = dynamic_cast<TreeItemMailbox *>(
                                static_cast<TreeItem *>(proxyIndex.internalPointer())
                            );
    Q_ASSERT(mbox);
    if (role > RoleBase && role < RoleInvalidLastOne)
        return mbox->data(static_cast<Imap::Mailbox::Model *>(sourceModel()), role);
    else
        return QAbstractProxyModel::data(createIndex(proxyIndex.row(), 0, proxyIndex.internalPointer()), role);
}
Example #8
0
bool MailboxModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
                                int row, int column, const QModelIndex &parent)
{
    Q_UNUSED(row); Q_UNUSED(column);
    if (action != Qt::CopyAction && action != Qt::MoveAction)
        return false;

    if (! parent.isValid())
        return false;

    if (! static_cast<Model *>(sourceModel())->isNetworkAvailable())
        return false;

    TreeItemMailbox *target = dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(parent.internalPointer()));
    Q_ASSERT(target);

    if (! target->isSelectable())
        return false;

    QByteArray encodedData = data->data(QLatin1String("application/x-trojita-message-list"));
    QDataStream stream(&encodedData, QIODevice::ReadOnly);

    Q_ASSERT(! stream.atEnd());
    QString origMboxName;
    stream >> origMboxName;
    TreeItemMailbox *origMbox = static_cast<Model *>(sourceModel())->findMailboxByName(origMboxName);
    if (! origMbox) {
        qDebug() << "Can't find original mailbox when performing a drag&drop on messages";
        return false;
    }

    uint uidValidity;
    stream >> uidValidity;
    if (uidValidity != origMbox->syncState.uidValidity()) {
        qDebug() << "UID validity for original mailbox got changed, can't copy messages";
        return false;
    }

    Imap::Uids uids;
    stream >> uids;

    static_cast<Model *>(sourceModel())->copyMoveMessages(origMbox, target->mailbox(), uids,
            (action == Qt::MoveAction) ? MOVE : COPY);
    return true;
}
Example #9
0
Qt::ItemFlags MailboxModel::flags(const QModelIndex &index) const
{
    if (! index.isValid())
        return QAbstractProxyModel::flags(index);

    TreeItemMailbox *mbox = dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(index.internalPointer()));
    Q_ASSERT(mbox);

    Qt::ItemFlags res = QAbstractProxyModel::flags(index);
    if (!mbox->isSelectable()) {
        res &= ~Qt::ItemIsSelectable;
        res |= Qt::ItemIsEnabled;
    }
    if (static_cast<Model *>(sourceModel())->isNetworkAvailable()) {
        res |= Qt::ItemIsDropEnabled;
    }
    return res;
}
void NumberOfMessagesTask::perform()
{
    parser = conn->parser;
    markAsActiveTask();

    IMAP_TASK_CHECK_ABORT_DIE;

    if (! mailboxIndex.isValid()) {
        // FIXME: add proper fix
        log(QLatin1String("Mailbox vanished before we could ask for number of messages inside"));
        _completed();
        return;
    }
    TreeItemMailbox *mailbox = dynamic_cast<TreeItemMailbox *>(static_cast<TreeItem *>(mailboxIndex.internalPointer()));
    Q_ASSERT(mailbox);

    tag = parser->status(mailbox->mailbox(), requestedStatusOptions());
}