xref: /haiku/src/add-ons/network_settings/telnetd/TelnetServiceAddOn.cpp (revision e711e6e42fd7ec3111ba9dc2324fa8efedd6674b)
1 /*
2  * Copyright 2015 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  */
8 
9 
10 #include <Catalog.h>
11 #include <NetworkSettings.h>
12 #include <NetworkSettingsAddOn.h>
13 
14 #include "ServiceListItem.h"
15 #include "ServiceView.h"
16 
17 
18 using namespace BNetworkKit;
19 
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "TelnetServiceAddOn"
23 
24 
25 class TelnetServiceAddOn : public BNetworkSettingsAddOn {
26 public:
27 								TelnetServiceAddOn(image_id image,
28 									BNetworkSettings& settings);
29 	virtual						~TelnetServiceAddOn();
30 
31 	virtual	BNetworkSettingsItem*
32 								CreateNextItem(uint32& cookie);
33 };
34 
35 
36 class TelnetServiceItem : public BNetworkSettingsItem {
37 public:
38 								TelnetServiceItem(BNetworkSettings& settings);
39 	virtual						~TelnetServiceItem();
40 
41 	virtual	BNetworkSettingsType
42 								Type() const;
43 
44 	virtual BListItem*			ListItem();
45 	virtual BView*				View();
46 
47 	virtual	status_t			Revert();
48 	virtual bool				IsRevertable();
49 
50 	virtual void				SettingsUpdated(uint32 which);
51 
52 private:
53 			BNetworkSettings&	fSettings;
54 			BListItem*			fItem;
55 			ServiceView*		fView;
56 };
57 
58 
59 // #pragma mark -
60 
61 
62 TelnetServiceItem::TelnetServiceItem(BNetworkSettings& settings)
63 	:
64 	fSettings(settings),
65 	fItem(new ServiceListItem("telnet", B_TRANSLATE("Telnet server"),
66 		settings)),
67 	fView(NULL)
68 {
69 }
70 
71 
72 TelnetServiceItem::~TelnetServiceItem()
73 {
74 	if (fView->Parent() == NULL)
75 		delete fView;
76 
77 	delete fItem;
78 }
79 
80 
81 BNetworkSettingsType
82 TelnetServiceItem::Type() const
83 {
84 	return B_NETWORK_SETTINGS_TYPE_SERVICE;
85 }
86 
87 
88 BListItem*
89 TelnetServiceItem::ListItem()
90 {
91 	return fItem;
92 }
93 
94 
95 BView*
96 TelnetServiceItem::View()
97 {
98 	if (fView == NULL) {
99 		fView = new ServiceView("telnet", "telnetd",
100 			B_TRANSLATE("Telnet server"),
101 			B_TRANSLATE("The Telnet server allows you to remotely access "
102 				"your machine with a terminal session using the telnet "
103 				"protocol.\n\nPlease note that it is an insecure and "
104 				"unencrypted connection."), fSettings);
105 	}
106 
107 	return fView;
108 }
109 
110 
111 status_t
112 TelnetServiceItem::Revert()
113 {
114 	return fView != NULL ? fView->Revert() : B_OK;
115 }
116 
117 
118 bool
119 TelnetServiceItem::IsRevertable()
120 {
121 	return fView != NULL ? fView->IsRevertable() : false;
122 }
123 
124 
125 void
126 TelnetServiceItem::SettingsUpdated(uint32 which)
127 {
128 	if (fView != NULL)
129 		fView->SettingsUpdated(which);
130 }
131 
132 
133 // #pragma mark -
134 
135 
136 TelnetServiceAddOn::TelnetServiceAddOn(image_id image,
137 	BNetworkSettings& settings)
138 	:
139 	BNetworkSettingsAddOn(image, settings)
140 {
141 }
142 
143 
144 TelnetServiceAddOn::~TelnetServiceAddOn()
145 {
146 }
147 
148 
149 BNetworkSettingsItem*
150 TelnetServiceAddOn::CreateNextItem(uint32& cookie)
151 {
152 	if (cookie++ == 0)
153 		return new TelnetServiceItem(Settings());
154 
155 	return NULL;
156 }
157 
158 
159 // #pragma mark -
160 
161 
162 extern "C"
163 BNetworkSettingsAddOn*
164 instantiate_network_settings_add_on(image_id image, BNetworkSettings& settings)
165 {
166 	return new TelnetServiceAddOn(image, settings);
167 }
168