2 * Copyright 2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Michael Weirauch, dev@m-phasis.de
10 #include "DeskbarReplicant.h"
13 #include <Application.h>
16 #include <IconUtils.h>
19 #include <PopUpMenu.h>
20 #include <Resources.h>
24 #include <bluetoothserver_p.h>
26 extern "C" _EXPORT BView
*instantiate_deskbar_item(void);
27 status_t
our_image(image_info
& image
);
29 const uint32 kMsgOpenBluetoothPreferences
= 'obtp';
30 const uint32 kMsgQuitBluetoothServer
= 'qbts';
32 const char* kDeskbarItemName
= "BluetoothServerReplicant";
33 const char* kClassName
= "DeskbarReplicant";
38 DeskbarReplicant::DeskbarReplicant(BRect frame
, int32 resizingMode
)
39 : BView(frame
, kDeskbarItemName
, resizingMode
,
40 B_WILL_DRAW
| B_FRAME_EVENTS
)
46 DeskbarReplicant::DeskbarReplicant(BMessage
* archive
)
53 DeskbarReplicant::~DeskbarReplicant()
59 DeskbarReplicant::_Init()
64 if (our_image(info
) != B_OK
)
67 BFile
file(info
.name
, B_READ_ONLY
);
68 if (file
.InitCheck() < B_OK
)
71 BResources
resources(&file
);
72 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
73 if (resources
.InitCheck() < B_OK
)
78 const void* data
= resources
.LoadResource(B_VECTOR_ICON_TYPE
,
81 BBitmap
* icon
= new BBitmap(Bounds(), B_RGBA32
);
82 if (icon
->InitCheck() == B_OK
83 && BIconUtils::GetVectorIcon((const uint8
*)data
,
84 size
, icon
) == B_OK
) {
93 DeskbarReplicant::Instantiate(BMessage
* archive
)
95 if (!validate_instantiation(archive
, kClassName
))
98 return new DeskbarReplicant(archive
);
103 DeskbarReplicant::Archive(BMessage
* archive
, bool deep
) const
105 status_t status
= BView::Archive(archive
, deep
);
107 status
= archive
->AddString("add_on", BLUETOOTH_SIGNATURE
);
109 status
= archive
->AddString("class", kClassName
);
116 DeskbarReplicant::AttachedToWindow()
118 BView::AttachedToWindow();
121 SetLowColor(ViewColor());
126 DeskbarReplicant::Draw(BRect updateRect
)
129 /* At least display something... */
130 rgb_color lowColor
= LowColor();
131 SetLowColor(0, 113, 187, 255);
132 FillRoundRect(Bounds().InsetBySelf(3.f
, 0.f
), 5.f
, 7.f
, B_SOLID_LOW
);
133 SetLowColor(lowColor
);
135 SetDrawingMode(B_OP_ALPHA
);
137 SetDrawingMode(B_OP_COPY
);
143 DeskbarReplicant::MessageReceived(BMessage
* msg
)
146 case kMsgOpenBluetoothPreferences
:
147 be_roster
->Launch(BLUETOOTH_APP_SIGNATURE
);
150 case kMsgQuitBluetoothServer
:
151 _QuitBluetoothServer();
155 BView::MessageReceived(msg
);
161 DeskbarReplicant::MouseDown(BPoint where
)
165 GetMouse(&point
, &buttons
);
166 if (!(buttons
& B_SECONDARY_MOUSE_BUTTON
)) {
170 BPopUpMenu
* menu
= new BPopUpMenu(B_EMPTY_STRING
, false, false);
172 menu
->AddItem(new BMenuItem("Settings" B_UTF8_ELLIPSIS
,
173 new BMessage(kMsgOpenBluetoothPreferences
)));
175 // TODO show list of known/paired devices
177 menu
->AddItem(new BMenuItem("Quit",
178 new BMessage(kMsgQuitBluetoothServer
)));
180 menu
->SetTargetForItems(this);
181 ConvertToScreen(&point
);
182 menu
->Go(point
, true, true, true);
187 DeskbarReplicant::_QuitBluetoothServer()
189 if (!be_roster
->IsRunning(BLUETOOTH_SIGNATURE
)) {
190 // The server isn't running, so remove ourself
192 deskbar
.RemoveItem(kDeskbarItemName
);
196 status_t status
= BMessenger(BLUETOOTH_SIGNATURE
).SendMessage(
199 _ShowErrorAlert("Stopping the Bluetooth server failed.", status
);
205 DeskbarReplicant::_ShowErrorAlert(BString msg
, status_t status
)
207 msg
<< "\n\nError: " << strerror(status
);
208 BAlert
* alert
= new BAlert("Bluetooth error", msg
.String(), "OK");
209 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
216 extern "C" _EXPORT BView
*
217 instantiate_deskbar_item(void)
219 return new DeskbarReplicant(BRect(0, 0, 15, 15),
220 B_FOLLOW_LEFT
| B_FOLLOW_TOP
);
227 our_image(image_info
& image
)
230 while (get_next_image_info(B_CURRENT_TEAM
, &cookie
, &image
) == B_OK
) {
231 if ((char *)our_image
>= (char *)image
.text
232 && (char *)our_image
<= (char *)image
.text
+ image
.text_size
)