xref: /haiku/src/preferences/mail/AutoConfigWindow.cpp (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
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 	fRootView->AddChild(fNextButton);
49 
50 	fBoxRect = Bounds();
51 	fBoxRect.InsetBy(5,5);
52 	fBoxRect.bottom-= buttonHeight + 5;
53 
54 	fMainView = new AutoConfigView(fBoxRect, fAutoConfig);
55 	fMainView->SetLabel("Account Settings");
56 	fRootView->AddChild(fMainView);
57 }
58 
59 
60 AutoConfigWindow::~AutoConfigWindow()
61 {
62 
63 }
64 
65 
66 void
67 AutoConfigWindow::MessageReceived(BMessage* msg)
68 {
69 	status_t status = B_ERROR;
70 	BAlert* invalidMailAlert = NULL;
71 	switch (msg->what)
72 	{
73 		case kOkMsg:
74 		{
75 			if (fMainConfigState) {
76 				fMainView->GetBasicAccountInfo(fAccountInfo);
77 				if (!fMainView->IsValidMailAddress(fAccountInfo.email)) {
78 					invalidMailAlert = new BAlert("invalidMailAlert",
79 													"Enter a valid e-mail address.",
80 													"Ok");
81 					invalidMailAlert->Go();
82 					return;
83 				}
84 				if (fAutoConfigServer) {
85 					status = fAutoConfig.GetInfoFromMailAddress(fAccountInfo.email.String(),
86 																&(fAccountInfo.providerInfo));
87 				}
88 				if(status == B_OK){
89 					fParentWindow->Lock();
90 					GenerateBasicAccount();
91 					fParentWindow->Unlock();
92 					Quit();
93 				}
94 				fMainConfigState = false;
95 				fServerConfigState = true;
96 				fMainView->Hide();
97 
98 				fServerView = new ServerSettingsView(fBoxRect, fAccountInfo);
99 				fRootView->AddChild(fServerView);
100 
101 				fBackButton->SetEnabled(true);
102 			}
103 			else{
104 				fServerView->GetServerInfo(fAccountInfo);
105 				fParentWindow->Lock();
106 				GenerateBasicAccount();
107 				fParentWindow->Unlock();
108 				Quit();
109 			}
110 			break;
111 		}
112 		case kBackMsg:
113 		{
114 			if(fServerConfigState){
115 				fServerView->GetServerInfo(fAccountInfo);
116 
117 				fMainConfigState = true;
118 				fServerConfigState = false;
119 
120 				fRootView->RemoveChild(fServerView);
121 				delete fServerView;
122 
123 				fMainView->Show();
124 				fBackButton->SetEnabled(false);
125 			}
126 			break;
127 		}
128 		case kServerChangedMsg:
129 		{
130 			fAutoConfigServer = false;
131 			break;
132 		}
133 		default:
134 			BWindow::MessageReceived(msg);
135 			break;
136 	}
137 }
138 
139 
140 bool
141 AutoConfigWindow::QuitRequested(void)
142 {
143 	return(TRUE);
144 }
145 
146 
147 
148 
149 Account*
150 AutoConfigWindow::GenerateBasicAccount()
151 {
152 	if(!fAccount){
153 		fAccount = Accounts::NewAccount();
154 		fAccount->SetType(fAccountInfo.type);
155 		fAccount->SetName(fAccountInfo.accountName.String());
156 		fAccount->SetRealName(fAccountInfo.name.String());
157 		fAccount->SetReturnAddress(fAccountInfo.email.String());
158 
159 		BMailChain *inbound = fAccount->Inbound();
160 
161 		BString inServerName;
162 		int32 authType = 0;
163 		int32 ssl = 0;
164 		if (fAccountInfo.inboundType == IMAP) {
165 			inServerName = fAccountInfo.providerInfo.imap_server;
166 			ssl = fAccountInfo.providerInfo.ssl_imap;
167 		}
168 		else {
169 			inServerName = fAccountInfo.providerInfo.pop_server;
170 			authType = fAccountInfo.providerInfo.authentification_pop;
171 			ssl = fAccountInfo.providerInfo.ssl_pop;
172 		}
173 		if (fAccountInfo.type == INBOUND_TYPE
174 				|| fAccountInfo.type == IN_AND_OUTBOUND_TYPE)
175 		{
176 			BMessage inboundArchive;
177 			inboundArchive.AddString("server", inServerName);
178 			inboundArchive.AddInt32("auth_method", authType);
179 			inboundArchive.AddInt32("flavor", ssl);
180 			inboundArchive.AddString("username", fAccountInfo.loginName);
181 			inboundArchive.AddString("password", fAccountInfo.password);
182 			inboundArchive.AddBool("leave_mail_on_server", true);
183 			inboundArchive.AddBool("delete_remote_when_local", true);
184 			inbound->SetFilter(0, inboundArchive, fAccountInfo.inboundProtocol);
185 		}
186 
187 		if (fAccountInfo.type == OUTBOUND_TYPE
188 				|| fAccountInfo.type == IN_AND_OUTBOUND_TYPE)
189 		{
190 			BMailChain *outbound = fAccount->Outbound();
191 			BMessage outboundArchive;
192 			outboundArchive.AddString("server",
193 										fAccountInfo.providerInfo.smtp_server);
194 			outboundArchive.AddString("username", fAccountInfo.loginName);
195 			outboundArchive.AddString("password", fAccountInfo.password);
196 			outboundArchive.AddInt32("auth_method",
197 										fAccountInfo.providerInfo.authentification_smtp);
198 			outboundArchive.AddInt32("flavor", fAccountInfo.providerInfo.ssl_smtp);
199 			outbound->SetFilter(1, outboundArchive,
200 									fAccountInfo.outboundProtocol);
201 		}
202 	}
203 	return fAccount;
204 }
205