vfs: check userland buffers before reading them.
[haiku.git] / src / bin / network / ppp_up / PPPDeskbarReplicant.cpp
blobdfdcbffd97fcffbb6467370a2948aaecfac6b827
1 /*
2 * Copyright 2005, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
4 */
6 #include "PPPDeskbarReplicant.h"
8 #include "PPPUpApplication.h"
9 #include "PPPStatusWindow.h"
10 #include <PPPInterface.h>
12 #include <Deskbar.h>
13 #include <MenuItem.h>
14 #include <Message.h>
15 #include <PopUpMenu.h>
18 // message constants
19 static const uint32 kMsgDisconnect = 'DISC';
20 static const uint32 kMsgStatus = 'STAT';
22 // labels
23 static const char *kLabelDisconnect = "Disconnect";
24 static const char *kLabelStatus = "Status";
27 PPPDeskbarReplicant::PPPDeskbarReplicant(ppp_interface_id id)
28 : BView(BRect(0, 0, 15, 15), "PPPDeskbarReplicant", B_FOLLOW_NONE, 0),
29 fID(id)
31 Init();
35 PPPDeskbarReplicant::PPPDeskbarReplicant(BMessage *message)
36 : BView(BRect(0, 0, 15, 15), "PPPDeskbarReplicant", B_FOLLOW_NONE, 0)
38 message->FindInt32("interface", reinterpret_cast<int32*>(&fID));
39 Init();
43 PPPDeskbarReplicant::~PPPDeskbarReplicant()
45 delete fContextMenu;
47 fWindow->LockLooper();
48 fWindow->Quit();
52 PPPDeskbarReplicant*
53 PPPDeskbarReplicant::Instantiate(BMessage *data)
55 if(!validate_instantiation(data, "PPPDeskbarReplicant"))
56 return NULL;
58 return new PPPDeskbarReplicant(data);
62 status_t
63 PPPDeskbarReplicant::Archive(BMessage *data, bool deep) const
65 BView::Archive(data, deep);
67 data->AddString("add_on", APP_SIGNATURE);
68 data->AddInt32("interface", fID);
69 return B_NO_ERROR;
73 void
74 PPPDeskbarReplicant::AttachedToWindow()
76 BMenuItem *item = new BMenuItem(kLabelDisconnect, new BMessage(kMsgDisconnect));
77 item->SetTarget(fWindow->StatusView());
78 fContextMenu->AddItem(item);
79 fContextMenu->AddSeparatorItem();
80 item = new BMenuItem(kLabelStatus, new BMessage(kMsgStatus));
81 item->SetTarget(this);
82 fContextMenu->AddItem(item);
86 void
87 PPPDeskbarReplicant::MessageReceived(BMessage *message)
89 switch(message->what) {
90 case PPP_REPORT_MESSAGE: {
91 int32 type;
92 message->FindInt32("type", &type);
93 if(type == PPP_DESTRUCTION_REPORT)
94 BDeskbar().RemoveItem(Name());
95 } break;
97 case kMsgStatus:
98 fWindow->Show();
99 break;
101 default:
102 BView::MessageReceived(message);
107 void
108 PPPDeskbarReplicant::MouseDown(BPoint point)
110 Looper()->CurrentMessage()->FindInt32("buttons", &fLastButtons);
112 if(fLastButtons & B_SECONDARY_MOUSE_BUTTON) {
113 ConvertToScreen(&point);
114 fContextMenu->Go(point, true, true, true);
119 void
120 PPPDeskbarReplicant::MouseUp(BPoint point)
122 if(fLastButtons & B_PRIMARY_MOUSE_BUTTON) {
123 // TODO: center on screen
124 // fWindow->MoveTo(center_on_screen(fWindow->Frame(), fWindow));
125 fWindow->Show();
130 void
131 PPPDeskbarReplicant::Draw(BRect updateRect)
133 // TODO: I want a nice blinking icon!
134 MovePenTo(4, 12);
135 DrawString("P");
139 void
140 PPPDeskbarReplicant::Init()
142 BString name("PPPDeskbarReplicant");
143 name << fID;
144 SetName(name.String());
146 BRect rect(50,50,380,150);
147 fWindow = new PPPStatusWindow(rect, fID);
149 fContextMenu = new BPopUpMenu("PPPContextMenu", false);
151 // watch interface destruction
152 PPPInterface interface(fID);
153 if(interface.InitCheck() != B_OK)
154 return;