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