vfs: check userland buffers before reading them.
[haiku.git] / src / servers / bluetooth / DeskbarReplicant.cpp
blob226cd4f6f850f67b29c1682ebbff123ce3c91437
1 /*
2 * Copyright 2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Weirauch, dev@m-phasis.de
7 */
10 #include "DeskbarReplicant.h"
12 #include <Alert.h>
13 #include <Application.h>
14 #include <Bitmap.h>
15 #include <Deskbar.h>
16 #include <IconUtils.h>
17 #include <MenuItem.h>
18 #include <Message.h>
19 #include <PopUpMenu.h>
20 #include <Resources.h>
21 #include <Roster.h>
22 #include <String.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";
35 // #pragma mark -
38 DeskbarReplicant::DeskbarReplicant(BRect frame, int32 resizingMode)
39 : BView(frame, kDeskbarItemName, resizingMode,
40 B_WILL_DRAW | B_FRAME_EVENTS)
42 _Init();
46 DeskbarReplicant::DeskbarReplicant(BMessage* archive)
47 : BView(archive)
49 _Init();
53 DeskbarReplicant::~DeskbarReplicant()
58 void
59 DeskbarReplicant::_Init()
61 fIcon = NULL;
63 image_info info;
64 if (our_image(info) != B_OK)
65 return;
67 BFile file(info.name, B_READ_ONLY);
68 if (file.InitCheck() < B_OK)
69 return;
71 BResources resources(&file);
72 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
73 if (resources.InitCheck() < B_OK)
74 return;
75 #endif
77 size_t size;
78 const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE,
79 "tray_icon", &size);
80 if (data != NULL) {
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) {
85 fIcon = icon;
86 } else
87 delete icon;
92 DeskbarReplicant *
93 DeskbarReplicant::Instantiate(BMessage* archive)
95 if (!validate_instantiation(archive, kClassName))
96 return NULL;
98 return new DeskbarReplicant(archive);
102 status_t
103 DeskbarReplicant::Archive(BMessage* archive, bool deep) const
105 status_t status = BView::Archive(archive, deep);
106 if (status == B_OK)
107 status = archive->AddString("add_on", BLUETOOTH_SIGNATURE);
108 if (status == B_OK)
109 status = archive->AddString("class", kClassName);
111 return status;
115 void
116 DeskbarReplicant::AttachedToWindow()
118 BView::AttachedToWindow();
119 AdoptParentColors();
121 SetLowColor(ViewColor());
125 void
126 DeskbarReplicant::Draw(BRect updateRect)
128 if (!fIcon) {
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);
134 } else {
135 SetDrawingMode(B_OP_ALPHA);
136 DrawBitmap(fIcon);
137 SetDrawingMode(B_OP_COPY);
142 void
143 DeskbarReplicant::MessageReceived(BMessage* msg)
145 switch (msg->what) {
146 case kMsgOpenBluetoothPreferences:
147 be_roster->Launch(BLUETOOTH_APP_SIGNATURE);
148 break;
150 case kMsgQuitBluetoothServer:
151 _QuitBluetoothServer();
152 break;
154 default:
155 BView::MessageReceived(msg);
160 void
161 DeskbarReplicant::MouseDown(BPoint where)
163 BPoint point;
164 uint32 buttons;
165 GetMouse(&point, &buttons);
166 if (!(buttons & B_SECONDARY_MOUSE_BUTTON)) {
167 return;
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);
186 void
187 DeskbarReplicant::_QuitBluetoothServer()
189 if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
190 // The server isn't running, so remove ourself
191 BDeskbar deskbar;
192 deskbar.RemoveItem(kDeskbarItemName);
194 return;
196 status_t status = BMessenger(BLUETOOTH_SIGNATURE).SendMessage(
197 B_QUIT_REQUESTED);
198 if (status < B_OK) {
199 _ShowErrorAlert("Stopping the Bluetooth server failed.", status);
204 void
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);
210 alert->Go(NULL);
213 // #pragma mark -
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);
223 // #pragma mark -
226 status_t
227 our_image(image_info& image)
229 int32 cookie = 0;
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)
233 return B_OK;
236 return B_ERROR;