summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/snmp/monitordialog.cpp
blob: 1bda0aa59abdee96df6d8d8d9d517fc8d05fd202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* This file is part of the KDE project
   Copyright (C) 2003 Simon Hausmann <hausmann@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "monitordialog.h"
#include "identifier.h"
#include "browsedialog.h"

#include <tqcombobox.h>
#include <tqlineedit.h>
#include <tqspinbox.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>
#include <tqcheckbox.h>
#include <tqlabel.h>

#include <klocale.h>
#include <kpushbutton.h>

#include <assert.h>

using namespace KSim::Snmp;

MonitorDialog::MonitorDialog( const HostConfigMap &hosts, TQWidget *parent, const char *name )
    : MonitorDialogBase( parent, name ), m_hosts( hosts )
{
    init();
}

MonitorDialog::MonitorDialog( const MonitorConfig &monitor, const HostConfigMap &hosts, TQWidget *parent, const char *name )
    : MonitorDialogBase( parent, name ), m_hosts( hosts )
{
    init( monitor );
}

MonitorConfig MonitorDialog::monitorConfig() const
{
    MonitorConfig result;

    result.host = currentHost();
    if ( result.host.isNull() )
        return MonitorConfig();

    result.name = monitorName->text();
    if ( result.name.isEmpty() )
        return MonitorConfig();

    result.oid = oid->text();
    if ( Identifier::fromString( result.oid ).isNull() )
        return MonitorConfig();

    result.refreshInterval.minutes = updateIntervalMinutes->value();
    result.refreshInterval.seconds = updateIntervalSeconds->value();

    result.display = stringToMonitorDisplayType( displayType->currentText() );

    if ( result.display == MonitorConfig::Label ) {
        result.useCustomFormatString = customFormatStringCheckBox->isChecked();
        if ( result.useCustomFormatString )
            result.customFormatString = customFormatString->text();
    } else
        result.displayCurrentValueInline = displayCurrentValueInline->isChecked();

    return result;
}

void MonitorDialog::checkValues()
{
    bool currentDisplayTypeIsLabel = stringToMonitorDisplayType( displayType->currentText() ) == MonitorConfig::Label;
    labelOptions->setEnabled( currentDisplayTypeIsLabel );
    chartOptions->setEnabled( !currentDisplayTypeIsLabel );

    TQString statusText;

    if ( monitorName->text().isEmpty() )
        statusText = i18n( "Please enter a name for this monitor" );

    if ( statusText.isEmpty() && Identifier::fromString( oid->text() ).isNull() )
        statusText = i18n( "Please enter a valid name for the object identifier" );

    if ( statusText.isEmpty() )
        statusText = " "; // a space to keep the height

    status->setText( statusText );

    buttonOk->setEnabled( !monitorConfig().isNull() );
}

void MonitorDialog::testObject()
{
    // ### FIXME
}

void MonitorDialog::browse()
{
    HostConfig host = currentHost();
    if ( host.isNull() )
        return;

    BrowseDialog dlg( host, oid->text(), this );
    if ( dlg.exec() )
        oid->setText( dlg.selectedObjectIdentifier() );
}

void MonitorDialog::init( const MonitorConfig &monitor )
{
    customFormatString->setText( "%s" );

    status->setText( " " );

    host->clear();

    TQStringList allHosts;
    for ( HostConfigMap::ConstIterator it = m_hosts.begin(); it != m_hosts.end(); ++it )
        allHosts << it.key();

    host->insertStringList( allHosts );

    displayType->clear();
    displayType->insertStringList( allDisplayTypes() );

    if ( monitor.isNull() ) {
        checkValues();
        return;
    }

    monitorName->setText( monitor.name );

    assert( allHosts.findIndex( monitor.host.name ) != -1 );

    host->setCurrentItem( allHosts.findIndex( monitor.host.name ) );

    oid->setText( monitor.oid );

    updateIntervalMinutes->setValue( monitor.refreshInterval.minutes );
    updateIntervalSeconds->setValue( monitor.refreshInterval.seconds );

    displayType->setCurrentItem( allDisplayTypes().findIndex( monitorDisplayTypeToString( monitor.display ) ) );

    customFormatStringCheckBox->setChecked( monitor.useCustomFormatString );
    customFormatString->setEnabled( monitor.useCustomFormatString );

    if ( monitor.useCustomFormatString )
        customFormatString->setText( monitor.customFormatString );

    displayCurrentValueInline->setChecked( monitor.displayCurrentValueInline );

    checkValues();
}

HostConfig MonitorDialog::currentHost() const
{
    HostConfigMap::ConstIterator hostIt = m_hosts.find( host->currentText() );
    if ( hostIt == m_hosts.end() )
        return HostConfig();

    return *hostIt;
}

#include "monitordialog.moc"
/* vim: et sw=4 ts=4
 */