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.
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 <MailSettings.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
),
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
));
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
);
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()
85 AutoConfigWindow::MessageReceived(BMessage
* msg
)
87 status_t status
= B_ERROR
;
88 BAlert
* invalidMailAlert
= NULL
;
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."),
98 invalidMailAlert
->SetFlags(invalidMailAlert
->Flags() | B_CLOSE_ON_ESCAPE
);
99 invalidMailAlert
->Go();
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();
113 fMainConfigState
= false;
114 fServerConfigState
= true;
117 fServerView
= new ServerSettingsView(fBoxRect
, fAccountInfo
);
118 fRootView
->AddChild(fServerView
);
120 fBackButton
->SetEnabled(true);
121 fNextButton
->SetLabel(B_TRANSLATE("Finish"));
123 fServerView
->GetServerInfo(fAccountInfo
);
124 fParentWindow
->Lock();
125 GenerateBasicAccount();
126 fParentWindow
->Unlock();
132 if (fServerConfigState
) {
133 fServerView
->GetServerInfo(fAccountInfo
);
135 fMainConfigState
= true;
136 fServerConfigState
= false;
138 fRootView
->RemoveChild(fServerView
);
142 fBackButton
->SetEnabled(false);
146 case kServerChangedMsg
:
147 fAutoConfigServer
= false;
151 BWindow::MessageReceived(msg
);
158 AutoConfigWindow::QuitRequested()
164 BMailAccountSettings
*
165 AutoConfigWindow::GenerateBasicAccount()
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
;
182 if (fAccountInfo
.inboundType
== IMAP
) {
183 inServerName
= fAccountInfo
.providerInfo
.imap_server
;
184 ssl
= fAccountInfo
.providerInfo
.ssl_imap
;
185 fAccount
->SetInboundAddOn("IMAP");
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();