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
|
/***************************************************************************
* Copyright (C) 2004 by Hugo Parente Lima *
* hugo_pl@users.sourceforge.net *
* *
* 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 "knetstats.h"
#include "knetstatsview.h"
#include "configure.h"
#include "interface.h"
// TQt includes
#include <tqstringlist.h>
#include <tqdir.h>
#include <tqtimer.h>
// KDE includes
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <tdemessagebox.h>
#include <tdeapplication.h>
#include <tdeaction.h>
#include <tdeactioncollection.h>
#include <tdestdaccel.h>
#include <kstdaction.h>
#include <khelpmenu.h>
#include <tdeaboutapplication.h>
#include <tdepopupmenu.h>
#include <kiconloader.h>
#include <kpushbutton.h>
KNetStats::KNetStats() : TQWidget(0, "knetstats"), mConfigure(0), mBoot(true) {
setupUi();
int count;
detectNewInterfaces(&count);
if (!count && numInterfaces()) // caos!!! All interfaces are invisible! turn all visible...
for(InterfaceMap::Iterator i = mInterfaces.begin(); i != mInterfaces.end(); ++i)
i.data()->setVisible(true);
mBoot = false;
TQTimer* timer = new TQTimer(this);
connect(timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(detectNewInterfaces()));
timer->start(IF_DETECTION_INTERVAL);
}
KNetStats::~KNetStats() {
for (InterfaceMap::Iterator it = mInterfaces.begin(); it != mInterfaces.end(); ++it)
delete it.data();
}
void KNetStats::setupUi() {
setIcon(kapp->icon());
// Cria o contextMenu
mActionCollection = new TDEActionCollection(this);
mContextMenu = new TDEPopupMenu(this);
mContextMenu->insertTitle( kapp->miniIcon(), kapp->caption() );
TDEAction* configure = KStdAction::preferences(TQT_TQOBJECT(this), TQT_SLOT(configure()), mActionCollection, "configure");
configure->plug(mContextMenu);
mContextMenu->insertSeparator();
KHelpMenu* helpmenu = new KHelpMenu(this, TQString(), false);
mContextMenu->insertItem( i18n("&Help"), helpmenu->menu() );
mContextMenu->insertSeparator();
TDEAction* quitAction = KStdAction::quit(TQT_TQOBJECT(kapp), TQT_SLOT(quit()), mActionCollection);
quitAction->plug(mContextMenu);
connect(helpmenu, TQT_SIGNAL(showAboutApplication()), this, TQT_SLOT(about()));
}
TQStringList KNetStats::scanInterfaces() {
TQDir dir("/sys/class/net");
if (!dir.exists()) {
KMessageBox::error(0, i18n("You need kernel 2.6.x with support to the /sys filesystem."));
return TQStringList();
} else {
TQStringList list = dir.entryList(TQDir::Dirs);
list.pop_front(); // removes "." and ".." entries
list.pop_front();
return list;
}
}
bool KNetStats::configure() {
if (mConfigure)
mConfigure->show();
else {
/* if (!mInterfaces.size()) {
KMessageBox::error(this, i18n("You don't have any network interface.\nKNetStats will quit now."));
return false;
}*/
mConfigure = new Configure(this, mInterfaces);
connect(mConfigure->mOk, TQT_SIGNAL(clicked()), this, TQT_SLOT(configOk()));
connect(mConfigure->mApply, TQT_SIGNAL(clicked()), this, TQT_SLOT(configApply()));
connect(mConfigure->mCancel, TQT_SIGNAL(clicked()), this, TQT_SLOT(configCancel()));
mConfigure->show();
}
return true;
}
void KNetStats::configOk() {
if (mConfigure->canSaveConfig()) {
saveConfig( mConfigure->options() );
delete mConfigure;
mConfigure = 0;
}
}
void KNetStats::configApply() {
if (mConfigure->canSaveConfig())
saveConfig( mConfigure->options() );
}
void KNetStats::configCancel() {
delete mConfigure;
mConfigure = 0;
}
void KNetStats::saveConfig(const OptionsMap& options) {
TDEConfig* cfg = kapp->config();
for(OptionsMap::ConstIterator i = options.begin(); i != options.end(); ++i) {
TDEConfigGroupSaver groupSaver(cfg, i.key());
const KNetStatsView::Options& opt = i.data();
// general
cfg->writeEntry("UpdateInterval", opt.mUpdateInterval);
cfg->writeEntry("ViewMode", opt.mViewMode);
cfg->writeEntry("Monitoring", opt.mMonitoring);
// txt view
cfg->writeEntry("TxtFont", opt.mTxtFont);
cfg->writeEntry("TxtUplColor", opt.mTxtUplColor);
cfg->writeEntry("TxtDldColor", opt.mTxtDldColor);
// IconView
cfg->writeEntry("Theme", opt.mTheme);
// Graphic view
cfg->writeEntry("ChartUplColor", opt.mChartUplColor);
cfg->writeEntry("ChartDldColor", opt.mChartDldColor);
cfg->writeEntry("ChartBgColor", opt.mChartBgColor);
cfg->writeEntry("ChartUseTransparentBackground", opt.mChartTransparentBackground);
}
for (InterfaceMap::ConstIterator i = mInterfaces.begin(); i != mInterfaces.end(); ++i)
i.data()->update();
}
void KNetStats::about() {
TDEAboutApplication dlg(this);
dlg.exec();
}
void KNetStats::detectNewInterfaces(int* count_ptr) {
int count = 0;
const TQStringList& interfaces = KNetStats::scanInterfaces();
TQStringList::ConstIterator i = interfaces.begin();
for(; i != interfaces.end(); ++i) {
InterfaceMap::Iterator elem = mInterfaces.find(*i);
if (elem == mInterfaces.end())
if (createInterface(*i))
count++;
}
if (count && count_ptr)
*count_ptr = count;
}
bool KNetStats::createInterface(const TQString& name) {
// Cria a interface e adiciona em mInterfaces.
Interface* interface = new Interface(this, name);
mInterfaces.insert(name, interface);
if (!mBoot)
interface->say(i18n("New interface detected: %1").arg(name));
return interface->isVisible();
}
#include "knetstats.moc"
|