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