def __init__(self, library): self.last_location = None self.library = library self.last_location = "/home/koe/Documents/Library/ebooks/" Gtk.Window.__init__(self, title="Book Organizer") self.connect("delete-event", self.close_window) self.set_default_size(1600, 900) headerBar = Gtk.HeaderBar() headerBar.props.show_close_button = True headerBar.props.title = "Ebola" self.set_titlebar(headerBar) toolbar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) Gtk.StyleContext.add_class(toolbar.get_style_context(), "linked") buttonFilter = Gtk.Button() icon = Gio.ThemedIcon(name="edit-find-symbolic") image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON) buttonFilter.add(image) headerBar.pack_end(buttonFilter) buttonImportDir = Gtk.Button() icon = Gio.ThemedIcon(name="insert-object-symbolic") image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON) buttonImportDir.add(image) buttonImportDir.connect("clicked", self.on_import_directory) toolbar.pack_start(buttonImportDir, False, False, 0) buttonAddFile = Gtk.Button() icon = Gio.ThemedIcon(name="list-add-symbolic") image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON) buttonAddFile.add(image) buttonAddFile.connect("clicked", self.on_add_file) toolbar.pack_start(buttonAddFile, False, False, 0) headerBar.pack_end(toolbar) mainBox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) self.filtered_list = FilteredList() self.filtered_list.connect("realize", self.realize) filterScrolled = Gtk.ScrolledWindow() filterScrolled.add_with_viewport(self.filtered_list) mainPaned = Gtk.Paned(orientation=Gtk.Orientation.HORIZONTAL) bookmarks = NavigationBar(library) mainPaned.add1(bookmarks) mainPaned.add2(filterScrolled) mainPaned.add2(self.filtered_list) mainPaned.props.position = 350 mainBox.pack_start(mainPaned, True, True, 0) self.add(mainBox)
class BookOrganizerWindow(Gtk.Window): def close_window(self, widget, event): Gtk.main_quit() self.library.export_to_file() def on_import_directory(self, widget): pass def add_filters(self, dialog): filterText = Gtk.FileFilter() filterText.set_name("eBook Files (PDF, DjVu)") filterText.add_mime_type("application/pdf") filterText.add_mime_type("image/vnd.djvu") dialog.add_filter(filterText) def on_file_link_clicked(self, widget, uri): if sys.platform.startswith('linux'): subprocess.call(["xdg-open", uri]) else: os.startfile(file) return True def on_add_file(self, widget): dialog = Gtk.FileChooserDialog("Add eBook to Library", self, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) self.add_filters(dialog) if self.last_location != None: dialog.set_filename("/home/koe/Documents/Library") response = dialog.run() fn = dialog.get_filename() dialog.destroy() if response == Gtk.ResponseType.OK: self.last_location = os.path.dirname(fn) try: b = self.library.add_local_book(fn) if b != None: self.filtered_list.add_book_item(b, self.library) except Exception as inst: if inst.args[0] == "ExtractionError": validInput = False isbn = [] while validInput == False: inputDialog = Gtk.MessageDialog(self, Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, "Insert ISBN") inputDialog.set_title("ISBN") inputDialog.format_secondary_markup("The ISBN automaitc extraction of a book failed.\n"+ "If you want to add the book <a href='" + fn + "'>" + os.path.basename(fn) + "</a> to the library, provide a valid ISBN number:") dialogBox = inputDialog.get_content_area() infoText = Gtk.Label() #infoText.set_markup() inputDialog.get_children()[0].get_children()[0].get_children()[1].get_children()[1].connect('activate-link', self.on_file_link_clicked) userInput = Gtk.Entry() userInput.set_size_request(100,0) dialogBox.pack_end(userInput, False, False, 0) #dialogBox.pack_end(infoText, False, False, 0) inputDialog.show_all() response = inputDialog.run() text = userInput.get_text() inputDialog.destroy() if response == Gtk.ResponseType.OK: validInput = True try: isbn = ISBN.from_string("".join(i for i in text if ord(i)<128)) b = self.library.add_local_book(fn, isbn=isbn) if b != None: self.filtered_list.add_book_item(b, self.library) except TypeError: validInput = False else: validInput = True else: raise inst errorDialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Importing the Book failed.") errorDialog.format_secondary_text("While importing the Book:\n'" + fn + "' the following error occured:\n" + "\n".join(map(str, inst.args))) errorDialog.run() errorDialog.destroy() def realize(self, widget): red = int(widget.get_style_context().get_background_color(Gtk.StateFlags.SELECTED).red * 255) green = int(widget.get_style_context().get_background_color(Gtk.StateFlags.SELECTED).green * 255) blue = int(widget.get_style_context().get_background_color(Gtk.StateFlags.SELECTED).blue * 255) newcss = "@define-color row_color shade(rgb("+str(red)+","+str(green)+","+str(blue)+"), 0.9);\n" + css red = int(widget.get_style_context().get_background_color(Gtk.StateFlags.PRELIGHT).red * 255) green = int(widget.get_style_context().get_background_color(Gtk.StateFlags.PRELIGHT).green * 255) blue = int(widget.get_style_context().get_background_color(Gtk.StateFlags.PRELIGHT).blue * 255) newcss = newcss + "@define-color focused_color rgb("+str(red)+","+str(green)+","+str(blue)+");\n" + css css_provider = Gtk.CssProvider() css_provider.load_from_data(bytearray(newcss, encoding="utf-8")) context = Gtk.StyleContext() context.add_provider_for_screen(Gdk.Screen.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) if self.library.default_db == "xml": for b in self.library.data: self.filtered_list.add_book_item(b, self.library) elif self.library.default_db == "sqlite": for b in self.library.get_books(): pass def __init__(self, library): self.last_location = None self.library = library self.last_location = "/home/koe/Documents/Library/ebooks/" Gtk.Window.__init__(self, title="Book Organizer") self.connect("delete-event", self.close_window) self.set_default_size(1600, 900) headerBar = Gtk.HeaderBar() headerBar.props.show_close_button = True headerBar.props.title = "Ebola" self.set_titlebar(headerBar) toolbar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) Gtk.StyleContext.add_class(toolbar.get_style_context(), "linked") buttonFilter = Gtk.Button() icon = Gio.ThemedIcon(name="edit-find-symbolic") image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON) buttonFilter.add(image) headerBar.pack_end(buttonFilter) buttonImportDir = Gtk.Button() icon = Gio.ThemedIcon(name="insert-object-symbolic") image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON) buttonImportDir.add(image) buttonImportDir.connect("clicked", self.on_import_directory) toolbar.pack_start(buttonImportDir, False, False, 0) buttonAddFile = Gtk.Button() icon = Gio.ThemedIcon(name="list-add-symbolic") image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON) buttonAddFile.add(image) buttonAddFile.connect("clicked", self.on_add_file) toolbar.pack_start(buttonAddFile, False, False, 0) headerBar.pack_end(toolbar) mainBox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) self.filtered_list = FilteredList() self.filtered_list.connect("realize", self.realize) filterScrolled = Gtk.ScrolledWindow() filterScrolled.add_with_viewport(self.filtered_list) mainPaned = Gtk.Paned(orientation=Gtk.Orientation.HORIZONTAL) bookmarks = NavigationBar(library) mainPaned.add1(bookmarks) mainPaned.add2(filterScrolled) mainPaned.add2(self.filtered_list) mainPaned.props.position = 350 mainBox.pack_start(mainPaned, True, True, 0) self.add(mainBox)