summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/snmp/monitorconfig.cpp
blob: d2dee456d876fbd638f794afd7881707c7b86c50 (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
/* This file is part of the KDE project
   Copyright (C) 2003 Simon Hausmann <[email protected]>

   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 "monitorconfig.h"
#include "labelmonitor.h"
#include "chartmonitor.h"

#include <assert.h>

using namespace KSim::Snmp;

MonitorConfig::MonitorConfig()
{
    display = Label;
    refreshInterval.minutes = refreshInterval.seconds = 0;
    useCustomFormatString = false;
    displayCurrentValueInline = false;
}

bool MonitorConfig::load( TDEConfigBase &config, const HostConfigMap &hosts )
{
    TQString hostName = config.readEntry( "Host" );
    if ( hostName.isEmpty() )
        return false;

    HostConfigMap::ConstIterator hostIt = hosts.find( hostName );
    if ( hostIt == hosts.end() )
        return false;

    host = *hostIt;

    name = config.readEntry( "MonitorName" );
    if ( name.isEmpty() )
        return false;

    oid = config.readEntry( "ObjectIdentifier" );
    if ( Identifier::fromString( oid ).isNull() )
        return false;

    bool ok = false;
    display = stringToMonitorDisplayType( config.readEntry( "DisplayType" ), &ok );
    if ( !ok )
        return false;

    refreshInterval.minutes = config.readUnsignedNumEntry( "RefreshIntervalMinutes" );
    refreshInterval.seconds = config.readUnsignedNumEntry( "RefreshIntervalSeconds" );

    if ( refreshInterval.minutes == 0 && refreshInterval.seconds == 0 )
        return false;

    if ( display == Label ) {
        useCustomFormatString = config.readBoolEntry( "UseCustomFormatString", useCustomFormatString );
        if ( useCustomFormatString )
            customFormatString = config.readEntry( "CustomFormatString" );
    } else
        displayCurrentValueInline = config.readBoolEntry( "DisplayCurrentValueInline", displayCurrentValueInline );

    return true;
}

void MonitorConfig::save( TDEConfigBase &config ) const
{
    if ( isNull() )
        return;

    config.writeEntry( "Host", host.name );
    config.writeEntry( "MonitorName", name );
    config.writeEntry( "ObjectIdentifier", oid );
    config.writeEntry( "DisplayType", monitorDisplayTypeToString( display ) );
    config.writeEntry( "RefreshIntervalMinutes", refreshInterval.minutes );
    config.writeEntry( "RefreshIntervalSeconds", refreshInterval.seconds );
    if ( display == Label ) {
        config.writeEntry( "UseCustomFormatString", useCustomFormatString );
        if ( useCustomFormatString )
            config.writeEntry( "CustomFormatString", customFormatString );
    } else
        config.writeEntry( "DisplayCurrentValueInline", displayCurrentValueInline );
}

TQWidget *MonitorConfig::createMonitorWidget( TQWidget *parent, const char *name )
{
    TQWidget *w;

    int refresh = refreshInterval.seconds * 1000 + refreshInterval.minutes * 60 * 1000;
    Identifier id = Identifier::fromString( oid );
    if ( id.isNull() )
        return 0;

    if ( display == Label )
        w = new LabelMonitor( *this, parent, name );
    else
        w = new ChartMonitor( *this, parent, name );

    Monitor *monitor = new Monitor( host, id, refresh, TQT_TQOBJECT(w) );
    TQObject::connect( monitor, TQT_SIGNAL( newData( const Value & ) ),
                      w, TQT_SLOT( setData( const Value & ) ) );
    return w;
}

TQString KSim::Snmp::monitorDisplayTypeToString( MonitorConfig::DisplayType type )
{
    switch ( type )
    {
        case MonitorConfig::Label: return TQString::fromLatin1( "Label" );
        case MonitorConfig::Chart: return TQString::fromLatin1( "Chart" );
        default: assert( false );
    };
    return TQString();
}

MonitorConfig::DisplayType KSim::Snmp::stringToMonitorDisplayType( TQString string, bool *ok )
{
    string = string.lower();
    if ( string == "chart" ) {
        if ( ok )
            *ok = true;
        return MonitorConfig::Chart;
    }
    if ( string == "label" ) {
        if ( ok )
            *ok = true;
        return MonitorConfig::Label;
    }
    if ( ok )
        *ok = false;
    return MonitorConfig::Chart;
}

TQStringList KSim::Snmp::allDisplayTypes()
{
    // !!! keep order with enum
    return TQStringList() << "Label" << "Chart";
}

void MonitorConfigMap::load( TDEConfigBase &config, const TQStringList &names, const HostConfigMap &hosts )
{
    clear();

    for ( TQStringList::ConstIterator it = names.begin(); it != names.end(); ++it ) {
        config.setGroup( "Monitor " + *it );

        MonitorConfig monitor;
        if ( !monitor.load( config, hosts ) )
            continue;

        insert( *it, monitor );
    }
}

TQStringList MonitorConfigMap::save( TDEConfigBase &config ) const
{
    TQStringList names;

    for ( ConstIterator it = begin(); it != end(); ++it ) {
        TQString name = it.key();

        names << name;

        config.setGroup( "Monitor " + name );
        ( *it ).save( config );
    }

    return names;
}

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