summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/snmp/hostdialog.cpp
blob: a1ccdc37369d3c3ce6e5b2294b87e31187340bad (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* 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 "hostdialog.h"
#include "probedialog.h"
#include "proberesultdialog.h"

#include <qcombobox.h>
#include <qwidgetstack.h>
#include <klineedit.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qspinbox.h>

#include <klocale.h>
#include <kmessagebox.h>

#include <assert.h>

using namespace KSim::Snmp;

HostDialog::HostDialog( QWidget *parent, const char *name )
    : HostDialogBase( parent, name )
{
    init();
    port->setValue( 161 );
}

HostDialog::HostDialog( const HostConfig &src, QWidget *parent, const char *name )
    : HostDialogBase( parent, name )
{
    init( src );
}

HostConfig HostDialog::settings() const
{
    HostConfig result;

    result.name = hostName->text();
    result.port = port->value();

    result.version = stringToSnmpVersion( snmpVersion->currentText() );
    if ( result.version != SnmpVersion3 ) {
        result.community = communityString->text();
    }

    result.securityName = securityName->text();

    result.securityLevel = stringToSecurityLevel( securityLevel->currentText() );

    if ( result.securityLevel == NoAuthPriv )
        return result;

    result.authentication.protocol = stringToAuthenticationProtocol( authenticationType->currentText() );
    result.authentication.key = authenticationPassphrase->text();

    if ( result.securityLevel == AuthNoPriv )
        return result;

    result.privacy.protocol = stringToPrivacyProtocol( privacyType->currentText() );
    result.privacy.key = privacyPassphrase->text();

    return result;
}

void HostDialog::showSnmpAuthenticationDetailsForVersion( const QString &versionStr )
{
    bool ok = false;
    SnmpVersion version = stringToSnmpVersion( versionStr, &ok );
    assert( ok );

    authenticationDetails->raiseWidget( version == SnmpVersion3 ? snmpV3Page : snmpV1Page );
}

void HostDialog::enableDisabledAuthenticationAndPrivacyElementsForSecurityLevel( const QString &levelStr )
{
    bool ok = false;
    SecurityLevel level = stringToSecurityLevel( levelStr, &ok );
    assert( ok );

    bool enableAuthentication = level != NoAuthPriv;
    bool enablePrivacy = level == AuthPriv;

    authenticationTypeLabel->setEnabled( enableAuthentication );
    authenticationType->setEnabled( enableAuthentication );
    authenticationPassphraseLabel->setEnabled( enableAuthentication );
    authenticationPassphrase->setEnabled( enableAuthentication );

    privacyTypeLabel->setEnabled( enablePrivacy );
    privacyType->setEnabled( enablePrivacy );
    privacyPassphraseLabel->setEnabled( enablePrivacy );
    privacyPassphrase->setEnabled( enablePrivacy );
}

void HostDialog::checkValidity()
{
    bool enableOk = true;
    bool ok = false;
    SnmpVersion version = stringToSnmpVersion( snmpVersion->currentText(), &ok );
    assert( ok );

    enableOk &= !hostName->text().isEmpty();

    if ( version == SnmpVersion3 )
        enableOk &= !securityName->text().isEmpty();
    else
        enableOk &= !communityString->text().isEmpty();

    buttonOk->setEnabled( enableOk );
    testHostButton->setEnabled( enableOk );
}

void HostDialog::testHost()
{
    ProbeDialog dlg( settings(), this );
    if ( dlg.exec() ) {
        ProbeDialog::ProbeResultList results = dlg.probeResults();

        ProbeResultDialog resultDlg( settings(), results, this );
        resultDlg.exec();
    }
}

void HostDialog::init( const HostConfig &src )
{
    // hide these, there's nothing to choose right now. might be that
    // net-snmp will support different privacy types in the future, but
    // apparently not now.
    privacyTypeLabel->hide();
    privacyType->hide();

    snmpVersion->insertStringList( allSnmpVersions() );
    securityLevel->insertStringList( allSecurityLevels() );
    authenticationType->insertStringList( allAuthenticationProtocols() );
    privacyType->insertStringList( allPrivacyProtocols() );

    if ( !src.isNull() )
        loadSettingsFromHostConfig( src );

    showSnmpAuthenticationDetailsForVersion( snmpVersion->currentText() );
    enableDisabledAuthenticationAndPrivacyElementsForSecurityLevel( securityLevel->currentText() );
    checkValidity();
}

void HostDialog::loadSettingsFromHostConfig( const HostConfig &src )
{
    hostName->setText( src.name );
    port->setValue( src.port );

    snmpVersion->setCurrentItem( allSnmpVersions().findIndex( snmpVersionToString( src.version ) ) );

    if ( src.version != SnmpVersion3 ) {
        communityString->setText( src.community );
        return;
    }

    securityName->setText( src.securityName );

    securityLevel->setCurrentItem( allSecurityLevels().findIndex( securityLevelToString( src.securityLevel ) ) );

    if ( src.securityLevel == NoAuthPriv )
        return;

    authenticationType->setCurrentItem( allAuthenticationProtocols().findIndex( authenticationProtocolToString( src.authentication.protocol ) ) );
    authenticationPassphrase->setText( src.authentication.key );

    if ( src.securityLevel == AuthNoPriv )
        return;

    privacyType->setCurrentItem( allPrivacyProtocols().findIndex( privacyProtocolToString( src.privacy.protocol ) ) );
    privacyPassphrase->setText( src.privacy.key );
}

#include "hostdialog.moc"

/* vim: et sw=4 ts=4
 */