settings.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. def get_settings_folder(flbry="flbry/"):
  34. try:
  35. data_dir = os.environ["XDG_DATA_HOME"] + "/" + flbry # Reducted back since it
  36. # broke too much and made
  37. # my settings appear in
  38. # $XDG_DATA_HOME folder
  39. # inside the repository.
  40. # WTF !!!
  41. except:
  42. data_dir = os.path.expanduser("~/.local/share/"+flbry)
  43. try:
  44. os.makedirs(data_dir)
  45. except:
  46. pass
  47. return data_dir
  48. def load():
  49. with open(get_settings_folder()+'config.json') as json_file:
  50. data = json.load(json_file)
  51. return data
  52. def save(data):
  53. with open(get_settings_folder()+'config.json', 'w') as fp:
  54. json.dump(data, fp, sort_keys=True, indent=4)
  55. def make_sure_file_exists():
  56. # Let's make some defaults
  57. defaults = {
  58. "GTK_icon_theme":"System Theme",
  59. "notifications":True,
  60. "auth_token": "",
  61. "autoconnect": False,
  62. "comment_api": "https://comments.odysee.com/api/v2",
  63. "lbrynet_binary": "flbry/lbrynet",
  64. "librarian_instance": "https://librarian.bcow.xyz/",
  65. "promote_fast_lbry_in_commnets":True
  66. }
  67. # Let's write the Default theme.
  68. try:
  69. with open(get_settings_folder()+"config.json") as f:
  70. data = json.load(f)
  71. for i in defaults:
  72. if i not in data:
  73. data[i] = defaults[i]
  74. except Exception as e:
  75. data = defaults
  76. with open(get_settings_folder()+'config.json', 'w') as fp:
  77. json.dump(data, fp, sort_keys=True, indent=4)
  78. def dialogue(w, win):
  79. # This is the settings dialogue
  80. # Configuring the window
  81. dialogWindow = Gtk.Dialog("Settings",
  82. buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  83. Gtk.STOCK_OK, Gtk.ResponseType.OK),
  84. )
  85. box = dialogWindow.get_content_area()
  86. #######################################################################
  87. # #
  88. # THEMES SELECTOR #
  89. # #
  90. #######################################################################
  91. themes_setting = Gtk.ComboBoxText()
  92. #themes_setting.set_relief(Gtk.ReliefStyle.NONE)
  93. # Themes are laoded from folder in ./icons
  94. select = 0
  95. icon_themes = os.listdir(os.getcwd()+"/icons")
  96. icon_themes.append("System Theme")
  97. for n, theme in enumerate(icon_themes):
  98. themes_setting.append_text( theme )
  99. if win.settings["GTK_icon_theme"] == theme:
  100. select = n
  101. themes_setting.set_active(select)
  102. theme_box = Gtk.HBox()
  103. theme_box.pack_start(Gtk.Label(" Icon Theme (requires restart) : "), False, False, False)
  104. theme_box.pack_end(themes_setting, False, False, False)
  105. box.pack_start(theme_box, False, False, 5)
  106. #######################################################################
  107. # #
  108. # ALL THE REST #
  109. # #
  110. #######################################################################
  111. # The settings are SHARED WITH FASTLBRY TERMINAL
  112. # The rest of settings are generated by what's available in the config
  113. # file. So there could be problems. First we need exclude list.
  114. exclude = ["GTK_icon_theme", # We have a separate thing for it
  115. "save_history", # Used by FastLBRY terminal, not interesting here
  116. "theme", # The theme of FastLBRY terminal
  117. "markdown_reader",# In FastLBRY GTK we have a UI implementation for this
  118. "graph_force_ASCII", # Another FastLBRY Termianl only function
  119. "ignore_width_forcing", # FASTLBRY TERMINAL AGAIN
  120. "channel" # This one is set in the top pannel of the window
  121. ]
  122. parts = {}
  123. for name in win.settings:
  124. if name in exclude:
  125. continue
  126. parts[name] = {}
  127. parts[name]["box"] = Gtk.HBox()
  128. # I want to beutify the name for the setting
  129. pname = name.replace("_", " ")
  130. pname = " "+pname[0].upper()+pname[1:].lower()+" "
  131. parts[name]["box"].pack_start(Gtk.Label(pname), False, False, False)
  132. # Then depending on the type of the data itself, we are going to give
  133. # inputs
  134. if type(win.settings[name]) == str:
  135. parts[name]["entry"] = Gtk.Entry()
  136. parts[name]["entry"].set_text(win.settings[name])
  137. parts[name]["entry"].set_size_request(300,40)
  138. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  139. elif type(win.settings[name]) == bool:
  140. parts[name]["entry"] = Gtk.Switch()
  141. parts[name]["entry"].set_active(win.settings[name])
  142. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  143. elif type(win.settings[name]) == float:
  144. parts[name]["number"] = Gtk.Adjustment(win.settings[name],
  145. lower=0.0001,
  146. upper=1000000000,
  147. step_increment=0.1)
  148. parts[name]["entry"] = Gtk.SpinButton(adjustment=parts[name]["number"],
  149. digits=4)
  150. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  151. elif type(win.settings[name]) == int:
  152. parts[name]["number"] = Gtk.Adjustment(win.settings[name],
  153. lower=0,
  154. upper=1000000000,
  155. step_increment=4)
  156. parts[name]["entry"] = Gtk.SpinButton(adjustment=parts[name]["number"])
  157. parts[name]["box"].pack_end(parts[name]["entry"], False, False, False)
  158. box.pack_start(parts[name]["box"], False, False, 5)
  159. # notifications_box = Gtk.HBox()
  160. # notifications_setting = Gtk.Switch()
  161. # notifications_setting.set_active(win.settings["notifications"])
  162. # notifications_box.pack_start(Gtk.Label(" Notifications: "), False, False, False)
  163. # notifications_box.pack_end(notifications_setting, False, False, False)
  164. # box.pack_start(notifications_box, False, False, False)
  165. #######################################################################
  166. # #
  167. # RUNING THE SETTINGS DIALOG #
  168. # #
  169. #######################################################################
  170. # Running the dialog
  171. box.show_all()
  172. response = dialogWindow.run()
  173. if response == Gtk.ResponseType.OK:
  174. # Here I want to overwrite all the setting to ones
  175. # chosen by the user.
  176. win.settings["GTK_icon_theme"] = themes_setting.get_active_text()
  177. for name in win.settings:
  178. if name in exclude:
  179. continue
  180. value = False
  181. if type(win.settings[name]) == str:
  182. value = parts[name]["entry"].get_text()
  183. elif type(win.settings[name]) == bool:
  184. value = parts[name]["entry"].get_active()
  185. elif type(win.settings[name]) == int:
  186. value = int(parts[name]["entry"].get_value())
  187. elif type(win.settings[name]) == float:
  188. value = parts[name]["entry"].get_value()
  189. win.settings[name] = value
  190. # Then we save everything.
  191. save(win.settings)
  192. dialogWindow.destroy()