fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / preferences / mail / AutoConfigWindow.cpp
blob1f8f33c9dc4dabba1dd2e9ba44f56e431327f7f0
1 /*
2 * Copyright 2007-2012, Haiku, Inc. All rights reserved.
3 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4 * Distributed under the terms of the MIT License.
5 */
8 #include "AutoConfigWindow.h"
10 #include "AutoConfig.h"
11 #include "AutoConfigView.h"
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>
23 #include <crypt.h>
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "AutoConfigWindow"
30 AutoConfigWindow::AutoConfigWindow(BRect rect, ConfigWindow *parent)
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)
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);
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);
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);
65 fBoxRect = Bounds();
66 fBoxRect.InsetBy(5,5);
67 fBoxRect.bottom-= buttonHeight + 5;
69 fMainView = new AutoConfigView(fBoxRect, fAutoConfig);
70 fMainView->SetLabel(B_TRANSLATE("Account settings"));
71 fRootView->AddChild(fMainView);
73 // Add a shortcut to close the window using Command-W
74 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
78 AutoConfigWindow::~AutoConfigWindow()
84 void
85 AutoConfigWindow::MessageReceived(BMessage* msg)
87 status_t status = B_ERROR;
88 BAlert* invalidMailAlert = NULL;
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->SetFlags(invalidMailAlert->Flags() | B_CLOSE_ON_ESCAPE);
99 invalidMailAlert->Go();
100 return;
102 if (fAutoConfigServer) {
103 status = fAutoConfig.GetInfoFromMailAddress(
104 fAccountInfo.email.String(),
105 &fAccountInfo.providerInfo);
107 if (status == B_OK) {
108 fParentWindow->Lock();
109 GenerateBasicAccount();
110 fParentWindow->Unlock();
111 Quit();
113 fMainConfigState = false;
114 fServerConfigState = true;
115 fMainView->Hide();
117 fServerView = new ServerSettingsView(fBoxRect, fAccountInfo);
118 fRootView->AddChild(fServerView);
120 fBackButton->SetEnabled(true);
121 fNextButton->SetLabel(B_TRANSLATE("Finish"));
122 } else {
123 fServerView->GetServerInfo(fAccountInfo);
124 fParentWindow->Lock();
125 GenerateBasicAccount();
126 fParentWindow->Unlock();
127 Quit();
129 break;
131 case kBackMsg:
132 if (fServerConfigState) {
133 fServerView->GetServerInfo(fAccountInfo);
135 fMainConfigState = true;
136 fServerConfigState = false;
138 fRootView->RemoveChild(fServerView);
139 delete fServerView;
141 fMainView->Show();
142 fBackButton->SetEnabled(false);
144 break;
146 case kServerChangedMsg:
147 fAutoConfigServer = false;
148 break;
150 default:
151 BWindow::MessageReceived(msg);
152 break;
157 bool
158 AutoConfigWindow::QuitRequested()
160 return true;
164 BMailAccountSettings*
165 AutoConfigWindow::GenerateBasicAccount()
167 if (!fAccount) {
168 fParentWindow->Lock();
169 fAccount = fParentWindow->AddAccount();
170 fParentWindow->Unlock();
173 fAccount->SetName(fAccountInfo.accountName.String());
174 fAccount->SetRealName(fAccountInfo.name.String());
175 fAccount->SetReturnAddress(fAccountInfo.email.String());
177 BMessage& inboundArchive = fAccount->InboundSettings();
178 inboundArchive.MakeEmpty();
179 BString inServerName;
180 int32 authType = 0;
181 int32 ssl = 0;
182 if (fAccountInfo.inboundType == IMAP) {
183 inServerName = fAccountInfo.providerInfo.imap_server;
184 ssl = fAccountInfo.providerInfo.ssl_imap;
185 fAccount->SetInboundAddOn("IMAP");
186 } else {
187 inServerName = fAccountInfo.providerInfo.pop_server;
188 authType = fAccountInfo.providerInfo.authentification_pop;
189 ssl = fAccountInfo.providerInfo.ssl_pop;
190 fAccount->SetInboundAddOn("POP3");
192 inboundArchive.AddString("server", inServerName);
193 inboundArchive.AddInt32("auth_method", authType);
194 inboundArchive.AddInt32("flavor", ssl);
195 inboundArchive.AddString("username", fAccountInfo.loginName);
196 set_passwd(&inboundArchive, "cpasswd", fAccountInfo.password);
197 inboundArchive.AddBool("leave_mail_on_server", true);
198 inboundArchive.AddBool("delete_remote_when_local", true);
200 BMessage& outboundArchive = fAccount->OutboundSettings();
201 outboundArchive.MakeEmpty();
202 fAccount->SetOutboundAddOn("SMTP");
203 outboundArchive.AddString("server",
204 fAccountInfo.providerInfo.smtp_server);
205 outboundArchive.AddString("username", fAccountInfo.loginName);
206 set_passwd(&outboundArchive, "cpasswd", fAccountInfo.password);
207 outboundArchive.AddInt32("auth_method",
208 fAccountInfo.providerInfo.authentification_smtp);
209 outboundArchive.AddInt32("flavor",
210 fAccountInfo.providerInfo.ssl_smtp);
212 fParentWindow->Lock();
213 fParentWindow->AccountUpdated(fAccount);
214 fParentWindow->Unlock();
216 return fAccount;