-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrefsWindow.py
252 lines (215 loc) · 9.77 KB
/
PrefsWindow.py
1
"""Macstodon - a Mastodon client for classic Mac OSMIT LicenseCopyright (c) 2022-2024 Scott Small and ContributorsPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associateddocumentation files (the "Software"), to deal in the Software without restriction, including without limitation therights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permitpersons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of theSoftware.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THEWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS ORCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OROTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."""# ############### Python Imports # ##############import EasyDialogsimport macostoolsimport shutilimport stringimport W# ########### My Imports# ##########from MacstodonConstants import VERSIONfrom MacstodonHelpers import buildTimelinePicker, dprint, okDialog# ############ PrefsWindow# ###########class PrefsWindow(W.ModalDialog): def __init__(self): """ Initializes the PrefsWindow class. """ W.ModalDialog.__init__(self, (200, 300), "Macstodon %s - Preferences" % VERSION) self.setupwidgets() # ######################### # Window Handling Functions # ######################### def setupwidgets(self): """ Defines the prefs window """ app = self.parent prefs = app.getprefs() # debugging dprint("prefs.toots_to_load_startup: %s" % prefs.toots_to_load_startup) dprint("prefs.toots_to_load_refresh: %s" % prefs.toots_to_load_refresh) dprint("prefs.toots_per_timeline: %s" % prefs.toots_per_timeline) dprint("prefs.show_avatars: %s" % prefs.show_avatars) dprint("prefs.show_banners: %s" % prefs.show_banners) dprint("prefs.timelinecol1: %s" % prefs.timelinecol1) dprint("prefs.timelinecol2: %s" % prefs.timelinecol2) dprint("prefs.timelinecol3: %s" % prefs.timelinecol3) # Toots to load at startup self.toots_to_load_startup_text = "Toots to load at startup:\r(0 for none)" self.toots_to_load_startup_label = W.TextBox((10, 10, 160, 32), self.toots_to_load_startup_text) self.toots_to_load_startup_field = W.EditText((170, 10, 24, 16), prefs.toots_to_load_startup) # Toots to load when pressing Refresh in a timeline self.toots_to_load_refresh_text = "Toots to load on refresh:\r(0 for unlimited)" self.toots_to_load_refresh_label = W.TextBox((10, 40, 160, 32), self.toots_to_load_refresh_text) self.toots_to_load_refresh_field = W.EditText((170, 40, 24, 16), prefs.toots_to_load_refresh) # Maximum amount of toots to keep in a timeline self.toots_per_timeline_text = "Max toots per timeline:" self.toots_per_timeline_label = W.TextBox((10, 70, 160, 32), self.toots_per_timeline_text) self.toots_per_timeline_field = W.EditText((170, 70, 24, 16), prefs.toots_per_timeline) # Timeline selection for each column picker_menu = buildTimelinePicker(self.parent) self.timelinecol1 = prefs.timelinecol1 self.timelinecol2 = prefs.timelinecol2 self.timelinecol3 = prefs.timelinecol3 try: int(self.timelinecol1) for list in app.lists: if list["id"] == self.timelinecol1: timelinecol1_text = "Column 1: %s" % list["title"] except ValueError: timelinecol1_text = "Column 1: %s" % string.capitalize(self.timelinecol1) try: int(self.timelinecol2) for list in app.lists: if list["id"] == self.timelinecol2: timelinecol2_text = "Column 2: %s" % list["title"] except ValueError: timelinecol2_text = "Column 2: %s" % string.capitalize(self.timelinecol2) try: int(self.timelinecol3) for list in app.lists: if list["id"] == self.timelinecol3: timelinecol3_text = "Column 3: %s" % list["title"] except ValueError: timelinecol3_text = "Column 3: %s" % string.capitalize(self.timelinecol3) self.timelinecol1_label = W.TextBox((10, 100, 160, 32), timelinecol1_text) self.timelinecol1_picker = W.PopupWidget((170, 100, 16, 16), picker_menu, self.timelinePicker1Callback) self.timelinecol2_label = W.TextBox((10, 130, 160, 32), timelinecol2_text) self.timelinecol2_picker = W.PopupWidget((170, 130, 16, 16), picker_menu, self.timelinePicker2Callback) self.timelinecol3_label = W.TextBox((10, 160, 160, 32), timelinecol3_text) self.timelinecol3_picker = W.PopupWidget((170, 160, 16, 16), picker_menu, self.timelinePicker3Callback) # Show/hide images, and clear image cache self.show_avatars_box = W.CheckBox((10, 190, 160, 16), "Show Avatars", None, prefs.show_avatars) self.show_banners_box = W.CheckBox((10, 210, 160, 16), "Show Banners", self.showBannersCallback, prefs.show_banners) self.clear_cache_btn = W.Button((10, 240, 120, 16), "Clear Image Cache", self.clearImageCacheCallback) # Save/cancel changes self.cancel_btn = W.Button((10, -22, 60, 16), "Cancel", self.close) self.save_btn = W.Button((-70, -22, 60, 16), "Save", self.saveButtonCallback) self.setdefaultbutton(self.save_btn) # ################## # Callback Functions # ################## def clearImageCacheCallback(self): """ Run when the user clicks the "Clear Image Cache" button """ app = self.parent shutil.rmtree(app.cachefolderpath, ignore_errors=1) macostools.mkdirs(app.cachefolderpath) okDialog("Image cache cleared.") def showBannersCallback(self): """ Run when the user clicks the "Show Banners" checkbox """ if self.show_banners_box.get(): okDialog( "WARNING - while this makes profiles look pretty, it also has a memory leak affecting uncached images that will cause the app to crash eventually." ) def saveButtonCallback(self): """ Run when the user clicks the "Save" button """ app = self.parent prefs = app.getprefs() prefs.toots_to_load_startup = self.toots_to_load_startup_field.get() prefs.toots_to_load_refresh = self.toots_to_load_refresh_field.get() prefs.toots_per_timeline = self.toots_per_timeline_field.get() prefs.show_avatars = self.show_avatars_box.get() prefs.show_banners = self.show_banners_box.get() prefs.timelinecol1 = self.timelinecol1 prefs.timelinecol2 = self.timelinecol2 prefs.timelinecol3 = self.timelinecol3 prefs.save() self.close() def timelinePicker1Callback(self, item): """ Run when the user selects a timeline for column 1 """ if item == "__hashtag": hashtag = EasyDialogs.AskString( "Please type the name of the hashtag to build a timeline for." ) if hashtag is not None: if hashtag[0] == "#": item = hashtag else: item = "#%s" % hashtag else: return app = self.parent self.timelinecol1 = item try: int(item) for list in app.lists: if list["id"] == item: self.timelinecol1_label.set("Column 1: %s" % list["title"]) except ValueError: self.timelinecol1_label.set("Column 1: %s" % string.capitalize(item)) def timelinePicker2Callback(self, item): """ Run when the user selects a timeline for column 2 """ if item == "__hashtag": hashtag = EasyDialogs.AskString( "Please type the name of the hashtag to build a timeline for." ) if hashtag is not None: if hashtag[0] == "#": item = hashtag else: item = "#%s" % hashtag else: return app = self.parent self.timelinecol2 = item try: int(item) for list in app.lists: if list["id"] == item: self.timelinecol2_label.set("Column 2: %s" % list["title"]) except ValueError: self.timelinecol2_label.set("Column 2: %s" % string.capitalize(item)) def timelinePicker3Callback(self, item): """ Run when the user selects a timeline for column 3 """ if item == "__hashtag": hashtag = EasyDialogs.AskString( "Please type the name of the hashtag to build a timeline for." ) if hashtag is not None: if hashtag[0] == "#": item = hashtag else: item = "#%s" % hashtag else: return app = self.parent self.timelinecol3 = item try: int(item) for list in app.lists: if list["id"] == item: self.timelinecol3_label.set("Column 3: %s" % list["title"]) except ValueError: self.timelinecol3_label.set("Column 3: %s" % string.capitalize(item))