xref: /haiku/src/preferences/network/NetworkWindow.cpp (revision 1f52c921e27aa442370e1bd4adc021acf2b78b64)
1 /*
2  * Copyright 2004-2019 Haiku Inc., All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  *	Authors:
6  *		Adrien Destugues, <pulkomandy@pulkomandy.tk>
7  *		Axel Dörfler, <axeld@pinc-software.de>
8  *		Alexander von Gluck, <kallisti5@unixzen.com>
9  */
10 
11 
12 #include "NetworkWindow.h"
13 
14 #include <net/if.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <Alert.h>
20 #include <Application.h>
21 #include <Button.h>
22 #include <Catalog.h>
23 #include <CheckBox.h>
24 #include <ControlLook.h>
25 #include <Deskbar.h>
26 #include <Directory.h>
27 #include <LayoutBuilder.h>
28 #include <NetworkDevice.h>
29 #include <NetworkInterface.h>
30 #include <NetworkNotifications.h>
31 #include <NetworkRoster.h>
32 #include <OutlineListView.h>
33 #include <Path.h>
34 #include <PathFinder.h>
35 #include <PathMonitor.h>
36 #include <Roster.h>
37 #include <ScrollView.h>
38 #include <StringItem.h>
39 #include <SymLink.h>
40 
41 #define ENABLE_PROFILES 0
42 #if ENABLE_PROFILES
43 #	include <PopUpMenu.h>
44 #endif
45 
46 #include "InterfaceListItem.h"
47 #include "InterfaceView.h"
48 #include "ServiceListItem.h"
49 
50 
51 const char* kNetworkStatusSignature = "application/x-vnd.Haiku-NetworkStatus";
52 
53 static const uint32 kMsgProfileSelected = 'prof';
54 static const uint32 kMsgProfileManage = 'mngp';
55 static const uint32 kMsgProfileNew = 'newp';
56 static const uint32 kMsgRevert = 'rvrt';
57 static const uint32 kMsgToggleReplicant = 'trep';
58 static const uint32 kMsgItemSelected = 'ItSl';
59 
60 BMessenger gNetworkWindow;
61 
62 
63 #undef B_TRANSLATION_CONTEXT
64 #define B_TRANSLATION_CONTEXT	"NetworkWindow"
65 
66 
67 class TitleItem : public BStringItem {
68 public:
69 	TitleItem(const char* title)
70 		:
71 		BStringItem(title)
72 	{
73 	}
74 
75 	void DrawItem(BView* owner, BRect bounds, bool complete)
76 	{
77 		owner->SetFont(be_bold_font);
78 		BStringItem::DrawItem(owner, bounds, complete);
79 		owner->SetFont(be_plain_font);
80 	}
81 
82 	void Update(BView* owner, const BFont* font)
83 	{
84 		BStringItem::Update(owner, be_bold_font);
85 	}
86 };
87 
88 
89 // #pragma mark -
90 
91 
92 NetworkWindow::NetworkWindow()
93 	:
94 	BWindow(BRect(100, 100, 400, 400), B_TRANSLATE("Network"), B_TITLED_WINDOW,
95 		B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
96 	fServicesItem(NULL),
97 	fDialUpItem(NULL),
98 	fVPNItem(NULL),
99 	fOtherItem(NULL)
100 {
101 	// Profiles section
102 #if ENABLE_PROFILES
103 	BPopUpMenu* profilesPopup = new BPopUpMenu("<none>");
104 	_BuildProfilesMenu(profilesPopup, kMsgProfileSelected);
105 
106 	BMenuField* profilesMenuField = new BMenuField("profiles_menu",
107 		B_TRANSLATE("Profile:"), profilesPopup);
108 
109 	profilesMenuField->SetFont(be_bold_font);
110 	profilesMenuField->SetEnabled(false);
111 #endif
112 
113 	// Settings section
114 
115 	fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
116 		new BMessage(kMsgRevert));
117 
118 	BMessage* message = new BMessage(kMsgToggleReplicant);
119 	BCheckBox* showReplicantCheckBox = new BCheckBox("showReplicantCheckBox",
120 		B_TRANSLATE("Show network status in Deskbar"), message);
121 	showReplicantCheckBox->SetExplicitMaxSize(
122 		BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
123 	showReplicantCheckBox->SetValue(_IsReplicantInstalled());
124 
125 	fListView = new BOutlineListView("list", B_SINGLE_SELECTION_LIST,
126 		B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS | B_NAVIGABLE);
127 	fListView->SetSelectionMessage(new BMessage(kMsgItemSelected));
128 
129 	BScrollView* scrollView = new BScrollView("ScrollView", fListView,
130 		0, false, true);
131 	scrollView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
132 
133 	fAddOnShellView = new BView("add-on shell", 0,
134 		new BGroupLayout(B_VERTICAL));
135 	fAddOnShellView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
136 
137 	fInterfaceView = new InterfaceView();
138 
139 	// Build the layout
140 	BLayoutBuilder::Group<>(this, B_VERTICAL)
141 		.SetInsets(B_USE_WINDOW_SPACING)
142 
143 #if ENABLE_PROFILES
144 		.AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
145 			.Add(profilesMenuField)
146 			.AddGlue()
147 		.End()
148 #endif
149 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
150 			.Add(scrollView)
151 			.Add(fAddOnShellView)
152 			.End()
153 		.Add(showReplicantCheckBox)
154 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
155 			.Add(fRevertButton)
156 			.AddGlue()
157 		.End();
158 
159 	gNetworkWindow = this;
160 
161 	_ScanInterfaces();
162 	_ScanAddOns();
163 	_UpdateRevertButton();
164 
165 	fListView->Select(0);
166 	_SelectItem(fListView->ItemAt(0));
167 		// Call this manually, so that CenterOnScreen() below already
168 		// knows the final window size.
169 
170 	// Set size of the list view from its contents
171 	float width;
172 	float height;
173 	fListView->GetPreferredSize(&width, &height);
174 	width += 2 * be_control_look->DefaultItemSpacing();
175 	fListView->SetExplicitSize(BSize(width, B_SIZE_UNSET));
176 	fListView->SetExplicitMinSize(BSize(width, std::min(height, 400.f)));
177 
178 	CenterOnScreen();
179 
180 	fSettings.StartMonitoring(this);
181 	start_watching_network(B_WATCH_NETWORK_INTERFACE_CHANGES
182 		| B_WATCH_NETWORK_LINK_CHANGES | B_WATCH_NETWORK_WLAN_CHANGES, this);
183 }
184 
185 
186 NetworkWindow::~NetworkWindow()
187 {
188 	stop_watching_network(this);
189 	fSettings.StopMonitoring(this);
190 }
191 
192 
193 bool
194 NetworkWindow::QuitRequested()
195 {
196 	be_app->PostMessage(B_QUIT_REQUESTED);
197 	return true;
198 }
199 
200 
201 void
202 NetworkWindow::MessageReceived(BMessage* message)
203 {
204 	switch (message->what) {
205 		case kMsgProfileNew:
206 			break;
207 
208 		case kMsgProfileSelected:
209 		{
210 			const char* path;
211 			if (message->FindString("path", &path) != B_OK)
212 				break;
213 
214 			// TODO!
215 			break;
216 		}
217 
218 		case kMsgItemSelected:
219 		{
220 			BListItem* listItem = fListView->FullListItemAt(
221 				fListView->FullListCurrentSelection());
222 			if (listItem == NULL)
223 				break;
224 
225 			_SelectItem(listItem);
226 			break;
227 		}
228 
229 		case kMsgRevert:
230 		{
231 			SettingsMap::const_iterator iterator = fSettingsMap.begin();
232 			for (; iterator != fSettingsMap.end(); iterator++)
233 				iterator->second->Revert();
234 			break;
235 		}
236 
237 		case kMsgToggleReplicant:
238 		{
239 			_ShowReplicant(
240 				message->GetInt32("be:value", B_CONTROL_OFF) == B_CONTROL_ON);
241 			break;
242 		}
243 
244 		case B_PATH_MONITOR:
245 		{
246 			fSettings.Update(message);
247 			break;
248 		}
249 
250 		case B_NETWORK_MONITOR:
251 			_BroadcastConfigurationUpdate(*message);
252 			break;
253 
254 		case BNetworkSettings::kMsgInterfaceSettingsUpdated:
255 		case BNetworkSettings::kMsgNetworkSettingsUpdated:
256 		case BNetworkSettings::kMsgServiceSettingsUpdated:
257 			_BroadcastSettingsUpdate(message->what);
258 			break;
259 
260 		case kMsgSettingsItemUpdated:
261 			// TODO: update list item
262 			_UpdateRevertButton();
263 			break;
264 
265 		default:
266 			inherited::MessageReceived(message);
267 	}
268 }
269 
270 
271 void
272 NetworkWindow::_BuildProfilesMenu(BMenu* menu, int32 what)
273 {
274 	char currentProfile[256] = { 0 };
275 
276 	menu->SetRadioMode(true);
277 
278 	BDirectory dir("/boot/system/settings/network/profiles");
279 	if (dir.InitCheck() == B_OK) {
280 		BEntry entry;
281 
282 		dir.Rewind();
283 		while (dir.GetNextEntry(&entry) >= 0) {
284 			BPath name;
285 			entry.GetPath(&name);
286 
287 			if (entry.IsSymLink() &&
288 				strcmp("current", name.Leaf()) == 0) {
289 				BSymLink symlink(&entry);
290 
291 				if (symlink.IsAbsolute())
292 					// oh oh, sorry, wrong symlink...
293 					continue;
294 
295 				symlink.ReadLink(currentProfile, sizeof(currentProfile));
296 				continue;
297 			};
298 
299 			if (!entry.IsDirectory())
300 				continue;
301 
302 			BMessage* message = new BMessage(what);
303 			message->AddString("path", name.Path());
304 
305 			BMenuItem* item = new BMenuItem(name.Leaf(), message);
306 			menu->AddItem(item);
307 		}
308 	}
309 
310 	menu->AddSeparatorItem();
311 	menu->AddItem(new BMenuItem(B_TRANSLATE("New" B_UTF8_ELLIPSIS),
312 		new BMessage(kMsgProfileNew)));
313 	menu->AddItem(new BMenuItem(B_TRANSLATE("Manage" B_UTF8_ELLIPSIS),
314 		new BMessage(kMsgProfileManage)));
315 
316 	if (currentProfile[0] != '\0') {
317 		BMenuItem* item = menu->FindItem(currentProfile);
318 		if (item != NULL) {
319 			// TODO: translate
320 			BString label(item->Label());
321 			label << " (current)";
322 			item->SetLabel(label.String());
323 			item->SetMarked(true);
324 		}
325 	}
326 }
327 
328 
329 void
330 NetworkWindow::_ScanInterfaces()
331 {
332 	// Try existing devices first
333 	BNetworkRoster& roster = BNetworkRoster::Default();
334 	BNetworkInterface interface;
335 	uint32 cookie = 0;
336 
337 	while (roster.GetNextInterface(&cookie, interface) == B_OK) {
338 		if ((interface.Flags() & IFF_LOOPBACK) != 0)
339 			continue;
340 
341 		BNetworkDevice device(interface.Name());
342 		BNetworkInterfaceType type = B_NETWORK_INTERFACE_TYPE_OTHER;
343 
344 		if (device.IsWireless())
345 			type = B_NETWORK_INTERFACE_TYPE_WIFI;
346 		else if (device.IsEthernet())
347 			type = B_NETWORK_INTERFACE_TYPE_ETHERNET;
348 
349 		InterfaceListItem* item = new InterfaceListItem(interface.Name(), type);
350 		item->SetExpanded(true);
351 
352 		fInterfaceItemMap.insert(std::pair<BString, InterfaceListItem*>(
353 			BString(interface.Name()), item));
354 		fListView->AddItem(item);
355 	}
356 
357 	// TODO: Then consider those from the settings (for example, for USB)
358 }
359 
360 
361 void
362 NetworkWindow::_ScanAddOns()
363 {
364 	BStringList paths;
365 	BPathFinder::FindPaths(B_FIND_PATH_ADD_ONS_DIRECTORY, "Network Settings",
366 		paths);
367 
368 	// Collect add-on paths by name, so that each name will only be
369 	// loaded once.
370 	typedef std::map<BString, BPath> PathMap;
371 	PathMap addOnMap;
372 
373 	for (int32 i = 0; i < paths.CountStrings(); i++) {
374 		BDirectory directory(paths.StringAt(i));
375 		BEntry entry;
376 		while (directory.GetNextEntry(&entry) == B_OK) {
377 			BPath path;
378 			if (entry.GetPath(&path) != B_OK)
379 				continue;
380 
381 			if (addOnMap.find(path.Leaf()) == addOnMap.end())
382 				addOnMap.insert(std::pair<BString, BPath>(path.Leaf(), path));
383 		}
384 	}
385 
386 	for (PathMap::const_iterator addOnIterator = addOnMap.begin();
387 			addOnIterator != addOnMap.end(); addOnIterator++) {
388 		const BPath& path = addOnIterator->second;
389 
390 		image_id image = load_add_on(path.Path());
391 		if (image < 0) {
392 			printf("Failed to load %s addon: %s.\n", path.Path(),
393 				strerror(image));
394 			continue;
395 		}
396 
397 		BNetworkSettingsAddOn* (*instantiateAddOn)(image_id image,
398 			BNetworkSettings& settings);
399 
400 		status_t status = get_image_symbol(image,
401 			"instantiate_network_settings_add_on",
402 			B_SYMBOL_TYPE_TEXT, (void**)&instantiateAddOn);
403 		if (status != B_OK) {
404 			// No "addon instantiate function" symbol found in this addon
405 			printf("No symbol \"instantiate_network_settings_add_on\" found "
406 				"in %s addon: not a network setup addon!\n", path.Path());
407 			unload_add_on(image);
408 			continue;
409 		}
410 
411 		BNetworkSettingsAddOn* addOn = instantiateAddOn(image, fSettings);
412 		if (addOn == NULL) {
413 			unload_add_on(image);
414 			continue;
415 		}
416 
417 		fAddOns.AddItem(addOn);
418 
419 		// Per interface items
420 		ItemMap::const_iterator iterator = fInterfaceItemMap.begin();
421 		for (; iterator != fInterfaceItemMap.end(); iterator++) {
422 			const BString& interface = iterator->first;
423 			BListItem* interfaceItem = iterator->second;
424 
425 			uint32 cookie = 0;
426 			while (true) {
427 				BNetworkSettingsItem* item = addOn->CreateNextInterfaceItem(
428 					cookie, interface.String());
429 				if (item == NULL)
430 					break;
431 
432 				fSettingsMap[item->ListItem()] = item;
433 				fListView->AddUnder(item->ListItem(), interfaceItem);
434 			}
435 			fListView->SortItemsUnder(interfaceItem, true,
436 				NetworkWindow::_CompareListItems);
437 		}
438 
439 		// Generic items
440 		uint32 cookie = 0;
441 		while (true) {
442 			BNetworkSettingsItem* item = addOn->CreateNextItem(cookie);
443 			if (item == NULL)
444 				break;
445 
446 			fSettingsMap[item->ListItem()] = item;
447 			fListView->AddUnder(item->ListItem(),
448 				_ListItemFor(item->Type()));
449 		}
450 
451 		_SortItemsUnder(fServicesItem);
452 		_SortItemsUnder(fOtherItem);
453 	}
454 
455 	fListView->SortItemsUnder(NULL, true,
456 		NetworkWindow::_CompareTopLevelListItems);
457 }
458 
459 
460 BNetworkSettingsItem*
461 NetworkWindow::_SettingsItemFor(BListItem* item)
462 {
463 	SettingsMap::const_iterator found = fSettingsMap.find(item);
464 	if (found != fSettingsMap.end())
465 		return found->second;
466 
467 	return NULL;
468 }
469 
470 
471 void
472 NetworkWindow::_SortItemsUnder(BListItem* item)
473 {
474 	if (item != NULL)
475 		fListView->SortItemsUnder(item, true, NetworkWindow::_CompareListItems);
476 }
477 
478 
479 BListItem*
480 NetworkWindow::_ListItemFor(BNetworkSettingsType type)
481 {
482 	switch (type) {
483 		case B_NETWORK_SETTINGS_TYPE_SERVICE:
484 			if (fServicesItem == NULL)
485 				fServicesItem = _CreateItem(B_TRANSLATE("Services"));
486 			return fServicesItem;
487 
488 		case B_NETWORK_SETTINGS_TYPE_OTHER:
489 			if (fOtherItem == NULL)
490 				fOtherItem = _CreateItem(B_TRANSLATE("Other"));
491 			return fOtherItem;
492 
493 		default:
494 			return NULL;
495 	}
496 }
497 
498 
499 BListItem*
500 NetworkWindow::_CreateItem(const char* label)
501 {
502 	BListItem* item = new TitleItem(label);
503 	item->SetExpanded(true);
504 	fListView->AddItem(item);
505 	return item;
506 }
507 
508 
509 void
510 NetworkWindow::_SelectItem(BListItem* listItem)
511 {
512 	while (fAddOnShellView->CountChildren() > 0)
513 		fAddOnShellView->ChildAt(0)->RemoveSelf();
514 
515 	BView* nextView = NULL;
516 
517 	BNetworkSettingsItem* item = _SettingsItemFor(listItem);
518 	if (item != NULL) {
519 		nextView = item->View();
520 	} else {
521 		InterfaceListItem* item = dynamic_cast<InterfaceListItem*>(
522 			listItem);
523 		if (item != NULL) {
524 			fInterfaceView->SetTo(item->Name());
525 			nextView = fInterfaceView;
526 		}
527 	}
528 
529 	if (nextView != NULL)
530 		fAddOnShellView->AddChild(nextView);
531 }
532 
533 
534 void
535 NetworkWindow::_BroadcastSettingsUpdate(uint32 type)
536 {
537 	for (int32 index = 0; index < fListView->FullListCountItems(); index++) {
538 		BNetworkSettingsListener* listener
539 			= dynamic_cast<BNetworkSettingsListener*>(
540 				fListView->FullListItemAt(index));
541 		if (listener != NULL)
542 			listener->SettingsUpdated(type);
543 	}
544 
545 	SettingsMap::const_iterator iterator = fSettingsMap.begin();
546 	for (; iterator != fSettingsMap.end(); iterator++)
547 		iterator->second->SettingsUpdated(type);
548 
549 	_UpdateRevertButton();
550 }
551 
552 
553 void
554 NetworkWindow::_BroadcastConfigurationUpdate(const BMessage& message)
555 {
556 	for (int32 index = 0; index < fListView->FullListCountItems(); index++) {
557 		BNetworkConfigurationListener* listener
558 			= dynamic_cast<BNetworkConfigurationListener*>(
559 				fListView->FullListItemAt(index));
560 		if (listener != NULL)
561 			listener->ConfigurationUpdated(message);
562 	}
563 
564 	SettingsMap::const_iterator iterator = fSettingsMap.begin();
565 	for (; iterator != fSettingsMap.end(); iterator++)
566 		iterator->second->ConfigurationUpdated(message);
567 
568 	// TODO: improve invalidated region to the one that matters
569 	fListView->Invalidate();
570 	_UpdateRevertButton();
571 }
572 
573 
574 void
575 NetworkWindow::_UpdateRevertButton()
576 {
577 	bool enabled = false;
578 	SettingsMap::const_iterator iterator = fSettingsMap.begin();
579 	for (; iterator != fSettingsMap.end(); iterator++) {
580 		if (iterator->second->IsRevertable()) {
581 			enabled = true;
582 			break;
583 		}
584 	}
585 
586 	fRevertButton->SetEnabled(enabled);
587 }
588 
589 
590 void
591 NetworkWindow::_ShowReplicant(bool show)
592 {
593 	if (show) {
594 		const char* argv[] = {"--deskbar", NULL};
595 
596 		status_t status = be_roster->Launch(kNetworkStatusSignature, 1, argv);
597 		if (status != B_OK) {
598 			BString errorMessage;
599 			errorMessage.SetToFormat(
600 				B_TRANSLATE("Installing NetworkStatus in Deskbar failed: %s"),
601 				strerror(status));
602 			BAlert* alert = new BAlert(B_TRANSLATE("launch error"),
603 				errorMessage, B_TRANSLATE("OK"));
604 			alert->Go(NULL);
605 		}
606 	} else {
607 		BDeskbar deskbar;
608 		deskbar.RemoveItem("NetworkStatus");
609 	}
610 }
611 
612 
613 bool
614 NetworkWindow::_IsReplicantInstalled()
615 {
616 	BDeskbar deskbar;
617 	return deskbar.HasItem("NetworkStatus");
618 }
619 
620 
621 /*static*/ const char*
622 NetworkWindow::_ItemName(const BListItem* item)
623 {
624 	if (const BNetworkInterfaceListItem* listItem = dynamic_cast<
625 			const BNetworkInterfaceListItem*>(item))
626 		return listItem->Label();
627 
628 	if (const ServiceListItem* listItem = dynamic_cast<
629 			const ServiceListItem*>(item))
630 		return listItem->Label();
631 
632 	if (const BStringItem* stringItem = dynamic_cast<const BStringItem*>(item))
633 		return stringItem->Text();
634 
635 	return NULL;
636 }
637 
638 
639 /*static*/ int
640 NetworkWindow::_CompareTopLevelListItems(const BListItem* a, const BListItem* b)
641 {
642 	if (a == b)
643 		return 0;
644 
645 	if (const InterfaceListItem* itemA
646 			= dynamic_cast<const InterfaceListItem*>(a)) {
647 		if (const InterfaceListItem* itemB
648 				= dynamic_cast<const InterfaceListItem*>(b)) {
649 			return strcasecmp(itemA->Name(), itemB->Name());
650 		}
651 		return -1;
652 	} else if (dynamic_cast<const InterfaceListItem*>(b) != NULL)
653 		return 1;
654 /*
655 	if (a == fDialUpItem)
656 		return -1;
657 	if (b == fDialUpItem)
658 		return 1;
659 
660 	if (a == fServicesItem)
661 		return -1;
662 	if (b == fServicesItem)
663 		return 1;
664 */
665 	return _CompareListItems(a, b);
666 }
667 
668 
669 /*static*/ int
670 NetworkWindow::_CompareListItems(const BListItem* a, const BListItem* b)
671 {
672 	if (a == b)
673 		return 0;
674 
675 	const char* nameA = _ItemName(a);
676 	const char* nameB = _ItemName(b);
677 
678 	if (nameA != NULL && nameB != NULL)
679 		return strcasecmp(nameA, nameB);
680 	if (nameA != NULL)
681 		return 1;
682 	if (nameB != NULL)
683 		return -1;
684 
685 	return (addr_t)a > (addr_t)b ? 1 : -1;
686 }
687