summaryrefslogtreecommitdiff
path: root/support/autolatex/libs/gtk3/autolatex/utils/gsettings.py
blob: c013c33470e29ce3bbc02dd584e8cb1d8471864b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013-14  Stephane Galland <galland@arakhne.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#---------------------------------
# IMPORTS
#---------------------------------

# Standard libraries
import os
# Include the Gio, Gtk and Gedit libraries
from gi.repository import Gio, Gtk
# AutoLaTeX internal libs
from . import utils

#---------------------------------
# Constants
#---------------------------------

_GSETTINGS_BASE_KEY = "apps.autolatex"

#---------------------------------
# CLASS: Manager
#---------------------------------

#
# Manager of settings in the standard Gnome settings (gsettings).
#
class Manager:

  # Replies if the Gsettings schemas associated to AutoLaTeX are installed.
  def _is_schema_installed():
    for schema in Gio.Settings.list_schemas():
      if schema == _GSETTINGS_BASE_KEY:
        return True
    return False
  _is_schema_installed = staticmethod(_is_schema_installed)
  
  # Constructor.
  def __init__(self):
    if Manager._is_schema_installed(): 
      self._sig_binded_signals = {}
      self.settings = Gio.Settings.new(_GSETTINGS_BASE_KEY)
      # Force application of gsettings
      self._update_autolatex_cmd(self.settings.get_string('autolatex-cmd'))
      self._update_autolatex_backend_cmd(self.settings.get_string('autolatex-backend-cmd'))
      # Listen on changes
      self._sig_autolatex_cmd_changed = self.settings.connect("changed::autolatex-cmd", self.on_autolatex_cmd_changed)
      self._sig_autolatex_backend_cmd_changed = self.settings.connect("changed::autolatex-backend-cmd", self.on_autolatex_backend_cmd_changed)
    else:
      self.settings = None
      self._data = {
        'force-synctex': True,
        'show-progress-info': True,
        'save-before-run-autolatex': True,
      }

  # Unbind this manager to the Gsettings daemon.
  def unbind(self):
    if self.settings:
      self.settings.disconnect(self._sig_autolatex_cmd_changed)
      self.settings.disconnect(self._sig_autolatex_backend_cmd_changed)
      for datakey in self._sig_binded_signals:
        self.settings.disconnect(self._sig_binded_signals[datakey])
      self._sig_binded_signals = {}
      self.settings.apply()

  # Connect this manager to the Gsettings daemon for the given key.
  # The Gsettings daemon will notify the given callback each time
  # the value associated with the given key has changed.
  # @param datakey - key of the data to be connected to.
  # @param callback
  def connect(self, datakey, callback):
    if self.settings:
      self._sig_binded_signals[datakey] = self.settings.connect("changed::"+str(datakey), callback)

  # Disconnect this manager to the Gsettings daemon for the given key.
  def disconnect(self, datakey):
    if self.settings:
      self.settings.disconnect(self._sig_binded_signals[datakey])
      del self._sig_binded_signals[datakey]

  # Invoked to set the command line of AutoLaTeX frontend program.
  def _update_autolatex_cmd(self, cmd):
    if cmd and os.path.isfile(cmd) and os.access(cmd, os.X_OK):
      utils.AUTOLATEX_BINARY = cmd
    else:
      utils.AUTOLATEX_BINARY = utils.DEFAULT_AUTOLATEX_BINARY

  # Invoked to set the command line of the AutoLaTeX backend program.
  def _update_autolatex_backend_cmd(self, cmd):
    if cmd and os.path.isfile(cmd) and os.access(cmd, os.X_OK):
      utils.AUTOLATEX_BACKEND_BINARY = cmd
    else:
      utils.AUTOLATEX_BACKEND_BINARY = utils.DEFAULT_AUTOLATEX_BACKEND_BINARY

  # Invoked when the command line of the AutoLaTeX frontend program
  # has been detected has changed in the Gsettings deamon.
  def on_autolatex_cmd_changed(self, settings, key, data=None):
    if self.settings:
      self._update_autolatex_cmd(self.settings.get_string('autolatex-cmd'))

  # Invoked when the command line of the AutoLaTeX backend program
  # has been detected has changed in the Gsettings deamon.
  def on_autolatex_backend_cmd_changed(self, settings, key, data=None):
    if self.settings:
      self._update_autolatex_backend_cmd(self.settings.get_string('autolatex-backend-cmd'))

  # Replies the command line of the AutoLaTeX frontend program.
  def get_autolatex_cmd(self):
    if self.settings:
      path = self.settings.get_string('autolatex-cmd')
      return path if path else None
    else:
      return None

  # Change the command line of the AutoLaTeX frontend program.
  def set_autolatex_cmd(self,path):
    if self.settings:
      path = str(path) if path else ''
      self.settings.set_string('autolatex-cmd', path)
      self.settings.apply()
    else:
      self._update_autolatex_cmd(path)

  # Replies the command line of the AutoLaTeX backend program.
  def get_autolatex_backend_cmd(self):
    if self.settings:
      path = self.settings.get_string('autolatex-backend-cmd')
      return path if path else None
    else:
      return None

  # Change the command line of the AutoLaTeX backend program.
  def set_autolatex_backend_cmd(self, path):
    if self.settings:
      path = str(path) if path else ''
      self.settings.set_string('autolatex-backend-cmd', path)
      self.settings.apply()
    else:
      self._update_autolatex_backend_cmd(path)

  # Replies if the flag "force SyncTeX" is set or unset.
  def get_force_synctex(self):
    if self.settings:
      return self.settings.get_boolean('force-synctex')
    else:
      return self._data['force-synctex']

  # Set or unset the flag "force SyncTeX".
  def set_force_synctex(self, is_force):
    if self.settings:
      self.settings.set_boolean('force-synctex', bool(is_force))
      self.settings.apply()
    else:
      self._data['force-synctex'] = bool(is_force)

  # Replies if progress information must be shown.
  def get_progress_info_visibility(self):
    if self.settings:
      return self.settings.get_boolean('show-progress-info')
    else:
      return self._data['show-progress-info']

  # Enable or disable the progress information.
  def set_progress_info_visibility(self, is_shown):
    if self.settings:
      self.settings.set_boolean('show-progress-info', bool(is_shown))
      self.settings.apply()
    else:
      self._data['show-progress-info'] = bool(is_shown)

  # Replies if the files must be saved before running AutoLaTeX.
  def get_save_before_run_autolatex(self):
    if self.settings:
      return self.settings.get_boolean('save-before-run-autolatex')
    else:
      return self._data['save-before-run-autolatex']

  # Set or unset the flag that indicates if the files must be saved
  # before running AutoLaTeX.
  def set_save_before_run_autolatex(self, is_saving):
    if self.settings:
      self.settings.set_boolean('save-before-run-autolatex', bool(is_saving))
      self.settings.apply()
    else:
      self._data['save-before-run-autolatex'] = bool(is_saving)