xref: /haiku/src/preferences/mail/AutoConfigWindow.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2  * Copyright 2007-2015, Haiku, Inc. All rights reserved.
3  * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "AutoConfigWindow.h"
9 
10 #include "AutoConfig.h"
11 #include "AutoConfigView.h"
12 
13 #include <Alert.h>
14 #include <Application.h>
15 #include <Catalog.h>
16 #include <Directory.h>
17 #include <File.h>
18 #include <FindDirectory.h>
19 #include <LayoutBuilder.h>
20 #include <MailSettings.h>
21 #include <Message.h>
22 #include <Path.h>
23 
24 #include <crypt.h>
25 
26 
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "AutoConfigWindow"
29 
30 
31 const float kSpacing = 10;
32 
33 
34 AutoConfigWindow::AutoConfigWindow(BRect rect, ConfigWindow *parent)
35 	:
36 	BWindow(rect, B_TRANSLATE("Create new account"), B_TITLED_WINDOW_LOOK,
37 		B_MODAL_APP_WINDOW_FEEL, B_NOT_ZOOMABLE | B_AVOID_FRONT
38 			| B_AUTO_UPDATE_SIZE_LIMITS, B_ALL_WORKSPACES),
39 	fParentWindow(parent),
40 	fAccount(NULL),
41 	fMainConfigState(true),
42 	fServerConfigState(false),
43 	fAutoConfigServer(true)
44 {
45 	fContainerView = new BGroupView("config container");
46 
47 	fBackButton = new BButton("back", B_TRANSLATE("Back"),
48 		new BMessage(kBackMsg));
49 	fBackButton->SetEnabled(false);
50 
51 	fNextButton = new BButton("next", B_TRANSLATE("Next"),
52 		new BMessage(kOkMsg));
53 	fNextButton->MakeDefault(true);
54 
55 	fMainView = new AutoConfigView(fAutoConfig);
56 	fMainView->SetLabel(B_TRANSLATE("Account settings"));
57 	fContainerView->AddChild(fMainView);
58 
59 	BLayoutBuilder::Group<>(this, B_VERTICAL)
60 		.SetInsets(B_USE_DEFAULT_SPACING)
61 		.Add(fContainerView)
62 		.AddGroup(B_HORIZONTAL)
63 			.AddGlue()
64 			.Add(fBackButton)
65 			.Add(fNextButton);
66 
67 	// Add a shortcut to close the window using Command-W
68 	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
69 }
70 
71 
72 AutoConfigWindow::~AutoConfigWindow()
73 {
74 }
75 
76 
77 void
78 AutoConfigWindow::MessageReceived(BMessage* msg)
79 {
80 	status_t status = B_ERROR;
81 	BAlert* invalidMailAlert = NULL;
82 
83 	switch (msg->what) {
84 		case kOkMsg:
85 			if (fMainConfigState) {
86 				fMainView->GetBasicAccountInfo(fAccountInfo);
87 				if (!fMainView->IsValidMailAddress(fAccountInfo.email)) {
88 					invalidMailAlert = new BAlert("invalidMailAlert",
89 						B_TRANSLATE("Enter a valid e-mail address."),
90 						B_TRANSLATE("OK"));
91 					invalidMailAlert->SetFlags(invalidMailAlert->Flags()
92 						| B_CLOSE_ON_ESCAPE);
93 					invalidMailAlert->Go();
94 					return;
95 				}
96 				if (fAutoConfigServer) {
97 					status = fAutoConfig.GetInfoFromMailAddress(
98 						fAccountInfo.email.String(),
99 						&fAccountInfo.providerInfo);
100 				}
101 				if (status == B_OK) {
102 					fParentWindow->Lock();
103 					GenerateBasicAccount();
104 					fParentWindow->Unlock();
105 					Quit();
106 				}
107 				fMainConfigState = false;
108 				fServerConfigState = true;
109 				fMainView->Hide();
110 
111 				fServerView = new ServerSettingsView(fAccountInfo);
112 				fContainerView->AddChild(fServerView);
113 
114 				fBackButton->SetEnabled(true);
115 				fNextButton->SetLabel(B_TRANSLATE("Finish"));
116 			} else {
117 				fServerView->GetServerInfo(fAccountInfo);
118 				fParentWindow->Lock();
119 				GenerateBasicAccount();
120 				fParentWindow->Unlock();
121 				Quit();
122 			}
123 			break;
124 
125 		case kBackMsg:
126 			if (fServerConfigState) {
127 				fServerView->GetServerInfo(fAccountInfo);
128 
129 				fMainConfigState = true;
130 				fServerConfigState = false;
131 
132 				fContainerView->RemoveChild(fServerView);
133 				delete fServerView;
134 
135 				fMainView->Show();
136 				fBackButton->SetEnabled(false);
137 			}
138 			break;
139 
140 		case kServerChangedMsg:
141 			fAutoConfigServer = false;
142 			break;
143 
144 		default:
145 			BWindow::MessageReceived(msg);
146 			break;
147 	}
148 }
149 
150 
151 bool
152 AutoConfigWindow::QuitRequested()
153 {
154 	return true;
155 }
156 
157 
158 BMailAccountSettings*
159 AutoConfigWindow::GenerateBasicAccount()
160 {
161 	if (!fAccount) {
162 		fParentWindow->Lock();
163 		fAccount = fParentWindow->AddAccount();
164 		fParentWindow->Unlock();
165 	}
166 
167 	fAccount->SetName(fAccountInfo.accountName.String());
168 	fAccount->SetRealName(fAccountInfo.name.String());
169 	fAccount->SetReturnAddress(fAccountInfo.email.String());
170 
171 	BMessage& inboundArchive = fAccount->InboundSettings();
172 	inboundArchive.MakeEmpty();
173 	BString inServerName;
174 	int32 authType = 0;
175 	int32 ssl = 0;
176 	if (fAccountInfo.inboundType == IMAP) {
177 		inServerName = fAccountInfo.providerInfo.imap_server;
178 		ssl = fAccountInfo.providerInfo.ssl_imap;
179 		fAccount->SetInboundAddOn("IMAP");
180 	} else {
181 		inServerName = fAccountInfo.providerInfo.pop_server;
182 		authType = fAccountInfo.providerInfo.authentification_pop;
183 		ssl = fAccountInfo.providerInfo.ssl_pop;
184 		fAccount->SetInboundAddOn("POP3");
185 	}
186 	inboundArchive.AddString("server", inServerName);
187 	inboundArchive.AddInt32("auth_method", authType);
188 	inboundArchive.AddInt32("flavor", ssl);
189 	inboundArchive.AddString("username", fAccountInfo.loginName);
190 	set_passwd(&inboundArchive, "cpasswd", fAccountInfo.password);
191 	inboundArchive.AddBool("leave_mail_on_server", true);
192 	inboundArchive.AddBool("delete_remote_when_local", true);
193 
194 	BMessage& outboundArchive = fAccount->OutboundSettings();
195 	outboundArchive.MakeEmpty();
196 	fAccount->SetOutboundAddOn("SMTP");
197 	outboundArchive.AddString("server",
198 		fAccountInfo.providerInfo.smtp_server);
199 	outboundArchive.AddString("username", fAccountInfo.loginName);
200 	set_passwd(&outboundArchive, "cpasswd", fAccountInfo.password);
201 	outboundArchive.AddInt32("auth_method",
202 		fAccountInfo.providerInfo.authentification_smtp);
203 	outboundArchive.AddInt32("flavor",
204 		fAccountInfo.providerInfo.ssl_smtp);
205 
206 	fParentWindow->Lock();
207 	fParentWindow->AccountUpdated(fAccount);
208 	fParentWindow->Unlock();
209 
210 	return fAccount;
211 }
212