summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/net/netdialog.cpp
blob: 60a5d2037dd22027b5f8546a019d26d3bd2c11ed (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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 <tqcheckbox.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqtabwidget.h>
#include <tqwidget.h>
#include <tqlayout.h>
#include <tqfile.h>
#include <tqregexp.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(TQWidget *parent, const char *name)
   : TQTabDialog(parent, name, true)
{
  m_clicked = false;
  setCaption(kapp->makeStdCaption(i18n("Network Interface")));

  m_generalTab = new TQWidget(this);
  m_generalLayout = new TQGridLayout(m_generalTab);
  m_generalLayout->setSpacing(6);
  m_generalLayout->setMargin(11);

  m_deviceLabel = new TQLabel(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(TQSizePolicy(TQSizePolicy::Preferred,
     TQSizePolicy::Fixed));
  m_deviceCombo->setFocus();
  m_deviceCombo->setDuplicatesEnabled(false);
  m_generalLayout->addMultiCellWidget(m_deviceCombo, 0, 0, 1, 1);

  TQStringList output(createList());
  if (output.isEmpty()) {
    m_deviceCombo->insertItem("ppp0");
    m_deviceCombo->insertItem("eth0");
  }
  else
    m_deviceCombo->insertStringList(output);

  TQSpacerItem *deviceSpacer = new TQSpacerItem(20, 20,
     TQSizePolicy::Expanding, TQSizePolicy::Fixed);
  m_generalLayout->addMultiCell(deviceSpacer, 0, 0, 2, 2);

  m_timerBox = new TQGroupBox(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 TQVBoxLayout(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 TQCheckBox(m_timerBox);
  m_showTimer->setText(i18n("Show timer"));
  connect(m_showTimer, TQT_SIGNAL(toggled(bool)),
      m_timerEdit, TQT_SLOT(setEnabled(bool)));
  m_timerBoxLayout->addWidget(m_showTimer);
  m_timerBoxLayout->addWidget(m_timerEdit);

  m_hFormat = new TQLabel(m_timerBox);
  m_hFormat->setText(i18n("hh - Total hours online"));
  m_timerBoxLayout->addWidget(m_hFormat);

  m_mFormat = new TQLabel(m_timerBox);
  m_mFormat->setText(i18n("mm - Total minutes online"));
  m_timerBoxLayout->addWidget(m_mFormat);

  m_sFormat = new TQLabel(m_timerBox);
  m_sFormat->setText(i18n("ss - Total seconds online"));
  m_timerBoxLayout->addWidget(m_sFormat);
  m_generalLayout->addMultiCellWidget(m_timerBox, 1, 1, 0, 2);

  TQSpacerItem *spacer = new TQSpacerItem(20, 20,
      TQSizePolicy::Minimum, TQSizePolicy::Expanding);
  m_generalLayout->addMultiCell(spacer, 2, 2, 0, 0);
  addTab(m_generalTab, i18n("General"));

  m_commandTab = new TQWidget(this);
  m_commandLayout = new TQGridLayout(m_commandTab);
  m_commandLayout->setSpacing(6);
  m_commandLayout->setMargin(11);

  m_enableCommands = new TQCheckBox(m_commandTab);
  m_enableCommands->setText(i18n("Enable connect/disconnect"));
  m_commandLayout->addMultiCellWidget(m_enableCommands, 0, 0, 0, 2);

  m_cCommand = new TQLabel(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, TQT_SIGNAL(toggled(bool)),
      m_connectRequester, TQT_SLOT(setEnabled(bool)));
  m_commandLayout->addMultiCellWidget(m_connectRequester, 1, 1, 1, 2);

  m_dCommand = new TQLabel(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, TQT_SIGNAL(toggled(bool)),
      m_disconnectRequester, TQT_SLOT(setEnabled(bool)));
  m_commandLayout->addMultiCellWidget(m_disconnectRequester, 2, 2, 1, 2);

  TQSpacerItem *commandSpacer = new TQSpacerItem(20, 20,
      TQSizePolicy::Minimum, TQSizePolicy::Expanding);
  m_commandLayout->addItem(commandSpacer);
  addTab(m_commandTab, i18n("Commands"));

  setOkButton(KStdGuiItem::ok().text());
  setCancelButton(KStdGuiItem::cancel().text());
  connect(this, TQT_SIGNAL(applyButtonPressed()), TQT_SLOT(sendClicked()));
}

NetDialog::~NetDialog()
{
}

const TQString NetDialog::deviceName() const
{
  return m_deviceCombo->currentText();
}

bool NetDialog::timer()
{
  return m_showTimer->isChecked();
}

const TQString NetDialog::format() const
{
  return m_timerEdit->text();
}

bool NetDialog::commands()
{
  return m_enableCommands->isChecked();
}

const TQString NetDialog::cCommand() const
{
  return m_connectRequester->url();
}

const TQString NetDialog::dCommand() const
{
  return m_disconnectRequester->url();
}

void NetDialog::setDeviceName(const TQString &text)
{
  m_deviceCombo->setCurrentItem(text, true);
}

void NetDialog::setShowTimer(bool value)
{
  m_showTimer->setChecked(value);
}

void NetDialog::setFormat(const TQString &format)
{
  m_timerEdit->setText(format);
}

void NetDialog::setShowCommands(bool value)
{
  m_enableCommands->setChecked(value);
}

void NetDialog::setCCommand(const TQString &text)
{
  m_connectRequester->setURL(text);
}

void NetDialog::setDCommand(const TQString &text)
{
  m_disconnectRequester->setURL(text);
}

void NetDialog::sendClicked()
{
  m_clicked = true;
  kdDebug(2003) << "ok was clicked" << endl;
}

TQStringList NetDialog::createList() const
{
#ifdef __linux__
  TQFile file("/proc/net/dev");
  if (!file.open(IO_ReadOnly))
    return TQStringList();

  TQStringList output;
  TQTextStream textStream(&file);
  while (!textStream.atEnd())
    output.append(textStream.readLine());

  if (output.isEmpty())
    return TQStringList();

  output.pop_front();
  output.pop_front();

  TQStringList::Iterator it;
  TQStringList list;
  for (it = output.begin(); it != output.end(); ++it) {
    list = TQStringList::split(' ', (*it));
    (*it) = list[0].stripWhiteSpace();
    (*it).truncate((*it).find(':'));
  }

  return output;
#endif

#ifdef __FreeBSD__
  TQStringList 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 TQStringList();

  if (alloc < needed) {
    buf = new char[needed];

    if (buf == NULL)
      return TQStringList();

    alloc = needed;
  }

  if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    return TQStringList();

  lim = buf + needed;

  next = buf;
  while (next < lim) {
    ifm = (struct if_msghdr *)next;
    if (ifm->ifm_type != RTM_IFINFO)
      return TQStringList();

    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
}