vfs: check userland buffers before reading them.
[haiku.git] / src / bin / network / ppp_up / PPPStatusView.cpp
blob62e7c4c40d9b1a68b96fa098f136ce92c1ffcb19
1 /*
2 * Copyright 2005, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
4 */
6 #include "PPPStatusView.h"
8 #include <Box.h>
9 #include <Button.h>
10 #include <StringView.h>
11 #include <Window.h>
13 #include <cstdio>
14 #include <String.h>
16 #include <PPPManager.h>
19 // message constants
20 static const uint32 kMsgDisconnect = 'DISC';
22 // labels
23 static const char *kLabelDisconnect = "Disconnect";
24 static const char *kLabelConnectedSince = "Connected since: ";
25 static const char *kLabelReceived = "Received";
26 static const char *kLabelSent = "Sent";
28 // strings
29 static const char *kTextBytes = "Bytes";
30 static const char *kTextPackets = "Packets";
33 PPPStatusView::PPPStatusView(BRect rect, ppp_interface_id id)
34 : BView(rect, "PPPStatusView", B_FOLLOW_NONE, B_PULSE_NEEDED),
35 fInterface(id)
37 SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
39 rect = Bounds();
40 rect.InsetBy(5, 5);
41 rect.left = rect.right - 80;
42 rect.bottom = rect.top + 25;
43 fButton = new BButton(rect, "DisconnectButton", kLabelDisconnect,
44 new BMessage(kMsgDisconnect));
46 rect.right = rect.left - 10;
47 rect.left = rect.right - 80;
48 rect.top += 5;
49 rect.bottom = rect.top + 15;
50 fTime = new BStringView(rect, "Time", "");
51 fTime->SetAlignment(B_ALIGN_RIGHT);
52 fTime->SetFont(be_fixed_font);
53 rect.right = rect.left - 10;
54 rect.left = 5;
55 BStringView *connectedSince = new BStringView(rect, "ConnectedSince",
56 kLabelConnectedSince);
57 connectedSince->SetFont(be_fixed_font);
59 rect = Bounds();
60 rect.InsetBy(5, 5);
61 rect.top += 35;
62 rect.right = rect.left + (rect.Width() - 5) / 2;
63 BBox *received = new BBox(rect, "Received");
64 received->SetLabel(kLabelReceived);
65 rect = received->Bounds();
66 rect.InsetBy(10, 15);
67 rect.bottom = rect.top + 15;
68 fBytesReceived = new BStringView(rect, "BytesReceived", "");
69 fBytesReceived->SetAlignment(B_ALIGN_RIGHT);
70 fBytesReceived->SetFont(be_fixed_font);
71 rect.top = rect.bottom + 5;
72 rect.bottom = rect.top + 15;
73 fPacketsReceived = new BStringView(rect, "PacketsReceived", "");
74 fPacketsReceived->SetAlignment(B_ALIGN_RIGHT);
75 fPacketsReceived->SetFont(be_fixed_font);
77 rect = received->Frame();
78 rect.OffsetBy(rect.Width() + 5, 0);
79 BBox *sent = new BBox(rect, "sent");
80 sent->SetLabel(kLabelSent);
81 rect = received->Bounds();
82 rect.InsetBy(10, 15);
83 rect.bottom = rect.top + 15;
84 fBytesSent = new BStringView(rect, "BytesSent", "");
85 fBytesSent->SetAlignment(B_ALIGN_RIGHT);
86 fBytesSent->SetFont(be_fixed_font);
87 rect.top = rect.bottom + 5;
88 rect.bottom = rect.top + 15;
89 fPacketsSent = new BStringView(rect, "PacketsSent", "");
90 fPacketsSent->SetAlignment(B_ALIGN_RIGHT);
91 fPacketsSent->SetFont(be_fixed_font);
93 received->AddChild(fBytesReceived);
94 received->AddChild(fPacketsReceived);
95 sent->AddChild(fBytesSent);
96 sent->AddChild(fPacketsSent);
98 AddChild(fButton);
99 AddChild(fTime);
100 AddChild(connectedSince);
101 AddChild(received);
102 AddChild(sent);
104 ppp_interface_info_t info;
105 fInterface.GetInterfaceInfo(&info);
106 fConnectedSince = info.info.connectedSince;
110 void
111 PPPStatusView::AttachedToWindow()
113 fButton->SetTarget(this);
114 Window()->SetTitle(fInterface.Name());
118 void
119 PPPStatusView::MessageReceived(BMessage *message)
121 switch(message->what) {
122 case kMsgDisconnect:
123 fInterface.Down();
124 Window()->Hide();
125 break;
127 default:
128 BView::MessageReceived(message);
133 void
134 PPPStatusView::Pulse()
136 // update status
137 ppp_statistics statistics;
138 if(!fInterface.GetStatistics(&statistics)) {
139 fBytesReceived->SetText("");
140 fPacketsReceived->SetText("");
141 fBytesSent->SetText("");
142 fPacketsSent->SetText("");
143 return;
146 BString text;
147 bigtime_t time = system_time() - fConnectedSince;
148 time /= 1000000;
149 int32 seconds = time % 60;
150 time /= 60;
151 int32 minutes = time % 60;
152 int32 hours = time / 60;
153 char minsec[7];
154 if(hours) {
155 sprintf(minsec, ":%02" B_PRId32 ":%02" B_PRId32, minutes, seconds);
156 text << hours << minsec;
157 } else if(minutes) {
158 sprintf(minsec, "%" B_PRId32 ":%02" B_PRId32, minutes, seconds);
159 text << minsec;
160 } else
161 text << seconds;
162 fTime->SetText(text.String());
164 text = "";
165 text << statistics.bytesReceived << ' ' << kTextBytes;
166 fBytesReceived->SetText(text.String());
167 text = "";
168 text << statistics.packetsReceived << ' ' << kTextPackets;
169 fPacketsReceived->SetText(text.String());
170 text = "";
171 text << statistics.bytesSent << ' ' << kTextBytes;
172 fBytesSent->SetText(text.String());
173 text = "";
174 text << statistics.packetsSent << ' ' << kTextPackets;
175 fPacketsSent->SetText(text.String());