Configuration and tray icon.

Configuration is now properly saved for parameters in Options window.
Also they are restored fine.

Added tray icon with two default actions: show/hide and exit. More
to come.
This commit is contained in:
2016-10-05 01:03:46 +05:00
parent 0f5dd8f236
commit 8e58104cfa
7 changed files with 161 additions and 26 deletions

View File

@@ -17,6 +17,7 @@ import (
"strconv"
// local
"github.com/pztrn/urtrator/configuration"
"github.com/pztrn/urtrator/datamodels"
// Other
@@ -25,16 +26,31 @@ import (
)
type Database struct {
// Configuration.
cfg *configuration.Config
// Pointer to initialized database connection.
Db *sqlx.DB
}
func (d *Database) Close() {
fmt.Println("Closing database...")
// Save configuration.
// Delete previous configuration.
d.Db.MustExec("DELETE FROM configuration")
tx := d.Db.MustBegin()
for k, v := range cfg.Cfg {
cfg_item := datamodels.Configuration{}
cfg_item.Key = k
cfg_item.Value = v
tx.NamedExec("INSERT INTO configuration (key, value) VALUES (:key, :value)", &cfg_item)
}
tx.Commit()
d.Db.Close()
}
func (d *Database) Initialize() {
func (d *Database) Initialize(cfg *configuration.Config) {
fmt.Println("Initializing database...")
// Connect to database.
@@ -45,6 +61,15 @@ func (d *Database) Initialize() {
fmt.Println(err.Error())
}
d.Db = db
// Load configuration.
cfgs := []datamodels.Configuration{}
d.Db.Select(&cfgs, "SELECT * FROM configuration")
if len(cfgs) > 0 {
for i := range cfgs {
cfg.Cfg[cfgs[i].Key] = cfgs[i].Value
}
}
}
func (d *Database) Migrate() {

View File

@@ -39,30 +39,13 @@ INSERT INTO database (version) VALUES (1);
// Migrate database to latest version.
// ToDo: make it more good :).
func migrate_full(db *Database, version int) {
if version < 1 {
start_to_one(db)
version = 1
}
if version == 1 {
one_to_two(db)
version = 2
}
if version == 2 {
two_to_three(db)
version = 3
}
if version == 3 {
three_to_four(db)
version = 4
}
if version == 4 {
four_to_five(db)
version = 5
}
if version == 5 {
five_to_six(db)
version = 6
}
if version < 1 {start_to_one(db); version = 1}
if version == 1 {one_to_two(db); version = 2}
if version == 2 {two_to_three(db); version = 3}
if version == 3 {three_to_four(db); version = 4}
if version == 4 {four_to_five(db); version = 5}
if version == 5 {five_to_six(db); version = 6 }
if version == 6 {six_to_seven(db); version = 7}
}
// Initial database structure.
@@ -108,3 +91,10 @@ func five_to_six(db *Database) {
db.Db.MustExec("ALTER TABLE servers ADD profile_to_use VARCHAR(128) DEFAULT ''")
db.Db.MustExec("UPDATE database SET version=6")
}
// Configuration storage.
func six_to_seven(db *Database) {
fmt.Println("Upgrading database from 6 to 7...")
db.Db.MustExec("CREATE TABLE configuration (key VARCHAR(128) NOT NULL, value VARCHAR(1024) NOT NULL)")
db.Db.MustExec("UPDATE database SET version=7")
}