void DVRServerRepository::loadServers()
{
    Q_ASSERT(m_servers.isEmpty());

    QSettings settings;
    settings.beginGroup(QLatin1String("servers"));
    QStringList groups = settings.childGroups();

    DVRServerSettingsReader settingsReader;
    foreach (QString group, groups)
    {
        bool ok = false;
        int id = (int)group.toUInt(&ok);
        if (!ok)
        {
            qWarning("Ignoring invalid server ID from configuration");
            continue;
        }

        DVRServer *server = settingsReader.readServer(id);
        if (!server)
        {
            qWarning("Ignoring invalid server from configuration");
            continue;
        }

        server->setParent(this);
        connect(server, SIGNAL(serverRemoved(DVRServer*)), this, SLOT(onServerRemoved(DVRServer*)));
        connect(server, SIGNAL(statusAlertMessageChanged(QString)), this, SIGNAL(serverAlertsChanged()));

        m_servers.append(server);
        m_maxServerId = qMax(m_maxServerId, id);
    }
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::addNewServer()
{
    DVRServer *server = m_serverRepository->createServer(tr("New Server"));
    server->configuration().setAutoConnect(true);
    server->configuration().setPort(7001);
    server->configuration().setConnectionType(DVRServerConnectionType::RTSP);

    if (!m_serversView->currentIndex().isValid())
        saveChanges(server);
    setCurrentServer(server);

    m_nameEdit->setFocus();
    m_nameEdit->selectAll();
}
DVRServer * DVRServerRepository::createServer(const QString& name)
{
    int id = ++m_maxServerId;

    DVRServer *server = new DVRServer(id, this);
    server->configuration().setDisplayName(name);

    emit serverAboutToBeAdded(server);

    m_servers.append(server);
    connect(server, SIGNAL(serverRemoved(DVRServer*)), this, SLOT(onServerRemoved(DVRServer*)));
    connect(server, SIGNAL(statusAlertMessageChanged(QString)), this, SIGNAL(serverAlertsChanged()));

    emit serverAdded(server);
    return server;
}
void OptionsServerPage::deleteServer()
{
    DVRServer *server = m_serversView->currentIndex().data(DVRServersModel::DVRServerRole).value<DVRServer *>();

    if (!server)
        return;

    QMessageBox dlg(QMessageBox::Question, tr("Delete DVR Server"), tr("Are you sure you want to delete <b>%1</b>?")
                    .arg(Qt::escape(server->configuration().displayName())), QMessageBox::NoButton, this);
    QPushButton *delBtn = dlg.addButton(tr("Delete"), QMessageBox::DestructiveRole);
    dlg.addButton(QMessageBox::Cancel);
    dlg.exec();

    if (dlg.clickedButton() != delBtn)
        return;

    server->removeServer();
}
QMimeData *DVRServersModel::mimeData(const QModelIndexList &indexes) const
{
    QByteArray data;
    QDataStream stream(&data, QIODevice::WriteOnly);

    DVRCameraStreamWriter cameraWriter(stream);

    foreach (QModelIndex index, indexes)
    {
        DVRServer *server = serverForRow(index);
        if (server)
        {
            foreach (DVRCamera *camera, server->cameras())
                cameraWriter.writeCamera(camera);
        }
        else
        {
            DVRCamera *camera = cameraForRow(index);
            if (camera)
                cameraWriter.writeCamera(camera);
        }
    }
DVRServer * DVRServerSettingsReader::readServer(int serverId) const
{
    Q_ASSERT(serverId >= 0);

    DVRServer *server = new DVRServer(serverId);
    server->configuration().setDisplayName(readSetting(serverId, QLatin1String("displayName")).toString());
    server->configuration().setHostname(readSetting(serverId, QLatin1String("hostname")).toString());
    server->configuration().setPort(readSetting(serverId, QLatin1String("port")).toInt());
    server->configuration().setUsername(readSetting(serverId, QLatin1String("username")).toString());
    server->configuration().setPassword(readSetting(serverId, QLatin1String("password")).toString());
    server->configuration().setAutoConnect(readSetting(serverId, QLatin1String("autoConnect"), true).toBool());
    server->configuration().setSslDigest(readSetting(serverId, QLatin1String("sslDigest")).toByteArray());
    server->configuration().setConnectionType(readSetting(serverId, QLatin1String("connectionType"), DVRServerConnectionType::RTSP).toInt());

    return server;
}
bool DVRServersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || role != Qt::EditRole)
        return false;

    DVRServer *server = serverForRow(index);
    if (!server)
        return false;

    switch (index.column())
    {
    case 0:
        {
            QString name = value.toString().trimmed();
            if (name.isEmpty())
                return false;

            /* dataChanged will be emitted in response to the DVRServer::changed() signal */
            server->configuration().setDisplayName(name);
        }
        break;
    case 1:
        server->configuration().setHostname(value.toString());
        break;
    case 2:
        server->configuration().setUsername(value.toString());
        break;
    case 3:
        server->configuration().setPassword(value.toString());
        break;
    default:
        return false;
    }

    return true;
}
Exemple #9
0
void BluecherryApp::addLocalServer()
{
#ifdef Q_OS_LINUX
    DVRServer *s = m_serverRepository->createServer(tr("Local"));
    s->configuration().setHostname(QHostAddress(QHostAddress::LocalHost).toString());
    /* This must match the default username and password for the server */
    s->configuration().setUsername(QLatin1String("Admin"));
    s->configuration().setPassword(QLatin1String("bluecherry"));
    s->configuration().setPort(7001);
    s->configuration().setAutoConnect(true);
#endif
}
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();
}
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());
}