@@ -24,7 +24,58 @@ class SettingsData:
2424 '''Class for managing current user's application settings'''
2525
2626 def __init__ (self ):
27- pass
27+ self .__settings_data_path = os .path .join (CONFIG_DIR , 'data.json' )
28+ self .__settings_defaults = {
29+ 'use_poppler_tools' : False ,
30+ }
31+ self .__settings_data = self .__get_settings_data ()
32+
33+ @property
34+ def use_poppler_tools (self ):
35+ '''If set to True, PyPDF Builder will first try to use Poppler Tools where possible
36+ to produce the desired PDFs.
37+
38+ The getter will first try to return the value stored in the
39+ instance, then try to read it out of the user data file, and if all else fails,
40+ set it to False and return that value.
41+
42+ The setter will set the according class instance property and save that property to
43+ a settings data file. If no such file exists yet, one will be created.
44+ '''
45+ return self .__settings_data .get ('use_poppler_tools' , self .__get_settings_data ()['use_poppler_tools' ])
46+
47+ @use_poppler_tools .setter
48+ def use_poppler_tools (self , val ):
49+ self .__settings_data ['use_poppler_tools' ] = val
50+ self .__save_settings_data ()
51+
52+ def __get_settings_data (self ):
53+ '''Method to retrieve current user's settings data
54+
55+ Return:
56+ dict: Dictionary of settings data with keys:
57+ * `use_poppler_tools`: user Poppler PDF tools by default
58+ '''
59+ try :
60+ with (open (self .__settings_data_path , 'r' )) as datafile :
61+ settings_data = json .load (datafile )
62+ # make sure all values are returned. If a key is non-existant, fill it with default value
63+ for key , val in self .__settings_defaults .items ():
64+ if key not in settings_data :
65+ settings_data [key ] = val
66+ except FileNotFoundError :
67+ settings_data = self .__settings_defaults
68+ return settings_data
69+
70+ def __save_settings_data (self ):
71+ if not os .path .exists (os .path .dirname (self .__settings_data_path )):
72+ plPath (os .path .dirname (self .__settings_data_path )).mkdir (parents = True , exist_ok = True )
73+ try :
74+ with (open (self .__settings_data_path , 'w' )) as datafile :
75+ json .dump (self .__settings_data , datafile )
76+ except FileNotFoundError :
77+ print ('Something went horribly wrong while trying to save your current user data.' )
78+
2879
2980class UserData :
3081 '''Class for storing current user's application data'''
@@ -474,6 +525,7 @@ def __init__(self):
474525 self .mainmenu = self .builder .get_object ('MainMenu' )
475526 self .mainwindow .config (menu = self .mainmenu )
476527 self .__status_text_variable = self .builder .get_variable ('application_status_text' )
528+ self .__settings_use_poppler_variable = self .builder .get_variable ('settings_use_poppler' )
477529 self .status_text = None
478530 self .builder .connect_callbacks (self )
479531
@@ -582,10 +634,10 @@ def show_settings(self, *args, **kwargs):
582634 method definition in case it is triggered by the keyboard shortcut, in which
583635 case `event` gets passed into the call.'''
584636 self .settings_dialog .run ()
585- # load data from settings and update widgets in dialog accordingly
637+ self . __settings_use_poppler_variable . set ( self . settings_data . use_poppler_tools )
586638
587639 def close_settings (self , * args , ** kwargs ):
588- # save settings and close it up
640+ self . settings_data . use_poppler_tools = self . __settings_use_poppler_variable . get ()
589641 self .settings_dialog .close ()
590642
591643 def cancel_settings (self , * args , ** kwargs ):
0 commit comments