-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
93 lines (72 loc) · 2.96 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import ctypes
import sys
import os
import threading
import time
from PyQt6.QtWidgets import QApplication, QSystemTrayIcon, QMenu
from PyQt6.QtGui import QIcon, QAction
class SHQUERYRBINFO(ctypes.Structure):
_fields_ = [
("cbSize", ctypes.c_ulong),
("i64Size", ctypes.c_int64),
("i64NumItems", ctypes.c_int64)
]
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def load_icon(icon_path):
return QIcon(resource_path(icon_path))
def show_notification(title, message, icon_path=None):
tray_icon.showMessage(title, message, QIcon(resource_path(icon_path)), 5000)
def empty_recycle_bin():
SHEmptyRecycleBin = ctypes.windll.shell32.SHEmptyRecycleBinW
flags = 0x01 # SHERB_NOCONFIRMATION
bin_path = ctypes.create_unicode_buffer(260)
ctypes.windll.shell32.SHGetFolderPathW(0, 0x0005, 0, 0, bin_path)
result = SHEmptyRecycleBin(None, bin_path, flags)
if result == 0 or result == -2147418113:
show_notification("Корзина", "Корзина успешно очищена.", "icons/minibin-kt-empty.ico")
else:
show_notification("Корзина", f"Произошла ошибка при очистке корзины. Код ошибки: {result}", "icons/minibin-kt-full.ico")
update_icon()
def open_recycle_bin():
os.startfile("shell:RecycleBinFolder")
def exit_program():
QApplication.quit()
def update_icon():
if is_recycle_bin_empty():
tray_icon.setIcon(load_icon("icons/minibin-kt-empty.ico"))
else:
tray_icon.setIcon(load_icon("icons/minibin-kt-full.ico"))
def is_recycle_bin_empty():
rbinfo = SHQUERYRBINFO()
rbinfo.cbSize = ctypes.sizeof(SHQUERYRBINFO)
result = ctypes.windll.shell32.SHQueryRecycleBinW(None, ctypes.byref(rbinfo))
if result != 0:
print("Ошибка при запросе состояния корзины.")
return False
return rbinfo.i64NumItems == 0
def periodic_update():
while True:
update_icon()
time.sleep(3) # Интервал обновления в секундах
if __name__ == "__main__":
app = QApplication(sys.argv)
tray_icon = QSystemTrayIcon()
tray_icon.setIcon(load_icon("icons/minibin-kt-empty.ico"))
tray_menu = QMenu()
open_action = QAction("Открыть корзину", triggered=open_recycle_bin)
empty_action = QAction("Очистить корзину", triggered=empty_recycle_bin)
exit_action = QAction("Выход", triggered=exit_program)
tray_menu.addAction(open_action)
tray_menu.addAction(empty_action)
tray_menu.addAction(exit_action)
tray_icon.setContextMenu(tray_menu)
tray_icon.show()
# Запуск таймера в отдельном потоке
update_thread = threading.Thread(target=periodic_update, daemon=True)
update_thread.start()
sys.exit(app.exec())