summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/net
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commit2bda8f7717adf28da4af0d34fb82f63d2868c31d (patch)
tree8d927b7b47a90c4adb646482a52613f58acd6f8c /ksim/monitors/net
downloadtdeutils-2bda8f7717adf28da4af0d34fb82f63d2868c31d.tar.gz
tdeutils-2bda8f7717adf28da4af0d34fb82f63d2868c31d.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeutils@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'ksim/monitors/net')
-rw-r--r--ksim/monitors/net/Makefile.am14
-rw-r--r--ksim/monitors/net/Net.desktop112
-rw-r--r--ksim/monitors/net/ksimnet.cpp672
-rw-r--r--ksim/monitors/net/ksimnet.h90
-rw-r--r--ksim/monitors/net/netconfig.cpp296
-rw-r--r--ksim/monitors/net/netconfig.h67
-rw-r--r--ksim/monitors/net/netdevices.h218
-rw-r--r--ksim/monitors/net/netdialog.cpp328
-rw-r--r--ksim/monitors/net/netdialog.h85
9 files changed, 1882 insertions, 0 deletions
diff --git a/ksim/monitors/net/Makefile.am b/ksim/monitors/net/Makefile.am
new file mode 100644
index 0000000..291eb71
--- /dev/null
+++ b/ksim/monitors/net/Makefile.am
@@ -0,0 +1,14 @@
+kde_module_LTLIBRARIES = ksim_net.la
+ksim_net_la_SOURCES = ksimnet.cpp netdialog.cpp netconfig.cpp
+
+ksim_net_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN)
+ksim_net_la_LIBADD = ../../library/libksimcore.la $(LIB_KIO)
+
+EXTRA_DIST = Net.desktop
+
+INCLUDES= -I$(top_srcdir)/ksim/library $(all_includes)
+
+METASOURCES = AUTO
+
+mon_DATA = Net.desktop
+mondir = $(kde_datadir)/ksim/monitors
diff --git a/ksim/monitors/net/Net.desktop b/ksim/monitors/net/Net.desktop
new file mode 100644
index 0000000..2fa6b87
--- /dev/null
+++ b/ksim/monitors/net/Net.desktop
@@ -0,0 +1,112 @@
+[Desktop Entry]
+Comment=Net Status Monitor
+Comment[ar]=مراقب حالة الشبكة
+Comment[bg]=Мониторинг на мрежата
+Comment[bs]=Nadzor statusa mreže
+Comment[ca]=Monitor de l'estat de xarxa
+Comment[cs]=Monitor stavu sítě
+Comment[cy]=Monitr Statws Rhwyd
+Comment[da]=Overvågning af netstatus
+Comment[de]=Statusanzeige fürs Netzwerk
+Comment[el]=Επόπτης κατάστασης δικτύου
+Comment[eo]=Rigardilo por la retstato
+Comment[es]=Monitor de estado de la red
+Comment[et]=Võrgu oleku monitooring
+Comment[eu]=Sarearen Egoera Begiralea
+Comment[fa]=Net نمایشگر وضعیت
+Comment[fi]=Verkon tilavalvonta
+Comment[fr]=Indicateur d'état réseau
+Comment[ga]=Monatóir Stádais an Ghréasáin
+Comment[he]=צג מצב רשת
+Comment[hi]=नेट स्थिति मॉनीटर
+Comment[hu]=Hálózati állapotfigyelő
+Comment[is]=Eftirlit með stöðu netsins
+Comment[it]=Controllo dello stato della rete
+Comment[ja]=ネットワーク状態モニタ
+Comment[ka]=ქსელის სტატუსის სტატუსი
+Comment[kk]=Желінің күйін бақылау
+Comment[km]=កម្មវិធី​ត្រួតពិនិត្យ​ស្ថានភាព​បណ្តាញ
+Comment[lt]=Net būsenos stebėtojas
+Comment[mk]=Монитор за статусот на мрежата
+Comment[ms]=Pemerhati Status Net
+Comment[nb]=Overvåkning av nettstatus
+Comment[nds]=Statuskieker för't Nettwark
+Comment[ne]=सञ्जाल स्थिति मोनिटर
+Comment[nl]=Netstatus-monitor
+Comment[nn]=Overvaking av nettstatus
+Comment[pa]=ਨੈੱਟ ਹਾਲਤ ਨਿਗਰਾਨ
+Comment[pl]=Ruch w sieci
+Comment[pt]=Monitor de Estado da Rede
+Comment[pt_BR]=Monitor de estado da rede
+Comment[ro]=Monitor stare reţea
+Comment[ru]=Монитор сети
+Comment[sk]=Monitor stavu siete
+Comment[sl]=Nadzornik stanja mreže
+Comment[sr]=Надгледање статуса мреже
+Comment[sr@Latn]=Nadgledanje statusa mreže
+Comment[sv]=Övervakar nätverkets status
+Comment[ta]=வலை நிலை கண்காணி
+Comment[tg]=Дидабони Ҳолати Шабака
+Comment[th]=เครื่องมือสอดส่องสถานะของเน็ต
+Comment[tr]=Ağ İçin Durum İzleyici
+Comment[uk]=Монітор стану мережі
+Comment[uz]=Tarmoqni nazorat qilish plagini
+Comment[uz@cyrillic]=Тармоқни назорат қилиш плагини
+Comment[wa]=Corwaitoe di l' estat del rantoele
+Comment[zh_CN]=网络状态监视器
+Comment[zh_TW]=網路狀態監視器
+Comment[zu]=Umlawuli Wezinga Lenethi
+Icon=network
+Name=Net
+Name[ar]=الشبكة
+Name[bg]=Мрежа
+Name[br]=Rouedad
+Name[ca]=Xarxa
+Name[cs]=Síť
+Name[cy]=Rhwyd
+Name[el]=Δίκτυο
+Name[eo]=Reto
+Name[es]=Red
+Name[ga]=Gréasán
+Name[he]=רשת
+Name[hi]=नेट
+Name[is]=Netið
+Name[it]=Rete
+Name[ja]=ネット
+Name[kk]=Желі
+Name[km]=បណ្តាញ
+Name[lv]=Tīkls
+Name[mk]=Мрежа
+Name[nb]=Nett
+Name[nds]=Nett
+Name[ne]=सञ्जाल
+Name[nn]=Nett
+Name[pa]=ਨੈੱਟ
+Name[pl]=Sieć
+Name[pt]=Rede
+Name[pt_BR]=Rede
+Name[ro]=Reţea
+Name[ru]=Сеть
+Name[sk]=Sieť
+Name[sl]=Mreža
+Name[sr]=Мрежа
+Name[sr@Latn]=Mreža
+Name[sv]=Nätverk
+Name[ta]= வலை
+Name[tg]=Шабака
+Name[th]=เน็ต
+Name[tr]=ağ
+Name[uk]=Мережа
+Name[uz]=Tarmoq
+Name[uz@cyrillic]=Тармоқ
+Name[wa]=Rantoele
+Name[xh]=Umnatha
+Name[zh_CN]=网络
+Name[zh_TW]=網路
+Name[zu]=Inethi
+X-KSIM-VERSION=0.1
+X-KSIM-LIBRARY=net
+X-KSIM-COPYRIGHT=(C) 2001 Robbie Ward
+X-KSIM-EMAIL=linuxphreak@gmx.co.uk
+X-KSIM-PREFS=Network
+X-KSIM-AUTHOR=Robbie Ward
diff --git a/ksim/monitors/net/ksimnet.cpp b/ksim/monitors/net/ksimnet.cpp
new file mode 100644
index 0000000..45f86c2
--- /dev/null
+++ b/ksim/monitors/net/ksimnet.cpp
@@ -0,0 +1,672 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <time.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
+
+static int mib[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
+#endif
+
+#include <qpushbutton.h>
+#include <qtextstream.h>
+#include <qfile.h>
+#include <qdatetime.h>
+#include <qlayout.h>
+#include <qtimer.h>
+#include <qregexp.h>
+#include <qcursor.h>
+#include <qpopupmenu.h>
+
+#include <kdebug.h>
+#include <klocale.h>
+#include <kaboutapplication.h>
+#include <kaboutdata.h>
+#include <ksimpleconfig.h>
+#include <kglobal.h>
+#include <krun.h>
+#include <kapplication.h>
+#include <kiconloader.h>
+
+#include "ksimnet.h"
+#include "netconfig.h"
+#include <themetypes.h>
+
+#define NET_UPDATE 1000
+#define LED_UPDATE 125
+
+KSIM_INIT_PLUGIN(NetPlugin)
+
+NetPlugin::NetPlugin(const char *name)
+ : KSim::PluginObject(name)
+{
+ setConfigFileName(instanceName());
+}
+
+NetPlugin::~NetPlugin()
+{
+}
+
+KSim::PluginView *NetPlugin::createView(const char *className)
+{
+ return new NetView(this, className);
+}
+
+KSim::PluginPage *NetPlugin::createConfigPage(const char *className)
+{
+ return new NetConfig(this, className);
+}
+
+void NetPlugin::showAbout()
+{
+ QString version = kapp->aboutData()->version();
+
+ KAboutData aboutData(instanceName(),
+ I18N_NOOP("KSim Net Plugin"), version.latin1(),
+ I18N_NOOP("A net plugin for KSim"),
+ KAboutData::License_GPL, "(C) 2001 Robbie Ward");
+
+ aboutData.addAuthor("Robbie Ward", I18N_NOOP("Author"),
+ "linuxphreak@gmx.co.uk");
+ aboutData.addAuthor("Heitham Omar", I18N_NOOP("FreeBSD ports"),
+ "super_ice@ntlworld.com");
+
+ KAboutApplication(&aboutData).exec();
+}
+
+NetView::NetView(KSim::PluginObject *parent, const char *name)
+ : KSim::PluginView(parent, name)
+{
+#ifdef __linux__
+ m_procStream = 0L;
+ if ((m_procFile = fopen("/proc/net/dev", "r")))
+ m_procStream = new QTextStream(m_procFile, IO_ReadOnly);
+#endif
+#ifdef __FreeBSD__
+ m_buf = 0;
+ m_allocSize = 0;
+#endif
+
+ m_firstTime = true;
+ m_netLayout = new QVBoxLayout(this);
+
+ m_networkList = createList();
+ addDisplay();
+
+ m_netTimer = new QTimer(this);
+ connect(m_netTimer, SIGNAL(timeout()), SLOT(updateGraph()));
+ m_netTimer->start(NET_UPDATE);
+
+ m_lightTimer = new QTimer(this);
+ connect(m_lightTimer, SIGNAL(timeout()), SLOT(updateLights()));
+ m_lightTimer->start(LED_UPDATE);
+
+ updateGraph();
+}
+
+NetView::~NetView()
+{
+#ifdef __linux__
+ delete m_procStream;
+
+ if (m_procFile)
+ fclose(m_procFile);
+#endif
+
+ cleanup();
+}
+
+void NetView::reparseConfig()
+{
+ Network::List networkList = createList();
+ if ( networkList == m_networkList )
+ return;
+
+ m_netTimer->stop();
+ m_lightTimer->stop();
+ m_firstTime = true;
+
+ cleanup();
+
+ m_networkList = networkList;
+ addDisplay();
+
+ m_netTimer->start(NET_UPDATE);
+ m_lightTimer->start(LED_UPDATE);
+}
+
+void NetView::cleanup()
+{
+ Network::List::Iterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ ( *it ).cleanup();
+ }
+
+ m_networkList.clear();
+}
+
+void NetView::addDisplay()
+{
+ int i = 0;
+
+ Network::List::Iterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ KSim::LedLabel *led = addLedLabel( ( *it ).name() );
+ KSim::Label *label = ( ( *it ).showTimer() ? addLabel() : 0L );
+ QPopupMenu * popup = ( ( *it ).commandsEnabled() ?
+ addPopupMenu( ( *it ).name(), i ) : 0L );
+ KSim::Chart *chart = addChart();
+ //KSim::LedLabel *led = addLedLabel( ( *it ).name() );
+ //KSim::Label *label = ( ( *it ).showTimer() ? addLabel() : 0L );
+ //QPopupMenu * popup = ( ( *it ).commandsEnabled() ?
+ //addPopupMenu( ( *it ).name(), i ) : 0L );
+
+ if ( ( *it ).commandsEnabled() )
+ {
+ if ( chart )
+ {
+ chart->installEventFilter( this );
+ }
+
+ if ( led )
+ {
+ led->installEventFilter( this );
+ }
+
+ if ( label )
+ {
+ label->installEventFilter( this );
+ }
+ }
+
+ ( *it ).setDisplay( chart, led, label, popup );
+ ++i;
+ }
+}
+
+// Run the connect command
+void NetView::runConnectCommand( int value )
+{
+ int i = 0;
+ Network::List::ConstIterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( value == i )
+ {
+ // I use KRun here as it provides startup notification
+ if ( !( *it ).connectCommand().isNull() )
+ {
+ KRun::runCommand( ( *it ).connectCommand() );
+ }
+
+ break;
+ }
+ ++i;
+ }
+}
+
+// Run the disconnect command
+void NetView::runDisconnectCommand( int value )
+{
+ int i = 0;
+ Network::List::ConstIterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( value == i )
+ {
+ // I use KRun here as it provides startup notification
+ if ( !( *it ).disconnectCommand().isNull() )
+ {
+ KRun::runCommand( ( *it ).disconnectCommand() );
+ }
+
+ break;
+ }
+
+ ++i;
+ }
+}
+
+Network::List NetView::createList() const
+{
+ config()->setGroup( "Net" );
+ int amount = config()->readNumEntry( "deviceAmount", 0 );
+
+ Network::List list;
+ for ( int i = 0; i < amount; ++i )
+ {
+ if ( !config()->hasGroup( "device-" + QString::number( i ) ) )
+ {
+ continue;
+ }
+
+ config()->setGroup( "device-" + QString::number( i ) );
+
+ list.append( Network( config()->readEntry( "deviceName" ),
+ config()->readEntry( "deviceFormat" ),
+ config()->readBoolEntry( "showTimer" ),
+ config()->readBoolEntry( "commands" ),
+ config()->readEntry( "cCommand" ),
+ config()->readEntry("dCommand") ) );
+ }
+
+ qHeapSort( list );
+ return list;
+}
+
+void NetView::updateLights()
+{
+ Network::List::Iterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( isOnline( ( *it ).name() ) )
+ {
+ unsigned long receiveDiff = ( *it ).data().in - ( *it ).oldData().in;
+ unsigned long sendDiff = ( *it ).data().out - ( *it ).oldData().out;
+ unsigned long halfMax = ( *it ).maxValue() / 2;
+
+ ( *it ).led()->setMaxValue( ( *it ).maxValue() / 1024 );
+ ( *it ).led()->setValue( receiveDiff / 1024 );
+
+ if ( receiveDiff == 0 )
+ {
+ ( *it ).led()->setOff( KSim::Led::First );
+ }
+ else if ( ( receiveDiff / 1024 ) >= halfMax )
+ {
+ ( *it ).led()->setOn( KSim::Led::First );
+ }
+ else
+ {
+ ( *it ).led()->toggle( KSim::Led::First );
+ }
+
+ if ( sendDiff == 0 )
+ {
+ ( *it ).led()->setOff( KSim::Led::Second );
+ }
+ else if ( ( sendDiff / 1024 ) >= halfMax )
+ {
+ ( *it ).led()->setOn( KSim::Led::Second );
+ }
+ else
+ {
+ ( *it ).led()->toggle( KSim::Led::Second );
+ }
+ }
+ else
+ {
+ ( *it ).led()->setMaxValue( 0 );
+ ( *it ).led()->setValue( 0 );
+ ( *it ).led()->setOff( KSim::Led::First );
+ ( *it ).led()->setOff( KSim::Led::Second );
+ }
+ }
+}
+
+void NetView::updateGraph()
+{
+ int timer = 0;
+ int hours = 0;
+ int minutes = 0;
+ int seconds = 0;
+
+ time_t start = 0;
+ struct stat st;
+
+ QTime netTime;
+ QString timeDisplay;
+ QString pid( "/var/run/%1.pid" );
+ QString newPid;
+
+ Network::List::Iterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( isOnline( ( *it ).name() ) )
+ {
+ NetData data;
+
+ if ( ( *it ).label() )
+ {
+ timeDisplay = ( *it ).format();
+ newPid = pid.arg( ( *it ).name() );
+
+ if ( QFile::exists( newPid ) && stat( QFile::encodeName( newPid ).data(), &st ) == 0 )
+ {
+ start = st.st_mtime;
+
+ timer = static_cast<int>( difftime( time( 0 ), start ) );
+ hours = timer / 3600;
+ minutes = (timer - hours * 3600) / 60;
+ seconds = timer % 60;
+ if ( netTime.isValid( hours, minutes, seconds ) )
+ netTime.setHMS( hours, minutes, seconds );
+ }
+
+ // Keep backwards compat for now
+ if ( timeDisplay.contains( '%' ) > 0 )
+ timeDisplay.replace( '%', "" );
+
+ ( *it ).label()->setText( netTime.toString( timeDisplay ) );
+ }
+
+ netStatistics( ( *it ).name(), data );
+ ( *it ).setData( data );
+
+ unsigned long receiveDiff = data.in - ( *it ).oldData().in;
+ unsigned long sendDiff = data.out - ( *it ).oldData().out;
+
+ if ( m_firstTime )
+ {
+ receiveDiff = sendDiff = 0;
+ }
+
+ ( *it ).chart()->setValue( receiveDiff, sendDiff );
+ ( *it ).setMaxValue( ( *it ).chart()->maxValue() );
+
+ QString receiveString = KGlobal::locale()->formatNumber( ( float ) receiveDiff / 1024.0, 1 );
+ QString sendString = KGlobal::locale()->formatNumber( ( float ) sendDiff / 1024.0, 1 );
+
+ ( *it ).chart()->setText( i18n( "in: %1k" ).arg( receiveString ),
+ i18n( "out: %1k" ).arg( sendString ) );
+ }
+ else
+ {
+ ( *it ).setData( NetData() );
+ ( *it ).chart()->setValue( 0, 0 );
+
+ ( *it ).chart()->setText( i18n( "in: %1k" ).arg( KGlobal::locale()->formatNumber( 0.0, 1 ) ),
+ i18n( "out: %1k" ).arg( KGlobal::locale()->formatNumber( 0.0, 1 ) ) );
+
+ if ( ( *it ).label() )
+ ( *it ).label()->setText( i18n( "offline" ) );
+ }
+ }
+
+ if ( m_firstTime )
+ m_firstTime = false;
+}
+
+KSim::Chart *NetView::addChart()
+{
+ KSim::Chart *chart = new KSim::Chart(false, 0, this);
+ m_netLayout->addWidget(chart);
+ chart->show();
+ return chart;
+}
+
+KSim::LedLabel *NetView::addLedLabel(const QString &device)
+{
+ KSim::LedLabel *ledLabel = new KSim::LedLabel(0, KSim::Types::Net,
+ device, this);
+
+ ledLabel->show();
+ m_netLayout->addWidget(ledLabel);
+ return ledLabel;
+}
+
+KSim::Label *NetView::addLabel()
+{
+ KSim::Label *label = new KSim::Label(KSim::Types::None, this);
+ label->show();
+ m_netLayout->addWidget(label);
+ return label;
+}
+
+QPopupMenu *NetView::addPopupMenu(const QString &device, int value)
+{
+ QPopupMenu *popup = new QPopupMenu(this);
+ popup->insertItem(SmallIcon("network"), i18n("Connect"), this,
+ SLOT(runConnectCommand(int)), 0, 1);
+ popup->setItemParameter(1, value);
+ popup->insertItem(SmallIcon("network"), i18n("Disconnect"), this,
+ SLOT(runDisconnectCommand(int)), 0, 2);
+ popup->setItemParameter(2, value);
+ menu()->insertItem(device, popup, 100 + value);
+ return popup;
+}
+
+void NetView::netStatistics(const QString &device, NetData &data)
+{
+#ifdef __linux__
+ if (m_procFile == 0) {
+ data.in = 0;
+ data.out = 0;
+ return;
+ }
+
+ QString output;
+ QString parser;
+ // Parse the proc file
+ while (!m_procStream->atEnd()) {
+ parser = m_procStream->readLine();
+ // remove all the entries apart from the line containing 'device'
+ if (parser.find(device) != -1)
+ output = parser;
+ }
+
+ if (output.isEmpty()) {
+ data.in = 0;
+ data.out = 0;
+ return;
+ }
+
+ // make sure our output doesn't contain "eth0:11210107" so we dont
+ // end up with netList[1] actually being netList[2]
+ output.replace(QRegExp(":"), " ");
+ QStringList netList = QStringList::split(' ', output);
+
+ data.in = netList[1].toULong();
+ data.out = netList[9].toULong();
+
+ fseek(m_procFile, 0L, SEEK_SET);
+#endif
+
+#ifdef __FreeBSD__
+ struct if_msghdr *ifm, *nextifm;
+ struct sockaddr_dl *sdl;
+ char *lim, *next;
+ size_t needed;
+ char s[32];
+
+ if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+ return;
+
+ if (m_allocSize < needed) {
+ if (m_buf != NULL)
+ delete[] m_buf;
+
+ m_buf = new char[needed];
+
+ if (m_buf == NULL)
+ return;
+
+ m_allocSize = needed;
+ }
+
+ if (sysctl(mib, 6, m_buf, &needed, NULL, 0) < 0)
+ return;
+
+ lim = m_buf + needed;
+
+ next = m_buf;
+ while (next < lim) {
+ ifm = (struct if_msghdr *)next;
+ if (ifm->ifm_type != RTM_IFINFO)
+ return;
+
+ next += ifm->ifm_msglen;
+
+ // get an interface with a network address
+ while (next < lim) {
+ nextifm = (struct if_msghdr *)next;
+ if (nextifm->ifm_type != RTM_NEWADDR)
+ break;
+
+ next += nextifm->ifm_msglen;
+ }
+
+ // if the interface is up
+ if (ifm->ifm_flags & IFF_UP) {
+ sdl = (struct sockaddr_dl *)(ifm + 1);
+ if (sdl->sdl_family != AF_LINK)
+ continue;
+
+ strncpy(s, sdl->sdl_data, sdl->sdl_nlen);
+ s[sdl->sdl_nlen] = '\0';
+
+ if (strcmp(device.local8Bit().data(), s) == 0) {
+ data.in = ifm->ifm_data.ifi_ibytes;
+ data.out = ifm->ifm_data.ifi_obytes;
+ return;
+ }
+ }
+ }
+#endif
+}
+
+bool NetView::isOnline(const QString &device)
+{
+#ifdef __linux__
+ QFile file("/proc/net/route");
+ if (!file.open(IO_ReadOnly))
+ return -1;
+
+ return (QTextStream(&file).read().find(device) != -1 ? true : false);
+#endif
+
+#ifdef __FreeBSD__
+ struct if_msghdr *ifm, *nextifm;
+ struct sockaddr_dl *sdl;
+ char *lim, *next;
+ size_t needed;
+ char s[32];
+
+ if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+ return false;
+
+ if (m_allocSize < needed) {
+ if (m_buf != NULL)
+ delete[] m_buf;
+
+ m_buf = new char[needed];
+
+ if (m_buf == NULL)
+ return false;
+
+ m_allocSize = needed;
+ }
+
+ if (sysctl(mib, 6, m_buf, &needed, NULL, 0) < 0)
+ return false;
+
+ lim = m_buf + needed;
+
+ next = m_buf;
+ while (next < lim) {
+ ifm = (struct if_msghdr *)next;
+ if (ifm->ifm_type != RTM_IFINFO)
+ return false;
+
+ next += ifm->ifm_msglen;
+
+ // get an interface with a network address
+ while (next < lim) {
+ nextifm = (struct if_msghdr *)next;
+ if (nextifm->ifm_type != RTM_NEWADDR)
+ break;
+
+ next += nextifm->ifm_msglen;
+ }
+
+ // if the interface is up
+ if (ifm->ifm_flags & IFF_UP) {
+ sdl = (struct sockaddr_dl *)(ifm + 1);
+ if (sdl->sdl_family != AF_LINK)
+ continue;
+
+ strncpy(s, sdl->sdl_data, sdl->sdl_nlen);
+ s[sdl->sdl_nlen] = '\0';
+
+ if (strcmp(s, device.local8Bit().data()) == 0)
+ return true;
+ }
+ }
+
+ return false;
+#endif
+}
+
+// EventFilter
+bool NetView::eventFilter( QObject * o, QEvent * e )
+{
+ // find out which interface we are
+ int i = 0;
+ Network::List::Iterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( o == ( *it ).chart() || o == ( *it ).label() || o == ( *it ).led() )
+ {
+ break;
+ }
+
+ ++i;
+ }
+
+ if ( e->type() == QEvent::MouseButtonPress )
+ {
+ if ( static_cast<QMouseEvent *>( e )->button() == QMouseEvent::RightButton )
+ {
+ showMenu(i);
+ }
+
+ return true;
+ }
+
+ return false;
+}
+
+void NetView::showMenu(int i) {
+
+ QPopupMenu menu;
+ menu.insertItem( SmallIcon("network"), i18n("Connect"), 1);
+ menu.insertItem( SmallIcon("network"), i18n("Disconnect"), 2);
+ switch (menu.exec(QCursor::pos())) {
+ case 1:
+ runConnectCommand(i);
+ break;
+ case 2:
+ runDisconnectCommand(i);
+ break;
+ }
+}
+
+
+#include "ksimnet.moc"
diff --git a/ksim/monitors/net/ksimnet.h b/ksim/monitors/net/ksimnet.h
new file mode 100644
index 0000000..bdb76e6
--- /dev/null
+++ b/ksim/monitors/net/ksimnet.h
@@ -0,0 +1,90 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef KSIMNET_H
+#define KSIMNET_H
+
+#include <pluginmodule.h>
+#include "netdevices.h"
+#include <stdio.h>
+
+class QTimer;
+class QVBoxLayout;
+class QTextStream;
+
+class NetPlugin : public KSim::PluginObject
+{
+ public:
+ NetPlugin(const char *name);
+ ~NetPlugin();
+
+ virtual KSim::PluginView *createView(const char *);
+ virtual KSim::PluginPage *createConfigPage(const char *);
+
+ virtual void showAbout();
+};
+
+class NetView : public KSim::PluginView
+{
+ Q_OBJECT
+ public:
+ NetView(KSim::PluginObject *parent, const char *name);
+ ~NetView();
+
+ virtual void reparseConfig();
+
+ private slots:
+ void cleanup();
+ void updateLights();
+ void updateGraph();
+ void addDisplay();
+ void runConnectCommand(int);
+ void runDisconnectCommand(int);
+
+ protected:
+ bool eventFilter(QObject *, QEvent *);
+
+ private:
+ Network::List createList() const;
+
+ KSim::Chart *addChart();
+ KSim::LedLabel *addLedLabel(const QString &device);
+ KSim::Label *addLabel();
+ QPopupMenu *addPopupMenu(const QString &device, int value);
+
+ void netStatistics(const QString &device, NetData &data);
+ bool isOnline(const QString &device);
+
+ void showMenu(int i);
+
+ bool m_firstTime;
+ Network::List m_networkList;
+ QTimer *m_netTimer;
+ QTimer *m_lightTimer;
+ QVBoxLayout *m_netLayout;
+#ifdef __linux__
+ FILE *m_procFile;
+ QTextStream *m_procStream;
+#endif
+#ifdef __FreeBSD__
+ char *m_buf;
+ int m_allocSize;
+#endif
+};
+#endif
diff --git a/ksim/monitors/net/netconfig.cpp b/ksim/monitors/net/netconfig.cpp
new file mode 100644
index 0000000..ca424b3
--- /dev/null
+++ b/ksim/monitors/net/netconfig.cpp
@@ -0,0 +1,296 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <qpushbutton.h>
+#include <qlayout.h>
+#include <qcursor.h>
+
+#include <klistview.h>
+#include <ksimpleconfig.h>
+#include <klocale.h>
+#include <kiconloader.h>
+#include <kapplication.h>
+#include <kpopupmenu.h>
+#include <kdebug.h>
+#include <kmessagebox.h>
+
+#include "netconfig.h"
+#include "netconfig.moc"
+#include "netdialog.h"
+
+NetConfig::NetConfig(KSim::PluginObject *parent, const char *name)
+ : KSim::PluginPage(parent, name)
+{
+ m_yes = i18n("yes");
+ m_no = i18n("no");
+
+ QVBoxLayout *mainLayout = new QVBoxLayout(this);
+ mainLayout->setSpacing(6);
+
+ usingBox = new KListView(this);
+ usingBox ->addColumn(i18n("Interface"));
+ usingBox ->addColumn(i18n("Timer"));
+ usingBox ->addColumn(i18n("Commands"));
+ usingBox->setAllColumnsShowFocus(true);
+
+ connect(usingBox, SIGNAL(contextMenu(KListView *,
+ QListViewItem *, const QPoint &)),
+ SLOT(menu(KListView *, QListViewItem *, const QPoint &)));
+ connect(usingBox, SIGNAL(doubleClicked(QListViewItem *)),
+ SLOT(modifyItem(QListViewItem *)));
+ mainLayout->addWidget(usingBox);
+
+ layout = new QHBoxLayout;
+ layout->setSpacing(6);
+
+ QSpacerItem *spacer = new QSpacerItem(20, 20,
+ QSizePolicy::Expanding, QSizePolicy::Minimum);
+ layout->addItem(spacer);
+
+ insertButton = new QPushButton(this);
+ insertButton->setText(i18n("Add..."));
+ connect(insertButton, SIGNAL(clicked()), SLOT(showNetDialog()));
+ layout->addWidget(insertButton);
+
+ modifyButton = new QPushButton(this);
+ modifyButton->setText(i18n("Modify..."));
+ connect(modifyButton, SIGNAL(clicked()), SLOT(modifyCurrent()));
+ layout->addWidget(modifyButton);
+
+ removeButton = new QPushButton(this);
+ removeButton->setText(i18n("Remove"));
+ connect(removeButton, SIGNAL(clicked()), SLOT(removeCurrent()));
+ layout->addWidget(removeButton);
+
+ mainLayout->addLayout(layout);
+}
+
+NetConfig::~NetConfig()
+{
+}
+
+void NetConfig::saveConfig()
+{
+ qHeapSort( m_networkList);
+
+ int i = 0;
+ Network::List::Iterator it;
+ for( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ config()->setGroup( "device-" + QString::number( i ) );
+ config()->writeEntry( "deviceName", ( *it ).name() );
+ config()->writeEntry( "showTimer", ( *it ).showTimer() );
+ config()->writeEntry( "deviceFormat", ( *it ).format() );
+ config()->writeEntry( "commands", ( *it ).commandsEnabled() );
+ config()->writeEntry( "cCommand", ( *it ).connectCommand() );
+ config()->writeEntry( "dCommand", ( *it ).disconnectCommand() );
+ i++;
+ }
+
+ config()->setGroup( "Net" );
+ config()->writeEntry( "deviceAmount", m_networkList.count() );
+}
+
+void NetConfig::readConfig()
+{
+ usingBox->clear();
+ m_networkList.clear();
+
+ config()->setGroup( "Net" );
+ int deviceAmount = config()->readNumEntry( "deviceAmount" );
+
+ for ( int i = 0; i < deviceAmount; ++i )
+ {
+ if ( !config()->hasGroup( "device-" + QString::number( i ) ) )
+ {
+ continue;
+ }
+
+ config()->setGroup("device-" + QString::number(i));
+
+ m_networkList.append( Network( config()->readEntry( "deviceName" ),
+ config()->readEntry( "deviceFormat" ),
+ config()->readBoolEntry( "showTimer" ),
+ config()->readBoolEntry( "commands" ),
+ config()->readEntry( "cCommand" ),
+ config()->readEntry( "dCommand" ) ) );
+
+ (void) new QListViewItem( usingBox,
+ config()->readEntry( "deviceName" ),
+ boolToString( config()->readBoolEntry( "showTimer" ) ),
+ boolToString( config()->readBoolEntry( "commands" ) ) );
+ }
+}
+
+void NetConfig::menu(KListView *, QListViewItem *item, const QPoint &)
+{
+ aboutMenu = new QPopupMenu(this);
+
+ if (item) {
+ aboutMenu->insertItem(i18n("&Add Net Device"), 3);
+ aboutMenu->insertItem(i18n("&Modify '%1'").arg(item->text(0)), 2);
+ aboutMenu->insertItem(i18n("&Remove '%1'").arg(item->text(0)), 1);
+ }
+ else {
+ aboutMenu->insertItem(i18n("&Add Net Device"), 3);
+ aboutMenu->insertItem(i18n("&Modify..."), 2);
+ aboutMenu->insertItem(i18n("&Remove..."), 1);
+ aboutMenu->setItemEnabled(1, false);
+ aboutMenu->setItemEnabled(2, false);
+ }
+
+ switch (aboutMenu->exec(QCursor::pos())) {
+ case 1:
+ removeItem(item);
+ break;
+ case 2:
+ modifyItem(item);
+ break;
+ case 3:
+ showNetDialog();
+ break;
+ }
+
+ delete aboutMenu;
+}
+
+void NetConfig::modifyItem(QListViewItem *item)
+{
+ if (!item)
+ return;
+
+ netDialog = new NetDialog(this);
+ Network::List::Iterator it, netDevice;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( ( *it ).name() == item->text( 0 ) )
+ {
+ netDevice = it;
+ netDialog->setDeviceName( ( *it ).name() );
+ netDialog->setShowTimer( ( *it ).showTimer() );
+ netDialog->setFormat( ( *it ).format() );
+ netDialog->setShowCommands( ( *it ).commandsEnabled() );
+ netDialog->setCCommand( ( *it ).connectCommand() );
+ netDialog->setDCommand( ( *it ).disconnectCommand() );
+ break;
+ }
+ }
+
+ netDialog->exec();
+ if ( netDialog->okClicked() )
+ {
+ m_networkList.remove( netDevice );
+ m_networkList.append( Network( netDialog->deviceName(),
+ netDialog->format(),
+ netDialog->timer(),
+ netDialog->commands(),
+ netDialog->cCommand(),
+ netDialog->dCommand() ) );
+
+ item->setText( 0, netDialog->deviceName() );
+ item->setText( 1, boolToString( netDialog->timer() ) );
+ item->setText( 2, boolToString( netDialog->commands() ) );
+ }
+
+ delete netDialog;
+}
+
+void NetConfig::removeItem(QListViewItem *item)
+{
+ if (!item)
+ return;
+
+ int result = KMessageBox::warningContinueCancel(0, i18n("Are you sure you "
+ "want to remove the net interface '%1'?").arg(item->text(0)), QString::null, KStdGuiItem::del());
+
+ if (result == KMessageBox::Cancel)
+ return;
+
+ int i = 0;
+ Network::List::Iterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( item->text( 0 ) == ( *it ).name() )
+ {
+ m_networkList.remove( it );
+ if ( config()->deleteGroup( "device-" + QString::number( i ) ) )
+ kdDebug(2003) << "device-" << i << " was deleted" << endl;
+
+ break;
+ }
+ i++;
+ }
+
+ delete item;
+}
+
+void NetConfig::removeCurrent()
+{
+ removeItem(usingBox->currentItem());
+}
+
+void NetConfig::modifyCurrent()
+{
+ modifyItem(usingBox->currentItem());
+}
+
+void NetConfig::showNetDialog()
+{
+ netDialog = new NetDialog(this, "netDialog");
+ netDialog->exec();
+ if (netDialog->okClicked())
+ getStats();
+
+ delete netDialog;
+}
+
+void NetConfig::getStats()
+{
+ Network::List::ConstIterator it;
+ for ( it = m_networkList.begin(); it != m_networkList.end(); ++it )
+ {
+ if ( ( *it ).name() == netDialog->deviceName() )
+ {
+ KMessageBox::sorry(0, i18n("You already have a network "
+ "interface by this name. Please select a different interface"));
+
+ return;
+ }
+ }
+
+ m_networkList.append( Network( netDialog->deviceName(),
+ netDialog->format(),
+ netDialog->timer(),
+ netDialog->commands(),
+ netDialog->cCommand(),
+ netDialog->dCommand() ) );
+
+ (void) new QListViewItem( usingBox,
+ netDialog->deviceName(),
+ boolToString( netDialog->timer() ),
+ boolToString( netDialog->commands() ) );
+}
+
+const QString &NetConfig::boolToString(bool value) const
+{
+ if (value)
+ return m_yes;
+
+ return m_no;
+}
diff --git a/ksim/monitors/net/netconfig.h b/ksim/monitors/net/netconfig.h
new file mode 100644
index 0000000..9ec5c8d
--- /dev/null
+++ b/ksim/monitors/net/netconfig.h
@@ -0,0 +1,67 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef NETCONFIG_H
+#define NETCONFIG_H
+
+#include <pluginmodule.h>
+#include "netdevices.h"
+
+class NetDialog;
+class QPopupMenu;
+class QPushButton;
+class KListView;
+class QHBoxLayout;
+class QListViewItem;
+
+class NetConfig : public KSim::PluginPage
+{
+ Q_OBJECT
+ public:
+ NetConfig(KSim::PluginObject *parent, const char *name);
+ virtual ~NetConfig();
+
+ virtual void saveConfig();
+ virtual void readConfig();
+
+ private slots:
+ void menu(KListView *, QListViewItem *, const QPoint &);
+ void modifyItem(QListViewItem *);
+ void removeItem(QListViewItem *);
+ void removeCurrent();
+ void modifyCurrent();
+ void showNetDialog();
+ void getStats();
+
+ private:
+ // Couldnt think of a better name for this :)
+ const QString &boolToString(bool) const;
+
+ QHBoxLayout *layout;
+ QPushButton *insertButton;
+ QPushButton *removeButton;
+ QPushButton *modifyButton;
+ KListView *usingBox;
+ NetDialog *netDialog;
+ Network::List m_networkList;
+ QString m_yes;
+ QString m_no;
+ QPopupMenu *aboutMenu;
+};
+#endif
diff --git a/ksim/monitors/net/netdevices.h b/ksim/monitors/net/netdevices.h
new file mode 100644
index 0000000..2a5a680
--- /dev/null
+++ b/ksim/monitors/net/netdevices.h
@@ -0,0 +1,218 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef NETDEVICES_H
+#define NETDEVICES_H
+
+#include <chart.h>
+#include <led.h>
+
+class NetData
+{
+ public:
+ NetData() : in(0), out(0) {}
+ NetData &operator=( const NetData & rhs )
+ {
+ in = rhs.in;
+ out = rhs.out;
+
+ return *this;
+ }
+
+ unsigned long in;
+ unsigned long out;
+};
+
+class Network
+{
+ public:
+ typedef QValueList<Network> List;
+
+ Network()
+ {
+ m_chart = 0;
+ m_led = 0;
+ m_label = 0;
+ m_popup = 0;
+
+ m_max = 0;
+ }
+
+ Network( const QString & name,
+ const QString & format,
+ bool timer,
+ bool commands,
+ const QString & cCommand,
+ const QString & dCommand )
+ : m_name( name ),
+ m_format( format ),
+ m_timer( timer ),
+ m_commands( commands ),
+ m_cCommand( cCommand ),
+ m_dCommand( dCommand )
+ {
+ m_chart = 0;
+ m_led = 0;
+ m_label = 0;
+ m_popup = 0;
+
+ m_max = 0;
+ }
+
+ bool operator==( const Network & rhs ) const
+ {
+ return m_name == rhs.m_name &&
+ m_format == rhs.m_format &&
+ m_timer == rhs.m_timer &&
+ m_commands == rhs.m_commands &&
+ m_cCommand == rhs.m_cCommand &&
+ m_dCommand == rhs.m_dCommand;
+ }
+
+ bool operator!=( const Network & rhs ) const
+ {
+ return !operator==(rhs);
+ }
+
+ bool operator<( const Network & rhs ) const
+ {
+ return m_name < rhs.m_name;
+ }
+
+ bool operator>( const Network & rhs ) const
+ {
+ return m_name > rhs.m_name;
+ }
+
+ bool operator<=( const Network & rhs ) const
+ {
+ return !( *this > rhs );
+ }
+
+ void cleanup()
+ {
+ delete m_chart;
+ delete m_label;
+ delete m_led;
+ delete m_popup;
+
+ m_chart = 0;
+ m_label = 0;
+ m_led = 0;
+ m_popup = 0;
+ }
+
+ void setData( const NetData & data )
+ {
+ m_old = m_data;
+ m_data = data;
+ }
+
+ const NetData & data() const
+ {
+ return m_data;
+ }
+
+ const NetData & oldData() const
+ {
+ return m_old;
+ }
+
+ void setMaxValue( int max )
+ {
+ m_max = max;
+ }
+
+ int maxValue() const
+ {
+ return m_max;
+ }
+
+ const QString & name() const
+ {
+ return m_name;
+ }
+
+ bool showTimer() const
+ {
+ return m_timer;
+ }
+
+ const QString & format() const
+ {
+ return m_format;
+ }
+
+ bool commandsEnabled() const
+ {
+ return m_commands;
+ }
+
+ const QString & connectCommand() const
+ {
+ return m_cCommand;
+ }
+
+ const QString & disconnectCommand() const
+ {
+ return m_dCommand;
+ }
+
+ void setDisplay( KSim::Chart * chart, KSim::LedLabel * led,
+ KSim::Label * label, QPopupMenu * popup )
+ {
+ m_chart = chart;
+ m_led = led;
+ m_label = label;
+ m_popup = popup;
+ }
+
+ KSim::Chart * chart()
+ {
+ return m_chart;
+ }
+
+ KSim::LedLabel * led()
+ {
+ return m_led;
+ }
+
+ KSim::Label * label()
+ {
+ return m_label;
+ }
+
+ private:
+ NetData m_data;
+ NetData m_old;
+ QString m_name;
+ QString m_format;
+ bool m_timer;
+ bool m_commands;
+ QString m_cCommand;
+ QString m_dCommand;
+
+ KSim::Chart * m_chart;
+ KSim::LedLabel * m_led;
+ KSim::Label * m_label;
+ QPopupMenu * m_popup;
+
+ int m_max;
+};
+#endif
diff --git a/ksim/monitors/net/netdialog.cpp b/ksim/monitors/net/netdialog.cpp
new file mode 100644
index 0000000..fe655d4
--- /dev/null
+++ b/ksim/monitors/net/netdialog.cpp
@@ -0,0 +1,328 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "netdialog.h"
+#include "netdialog.moc"
+
+#include <qcheckbox.h>
+#include <qgroupbox.h>
+#include <qlabel.h>
+#include <qpushbutton.h>
+#include <qtabwidget.h>
+#include <qwidget.h>
+#include <qlayout.h>
+#include <qfile.h>
+#include <qregexp.h>
+
+#include <klocale.h>
+#include <kcombobox.h>
+#include <kdebug.h>
+#include <klineedit.h>
+#include <kapplication.h>
+#include <kurlrequester.h>
+#include <kstdguiitem.h>
+
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
+#endif
+
+#include <string.h>
+
+NetDialog::NetDialog(QWidget *parent, const char *name)
+ : QTabDialog(parent, name, true)
+{
+ m_clicked = false;
+ setCaption(kapp->makeStdCaption(i18n("Network Interface")));
+
+ m_generalTab = new QWidget(this);
+ m_generalLayout = new QGridLayout(m_generalTab);
+ m_generalLayout->setSpacing(6);
+ m_generalLayout->setMargin(11);
+
+ m_deviceLabel = new QLabel(m_generalTab);
+ m_deviceLabel->setText(i18n("Interface:"));
+ m_generalLayout->addMultiCellWidget(m_deviceLabel, 0, 0, 0, 0);
+
+ m_deviceCombo = new KComboBox(true, m_generalTab);
+ m_deviceCombo->setSizePolicy(QSizePolicy(QSizePolicy::Preferred,
+ QSizePolicy::Fixed));
+ m_deviceCombo->setFocus();
+ m_deviceCombo->setDuplicatesEnabled(false);
+ m_generalLayout->addMultiCellWidget(m_deviceCombo, 0, 0, 1, 1);
+
+ QStringList output(createList());
+ if (output.isEmpty()) {
+ m_deviceCombo->insertItem("ppp0");
+ m_deviceCombo->insertItem("eth0");
+ }
+ else
+ m_deviceCombo->insertStringList(output);
+
+ QSpacerItem *deviceSpacer = new QSpacerItem(20, 20,
+ QSizePolicy::Expanding, QSizePolicy::Fixed);
+ m_generalLayout->addMultiCell(deviceSpacer, 0, 0, 2, 2);
+
+ m_timerBox = new QGroupBox(m_generalTab);
+ m_timerBox->setTitle(i18n("Timer"));
+ m_timerBox->setColumnLayout(0, Qt::Vertical);
+ m_timerBox->layout()->setSpacing(0);
+ m_timerBox->layout()->setMargin(0);
+ m_timerBoxLayout = new QVBoxLayout(m_timerBox->layout());
+ m_timerBoxLayout->setAlignment(Qt::AlignTop);
+ m_timerBoxLayout->setSpacing(6);
+ m_timerBoxLayout->setMargin(11);
+
+ m_timerEdit = new KLineEdit(m_timerBox);
+ m_timerEdit->setText("hh:mm:ss");
+ m_timerEdit->setEnabled(false);
+
+ m_showTimer = new QCheckBox(m_timerBox);
+ m_showTimer->setText(i18n("Show timer"));
+ connect(m_showTimer, SIGNAL(toggled(bool)),
+ m_timerEdit, SLOT(setEnabled(bool)));
+ m_timerBoxLayout->addWidget(m_showTimer);
+ m_timerBoxLayout->addWidget(m_timerEdit);
+
+ m_hFormat = new QLabel(m_timerBox);
+ m_hFormat->setText(i18n("hh - Total hours online"));
+ m_timerBoxLayout->addWidget(m_hFormat);
+
+ m_mFormat = new QLabel(m_timerBox);
+ m_mFormat->setText(i18n("mm - Total minutes online"));
+ m_timerBoxLayout->addWidget(m_mFormat);
+
+ m_sFormat = new QLabel(m_timerBox);
+ m_sFormat->setText(i18n("ss - Total seconds online"));
+ m_timerBoxLayout->addWidget(m_sFormat);
+ m_generalLayout->addMultiCellWidget(m_timerBox, 1, 1, 0, 2);
+
+ QSpacerItem *spacer = new QSpacerItem(20, 20,
+ QSizePolicy::Minimum, QSizePolicy::Expanding);
+ m_generalLayout->addMultiCell(spacer, 2, 2, 0, 0);
+ addTab(m_generalTab, i18n("General"));
+
+ m_commandTab = new QWidget(this);
+ m_commandLayout = new QGridLayout(m_commandTab);
+ m_commandLayout->setSpacing(6);
+ m_commandLayout->setMargin(11);
+
+ m_enableCommands = new QCheckBox(m_commandTab);
+ m_enableCommands->setText(i18n("Enable connect/disconnect"));
+ m_commandLayout->addMultiCellWidget(m_enableCommands, 0, 0, 0, 2);
+
+ m_cCommand = new QLabel(m_commandTab);
+ m_cCommand->setText(i18n("Connect command:"));
+ m_commandLayout->addMultiCellWidget(m_cCommand, 1, 1, 0, 0);
+
+ m_connectRequester = new KURLRequester(m_commandTab);
+ m_connectRequester->setMinimumSize(145, 0);
+ m_connectRequester->setEnabled(false);
+ connect(m_enableCommands, SIGNAL(toggled(bool)),
+ m_connectRequester, SLOT(setEnabled(bool)));
+ m_commandLayout->addMultiCellWidget(m_connectRequester, 1, 1, 1, 2);
+
+ m_dCommand = new QLabel(m_commandTab);
+ m_dCommand->setText(i18n("Disconnect command:"));
+ m_commandLayout->addMultiCellWidget(m_dCommand, 2, 2, 0, 0);
+
+ m_disconnectRequester = new KURLRequester(m_commandTab);
+ m_disconnectRequester->setMinimumSize(145, 0);
+ m_disconnectRequester->setEnabled(false);
+ connect(m_enableCommands, SIGNAL(toggled(bool)),
+ m_disconnectRequester, SLOT(setEnabled(bool)));
+ m_commandLayout->addMultiCellWidget(m_disconnectRequester, 2, 2, 1, 2);
+
+ QSpacerItem *commandSpacer = new QSpacerItem(20, 20,
+ QSizePolicy::Minimum, QSizePolicy::Expanding);
+ m_commandLayout->addItem(commandSpacer);
+ addTab(m_commandTab, i18n("Commands"));
+
+ setOkButton(KStdGuiItem::ok().text());
+ setCancelButton(KStdGuiItem::cancel().text());
+ connect(this, SIGNAL(applyButtonPressed()), SLOT(sendClicked()));
+}
+
+NetDialog::~NetDialog()
+{
+}
+
+const QString NetDialog::deviceName() const
+{
+ return m_deviceCombo->currentText();
+}
+
+bool NetDialog::timer()
+{
+ return m_showTimer->isChecked();
+}
+
+const QString NetDialog::format() const
+{
+ return m_timerEdit->text();
+}
+
+bool NetDialog::commands()
+{
+ return m_enableCommands->isChecked();
+}
+
+const QString NetDialog::cCommand() const
+{
+ return m_connectRequester->url();
+}
+
+const QString NetDialog::dCommand() const
+{
+ return m_disconnectRequester->url();
+}
+
+void NetDialog::setDeviceName(const QString &text)
+{
+ m_deviceCombo->setCurrentItem(text, true);
+}
+
+void NetDialog::setShowTimer(bool value)
+{
+ m_showTimer->setChecked(value);
+}
+
+void NetDialog::setFormat(const QString &format)
+{
+ m_timerEdit->setText(format);
+}
+
+void NetDialog::setShowCommands(bool value)
+{
+ m_enableCommands->setChecked(value);
+}
+
+void NetDialog::setCCommand(const QString &text)
+{
+ m_connectRequester->setURL(text);
+}
+
+void NetDialog::setDCommand(const QString &text)
+{
+ m_disconnectRequester->setURL(text);
+}
+
+void NetDialog::sendClicked()
+{
+ m_clicked = true;
+ kdDebug(2003) << "ok was clicked" << endl;
+}
+
+QStringList NetDialog::createList() const
+{
+#ifdef __linux__
+ QFile file("/proc/net/dev");
+ if (!file.open(IO_ReadOnly))
+ return QStringList();
+
+ QStringList output;
+ QTextStream textStream(&file);
+ while (!textStream.atEnd())
+ output.append(textStream.readLine());
+
+ if (output.isEmpty())
+ return QStringList();
+
+ output.pop_front();
+ output.pop_front();
+
+ QStringList::Iterator it;
+ QStringList list;
+ for (it = output.begin(); it != output.end(); ++it) {
+ list = QStringList::split(' ', (*it));
+ (*it) = list[0].stripWhiteSpace();
+ (*it).truncate((*it).find(':'));
+ }
+
+ return output;
+#endif
+
+#ifdef __FreeBSD__
+ QStringList output;
+ int mib[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
+ char *buf = 0;
+ int alloc = 0;
+ struct if_msghdr *ifm, *nextifm;
+ struct sockaddr_dl *sdl;
+ char *lim, *next;
+ size_t needed;
+ char s[32];
+
+ if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+ return QStringList();
+
+ if (alloc < needed) {
+ buf = new char[needed];
+
+ if (buf == NULL)
+ return QStringList();
+
+ alloc = needed;
+ }
+
+ if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+ return QStringList();
+
+ lim = buf + needed;
+
+ next = buf;
+ while (next < lim) {
+ ifm = (struct if_msghdr *)next;
+ if (ifm->ifm_type != RTM_IFINFO)
+ return QStringList();
+
+ next += ifm->ifm_msglen;
+
+ // get an interface with a network address
+ while (next < lim) {
+ nextifm = (struct if_msghdr *)next;
+ if (nextifm->ifm_type != RTM_NEWADDR)
+ break;
+
+ next += nextifm->ifm_msglen;
+ }
+
+ // if the interface is up
+ if (ifm->ifm_flags & IFF_UP) {
+ sdl = (struct sockaddr_dl *)(ifm + 1);
+ if (sdl->sdl_family != AF_LINK)
+ continue;
+
+ strncpy(s, sdl->sdl_data, sdl->sdl_nlen);
+ s[sdl->sdl_nlen] = '\0';
+
+ output.append(s);
+ }
+ }
+
+ if (buf)
+ delete[] buf;
+
+ return output;
+#endif
+}
diff --git a/ksim/monitors/net/netdialog.h b/ksim/monitors/net/netdialog.h
new file mode 100644
index 0000000..b6d7801
--- /dev/null
+++ b/ksim/monitors/net/netdialog.h
@@ -0,0 +1,85 @@
+/* ksim - a system monitor for kde
+ *
+ * Copyright (C) 2001 Robbie Ward <linuxphreak@gmx.co.uk>
+ *
+ * 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; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef NETDIALOG_H
+#define NETDIALOG_H
+
+#include <qtabdialog.h>
+
+class QVBoxLayout;
+class QGridLayout;
+class QCheckBox;
+class QGroupBox;
+class QLabel;
+class QWidget;
+class KComboBox;
+class KLineEdit;
+class KURLRequester;
+
+class NetDialog : public QTabDialog
+{
+ Q_OBJECT
+ public:
+ NetDialog(QWidget *parent, const char *name = 0);
+ ~NetDialog();
+
+ const QString deviceName() const;
+ bool timer();
+ const QString format() const;
+ bool commands();
+ const QString cCommand() const;
+ const QString dCommand() const;
+ bool okClicked() { return m_clicked; }
+
+ public slots:
+ void setDeviceName(const QString &);
+ void setShowTimer(bool);
+ void setFormat(const QString &);
+ void setShowCommands(bool);
+ void setCCommand(const QString &);
+ void setDCommand(const QString &);
+
+ private slots:
+ void sendClicked();
+
+ private:
+ QStringList createList() const;
+
+ bool m_clicked;
+ QWidget *m_generalTab;
+ QLabel *m_deviceLabel;
+ KComboBox *m_deviceCombo;
+ QGroupBox *m_timerBox;
+ QCheckBox *m_showTimer;
+ KLineEdit *m_timerEdit;
+ QLabel *m_hFormat;
+ QLabel *m_mFormat;
+ QLabel *m_sFormat;
+ QWidget *m_commandTab;
+ QCheckBox *m_enableCommands;
+ QLabel *m_cCommand;
+ KURLRequester *m_connectRequester;
+ QLabel *m_dCommand;
+ KURLRequester *m_disconnectRequester;
+
+ QGridLayout *m_generalLayout;
+ QVBoxLayout *m_timerBoxLayout;
+ QGridLayout *m_commandLayout;
+};
+#endif // NETDIALOG_H