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
19static const uint32 kMsgDisconnect = 'DISC';
20static const uint32 kMsgStatus = 'STAT';
21
22// labels
23static const char *kLabelDisconnect = "Disconnect";
24static const char *kLabelStatus = "Status";
25
26
27PPPDeskbarReplicant::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
35PPPDeskbarReplicant::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
43PPPDeskbarReplicant::~PPPDeskbarReplicant()
44{
45	delete fContextMenu;
46
47	fWindow->LockLooper();
48	fWindow->Quit();
49}
50
51
52PPPDeskbarReplicant*
53PPPDeskbarReplicant::Instantiate(BMessage *data)
54{
55	if(!validate_instantiation(data, "PPPDeskbarReplicant"))
56		return NULL;
57
58	return new PPPDeskbarReplicant(data);
59}
60
61
62status_t
63PPPDeskbarReplicant::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
73void
74PPPDeskbarReplicant::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
86void
87PPPDeskbarReplicant::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
107void
108PPPDeskbarReplicant::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
119void
120PPPDeskbarReplicant::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
130void
131PPPDeskbarReplicant::Draw(BRect updateRect)
132{
133	// TODO: I want a nice blinking icon!
134	MovePenTo(4, 12);
135	DrawString("P");
136}
137
138
139void
140PPPDeskbarReplicant::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