def __download_update(self, game: Game = None) -> None: Config.set("current_download", game.id) GLib.idle_add(self.__update_to_state, game.state.UPDATE_QUEUED, game) download_info = self.api.get_download_info(game) # Start the download for all files game.downloads = [] download_path = game.update_path finish_func = self.__update for key, file_info in enumerate(download_info['files']): if key > 0: download_path = "{}-{}.bin".format(self.update_path, key) download = Download(url=self.api.get_real_download_link( file_info["downlink"]), save_location=download_path, finish_func=finish_func, finish_func_args=game, progress_func=self.set_progress, progress_func_args=game, cancel_func=self.__cancel_update, cancel_func_args=game, number=key + 1, out_of_amount=len(download_info['files'])) game.downloads.append(download) DownloadManager.download(game.downloads)
def get_user_info(self, finish_fn=None) -> str: username = Config.get("username") userid = Config.get("user_id") avatar = None if userid is not None: user_dir = os.path.join(CACHE_DIR, "user/{}".format(userid)) if os.path.exists(user_dir) == False: os.makedirs(user_dir) avatar = os.path.join(user_dir, "avatar_menu_user_av_small.jpg") if not (os.path.isfile(avatar) and os.path.exists(avatar)): avatar = None if (avatar is not None and finish_fn is not None): finish_fn() if not username or not avatar: url = "https://embed.gog.com/userData.json" response = self.__request(url) username = response["username"] userid = response["userId"] Config.set("user_id", userid) Config.set("username", username) user_dir = os.path.join(CACHE_DIR, "user/{}".format(userid)) if os.path.exists(user_dir) == False: os.makedirs(user_dir) avatar = os.path.join(user_dir, "avatar_menu_user_av_small.jpg") download = Download(response["avatar"] + "_menu_user_av_small.jpg", avatar) download.register_finish_function(finish_fn) DownloadManager.download_now(download) return username
def save_pressed(self, button): self.__save_language_choice() Config.set("keep_installers", self.switch_keep_installers.get_active()) Config.set("stay_logged_in", self.switch_stay_logged_in.get_active()) Config.set("show_fps", self.switch_show_fps.get_active()) Config.set("create_shortcuts", self.switch_create_shortcuts.get_active()) Config.set("install_dlcs", self.switch_install_dlcs.get_active()) Config.set("automatic_updates", self.switch_automatic_updates.get_active()) Config.set("do_not_show_backgrounds",self.switch_do_not_show_backgrounds.get_active()) Config.set("do_not_show_media_tab",self.switch_do_not_show_media_tab.get_active()) if self.switch_show_windows_games.get_active() != Config.get("show_windows_games"): Config.set("show_windows_games", self.switch_show_windows_games.get_active()) self.parent.reset_library() # Only change the install_dir is it was actually changed if self.button_file_chooser.get_filename() != Config.get("install_dir"): if self.__save_install_dir_choice(): DownloadManager.cancel_all_downloads() self.parent.reset_library() else: dialog = Gtk.MessageDialog( parent=self, modal=True, destroy_with_parent=True, message_type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK, text=_("{} isn't a usable path").format(self.button_file_chooser.get_filename()) ) dialog.run() dialog.destroy() self.destroy()
def __install(self, game: Game = None): GLib.idle_add(self.__update_to_state, game.state.INSTALLING, game) game.install_dir = game.get_install_dir() try: if os.path.exists(game.keep_path): install_game(game, game.keep_path, main_window=self) else: install_game(game, game.download_path, main_window=self) except (FileNotFoundError, BadZipFile): GLib.idle_add(self.__update_to_state, game.state.DOWNLOADABLE, game) return GLib.idle_add(self.__update_to_state, game.state.INSTALLED, game) GLib.idle_add(self.__reload_state, game) # make user to add the game to the side bar # check if DLCs should also be installed if game.type == "game" and Config.get("install_dlcs"): # first ensure we know about game dlcs self.library.update_dlcs_for_game(game) if len(game.dlcs) == 0: return # now grab DLCs that can be installed downloads = [] for dlc in game.dlcs: try: download_info = self.api.get_download_info( dlc, game.platform, True, dlc.get_installers()) except Exception: # could not find a valid target, ignore it continue # set dlc information now, otherwise this will break later dlc.platform = game.platform dlc.language = game.language # add download # Start the download for all files for key, file_info in enumerate(download_info['files']): if key > 0: download_path = "{}-{}.bin".format( dlc.download_path, key) else: download_path = dlc.download_path download = Download(url=self.api.get_real_download_link( file_info["downlink"]), title=dlc.name, associated_object=dlc, save_location=download_path, number=key + 1, file_size=download_info["total_size"], out_of_amount=len( download_info['files'])) download.register_finish_function(self.__dlc_finish_func, [game, dlc]) download.register_progress_function( self.set_progress, game) download.register_cancel_function(self.__cancel_download, game) downloads.append(download) DownloadManager.download(downloads)
def __add_video(self, vidx, video_url, thumbnail_url): if self.__set_video(vidx, video_url): return True # Download the thumbnail img = os.path.join(self.game.cache_dir, "video_{}.jpg".format(vidx)) download = Download(thumbnail_url, img) download.register_finish_function(self.__set_video, (vidx, video_url)) DownloadManager.download_now(download) return True
def __add_screenshot(self, image_id, image_url): if self.__set_screenshot(image_id): return True # Download the thumbnail img = os.path.join(self.game.cache_dir, "screenshot_{}_ggvgt_2x.jpg".format(image_id)) download = Download(image_url, img) download.register_finish_function(self.__set_screenshot, image_id) DownloadManager.download_now(download) return True
def __cancel_download(self, game: Game = None): DownloadManager.cancel_download(game.downloads) GLib.idle_add(self.__reload_state, game) if game.state == game.state.DOWNLOADING: ## remove sidebar tile if game.sidebar_tile is not None: game.sidebar_tile.get_parent().get_parent().remove( game.sidebar_tile.get_parent()) game.sidebar_tile = None GLib.idle_add(self.__update_to_state, game.state.DOWNLOADABLE, game) elif game.state == game.state.UPDATE_DOWNLOADING: GLib.idle_add(self.__update_to_state, game.state.INSTALLED, game)
def load_thumbnail(self): if self.__set_image(): return True if not self.game.image_url or not self.game.id: return False # Download the thumbnail image_url = "https:{}_196.jpg".format(self.game.image_url) thumbnail = os.path.join(THUMBNAIL_DIR, "{}_196.jpg".format(self.game.id)) download = Download(image_url, thumbnail) download.register_finish_function(self.__set_image) DownloadManager.download_now(download) return True
def __load_background_image(self): if self.__set_background_image(): return True if not self.game.background_url or not self.game.id: return False # Download the thumbnail image_url = "https:{}".format(self.game.background_url) img = os.path.join(self.game.cache_dir, "{}_background.jpg".format(self.game.id)) download = Download(image_url, img) download.register_finish_function(self.__set_background_image) DownloadManager.download_now(download) return True
def load_icon(self): if self.__set_image(): return True if not self.game.sidebar_icon_url or not self.game.id: return False # Download the thumbnail image_url = "https:{}".format(self.game.sidebar_icon_url) icon = os.path.join(self.game.cache_dir, "{}_sbicon.png".format(self.game.id)) download = Download(image_url, icon) download.register_finish_function(self.__set_image) DownloadManager.download_now(download) return True
def __init__(self, name): Gtk.ApplicationWindow.__init__(self, title=name) self.api = Api() self.offline = False self.library = Library(self.api) self.games = [] self.library_view = LibraryView(self, library=self.library, api=self.api) self.details = None res = self.get_screen_resolution() # we got resolution if res[0] > 0 and res[0] <= 1368: self.set_default_size(1024, 700) # Set the icon icon = GdkPixbuf.Pixbuf.new_from_file(LOGO_IMAGE_PATH) self.set_default_icon_list([icon]) self.installed_search.connect("search-changed", self.filter_installed) self.selection_button.hide() # Show the window self.show_all() self.selection_button.hide() self.selection_label.set_text(_("Library")) if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR) # Create the thumbnails directory if not os.path.exists(THUMBNAIL_DIR): os.makedirs(THUMBNAIL_DIR) # Interact with the API self.__authenticate() self.user_photo.set_tooltip_text( self.api.get_user_info(self.__set_avatar)) self.sync_library() # Check what was the last view if Config.get("last_view") == "Game": print("last view as game..") else: self.__show_library() # Register self as a download manager listener DownloadManager.register_listener(self.__download_listener_func)
def on_donwload_action_clicked(self, widget): # get the current download state state = self.download.state() if state == state.DOWNLOADING: self.download.pause() elif state == state.QUEUED: # nothing can be done in this state return elif state == state.PAUSED: self.download.resume() elif state == state.FINISHED or state == state.ERROR: DownloadManager.clear_download(self.download) for child in self.parent.get_children(): if child.get_children()[0] == self: self.parent.remove(child) self.destroy() return;
def __update_downloads(self): # disabled now for child in self.downloads_list.get_children(): self.downloads_list.remove(child) for download in DownloadManager.list(): row = DownloadRow(self.downloads_list, download, self.api) GLib.idle_add(self.downloads_list.prepend, row)
def load_icon(self): if self.__set_image(): return True if not self.achievement.image_url_unlocked or not self.achievement.achievement_id: return False # Download the thumbnail image_url = self.achievement.image_url_unlocked if self.achievement.date_unlocked is not None else self.achievement.image_url_locked icon = os.path.join( self.game.cache_dir, "achievement_{}_{}.jpg".format( self.achievement.achievement_id, "unlocked" if self.achievement.date_unlocked is not None else "locked")) download = Download(image_url, icon) download.register_finish_function(self.__set_image) DownloadManager.download_now(download) return True
def __download_file(self, widget, link: str, fname: str = None): dialog: Gtk.FileChooserDialog = Gtk.FileChooserDialog( _("Download"), self.parent, action=Gtk.FileChooserAction.SAVE, buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT)) file_name = link[link.rfind("/") + 1:] if fname is None else fname dialog.set_current_name(file_name) response = dialog.run() if response == Gtk.ResponseType.ACCEPT: download = Download(url=self.api.get_real_download_link(link), save_location=dialog.get_filename()) DownloadManager.download(download) print("Downloading: \"{}\" to \"{}\"".format( link, dialog.get_filename())) dialog.destroy()
def __install_file(self, widget, link: str, ftype: str = None, product_id: int = None, fsize: int = 0, treeiter: Gtk.TreeIter = None): if (ftype == _("Installer")): # just follow the default path self.parent.download_game(self.game) elif ftype == "DLC " + _("Installer"): dlc = None if product_id is not None: for i in self.game.dlcs: if i.id == product_id: dlc = i break if dlc is not None: dlc.platform = self.downloadstree.get_model().get_value( treeiter, 10) dlc.language = DOWNLOAD_LANGUAGES_TO_GOG_CODE[ self.downloadstree.get_model().get_value(treeiter, 2)] dlc.available_version = self.downloadstree.get_model( ).get_value(treeiter, 5) download = Download(url=self.api.get_real_download_link(link), save_location=dlc.download_path, title=dlc.name, associated_object=dlc, file_size=fsize) else: # fallback to game as source of generic information download = Download(url=self.api.get_real_download_link(link), save_location=self.game.download_path, title=self.game.name, associated_object=self.game, file_size=fsize) download.register_finish_function(self.__finish_download, [treeiter, dlc]) download.register_state_function(self.__state_func, treeiter) DownloadManager.download(download) print("Downloading: \"{}\"".format(link))
def __download_file(self, game: Game, operating_system=None) -> None: Config.set("current_download", game.id) GLib.idle_add(self.__update_to_state, game.state.QUEUED, game) current_os = platform.system() if current_os == "Linux": current_os = "linux" elif current_os == "Windows": current_os = "windows" elif current_os == "Darwin": current_os = "mac" # pick current os if none was passed if operating_system is None: operating_system = current_os if game.platform is None: game.platform = operating_system download_info = self.api.get_download_info( game, operating_system=operating_system) # Start the download for all files game.downloads = [] download_path = game.download_path finish_func = self.__install for key, file_info in enumerate(download_info['files']): if key > 0: download_path = "{}-{}.bin".format(self.download_path, key) download = Download(url=self.api.get_real_download_link( file_info["downlink"]), title=download_info["name"], associated_object=game, save_location=download_path, number=key + 1, file_size=download_info["total_size"], out_of_amount=len(download_info['files'])) download.register_finish_function(finish_func, game) download.register_progress_function(self.set_progress, game) download.register_cancel_function(self.__cancel_download, game) game.downloads.append(download) DownloadManager.download(game.downloads)