settings.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. "live_stream_player":"vlc",
  70. "default_tab":"following"
  71. }
  72. # List of old and unneded or changed setting names. For example if
  73. # there was a spelling error in the key. This list exists for
  74. # backward compatibility, so users could update to the new version
  75. # without seeing two separate settings all of a sudden.
  76. old_and_bad = [
  77. "promote_fast_lbry_in_commnets" # issue #26 | Bad spelling
  78. ]
  79. # Let's write the Default theme.
  80. try:
  81. with open(get_settings_folder()+"config.json") as f:
  82. data = json.load(f)
  83. # Adding missing
  84. for i in defaults:
  85. if i not in data:
  86. data[i] = defaults[i]
  87. # Removing bad
  88. for i in old_and_bad:
  89. if i in data:
  90. del data[i]
  91. except Exception as e:
  92. data = defaults
  93. with open(get_settings_folder()+'config.json', 'w') as fp:
  94. json.dump(data, fp, sort_keys=True, indent=4)
  95. def dialogue(w, win):
  96. # This is the settings dialogue
  97. # Configuring the window
  98. dialogWindow = Gtk.Dialog("Settings",
  99. buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  100. Gtk.STOCK_OK, Gtk.ResponseType.OK),
  101. )
  102. dialogWindow.set_size_request(500,500)
  103. mainbox = dialogWindow.get_content_area()
  104. scrl = Gtk.ScrolledWindow()
  105. box = Gtk.VBox()
  106. mainbox.pack_start(scrl, 1,1,1)
  107. scrl.add(box)
  108. mainbox.show_all()
  109. #######################################################################
  110. # #
  111. # THEMES SELECTOR #
  112. # #
  113. #######################################################################
  114. themes_setting = Gtk.ComboBoxText()
  115. #themes_setting.set_relief(Gtk.ReliefStyle.NONE)
  116. # Themes are laoded from folder in ./icons
  117. select = 0
  118. icon_themes = os.listdir(os.getcwd()+"/icons")
  119. icon_themes.append("System Theme")
  120. for n, theme in enumerate(icon_themes):
  121. themes_setting.append_text( theme )
  122. if win.settings["GTK_icon_theme"] == theme:
  123. select = n
  124. themes_setting.set_active(select)
  125. theme_box = Gtk.HBox()
  126. theme_box.pack_start(Gtk.Label(" Icon Theme (requires restart) : "), False, False, False)
  127. theme_box.pack_end(themes_setting, False, False, False)
  128. box.pack_start(theme_box, False, False, 5)
  129. # The default tab. Whether it's following or suggestions
  130. default_tab = Gtk.ComboBoxText()
  131. default_tab.append_text("Following")
  132. default_tab.append_text("Suggestions")
  133. if win.settings["default_tab"] == "suggest":
  134. default_tab.set_active(1)
  135. else:
  136. default_tab.set_active(0)
  137. default_box = Gtk.HBox()
  138. default_box.pack_start(Gtk.Label(" Default Tab : "), False, False, False)
  139. default_box.pack_end(default_tab, False, False, False)
  140. box.pack_start(default_box, False, False, 5)
  141. #######################################################################
  142. # #
  143. # ALL THE REST #
  144. # #
  145. #######################################################################
  146. # The settings are SHARED WITH FASTLBRY TERMINAL
  147. # The rest of settings are generated by what's available in the config
  148. # file. So there could be problems. First we need exclude list.
  149. exclude = ["GTK_icon_theme", # We have a separate thing for it
  150. "save_history", # Used by FastLBRY terminal, not interesting here
  151. "theme", # The theme of FastLBRY terminal
  152. "markdown_reader",# In FastLBRY GTK we have a UI implementation for this
  153. "graph_force_ASCII", # Another FastLBRY Termianl only function
  154. "ignore_width_forcing", # FASTLBRY TERMINAL AGAIN
  155. "channel", # This one is set in the top pannel of the window
  156. "default_tab" # Implemented separately
  157. ]
  158. parts = {}
  159. for name in win.settings:
  160. if name in exclude:
  161. continue
  162. parts[name] = {}
  163. parts[name]["box"] = Gtk.HBox()
  164. # I want to beutify the name for the setting
  165. pname = name.replace("_", " ")
  166. pname = " "+pname[0].upper()+pname[1:].lower()+" "
  167. parts[name]["box"].pack_start(Gtk.Label(pname), False, False, False)
  168. # Then depending on the type of the data itself, we are going to give
  169. # inputs
  170. if type(win.settings[name]) == str:
  171. parts[name]["entry"] = Gtk.Entry()
  172. parts[name]["entry"].set_text(win.settings[name])
  173. parts[name]["entry"].set_size_request(300,40)
  174. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  175. elif type(win.settings[name]) == bool:
  176. parts[name]["entry"] = Gtk.Switch()
  177. parts[name]["entry"].set_active(win.settings[name])
  178. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  179. elif type(win.settings[name]) == float:
  180. parts[name]["number"] = Gtk.Adjustment(win.settings[name],
  181. lower=0.0001,
  182. upper=1000000000,
  183. step_increment=0.1)
  184. parts[name]["entry"] = Gtk.SpinButton(adjustment=parts[name]["number"],
  185. digits=4)
  186. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  187. elif type(win.settings[name]) == int:
  188. parts[name]["number"] = Gtk.Adjustment(win.settings[name],
  189. lower=0,
  190. upper=1000000000,
  191. step_increment=4)
  192. parts[name]["entry"] = Gtk.SpinButton(adjustment=parts[name]["number"])
  193. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  194. elif type(win.settings[name]) == list:
  195. parts[name]["box"] = Gtk.VBox()
  196. parts[name]["box"].pack_start(Gtk.Label(pname), False, False, False)
  197. parts[name]["box"].pack_start( ui.tags_editor(win, win.settings[name]), True, True, 0)
  198. box.pack_start(parts[name]["box"], False, False, 5)
  199. # notifications_box = Gtk.HBox()
  200. # notifications_setting = Gtk.Switch()
  201. # notifications_setting.set_active(win.settings["notifications"])
  202. # notifications_box.pack_start(Gtk.Label(" Notifications: "), False, False, False)
  203. # notifications_box.pack_end(notifications_setting, False, False, False)
  204. # box.pack_start(notifications_box, False, False, False)
  205. #######################################################################
  206. # #
  207. # RUNING THE SETTINGS DIALOG #
  208. # #
  209. #######################################################################
  210. # Running the dialog
  211. box.show_all()
  212. response = dialogWindow.run()
  213. if response == Gtk.ResponseType.OK:
  214. # Here I want to overwrite all the setting to ones
  215. # chosen by the user.
  216. win.settings["GTK_icon_theme"] = themes_setting.get_active_text()
  217. if default_tab.get_active_text() == "Suggestions":
  218. win.settings["default_tab"] = "suggest"
  219. else:
  220. win.settings["default_tab"] = "following"
  221. for name in win.settings:
  222. if name in exclude:
  223. continue
  224. value = win.settings[name]
  225. if type(win.settings[name]) == str:
  226. value = parts[name]["entry"].get_text()
  227. elif type(win.settings[name]) == bool:
  228. value = parts[name]["entry"].get_active()
  229. elif type(win.settings[name]) == int:
  230. value = int(parts[name]["entry"].get_value())
  231. elif type(win.settings[name]) == float:
  232. value = parts[name]["entry"].get_value()
  233. win.settings[name] = value
  234. # Then we save everything.
  235. save(win.settings)
  236. if not win.settings["lock_password"]:
  237. win.lock_button.set_sensitive(False)
  238. else:
  239. win.lock_button.set_sensitive(True)
  240. dialogWindow.destroy()