2 * Copyright 2007-2015, Haiku, Inc. All rights reserved.
3 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4 * Distributed under the terms of the MIT License.
8 #include "AutoConfigWindow.h"
10 #include "AutoConfig.h"
11 #include "AutoConfigView.h"
14 #include <Application.h>
16 #include <Directory.h>
18 #include <FindDirectory.h>
19 #include <LayoutBuilder.h>
20 #include <MailSettings.h>
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "AutoConfigWindow"
31 const float kSpacing
= 10;
34 AutoConfigWindow::AutoConfigWindow(BRect rect
, ConfigWindow
*parent
)
36 BWindow(rect
, B_TRANSLATE("Create new account"), B_TITLED_WINDOW_LOOK
,
37 B_MODAL_APP_WINDOW_FEEL
, B_NOT_ZOOMABLE
| B_AVOID_FRONT
38 | B_AUTO_UPDATE_SIZE_LIMITS
, B_ALL_WORKSPACES
),
39 fParentWindow(parent
),
41 fMainConfigState(true),
42 fServerConfigState(false),
43 fAutoConfigServer(true)
45 fContainerView
= new BGroupView("config container");
47 fBackButton
= new BButton("back", B_TRANSLATE("Back"),
48 new BMessage(kBackMsg
));
49 fBackButton
->SetEnabled(false);
51 fNextButton
= new BButton("next", B_TRANSLATE("Next"),
52 new BMessage(kOkMsg
));
53 fNextButton
->MakeDefault(true);
55 fMainView
= new AutoConfigView(fAutoConfig
);
56 fMainView
->SetLabel(B_TRANSLATE("Account settings"));
57 fContainerView
->AddChild(fMainView
);
59 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
60 .SetInsets(B_USE_DEFAULT_SPACING
)
62 .AddGroup(B_HORIZONTAL
)
67 // Add a shortcut to close the window using Command-W
68 AddShortcut('W', B_COMMAND_KEY
, new BMessage(B_QUIT_REQUESTED
));
72 AutoConfigWindow::~AutoConfigWindow()
78 AutoConfigWindow::MessageReceived(BMessage
* msg
)
80 status_t status
= B_ERROR
;
81 BAlert
* invalidMailAlert
= NULL
;
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."),
91 invalidMailAlert
->SetFlags(invalidMailAlert
->Flags()
93 invalidMailAlert
->Go();
96 if (fAutoConfigServer
) {
97 status
= fAutoConfig
.GetInfoFromMailAddress(
98 fAccountInfo
.email
.String(),
99 &fAccountInfo
.providerInfo
);
101 if (status
== B_OK
) {
102 fParentWindow
->Lock();
103 GenerateBasicAccount();
104 fParentWindow
->Unlock();
107 fMainConfigState
= false;
108 fServerConfigState
= true;
111 fServerView
= new ServerSettingsView(fAccountInfo
);
112 fContainerView
->AddChild(fServerView
);
114 fBackButton
->SetEnabled(true);
115 fNextButton
->SetLabel(B_TRANSLATE("Finish"));
117 fServerView
->GetServerInfo(fAccountInfo
);
118 fParentWindow
->Lock();
119 GenerateBasicAccount();
120 fParentWindow
->Unlock();
126 if (fServerConfigState
) {
127 fServerView
->GetServerInfo(fAccountInfo
);
129 fMainConfigState
= true;
130 fServerConfigState
= false;
132 fContainerView
->RemoveChild(fServerView
);
136 fBackButton
->SetEnabled(false);
140 case kServerChangedMsg
:
141 fAutoConfigServer
= false;
145 BWindow::MessageReceived(msg
);
152 AutoConfigWindow::QuitRequested()
158 BMailAccountSettings
*
159 AutoConfigWindow::GenerateBasicAccount()
162 fParentWindow
->Lock();
163 fAccount
= fParentWindow
->AddAccount();
164 fParentWindow
->Unlock();
167 fAccount
->SetName(fAccountInfo
.accountName
.String());
168 fAccount
->SetRealName(fAccountInfo
.name
.String());
169 fAccount
->SetReturnAddress(fAccountInfo
.email
.String());
171 BMessage
& inboundArchive
= fAccount
->InboundSettings();
172 inboundArchive
.MakeEmpty();
173 BString inServerName
;
176 if (fAccountInfo
.inboundType
== IMAP
) {
177 inServerName
= fAccountInfo
.providerInfo
.imap_server
;
178 ssl
= fAccountInfo
.providerInfo
.ssl_imap
;
179 fAccount
->SetInboundAddOn("IMAP");
181 inServerName
= fAccountInfo
.providerInfo
.pop_server
;
182 authType
= fAccountInfo
.providerInfo
.authentification_pop
;
183 ssl
= fAccountInfo
.providerInfo
.ssl_pop
;
184 fAccount
->SetInboundAddOn("POP3");
186 inboundArchive
.AddString("server", inServerName
);
187 inboundArchive
.AddInt32("auth_method", authType
);
188 inboundArchive
.AddInt32("flavor", ssl
);
189 inboundArchive
.AddString("username", fAccountInfo
.loginName
);
190 set_passwd(&inboundArchive
, "cpasswd", fAccountInfo
.password
);
191 inboundArchive
.AddBool("leave_mail_on_server", true);
192 inboundArchive
.AddBool("delete_remote_when_local", true);
194 BMessage
& outboundArchive
= fAccount
->OutboundSettings();
195 outboundArchive
.MakeEmpty();
196 fAccount
->SetOutboundAddOn("SMTP");
197 outboundArchive
.AddString("server",
198 fAccountInfo
.providerInfo
.smtp_server
);
199 outboundArchive
.AddString("username", fAccountInfo
.loginName
);
200 set_passwd(&outboundArchive
, "cpasswd", fAccountInfo
.password
);
201 outboundArchive
.AddInt32("auth_method",
202 fAccountInfo
.providerInfo
.authentification_smtp
);
203 outboundArchive
.AddInt32("flavor",
204 fAccountInfo
.providerInfo
.ssl_smtp
);
206 fParentWindow
->Lock();
207 fParentWindow
->AccountUpdated(fAccount
);
208 fParentWindow
->Unlock();