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

Logout #7

Merged
merged 4 commits into from
Feb 6, 2024
Merged
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
57 changes: 45 additions & 12 deletions src/neknihy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ def __init__(self, configfile, debug=False):
self.updateBookList()
self.syncButtonMonitor()

self.gettext_map = {
"pridano": [
"Do čtečky byla přidána %i kniha, ",
"Do čtečky byly přidány %i knihy, ",
"Do čtečky bylo přidáno %i knih, "
],
"odstraneno": [
"odstraněna byla %i kniha, ",
"odstraněny byly %i knihy, ",
"odstraněno bylo %i knih, "
],
"celkem": [
"ve čtečce je %i vypůjčená kniha.",
"ve čtečce jsou %i vypůjčené knihy.",
"ve čtečce je %i vypůjčených knih."
]
}

def _resourcesFolder(self):
for resources in [
os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources'),
Expand Down Expand Up @@ -74,14 +92,16 @@ def addTooltip(self, button, tip):
def createGUI(self):
resources = self._resourcesFolder()
self._window = tk.Tk()
self._window.option_add('*Dialog.msg.font', 'Helvetica 12')
self._window.title("Neknihy")
self._window.geometry("800x600")
self._window.minsize(600, 400)
self._icon = tk.PhotoImage(file=os.path.join(resources, 'neknihy.png'))
self._window.iconphoto(True, self._icon)
style = ttk.Style(self._window)
if sys.platform == "darwin":
style = ttk.Style(self._window)
style.theme_use('default')
style.map('TButton', background=[('disabled', '#BBBBBB')])
nb = ttk.Notebook(self._window)

p1 = ttk.Frame(nb)
Expand Down Expand Up @@ -111,28 +131,31 @@ def createGUI(self):
length=70)

self._toolbarButtons = []
button = ttk.Button(toolbar, image=self._img_refresh, command=self.onRefreshBooks)
button = ttk.Button(toolbar, image=self._img_refresh,
command=self.onRefreshBooks, style="TButton")
button.pack(side="left", padx=5, pady=5)
self.addTooltip(button, "Načíst nové výpůjčky")
self._toolbarButtons.append(button)

button = ttk.Button(toolbar, image=self._img_download, command=self.onDownloadBooks)
button = ttk.Button(toolbar, image=self._img_download,
command=self.onDownloadBooks, style="TButton")
button.pack(side="left", padx=5, pady=5)
self.addTooltip(button, "Stáhnout nově zapůjčené knihy")
self._toolbarButtons.append(button)

button = ttk.Button(toolbar, image=self._img_return, command=self.onReturnBooks)
button = ttk.Button(toolbar, image=self._img_return,
command=self.onReturnBooks, style="TButton")
button.pack(side="left", padx=5, pady=5)
self.addTooltip(button, "Smazat knihy, u kterých\nvypršela výpůjční doba")
self._toolbarButtons.append(button)

self._sync_button = ttk.Button(toolbar,
image=self._img_to_reader,
command=self.onSyncReader)
self._sync_button = ttk.Button(toolbar, image=self._img_to_reader,
command=self.onSyncReader, style="TButton")
self._sync_button.pack(side="left", padx=5, pady=5)
self.addTooltip(self._sync_button, "Synchronizovat výpůjčky s čtečkou")

button = ttk.Button(toolbar, image=self._img_open, command=self.onShowBooks)
button = ttk.Button(toolbar, image=self._img_open,
command=self.onShowBooks, style="TButton")
button.pack(side="left", padx=5, pady=5)
self.addTooltip(button, "Otevřít složku s knihami")

Expand Down Expand Up @@ -319,6 +342,9 @@ def onRefreshBooks(self):
self.backgroundTaskMonitor()

def downloadBooks(self):
self.refreshBooks()
if self._error is not None:
return
try:
self.app.downloadBooks()
self._error = None
Expand All @@ -336,17 +362,24 @@ def onReturnBooks(self):
self.app.returnBooks()
self.updateBookList()

def gettext(self, sentence, amount):
idx = 0
if amount > 1:
idx = 1
if amount >= 5 or amount == 0:
idx = 2
return self.gettext_map[sentence][idx] % (amount)

def syncReader(self):
try:
self._message = None
self._error = None
result = self.app.syncReader()
if result is not None:
self._message = (
"Přidáno/odstraněno/zůstává ve čtečce: %i/%i/%i" % (
len(result["added"]),
len(result["removed"]),
result["total"])
self.gettext("pridano", len(result["added"])) +
self.gettext("odstraneno", len(result["removed"])) +
self.gettext("celkem", result["total"])
)
except Exception as e:
self._error = str(e)
Expand Down
10 changes: 8 additions & 2 deletions src/neknihy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ def downloadBooks(self):
if not self.settings.configured():
return
self.api.login(self.settings.email, self.settings.password)
exc = None
for i in range(len(self.books)):
if not self.bookDownloaded(i):
self.downloadBook(i)
try:
if not self.bookDownloaded(i):
self.downloadBook(i)
except Exception as e:
exc = e
self.saveBooks()
self.updateStatus()
if exc is not None:
raise exc

def returnBooks(self):
books = []
Expand Down
Loading