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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
|
/***************************************************************************
knetworkconf.cpp - description
-------------------
begin : Sun Jan 12 8:54:19 UTC 2003
copyright : (C) 2003 by Juan Luis Baptiste
email : jbaptiste@merlinux.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. *
* *
***************************************************************************/
#include <kiconloader.h>
#include <dcopclient.h>
#include "knetworkconf.h"
KNetworkConf::KNetworkConf(TQWidget *parent, const char *name) : DCOPObject("KNetworkConfIface"), KNetworkConfDlg(parent, name)
{
netInfo = 0L;
makeButtonsResizeable();
config = new KNetworkConfigParser();
klvCardList->setAllColumnsShowFocus(true);
klvKnownHosts->setAllColumnsShowFocus(true);
klvProfilesList->setAllColumnsShowFocus(true);
klvProfilesList->setRenameable(0,true);
klvProfilesList->setRenameable(1,true);
TQToolTip::remove( static_cast<TQListView*>(klvProfilesList) );
tooltip = new KProfilesListViewToolTip(klvProfilesList);
//Connect signals emmitted by the backend to know when data is ready to be painted.
connect(config,TQT_SIGNAL(readyLoadingNetworkInfo()),this,TQT_SLOT(getNetworkInfoSlot()));
connect(config,TQT_SIGNAL(readyLoadingNetworkInfo()),this,TQT_SLOT(showMainWindow()));
connect(config,TQT_SIGNAL(readyLoadingNetworkInfo()),this,TQT_SLOT(enableSignals()));
connect(config, TQT_SIGNAL(setReadOnly(bool)),this,TQT_SLOT(setReadOnlySlot(bool)));
connect(klvCardList,
TQT_SIGNAL(contextMenu(TDEListView*,TQListViewItem*,const TQPoint&)),
this,
TQT_SLOT(showInterfaceContextMenuSlot(TDEListView*,TQListViewItem*, const TQPoint&)));
// Register with DCOP - No longer needed as now we are a kcontrol module?
if ( !kapp->dcopClient()->isRegistered() ) {
kapp->dcopClient()->registerAs( "knetworkconf" );
kapp->dcopClient()->setDefaultObject( objId() );
}
//Temporarly added while gst supports routing option.
// cbEnableRouting->hide();
}
KNetworkConf::~KNetworkConf()
{
delete config;
}
void KNetworkConf::getNetworkInfoSlot()
{
netInfo = config->getNetworkInfo();
if (netInfo == NULL)
{
KMessageBox::error(this,
i18n("Could not load network configuration information."),
i18n("Error Reading Configuration File"));
//kapp->quit();
//exit(1);
}
else
{
//TQPtrList<KNetworkInterface> deviceList;
routingInfo = netInfo->getRoutingInfo();
dnsInfo = netInfo->getDNSInfo();
profilesList = netInfo->getProfilesList();
loadNetworkDevicesInfo();
loadRoutingInfo();
loadDNSInfo();
loadNetworkProfiles();
nameServersModified = false;
devicesModified = false;
modified = false;
// kpbApply->setEnabled(false);
}
}
/**
Fill the Listview with the info of the network interfaces.
*/
void KNetworkConf::loadNetworkDevicesInfo()
{
KNetworkInterface *device;
TQPixmap activeEthernetDeviceImg(locate("icon","hicolor/22x22/actions/network_connected_lan_knc.png"));
TQPixmap inactiveEthernetDeviceImg(locate("icon","hicolor/22x22/actions/network_disconnected_lan.png"));
TQPixmap activeWirelessDeviceImg(locate("icon","hicolor/22x22/actions/network_traffic_wlan.png"));
TQPixmap inactiveWirelessDeviceImg(locate("icon","hicolor/22x22/actions/network_disconnected_wlan.png"));
klvCardList->clear();
TQPtrList<KNetworkInterface> deviceList = netInfo->getDeviceList();
for (device = deviceList.first(); device; device = deviceList.next())
{
if ( device->getType() != "loopback" )
{
if (klvCardList->findItem(device->getDeviceName(),0,CaseSensitive|ExactMatch) == 0)
{
TQListViewItem * item = new TQListViewItem( klvCardList, 0 );
if (device->isActive())
{
if (device->getType() == "ethernet")
item->setPixmap(0,activeEthernetDeviceImg);
else if (device->getType() == "wireless")
item->setPixmap(0,activeWirelessDeviceImg);
item->setText(3,i18n("Enabled"));
item->setPixmap(3,SmallIcon("ok"));
}
else
{
if (device->getType() == "ethernet")
item->setPixmap(0,inactiveEthernetDeviceImg);
else if (device->getType() == "wireless")
item->setPixmap(0,inactiveWirelessDeviceImg);
item->setText(3,i18n("Disabled"));
item->setPixmap(3,SmallIcon("process-stop"));
if (device->getBootProto().lower() == "dhcp")
item->setText(1,"");
}
item->setText(0,device->getDeviceName());
item->setText(1,device->getIpAddress());
if (device->getBootProto() == "none")
item->setText(2,i18n("Manual"));
else
item->setText(2,device->getBootProto());
item->setText(4,device->getDescription());
TQStringList l = deviceNamesList.grep(device->getDeviceName());
if (l.size() == 0)
deviceNamesList.append(device->getDeviceName());
}
}
}
}
/** Terminates the application
*/
void KNetworkConf::quitSlot(){
int code = 0;
connect( config, TQT_SIGNAL(readyLoadingNetworkInfo()), this, TQT_SLOT(quitSlot()) );
if (modified)
{
code = KMessageBox::warningYesNoCancel(this,
i18n("The new configuration has not been saved.\nDo you want to apply changes before quitting?"),
i18n("New Configuration Not Saved"),KStdGuiItem::apply(),KStdGuiItem::quit());
if (code == KMessageBox::Yes)
saveInfoSlot();
else if (code == KMessageBox::No)
kapp->quit();
}
else
kapp->quit();
}
/** Enables the configure and remove buttons. */
void KNetworkConf::enableButtonsSlot(){
if (!readOnly)
{
kpbConfigureNetworkInterface->setEnabled(true);
TQListViewItem *item = klvCardList->currentItem();
TQString currentDevice = item->text(0);
KNetworkInterface *dev = getDeviceInfo(currentDevice);
if (dev->isActive())
{
kpbUpButton->setEnabled(false);
kpbDownButton->setEnabled(true);
}
else
{
kpbUpButton->setEnabled(true);
kpbDownButton->setEnabled(false);
}
}
}
/** opens the add server dialog. */
void KNetworkConf::addServerSlot(){
KAddDNSServerDlg addDlg(this, 0);
addDlg.exec();
if(addDlg.modified())
{
klbDomainServerList->insertItem(addDlg.kleNewServer->text());
nameServersModified = true;
enableApplyButtonSlot();
}
}
/** opens the edit server dialog. */
void KNetworkConf::editServerSlot(){
KAddDNSServerDlg dlg(this, 0);
if (klbDomainServerList->currentItem() >= 0)
{
int currentPos = klbDomainServerList->currentItem();
dlg.setCaption(i18n("Edit Server"));
TQListBoxItem *item = klbDomainServerList->item(currentPos);
TQString currentText = item->text();
dlg.kleNewServer->setText(currentText);
dlg.kpbAddServer->setText(i18n("&OK"));
dlg.exec();
if(dlg.modified())
{
klbDomainServerList->changeItem(dlg.kleNewServer->text(),currentPos);
nameServersModified = true;
enableApplyButtonSlot();
}
}
}
/** Pops up the window for adding a new interface. */
void KNetworkConf::configureDeviceSlot(){
TQListViewItem *item = klvCardList->currentItem();
KWirelessInterface *wifiDev = NULL;
//KAddDeviceContainer *configDlg = new KAddDeviceContainer(this,0);
KAddDeviceContainer configDlg(this,0);
if (item != NULL)
{
TQString currentDevice = item->text(0);
configDlg.setCaption(i18n("Configure Device %1").arg(currentDevice));
KNetworkInterface *dev = getDeviceInfo(currentDevice);
KAddDeviceDlgExtension *advancedOptions = (KAddDeviceDlgExtension *)configDlg.extension();
if (dev->getBootProto() == "none")
{
configDlg.addDlg->rbBootProtoManual->setChecked(true);
configDlg.addDlg->rbBootProtoAuto->setChecked(false);
}
else if (dev->getBootProto().lower() == "dhcp")
{
configDlg.addDlg->kcbAutoBootProto->setCurrentItem(0);
configDlg.addDlg->rbBootProtoManual->setChecked(false);
configDlg.addDlg->rbBootProtoAuto->setChecked(true);
configDlg.addDlg->kleIPAddress->setEnabled(false);
configDlg.addDlg->kcbNetmask->setEnabled(false);
configDlg.kpbAdvanced->setEnabled(false);
}
else if (dev->getBootProto().lower() == "bootp")
{
configDlg.addDlg->kcbAutoBootProto->setCurrentItem(1);
configDlg.addDlg->rbBootProtoManual->setChecked(false);
configDlg.addDlg->rbBootProtoAuto->setChecked(true);
configDlg.kpbAdvanced->setEnabled(false);
}
if (dev->getOnBoot() == "yes")
configDlg.addDlg->kcbstartAtBoot->setChecked(true);
else
{
configDlg.addDlg->kcbstartAtBoot->setChecked(false);
}
configDlg.addDlg->kleIPAddress->setText(dev->getIpAddress());
advancedOptions->kleDescription->setText(dev->getDescription());
if (!dev->getBroadcast().isEmpty())
advancedOptions->kleBroadcast->setText(dev->getBroadcast());
else
advancedOptions->kleBroadcast->setText(KAddressValidator::calculateBroadcast(dev->getIpAddress(),dev->getNetmask()));
advancedOptions->kleGateway->setText(dev->getGateway());
if (!dev->getNetmask().isEmpty())
configDlg.addDlg->kcbNetmask->setCurrentText(dev->getNetmask());
if (readOnly)
{
configDlg.addDlg->kcbAutoBootProto->setEnabled(false);
configDlg.addDlg->kcbstartAtBoot->setEnabled(false);
advancedOptions->gbAdvancedDeviceInfo->setEnabled(false);
}
//If the interface is wireless, then add the wireless configuration widget
if (dev->getType() == WIRELESS_IFACE_TYPE){
wifiDev = static_cast<KWirelessInterface*>(dev);
configDlg.addWirelessWidget();
configDlg.extDlg->kleEssid->setText(wifiDev->getEssid());
configDlg.extDlg->kleWepKey->setText(wifiDev->getWepKey());
if (wifiDev->getKeyType() == WIRELESS_WEP_KEY_TYPE_ASCII)
configDlg.extDlg->qcbKeyType->setCurrentItem(0);
else if (wifiDev->getKeyType() == WIRELESS_WEP_KEY_TYPE_HEXADECIMAL)
configDlg.extDlg->qcbKeyType->setCurrentItem(1);
}
configDlg.addButtons();
//Disable Apply button so it only is enabled when a change is made
configDlg.kpbApply->setEnabled(false);
configDlg.exec();
if (configDlg.modified())
{
if(configDlg.addDlg->rbBootProtoManual->isChecked())
{
item->setText(2,i18n("Manual"));
dev->setBootProto("none");
}
//If the selected boot protocol is dhcp or bootp (Auto), then we don't need the
//past IP address, netmask, network and broadcast, as a new one will be assigned by
//the dhcp server.
else if (configDlg.addDlg->rbBootProtoAuto->isChecked())
{
if (configDlg.addDlg->kcbAutoBootProto->currentText() != dev->getBootProto())
{
dev->setIpAddress("");
configDlg.addDlg->kleIPAddress->setText("");
dev->setGateway("");
dev->setNetmask("");
dev->setNetwork("");
dev->setBroadcast("");
}
item->setText(2,configDlg.addDlg->kcbAutoBootProto->currentText());
dev->setBootProto(configDlg.addDlg->kcbAutoBootProto->currentText());
}
item->setText(1,configDlg.addDlg->kleIPAddress->text());
item->setText(4,advancedOptions->kleDescription->text());
if (valuesChanged(dev,
configDlg.addDlg->kcbAutoBootProto->currentText(),
configDlg.addDlg->kcbNetmask->currentText(),
configDlg.addDlg->kleIPAddress->text(),
advancedOptions->kleGateway->text(),
configDlg.addDlg->kcbstartAtBoot->isChecked(),
advancedOptions->kleDescription->text(),
advancedOptions->kleBroadcast->text()))
{
dev->setIpAddress(configDlg.addDlg->kleIPAddress->text().stripWhiteSpace());
dev->setGateway(advancedOptions->kleGateway->text().stripWhiteSpace());
dev->setNetmask(configDlg.addDlg->kcbNetmask->currentText().stripWhiteSpace());
TQString network = KAddressValidator::calculateNetwork(dev->getIpAddress().stripWhiteSpace(),dev->getNetmask().stripWhiteSpace());
dev->setNetwork(network);
TQString broadcast = advancedOptions->kleBroadcast->text().stripWhiteSpace();
if (broadcast.isEmpty())
broadcast = KAddressValidator::calculateBroadcast(dev->getIpAddress().stripWhiteSpace(),dev->getNetmask().stripWhiteSpace());
dev->setBroadcast(broadcast);
dev->setDescription(advancedOptions->kleDescription->text());
if (configDlg.addDlg->kcbstartAtBoot->isChecked())
dev->setOnBoot("yes");
else
dev->setOnBoot("no");
}
//If the interface is wireless, then save the wireless configuration options
if (dev->getType() == WIRELESS_IFACE_TYPE){
wifiDev->setEssid(configDlg.extDlg->kleEssid->text());
wifiDev->setWepKey(configDlg.extDlg->kleWepKey->password());
wifiDev->setKeyType(configDlg.extDlg->qcbKeyType->currentText());
dev = wifiDev;
}
devicesModified = true;
enableApplyButtonSlot();
}
}
}
/**Returns the info of the network device 'device or NULL if not found.'*/
KNetworkInterface * KNetworkConf::getDeviceInfo(TQString device){
TQPtrList<KNetworkInterface> deviceList = netInfo->getDeviceList();
TQPtrListIterator<KNetworkInterface> i(deviceList);
KNetworkInterface *temp;
while ((temp = i.current()) != 0)
{
if (temp->getDeviceName() == device)
{
return temp;
}
++i;
}
return NULL;
}
/**Returns the name of the network device that corresponds to the IP address 'ipAddr' or NULL if not found.'*/
TQString KNetworkConf::getDeviceName(TQString ipAddr){
TQPtrList<KNetworkInterface> deviceList = netInfo->getDeviceList();
TQPtrListIterator<KNetworkInterface> i(deviceList);
KNetworkInterface *temp;
while ((temp = i.current()) != 0)
{
if (temp->getIpAddress().compare(ipAddr) == 0)
{
return temp->getDeviceName();
}
++i;
}
return NULL;
}
/** Looks in the output returned by ifconfig to see if there are the devices up or down.*/
void KNetworkConf::readFromStdout(){
commandOutput = "";
commandOutput += procUpdateDevice->readStdout();
}
/** Loads the info about the default gateway and host and domain names. */
void KNetworkConf::loadRoutingInfo(){
//routingInfo = config->getNetworkInfoSlot();
if (!routingInfo->getGateway().isEmpty())
kleDefaultRoute->setText(routingInfo->getGateway());
else
{
//Take the default gateway from the gateway field of the default gateway interface
//because some platforms (Debian-like ones) seems that don't handle the concept of a default
//gateway, instead a gateway per interface.
KNetworkInterface *device;
TQString defaultGwDevice = routingInfo->getGatewayDevice();
TQPtrList<KNetworkInterface> deviceList = netInfo->getDeviceList();
for (device = deviceList.first(); device; device = deviceList.next())
{
if ( device->getDeviceName() == defaultGwDevice )
{
if ( !device->getGateway().isEmpty() )
{
kleDefaultRoute->setText(device->getGateway());
}
}
}
}
kcbGwDevice->clear();
kcbGwDevice->insertStringList(deviceNamesList);
if (!routingInfo->getGatewayDevice().isEmpty())
kcbGwDevice->setCurrentText(routingInfo->getGatewayDevice());
/* if (routingInfo->isForwardIPv4Enabled().compare("yes") == 0)
cbEnableRouting->setChecked(true);
else
cbEnableRouting->setChecked(false);*/
}
void KNetworkConf::loadDNSInfo(){
TQStringList nameServers;
if (dnsInfo == NULL)
KMessageBox::error(this,i18n("Could not open file '/etc/resolv.conf' for reading."),
i18n("Error Loading Config Files"));
else
{
kleHostName->setText(dnsInfo->getMachineName());
kleDomainName->setText(dnsInfo->getDomainName());
klbDomainServerList->clear();
nameServers = dnsInfo->getNameServers();
for ( TQStringList::Iterator it = nameServers.begin(); it != nameServers.end(); ++it)
{
klbDomainServerList->insertItem(*it);
}
klvKnownHosts->clear();
knownHostsList = dnsInfo->getKnownHostsList();
TQPtrListIterator<KKnownHostInfo> it(knownHostsList);
KKnownHostInfo *host;
while ((host = it.current()) != 0)
{
++it;
if (!(host->getIpAddress().isEmpty()))
{
TQListViewItem * item = new TQListViewItem( klvKnownHosts, 0 );
item->setText(0,host->getIpAddress());
TQStringList aliasesList = host->getAliases();
TQString aliases;
for ( TQStringList::Iterator it = aliasesList.begin(); it != aliasesList.end(); ++it )
{
aliases += *it+" ";
}
item->setText(1,aliases);
}
}
}
}
void KNetworkConf::loadNetworkProfiles(){
TQPtrListIterator<KNetworkInfo> it(profilesList);
KNetworkInfo *profile = NULL;
klvProfilesList->clear();
while ((profile = it.current()) != 0)
{
++it;
if (!profile->getProfileName().isEmpty())
{
TQListViewItem * item = new TQListViewItem( klvProfilesList, 0 );
item->setText(0,profile->getProfileName());
}
}
}
/** Shows the help browser. Hopefully some day it will be one :-). */
void KNetworkConf::helpSlot(){
kapp->invokeHelp();
}
/** No descriptions */
void KNetworkConf::aboutSlot(){
TDEAboutApplication *about = new TDEAboutApplication(kapp->aboutData(),0);
// about->setLogo(locate("icon","knetworkconf.png"));
//tqDebug("locate icon= %s",locate("icon","knetworkconf.png").latin1());
about->show();
}
/** No descriptions */
void KNetworkConf::enableApplyButtonSlot(){
//if (!readOnly)
//kpbApply->setEnabled(true);
modified = true;
emit networkStateChanged(true);
}
/** Puts the application in read-only mode. This happens when the user runing
the application t root. */
void KNetworkConf::setReadOnly(bool state){
KNetworkConf::readOnly = state;
}
/** No descriptions */
void KNetworkConf::enableApplyButtonSlot(const TQString &text){
Q_UNUSED(text)
enableApplyButtonSlot();
}
/** No descriptions */
void KNetworkConf::enableApplyButtonSlot(bool){
enableApplyButtonSlot();
}
/** No descriptions */
void KNetworkConf::removeServerSlot(){
if (klbDomainServerList->currentItem() >= 0)
{
klbDomainServerList->removeItem(klbDomainServerList->currentItem());
enableApplyButtonSlot();
}
}
void KNetworkConf::moveUpServerSlot(){
int curPos = klbDomainServerList->currentItem();
int antPos = klbDomainServerList->currentItem() - 1;
if (antPos >= 0)
{
TQListBoxItem *current = klbDomainServerList->item(curPos);
TQListBoxItem *ant = current->prev();
TQString antText = ant->text();
klbDomainServerList->removeItem(antPos);
klbDomainServerList->insertItem(antText,curPos);
enableApplyButtonSlot();
}
}
void KNetworkConf::moveDownServerSlot(){
int curPos = klbDomainServerList->currentItem();
unsigned nextPos = klbDomainServerList->currentItem() + 1;
if (curPos != -1)
{
if (klbDomainServerList->count() >= nextPos)
{
TQListBoxItem *current = klbDomainServerList->item(curPos);
TQString curText = current->text();
klbDomainServerList->removeItem(curPos);
klbDomainServerList->insertItem(curText,nextPos);
klbDomainServerList->setSelected(nextPos,true);
enableApplyButtonSlot();
}
}
}
/** Disables all buttons a line edit widgets when the user has read only access. */
void KNetworkConf::disableAll(){
kleHostName->setReadOnly(true);
kleDomainName->setReadOnly(true);
tlDomainName->setEnabled(false);
tlHostName->setEnabled(false);
disconnect(klvCardList,TQT_SIGNAL(doubleClicked(TQListViewItem *)),this,TQT_SLOT(configureDeviceSlot()));
klvCardList->setEnabled(false);
kpbUpButton->setEnabled(false);
kpbDownButton->setEnabled(false);
kpbConfigureNetworkInterface->setEnabled(false);
gbDefaultGateway->setEnabled(false);
gbDNSServersList->setEnabled(false);
gbKnownHostsList->setEnabled(false);
// gbNetworkOptions->setEnabled(false);
}
/** Saves all the modified info of devices, routes,etc. */
void KNetworkConf::saveInfoSlot(){
config->setProgramVersion(getVersion());
if (!KAddressValidator::isValidIPAddress(kleDefaultRoute->text()) && (!(kleDefaultRoute->text().isEmpty())))
{
KMessageBox::error(this,i18n("The default Gateway IP address is invalid."),i18n("Invalid IP Address"));
}
else
{
//Update DNS info
routingInfo->setDomainName(kleDomainName->text());
routingInfo->setHostName(kleHostName->text());
dnsInfo->setDomainName(kleDomainName->text());
dnsInfo->setMachineName(kleHostName->text());
dnsInfo->setNameServers(getNamserversList(klbDomainServerList));
dnsInfo->setKnownHostsList(getKnownHostsList(klvKnownHosts));
//Update routing info
routingInfo->setGateway(kleDefaultRoute->text());
if (routingInfo->getGateway().isEmpty())
routingInfo->setGatewayDevice("");
if (!kleDefaultRoute->text().isEmpty())
routingInfo->setGatewayDevice(kcbGwDevice->currentText());
//Save all info
//netInfo->setDeviceList(deviceList);
netInfo->setRoutingInfo(routingInfo);
netInfo->setDNSInfo(dnsInfo);
//Add the default gateway to the gateway field of the default gateway interface
//because some platforms (Debian-like ones) get the default gateway from there
//instead from the default gateway field. funny huh?
KNetworkInterface *device;
TQString defaultGwDevice = routingInfo->getGatewayDevice();
TQString defaultGwAddress = routingInfo->getGateway();
TQPtrList<KNetworkInterface> deviceList = netInfo->getDeviceList();
for (device = deviceList.first(); device; device = deviceList.next())
{
if ( device->getGateway().length() == 0 )
{
if ( device->getDeviceName() == defaultGwDevice )
{
device->setGateway(defaultGwAddress);
}
}
}
config->saveNetworkInfo(netInfo);
modified = false;
}
}
/** Creates a TQStringList with the IP addresses contained in the TQListBox of name servers. */
TQStringList KNetworkConf::getNamserversList(TDEListBox * serverList){
TQStringList list;
for (unsigned i = 0; i < serverList->count(); i++)
{
list.append(serverList->text(i));
}
return list;
}
/** Creates a TQPtrList<KKownHostInfo> with the info contained in the TDEListView of name servers. */
TQPtrList<KKnownHostInfo> KNetworkConf::getKnownHostsList(TDEListView * hostsList){
TQPtrList<KKnownHostInfo> list;
TQListViewItem *it = hostsList->firstChild();
for (int i = 0; i < hostsList->childCount(); i++)
{
KKnownHostInfo *host = new KKnownHostInfo();
if (!(it->text(0).isEmpty()))
{
host->setIpAddress(it->text(0));
// host->setHostName(it->text(1));
host->setAliases(TQStringList::split(" ",it->text(1)));
it = it->nextSibling();
list.append(host);
}
}
return list;
}
TQString KNetworkConf::getVersion(){
return version;
}
void KNetworkConf::setVersion(TQString ver){
KNetworkConf::version = ver;
}
/** Changes the state of device 'dev' to DEVICE_UP or DEVICE_DOWN.
Return true on success, false on failure. */
void KNetworkConf::changeDeviceState(const TQString &dev, int state){
// If the text "Changing device state" is user visible it cannot be the
// name parameter to the constructor.
KInterfaceUpDownDlg* dialog = new KInterfaceUpDownDlg(this,"Changing device state");
if (state == DEVICE_UP)
dialog->label->setText(i18n("Enabling interface <b>%1</b>").arg(dev));
else
dialog->label->setText(i18n("Disabling interface <b>%1</b>").arg(dev));
dialog->setModal(true);
dialog->show();
procDeviceState = new TQProcess(TQT_TQOBJECT(this));
TQString cmd;
procDeviceState->addArgument( locate("data",BACKEND_PATH) );
//If the platform couldn't be autodetected specify it manually
if (netInfo->getPlatformName() != TQString())
{
procDeviceState->addArgument( "--platform" );
procDeviceState->addArgument( netInfo->getPlatformName() );
}
procDeviceState->addArgument( "-d" );
if (state == DEVICE_UP)
procDeviceState->addArgument("enable_iface::"+dev+"::1" );
else if (state == DEVICE_DOWN)
procDeviceState->addArgument("enable_iface::"+dev+"::0" );
connect( procDeviceState, TQT_SIGNAL(readyReadStdout()),this, TQT_SLOT(readFromStdoutUpDown()) );
connect( procDeviceState, TQT_SIGNAL(readyReadStderr()),this, TQT_SLOT(readFromStdErrUpDown()) );
connect( procDeviceState, TQT_SIGNAL(processExited()),this, TQT_SLOT(verifyDeviceStateChanged()) );
connect( procDeviceState, TQT_SIGNAL(processExited()), dialog, TQT_SLOT(close()) );
currentDevice = dev;
commandOutput = "";
if ( !procDeviceState->start() )
{
// error handling
KMessageBox::error(this,
i18n("Could not launch backend to change network device state. You will have to do it manually."),
i18n("Error"));
dialog->close();
}
}
void KNetworkConf::readFromStdoutUpDown(){
commandOutput.append(procDeviceState->readStdout());
}
void KNetworkConf::verifyDeviceStateChanged(){
KNetworkInterface *dev;
TQPixmap activeEthernetDeviceImg(BarIcon("network_connected_lan_knc"));
TQPixmap inactiveEthernetDeviceImg(BarIcon("network_disconnected_lan"));
TQPixmap activeWirelessDeviceImg(BarIcon("network_traffic_wlan"));
TQPixmap inactiveWirelessDeviceImg(BarIcon("network_disconnected_wlan"));
commandOutput = commandOutput.section('\n',1);
if (commandErrOutput.length() > 0)
{
KMessageBox::error(this,
i18n("There was an error changing the device's state. You will have to do it manually."),
i18n("Could Not Change Device State"));
}
else if (commandOutput == "\n<!-- GST: end of request -->")
{
TQListViewItem *item = klvCardList->findItem(currentDevice,0,ExactMatch);
if (item != NULL)
{
dev = getDeviceInfo(currentDevice);
if (!dev->isActive())
{
dev->setActive(true);
if (dev->getType() == "ethernet")
item->setPixmap(0,activeEthernetDeviceImg);
else if (dev->getType() == "wireless")
item->setPixmap(0,activeWirelessDeviceImg);
item->setText(3,i18n("Enabled"));
item->setPixmap(3,SmallIcon("ok"));
// config->runDetectionScript(netInfo->getPlatformName());
config->listIfaces(netInfo->getPlatformName());
// item->setText(1,dev->getIpAddress());
}
else
{
dev->setActive(false);
if (dev->getType() == "ethernet")
item->setPixmap(0,inactiveEthernetDeviceImg);
else if (dev->getType() == "wireless")
item->setPixmap(0,inactiveWirelessDeviceImg);
item->setText(3,i18n("Disabled"));
item->setPixmap(3,SmallIcon("process-stop"));
if (dev->getBootProto().lower() == "dhcp")
item->setText(1,"");
}
enableButtonsSlot();
}
}
}
/** Returns a list of strings of all the configured devices. */
TQStringList KNetworkConf::getDeviceList(){
TQStringList list;
KNetworkInterface * device;
TQPtrList<KNetworkInterface> deviceList = netInfo->getDeviceList();
for (device = deviceList.first(); device; device = deviceList.next())
{
list.append(device->getDeviceName());
}
return list;
}
/** No descriptions */
bool KNetworkConf::valuesChanged(KNetworkInterface *dev,
TQString bootProto,
TQString netmask,
TQString ipAddr,
TQString gateway,
bool onBoot,
TQString desc,
TQString broadcast){
if ((dev->getBootProto() != bootProto) ||
(dev->getNetmask() != netmask) ||
(dev->getIpAddress() != ipAddr) ||
(dev->getGateway() != gateway) ||
((dev->getOnBoot() == "yes") && !(onBoot)) ||
((dev->getOnBoot() == "no") && (onBoot)) ||
(dev->getDescription() != desc) ||
(dev->getBroadcast() != broadcast))
return true;
else
return false;
}
/** Sets the TQPushButton::autoResize() in true for all buttons. */
void KNetworkConf::makeButtonsResizeable(){
kpbConfigureNetworkInterface->setAutoResize(true);
kcbGwDevice->setAutoResize(true);
kpbAddDomainServer->setAutoResize(true);
kpbEditDomainServer->setAutoResize(true);
kpbRemoveDomainServer->setAutoResize(true);
kpbUpButton->setAutoResize(true);
kpbDownButton->setAutoResize(true);
kpbAddKnownHost->setAutoResize(true);
kpbEditKnownHost->setAutoResize(true);
kpbRemoveKnownHost->setAutoResize(true);
}
void KNetworkConf::enableInterfaceSlot()
{
if (modified) {
if (KMessageBox::warningContinueCancel(this,
i18n("The new configuration has not been saved.\nApply changes?"),
i18n("New Configuration Not Saved"),
KStdGuiItem::apply()) == KMessageBox::Continue)
saveInfoSlot();
else
return;
}
KNetworkInterface *dev = getDeviceInfo(klvCardList->currentItem()->text(0));
if (dev->isActive())
changeDeviceState(dev->getDeviceName(),DEVICE_DOWN);
else
changeDeviceState(dev->getDeviceName(),DEVICE_UP);
}
void KNetworkConf::disableInterfaceSlot()
{
if (modified) {
if (KMessageBox::warningContinueCancel(this,
i18n("The new configuration has not been saved.\nApply changes?"),
i18n("New Configuration Not Saved"),
KStdGuiItem::apply()) == KMessageBox::Continue)
saveInfoSlot();
else
return;
}
KNetworkInterface *dev = getDeviceInfo(klvCardList->currentItem()->text(0));
if (dev->isActive())
changeDeviceState(dev->getDeviceName(),DEVICE_DOWN);
else
changeDeviceState(dev->getDeviceName(),DEVICE_UP);
}
/** Adds a new host to the TDEListView that has the known hosts. */
void KNetworkConf::addKnownHostSlot(){
KAddKnownHostDlg dlg(this,0);
dlg.setCaption(i18n("Add New Static Host"));
TQString aliases;
dlg.exec();
if (!dlg.kleIpAddress->text().isEmpty() && dlg.klbAliases->firstItem() )
{
TQListViewItem * item = new TQListViewItem( klvKnownHosts, 0 );
item->setText(0,dlg.kleIpAddress->text());
for ( uint i = 0; i < dlg.klbAliases->count(); i++ )
aliases += dlg.klbAliases->text(i) + " ";
item->setText(1,aliases);
enableApplyButtonSlot();
}
}
/** Removes a known host from the list view */
void KNetworkConf::removeKnownHostSlot()
{
if (klvKnownHosts->currentItem() != 0)
{
klvKnownHosts->removeItem(klvKnownHosts->currentItem());
enableApplyButtonSlot();
}
}
/** Edits the info about a known host. */
void KNetworkConf::editKnownHostSlot()
{
KAddKnownHostDlg dlg(this,0);
dlg.setCaption(i18n("Edit Static Host"));
TQListViewItem *item = klvKnownHosts->currentItem();
dlg.kleIpAddress->setText(item->text(0));
TQStringList aliases = TQStringList::split( " ", item->text(1) );
int n = 0;
for ( TQStringList::Iterator it = aliases.begin(); it != aliases.end(); ++it, ++n )
{
TQString alias = *it;
dlg.klbAliases->insertItem(alias,n);
}
dlg.exec();
TQString _aliases;
if (!dlg.kleIpAddress->text().isEmpty() && dlg.klbAliases->firstItem() )
{
TQListViewItem * item = klvKnownHosts->currentItem();
item->setText(0,dlg.kleIpAddress->text());
for ( uint i = 0; i < dlg.klbAliases->count(); i++ )
_aliases += dlg.klbAliases->text(i) + " ";
item->setText(1,_aliases);
enableApplyButtonSlot();
}
}
/** Shows the main window after the network info has been loaded. */
void KNetworkConf::showMainWindow()
{
show();
}
/** No descriptions */
void KNetworkConf::readFromStdErrUpDown()
{
commandErrOutput.append(procDeviceState->readStderr());
}
/** Sees if a device is active or not in the ifconfig output. Not very nice, but it works. Inthe future, this has to be managed by gst. */
bool KNetworkConf::isDeviceActive(const TQString &device, const TQString &ifconfigOutput){
TQString temp = ifconfigOutput.section(device,1,1);
if (temp.isEmpty())
return false;
else
{
TQString temp2 = temp.section("UP",0,0); //two firts lines of the device info.
TQString temp3 = temp2.section("\n",0,0); //Link encap:Ethernet HWaddr 00:00:21:C5:99:A0
TQString temp4 = temp2.section("\n",1,1); //inet addr:192.168.1.1 Bcast:192.255.255.255 Mask:255.0.0.0
temp3 = temp3.stripWhiteSpace();
temp4 = temp4.stripWhiteSpace();
TQString temp5 = temp3.section(" ",4,4); //00:00:21:C5:99:A0
TQString temp6 = temp4.section(" ",1,1); // addr:192.168.1.1
temp6 = temp6.section(":",1,1); //192.168.1.1
TQString temp7 = temp4.section(" ",3,3); //Bcast:192.255.255.255
temp7 = temp7.section(":",1,1); //192.255.255.255
TQString temp8 = temp4.section(" ",5,5); // Mask:255.0.0.0
temp8 = temp8.section(":",1,1); //255.0.0.0
//If the ip address is empty it must be a dhcp interface, so fill these fields:
if (temp6.isEmpty())
return false;
}
return true;
}
void KNetworkConf::setReadOnlySlot(bool state)
{
state = !state;
gbDefaultGateway->setEnabled(state);
kleDomainName->setEnabled(state);
kleHostName->setEnabled(state);
gbDNSServersList->setEnabled(state);
gbKnownHostsList->setEnabled(state);
klvCardList->setEnabled(state);
kpbUpButton->setEnabled(state);
kpbDownButton->setEnabled(state);
kpbConfigureNetworkInterface->setEnabled(state);
}
/*Shows a context menu when right-clicking in the interface list*/
void KNetworkConf::showInterfaceContextMenuSlot(TDEListView* lv, TQListViewItem* lvi, const TQPoint& pt)
{
Q_UNUSED(lv)
Q_UNUSED(lvi)
TDEPopupMenu *context = new TDEPopupMenu( this );
TQ_CHECK_PTR( context );
context->insertItem( "&Enable Interface", this, TQT_SLOT(enableInterfaceSlot()));
context->insertItem( "&Disable Interface", this, TQT_SLOT(disableInterfaceSlot()));
TQListViewItem *item = klvCardList->currentItem();
TQString currentDevice = item->text(0);
KNetworkInterface *dev = getDeviceInfo(currentDevice);
if (dev->isActive())
{
context->setItemEnabled(0,false);
context->setItemEnabled(1,true);
}
else
{
context->setItemEnabled(0,true);
context->setItemEnabled(1,false);
}
context->insertSeparator(2);
context->insertItem( "&Configure Interface...", this, TQT_SLOT(configureDeviceSlot()));
context->popup(pt);
//context->insertItem( "About &TQt", this, TQT_SLOT(aboutTQt()) );
}
void KNetworkConf::enableSignals()
{
tooltip->setProfiles(netInfo->getProfilesList());
connect(kleDefaultRoute,TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(enableApplyButtonSlot(const TQString&)));
connect(kleDomainName,TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(enableApplyButtonSlot(const TQString&)));
connect(kleHostName,TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(enableApplyButtonSlot(const TQString&)));
}
void KNetworkConf::enableProfileSlot()
{
//Get selected profile
TQListViewItem *item = klvProfilesList->currentItem();
if (item != NULL)
{
TQString selectedProfile = item->text(0);
//And search for it in the profiles list
KNetworkInfo *profile = getProfile(netInfo->getProfilesList(),selectedProfile);
if (profile != NULL)
{
profile->setProfilesList(netInfo->getProfilesList());
config->saveNetworkInfo(profile);
modified = false;
//connect( config, TQT_SIGNAL(readyLoadingNetworkInfo()), this, TQT_SLOT(showSelectedProfile(selectedProfile)) );
}
else
KMessageBox::error(this,
i18n("Could not load the selected Network Profile."),
i18n("Error Reading Profile"));
}
}
KNetworkInfo *KNetworkConf::getProfile(TQPtrList<KNetworkInfo> profilesList, TQString selectedProfile)
{
TQPtrListIterator<KNetworkInfo> it(profilesList);
KNetworkInfo *net = NULL;
while ((net = it.current()) != 0)
{
++it;
if (net->getProfileName() == selectedProfile)
break;
}
return net;
}
void KNetworkConf::createProfileSlot()
{
if (!netInfo)
return;
bool ok;
TQString newProfileName = KInputDialog::getText(i18n("Create New Network Profile"),
i18n("Name of new profile:"),
TQString(), &ok, this );
if ( ok && !newProfileName.isEmpty() )
{
TQPtrList<KNetworkInfo> profiles = netInfo->getProfilesList();
KNetworkInfo *currentProfile = getProfile(profiles,newProfileName);
KNetworkInfo *newProfile = new KNetworkInfo();
//If there isn't a profile with the new name we add it to the list.
if (currentProfile == NULL)
{
new TQListViewItem( klvProfilesList,newProfileName);
//memcpy(newProfile,netInfo,sizeof(netInfo) + sizeof(KRoutingInfo) + sizeof(KDNSInfo));
//Is there a better way to copy an object? the above memcpy doesn't do the trick
newProfile->setProfileName(newProfileName);
newProfile->setDNSInfo(netInfo->getDNSInfo());
newProfile->setDeviceList(netInfo->getDeviceList());
newProfile->setNetworkScript(netInfo->getNetworkScript());
newProfile->setPlatformName(netInfo->getPlatformName());
newProfile->setProfilesList(netInfo->getProfilesList());
newProfile->setRoutingInfo(netInfo->getRoutingInfo());
profiles.append(newProfile);
netInfo->setProfilesList(profiles);
enableApplyButtonSlot();
}
else
KMessageBox::error(this,
i18n("There is already another profile with that name."),
i18n("Error"));
}
}
/*void KNetworkConf::updateProfileNameSlot(TQListViewItem *item)
{
TQString newName = item->text(0);
if (newName.isEmpty())
KMessageBox::error(this,
i18n("The profile name can't be left blank."),
i18n("Error"));
else
{
KNetworkInfo *currentProfile = getProfile(netInfo->getProfilesList(),newName);
KNetworkInfo *newProfile = new KNetworkInfo();
//If there is a profile with that name we rename it to the new name.
if (currentProfile != NULL)
{
currentProfile->setProfileName(item->text(0));
modified = false;
enableApplyButtonSlot();
}
}
}*/
void KNetworkConf::removeProfileSlot()
{
TQListViewItem *item= klvProfilesList->selectedItem();
if (item != NULL)
{
/* if (KMessageBox::warningContinueCancel(this,
i18n("Are you sure you want to delete the selected network profile?"),
i18n("Delete Profile"),KStdGuiItem::del()) == KMessageBox::Continue)*/
{
TQString selectedProfile = item->text(0);
TQPtrList<KNetworkInfo> profiles = netInfo->getProfilesList();
KNetworkInfo *profileToDelete = NULL;
for ( profileToDelete = profiles.first(); profileToDelete; profileToDelete = profiles.next() )
{
TQString profileName = profileToDelete->getProfileName();
if (profileName == selectedProfile)
{
profiles.remove(profileToDelete);
netInfo->setProfilesList(profiles);
klvProfilesList->takeItem(item);
modified = false;
enableApplyButtonSlot();
break;
}
}
}
}
}
void KNetworkConf::updateProfileSlot()
{
TQListViewItem *item= klvProfilesList->selectedItem();
if (item != NULL)
{
TQString selectedProfile = item->text(0);
TQPtrList<KNetworkInfo> profiles = netInfo->getProfilesList();
KNetworkInfo *profileToUpdate = NULL;
KNetworkInfo *newProfile = new KNetworkInfo();
for ( profileToUpdate = profiles.first(); profileToUpdate; profileToUpdate = profiles.next() )
{
TQString profileName = profileToUpdate->getProfileName();
if (profileName == selectedProfile)
{
tqDebug("profile updated");
newProfile->setProfileName(profileName);
newProfile->setDNSInfo(netInfo->getDNSInfo());
newProfile->setDeviceList(netInfo->getDeviceList());
newProfile->setNetworkScript(netInfo->getNetworkScript());
newProfile->setPlatformName(netInfo->getPlatformName());
newProfile->setProfilesList(netInfo->getProfilesList());
newProfile->setRoutingInfo(netInfo->getRoutingInfo());
profileToUpdate = netInfo;
int curPos = profiles.at();
// profileToUpdate->setProfileName(profileName);
profiles.remove();
profiles.insert(curPos,newProfile);
netInfo->setProfilesList(profiles);
modified = false;
enableApplyButtonSlot();
break;
}
}
}
}
TQString KNetworkConf::handbookDocPath() const
{
int index = tabWidget->currentPageIndex();
if (index == 0)
return "knetworkconf/using-the-module.html#network-interfaces";
else if (index == 1)
return "knetworkconf/route.html";
else if (index == 2)
return "knetworkconf/dns.html";
else if (index == 3)
return "knetworkconf/profile.html";
else
return TQString::null;
}
#include "knetworkconf.moc"
|