xref: /haiku/src/add-ons/network_settings/vpn/VPNInterfaceAddOn.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2015-2017 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, <axeld@pinc-software.de>
7  *		Ingo Weinhold <ingo_weinhold@gmx.de>
8  *		Alexander von Gluck IV <kallisti5@unixzen.com>
9  */
10 
11 
12 #include <pwd.h>
13 
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <LayoutBuilder.h>
17 #include <NetworkSettings.h>
18 #include <NetworkSettingsAddOn.h>
19 #include <StringView.h>
20 #include <TextView.h>
21 
22 #include <NetServer.h>
23 #include <RegistrarDefs.h>
24 #include <user_group.h>
25 #include <util/KMessage.h>
26 
27 #include "InterfaceListItem.h"
28 #include "InterfaceView.h"
29 
30 
31 using namespace BNetworkKit;
32 
33 
34 #undef B_TRANSLATION_CONTEXT
35 #define B_TRANSLATION_CONTEXT "VPNInterfaceAddOn"
36 
37 
38 static const uint32 kMsgToggleService = 'tgls';
39 
40 
41 class VPNInterfaceAddOn : public BNetworkSettingsAddOn {
42 public:
43 								VPNInterfaceAddOn(image_id image,
44 									BNetworkSettings& settings);
45 	virtual						~VPNInterfaceAddOn();
46 
47 	virtual	BNetworkSettingsItem*
48 								CreateNextItem(uint32& cookie);
49 };
50 
51 
52 class VPNInterfaceView : public InterfaceView {
53 public:
54 								VPNInterfaceView(BNetworkSettings& settings);
55 	virtual						~VPNInterfaceView();
56 };
57 
58 
59 class VPNInterfaceItem : public BNetworkSettingsItem {
60 public:
61 								VPNInterfaceItem(BNetworkSettings& settings);
62 	virtual						~VPNInterfaceItem();
63 
64 	virtual	BNetworkSettingsType
65 								Type() const;
66 
67 	virtual BListItem*			ListItem();
68 	virtual BView*				View();
69 
70 	virtual status_t			Revert();
71 	virtual bool				IsRevertable();
72 
73 private:
74 			BNetworkSettings&	fSettings;
75 			BListItem*			fItem;
76 			InterfaceView*		fView;
77 };
78 
79 
80 // #pragma mark -
81 
82 
83 VPNInterfaceView::VPNInterfaceView(BNetworkSettings& settings)
84 	:
85 	InterfaceView()
86 {
87 }
88 
89 
90 VPNInterfaceView::~VPNInterfaceView()
91 {
92 }
93 
94 
95 // #pragma mark -
96 
97 
98 VPNInterfaceItem::VPNInterfaceItem(BNetworkSettings& settings)
99 	:
100 	fSettings(settings),
101 	fItem(new InterfaceListItem("My Awesome VPN",
102 		B_NETWORK_INTERFACE_TYPE_VPN)),
103 	fView(NULL)
104 {
105 }
106 
107 
108 VPNInterfaceItem::~VPNInterfaceItem()
109 {
110 	if (fView->Parent() == NULL)
111 		delete fView;
112 
113 	delete fItem;
114 }
115 
116 
117 BNetworkSettingsType
118 VPNInterfaceItem::Type() const
119 {
120 	return B_NETWORK_SETTINGS_TYPE_VPN;
121 }
122 
123 
124 BListItem*
125 VPNInterfaceItem::ListItem()
126 {
127 	return fItem;
128 }
129 
130 
131 BView*
132 VPNInterfaceItem::View()
133 {
134 	if (fView == NULL)
135 		fView = new VPNInterfaceView(fSettings);
136 
137 	return fView;
138 }
139 
140 
141 status_t
142 VPNInterfaceItem::Revert()
143 {
144 	return B_OK;
145 }
146 
147 bool
148 VPNInterfaceItem::IsRevertable()
149 {
150 	// TODO
151 	return false;
152 }
153 
154 
155 // #pragma mark -
156 
157 
158 VPNInterfaceAddOn::VPNInterfaceAddOn(image_id image,
159 	BNetworkSettings& settings)
160 	:
161 	BNetworkSettingsAddOn(image, settings)
162 {
163 }
164 
165 
166 VPNInterfaceAddOn::~VPNInterfaceAddOn()
167 {
168 }
169 
170 
171 BNetworkSettingsItem*
172 VPNInterfaceAddOn::CreateNextItem(uint32& cookie)
173 {
174 	if (cookie++ == 0)
175 		return new VPNInterfaceItem(Settings());
176 
177 	return NULL;
178 }
179 
180 
181 // #pragma mark -
182 
183 
184 extern "C"
185 BNetworkSettingsAddOn*
186 instantiate_network_settings_add_on(image_id image, BNetworkSettings& settings)
187 {
188 	return new VPNInterfaceAddOn(image, settings);
189 }
190