From ccaf08e2387525b61f51477327040f1bd8254f04 Mon Sep 17 00:00:00 2001 From: Tobias Date: Tue, 26 Dec 2023 15:29:27 +0100 Subject: [PATCH] Get and display location to the preference UI --- .../5.4/preferences/preferences.glade | 76 ++++++++++++------- .../5.4/preferences/preferences.py | 32 +++++++- .../5.4/preferences/scripts/location.py | 20 ++++- .../preferences/scripts/suntimes_refresh.py | 0 .../5.4/preferences/scripts/time_bar_chart.py | 4 +- 5 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/suntimes_refresh.py diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.glade b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.glade index 81c3849..77feffe 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.glade +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.glade @@ -1204,43 +1204,21 @@ - + True True - + True False 8 8 8 8 - True - - - True - False - start - Longitude - - - False - True - 0 - - - - - True - True - - - - False - True - 1 - - + label + + + @@ -1288,6 +1266,48 @@ + + + True + True + + + True + False + 8 + 8 + 8 + 8 + True + + + True + False + start + Longitude + + + False + True + 0 + + + + + True + True + + + + False + True + 1 + + + + + + True diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.py index a85822b..da2726a 100755 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/preferences.py @@ -15,7 +15,8 @@ from gi.repository import Gtk, GdkPixbuf # Global definitions -GLADE_URI = os.path.dirname(os.path.abspath(__file__)) + "/preferences.glade" +PREFERENCES_URI = os.path.dirname(os.path.abspath(__file__)) +GLADE_URI = PREFERENCES_URI + "/preferences.glade" @@ -93,6 +94,8 @@ class Preferences: ## Location & Times self.tb_network_location = self.builder.get_object("tb_network_location") + self.lb_current_location = self.builder.get_object("lb_current_location") + self.lbr_current_location = self.builder.get_object("lbr_current_location") self.tb_custom_location = self.builder.get_object("tb_custom_location") self.tb_time_periods = self.builder.get_object("tb_time_periods") self.lbr_network_location = self.builder.get_object("lbr_network_location") @@ -149,10 +152,10 @@ class Preferences: time_periods_min.append(time_range[0].hour * 60 + time_range[0].minute) # Create time bar - self.time_bar_chart.create_bar_chart(1200, 150, time_periods_min) + self.time_bar_chart.create_bar_chart(PREFERENCES_URI, 1200, 150, time_periods_min) # Load to the view - pixbuf = GdkPixbuf.Pixbuf.new_from_file("time_bar.svg") + pixbuf = GdkPixbuf.Pixbuf.new_from_file(PREFERENCES_URI + "/time_bar.svg") self.img_bar.set_from_pixbuf(pixbuf) @@ -210,10 +213,27 @@ class Preferences: self.tb_time_periods.set_active(False) self.lbr_network_location.set_visible(True) + self.lbr_current_location.set_visible(True) self.lbr_custom_location_longitude.set_visible(False) self.lbr_custom_location_latitude.set_visible(False) self.lbr_time_periods.set_visible(False) + # Start a thread to get the current location + locationThread = Location() + locationThread.start() + locationThread.join() + + location = locationThread.result + + # Display the location in the UI + self.lb_current_location.set_text("Latitude: " + str(location["latitude"]) + ", Longitude: " + \ + str(location["longitude"])) + + # Store the location to the preferences + self.settings_dict[PrefenceEnums.LATITUDE] = location["latitude"] + self.settings_dict[PrefenceEnums.LONGITUDE] = location["longitude"] + + def on_toggle_button_custom_location_clicked(self, button): if button.get_active(): self.settings_dict[PrefenceEnums.PERIOD_SOURCE] = PeriodSourceEnum.CUSTOMLOCATION @@ -221,10 +241,12 @@ class Preferences: self.tb_time_periods.set_active(False) self.lbr_network_location.set_visible(False) + self.lbr_current_location.set_visible(False) self.lbr_custom_location_longitude.set_visible(True) self.lbr_custom_location_latitude.set_visible(True) self.lbr_time_periods.set_visible(False) + def on_toggle_button_time_periods_clicked(self, button): if button.get_active(): self.settings_dict[PrefenceEnums.PERIOD_SOURCE] = PeriodSourceEnum.CUSTOMTIMEPERIODS @@ -232,16 +254,20 @@ class Preferences: self.tb_custom_location.set_active(False) self.lbr_network_location.set_visible(False) + self.lbr_current_location.set_visible(False) self.lbr_custom_location_longitude.set_visible(False) self.lbr_custom_location_latitude.set_visible(False) self.lbr_time_periods.set_visible(True) + def on_spb_network_location_refresh_time_changed(self, spin_button): self.settings_dict[PrefenceEnums.LOCATION_REFRESH_INTERVALS] = spin_button.get_value() + def on_etr_longitude_changed(self, entry): self.settings_dict[PrefenceEnums.LONGITUDE] = entry.get_text() + def on_etr_latitude_changed(self, entry): self.settings_dict[PrefenceEnums.LATITUDE] = entry.get_text() diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/location.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/location.py index 3938582..6ed34f9 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/location.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/location.py @@ -1,3 +1,17 @@ -def get_location_by_network() -> list: - #todo - return [] \ No newline at end of file +import urllib.request, json +from threading import Thread + +class Location(Thread): + def __init__(self): + Thread.__init__(self) + self.GEO_URL = "https://get.geojs.io/v1/ip/geo.json" + + def run(self) -> dict: + request = urllib.request.urlopen(self.GEO_URL) + + data = json.load(request) + + self.result = { + "latitude": data["latitude"], + "longitude": data["longitude"] + } diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/suntimes_refresh.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/suntimes_refresh.py new file mode 100644 index 0000000..e69de29 diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/time_bar_chart.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/time_bar_chart.py index 500005e..5e3edb8 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/time_bar_chart.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/preferences/scripts/time_bar_chart.py @@ -20,7 +20,7 @@ class Time_Bar_Chart: self.bar_pos_x = [] - def create_bar_chart(self, image_width: int, image_height: int, times: list): + def create_bar_chart(self, save_location: str, image_width: int, image_height: int, times: list): """ Create a time bar chart Args: @@ -36,7 +36,7 @@ class Time_Bar_Chart: self.image_code.insert(0, '' % (image_width, image_height)) self.image_code.append('') - file = open("time_bar.svg", "w") + file = open(save_location + "/time_bar.svg", "w") for i in self.image_code: file.write(i + '\n')