1 /* 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. 5 */ 6 7 8 #include "AutoConfigWindow.h" 9 10 #include "AutoConfig.h" 11 #include "AutoConfigView.h" 12 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 <LayoutBuilder.h> 20 #include <MailSettings.h> 21 #include <Message.h> 22 #include <Path.h> 23 24 #include <crypt.h> 25 26 27 #undef B_TRANSLATION_CONTEXT 28 #define B_TRANSLATION_CONTEXT "AutoConfigWindow" 29 30 31 const float kSpacing = 10; 32 33 34 AutoConfigWindow::AutoConfigWindow(BRect rect, ConfigWindow *parent) 35 : 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), 40 fAccount(NULL), 41 fMainConfigState(true), 42 fServerConfigState(false), 43 fAutoConfigServer(true) 44 { 45 fContainerView = new BGroupView("config container"); 46 47 fBackButton = new BButton("back", B_TRANSLATE("Back"), 48 new BMessage(kBackMsg)); 49 fBackButton->SetEnabled(false); 50 51 fNextButton = new BButton("next", B_TRANSLATE("Next"), 52 new BMessage(kOkMsg)); 53 fNextButton->MakeDefault(true); 54 55 fMainView = new AutoConfigView(fAutoConfig); 56 fMainView->SetLabel(B_TRANSLATE("Account settings")); 57 fContainerView->AddChild(fMainView); 58 59 BLayoutBuilder::Group<>(this, B_VERTICAL) 60 .SetInsets(B_USE_DEFAULT_SPACING) 61 .Add(fContainerView) 62 .AddGroup(B_HORIZONTAL) 63 .AddGlue() 64 .Add(fBackButton) 65 .Add(fNextButton); 66 67 // determine the proper height 68 float newHeight = fMainView->Frame().bottom + buttonHeight + kSpacing * 2; 69 ResizeBy(0, newHeight - Bounds().Height()); 70 71 // Add a shortcut to close the window using Command-W 72 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 73 } 74 75 76 AutoConfigWindow::~AutoConfigWindow() 77 { 78 } 79 80 81 void 82 AutoConfigWindow::MessageReceived(BMessage* msg) 83 { 84 status_t status = B_ERROR; 85 BAlert* invalidMailAlert = NULL; 86 87 switch (msg->what) { 88 case kOkMsg: 89 if (fMainConfigState) { 90 fMainView->GetBasicAccountInfo(fAccountInfo); 91 if (!fMainView->IsValidMailAddress(fAccountInfo.email)) { 92 invalidMailAlert = new BAlert("invalidMailAlert", 93 B_TRANSLATE("Enter a valid e-mail address."), 94 B_TRANSLATE("OK")); 95 invalidMailAlert->SetFlags(invalidMailAlert->Flags() 96 | B_CLOSE_ON_ESCAPE); 97 invalidMailAlert->Go(); 98 return; 99 } 100 if (fAutoConfigServer) { 101 status = fAutoConfig.GetInfoFromMailAddress( 102 fAccountInfo.email.String(), 103 &fAccountInfo.providerInfo); 104 } 105 if (status == B_OK) { 106 fParentWindow->Lock(); 107 GenerateBasicAccount(); 108 fParentWindow->Unlock(); 109 Quit(); 110 } 111 fMainConfigState = false; 112 fServerConfigState = true; 113 fMainView->Hide(); 114 115 fServerView = new ServerSettingsView(fAccountInfo); 116 fContainerView->AddChild(fServerView); 117 118 fBackButton->SetEnabled(true); 119 fNextButton->SetLabel(B_TRANSLATE("Finish")); 120 } else { 121 fServerView->GetServerInfo(fAccountInfo); 122 fParentWindow->Lock(); 123 GenerateBasicAccount(); 124 fParentWindow->Unlock(); 125 Quit(); 126 } 127 break; 128 129 case kBackMsg: 130 if (fServerConfigState) { 131 fServerView->GetServerInfo(fAccountInfo); 132 133 fMainConfigState = true; 134 fServerConfigState = false; 135 136 fContainerView->RemoveChild(fServerView); 137 delete fServerView; 138 139 fMainView->Show(); 140 fBackButton->SetEnabled(false); 141 } 142 break; 143 144 case kServerChangedMsg: 145 fAutoConfigServer = false; 146 break; 147 148 default: 149 BWindow::MessageReceived(msg); 150 break; 151 } 152 } 153 154 155 bool 156 AutoConfigWindow::QuitRequested() 157 { 158 return true; 159 } 160 161 162 BMailAccountSettings* 163 AutoConfigWindow::GenerateBasicAccount() 164 { 165 if (!fAccount) { 166 fParentWindow->Lock(); 167 fAccount = fParentWindow->AddAccount(); 168 fParentWindow->Unlock(); 169 } 170 171 fAccount->SetName(fAccountInfo.accountName.String()); 172 fAccount->SetRealName(fAccountInfo.name.String()); 173 fAccount->SetReturnAddress(fAccountInfo.email.String()); 174 175 BMessage& inboundArchive = fAccount->InboundSettings(); 176 inboundArchive.MakeEmpty(); 177 BString inServerName; 178 int32 authType = 0; 179 int32 ssl = 0; 180 if (fAccountInfo.inboundType == IMAP) { 181 inServerName = fAccountInfo.providerInfo.imap_server; 182 ssl = fAccountInfo.providerInfo.ssl_imap; 183 fAccount->SetInboundAddOn("IMAP"); 184 } else { 185 inServerName = fAccountInfo.providerInfo.pop_server; 186 authType = fAccountInfo.providerInfo.authentification_pop; 187 ssl = fAccountInfo.providerInfo.ssl_pop; 188 fAccount->SetInboundAddOn("POP3"); 189 } 190 inboundArchive.AddString("server", inServerName); 191 inboundArchive.AddInt32("auth_method", authType); 192 inboundArchive.AddInt32("flavor", ssl); 193 inboundArchive.AddString("username", fAccountInfo.loginName); 194 set_passwd(&inboundArchive, "cpasswd", fAccountInfo.password); 195 inboundArchive.AddBool("leave_mail_on_server", true); 196 inboundArchive.AddBool("delete_remote_when_local", true); 197 198 BMessage& outboundArchive = fAccount->OutboundSettings(); 199 outboundArchive.MakeEmpty(); 200 fAccount->SetOutboundAddOn("SMTP"); 201 outboundArchive.AddString("server", 202 fAccountInfo.providerInfo.smtp_server); 203 outboundArchive.AddString("username", fAccountInfo.loginName); 204 set_passwd(&outboundArchive, "cpasswd", fAccountInfo.password); 205 outboundArchive.AddInt32("auth_method", 206 fAccountInfo.providerInfo.authentification_smtp); 207 outboundArchive.AddInt32("flavor", 208 fAccountInfo.providerInfo.ssl_smtp); 209 210 fParentWindow->Lock(); 211 fParentWindow->AccountUpdated(fAccount); 212 fParentWindow->Unlock(); 213 214 return fAccount; 215 } 216