Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a feature which randomizes the wallpaper based on a user defined interval by creating a script for the user #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 120 additions & 2 deletions waypaper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
from pathlib import Path
from PIL import Image
import subprocess

from waypaper.aboutdata import AboutData
from waypaper.changer import change_wallpaper
Expand Down Expand Up @@ -63,15 +64,21 @@ def __init__(self, txt: Chinese|English|French|German|Polish|Russian|Spanish) ->
self.selected_index = 0
self.highlighted_image_row = 0
self.init_ui()
self.random_script_is_running = self.check_script("waypaper_random.sh")

# Start the image processing in a separate thread:
threading.Thread(target=self.process_images).start()

def check_script(self, script_name: str) -> bool:
"""Check if a given script is running"""
result = subprocess.run(['pgrep', '-f', script_name], capture_output=True, text=True)
return bool(result.stdout.strip())

def init_ui(self) -> None:
"""Initialize the UI elements of the application"""

# Create a vertical box for layout:
self.main_box = Gtk.VBox(spacing=10)
self.main_box = Gtk.VBox(spacing=50)
self.add(self.main_box)

# Create a button to open folder dialog:
Expand Down Expand Up @@ -149,6 +156,26 @@ def init_ui(self) -> None:
self.random_button.connect("clicked", self.on_random_clicked)
self.random_button.set_tooltip_text(self.txt.tip_random)

#Create a button that makes a bash script to randmize the images every x minutes
self.random_script_button = Gtk.Button(label=self.txt.msg_random_script)
self.random_script_button.connect("clicked", self.on_random_script_clicked)
self.random_script_button.set_tooltip_text(self.txt.tip_random_script)

#make a drop down menu for random script options
self.random_script_menu = Gtk.Menu()

#create a menu item for the random script to be ran every x minutes
self.start_script_menu_item = Gtk.MenuItem(label=self.txt.msg_random_script_change_interval)
self.start_script_menu_item.connect("activate",self.on_random_script_activate)
self.random_script_menu.append(self.start_script_menu_item)

#stop the menu
self.stop_script_menu_item = Gtk.MenuItem(label=self.txt.msg_random_script_stop)
self.stop_script_menu_item.connect("activate", self.on_stop_random_script_activate)
self.random_script_menu.append(self.stop_script_menu_item)

self.random_script_menu.show_all()

# Create a box to contain the bottom row of buttons with margin:
self.bottom_button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=20)
self.bottom_button_box.set_margin_bottom(10)
Expand All @@ -175,6 +202,8 @@ def init_ui(self) -> None:
self.options_box.pack_end(self.exit_button, False, False, 0)
self.options_box.pack_end(self.options_button, False, False, 0)
self.options_box.pack_end(self.refresh_button, False, False, 0)
self.options_box.pack_end(self.random_script_button, False, False, 0)
self.options_box.pack_end(self.random_script_menu, False, False, 0)
self.options_box.pack_end(self.random_button, False, False, 0)
self.options_box.pack_end(self.sort_option_combo, False, False, 0)
self.options_box.pack_start(self.backend_option_combo, False, False, 0)
Expand Down Expand Up @@ -482,6 +511,96 @@ def on_random_clicked(self, widget) -> None:
"""On clicking random button, set random wallpaper"""
self.set_random_wallpaper()

def on_random_script_clicked(self, widget) -> None:
"""On clicking random script button, create/execute a bash script to randmize the images every x minutes"""
# self.set_random_script()
self.random_script_menu.popup_at_widget(widget, Gdk.Gravity.NORTH, Gdk.Gravity.SOUTH, None)

def on_random_script_activate(self, widget) -> None:
"""Start the random script with the specified interval"""
self.show_interval_input_dialog()

def on_stop_random_script_activate(self, widget) -> None:
"""Stop the random script"""
self.stop_random_script()

def show_interval_input_dialog(self):
dialog = Gtk.Dialog(title="Set Interval", transient_for=self, flags=0)
#I do not know if these will get translated to other languages
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK, Gtk.ResponseType.OK)

box = dialog.get_content_area()
box.set_spacing(6)
box.set_margin_top(10)
box.set_margin_bottom(10)
box.set_margin_start(10)
box.set_margin_end(10)

label = Gtk.Label(label=self.txt.msg_random_script_interval_seconds_label)
self.interval_input_entry = Gtk.Entry()
self.interval_input_entry.set_text("600")

box.pack_start(label, False, False, 0)
box.pack_start(self.interval_input_entry, False, False, 0)

dialog.show_all()
response = dialog.run()

if response == Gtk.ResponseType.OK:
interval = int(self.interval_input_entry.get_text())
self.set_random_script(interval)

dialog.destroy()

def set_random_script(self, interval: int) -> None:
"""Create/execute a bash script to randomize the images every x minutes"""
print("Creating or executing the script")
script_path = Path(self.cf.image_folder) / "waypaper_random.sh"
print(f"Interval is: {interval}")

script_content = f"""#!/bin/bash
while true; do
sleep {interval}
waypaper --random
done
"""
#check if the script is already running to prevent crazy bugs
if self.random_script_is_running:
self.show_message_dialog("Random script already running! Please stop the script before setting a new interval.")
return

#delete the existing script if it exists because then the interval will not be updated
if script_path.exists():
script_path.unlink()

#create a new script with the updated interval and make it executable
with open(script_path, "w") as script_file:
script_file.write(script_content)
os.chmod(script_path, 0o755)

os.system(f"nohup {script_path} &")
self.random_script_is_running = True

def show_message_dialog(self, message: str) -> None:
"""Show a message dialog to the user"""
dialog = Gtk.MessageDialog(
parent=self,
flags=0,
type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
message_format=message,
)
dialog.run()
dialog.destroy()

def stop_random_script(self) -> None:
"""Stop the random script"""
result = subprocess.run(['pkill', '-f', "waypaper_random.sh"], capture_output=True, text=True)
if result.returncode == 0:
self.random_script_is_running = False
print("Random script stopped.")
else:
print("No random script running.")

def on_exit_clicked(self, widget) -> None:
"""On clicking exit button, exit"""
Expand All @@ -499,7 +618,6 @@ def set_random_wallpaper(self) -> None:
change_wallpaper(self.cf.selected_wallpaper, self.cf, self.cf.selected_monitor, self.txt)
self.cf.save()


def clear_cache(self) -> None:
"""Delete cache folder and reprocess the images"""
try:
Expand Down
5 changes: 5 additions & 0 deletions waypaper/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def __init__(self):
self.msg_select = "Select"
self.msg_refresh = "Refresh"
self.msg_random = "Random"
self.msg_random_script = "Randomize Script"
self.msg_random_script_change_interval = "Change the wallpaper interval"
self.msg_random_script_stop = "Stop the random wallpaper script"
self.msg_random_script_interval_seconds_label = "Interval (seconds):"
self.msg_exit = "Exit"
self.msg_subfolders = "Show subfolders"
self.msg_hidden = "Show hidden"
Expand Down Expand Up @@ -49,6 +53,7 @@ def __init__(self):
self.tip_display = "Choose display"
self.tip_color = "Choose background color"
self.tip_random = "Set random wallpaper"
self.tip_random_script = "Create/execute a script to randomize the images every x minutes"
self.tip_exit = "Exit the application"


Expand Down