Qt::ItemFlags DVRServersModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    Qt::ItemFlags re = Qt::ItemIsSelectable;
    DVRServer *s = 0;
    DVRCamera *camera = 0;

    if (index.internalPointer())
    {
        camera = cameraForRow(index);
        if (camera)
            s = camera->data().server();
    }
    else
        s = serverForRow(index);

    if (!m_offlineDisabled || (s && s->isOnline() && (!camera || !camera->data().disabled())))
        re |= Qt::ItemIsEnabled;
    else
        return re;

    if (!index.parent().isValid())
        re |= Qt::ItemIsEditable;
    re |= Qt::ItemIsDragEnabled;

    return re;
}
void OptionsServerPage::currentServerChanged(const QModelIndex &newIndex, const QModelIndex &oldIndex)
{
    DVRServer *server = oldIndex.data(DVRServersModel::DVRServerRole).value<DVRServer *>();
    if (server)
    {
        saveChanges(server);
        server->disconnect(this);
        m_connectionStatus->setVisible(false);
    }

    server = newIndex.data(DVRServersModel::DVRServerRole).value<DVRServer *>();
    if (!server)
    {
        m_nameEdit->clear();
        m_hostnameEdit->clear();
        m_portEdit->clear();
        m_usernameEdit->clear();
        m_passwordEdit->clear();
        m_connectionType->setCurrentIndex(DVRServerConnectionType::RTSP);
        return;
    }

    m_nameEdit->setText(server->configuration().displayName());
    m_hostnameEdit->setText(server->configuration().hostname());
    m_portEdit->setText(QString::number(server->serverPort()));
    m_usernameEdit->setText(server->configuration().username());
    m_passwordEdit->setText(server->configuration().password());
    m_autoConnect->setChecked(server->configuration().autoConnect());
    m_connectionType->setCurrentIndex(server->configuration().connectionType());

    connect(server, SIGNAL(loginSuccessful()), SLOT(setLoginSuccessful()));
    connect(server, SIGNAL(loginError(QString)), SLOT(setLoginError(QString)));
    connect(server, SIGNAL(loginRequestStarted()), SLOT(setLoginConnecting()));

    checkServer();

    if (server->isOnline())
        setLoginSuccessful();
    else if (server->isLoginPending())
        setLoginConnecting();
    else if (!server->errorMessage().isEmpty())
        setLoginError(server->errorMessage());
}
QVariant DVRServersModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    DVRServer *server = serverForRow(index);
    if (server)
    {
        if (role == Qt::ToolTipRole)
        {
            return tr("<span style='white-space:nowrap'><b>%1</b><br>%3 @ %2</span>", "tooltip")
                    .arg(Qt::escape(server->configuration().displayName()))
                    .arg(Qt::escape(server->configuration().hostname()))
                    .arg(Qt::escape(server->configuration().username()));
        }
        else if (role == DVRServerRole)
            return QVariant::fromValue(server);

        switch (index.column())
        {
        case 0:
            if (role == Qt::DisplayRole || role == Qt::EditRole)
                return server->configuration().displayName();
            else if (role == Qt::DecorationRole)
            {
                if (server->status() == DVRServer::LoginError)
                    return m_statusErrorIcon;

                if (!server->statusAlertMessage().isEmpty())
                    return m_statusAlertIcon;

                if (m_offlineDisabled)
                    return m_statusIcon;
                else
                    return m_statusIcon.pixmap(16, server->isOnline() ? QIcon::Normal : QIcon::Disabled);
            }
            break;
        case 1:
            if (role == Qt::DisplayRole || role == Qt::EditRole)
                return server->configuration().hostname();
            break;
        case 2:
            if (role == Qt::DisplayRole || role == Qt::EditRole)
                return server->configuration().username();
            break;
        }
    }

    DVRCamera *camera = cameraForRow(index);
    if (camera)
    {
        switch (role)
        {
        case DVRCameraRole:
            return QVariant::fromValue(camera);
        case Qt::DisplayRole:
            return camera->data().displayName();
        case Qt::DecorationRole:
            return QIcon(QLatin1String(":/icons/webcam.png"));
        }
    }

    return QVariant();
}