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 */ 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 <MailSettings.h> 20 #include <Message.h> 21 #include <Path.h> 22 23 #include <crypt.h> 24 25 26 #undef B_TRANSLATION_CONTEXT 27 #define B_TRANSLATION_CONTEXT "AutoConfigWindow" 28 29 30 AutoConfigWindow::AutoConfigWindow(BRect rect, ConfigWindow *parent) 31 : 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) 40 { 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); 45 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); 57 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); 64 65 fBoxRect = Bounds(); 66 fBoxRect.InsetBy(5,5); 67 fBoxRect.bottom-= buttonHeight + 5; 68 69 fMainView = new AutoConfigView(fBoxRect, fAutoConfig); 70 fMainView->SetLabel(B_TRANSLATE("Account settings")); 71 fRootView->AddChild(fMainView); 72 73 // Add a shortcut to close the window using Command-W 74 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 75 } 76 77 78 AutoConfigWindow::~AutoConfigWindow() 79 { 80 81 } 82 83 84 void 85 AutoConfigWindow::MessageReceived(BMessage* msg) 86 { 87 status_t status = B_ERROR; 88 BAlert* invalidMailAlert = NULL; 89 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; 101 } 102 if (fAutoConfigServer) { 103 status = fAutoConfig.GetInfoFromMailAddress( 104 fAccountInfo.email.String(), 105 &fAccountInfo.providerInfo); 106 } 107 if (status == B_OK) { 108 fParentWindow->Lock(); 109 GenerateBasicAccount(); 110 fParentWindow->Unlock(); 111 Quit(); 112 } 113 fMainConfigState = false; 114 fServerConfigState = true; 115 fMainView->Hide(); 116 117 fServerView = new ServerSettingsView(fBoxRect, fAccountInfo); 118 fRootView->AddChild(fServerView); 119 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(); 128 } 129 break; 130 131 case kBackMsg: 132 if (fServerConfigState) { 133 fServerView->GetServerInfo(fAccountInfo); 134 135 fMainConfigState = true; 136 fServerConfigState = false; 137 138 fRootView->RemoveChild(fServerView); 139 delete fServerView; 140 141 fMainView->Show(); 142 fBackButton->SetEnabled(false); 143 } 144 break; 145 146 case kServerChangedMsg: 147 fAutoConfigServer = false; 148 break; 149 150 default: 151 BWindow::MessageReceived(msg); 152 break; 153 } 154 } 155 156 157 bool 158 AutoConfigWindow::QuitRequested() 159 { 160 return true; 161 } 162 163 164 BMailAccountSettings* 165 AutoConfigWindow::GenerateBasicAccount() 166 { 167 if (!fAccount) { 168 fParentWindow->Lock(); 169 fAccount = fParentWindow->AddAccount(); 170 fParentWindow->Unlock(); 171 } 172 173 fAccount->SetName(fAccountInfo.accountName.String()); 174 fAccount->SetRealName(fAccountInfo.name.String()); 175 fAccount->SetReturnAddress(fAccountInfo.email.String()); 176 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"); 191 } 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); 199 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); 211 212 fParentWindow->Lock(); 213 fParentWindow->AccountUpdated(fAccount); 214 fParentWindow->Unlock(); 215 216 return fAccount; 217 } 218