xref: /haiku/src/preferences/mail/AutoConfigWindow.cpp (revision 323b65468e5836bb27a5e373b14027d902349437)
1 /*
2  * Copyright 2007-2011, 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 <MailSettings.h>
20 #include <Message.h>
21 #include <Path.h>
22 
23 #include <crypt.h>
24 
25 
26 #undef B_TRANSLATE_CONTEXT
27 #define B_TRANSLATE_CONTEXT "AutoConfigWindow"
28 
29 
30 AutoConfigWindow::AutoConfigWindow(BRect rect, ConfigWindow *parent)
31 	:
32 	BWindow(rect, B_TRANSLATE("Create new account"), B_TITLED_WINDOW_LOOK,
33 		B_MODAL_APP_WINDOW_FEEL,
34 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AVOID_FRONT, B_ALL_WORKSPACES),
35 	fParentWindow(parent),
36 	fAccount(NULL),
37 	fMainConfigState(true),
38 	fServerConfigState(false),
39 	fAutoConfigServer(true)
40 {
41 	fRootView = new BView(Bounds(), "root auto config view",
42 		B_FOLLOW_ALL_SIDES, B_NAVIGABLE);
43 	fRootView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
44 	AddChild(fRootView);
45 
46 	int32 buttonHeight = 25;
47 	int32 buttonWidth = 50;
48 	BRect buttonRect = Bounds();
49 	buttonRect.InsetBy(5,5);
50 	buttonRect.top = buttonRect.bottom - buttonHeight;
51 	buttonRect.left = buttonRect.right - 2 * buttonWidth - 5;
52 	buttonRect.right = buttonRect.left + buttonWidth;
53 	fBackButton = new BButton(buttonRect, "back", B_TRANSLATE("Back"),
54 		new BMessage(kBackMsg));
55 	fBackButton->SetEnabled(false);
56 	fRootView->AddChild(fBackButton);
57 
58 	buttonRect.left += 5 + buttonWidth;
59 	buttonRect.right = buttonRect.left + buttonWidth;
60 	fNextButton = new BButton(buttonRect, "next", B_TRANSLATE("Next"),
61 		new BMessage(kOkMsg));
62 	fNextButton->MakeDefault(true);
63 	fRootView->AddChild(fNextButton);
64 
65 	fBoxRect = Bounds();
66 	fBoxRect.InsetBy(5,5);
67 	fBoxRect.bottom-= buttonHeight + 5;
68 
69 	fMainView = new AutoConfigView(fBoxRect, fAutoConfig);
70 	fMainView->SetLabel(B_TRANSLATE("Account settings"));
71 	fRootView->AddChild(fMainView);
72 
73 	// Add a shortcut to close the window using Command-W
74 	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
75 }
76 
77 
78 AutoConfigWindow::~AutoConfigWindow()
79 {
80 
81 }
82 
83 
84 void
85 AutoConfigWindow::MessageReceived(BMessage* msg)
86 {
87 	status_t status = B_ERROR;
88 	BAlert* invalidMailAlert = NULL;
89 
90 	switch (msg->what) {
91 		case kOkMsg:
92 			if (fMainConfigState) {
93 				fMainView->GetBasicAccountInfo(fAccountInfo);
94 				if (!fMainView->IsValidMailAddress(fAccountInfo.email)) {
95 					invalidMailAlert = new BAlert("invalidMailAlert",
96 						B_TRANSLATE("Enter a valid e-mail address."),
97 						B_TRANSLATE("OK"));
98 					invalidMailAlert->Go();
99 					return;
100 				}
101 				if (fAutoConfigServer) {
102 					status = fAutoConfig.GetInfoFromMailAddress(
103 						fAccountInfo.email.String(),
104 						&fAccountInfo.providerInfo);
105 				}
106 				if (status == B_OK) {
107 					fParentWindow->Lock();
108 					GenerateBasicAccount();
109 					fParentWindow->Unlock();
110 					Quit();
111 				}
112 				fMainConfigState = false;
113 				fServerConfigState = true;
114 				fMainView->Hide();
115 
116 				fServerView = new ServerSettingsView(fBoxRect, fAccountInfo);
117 				fRootView->AddChild(fServerView);
118 
119 				fBackButton->SetEnabled(true);
120 				fNextButton->SetLabel(B_TRANSLATE("Finish"));
121 			} else {
122 				fServerView->GetServerInfo(fAccountInfo);
123 				fParentWindow->Lock();
124 				GenerateBasicAccount();
125 				fParentWindow->Unlock();
126 				Quit();
127 			}
128 			break;
129 
130 		case kBackMsg:
131 			if (fServerConfigState) {
132 				fServerView->GetServerInfo(fAccountInfo);
133 
134 				fMainConfigState = true;
135 				fServerConfigState = false;
136 
137 				fRootView->RemoveChild(fServerView);
138 				delete fServerView;
139 
140 				fMainView->Show();
141 				fBackButton->SetEnabled(false);
142 			}
143 			break;
144 
145 		case kServerChangedMsg:
146 			fAutoConfigServer = false;
147 			break;
148 
149 		default:
150 			BWindow::MessageReceived(msg);
151 			break;
152 	}
153 }
154 
155 
156 bool
157 AutoConfigWindow::QuitRequested()
158 {
159 	return true;
160 }
161 
162 
163 BMailAccountSettings*
164 AutoConfigWindow::GenerateBasicAccount()
165 {
166 	if (!fAccount) {
167 		fParentWindow->Lock();
168 		fAccount = fParentWindow->AddAccount();
169 		fParentWindow->Unlock();
170 	}
171 
172 	fAccount->SetName(fAccountInfo.accountName.String());
173 	fAccount->SetRealName(fAccountInfo.name.String());
174 	fAccount->SetReturnAddress(fAccountInfo.email.String());
175 
176 	BMessage& inboundArchive = fAccount->InboundSettings().EditSettings();
177 	inboundArchive.MakeEmpty();
178 	BString inServerName;
179 	int32 authType = 0;
180 	int32 ssl = 0;
181 	if (fAccountInfo.inboundType == IMAP) {
182 		inServerName = fAccountInfo.providerInfo.imap_server;
183 		ssl = fAccountInfo.providerInfo.ssl_imap;
184 		fAccount->SetInboundAddon("IMAP");
185 	} else {
186 		inServerName = fAccountInfo.providerInfo.pop_server;
187 		authType = fAccountInfo.providerInfo.authentification_pop;
188 		ssl = fAccountInfo.providerInfo.ssl_pop;
189 		fAccount->SetInboundAddon("POP3");
190 	}
191 	inboundArchive.AddString("server", inServerName);
192 	inboundArchive.AddInt32("auth_method", authType);
193 	inboundArchive.AddInt32("flavor", ssl);
194 	inboundArchive.AddString("username", fAccountInfo.loginName);
195 	set_passwd(&inboundArchive, "cpasswd", fAccountInfo.password);
196 	inboundArchive.AddBool("leave_mail_on_server", true);
197 	inboundArchive.AddBool("delete_remote_when_local", true);
198 
199 	BMessage& outboundArchive = fAccount->OutboundSettings().EditSettings();
200 	outboundArchive.MakeEmpty();
201 	fAccount->SetOutboundAddon("SMTP");
202 	outboundArchive.AddString("server",
203 		fAccountInfo.providerInfo.smtp_server);
204 	outboundArchive.AddString("username", fAccountInfo.loginName);
205 	set_passwd(&outboundArchive, "cpasswd", fAccountInfo.password);
206 	outboundArchive.AddInt32("auth_method",
207 		fAccountInfo.providerInfo.authentification_smtp);
208 	outboundArchive.AddInt32("flavor",
209 		fAccountInfo.providerInfo.ssl_smtp);
210 
211 	fParentWindow->Lock();
212 	fParentWindow->AccountUpdated(fAccount);
213 	fParentWindow->Unlock();
214 
215 	return fAccount;
216 }
217