From a6d53a3fd9592fcff1be92c80b8c53643745ec2d Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Wed, 10 May 2017 12:04:01 +0500 Subject: [PATCH] Profiles system stability improvements and fixes, qt5 works. Now everything that uses Profiles from cache will also use it's mutex to lock access. This brings more stability improvements :). Also fixed profile update in database. Qt5 interface is now translated (kinda). --- cache/cache_profiles.go | 29 ++++++++++++++--------------- ui/gtk2/options.go | 2 ++ ui/gtk2/options_profile.go | 1 + ui/qt5/mainwindow_init.go | 14 +++++++------- urtrator.go | 1 + 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/cache/cache_profiles.go b/cache/cache_profiles.go index 129ffad..3bf5c51 100644 --- a/cache/cache_profiles.go +++ b/cache/cache_profiles.go @@ -34,14 +34,18 @@ func (c *Cache) CreateProfile(name string) { func (c *Cache) deleteProfile(data map[string]string) { fmt.Println("Deleting profile " + data["profile_name"]) + c.ProfilesMutex.Lock() _, ok := c.Profiles[data["profile_name"]] + c.ProfilesMutex.Unlock() if ok { c.ProfilesMutex.Lock() delete(c.Profiles, data["profile_name"]) c.ProfilesMutex.Unlock() } + c.ProfilesMutex.Lock() _, ok1 := c.Profiles[data["profile_name"]] + c.ProfilesMutex.Unlock() if !ok1 { fmt.Println("Profile deleted") Database.Db.MustExec(Database.Db.Rebind("DELETE FROM urt_profiles WHERE name=?"), data["profile_name"]) @@ -61,32 +65,30 @@ func (c *Cache) FlushProfiles(data map[string]string) { cached_profiles := make(map[string]*datamodels.Profile) for i := range raw_profiles { - cached_profiles[raw_profiles[i].Name] = &raw_profiles[i] + cached_profiles[raw_profiles[i].Name] = c.Profiles[raw_profiles[i].Name].Profile } new_profiles := make(map[string]*datamodels.Profile) + c.ProfilesMutex.Lock() for _, profile := range c.Profiles { _, ok := cached_profiles[profile.Profile.Name] if !ok { fmt.Println("Flushing new profile " + profile.Profile.Name) - new_profiles[profile.Profile.Name] = &datamodels.Profile{} - new_profiles[profile.Profile.Name].Name = profile.Profile.Name - new_profiles[profile.Profile.Name].Version = profile.Profile.Version - new_profiles[profile.Profile.Name].Binary = profile.Profile.Binary - new_profiles[profile.Profile.Name].Second_x_session = profile.Profile.Second_x_session - new_profiles[profile.Profile.Name].Additional_params = profile.Profile.Additional_params + new_profiles[profile.Profile.Name] = profile.Profile } } + c.ProfilesMutex.Unlock() tx := Database.Db.MustBegin() fmt.Println("Adding new profiles...") for _, profile := range new_profiles { - tx.NamedExec("INSERT INTO urt_profiles (name, version, binary, second_x_session, additional_parameters) VALUES (:name, :version, :binary, :second_x_session, :additional_parameters)", &profile) + tx.NamedExec("INSERT INTO urt_profiles (name, version, binary, second_x_session, additional_parameters, profile_path) VALUES (:name, :version, :binary, :second_x_session, :additional_parameters, :profile_path)", &profile) } fmt.Println("Updating existing profiles...") for _, profile := range cached_profiles { - tx.NamedExec("UPDATE urt_profiles SET name=:name, version=:version, binary=:binary, second_x_session=:second_x_session, additional_parameters=:additional_parameters WHERE name=:name", &profile) + fmt.Println(fmt.Sprintf("%+v", profile)) + tx.NamedExec("UPDATE urt_profiles SET name=:name, version=:version, binary=:binary, second_x_session=:second_x_session, additional_parameters=:additional_parameters, profile_path=:profile_path WHERE name=:name", &profile) } tx.Commit() fmt.Println("Done") @@ -101,15 +103,12 @@ func (c *Cache) LoadProfiles(data map[string]string) { fmt.Println(err.Error()) } + c.ProfilesMutex.Lock() for _, profile := range raw_profiles { c.Profiles[profile.Name] = &cachemodels.Profile{} - c.Profiles[profile.Name].Profile = &datamodels.Profile{} - c.Profiles[profile.Name].Profile.Name = profile.Name - c.Profiles[profile.Name].Profile.Version = profile.Version - c.Profiles[profile.Name].Profile.Binary = profile.Binary - c.Profiles[profile.Name].Profile.Second_x_session = profile.Second_x_session - c.Profiles[profile.Name].Profile.Additional_params = profile.Additional_params + c.Profiles[profile.Name].Profile = &profile } + c.ProfilesMutex.Unlock() fmt.Println("Load completed. Loaded " + strconv.Itoa(len(c.Profiles)) + " profiles.") } diff --git a/ui/gtk2/options.go b/ui/gtk2/options.go index c23a126..d5f24fb 100644 --- a/ui/gtk2/options.go +++ b/ui/gtk2/options.go @@ -363,12 +363,14 @@ func (o *OptionsDialog) loadProfiles(data map[string]string) { fmt.Println("Loading profiles...") o.profiles_list_store.Clear() + ctx.Cache.ProfilesMutex.Lock() for _, p := range ctx.Cache.Profiles { var iter gtk.TreeIter o.profiles_list_store.Append(&iter) o.profiles_list_store.Set(&iter, 0, p.Profile.Name) o.profiles_list_store.Set(&iter, 1, p.Profile.Version) } + ctx.Cache.ProfilesMutex.Unlock() } func (o *OptionsDialog) saveAppearance() { diff --git a/ui/gtk2/options_profile.go b/ui/gtk2/options_profile.go index 5547402..4b14fa4 100644 --- a/ui/gtk2/options_profile.go +++ b/ui/gtk2/options_profile.go @@ -393,6 +393,7 @@ func (op *OptionsProfile) saveProfile() { } } } + ctx.Eventer.LaunchEvent("flushProfiles", nil) ctx.Eventer.LaunchEvent("loadProfilesIntoOptionsWindow", map[string]string{}) ctx.Eventer.LaunchEvent("loadProfilesIntoMainWindow", map[string]string{}) op.window.Destroy() diff --git a/ui/qt5/mainwindow_init.go b/ui/qt5/mainwindow_init.go index ff58e67..0da224a 100644 --- a/ui/qt5/mainwindow_init.go +++ b/ui/qt5/mainwindow_init.go @@ -176,7 +176,7 @@ func (m *MainWindow) initializeTabs() { serverspagewidget := widgets.NewQWidget(nil, core.Qt__Widget) serverspagewidget_policy := widgets.NewQSizePolicy2(widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__DefaultType) serverspagewidget.SetSizePolicy(serverspagewidget_policy) - m.tabs.AddTab(serverspagewidget, "Servers") + m.tabs.AddTab(serverspagewidget, ctx.Translator.Translate("Servers", nil)) serverspagewidget_layout := widgets.NewQHBoxLayout() serverspagewidget_layout.SetContentsMargins(4, 4, 4, 4) serverspagewidget.SetLayout(serverspagewidget_layout) @@ -200,7 +200,7 @@ func (m *MainWindow) initializeTabs() { // Hide offline servers checkbox. m.all_servers_hide_offline = widgets.NewQCheckBox(nil) - m.all_servers_hide_offline.SetText("Hide offline servers") + m.all_servers_hide_offline.SetText(ctx.Translator.Translate("Hide offline servers", nil)) m.all_servers_hide_offline.SetSizePolicy(filters_size_policy) serverspagewidget_filters_layout.AddWidget(m.all_servers_hide_offline, 0, core.Qt__AlignTop) // Restore value of hide offline servers checkbox. @@ -216,7 +216,7 @@ func (m *MainWindow) initializeTabs() { // Hide private servers. m.all_servers_hide_private = widgets.NewQCheckBox(nil) - m.all_servers_hide_private.SetText("Hide private servers") + m.all_servers_hide_private.SetText(ctx.Translator.Translate("Hide private servers", nil)) m.all_servers_hide_private.SetSizePolicy(filters_size_policy) serverspagewidget_filters_layout.AddWidget(m.all_servers_hide_private, 0, core.Qt__AlignTop) // Restore checkbox value. @@ -251,7 +251,7 @@ func (m *MainWindow) initializeTabs() { // Create a strings slice with gamemodes, using sorted keys. gmodes := make([]string, 0, len(m.gamemodes)) // Add "All gamemodes" as first gamemode :) - gmodes = append(gmodes, "All gamemodes") + gmodes = append(gmodes, ctx.Translator.Translate("All gamemodes", nil)) for i := range gm_keys { ks := strconv.Itoa(gm_keys[i]) gmodes = append(gmodes, m.gamemodes[ks]) @@ -268,7 +268,7 @@ func (m *MainWindow) initializeTabs() { favoritespagewidget := widgets.NewQWidget(nil, core.Qt__Widget) favoritespagewidget_policy := widgets.NewQSizePolicy2(widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__DefaultType) favoritespagewidget.SetSizePolicy(favoritespagewidget_policy) - m.tabs.AddTab(favoritespagewidget, "Favorites") + m.tabs.AddTab(favoritespagewidget, ctx.Translator.Translate("Favorites", nil)) favoritespagewidget_layout := widgets.NewQHBoxLayout() favoritespagewidget_layout.SetContentsMargins(4, 4, 4, 4) favoritespagewidget.SetLayout(favoritespagewidget_layout) @@ -292,7 +292,7 @@ func (m *MainWindow) initializeTabs() { // Hide offline servers checkbox. m.fav_servers_hide_offline = widgets.NewQCheckBox(nil) - m.fav_servers_hide_offline.SetText("Hide offline servers") + m.fav_servers_hide_offline.SetText(ctx.Translator.Translate("Hide offline servers", nil)) m.fav_servers_hide_offline.SetSizePolicy(filters_size_policy) favoritespagewidget_filters_layout.AddWidget(m.fav_servers_hide_offline, 0, core.Qt__AlignTop) // Restore it's value. @@ -307,7 +307,7 @@ func (m *MainWindow) initializeTabs() { // Hide private servers. m.fav_servers_hide_private = widgets.NewQCheckBox(nil) - m.fav_servers_hide_private.SetText("Hide private servers") + m.fav_servers_hide_private.SetText(ctx.Translator.Translate("Hide private servers", nil)) m.fav_servers_hide_private.SetSizePolicy(filters_size_policy) favoritespagewidget_filters_layout.AddWidget(m.fav_servers_hide_private, 0, core.Qt__AlignTop) fav_servers_hide_private_cb_val, ok := ctx.Cfg.Cfg["/serverslist/favorite/hide_private"] diff --git a/urtrator.go b/urtrator.go index 2c7ee99..e63742a 100644 --- a/urtrator.go +++ b/urtrator.go @@ -14,6 +14,7 @@ import ( "github.com/pztrn/urtrator/common" "github.com/pztrn/urtrator/context" "github.com/pztrn/urtrator/ui/gtk2" + //"github.com/pztrn/urtrator/ui/qt5" // stdlib "fmt"