xref: /haiku/src/preferences/mail/ConfigViews.cpp (revision 3ecb7fb4415b319b6aac606551d51efad21037df)
1 /*
2  * Copyright 2007-2012, Haiku, Inc. All rights reserved.
3  * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 //!	Config views for the account, protocols, and filters.
10 
11 
12 #include "ConfigViews.h"
13 
14 #include <Alert.h>
15 #include <Button.h>
16 #include <Catalog.h>
17 #include <Directory.h>
18 #include <Entry.h>
19 #include <FindDirectory.h>
20 #include <ListView.h>
21 #include <LayoutBuilder.h>
22 #include <Locale.h>
23 #include <Looper.h>
24 #include <MenuField.h>
25 #include <MenuItem.h>
26 #include <Path.h>
27 #include <PopUpMenu.h>
28 #include <TextControl.h>
29 
30 #include <string.h>
31 
32 #include <MailSettings.h>
33 
34 #include "FilterConfigView.h"
35 
36 
37 #undef B_TRANSLATION_CONTEXT
38 #define B_TRANSLATION_CONTEXT "Config Views"
39 
40 
41 // AccountConfigView
42 const uint32 kMsgAccountNameChanged = 'anmc';
43 
44 // ProtocolsConfigView
45 const uint32 kMsgProtocolChanged = 'prch';
46 
47 
48 // #pragma mark -
49 
50 
51 AccountConfigView::AccountConfigView(BMailAccountSettings* account)
52 	:
53 	BBox("account"),
54 	fAccount(account)
55 {
56 	SetLabel(B_TRANSLATE("Account settings"));
57 
58 	fNameControl = new BTextControl(NULL, B_TRANSLATE("Account name:"), NULL,
59 		new BMessage(kMsgAccountNameChanged));
60 	fRealNameControl = new BTextControl(NULL, B_TRANSLATE("Real name:"), NULL,
61 		NULL);
62 	fReturnAddressControl = new BTextControl(NULL,
63 		B_TRANSLATE("Return address:"), NULL, NULL);
64 
65 	BView* contents = new BView(NULL, 0);
66 	AddChild(contents);
67 
68 	BLayoutBuilder::Grid<>(contents, 0.f)
69 		.SetInsets(B_USE_DEFAULT_SPACING)
70 		.Add(fNameControl->CreateLabelLayoutItem(), 0, 0)
71 		.Add(fNameControl->CreateTextViewLayoutItem(), 1, 0)
72 		.Add(fRealNameControl->CreateLabelLayoutItem(), 0, 1)
73 		.Add(fRealNameControl->CreateTextViewLayoutItem(), 1, 1)
74 		.Add(fReturnAddressControl->CreateLabelLayoutItem(), 0, 2)
75 		.Add(fReturnAddressControl->CreateTextViewLayoutItem(), 1, 2)
76 		.AddGlue(0, 3);
77 }
78 
79 
80 void
81 AccountConfigView::DetachedFromWindow()
82 {
83 	fAccount->SetName(fNameControl->Text());
84 	fAccount->SetRealName(fRealNameControl->Text());
85 	fAccount->SetReturnAddress(fReturnAddressControl->Text());
86 }
87 
88 
89 void
90 AccountConfigView::AttachedToWindow()
91 {
92 	UpdateViews();
93 	fNameControl->SetTarget(this);
94 }
95 
96 
97 void
98 AccountConfigView::MessageReceived(BMessage *msg)
99 {
100 	switch (msg->what) {
101 		case kMsgAccountNameChanged:
102 			fAccount->SetName(fNameControl->Text());
103 			break;
104 
105 		default:
106 			BView::MessageReceived(msg);
107 	}
108 }
109 
110 
111 void
112 AccountConfigView::UpdateViews()
113 {
114 	fNameControl->SetText(fAccount->Name());
115 	fRealNameControl->SetText(fAccount->RealName());
116 	fReturnAddressControl->SetText(fAccount->ReturnAddress());
117 }
118 
119 
120 // #pragma mark -
121 
122 
123 ProtocolSettingsView::ProtocolSettingsView(const entry_ref& ref,
124 	const BMailAccountSettings& accountSettings,
125 	BMailProtocolSettings& settings)
126 	:
127 	BBox("protocol"),
128 	fSettings(settings),
129 	fSettingsView(NULL)
130 {
131 	status_t status = _CreateSettingsView(ref, accountSettings, settings);
132 	BView* view = fSettingsView;
133 
134 	if (status == B_OK) {
135 		SetLabel(ref.name);
136 	} else {
137 		BString text(B_TRANSLATE("An error occurred while creating the "
138 			"config view: %error."));
139 		text.ReplaceAll("%error", strerror(status));
140 		view = new BStringView("error", text.String());
141 
142 		SetLabel(B_TRANSLATE("Error!"));
143 	}
144 
145 	BView* contents = new BView(NULL, 0);
146 	AddChild(contents);
147 
148 	BLayoutBuilder::Group<>(contents, B_VERTICAL)
149 		.SetInsets(B_USE_DEFAULT_SPACING)
150 		.Add(view)
151 		.AddGlue();
152 }
153 
154 
155 void
156 ProtocolSettingsView::DetachedFromWindow()
157 {
158 	if (fSettingsView == NULL)
159 		return;
160 
161 	if (fSettingsView->SaveInto(fSettings) != B_OK)
162 		return;
163 
164 	// We need to remove the settings view before unloading its add-on
165 	fSettingsView->RemoveSelf();
166 	delete fSettingsView;
167 	fSettingsView = NULL;
168 	unload_add_on(fImage);
169 }
170 
171 
172 status_t
173 ProtocolSettingsView::_CreateSettingsView(const entry_ref& ref,
174 	const BMailAccountSettings& accountSettings,
175 	BMailProtocolSettings& settings)
176 {
177 	BMailSettingsView* (*instantiateConfig)(
178 		const BMailAccountSettings& accountSettings,
179 		BMailProtocolSettings& settings);
180 	BPath path(&ref);
181 	image_id image = load_add_on(path.Path());
182 	if (image < 0)
183 		return image;
184 
185 	if (get_image_symbol(image, "instantiate_protocol_settings_view",
186 			B_SYMBOL_TYPE_TEXT, (void**)&instantiateConfig) != B_OK) {
187 		unload_add_on(image);
188 		return B_MISSING_SYMBOL;
189 	}
190 
191 	fImage = image;
192 	fSettingsView = instantiateConfig(accountSettings, settings);
193 	return B_OK;
194 }
195