1/*
2 * Copyright 2005, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "PPPStatusView.h"
7
8#include <Box.h>
9#include <Button.h>
10#include <StringView.h>
11#include <Window.h>
12
13#include <cstdio>
14#include <String.h>
15
16#include <PPPManager.h>
17
18
19// message constants
20static const uint32 kMsgDisconnect = 'DISC';
21
22// labels
23static const char *kLabelDisconnect = "Disconnect";
24static const char *kLabelConnectedSince = "Connected since: ";
25static const char *kLabelReceived = "Received";
26static const char *kLabelSent = "Sent";
27
28// strings
29static const char *kTextBytes = "Bytes";
30static const char *kTextPackets = "Packets";
31
32
33PPPStatusView::PPPStatusView(BRect rect, ppp_interface_id id)
34	: BView(rect, "PPPStatusView", B_FOLLOW_NONE, B_PULSE_NEEDED),
35	fInterface(id)
36{
37	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
38
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));
45
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);
58
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);
76
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);
92
93	received->AddChild(fBytesReceived);
94	received->AddChild(fPacketsReceived);
95	sent->AddChild(fBytesSent);
96	sent->AddChild(fPacketsSent);
97
98	AddChild(fButton);
99	AddChild(fTime);
100	AddChild(connectedSince);
101	AddChild(received);
102	AddChild(sent);
103
104	ppp_interface_info_t info;
105	fInterface.GetInterfaceInfo(&info);
106	fConnectedSince = info.info.connectedSince;
107}
108
109
110void
111PPPStatusView::AttachedToWindow()
112{
113	fButton->SetTarget(this);
114	Window()->SetTitle(fInterface.Name());
115}
116
117
118void
119PPPStatusView::MessageReceived(BMessage *message)
120{
121	switch(message->what) {
122		case kMsgDisconnect:
123			fInterface.Down();
124			Window()->Hide();
125		break;
126
127		default:
128			BView::MessageReceived(message);
129	}
130}
131
132
133void
134PPPStatusView::Pulse()
135{
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;
144	}
145
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());
163
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());
176}
177