settings.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  60. # Let's write the Default theme.
  61. try:
  62. with open(get_settings_folder()+"config.json") as f:
  63. data = json.load(f)
  64. for i in defaults:
  65. if i not in data:
  66. data[i] = defaults[i]
  67. except Exception as e:
  68. data = defaults
  69. with open(get_settings_folder()+'config.json', 'w') as fp:
  70. json.dump(data, fp, sort_keys=True, indent=4)
  71. def dialogue(w, win):
  72. # This is the settings dialogue
  73. # Configuring the window
  74. dialogWindow = Gtk.Dialog("Settings",
  75. buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  76. Gtk.STOCK_OK, Gtk.ResponseType.OK),
  77. )
  78. box = dialogWindow.get_content_area()
  79. # Themes
  80. themes_setting = Gtk.ComboBoxText()
  81. # Themes are laoded from folder in ./icons
  82. select = 0
  83. icon_themes = os.listdir(os.getcwd()+"/icons")
  84. icon_themes.append("System Theme")
  85. for n, theme in enumerate(icon_themes):
  86. themes_setting.append_text( theme )
  87. if win.settings["GTK_icon_theme"] == theme:
  88. select = n
  89. themes_setting.set_active(select)
  90. theme_box = Gtk.HBox()
  91. theme_box.pack_start(Gtk.Label("Icon Theme (requires restart) : "), False, False, False)
  92. theme_box.pack_start(themes_setting, False, False, False)
  93. box.pack_start(theme_box, False, False, False)
  94. # Running the dialog
  95. box.show_all()
  96. response = dialogWindow.run()
  97. if response == Gtk.ResponseType.OK:
  98. # Here I want to overwrite all the setting to ones
  99. # chosen by the user.
  100. win.settings["GTK_icon_theme"] = themes_setting.get_active_text()
  101. # Then we save everything.
  102. save(win.settings)
  103. dialogWindow.destroy()