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).
This commit is contained in:
Stanislav Nikitin 2017-05-10 12:04:01 +05:00
parent 1e676437d8
commit a6d53a3fd9
5 changed files with 25 additions and 22 deletions

View File

@ -34,14 +34,18 @@ func (c *Cache) CreateProfile(name string) {
func (c *Cache) deleteProfile(data map[string]string) { func (c *Cache) deleteProfile(data map[string]string) {
fmt.Println("Deleting profile " + data["profile_name"]) fmt.Println("Deleting profile " + data["profile_name"])
c.ProfilesMutex.Lock()
_, ok := c.Profiles[data["profile_name"]] _, ok := c.Profiles[data["profile_name"]]
c.ProfilesMutex.Unlock()
if ok { if ok {
c.ProfilesMutex.Lock() c.ProfilesMutex.Lock()
delete(c.Profiles, data["profile_name"]) delete(c.Profiles, data["profile_name"])
c.ProfilesMutex.Unlock() c.ProfilesMutex.Unlock()
} }
c.ProfilesMutex.Lock()
_, ok1 := c.Profiles[data["profile_name"]] _, ok1 := c.Profiles[data["profile_name"]]
c.ProfilesMutex.Unlock()
if !ok1 { if !ok1 {
fmt.Println("Profile deleted") fmt.Println("Profile deleted")
Database.Db.MustExec(Database.Db.Rebind("DELETE FROM urt_profiles WHERE name=?"), data["profile_name"]) 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) cached_profiles := make(map[string]*datamodels.Profile)
for i := range raw_profiles { 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) new_profiles := make(map[string]*datamodels.Profile)
c.ProfilesMutex.Lock()
for _, profile := range c.Profiles { for _, profile := range c.Profiles {
_, ok := cached_profiles[profile.Profile.Name] _, ok := cached_profiles[profile.Profile.Name]
if !ok { if !ok {
fmt.Println("Flushing new profile " + profile.Profile.Name) fmt.Println("Flushing new profile " + profile.Profile.Name)
new_profiles[profile.Profile.Name] = &datamodels.Profile{} new_profiles[profile.Profile.Name] = profile.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
} }
} }
c.ProfilesMutex.Unlock()
tx := Database.Db.MustBegin() tx := Database.Db.MustBegin()
fmt.Println("Adding new profiles...") fmt.Println("Adding new profiles...")
for _, profile := range 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...") fmt.Println("Updating existing profiles...")
for _, profile := range cached_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() tx.Commit()
fmt.Println("Done") fmt.Println("Done")
@ -101,15 +103,12 @@ func (c *Cache) LoadProfiles(data map[string]string) {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
c.ProfilesMutex.Lock()
for _, profile := range raw_profiles { for _, profile := range raw_profiles {
c.Profiles[profile.Name] = &cachemodels.Profile{} c.Profiles[profile.Name] = &cachemodels.Profile{}
c.Profiles[profile.Name].Profile = &datamodels.Profile{} c.Profiles[profile.Name].Profile = &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.ProfilesMutex.Unlock()
fmt.Println("Load completed. Loaded " + strconv.Itoa(len(c.Profiles)) + " profiles.") fmt.Println("Load completed. Loaded " + strconv.Itoa(len(c.Profiles)) + " profiles.")
} }

View File

@ -363,12 +363,14 @@ func (o *OptionsDialog) loadProfiles(data map[string]string) {
fmt.Println("Loading profiles...") fmt.Println("Loading profiles...")
o.profiles_list_store.Clear() o.profiles_list_store.Clear()
ctx.Cache.ProfilesMutex.Lock()
for _, p := range ctx.Cache.Profiles { for _, p := range ctx.Cache.Profiles {
var iter gtk.TreeIter var iter gtk.TreeIter
o.profiles_list_store.Append(&iter) o.profiles_list_store.Append(&iter)
o.profiles_list_store.Set(&iter, 0, p.Profile.Name) o.profiles_list_store.Set(&iter, 0, p.Profile.Name)
o.profiles_list_store.Set(&iter, 1, p.Profile.Version) o.profiles_list_store.Set(&iter, 1, p.Profile.Version)
} }
ctx.Cache.ProfilesMutex.Unlock()
} }
func (o *OptionsDialog) saveAppearance() { func (o *OptionsDialog) saveAppearance() {

View File

@ -393,6 +393,7 @@ func (op *OptionsProfile) saveProfile() {
} }
} }
} }
ctx.Eventer.LaunchEvent("flushProfiles", nil)
ctx.Eventer.LaunchEvent("loadProfilesIntoOptionsWindow", map[string]string{}) ctx.Eventer.LaunchEvent("loadProfilesIntoOptionsWindow", map[string]string{})
ctx.Eventer.LaunchEvent("loadProfilesIntoMainWindow", map[string]string{}) ctx.Eventer.LaunchEvent("loadProfilesIntoMainWindow", map[string]string{})
op.window.Destroy() op.window.Destroy()

View File

@ -176,7 +176,7 @@ func (m *MainWindow) initializeTabs() {
serverspagewidget := widgets.NewQWidget(nil, core.Qt__Widget) serverspagewidget := widgets.NewQWidget(nil, core.Qt__Widget)
serverspagewidget_policy := widgets.NewQSizePolicy2(widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__DefaultType) serverspagewidget_policy := widgets.NewQSizePolicy2(widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__DefaultType)
serverspagewidget.SetSizePolicy(serverspagewidget_policy) serverspagewidget.SetSizePolicy(serverspagewidget_policy)
m.tabs.AddTab(serverspagewidget, "Servers") m.tabs.AddTab(serverspagewidget, ctx.Translator.Translate("Servers", nil))
serverspagewidget_layout := widgets.NewQHBoxLayout() serverspagewidget_layout := widgets.NewQHBoxLayout()
serverspagewidget_layout.SetContentsMargins(4, 4, 4, 4) serverspagewidget_layout.SetContentsMargins(4, 4, 4, 4)
serverspagewidget.SetLayout(serverspagewidget_layout) serverspagewidget.SetLayout(serverspagewidget_layout)
@ -200,7 +200,7 @@ func (m *MainWindow) initializeTabs() {
// Hide offline servers checkbox. // Hide offline servers checkbox.
m.all_servers_hide_offline = widgets.NewQCheckBox(nil) 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) m.all_servers_hide_offline.SetSizePolicy(filters_size_policy)
serverspagewidget_filters_layout.AddWidget(m.all_servers_hide_offline, 0, core.Qt__AlignTop) serverspagewidget_filters_layout.AddWidget(m.all_servers_hide_offline, 0, core.Qt__AlignTop)
// Restore value of hide offline servers checkbox. // Restore value of hide offline servers checkbox.
@ -216,7 +216,7 @@ func (m *MainWindow) initializeTabs() {
// Hide private servers. // Hide private servers.
m.all_servers_hide_private = widgets.NewQCheckBox(nil) 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) m.all_servers_hide_private.SetSizePolicy(filters_size_policy)
serverspagewidget_filters_layout.AddWidget(m.all_servers_hide_private, 0, core.Qt__AlignTop) serverspagewidget_filters_layout.AddWidget(m.all_servers_hide_private, 0, core.Qt__AlignTop)
// Restore checkbox value. // Restore checkbox value.
@ -251,7 +251,7 @@ func (m *MainWindow) initializeTabs() {
// Create a strings slice with gamemodes, using sorted keys. // Create a strings slice with gamemodes, using sorted keys.
gmodes := make([]string, 0, len(m.gamemodes)) gmodes := make([]string, 0, len(m.gamemodes))
// Add "All gamemodes" as first gamemode :) // 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 { for i := range gm_keys {
ks := strconv.Itoa(gm_keys[i]) ks := strconv.Itoa(gm_keys[i])
gmodes = append(gmodes, m.gamemodes[ks]) gmodes = append(gmodes, m.gamemodes[ks])
@ -268,7 +268,7 @@ func (m *MainWindow) initializeTabs() {
favoritespagewidget := widgets.NewQWidget(nil, core.Qt__Widget) favoritespagewidget := widgets.NewQWidget(nil, core.Qt__Widget)
favoritespagewidget_policy := widgets.NewQSizePolicy2(widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__DefaultType) favoritespagewidget_policy := widgets.NewQSizePolicy2(widgets.QSizePolicy__Expanding, widgets.QSizePolicy__Expanding, widgets.QSizePolicy__DefaultType)
favoritespagewidget.SetSizePolicy(favoritespagewidget_policy) favoritespagewidget.SetSizePolicy(favoritespagewidget_policy)
m.tabs.AddTab(favoritespagewidget, "Favorites") m.tabs.AddTab(favoritespagewidget, ctx.Translator.Translate("Favorites", nil))
favoritespagewidget_layout := widgets.NewQHBoxLayout() favoritespagewidget_layout := widgets.NewQHBoxLayout()
favoritespagewidget_layout.SetContentsMargins(4, 4, 4, 4) favoritespagewidget_layout.SetContentsMargins(4, 4, 4, 4)
favoritespagewidget.SetLayout(favoritespagewidget_layout) favoritespagewidget.SetLayout(favoritespagewidget_layout)
@ -292,7 +292,7 @@ func (m *MainWindow) initializeTabs() {
// Hide offline servers checkbox. // Hide offline servers checkbox.
m.fav_servers_hide_offline = widgets.NewQCheckBox(nil) 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) m.fav_servers_hide_offline.SetSizePolicy(filters_size_policy)
favoritespagewidget_filters_layout.AddWidget(m.fav_servers_hide_offline, 0, core.Qt__AlignTop) favoritespagewidget_filters_layout.AddWidget(m.fav_servers_hide_offline, 0, core.Qt__AlignTop)
// Restore it's value. // Restore it's value.
@ -307,7 +307,7 @@ func (m *MainWindow) initializeTabs() {
// Hide private servers. // Hide private servers.
m.fav_servers_hide_private = widgets.NewQCheckBox(nil) 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) m.fav_servers_hide_private.SetSizePolicy(filters_size_policy)
favoritespagewidget_filters_layout.AddWidget(m.fav_servers_hide_private, 0, core.Qt__AlignTop) 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"] fav_servers_hide_private_cb_val, ok := ctx.Cfg.Cfg["/serverslist/favorite/hide_private"]

View File

@ -14,6 +14,7 @@ import (
"github.com/pztrn/urtrator/common" "github.com/pztrn/urtrator/common"
"github.com/pztrn/urtrator/context" "github.com/pztrn/urtrator/context"
"github.com/pztrn/urtrator/ui/gtk2" "github.com/pztrn/urtrator/ui/gtk2"
//"github.com/pztrn/urtrator/ui/qt5"
// stdlib // stdlib
"fmt" "fmt"