summaryrefslogtreecommitdiffstats
path: root/knetload/devicedialog.cpp
blob: ab00fdb95ea5ad08ec1e0c6d25e060b40f0dc862 (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

/***************************************************************************
 *                                                                         *
 *   KNetLoad is copyright (c) 1999-2000, Markus Gustavsson                *
 *                         (c) 2002, Ben Burton                            *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "devicedialog.h"

#include <kcombobox.h>
#include <klocale.h>
#include <tqlabel.h>
#include <tqhbox.h>

#ifdef Q_OS_LINUX
#include <tqdir.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#endif

#define MAX_NET_DEV_LINE 512

DeviceDialog::DeviceDialog(const TQString& defaultDevice, TQWidget* tqparent) :
        KDialogBase(tqparent, "device dialog", true,
        i18n("Select Network Device"), Ok|Cancel, Ok),
        device(defaultDevice) {
    TQHBox* page = makeHBoxMainWidget();

    new TQLabel(i18n("Network device to monitor:"), page);

    // Items in the combo box are not wrapped with i18n() since they're
    // literal interface names.
    KComboBox* deviceBox = new KComboBox(true, page);
    
#ifndef Q_OS_LINUX
    deviceBox->insertItem("lo");
    deviceBox->insertItem("eth0");
    deviceBox->insertItem("ippp0");
    deviceBox->insertItem("ppp0");
#else
    if ( TQDir::root().exists("/sys/class/net") )
    { // Exists /sys, 2.6 series kernel
        TQDir sys("/sys/class/net");
        TQStringList l = sys.entryList();
        for(TQStringList::iterator it = l.begin(); it != l.end(); it++)
        {
            if ( (*it)[0] == '.' )
                continue;
            deviceBox->insertItem( *it );
        }
    } else { // Doesn't exists, kernel 2.4 or earlier
        static FILE* fd;
        static char line[MAX_NET_DEV_LINE];
        static char* pos;
        static char* iface;

    
        if ((fd = fopen("/proc/net/dev", "r")) == 0)
            return;

        // Read the unwanted header lines.
        fgets(line, MAX_NET_DEV_LINE, fd);
        fgets(line, MAX_NET_DEV_LINE, fd);

        // Read through the remaining lines until we find all devices
        while (! feof(fd)) {
            fgets(line, MAX_NET_DEV_LINE, fd);

            // Find the interface name for this line.
            for (iface = line; *iface == ' '; iface++)
                ; // (skip initial spaces)
            for (pos = iface; *pos != ':' && *pos != 0; pos++)
                ; // (move to next ':' or end of string)
            if (*pos == 0)
                continue; // (was not ':')
            *pos = 0;

            // Now iface points to a null-terminated string containing the
            // interface name for this particular line.
            deviceBox->insertItem( iface );
        }
        fclose(fd);
    }
#endif
    deviceBox->setCurrentText(defaultDevice);

    connect(deviceBox, TQT_SIGNAL(textChanged(const TQString&)),
        this, TQT_SLOT(updateDevice(const TQString&)));
}

const TQString& DeviceDialog::getDevice() const {
    return device;
}

void DeviceDialog::updateDevice(const TQString& text) {
    device = text;
}

#include "devicedialog.moc"