diff --git a/src/apps/bin/ppp_up/ConnectionView.cpp b/src/apps/bin/ppp_up/ConnectionView.cpp index 309f43665a..02ef257af6 100644 --- a/src/apps/bin/ppp_up/ConnectionView.cpp +++ b/src/apps/bin/ppp_up/ConnectionView.cpp @@ -33,6 +33,7 @@ static const char *kLabelName = "Username: "; static const char *kLabelPassword = "Password: "; static const char *kLabelConnect = "Connect"; static const char *kLabelCancel = "Cancel"; +static const char *kLabelAuthentication = "Authentication"; // connection status strings static const char *kTextConnecting = "Connecting..."; @@ -78,20 +79,16 @@ ConnectionView::ConnectionView(BRect rect, const char *name, ppp_interface_id id { SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); - BRect rect = Bounds(); + rect = Bounds(); rect.InsetBy(5, 5); rect.bottom = rect.top + 25 // space for topmost control + 3 * 20 // size of controls + 3 * 5; // space beween controls and bottom of box BBox *authenticationBox = new BBox(rect, "Authentication"); + authenticationBox->SetLabel(kLabelAuthentication); rect = authenticationBox->Bounds(); - rect.InsetBy(10, 5); - rect.top = 25; - BView *authenticationView = new BView(rect, "authenticationView", - B_FOLLOW_NONE, 0); - authenticationView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); - rect = authenticationView->Bounds(); + rect.InsetBy(10, 20); rect.bottom = rect.top + 20; fUsername = new BTextControl(rect, "username", kLabelName, NULL, NULL); rect.top = rect.bottom + 5; @@ -109,10 +106,9 @@ ConnectionView::ConnectionView(BRect rect, const char *name, ppp_interface_id id rect.bottom = rect.top + 20; fSavePassword = new BCheckBox(rect, "SavePassword", kLabelSavePassword, NULL); - authenticationView->AddChild(fUsername); - authenticationView->AddChild(fPassword); - authenticationView->AddChild(fSavePassword); - authenticationBox->AddChild(authenticationView); + authenticationBox->AddChild(fUsername); + authenticationBox->AddChild(fPassword); + authenticationBox->AddChild(fSavePassword); AddChild(authenticationBox); rect = authenticationBox->Frame(); diff --git a/src/apps/bin/ppp_up/ConnectionWindow.cpp b/src/apps/bin/ppp_up/ConnectionWindow.cpp index 223b914821..ff47515e2b 100644 --- a/src/apps/bin/ppp_up/ConnectionWindow.cpp +++ b/src/apps/bin/ppp_up/ConnectionWindow.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2004, Waldemar Kornewald + * Copyright 2004-2005, Waldemar Kornewald * Distributed under the terms of the MIT License. */ @@ -11,8 +11,8 @@ ConnectionWindow::ConnectionWindow(BRect frame, const char *name, ppp_interface_id id, thread_id replyThread) - : BWindow(frame, "", B_MODAL_WINDOW, - B_NOT_RESIZABLE | B_NOT_ZOOMABLE) + : BWindow(frame, "", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE, + B_ALL_WORKSPACES) { BString title("Connecting to "); title << "\"" << name << "\"..."; diff --git a/src/apps/bin/ppp_up/Jamfile b/src/apps/bin/ppp_up/Jamfile index a4ba9d1489..2605c559c2 100644 --- a/src/apps/bin/ppp_up/Jamfile +++ b/src/apps/bin/ppp_up/Jamfile @@ -10,8 +10,11 @@ AddResources ppp_up : ppp_up.rdef ; BinCommand ppp_up : ConnectionView.cpp ConnectionWindow.cpp + PPPDeskbarReplicant.cpp PPPUpAddon.cpp PPPUpApplication.cpp + StatusView.cpp + StatusWindow.cpp ; LinkSharedOSLibs ppp_up : libptpnet.a libppp.a be ; diff --git a/src/apps/bin/ppp_up/PPPDeskbarReplicant.cpp b/src/apps/bin/ppp_up/PPPDeskbarReplicant.cpp index 11ea375896..9f3db02f67 100644 --- a/src/apps/bin/ppp_up/PPPDeskbarReplicant.cpp +++ b/src/apps/bin/ppp_up/PPPDeskbarReplicant.cpp @@ -1,17 +1,138 @@ /* - * Copyright 2004, Waldemar Kornewald + * Copyright 2004-2005, Waldemar Kornewald * Distributed under the terms of the MIT License. */ #include "PPPDeskbarReplicant.h" -#include "ConnectionWindow.h" + +#include "PPPUpApplication.h" +#include "StatusWindow.h" +#include +#include + +#include +#include -PPPDeskbarReplicant::PPPDeskbarReplicant() +// TODO: remove this +/*extern "C" _EXPORT BView *instantiate_deskbar_item(); + +BView* +instantiate_deskbar_item() +{ + return new PPPDeskbarReplicant(); +}*/ + + +static +status_t +destruction_thread(void *data) +{ + thread_id sender; + ppp_report_packet report; + int32 reportCode; + while(true) { + reportCode = receive_data(&sender, &report, sizeof(report)); + if(reportCode != PPP_REPORT_CODE) + continue; + + if(report.type != PPP_DESTRUCTION_REPORT) + continue; + + // our interface has been destroyed, remove the corresponding replicant + // XXX: We do not know which ID the replicant has and there might exist + // multiple connections (replicants)! + BDeskbar().RemoveItem("PPPDeskbarReplicant"); + } + + return B_OK; +} + + +PPPDeskbarReplicant::PPPDeskbarReplicant(ppp_interface_id id) + : BView(BRect(0, 0, 15, 15), "PPPDeskbarReplicant", B_FOLLOW_NONE, 0), + fID(id) +{ + Init(); +} + + +PPPDeskbarReplicant::PPPDeskbarReplicant(BMessage *message) : BView(BRect(0, 0, 15, 15), "PPPDeskbarReplicant", B_FOLLOW_NONE, 0) { - BRect rect(0, 0, 300, 250); - fWindow = new ConnectionWindow(rect, id, sender); - fWindow->MoveTo(center_on_screen(rect, fWindow)); - fWindow->Show(); + message->FindInt32("interface", reinterpret_cast(&fID)); + Init(); +} + + +PPPDeskbarReplicant::~PPPDeskbarReplicant() +{ + fWindow->LockLooper(); + fWindow->Quit(); +} + + +PPPDeskbarReplicant* +PPPDeskbarReplicant::Instantiate(BMessage *data) +{ + if(!validate_instantiation(data, "PPPDeskbarReplicant")) + return NULL; + + return new PPPDeskbarReplicant(data); +} + + +status_t +PPPDeskbarReplicant::Archive(BMessage *data, bool deep = true) const +{ + BView::Archive(data, deep); + + data->AddString("add_on", APP_SIGNATURE); + data->AddInt32("interface", fID); + return B_NO_ERROR; +} + + +void +PPPDeskbarReplicant::MouseDown(BPoint point) +{ + Looper()->CurrentMessage()->FindInt32("buttons", &fLastButtons); + + // TODO: on secondary mouse button we want to show a pop-up menu +} + + +void +PPPDeskbarReplicant::MouseUp(BPoint point) +{ + if(fLastButtons & B_PRIMARY_MOUSE_BUTTON) { + fWindow->MoveTo(center_on_screen(fWindow->Frame(), fWindow)); + fWindow->Show(); + } +} + + +void +PPPDeskbarReplicant::Draw(BRect updateRect) +{ + // TODO: I want a nice blinking icon! + MovePenTo(4, 12); + DrawString("P"); +} + + +void +PPPDeskbarReplicant::Init() +{ + BRect rect(50,50,380,150); + fWindow = new StatusWindow(rect, fID); + + // watch interface destruction + PPPInterface interface(fID); + if(interface.InitCheck() != B_OK) + return; + + thread_id destructionThread = spawn_thread(destruction_thread, + "destruction_thread", B_NORMAL_PRIORITY, NULL); + interface.EnableReports(PPP_DESTRUCTION_REPORT, destructionThread); } diff --git a/src/apps/bin/ppp_up/PPPDeskbarReplicant.h b/src/apps/bin/ppp_up/PPPDeskbarReplicant.h index b52bb44ae4..0cf1f6109b 100644 --- a/src/apps/bin/ppp_up/PPPDeskbarReplicant.h +++ b/src/apps/bin/ppp_up/PPPDeskbarReplicant.h @@ -1,5 +1,5 @@ /* - * Copyright 2004, Waldemar Kornewald + * Copyright 2004-2005, Waldemar Kornewald * Distributed under the terms of the MIT License. */ @@ -7,11 +7,32 @@ #define PPP_DESKBAR_REPLICANT__H #include +#include + +class StatusWindow; class PPPDeskbarReplicant : public BView { public: - PPPDeskbarReplicant(); + PPPDeskbarReplicant(ppp_interface_id id); + PPPDeskbarReplicant(BMessage *message); + virtual ~PPPDeskbarReplicant(); + + static PPPDeskbarReplicant *Instantiate(BMessage *data); + virtual status_t Archive(BMessage *data, bool deep = true) const; + + virtual void MouseDown(BPoint point); + virtual void MouseUp(BPoint point); + + virtual void Draw(BRect updateRect); + + private: + void Init(); + + private: + StatusWindow *fWindow; + ppp_interface_id fID; + int32 fLastButtons; }; diff --git a/src/apps/bin/ppp_up/PPPUpApplication.cpp b/src/apps/bin/ppp_up/PPPUpApplication.cpp index 3b060e146d..6bc32becb2 100644 --- a/src/apps/bin/ppp_up/PPPUpApplication.cpp +++ b/src/apps/bin/ppp_up/PPPUpApplication.cpp @@ -1,9 +1,10 @@ /* - * Copyright 2004, Waldemar Kornewald + * Copyright 2004-2005, Waldemar Kornewald * Distributed under the terms of the MIT License. */ -#include +#include "PPPUpApplication.h" + #include #include "DialUpView.h" @@ -11,40 +12,8 @@ #include #include - -static const char *kSignature = "application/x-vnd.haiku.ppp_up"; - -extern "C" _EXPORT BView *instantiate_deskbar_item(); - - -class PPPUpApplication : public BApplication { - public: - PPPUpApplication(const char *name, ppp_interface_id id, - thread_id replyThread); - - virtual void ReadyToRun(); - - const char *Name() const - { return fName; } - ppp_interface_id ID() const - { return fID; } - thread_id ReplyThread() const - { return fReplyThread; } - - private: - const char *fName; - ppp_interface_id fID; - thread_id fReplyThread; - ConnectionWindow *fWindow; -}; - - -BView* -instantiate_deskbar_item() -{ - // TODO: implement me! - return NULL; -} +#include +#include "PPPDeskbarReplicant.h" static @@ -95,7 +64,6 @@ report_thread(void *data) } if(report.code == PPP_REPORT_GOING_UP) { - // TODO: // create connection window (it will send the reply for us) (DONE?) BRect rect(150, 50, 450, 435); if(!window) { @@ -107,12 +75,13 @@ report_thread(void *data) // wait for reply from window and forward it to the kernel thread_id tmp; PPP_REPLY(sender, receive_data(&tmp, NULL, 0)); - -// PPP_REPLY(sender, B_OK); - // TODO: remove this when finished with above } else { - if(report.code == PPP_REPORT_UP_SUCCESSFUL) - ; // TODO: add deskbar replicant + if(report.code == PPP_REPORT_UP_SUCCESSFUL) { + // add deskbar replicant (DONE?) + PPPDeskbarReplicant *replicant = new PPPDeskbarReplicant(id); + BDeskbar().AddItem(replicant); + delete replicant; + } PPP_REPLY(sender, PPP_OK_DISABLE_REPORTS); } @@ -144,7 +113,7 @@ main(int argc, const char *argv[]) PPPUpApplication::PPPUpApplication(const char *name, ppp_interface_id id, thread_id replyThread) - : BApplication(kSignature), + : BApplication(APP_SIGNATURE), fName(name), fID(id), fReplyThread(replyThread) diff --git a/src/apps/bin/ppp_up/PPPUpApplication.h b/src/apps/bin/ppp_up/PPPUpApplication.h new file mode 100644 index 0000000000..601a851d9e --- /dev/null +++ b/src/apps/bin/ppp_up/PPPUpApplication.h @@ -0,0 +1,39 @@ +/* + * Copyright 2005, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#ifndef PPP_UP_APPLICATION__H +#define PPP_UP_APPLICATION__H + +#include +#include + +class ConnectionWindow; + +#define APP_SIGNATURE "application/x-vnd.haiku.ppp_up" + + +class PPPUpApplication : public BApplication { + public: + PPPUpApplication(const char *name, ppp_interface_id id, + thread_id replyThread); + + virtual void ReadyToRun(); + + const char *Name() const + { return fName; } + ppp_interface_id ID() const + { return fID; } + thread_id ReplyThread() const + { return fReplyThread; } + + private: + const char *fName; + ppp_interface_id fID; + thread_id fReplyThread; + ConnectionWindow *fWindow; +}; + + +#endif diff --git a/src/apps/bin/ppp_up/StatusView.cpp b/src/apps/bin/ppp_up/StatusView.cpp new file mode 100644 index 0000000000..b61f4f4460 --- /dev/null +++ b/src/apps/bin/ppp_up/StatusView.cpp @@ -0,0 +1,176 @@ +/* + * Copyright 2005, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#include "StatusView.h" + +#include +#include +#include +#include + +#include +#include + +#include + + +// message constants +static const uint32 kMsgDisconnect = 'DISC'; + +// labels +static const char *kLabelDisconnect = "Disconnect"; +static const char *kLabelConnectedSince = "Connected Since: "; +static const char *kLabelReceived = "Received"; +static const char *kLabelSent = "Sent"; + +// strings +static const char *kTextBytes = "Bytes"; +static const char *kTextPackets = "Packets"; + + +StatusView::StatusView(BRect rect, ppp_interface_id id) + : BView(rect, "StatusView", B_FOLLOW_NONE, B_PULSE_NEEDED), + fInterface(id) +{ + SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + + rect = Bounds(); + rect.InsetBy(5, 5); + rect.left = rect.right - 80; + rect.bottom = rect.top + 25; + fButton = new BButton(rect, "DisconnectButton", kLabelDisconnect, + new BMessage(kMsgDisconnect)); + + rect.right = rect.left - 10; + rect.left = rect.right - 80; + rect.top += 5; + rect.bottom = rect.top + 15; + fTime = new BStringView(rect, "Time", ""); + fTime->SetAlignment(B_ALIGN_RIGHT); + fTime->SetFont(be_fixed_font); + rect.right = rect.left - 10; + rect.left = 5; + BStringView *connectedSince = new BStringView(rect, "ConnectedSince", + kLabelConnectedSince); + connectedSince->SetFont(be_fixed_font); + + rect = Bounds(); + rect.InsetBy(5, 5); + rect.top += 35; + rect.right = rect.left + (rect.Width() - 5) / 2; + BBox *received = new BBox(rect, "Received"); + received->SetLabel(kLabelReceived); + rect = received->Bounds(); + rect.InsetBy(10, 15); + rect.bottom = rect.top + 15; + fBytesReceived = new BStringView(rect, "BytesReceived", ""); + fBytesReceived->SetAlignment(B_ALIGN_RIGHT); + fBytesReceived->SetFont(be_fixed_font); + rect.top = rect.bottom + 5; + rect.bottom = rect.top + 15; + fPacketsReceived = new BStringView(rect, "PacketsReceived", ""); + fPacketsReceived->SetAlignment(B_ALIGN_RIGHT); + fPacketsReceived->SetFont(be_fixed_font); + + rect = received->Frame(); + rect.OffsetBy(rect.Width() + 5, 0); + BBox *sent = new BBox(rect, "sent"); + sent->SetLabel(kLabelSent); + rect = received->Bounds(); + rect.InsetBy(10, 15); + rect.bottom = rect.top + 15; + fBytesSent = new BStringView(rect, "BytesSent", ""); + fBytesSent->SetAlignment(B_ALIGN_RIGHT); + fBytesSent->SetFont(be_fixed_font); + rect.top = rect.bottom + 5; + rect.bottom = rect.top + 15; + fPacketsSent = new BStringView(rect, "PacketsSent", ""); + fPacketsSent->SetAlignment(B_ALIGN_RIGHT); + fPacketsSent->SetFont(be_fixed_font); + + received->AddChild(fBytesReceived); + received->AddChild(fPacketsReceived); + sent->AddChild(fBytesSent); + sent->AddChild(fPacketsSent); + + AddChild(fButton); + AddChild(fTime); + AddChild(connectedSince); + AddChild(received); + AddChild(sent); + + ppp_interface_info_t info; + fInterface.GetInterfaceInfo(&info); + fConnectedSince = info.info.connectedSince; +} + + +void +StatusView::AttachedToWindow() +{ + fButton->SetTarget(this); + Window()->SetTitle(fInterface.Name()); +} + + +void +StatusView::MessageReceived(BMessage *message) +{ + switch(message->what) { + case kMsgDisconnect: + fInterface.Down(); + Window()->Hide(); + break; + + default: + BView::MessageReceived(message); + } +} + + +void +StatusView::Pulse() +{ + // update status + ppp_statistics statistics; + if(!fInterface.GetStatistics(&statistics)) { + fBytesReceived->SetText(""); + fPacketsReceived->SetText(""); + fBytesSent->SetText(""); + fPacketsSent->SetText(""); + return; + } + + BString text; + bigtime_t time = system_time() - fConnectedSince; + time /= 1000000; + int32 seconds = time % 60; + time /= 60; + int32 minutes = time % 60; + int32 hours = time / 60; + char minsec[7]; + if(hours) { + sprintf(minsec, ":%02ld:%02ld", minutes, seconds); + text << hours << minsec; + } else if(minutes) { + sprintf(minsec, "%ld:%02ld", minutes, seconds); + text << minsec; + } else + text << seconds; + fTime->SetText(text.String()); + + text = ""; + text << statistics.bytesReceived << ' ' << kTextBytes; + fBytesReceived->SetText(text.String()); + text = ""; + text << statistics.packetsReceived << ' ' << kTextPackets; + fPacketsReceived->SetText(text.String()); + text = ""; + text << statistics.bytesSent << ' ' << kTextBytes; + fBytesSent->SetText(text.String()); + text = ""; + text << statistics.packetsSent << ' ' << kTextPackets; + fPacketsSent->SetText(text.String()); +} diff --git a/src/apps/bin/ppp_up/StatusView.h b/src/apps/bin/ppp_up/StatusView.h new file mode 100644 index 0000000000..1808ac8edc --- /dev/null +++ b/src/apps/bin/ppp_up/StatusView.h @@ -0,0 +1,30 @@ +/* + * Copyright 2005, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#ifndef STATUS_VIEW__H +#define STATUS_VIEW__H + +#include +#include + + +class StatusView : public BView { + public: + StatusView(BRect rect, ppp_interface_id id); + + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage *message); + virtual void Pulse(); + + private: + BButton *fButton; + BStringView *fTime; + BStringView *fBytesReceived, *fBytesSent, *fPacketsReceived, *fPacketsSent; + bigtime_t fConnectedSince; + PPPInterface fInterface; +}; + + +#endif diff --git a/src/apps/bin/ppp_up/StatusWindow.cpp b/src/apps/bin/ppp_up/StatusWindow.cpp new file mode 100644 index 0000000000..f335bc2949 --- /dev/null +++ b/src/apps/bin/ppp_up/StatusWindow.cpp @@ -0,0 +1,27 @@ +/* + * Copyright 2005, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#include "StatusWindow.h" +#include "StatusView.h" + + +StatusWindow::StatusWindow(BRect frame, ppp_interface_id id) + : BWindow(frame, "", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE, + B_ALL_WORKSPACES) +{ + SetPulseRate(1000000); + + StatusView *view = new StatusView(Bounds(), id); + AddChild(view); +} + + +bool +StatusWindow::QuitRequested() +{ + // only the replicant may delete this window! + Hide(); + return false; +} diff --git a/src/apps/bin/ppp_up/StatusWindow.h b/src/apps/bin/ppp_up/StatusWindow.h new file mode 100644 index 0000000000..41d9eb1d1a --- /dev/null +++ b/src/apps/bin/ppp_up/StatusWindow.h @@ -0,0 +1,21 @@ +/* + * Copyright 2005, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#ifndef STATUS_WINDOW__H +#define STATUS_WINDOW__H + +#include +#include + + +class StatusWindow : public BWindow { + public: + StatusWindow(BRect frame, ppp_interface_id id); + + virtual bool QuitRequested(); +}; + + +#endif