Subversion Repositories basico

Rev

Rev 279 | Rev 297 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
232 t00m 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
264 t00m 3
"""
251 t00m 4
# File: wdg_settingsview.py
232 t00m 5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Settings Widget
264 t00m 8
"""
232 t00m 9
 
237 t00m 10
import os
232 t00m 11
from os.path import sep as SEP
237 t00m 12
import glob
276 t00m 13
from html import escape
279 t00m 14
import logging
232 t00m 15
import gi
16
gi.require_version('Gtk', '3.0')
17
gi.require_version('Gdk', '3.0')
18
from gi.repository import Gtk
19
from gi.repository import Gdk
20
from gi.repository import Pango
21
 
276 t00m 22
from basico.core.mod_srv import Service
243 t00m 23
from basico.core.mod_env import ROOT, USER_DIR, APP, LPATH, GPATH, FILE
232 t00m 24
 
279 t00m 25
logger = logging.getLogger(__name__)
237 t00m 26
 
276 t00m 27
class SettingsView(Gtk.ScrolledWindow, Service):
232 t00m 28
    def __init__(self, app):
29
        Gtk.ScrolledWindow.__init__(self)
30
        self.app = app
31
        self.get_services()
32
        self.setup()
33
 
34
    def get_services(self):
35
        """Load services to be used in this class
36
        """
246 t00m 37
        self.srvgui = self.app.get_service("GUI")
38
        self.srvbnr = self.app.get_service('BNR')
39
        self.srvutl = self.app.get_service('Utils')
40
        self.srvant = self.app.get_service('Annotation')
41
        self.srvdtb = self.app.get_service('DB')
42
        self.srvbnr = self.app.get_service('BNR')
232 t00m 43
 
44
 
45
    def setup(self):
46
        self.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
47
        self.set_shadow_type(Gtk.ShadowType.IN)
48
        self.set_hexpand(True)
49
        self.set_vexpand(True)
50
        viewport = Gtk.Viewport()
51
        self.treeview = Gtk.TreeView()
237 t00m 52
        self.treeview.grab_focus()
232 t00m 53
        viewport.add(self.treeview)
54
        self.add(viewport)
55
 
56
        # Setup model
57
        self.model = Gtk.TreeStore(
58
            str,        # Primary key: SECTION-KEY-VALUE
59
            str,        # Key
60
            str,        # Value
61
        )
62
 
63
        # Setup columns
64
        # Primary key: SECTION-KEY-VALUE
65
        self.renderer_pkey = Gtk.CellRendererText()
66
        self.column_pkey = Gtk.TreeViewColumn('Primary Key', self.renderer_pkey, text=0)
67
        self.column_pkey.set_visible(False)
68
        self.column_pkey.set_expand(False)
69
        self.column_pkey.set_clickable(False)
70
        self.column_pkey.set_sort_indicator(False)
71
        self.treeview.append_column(self.column_pkey)
72
 
73
        # Key
74
        self.renderer_key = Gtk.CellRendererText()
75
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, markup=1)
76
        self.column_key.set_visible(True)
77
        self.column_key.set_expand(False)
78
        self.column_key.set_clickable(False)
79
        self.column_key.set_sort_indicator(False)
80
        self.treeview.append_column(self.column_key)
81
 
82
        # Value
83
        self.renderer_value = Gtk.CellRendererText()
84
        self.column_value = Gtk.TreeViewColumn('Value', self.renderer_value, markup=2)
85
        self.column_value.set_visible(True)
86
        self.column_value.set_expand(True)
87
        self.column_value.set_clickable(False)
88
        self.column_value.set_sort_indicator(False)
89
        self.treeview.append_column(self.column_value)
90
 
91
        # TreeView common
92
        self.sorted_model = Gtk.TreeModelSort(model=self.model)
237 t00m 93
        self.sorted_model.set_sort_column_id(0, Gtk.SortType.ASCENDING)
232 t00m 94
        self.treeview.set_model(self.sorted_model)
237 t00m 95
        self.treeview.modify_font(Pango.FontDescription('Monospace 10'))
232 t00m 96
 
97
        self.treeview.set_can_focus(False)
237 t00m 98
        self.treeview.set_headers_visible(False)
232 t00m 99
        self.treeview.set_enable_search(True)
100
        self.treeview.set_hover_selection(False)
101
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
102
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))
103
 
104
        # Selection
105
        self.selection = self.treeview.get_selection()
106
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)
107
 
108
        self.show_all()
109
 
110
 
111
    def update(self):
112
        self.model.clear()
246 t00m 113
        self.srvstg = self.app.get_service('Settings')
114
        config = self.srvstg.load()
232 t00m 115
        sdict = {}
116
 
117
        # ENVIRONMENT NODE
118
        root = self.model.append(None, ['root', '<big><b>Environment</b></big>', ''])
119
 
120
 
238 t00m 121
        # ~ self.model.append(root, ['', '<span color="blue">App System Dir</span>', ROOT])
122
        # ~ self.model.append(root, ['', '<span color="blue">App Local Dir</span>', LPATH['ROOT']])
232 t00m 123
 
124
        pid = self.model.append(root, ['', '<b>APP</b>', ''])
125
        for key in APP:
126
            value = APP[key]
127
            if isinstance(value, list):
128
                value = ('\n'.join(value))
129
            value = escape(value)
130
            self.model.append(pid, ['APP-%s' % key, '<span color="blue">%s</span>' % key, value])
238 t00m 131
 
232 t00m 132
        pid = self.model.append(root, ['', '<b>LPATH</b>', ''])
133
        for key in LPATH:
134
            self.model.append(pid, ['LPATH-%s' % key, '<span color="blue">%s</span>' % key, LPATH[key]])
238 t00m 135
 
232 t00m 136
        pid = self.model.append(root, ['', '<b>GPATH</b>', ''])
137
        for key in GPATH:
138
            self.model.append(pid, ['GPATH-%s' % key, '<span color="blue">%s</span>' % key, GPATH[key]])
238 t00m 139
 
232 t00m 140
        pid = self.model.append(root, ['', '<b>FILE</b>', ''])
141
        for key in FILE:
142
            self.model.append(pid, ['FILE-%s' % key, '<span color="blue">%s</span>' % key, FILE[key]])
143
 
144
        # SETTINGS NODE
145
        root = self.model.append(None, ['root', '<big><b>Settings</b></big>', ''])
146
 
147
        def get_pid(key):
148
            try:
149
                pid = sdict[key]
150
            except:
151
                pid = self.model.append(root, [key, '<b>%s</b>' % key, ''])
152
                sdict[key] = pid
153
            return pid
154
 
155
        for skey in config.keys():
156
            pid = get_pid(skey)
157
            for key in config[skey]:
158
                value = str(config[skey][key])
159
                pkey = '%s-%s-%s' % (skey, key, value)
160
                self.model.append(pid, [pkey, '<span color="blue">%s</span>' % key, value])
161
 
162
 
163
        # WIDGETS NODE
287 t00m 164
        root = self.model.append(None, ['root', '<big><b>Runtime Objects</b></big>', ''])
232 t00m 165
 
287 t00m 166
        wdgdict = {}
167
        widgets = self.srvgui.get_widgets()
168
        for name in widgets:
169
            objname = escape(str(widgets[name]))
170
            pobj = gobject = objname[4:objname.find(' ')]
171
            gobj_left = gobject[:gobject.find('.')]
172
            gobj_right = gobject[gobject.find('.')+1:]
173
            pobj = gobject[:gobject.find('.')]
174
            try:
175
                ppid_left = wdgdict[gobj_left]
176
            except:
177
                ppid_left = self.model.append(root, [gobj_left, '<b>%s</b>' % gobj_left, ''])
178
                wdgdict[gobj_left] = ppid_left
232 t00m 179
 
180
 
287 t00m 181
            try:
182
                ppid_right = wdgdict[gobj_right]
183
            except:
184
                ppid_right = self.model.append(ppid_left, [gobj_right, '<b>%s</b>' % gobj_right, ''])
185
                wdgdict[gobj_right] = ppid_right
232 t00m 186
 
287 t00m 187
            self.model.append(ppid_right, [gobject, name, objname[4:-4]])
237 t00m 188
 
189
        # BACKUPS NODE
276 t00m 190
        # ~ root = self.model.append(None, ['root', '<big><b>Backups Overview</b></big>', ''])
191
        # ~ backups = self.srvbnr.get_all_backups()
192
        # ~ for backup in backups:
193
            # ~ try:
194
                # ~ timestamp = os.path.basename(backup)[7:-4]
195
                # ~ fuzzydate = self.srvutl.fuzzy_date_from_timestamp(timestamp)
196
                # ~ sapnotes, annotations = self.srvbnr.test(backup)
197
                # ~ message = "Database backed up %s. It contains %d SAP Notes and %d annotations." % (fuzzydate, sapnotes, annotations)
198
                # ~ self.model.append(root, [backup, os.path.basename(backup), message])
199
            # ~ except:
200
                # ~ # It will fail the first time Basico is launched as there is no database
201
                # ~ pass
237 t00m 202
 
203
 
204
        # STATS NODE
205
        root = self.model.append(None, ['root', '<big><b>Stats Overview</b></big>', ''])
246 t00m 206
        s_count = self.srvdtb.get_total()
207
        a_count = self.srvant.get_total()
208
        stats = self.srvdtb.get_stats()
237 t00m 209
        sapnotes_node = self.model.append(root, ['', 'SAP Notes', "%6d" % s_count])
210
        components_node = self.model.append(sapnotes_node, ['', 'Components', ''])
211
        for component in stats['maincomp']:
212
            self.model.append(components_node, [component, component, "%6d" % stats['maincomp'][component]])
213
        self.model.append(root, ['', 'Annotations', "%6d" % a_count])
276 t00m 214