diff --git a/ressources/icons/France.png b/ressources/icons/France.png new file mode 100644 index 0000000..21d0419 Binary files /dev/null and b/ressources/icons/France.png differ diff --git a/ressources/icons/United Kingdom.png b/ressources/icons/United Kingdom.png new file mode 100644 index 0000000..82cf5f1 Binary files /dev/null and b/ressources/icons/United Kingdom.png differ diff --git a/src/controller/KissCount.cpp b/src/controller/KissCount.cpp index 2ca86f2..4655a39 100644 --- a/src/controller/KissCount.cpp +++ b/src/controller/KissCount.cpp @@ -245,3 +245,9 @@ void KissCount::KillMe() _user = NULL; _wxUI->ChangeUser(); } + +void KissCount::SetLanguage(wxLanguage language) +{ + _db->SetLanguage(_user, language); + _user->_preferences[wxT("language")] = wxString::Format(wxT("%d"), language) ; +} diff --git a/src/controller/KissCount.h b/src/controller/KissCount.h index 081260e..16315ae 100644 --- a/src/controller/KissCount.h +++ b/src/controller/KissCount.h @@ -45,6 +45,8 @@ class KissCount void GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearTo); void KillMe(); + + void SetLanguage(wxLanguage language); private: wxUI* _wxUI; Database* _db; diff --git a/src/model/Database.cpp b/src/model/Database.cpp index 6e9d691..8e5fe59 100644 --- a/src/model/Database.cpp +++ b/src/model/Database.cpp @@ -882,3 +882,31 @@ void Database::KillMe(User* user) req = wxT("DELETE FROM user WHERE id='") + user->_id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } + +void Database::SetLanguage(User* user, wxLanguage language) +{ + wxString req; + req = wxT("UPDATE preference SET ") ; + req += wxT("name='language'"); + req += wxT(", value='") + wxString::Format(wxT("%d"), language) + wxT("'"); + req += wxT(" WHERE user='") + user->_id + wxT("'"); + + try + { + if (!_db.ExecuteUpdate(req)) + { + req = wxT("INSERT INTO preference ('user', 'name', 'value') VALUES ('") ; + req += user->_id + wxT("'"); + req += wxT(" ,'language'"); + req += wxT(" ,'") + wxString::Format(wxT("%d"), language) + wxT("'"); + req += wxT(")"); + EXECUTE_SQL_UPDATE(req, ); + } + } + catch (wxSQLite3Exception e) + { + std::cerr << req.mb_str() << "\n" ; + std::cerr << e.GetMessage().mb_str() << "\n" ; + return ; + } +} diff --git a/src/model/Database.h b/src/model/Database.h index 6e5cbdf..245cd67 100644 --- a/src/model/Database.h +++ b/src/model/Database.h @@ -47,6 +47,8 @@ class Database void ChangeName(User* user, const wxString& name); void NewUser(const wxString& name); + void SetLanguage(User* user, wxLanguage language); + void KillMe(User* user); private: wxSQLite3Database _db; diff --git a/src/model/User.cpp b/src/model/User.cpp index 117f8bf..3f105cc 100644 --- a/src/model/User.cpp +++ b/src/model/User.cpp @@ -92,7 +92,7 @@ wxLanguage User::GetLanguage() long val; if (res == wxT("")) - return wxLANGUAGE_ENGLISH_UK ; + return wxLANGUAGE_ENGLISH ; res.ToLong(&val); diff --git a/src/view/PreferencesPanel.cpp b/src/view/PreferencesPanel.cpp index e50be75..283cef2 100644 --- a/src/view/PreferencesPanel.cpp +++ b/src/view/PreferencesPanel.cpp @@ -196,12 +196,11 @@ void PreferencesPanel::InitCategories(User* user) void PreferencesPanel::InitLanguage(User* user) { int i, select=0; - wxLanguage pref = user->GetLanguage(); for (i=0; iAppend(languages[i].name, wxBitmap(languages[i].icon)); - if (languages[i].language == pref) + if (languages[i].language == _wxUI->_language) select = i; } @@ -478,7 +477,15 @@ void PreferencesPanel::OnChangePassword(wxCommandEvent& event) void PreferencesPanel::OnLanguageChange(wxCommandEvent& event) { wxLanguage language = languages[_language->GetSelection()].language; - _wxUI->SetLanguage(language); + if (_wxUI->SetLanguage(language) || language == wxLANGUAGE_ENGLISH) + { + _wxUI->NeedReload(); + _kiss->SetLanguage(language); + wxMessageBox(_("Language successfully changed, please go to another panel"), _("KissCount"), wxICON_INFORMATION | wxOK); + } + else + wxMessageBox(_("Language not changed"), _("KissCount"), wxICON_ERROR | wxOK); + } void PreferencesPanel::OnShow(wxShowEvent& event) diff --git a/src/view/SupportedLanguages.cpp b/src/view/SupportedLanguages.cpp new file mode 100644 index 0000000..7945935 --- /dev/null +++ b/src/view/SupportedLanguages.cpp @@ -0,0 +1,7 @@ +#include "SupportedLanguages.h" + +language languages[NB_SUPPORTED_LANGUAGES] = { + { wxT("English"), wxT(ICONS_PATH "/United Kingdom.png"), wxLANGUAGE_ENGLISH}, + { wxT("Français"), wxT(ICONS_PATH "/France.png"), wxLANGUAGE_FRENCH} +}; + diff --git a/src/view/SupportedLanguages.h b/src/view/SupportedLanguages.h new file mode 100644 index 0000000..752e08f --- /dev/null +++ b/src/view/SupportedLanguages.h @@ -0,0 +1,17 @@ +#ifndef SUPPORTEDLANGUAGES_H +#define SUPPORTEDLANGUAGES_H + +#include + +typedef struct { + wxString name; + wxString icon; + wxLanguage language; +} language ; + +#define NB_SUPPORTED_LANGUAGES 2 +#define ICONS_PATH "./ressources/icons" + +extern language languages[NB_SUPPORTED_LANGUAGES]; + +#endif diff --git a/src/view/wxUI.cpp b/src/view/wxUI.cpp index f28965d..de126cf 100644 --- a/src/view/wxUI.cpp +++ b/src/view/wxUI.cpp @@ -22,19 +22,6 @@ wxUI::wxUI(KissCount* kiss, const wxString& title, const wxPoint& pos, const wxS // CreateStatusBar(); // SetStatusText( wxT("Welcome to wxWidgets!") ); - months[0] = _("january"); - months[1] = _("february"); - months[2] = _("march"); - months[3] = _("april"); - months[4] = _("may"); - months[5] = _("june"); - months[6] = _("july"); - months[7] = _("august"); - months[8] = _("september"); - months[9] = _("october"); - months[10] = _("november"); - months[11] = _("december") ; - SetSizer(_hbox); _hbox->Add(buttons); @@ -49,6 +36,8 @@ wxUI::~wxUI() bool wxUI::SetLanguage(long language) { + bool res = true; + if (_locale) delete _locale; _locale = NULL; @@ -63,16 +52,39 @@ bool wxUI::SetLanguage(long language) _locale->AddCatalog(wxT("french")); _locale->AddCatalog(wxT("kisscount")); + + _language = (wxLanguage) language; } - if ((_locale == NULL || !_locale->IsOk()) && wxLocale::IsAvailable(wxLANGUAGE_ENGLISH)) + if (_locale == NULL || !_locale->IsOk()) { if (_locale) delete _locale; - _locale = new wxLocale( wxLANGUAGE_ENGLISH ); - return false; + _locale = new wxLocale(); + +#ifdef __WXGTK__ + _locale->AddCatalogLookupPathPrefix(wxT("./ressources/po")); +#endif + + _locale->AddCatalog(wxT("kisscount")); + + _language = wxLANGUAGE_ENGLISH; + res = false; } - return true; + months[0] = _("january"); + months[1] = _("february"); + months[2] = _("march"); + months[3] = _("april"); + months[4] = _("may"); + months[5] = _("june"); + months[6] = _("july"); + months[7] = _("august"); + months[8] = _("september"); + months[9] = _("october"); + months[10] = _("november"); + months[11] = _("december") ; + + return res; } void wxUI::ShowAccount() @@ -93,6 +105,8 @@ void wxUI::ChangeUser() void wxUI::LoadUser() { + User* user = _kiss->GetUser(); + if (_curPanel) { _hbox->Detach(_curPanel); @@ -105,6 +119,9 @@ void wxUI::LoadUser() if (_preferencesPanel) delete _preferencesPanel; + if (user->_preferences[wxT("language")] != wxT("")) + SetLanguage(user->GetLanguage()); + _preferencesPanel = new PreferencesPanel(_kiss, this); _accountPanel = new AccountPanel(_kiss, this); @@ -113,7 +130,8 @@ void wxUI::LoadUser() void wxUI::ShowPanel(wxPanel* panel) { - int month, year; + int month, year, account=0, preferences=0; + wxShowEvent event; if (!panel) return; @@ -122,14 +140,40 @@ void wxUI::ShowPanel(wxPanel* panel) _hbox->Detach(_curPanel); _curPanel->Hide(); } - - if (panel == _accountPanel && _needReload) + + if (_needReload) { - month = _accountPanel->_curMonth; - year = _accountPanel->_curYear; - delete _accountPanel; - panel = _accountPanel = new AccountPanel(_kiss, this); - _accountPanel->ShowMonth(month, year); + if (panel == _accountPanel) + { + account = 1; + month = _accountPanel->_curMonth; + year = _accountPanel->_curYear; + } + + if (panel == _preferencesPanel) + { + preferences = 1; + } + + delete _accountPanel; + delete _preferencesPanel; + + _accountPanel = new AccountPanel(_kiss, this); + if (year != -1) + _accountPanel->ShowMonth(month, year); + _preferencesPanel = new PreferencesPanel(_kiss, this); + + if (account) + { + _accountPanel->OnShow(event); + panel = _accountPanel; + } + if (preferences) + { + _preferencesPanel->OnShow(event); + panel = _preferencesPanel; + } + _needReload = false; } diff --git a/src/view/wxUI.h b/src/view/wxUI.h index 6a89e8f..3afab90 100644 --- a/src/view/wxUI.h +++ b/src/view/wxUI.h @@ -33,8 +33,11 @@ class wxUI: public wxFrame void KillMe(); + void ShowPanel(wxPanel* panel); void NeedReload(); + wxLanguage _language; + private: KissCount *_kiss; wxBoxSizer *_hbox; @@ -43,8 +46,6 @@ class wxUI: public wxFrame wxPanel *_curPanel; wxLocale *_locale; bool _needReload; - - void ShowPanel(wxPanel* panel); }; #endif