settings.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. # This file will handle all kinds of settings
  30. import os
  31. import json
  32. from gi.repository import Gtk
  33. from flbry import ui
  34. def get_settings_folder(flbry="flbry/"):
  35. try:
  36. data_dir = os.environ["XDG_DATA_HOME"] + "/" + flbry # Reducted back since it
  37. # broke too much and made
  38. # my settings appear in
  39. # $XDG_DATA_HOME folder
  40. # inside the repository.
  41. # WTF !!!
  42. except:
  43. data_dir = os.path.expanduser("~/.local/share/"+flbry)
  44. try:
  45. os.makedirs(data_dir)
  46. except:
  47. pass
  48. return data_dir
  49. def load():
  50. with open(get_settings_folder()+'config.json') as json_file:
  51. data = json.load(json_file)
  52. return data
  53. def save(data):
  54. with open(get_settings_folder()+'config.json', 'w') as fp:
  55. json.dump(data, fp, sort_keys=True, indent=4)
  56. def make_sure_file_exists():
  57. # Let's make some defaults
  58. defaults = {
  59. "GTK_icon_theme":"System Theme",
  60. "notifications":True,
  61. "auth_token": "",
  62. "autoconnect": False,
  63. "comment_api": "https://comments.odysee.com/api/v2",
  64. "lbrynet_binary": "flbry/lbrynet",
  65. "librarian_instance": "https://librarian.bcow.xyz/",
  66. "promote_fast_lbry_in_comments":False,
  67. "filter_tags":["mature", "sex", "porn"],
  68. "lock_password":""
  69. }
  70. # List of old and unneded or changed setting names. For example if
  71. # there was a spelling error in the key. This list exists for
  72. # backward compatibility, so users could update to the new version
  73. # without seeing two separate settings all of a sudden.
  74. old_and_bad = [
  75. "promote_fast_lbry_in_commnets" # issue #26 | Bad spelling
  76. ]
  77. # Let's write the Default theme.
  78. try:
  79. with open(get_settings_folder()+"config.json") as f:
  80. data = json.load(f)
  81. # Adding missing
  82. for i in defaults:
  83. if i not in data:
  84. data[i] = defaults[i]
  85. # Removing bad
  86. for i in old_and_bad:
  87. if i in data:
  88. del data[i]
  89. except Exception as e:
  90. data = defaults
  91. with open(get_settings_folder()+'config.json', 'w') as fp:
  92. json.dump(data, fp, sort_keys=True, indent=4)
  93. def dialogue(w, win):
  94. # This is the settings dialogue
  95. # Configuring the window
  96. dialogWindow = Gtk.Dialog("Settings",
  97. buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  98. Gtk.STOCK_OK, Gtk.ResponseType.OK),
  99. )
  100. box = dialogWindow.get_content_area()
  101. #######################################################################
  102. # #
  103. # THEMES SELECTOR #
  104. # #
  105. #######################################################################
  106. themes_setting = Gtk.ComboBoxText()
  107. #themes_setting.set_relief(Gtk.ReliefStyle.NONE)
  108. # Themes are laoded from folder in ./icons
  109. select = 0
  110. icon_themes = os.listdir(os.getcwd()+"/icons")
  111. icon_themes.append("System Theme")
  112. for n, theme in enumerate(icon_themes):
  113. themes_setting.append_text( theme )
  114. if win.settings["GTK_icon_theme"] == theme:
  115. select = n
  116. themes_setting.set_active(select)
  117. theme_box = Gtk.HBox()
  118. theme_box.pack_start(Gtk.Label(" Icon Theme (requires restart) : "), False, False, False)
  119. theme_box.pack_end(themes_setting, False, False, False)
  120. box.pack_start(theme_box, False, False, 5)
  121. #######################################################################
  122. # #
  123. # ALL THE REST #
  124. # #
  125. #######################################################################
  126. # The settings are SHARED WITH FASTLBRY TERMINAL
  127. # The rest of settings are generated by what's available in the config
  128. # file. So there could be problems. First we need exclude list.
  129. exclude = ["GTK_icon_theme", # We have a separate thing for it
  130. "save_history", # Used by FastLBRY terminal, not interesting here
  131. "theme", # The theme of FastLBRY terminal
  132. "markdown_reader",# In FastLBRY GTK we have a UI implementation for this
  133. "graph_force_ASCII", # Another FastLBRY Termianl only function
  134. "ignore_width_forcing", # FASTLBRY TERMINAL AGAIN
  135. "channel" # This one is set in the top pannel of the window
  136. ]
  137. parts = {}
  138. for name in win.settings:
  139. if name in exclude:
  140. continue
  141. parts[name] = {}
  142. parts[name]["box"] = Gtk.HBox()
  143. # I want to beutify the name for the setting
  144. pname = name.replace("_", " ")
  145. pname = " "+pname[0].upper()+pname[1:].lower()+" "
  146. parts[name]["box"].pack_start(Gtk.Label(pname), False, False, False)
  147. # Then depending on the type of the data itself, we are going to give
  148. # inputs
  149. if type(win.settings[name]) == str:
  150. parts[name]["entry"] = Gtk.Entry()
  151. parts[name]["entry"].set_text(win.settings[name])
  152. parts[name]["entry"].set_size_request(300,40)
  153. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  154. elif type(win.settings[name]) == bool:
  155. parts[name]["entry"] = Gtk.Switch()
  156. parts[name]["entry"].set_active(win.settings[name])
  157. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  158. elif type(win.settings[name]) == float:
  159. parts[name]["number"] = Gtk.Adjustment(win.settings[name],
  160. lower=0.0001,
  161. upper=1000000000,
  162. step_increment=0.1)
  163. parts[name]["entry"] = Gtk.SpinButton(adjustment=parts[name]["number"],
  164. digits=4)
  165. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  166. elif type(win.settings[name]) == int:
  167. parts[name]["number"] = Gtk.Adjustment(win.settings[name],
  168. lower=0,
  169. upper=1000000000,
  170. step_increment=4)
  171. parts[name]["entry"] = Gtk.SpinButton(adjustment=parts[name]["number"])
  172. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  173. elif type(win.settings[name]) == list:
  174. parts[name]["box"] = Gtk.VBox()
  175. parts[name]["box"].pack_start(Gtk.Label(pname), False, False, False)
  176. parts[name]["box"].pack_start( ui.tags_editor(win, win.settings[name]), True, True, 0)
  177. box.pack_start(parts[name]["box"], False, False, 5)
  178. # notifications_box = Gtk.HBox()
  179. # notifications_setting = Gtk.Switch()
  180. # notifications_setting.set_active(win.settings["notifications"])
  181. # notifications_box.pack_start(Gtk.Label(" Notifications: "), False, False, False)
  182. # notifications_box.pack_end(notifications_setting, False, False, False)
  183. # box.pack_start(notifications_box, False, False, False)
  184. #######################################################################
  185. # #
  186. # RUNING THE SETTINGS DIALOG #
  187. # #
  188. #######################################################################
  189. # Running the dialog
  190. box.show_all()
  191. response = dialogWindow.run()
  192. if response == Gtk.ResponseType.OK:
  193. # Here I want to overwrite all the setting to ones
  194. # chosen by the user.
  195. win.settings["GTK_icon_theme"] = themes_setting.get_active_text()
  196. for name in win.settings:
  197. if name in exclude:
  198. continue
  199. value = win.settings[name]
  200. if type(win.settings[name]) == str:
  201. value = parts[name]["entry"].get_text()
  202. elif type(win.settings[name]) == bool:
  203. value = parts[name]["entry"].get_active()
  204. elif type(win.settings[name]) == int:
  205. value = int(parts[name]["entry"].get_value())
  206. elif type(win.settings[name]) == float:
  207. value = parts[name]["entry"].get_value()
  208. win.settings[name] = value
  209. # Then we save everything.
  210. save(win.settings)
  211. if not win.settings["lock_password"]:
  212. win.lock_button.set_sensitive(False)
  213. else:
  214. win.lock_button.set_sensitive(True)
  215. dialogWindow.destroy()