| 1 |
/* |
| 2 |
Copyright (C) 2009-2010 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <wx/wx.h> |
| 20 |
#include <wx/html/htmlwin.h> |
| 21 |
#include <wx/valgen.h> |
| 22 |
#include <wx/valtext.h> |
| 23 |
#include <wx/datetime.h> |
| 24 |
#include <wx/sstream.h> |
| 25 |
#include <wx/tokenzr.h> |
| 26 |
|
| 27 |
#include "generated/configure_launcher.h" |
| 28 |
#include "tabs/WelcomePage.h" |
| 29 |
#include "controls/StatusBar.h" |
| 30 |
#include "global/ProfileKeys.h" |
| 31 |
#include "apis/HelpManager.h" |
| 32 |
#include "apis/SkinManager.h" |
| 33 |
|
| 34 |
#include "global/MemoryDebugging.h" // Last include for memory debugging |
| 35 |
|
| 36 |
#define TIME_BETWEEN_NEWS_UPDATES wxTimeSpan::Day() |
| 37 |
|
| 38 |
class CloneProfileDialog: public wxDialog { |
| 39 |
public: |
| 40 |
CloneProfileDialog(wxWindow* parent); |
| 41 |
const wxString GetSourceProfileName(); |
| 42 |
const wxString& GetNewProfileName(); |
| 43 |
bool UseProfileCloning() const { return useProfileCloning; } |
| 44 |
void OnUpdateText(wxCommandEvent& event); |
| 45 |
void OnPressEnterKey(wxCommandEvent& event); |
| 46 |
void OnClickCloneCheckbox(wxCommandEvent& event); |
| 47 |
private: |
| 48 |
bool useProfileCloning; |
| 49 |
wxString newProfileName; |
| 50 |
wxButton *createButton; |
| 51 |
wxChoice *cloneFrom; |
| 52 |
|
| 53 |
bool NewNameIsValid(); |
| 54 |
|
| 55 |
DECLARE_EVENT_TABLE(); |
| 56 |
}; |
| 57 |
|
| 58 |
class DeleteProfileDialog: public wxDialog { |
| 59 |
public: |
| 60 |
DeleteProfileDialog(wxWindow* parent, wxString name); |
| 61 |
}; |
| 62 |
|
| 63 |
/** Class that manages the header image for the welcome tab. */ |
| 64 |
class HeaderBitmap: public wxPanel { |
| 65 |
public: |
| 66 |
HeaderBitmap(wxWindow* parent): wxPanel(parent, wxID_ANY) { |
| 67 |
SkinSystem::GetSkinSystem()->RegisterTCSkinChanged(this); |
| 68 |
UpdateBanner(); |
| 69 |
} |
| 70 |
virtual void OnPaint(wxPaintEvent& WXUNUSED(event)) { |
| 71 |
wxPaintDC dc(this); |
| 72 |
dc.DrawBitmap(this->bitmap, (this->GetSize().GetWidth()/2) - (this->bitmap.GetWidth()/2), 0); |
| 73 |
} |
| 74 |
private: |
| 75 |
void UpdateBanner() { |
| 76 |
this->bitmap = SkinSystem::GetSkinSystem()->GetBanner(); |
| 77 |
wxASSERT_MSG(this->bitmap.IsOk(), _("Loaded bitmap is invalid.")); |
| 78 |
|
| 79 |
this->SetMinSize(wxSize(bitmap.GetWidth(), bitmap.GetHeight())); |
| 80 |
} |
| 81 |
void OnTCSkinChanged(wxCommandEvent &WXUNUSED(event)) { |
| 82 |
UpdateBanner(); |
| 83 |
} |
| 84 |
wxBitmap bitmap; |
| 85 |
DECLARE_EVENT_TABLE(); |
| 86 |
}; |
| 87 |
|
| 88 |
BEGIN_EVENT_TABLE(HeaderBitmap, wxPanel) |
| 89 |
EVT_PAINT(HeaderBitmap::OnPaint) |
| 90 |
EVT_COMMAND(wxID_NONE, EVT_TC_SKIN_CHANGED, HeaderBitmap::OnTCSkinChanged) |
| 91 |
END_EVENT_TABLE() |
| 92 |
|
| 93 |
BEGIN_EVENT_TABLE(WelcomePage, wxPanel) |
| 94 |
EVT_HTML_LINK_CLICKED(ID_SUMMARY_HTML_PANEL, WelcomePage::LinkClicked) |
| 95 |
EVT_HTML_CELL_HOVER(ID_SUMMARY_HTML_PANEL, WelcomePage::LinkHover) |
| 96 |
EVT_HTML_LINK_CLICKED(ID_NEWS_HTML_PANEL, WelcomePage::LinkClicked) |
| 97 |
EVT_HTML_CELL_HOVER(ID_NEWS_HTML_PANEL, WelcomePage::LinkHover) |
| 98 |
|
| 99 |
EVT_COMMAND( wxID_NONE, EVT_PROFILE_CHANGE, WelcomePage::ProfileCountChanged) |
| 100 |
EVT_COMMAND( wxID_NONE, EVT_CURRENT_PROFILE_CHANGED, WelcomePage::ProfileCountChanged) |
| 101 |
EVT_COMMAND( wxID_NONE, EVT_TC_SKIN_CHANGED, WelcomePage::OnTCSkinChanged) |
| 102 |
|
| 103 |
// Profile controls |
| 104 |
EVT_BUTTON(ID_NEW_PROFILE, WelcomePage::ProfileButtonClicked) |
| 105 |
EVT_BUTTON(ID_DELETE_PROFILE, WelcomePage::ProfileButtonClicked) |
| 106 |
EVT_BUTTON(ID_SAVE_PROFILE, WelcomePage::ProfileButtonClicked) |
| 107 |
EVT_CHECKBOX(ID_SAVE_DEFAULT_CHECK, WelcomePage::AutoSaveProfilesChecked) |
| 108 |
EVT_CHOICE(ID_PROFILE_COMBO, WelcomePage::ProfileChanged) |
| 109 |
|
| 110 |
EVT_CHECKBOX(ID_NET_DOWNLOAD_NEWS, WelcomePage::OnDownloadNewsCheck) |
| 111 |
|
| 112 |
EVT_IDLE(WelcomePage::UpdateNews) |
| 113 |
END_EVENT_TABLE() |
| 114 |
|
| 115 |
BEGIN_EVENT_TABLE(CloneProfileDialog, wxDialog) |
| 116 |
EVT_TEXT(ID_CLONE_PROFILE_NEWNAME, CloneProfileDialog::OnUpdateText) |
| 117 |
EVT_TEXT_ENTER(ID_CLONE_PROFILE_NEWNAME, CloneProfileDialog::OnPressEnterKey) |
| 118 |
EVT_CHECKBOX(ID_CLONE_PROFILE_CHECKBOX, CloneProfileDialog::OnClickCloneCheckbox) |
| 119 |
END_EVENT_TABLE() |
| 120 |
|
| 121 |
WelcomePage::WelcomePage(wxWindow* parent): |
| 122 |
wxPanel(parent, wxID_ANY), lastLinkInfo(NULL), needToUpdateNews(false) |
| 123 |
{ |
| 124 |
ProMan* proman = ProMan::GetProfileManager(); |
| 125 |
SkinSystem::RegisterTCSkinChanged(this); |
| 126 |
|
| 127 |
#if 0 |
| 128 |
// language |
| 129 |
wxStaticText* launcherLanguageText = new wxStaticText(this, wxID_ANY, _("Launcher language:")); |
| 130 |
wxChoice* launcherLanguageCombo = new wxChoice(this, wxID_ANY); |
| 131 |
launcherLanguageCombo->Insert(_("English (US)"), 0); |
| 132 |
launcherLanguageCombo->SetSelection(0); |
| 133 |
launcherLanguageCombo->Disable(); |
| 134 |
|
| 135 |
wxBoxSizer* languageSizer = new wxBoxSizer(wxHORIZONTAL); |
| 136 |
languageSizer->AddStretchSpacer(2); |
| 137 |
languageSizer->Add(launcherLanguageText); |
| 138 |
languageSizer->Add(launcherLanguageCombo); |
| 139 |
#endif |
| 140 |
// header image |
| 141 |
HeaderBitmap* header = new HeaderBitmap(this); |
| 142 |
|
| 143 |
// Info |
| 144 |
wxStaticBox* generalBox = new wxStaticBox(this, wxID_ANY, _("")); |
| 145 |
wxHtmlWindow* general = new wxHtmlWindow(this, ID_SUMMARY_HTML_PANEL, wxDefaultPosition, wxDefaultSize, wxHW_SCROLLBAR_NEVER ); |
| 146 |
general->SetPage(SkinSystem::GetSkinSystem()->GetWelcomeText()); |
| 147 |
general->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(WelcomePage::OnMouseOut)); |
| 148 |
|
| 149 |
wxStaticBoxSizer* generalSizer = new wxStaticBoxSizer(generalBox, wxVERTICAL); |
| 150 |
generalSizer->SetMinSize(wxSize(-1, 175)); |
| 151 |
generalSizer->Add(general, wxSizerFlags().Expand().Proportion(1)); |
| 152 |
|
| 153 |
// Profiles |
| 154 |
wxStaticBox* profileBox = new wxStaticBox(this, wxID_ANY, _("Profile management")); |
| 155 |
wxChoice* profileCombo = new wxChoice(this, ID_PROFILE_COMBO, |
| 156 |
wxDefaultPosition, |
| 157 |
wxDefaultSize, |
| 158 |
0, // number of choices |
| 159 |
0, // choices |
| 160 |
wxCB_SORT); |
| 161 |
profileCombo->Append(proman->GetAllProfileNames()); |
| 162 |
proman->AddEventHandler(this); |
| 163 |
|
| 164 |
wxString lastselected; |
| 165 |
proman->GlobalRead(GBL_CFG_MAIN_LASTPROFILE, &lastselected, ProMan::DEFAULT_PROFILE_NAME); |
| 166 |
profileCombo->SetStringSelection(lastselected); |
| 167 |
|
| 168 |
wxButton* newButton = new wxButton(this, ID_NEW_PROFILE, _("New...")); |
| 169 |
wxButton* deleteButton = new wxButton(this, ID_DELETE_PROFILE, _("Delete...")); |
| 170 |
wxButton* saveButton = new wxButton(this, ID_SAVE_PROFILE, _("Save")); |
| 171 |
|
| 172 |
wxCheckBox* autoSaveProfilesCheck = new wxCheckBox(this, ID_SAVE_DEFAULT_CHECK, _("Save profiles automatically")); |
| 173 |
bool autosave; |
| 174 |
proman->GlobalRead(GBL_CFG_MAIN_AUTOSAVEPROFILES, &autosave, true, true); |
| 175 |
autoSaveProfilesCheck->SetValue(autosave); |
| 176 |
|
| 177 |
wxCommandEvent autoSaveEvent(wxEVT_COMMAND_CHECKBOX_CLICKED, ID_SAVE_DEFAULT_CHECK); |
| 178 |
autoSaveEvent.SetInt(autosave); |
| 179 |
this->AutoSaveProfilesChecked(autoSaveEvent); |
| 180 |
|
| 181 |
wxBoxSizer* profileButtonsSizer = new wxBoxSizer(wxHORIZONTAL); |
| 182 |
profileButtonsSizer->Add(newButton); |
| 183 |
profileButtonsSizer->Add(deleteButton); |
| 184 |
profileButtonsSizer->Add(saveButton); |
| 185 |
profileButtonsSizer->AddStretchSpacer(1); |
| 186 |
profileButtonsSizer->Add(autoSaveProfilesCheck, 0, wxALIGN_CENTER_VERTICAL); |
| 187 |
|
| 188 |
wxStaticBoxSizer* profileVerticalSizer = new wxStaticBoxSizer(profileBox, wxVERTICAL); |
| 189 |
profileVerticalSizer->Add(profileCombo, 0, wxALL | wxEXPAND, 5); |
| 190 |
profileVerticalSizer->Add(profileButtonsSizer, 0, wxALL | wxEXPAND, 5); |
| 191 |
|
| 192 |
// Latest news |
| 193 |
wxStaticBox* newsBox = new wxStaticBox(this, ID_NEWS_BOX, SkinSystem::GetSkinSystem()->GetNewsSource().GetLabel()); |
| 194 |
wxHtmlWindow* newsView = new wxHtmlWindow(this, ID_NEWS_HTML_PANEL); |
| 195 |
newsView->SetPage(_T("")); |
| 196 |
newsView->Connect(wxEVT_LEAVE_WINDOW, wxMouseEventHandler(WelcomePage::OnMouseOut)); |
| 197 |
updateNewsCheck = new wxCheckBox(this, ID_NET_DOWNLOAD_NEWS, _("Retrieve news at startup")); |
| 198 |
updateNewsCheck->SetToolTip(_("Check this to have the launcher retrieve the news the next time it runs")); |
| 199 |
updateNewsCheck->Disable(); |
| 200 |
this->Connect(wxEVT_IDLE, wxIdleEventHandler(WelcomePage::getOrPromptUpdateNews)); |
| 201 |
|
| 202 |
wxStaticBoxSizer* newsSizer = new wxStaticBoxSizer(newsBox, wxVERTICAL); |
| 203 |
newsSizer->Add(newsView, |
| 204 |
wxSizerFlags().Expand().Proportion(1).Border(wxLEFT|wxRIGHT, 5)); |
| 205 |
newsSizer->Add(updateNewsCheck, |
| 206 |
#if IS_WIN32 |
| 207 |
wxSizerFlags().Right().Border(wxTOP|wxRIGHT|wxBOTTOM,5)); |
| 208 |
#else // helps ensure that all four news items appear without having to scroll |
| 209 |
wxSizerFlags().Right().Border(wxTOP|wxRIGHT,5)); |
| 210 |
#endif |
| 211 |
|
| 212 |
// Final layout |
| 213 |
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); |
| 214 |
#if 0 |
| 215 |
sizer->Add(languageSizer); |
| 216 |
#endif |
| 217 |
sizer->Add(header, wxSizerFlags().Proportion(0).Expand().Border(wxTOP, 5)); |
| 218 |
sizer->Add(generalSizer, wxSizerFlags().Proportion(0).Expand().Border(wxLEFT|wxRIGHT|wxBOTTOM, 5)); |
| 219 |
sizer->Add(profileVerticalSizer, wxSizerFlags().Proportion(0).Expand().Border(wxLEFT|wxRIGHT|wxBOTTOM, 5)); |
| 220 |
sizer->Add(newsSizer, wxSizerFlags().Expand().Proportion(1).Border(wxLEFT|wxRIGHT|wxBOTTOM, 5)); |
| 221 |
|
| 222 |
// mildly hackish way of ensuring a minimum launcher window size on all platforms |
| 223 |
#if IS_WIN32 |
| 224 |
sizer->SetMinSize(wxSize(TAB_AREA_WIDTH-10, TAB_AREA_HEIGHT-5)); |
| 225 |
#else // to accommodate larger fonts/widgets on OS X/Linux |
| 226 |
sizer->SetMinSize(wxSize(TAB_AREA_WIDTH-10, TAB_AREA_HEIGHT+10)); |
| 227 |
#endif |
| 228 |
|
| 229 |
this->SetSizerAndFit(sizer); |
| 230 |
this->Layout(); |
| 231 |
} |
| 232 |
|
| 233 |
void WelcomePage::LinkClicked(wxHtmlLinkEvent &event) { |
| 234 |
wxHtmlLinkInfo info = event.GetLinkInfo(); |
| 235 |
wxString rest; |
| 236 |
if (info.GetHref().StartsWith(_T("help://"), &rest)) { |
| 237 |
HelpManager::OpenHelpByString(rest); |
| 238 |
} else { |
| 239 |
wxLaunchDefaultBrowser(info.GetHref()); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
void WelcomePage::LinkHover(wxHtmlCellEvent &event) { |
| 244 |
wxHtmlLinkInfo* info = event.GetCell()->GetLink(); // will be NULL if not a link. |
| 245 |
|
| 246 |
wxFrame *frame = dynamic_cast<wxFrame*>(this->GetParent()->GetParent()); |
| 247 |
wxASSERT( frame != NULL ); |
| 248 |
StatusBar *bar = dynamic_cast<StatusBar*>(frame->GetStatusBar()); |
| 249 |
wxASSERT( bar != NULL ); |
| 250 |
if (info == NULL) { |
| 251 |
if ( this->lastLinkInfo == NULL ) { |
| 252 |
// do nothing |
| 253 |
} else { |
| 254 |
bar->EndToolTipStatusText(); |
| 255 |
this->lastLinkInfo = NULL; |
| 256 |
} |
| 257 |
} else { |
| 258 |
if ( info == this->lastLinkInfo ) { |
| 259 |
// do nothing |
| 260 |
} else if ( this->lastLinkInfo == NULL ) { |
| 261 |
bar->StartToolTipStatusText(info->GetHref()); |
| 262 |
this->lastLinkInfo = reinterpret_cast<void *>(info); |
| 263 |
} else { |
| 264 |
// still on a URL but we potentially changed the URL |
| 265 |
bar->EndToolTipStatusText(); |
| 266 |
bar->StartToolTipStatusText(info->GetHref()); |
| 267 |
this->lastLinkInfo = reinterpret_cast<void *>(info); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void WelcomePage::OnMouseOut(wxMouseEvent &WXUNUSED(event)) { |
| 273 |
WXUNUSED(event); |
| 274 |
// clear url from status bar. |
| 275 |
wxFrame *frame = dynamic_cast<wxFrame*>(this->GetParent()->GetParent()->GetParent()); |
| 276 |
wxASSERT( frame != NULL ); |
| 277 |
StatusBar *bar = dynamic_cast<StatusBar*>(frame->GetStatusBar()); |
| 278 |
wxASSERT( bar != NULL ); |
| 279 |
bar->EndToolTipStatusText(); |
| 280 |
} |
| 281 |
|
| 282 |
/** Calls the dialogs for cloning, saving or deleting profiles. */ |
| 283 |
void WelcomePage::ProfileButtonClicked(wxCommandEvent& event) { |
| 284 |
wxChoice* profileCombo = dynamic_cast<wxChoice*>(wxWindow::FindWindowById(ID_PROFILE_COMBO, this)); |
| 285 |
ProMan *proman = ProMan::GetProfileManager(); |
| 286 |
|
| 287 |
switch(event.GetId()) { |
| 288 |
case ID_NEW_PROFILE: |
| 289 |
cloneNewProfile(profileCombo, proman); |
| 290 |
break; |
| 291 |
|
| 292 |
case ID_DELETE_PROFILE: |
| 293 |
deleteProfile(profileCombo, proman); |
| 294 |
break; |
| 295 |
|
| 296 |
case ID_SAVE_PROFILE: |
| 297 |
proman->SaveCurrentProfile(); |
| 298 |
break; |
| 299 |
default: |
| 300 |
wxCHECK_RET( false, _("Reached impossible location.\nHandler has been attached to a button that it cannot handle.")); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
void WelcomePage::AutoSaveProfilesChecked(wxCommandEvent& event) { |
| 305 |
ProMan* proman = ProMan::GetProfileManager(); |
| 306 |
|
| 307 |
if ( event.IsChecked() ) { |
| 308 |
proman->SetAutoSave(true); |
| 309 |
proman->GlobalWrite(GBL_CFG_MAIN_AUTOSAVEPROFILES, true); |
| 310 |
proman->SaveCurrentProfile(); |
| 311 |
wxLogStatus(_("Now autosaving profiles.")); |
| 312 |
} else { |
| 313 |
proman->SetAutoSave(false); |
| 314 |
proman->GlobalWrite(GBL_CFG_MAIN_AUTOSAVEPROFILES, false); |
| 315 |
wxLogStatus(_("No longer autosaving profiles.")); |
| 316 |
} |
| 317 |
} |
| 318 |
void WelcomePage::ProfileChanged(wxCommandEvent& WXUNUSED(event)) { |
| 319 |
wxChoice* profileCombo = dynamic_cast<wxChoice*>(wxWindow::FindWindowById(ID_PROFILE_COMBO, this)); |
| 320 |
wxString newProfile = profileCombo->GetStringSelection(); |
| 321 |
ProMan* proman = ProMan::GetProfileManager(); |
| 322 |
|
| 323 |
if (newProfile == proman->GetCurrentName()) { |
| 324 |
// user selected the current profile, so no need to switch, just return |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
if ( proman->DoesProfileExist(newProfile) ) { |
| 329 |
if ( proman->NeedToPromptToSave() ) { |
| 330 |
int response = wxMessageBox( |
| 331 |
ProMan::GetSaveDialogMessageText(ProMan::ON_PROFILE_SWITCH, proman->GetCurrentName()), |
| 332 |
ProMan::GetSaveDialogCaptionText(ProMan::ON_PROFILE_SWITCH, proman->GetCurrentName()), |
| 333 |
wxYES_NO|wxCANCEL, this); |
| 334 |
if ( response == wxYES ) { |
| 335 |
wxLogDebug(_T("saving profile %s before switching to profile %s"), |
| 336 |
proman->GetCurrentName().c_str(), newProfile.c_str()); |
| 337 |
proman->SaveCurrentProfile(); |
| 338 |
} else if ( response == wxCANCEL ) { |
| 339 |
wxLogInfo(_T("Cancelled switch to profile %s."), newProfile.c_str()); |
| 340 |
profileCombo->SetSelection(profileCombo->FindString(proman->GetCurrentName())); |
| 341 |
return; |
| 342 |
} else { |
| 343 |
wxLogWarning(_T("switching from profile %s to profile %s without saving changes"), |
| 344 |
proman->GetCurrentName().c_str(), newProfile.c_str()); |
| 345 |
} |
| 346 |
} |
| 347 |
if ( proman->SwitchTo(newProfile) ) { |
| 348 |
wxLogMessage(_T("Profile %s is now the active profile."), proman->GetCurrentName().c_str()); |
| 349 |
} else { |
| 350 |
wxLogError(_T("Unable to switch to %s, staying on %s."), newProfile.c_str(), proman->GetCurrentName().c_str()); |
| 351 |
} |
| 352 |
} else { |
| 353 |
wxLogError(_T("Profile does not exist. Use Clone to create profile first")); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
void WelcomePage::cloneNewProfile(wxChoice* profileCombo, ProMan* proman) { |
| 358 |
if ( proman->NeedToPromptToSave() ) { |
| 359 |
int response = wxMessageBox( |
| 360 |
ProMan::GetSaveDialogMessageText(ProMan::ON_PROFILE_CREATE, proman->GetCurrentName()), |
| 361 |
ProMan::GetSaveDialogCaptionText(ProMan::ON_PROFILE_CREATE, proman->GetCurrentName()), |
| 362 |
wxYES_NO|wxCANCEL, this); |
| 363 |
if ( response == wxYES ) { |
| 364 |
wxLogDebug(_T("Saving current profile %s before opening create profile dialog."), |
| 365 |
proman->GetCurrentName().c_str()); |
| 366 |
proman->SaveCurrentProfile(); |
| 367 |
} else if ( response == wxCANCEL ) { |
| 368 |
wxLogInfo(_T("Cancelled opening create profile dialog.")); |
| 369 |
return; |
| 370 |
} else { |
| 371 |
wxLogWarning(_T("Reverting unsaved changes to current profile %s before opening create profile dialog"), |
| 372 |
proman->GetCurrentName().c_str()); |
| 373 |
proman->RevertCurrentProfile(); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
CloneProfileDialog cloneDialog(this); |
| 378 |
|
| 379 |
if ( cloneDialog.ShowModal() == cloneDialog.GetAffirmativeId() ) { |
| 380 |
wxLogDebug(_T("User clicked %s"), cloneDialog.UseProfileCloning() ? _T("clone") : _T("create")); |
| 381 |
if ( proman->CreateProfile( |
| 382 |
cloneDialog.GetNewProfileName(), |
| 383 |
cloneDialog.GetSourceProfileName()) ) { |
| 384 |
if (cloneDialog.UseProfileCloning()) { |
| 385 |
wxLogStatus(_("Cloned profile '%s' from '%s'"), |
| 386 |
cloneDialog.GetNewProfileName().c_str(), |
| 387 |
cloneDialog.GetSourceProfileName().c_str()); |
| 388 |
} else { |
| 389 |
wxLogStatus(_("Created profile '%s'"), |
| 390 |
cloneDialog.GetNewProfileName().c_str()); |
| 391 |
} |
| 392 |
proman->SwitchTo(cloneDialog.GetNewProfileName()); |
| 393 |
} else { // profile creation was unsuccessful |
| 394 |
if (cloneDialog.UseProfileCloning()) { |
| 395 |
wxLogError(_("Unable to clone profile '%s' from '%s'. See log for details."), |
| 396 |
cloneDialog.GetNewProfileName().c_str(), |
| 397 |
cloneDialog.GetSourceProfileName().c_str()); |
| 398 |
} else { |
| 399 |
wxLogError(_("Unable to create profile '%s'. See log for details."), |
| 400 |
cloneDialog.GetNewProfileName().c_str()); |
| 401 |
} |
| 402 |
} |
| 403 |
} else { |
| 404 |
wxLogStatus(_("Profile creation aborted")); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
void WelcomePage::deleteProfile(wxChoice* profileCombo, ProMan* proman) { |
| 410 |
wxString nametodelete = profileCombo->GetStringSelection(); |
| 411 |
|
| 412 |
if ( proman->DoesProfileExist(nametodelete) ) { |
| 413 |
if (nametodelete == ProMan::DEFAULT_PROFILE_NAME) { |
| 414 |
wxLogWarning(_T("The default profile cannot be deleted.")); |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
DeleteProfileDialog deleteDialog(this, nametodelete); |
| 419 |
|
| 420 |
if ( deleteDialog.ShowModal() == deleteDialog.GetAffirmativeId()) { |
| 421 |
if ( proman->DeleteProfile(nametodelete) ) { |
| 422 |
wxLogStatus(_("Deleted profile named '%s'."), nametodelete.c_str()); |
| 423 |
} else { |
| 424 |
wxLogWarning(_("Unable to delete profile '%s', see log for more details."), nametodelete.c_str()); |
| 425 |
} |
| 426 |
} else { |
| 427 |
wxLogStatus(_("Deletion of profile '%s' cancelled"), nametodelete.c_str()); |
| 428 |
} |
| 429 |
} else { |
| 430 |
wxLogWarning(_T("Unable to delete non-existent profile '%s'"), nametodelete.c_str()); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
void WelcomePage::ProfileCountChanged(wxCommandEvent &WXUNUSED(event)) { |
| 435 |
wxChoice* profileCombo = dynamic_cast<wxChoice*>(wxWindow::FindWindowById(ID_PROFILE_COMBO, this)); |
| 436 |
wxCHECK_RET(profileCombo != NULL, _T("can't find the profile combobox")); |
| 437 |
ProMan *proman = ProMan::GetProfileManager(); |
| 438 |
|
| 439 |
profileCombo->Clear(); |
| 440 |
profileCombo->Append(proman->GetAllProfileNames()); |
| 441 |
profileCombo->SetStringSelection(proman->GetCurrentName()); |
| 442 |
} |
| 443 |
|
| 444 |
void WelcomePage::UpdateNews(wxIdleEvent& WXUNUSED(event)) { |
| 445 |
if ( !this->needToUpdateNews ) { |
| 446 |
return; |
| 447 |
} |
| 448 |
this->needToUpdateNews = false; |
| 449 |
wxHtmlWindow* newsWindow = dynamic_cast<wxHtmlWindow*>(wxWindow::FindWindowById(ID_NEWS_HTML_PANEL, this)); |
| 450 |
wxCHECK_RET(newsWindow != NULL, _T("Update news called, but can't find the news window")); |
| 451 |
|
| 452 |
ProMan* proman = ProMan::GetProfileManager(); |
| 453 |
|
| 454 |
bool allowedToUpdateNews; |
| 455 |
if ( !proman->GlobalRead(GBL_CFG_NET_DOWNLOAD_NEWS, &allowedToUpdateNews)) { |
| 456 |
return; |
| 457 |
} |
| 458 |
if (allowedToUpdateNews) { |
| 459 |
const NewsSource& newsSource(SkinSystem::GetSkinSystem()->GetNewsSource()); |
| 460 |
const NewsData* newsData = |
| 461 |
proman->NewsRead(newsSource.GetName()); |
| 462 |
|
| 463 |
wxASSERT((newsData == NULL) || newsData->IsValid()); |
| 464 |
|
| 465 |
if ((newsData != NULL) && |
| 466 |
(wxDateTime::Now() - newsData->lastDownloadNews < TIME_BETWEEN_NEWS_UPDATES) ) { |
| 467 |
// post the news that we have on file for now |
| 468 |
newsWindow->SetPage(newsData->theNews); |
| 469 |
} else { |
| 470 |
wxFileSystem filesystem; |
| 471 |
wxFSFile* news = filesystem.OpenFile(newsSource.GetNewsUrl(), wxFS_READ); |
| 472 |
if ( news == NULL ) { |
| 473 |
wxLogError(_("Error in retrieving news")); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
wxInputStream* rawNewsStream = news->GetStream(); |
| 478 |
wxLogDebug(_T("news loaded from %s with type %s"), news->GetLocation().c_str(), news->GetMimeType().c_str()); |
| 479 |
|
| 480 |
wxString newsStr; |
| 481 |
wxStringOutputStream newsStrStream(&newsStr); |
| 482 |
rawNewsStream->Read(newsStrStream); |
| 483 |
wxString formattedData(_T("<ul>")); |
| 484 |
wxStringTokenizer tok(newsStr, _T("\t"), wxTOKEN_STRTOK); |
| 485 |
|
| 486 |
while(tok.HasMoreTokens()) { |
| 487 |
wxString title(tok.GetNextToken()); |
| 488 |
wxCHECK_RET(tok.HasMoreTokens(), _T("news formatter has run out of tokens at wrong time")); |
| 489 |
wxString link(tok.GetNextToken()); |
| 490 |
wxCHECK_RET(tok.HasMoreTokens(), _T("news formatter has run out of tokens at wrong time")); |
| 491 |
wxString imglink(tok.GetNextToken()); |
| 492 |
|
| 493 |
formattedData += wxString::Format(_T("\n<li><a href='%s'>%s</a><!-- %s --></li>"), link.c_str(), title.c_str(), imglink.c_str()); |
| 494 |
} |
| 495 |
formattedData += _T("\n</ul>"); |
| 496 |
|
| 497 |
newsWindow->SetPage(formattedData); |
| 498 |
|
| 499 |
proman->NewsWrite( |
| 500 |
newsSource.GetName(), |
| 501 |
NewsData(formattedData, wxDateTime::Now())); |
| 502 |
} |
| 503 |
} else { |
| 504 |
newsWindow->SetPage(_("Automatic news retrieval disabled.")); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
void WelcomePage::getOrPromptUpdateNews(wxIdleEvent &WXUNUSED(event)) { |
| 509 |
this->Disconnect(wxEVT_IDLE, wxIdleEventHandler(WelcomePage::getOrPromptUpdateNews)); |
| 510 |
wxCHECK_RET(updateNewsCheck != NULL, wxT("checkbox is NULL")); |
| 511 |
|
| 512 |
bool updateNews; |
| 513 |
if (!ProMan::GetProfileManager()->GlobalRead(GBL_CFG_NET_DOWNLOAD_NEWS, &updateNews)) { |
| 514 |
wxDialog* updateNewsQuestion = |
| 515 |
new wxDialog(NULL, wxID_ANY, |
| 516 |
_("wxLauncher - network access request"), |
| 517 |
wxDefaultPosition, wxDefaultSize, |
| 518 |
wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP | wxDIALOG_NO_PARENT); |
| 519 |
updateNewsQuestion->SetBackgroundColour( |
| 520 |
wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
| 521 |
|
| 522 |
wxIcon helpIcon; |
| 523 |
helpIcon.CopyFromBitmap(SkinSystem::GetSkinSystem()->GetHelpIcon()); |
| 524 |
updateNewsQuestion->SetIcon(helpIcon); |
| 525 |
|
| 526 |
wxStaticText* updateNewsText1 = |
| 527 |
new wxStaticText(updateNewsQuestion, wxID_ANY, |
| 528 |
_("Should wxLauncher automatically retrieve the latest news?"), |
| 529 |
wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE); |
| 530 |
|
| 531 |
wxStaticText* updateNewsText2 = |
| 532 |
new wxStaticText(updateNewsQuestion, wxID_ANY, |
| 533 |
_("You can change this setting on the Welcome page."), |
| 534 |
wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE); |
| 535 |
|
| 536 |
wxButton* allowNewsUpdate = |
| 537 |
new wxButton(updateNewsQuestion, wxID_ANY, |
| 538 |
_("Yes")); |
| 539 |
wxButton* denyNewsUpdate = |
| 540 |
new wxButton(updateNewsQuestion, wxID_ANY, |
| 541 |
_("No")); |
| 542 |
updateNewsQuestion->SetAffirmativeId(allowNewsUpdate->GetId()); |
| 543 |
updateNewsQuestion->SetEscapeId(denyNewsUpdate->GetId()); |
| 544 |
#if 0 |
| 545 |
wxButton* helpButton = new wxButton(updateNewsQuestion, wxID_ANY, |
| 546 |
_T("?"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT); |
| 547 |
|
| 548 |
helpButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED, |
| 549 |
wxCommandEventHandler(WelcomePage::OnUpdateNewsHelp)); |
| 550 |
#endif |
| 551 |
|
| 552 |
wxBoxSizer* updateNewsSizer = new wxBoxSizer(wxHORIZONTAL); |
| 553 |
wxBoxSizer* bodySizer= new wxBoxSizer(wxVERTICAL); |
| 554 |
wxBoxSizer* choiceSizer = new wxBoxSizer(wxHORIZONTAL); |
| 555 |
|
| 556 |
wxStaticBitmap* questionImage = |
| 557 |
new wxStaticBitmap(updateNewsQuestion, wxID_ANY, SkinSystem::GetSkinSystem()->GetBigHelpIcon()); |
| 558 |
|
| 559 |
choiceSizer->Add(allowNewsUpdate, wxSizerFlags().Border(wxRIGHT, 5)); |
| 560 |
choiceSizer->Add(denyNewsUpdate); |
| 561 |
#if 0 |
| 562 |
choiceSizer->Add(helpButton, wxSizerFlags().Border(wxLEFT, 15)); |
| 563 |
#endif |
| 564 |
|
| 565 |
bodySizer->Add(updateNewsText1, wxSizerFlags().Center()); |
| 566 |
bodySizer->Add(updateNewsText2, wxSizerFlags().Center().Border(wxBOTTOM, 10)); |
| 567 |
bodySizer->Add(choiceSizer, wxSizerFlags().Center()); |
| 568 |
|
| 569 |
updateNewsSizer->Add(questionImage, wxSizerFlags().Center().Border(wxTOP|wxLEFT|wxBOTTOM, 5)); |
| 570 |
updateNewsSizer->Add(bodySizer, wxSizerFlags().Center().Border(wxALL, 5)); |
| 571 |
|
| 572 |
updateNewsQuestion->SetSizerAndFit(updateNewsSizer); |
| 573 |
updateNewsQuestion->Centre(wxBOTH | wxCENTRE_ON_SCREEN); |
| 574 |
|
| 575 |
if ( updateNewsQuestion->GetAffirmativeId() == updateNewsQuestion->ShowModal() ) { |
| 576 |
updateNews = true; |
| 577 |
} else { |
| 578 |
updateNews = false; |
| 579 |
} |
| 580 |
ProMan::GetProfileManager()->GlobalWrite(GBL_CFG_NET_DOWNLOAD_NEWS, updateNews); |
| 581 |
|
| 582 |
updateNewsQuestion->Destroy(); |
| 583 |
} |
| 584 |
updateNewsCheck->SetValue(updateNews); |
| 585 |
updateNewsCheck->Enable(); |
| 586 |
this->needToUpdateNews = true; |
| 587 |
} |
| 588 |
|
| 589 |
void WelcomePage::OnDownloadNewsCheck(wxCommandEvent& event) { |
| 590 |
wxCheckBox* checkbox = dynamic_cast<wxCheckBox*>(wxWindow::FindWindowById(event.GetId(), this)); |
| 591 |
wxCHECK_RET( checkbox != NULL, _T("OnDownloadNewsCheck called by non checkbox")); |
| 592 |
|
| 593 |
ProMan::GetProfileManager()->GlobalWrite(GBL_CFG_NET_DOWNLOAD_NEWS, checkbox->IsChecked()); |
| 594 |
} |
| 595 |
|
| 596 |
void WelcomePage::OnUpdateNewsHelp(wxCommandEvent &WXUNUSED(event)) { |
| 597 |
HelpManager::OpenHelpById(ID_MORE_INFO_PRIVACY); |
| 598 |
} |
| 599 |
|
| 600 |
void WelcomePage::OnTCSkinChanged(wxCommandEvent &WXUNUSED(event)) { |
| 601 |
wxHtmlWindow* general = dynamic_cast<wxHtmlWindow*>( |
| 602 |
wxWindow::FindWindowById(ID_SUMMARY_HTML_PANEL, this)); |
| 603 |
wxCHECK_RET(general != NULL, _T("Unable to find welcome text area")); |
| 604 |
general->SetPage(SkinSystem::GetSkinSystem()->GetWelcomeText()); |
| 605 |
|
| 606 |
wxStaticBox* newsBox = dynamic_cast<wxStaticBox*>( |
| 607 |
wxWindow::FindWindowById(ID_NEWS_BOX, this)); |
| 608 |
wxCHECK_RET(newsBox != NULL, _T("Unable to find news box")); |
| 609 |
newsBox->SetLabel(SkinSystem::GetSkinSystem()->GetNewsSource().GetLabel()); |
| 610 |
|
| 611 |
this->needToUpdateNews = true; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
/////////////////////////////////////////////////////////////////////////////// |
| 616 |
///// DIALOGS /// |
| 617 |
CloneProfileDialog::CloneProfileDialog(wxWindow* parent): |
| 618 |
wxDialog(parent, ID_CLONE_PROFILE_DIALOG, _("Create new profile"), wxDefaultPosition, wxDefaultSize) { |
| 619 |
wxStaticText *newNameText = new wxStaticText(this, wxID_ANY, _("New profile name:")); |
| 620 |
wxTextCtrl *newName = new wxTextCtrl(this, ID_CLONE_PROFILE_NEWNAME, wxEmptyString, |
| 621 |
wxDefaultPosition, wxSize(200,-1), wxTE_PROCESS_ENTER); |
| 622 |
|
| 623 |
wxSizer* nameSizer = new wxFlexGridSizer(3); |
| 624 |
nameSizer->AddStretchSpacer(1); |
| 625 |
nameSizer->Add(newNameText, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5); |
| 626 |
nameSizer->Add(newName, wxSizerFlags().Expand()); |
| 627 |
|
| 628 |
wxCheckBox* cloneFromCheckbox = new wxCheckBox(this, ID_CLONE_PROFILE_CHECKBOX, wxEmptyString); |
| 629 |
wxStaticText* cloneFromText = new wxStaticText(this, wxID_ANY, _("Clone settings from:")); |
| 630 |
cloneFrom = new wxChoice(this, wxID_ANY); |
| 631 |
|
| 632 |
nameSizer->Add(cloneFromCheckbox, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5); |
| 633 |
nameSizer->Add(cloneFromText, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5); |
| 634 |
nameSizer->Add(cloneFrom, wxSizerFlags().Expand()); |
| 635 |
|
| 636 |
this->createButton = new wxButton(this, wxID_OK, _("Create")); |
| 637 |
wxCommandEvent initDialogEvent; |
| 638 |
CloneProfileDialog::OnUpdateText(initDialogEvent); |
| 639 |
wxButton *closeButton = new wxButton(this, wxID_CANCEL, _("Cancel")); |
| 640 |
|
| 641 |
wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL); |
| 642 |
buttonSizer->Add(createButton, wxSizerFlags().Border(wxRIGHT, 5)); |
| 643 |
buttonSizer->Add(closeButton, wxSizerFlags()); |
| 644 |
|
| 645 |
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); |
| 646 |
sizer->Add(nameSizer, wxSizerFlags().Expand().Border(wxALL, 5)); |
| 647 |
sizer->Add(buttonSizer, wxSizerFlags().Right().Border(wxALL, 5)); |
| 648 |
|
| 649 |
newName->SetValidator(wxTextValidator(wxFILTER_NONE, &(this->newProfileName))); |
| 650 |
|
| 651 |
cloneFrom->Append(ProMan::GetProfileManager()->GetAllProfileNames()); |
| 652 |
cloneFrom->SetStringSelection(ProMan::GetProfileManager()->GetCurrentName()); |
| 653 |
|
| 654 |
wxCommandEvent initEvent(wxEVT_COMMAND_CHECKBOX_CLICKED, ID_CLONE_PROFILE_CHECKBOX); |
| 655 |
initEvent.SetInt(0); // will initialize checkbox to unchecked |
| 656 |
this->OnClickCloneCheckbox(initEvent); |
| 657 |
|
| 658 |
this->SetSizerAndFit(sizer); |
| 659 |
this->Layout(); |
| 660 |
this->Center(); |
| 661 |
wxLogDebug(_T("Clone Profile Dialog Created")); |
| 662 |
} |
| 663 |
|
| 664 |
const wxString& CloneProfileDialog::GetNewProfileName() { |
| 665 |
return this->newProfileName.Trim(true).Trim(false); // strip trailing/leading whitespace |
| 666 |
} |
| 667 |
|
| 668 |
const wxString CloneProfileDialog::GetSourceProfileName() { |
| 669 |
// can't return wxString& since cloneFrom returns a temporary var |
| 670 |
return this->UseProfileCloning() ? cloneFrom->GetStringSelection() : wxString(wxEmptyString); |
| 671 |
} |
| 672 |
|
| 673 |
void CloneProfileDialog::OnUpdateText(wxCommandEvent& event) { |
| 674 |
this->createButton->Enable(this->NewNameIsValid()); |
| 675 |
} |
| 676 |
|
| 677 |
void CloneProfileDialog::OnPressEnterKey(wxCommandEvent& event) { |
| 678 |
if (this->NewNameIsValid()) { |
| 679 |
wxCommandEvent simulatedClickEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); |
| 680 |
this->createButton->Command(simulatedClickEvent); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
void CloneProfileDialog::OnClickCloneCheckbox(wxCommandEvent& event) { |
| 685 |
if (event.IsChecked()) { |
| 686 |
this->useProfileCloning = true; |
| 687 |
this->createButton->SetLabel(_("Clone")); |
| 688 |
this->cloneFrom->Enable(); |
| 689 |
this->Refresh(); |
| 690 |
this->Update(); |
| 691 |
} else { |
| 692 |
this->useProfileCloning = false; |
| 693 |
this->createButton->SetLabel(_("Create")); |
| 694 |
this->cloneFrom->Disable(); |
| 695 |
this->Refresh(); |
| 696 |
this->Update(); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
// a valid name is non-empty and does not consist purely of whitespace |
| 701 |
bool CloneProfileDialog::NewNameIsValid() { |
| 702 |
wxTextCtrl* newName = dynamic_cast<wxTextCtrl*>(wxWindow::FindWindowById(ID_CLONE_PROFILE_NEWNAME, this)); |
| 703 |
wxCHECK_MSG(newName != NULL, false, _T("can't find the clone profile new name text ctrl")); |
| 704 |
return !(newName->GetValue().Trim().IsEmpty()); |
| 705 |
} |
| 706 |
|
| 707 |
DeleteProfileDialog::DeleteProfileDialog(wxWindow* parent, wxString name): |
| 708 |
wxDialog(parent, ID_DELETE_PROFILE_DIALOG, _("Delete profile?"), wxDefaultPosition, wxDefaultSize) { |
| 709 |
wxStaticText* text = new wxStaticText(this, wxID_ANY, |
| 710 |
wxString::Format(_("Are you sure you want to delete profile '%s'?"), name.c_str())); |
| 711 |
|
| 712 |
wxButton *deleteButton = new wxButton(this, wxID_ANY, _("Delete")); |
| 713 |
wxButton *cancelButton = new wxButton(this, wxID_ANY, _("Cancel")); |
| 714 |
|
| 715 |
wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL); |
| 716 |
buttonSizer->Add(deleteButton, wxSizerFlags().Border(wxRIGHT, 5)); |
| 717 |
buttonSizer->Add(cancelButton, wxSizerFlags()); |
| 718 |
|
| 719 |
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); |
| 720 |
sizer->Add(text, wxSizerFlags().Expand().Border(wxALL, 5)); |
| 721 |
sizer->Add(buttonSizer, wxSizerFlags().Right().Border(wxALL, 5)); |
| 722 |
|
| 723 |
this->SetSizerAndFit(sizer); |
| 724 |
this->Layout(); |
| 725 |
this->Center(); |
| 726 |
|
| 727 |
this->SetAffirmativeId(deleteButton->GetId()); |
| 728 |
this->SetEscapeId(cancelButton->GetId()); |
| 729 |
|
| 730 |
wxLogDebug(_T("Delete Profile Dialog created.")); |
| 731 |
} |