xref: /haiku/src/bin/network/ppp_up/PPPDeskbarReplicant.cpp (revision 5adca30a18617cc25bc50a9a12e2fe6539412ccb)
1 /*
2  * Copyright 2005, Waldemar Kornewald <wkornew@gmx.net>
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "PPPDeskbarReplicant.h"
7 
8 #include "PPPUpApplication.h"
9 #include "PPPStatusWindow.h"
10 #include <PPPInterface.h>
11 
12 #include <Deskbar.h>
13 #include <MenuItem.h>
14 #include <Message.h>
15 #include <PopUpMenu.h>
16 
17 
18 // message constants
19 static const uint32 kMsgDisconnect = 'DISC';
20 static const uint32 kMsgStatus = 'STAT';
21 
22 // labels
23 static const char *kLabelDisconnect = "Disconnect";
24 static const char *kLabelStatus = "Status";
25 
26 
PPPDeskbarReplicant(ppp_interface_id id)27 PPPDeskbarReplicant::PPPDeskbarReplicant(ppp_interface_id id)
28 	: BView(BRect(0, 0, 15, 15), "PPPDeskbarReplicant", B_FOLLOW_NONE, 0),
29 	fID(id)
30 {
31 	Init();
32 }
33 
34 
PPPDeskbarReplicant(BMessage * message)35 PPPDeskbarReplicant::PPPDeskbarReplicant(BMessage *message)
36 	: BView(BRect(0, 0, 15, 15), "PPPDeskbarReplicant", B_FOLLOW_NONE, 0)
37 {
38 	message->FindInt32("interface", reinterpret_cast<int32*>(&fID));
39 	Init();
40 }
41 
42 
~PPPDeskbarReplicant()43 PPPDeskbarReplicant::~PPPDeskbarReplicant()
44 {
45 	delete fContextMenu;
46 
47 	fWindow->LockLooper();
48 	fWindow->Quit();
49 }
50 
51 
52 PPPDeskbarReplicant*
Instantiate(BMessage * data)53 PPPDeskbarReplicant::Instantiate(BMessage *data)
54 {
55 	if(!validate_instantiation(data, "PPPDeskbarReplicant"))
56 		return NULL;
57 
58 	return new PPPDeskbarReplicant(data);
59 }
60 
61 
62 status_t
Archive(BMessage * data,bool deep) const63 PPPDeskbarReplicant::Archive(BMessage *data, bool deep) const
64 {
65 	BView::Archive(data, deep);
66 
67 	data->AddString("add_on", APP_SIGNATURE);
68 	data->AddInt32("interface", fID);
69 	return B_NO_ERROR;
70 }
71 
72 
73 void
AttachedToWindow()74 PPPDeskbarReplicant::AttachedToWindow()
75 {
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);
83 }
84 
85 
86 void
MessageReceived(BMessage * message)87 PPPDeskbarReplicant::MessageReceived(BMessage *message)
88 {
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;
96 
97 		case kMsgStatus:
98 			fWindow->Show();
99 		break;
100 
101 		default:
102 			BView::MessageReceived(message);
103 	}
104 }
105 
106 
107 void
MouseDown(BPoint point)108 PPPDeskbarReplicant::MouseDown(BPoint point)
109 {
110 	Looper()->CurrentMessage()->FindInt32("buttons", &fLastButtons);
111 
112 	if(fLastButtons & B_SECONDARY_MOUSE_BUTTON) {
113 		ConvertToScreen(&point);
114 		fContextMenu->Go(point, true, true, true);
115 	}
116 }
117 
118 
119 void
MouseUp(BPoint point)120 PPPDeskbarReplicant::MouseUp(BPoint point)
121 {
122 	if(fLastButtons & B_PRIMARY_MOUSE_BUTTON) {
123 		// TODO: center on screen
124 //		fWindow->MoveTo(center_on_screen(fWindow->Frame(), fWindow));
125 		fWindow->Show();
126 	}
127 }
128 
129 
130 void
Draw(BRect updateRect)131 PPPDeskbarReplicant::Draw(BRect updateRect)
132 {
133 	// TODO: I want a nice blinking icon!
134 	MovePenTo(4, 12);
135 	DrawString("P");
136 }
137 
138 
139 void
Init()140 PPPDeskbarReplicant::Init()
141 {
142 	BString name("PPPDeskbarReplicant");
143 	name << fID;
144 	SetName(name.String());
145 
146 	BRect rect(50,50,380,150);
147 	fWindow = new PPPStatusWindow(rect, fID);
148 
149 	fContextMenu = new BPopUpMenu("PPPContextMenu", false);
150 
151 	// watch interface destruction
152 	PPPInterface interface(fID);
153 	if(interface.InitCheck() != B_OK)
154 		return;
155 }
156