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 <NetworkSettingsAddOn.h>
12 #include <StringItem.h>
13
14 #include "InterfaceAddressView.h"
15
16
17 using namespace BNetworkKit;
18
19
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "IPv4InterfaceAddOn"
22
23
24 class IPv4InterfaceAddOn : public BNetworkSettingsAddOn {
25 public:
26 IPv4InterfaceAddOn(image_id image,
27 BNetworkSettings& settings);
28 virtual ~IPv4InterfaceAddOn();
29
30 virtual BNetworkSettingsInterfaceItem*
31 CreateNextInterfaceItem(uint32& cookie,
32 const char* interface);
33 };
34
35
36 class IPv4InterfaceItem : public BNetworkSettingsInterfaceItem {
37 public:
38 IPv4InterfaceItem(const char* interface,
39 BNetworkSettings& settings);
40 virtual ~IPv4InterfaceItem();
41
42 virtual BListItem* ListItem();
43 virtual BView* View();
44
45 virtual status_t Revert();
46 virtual bool IsRevertable();
47
48 virtual void ConfigurationUpdated(const BMessage& message);
49
50 private:
51 BNetworkSettings& fSettings;
52 BNetworkInterfaceListItem*
53 fItem;
54 InterfaceAddressView*
55 fView;
56 };
57
58
59 // #pragma mark -
60
61
IPv4InterfaceItem(const char * interface,BNetworkSettings & settings)62 IPv4InterfaceItem::IPv4InterfaceItem(const char* interface,
63 BNetworkSettings& settings)
64 :
65 BNetworkSettingsInterfaceItem(interface),
66 fSettings(settings),
67 fItem(new BNetworkInterfaceListItem(AF_INET, Interface(),
68 B_TRANSLATE("IPv4"), settings)),
69 fView(NULL)
70 {
71 }
72
73
~IPv4InterfaceItem()74 IPv4InterfaceItem::~IPv4InterfaceItem()
75 {
76 if (fView->Parent() == NULL)
77 delete fView;
78
79 delete fItem;
80 }
81
82
83 BListItem*
ListItem()84 IPv4InterfaceItem::ListItem()
85 {
86 return fItem;
87 }
88
89
90 BView*
View()91 IPv4InterfaceItem::View()
92 {
93 if (fView == NULL)
94 fView = new InterfaceAddressView(AF_INET, Interface(), fSettings);
95
96 return fView;
97 }
98
99
100 status_t
Revert()101 IPv4InterfaceItem::Revert()
102 {
103 return fView != NULL ? fView->Revert() : B_OK;
104 }
105
106
107 bool
IsRevertable()108 IPv4InterfaceItem::IsRevertable()
109 {
110 return fView != NULL ? fView->IsRevertable() : false;
111 }
112
113
114 void
ConfigurationUpdated(const BMessage & message)115 IPv4InterfaceItem::ConfigurationUpdated(const BMessage& message)
116 {
117 if (fView != NULL)
118 fView->ConfigurationUpdated(message);
119 }
120
121
122 // #pragma mark -
123
124
IPv4InterfaceAddOn(image_id image,BNetworkSettings & settings)125 IPv4InterfaceAddOn::IPv4InterfaceAddOn(image_id image,
126 BNetworkSettings& settings)
127 :
128 BNetworkSettingsAddOn(image, settings)
129 {
130 }
131
132
~IPv4InterfaceAddOn()133 IPv4InterfaceAddOn::~IPv4InterfaceAddOn()
134 {
135 }
136
137
138 BNetworkSettingsInterfaceItem*
CreateNextInterfaceItem(uint32 & cookie,const char * interface)139 IPv4InterfaceAddOn::CreateNextInterfaceItem(uint32& cookie,
140 const char* interface)
141 {
142 if (cookie++ == 0)
143 return new IPv4InterfaceItem(interface, Settings());
144
145 return NULL;
146 }
147
148
149 // #pragma mark -
150
151
152 extern "C"
153 BNetworkSettingsAddOn*
instantiate_network_settings_add_on(image_id image,BNetworkSettings & settings)154 instantiate_network_settings_add_on(image_id image, BNetworkSettings& settings)
155 {
156 return new IPv4InterfaceAddOn(image, settings);
157 }
158