def __on_file_choose(self, *args): """ User choose a file with FileChooser """ status = False name = self.file_selector.get_filename() if name is not None and len(name.strip()) > 0: path = Path(name.strip()).expanduser() try: mimetype = magic_from_file(path, mime=True) # Check mimetype format if mimetype is not None and '/' in mimetype: category, *filetype = mimetype.split('/') # Only retrieve text files if category == "text" and path.exists(): status = True # Unselect this file cause is not a text one else: self.file_selector.unselect_all() except Exception as error: self.logger.exception(error) self.set_response_sensitive(Gtk.ResponseType.APPLY, status)
def set_icon(self, widget, path, size=64): """ Set thumbnail icon from a specific path Parameters ---------- widget : Gtk.Widget Icon widget to update path : str Icon path size : int, optional Icon size in pixels (Default: 64) """ # Retrieve an empty icon icon = self.icons.blank(size) if path is not None: # Check icon from icons theme if not path.exists(): # Retrieve icon from collection if self.icons.theme.has_icon(str(path)): icon = self.icons.theme.load_icon( str(path), size, Gtk.IconLookupFlags.FORCE_SIZE) # Retrieve icon from file elif path.is_file(): # Check the file mime-type to avoid non-image file if magic_from_file(path, mime=True).startswith("image/"): icon = GdkPixbuf.Pixbuf.new_from_file_at_scale( str(path), size, size, True) widget.set_from_pixbuf(icon)
def set_icon(self, widget, path, size=64): """ Set thumbnail icon from a specific path Parameters ---------- widget : Gtk.Widget Icon widget to update path : pathlib.Path Icon path size : int, optional Icon size in pixels (Default: 64) """ # Retrieve an empty icon icon = self.icons.blank(size) if path is not None: # Check icon from icons theme if not path.exists(): collection_path = self.api.get_local("icons", f"{path}.png") # Retrieve icon from collection if collection_path.exists() and collection_path.is_file(): # Check the file mime-type to avoid non-image file if magic_from_file(collection_path, mime=True).startswith("image/"): icon = GdkPixbuf.Pixbuf.new_from_file_at_scale( str(collection_path), size, size, True) # Retrieve icon from file elif path.is_file(): # Check the file mime-type to avoid non-image file if magic_from_file(path, mime=True).startswith("image/"): icon = GdkPixbuf.Pixbuf.new_from_file_at_scale( str(path), size, size, True) widget.set_from_pixbuf(icon)
def __on_dnd_received_data(self, widget, context, x, y, data, info, time): """ Manage drag & drop acquisition Parameters ---------- widget : Gtk.Widget Object which receive signal context : Gdk.DragContext Drag context x : int X coordinate where the drop happened y : int Y coordinate where the drop happened data : Gtk.SelectionData Received data info : int Info that has been registered with the target in the Gtk.TargetList time : int Timestamp at which the data was received """ self.text_editor.handler_block(self.__drop_signal) # Current acquisition not respect text/uri-list if not info == 1337: return files = data.get_uris() if len(files) > 0: result = urlparse(files[0]) if result.scheme == "file": path = Path(url2pathname(result.path)).expanduser() try: mimetype = magic_from_file(path, mime=True) # Check mimetype format if mimetype is not None and '/' in mimetype: category, *filetype = mimetype.split('/') # Only retrieve text files if category == "text" and path.exists(): self.__on_import_file(None, path) except Exception as error: self.logger.exception(error) self.text_editor.handler_unblock(self.__drop_signal)
def append_icons(self, size): """ Append icons in icons view with a specific size Parameters ---------- size : int Specified icon size """ yield True # Retrieve files from icons collection collection_path = self.api.get_local("icons") for path in sorted(collection_path.glob("*.png")): # Retrieve an empty icon icon = self.icons.blank(size) # Generate an icon for found file if path.exists() and path.is_file(): # Check the file mime-type to avoid non-image file if magic_from_file(path, mime=True).startswith("image/"): icon = GdkPixbuf.Pixbuf.new_from_file_at_scale( str(path), size, size, True) self.icons_data[path.stem] = \ self.model_icons.append([icon, path.stem]) # Current icon match the choosen one if str(self.path) == path.stem: # Only select current icon if no selection is available if len(self.view_icons.get_selected_items()) == 0: self.view_icons.select_path( self.model_icons.get_path(self.icons_data[path.stem])) yield True # Remove thread id from memory self.thread = int() yield False
def init_configuration(gem): """ Initialize user configuration Parameters ---------- gem : gem.engine.api.GEM GEM API instance """ move_collection = False # ---------------------------------------- # Configuration # ---------------------------------------- for filename in ("gem.conf", "consoles.conf", "emulators.conf"): path = gem.get_config(filename) if not path.exists(): gem.logger.debug(f"Copy default {path}") # Copy default configuration copy(get_data("data", "config", filename), path) # ---------------------------------------- # Local # ---------------------------------------- for folder in ("logs", "notes"): path = gem.get_local(folder) if not path.exists(): gem.logger.debug(f"Generate {path} folder") try: path.mkdir(mode=0o755, parents=True) except FileExistsError: gem.logger.error(f"Path {path} already exists") # ---------------------------------------- # Cache # ---------------------------------------- for name in ("consoles", "emulators", "games"): sizes = getattr(Icons.Size, name.upper(), list()) for size in sizes: path = Folders.CACHE.joinpath(name, f"{size}x{size}") if not path.exists(): gem.logger.debug(f"Generate {path}") try: path.mkdir(mode=0o755, parents=True) except FileExistsError: gem.logger.error(f"Path {path} already exists") # ---------------------------------------- # Icons # ---------------------------------------- icons_path = gem.get_local("icons") # Create icons storage folder if not icons_path.exists(): gem.logger.debug(f"Generate {icons_path}") try: icons_path.mkdir(mode=0o755, parents=True) except FileExistsError: gem.logger.error(f"Path {icons_path} already exists") finally: move_collection = True # Remove older icons collections folders (GEM < 1.0) else: for folder in ("consoles", "emulators"): path = icons_path.joinpath(folder) if path.exists(): if path.is_dir(): rmtree(path) elif path.is_symlink(): path.unlink() move_collection = True # Copy default icons if move_collection: gem.logger.debug("Generate consoles icons folder") for filename in get_data("data", "icons").glob("*.png"): if filename.is_file(): # Check the file mime-type to avoid non-image file mime = magic_from_file(filename, mime=True) if mime.startswith("image/"): new_path = icons_path.joinpath(filename.name) if not new_path.exists(): gem.logger.debug(f"Copy {new_path}") copy(filename, new_path)