class MainApplication(KApplication): def __init__(self): KApplication.__init__(self) # in case something needs done before quitting self.connect(self, SIGNAL('aboutToQuit()'), self.quit) # place dcop object here self.dcop = DosboxHandler() self._setup_standard_directories() self._generate_data_directories() self.mainconfigfilename = os.path.join(self.datadir, 'dosbox-pykde.conf') if os.path.isfile(self.mainconfigfilename): self.generate_default_config() self.generate_main_objects() def generate_default_config(self): default_dbox_cfilename = os.path.join(self.datadir, 'dosbox.conf.default') # generate default config files if not already present # this would be a good time to use a wizard to setup config options generate_default_config(self.mainconfigfilename) generate_default_dosbox_config(default_dbox_cfilename) self.load_main_config() def generate_main_objects(self): # setup objects self.game_datahandler = self.make_new_datahandler() self.game_fileshandler = self.make_new_fileshandler() self.dosbox = self.make_new_dosbox_object() # we call self._generate_data_directories() in this method # because changes to the config may affect these options # and the corresponding application attributes def load_main_config(self): self.myconfig = MyConfig() self.myconfig.read([self.mainconfigfilename]) self._generate_archive_directories() def update_main_config(self, configobj): cfile = StringIO() configobj.write(cfile) cfile.seek(0) self.myconfig.readfp(cfile) self.save_main_config(self.mainconfigfilename) self.load_main_config() def save_main_config(self, filename=None): if filename is None: filename = self.mainconfigfilename cfile = file(filename, 'w') self.myconfig.write(cfile) cfile.close() def make_new_datahandler(self): return GameDataHandler(self) def make_new_fileshandler(self): return GameFilesHandler(self) def make_new_dosbox_object(self): return Dosbox(self) # this method sets up the directories used by the application # with respect to the KDE environment # currently the main config file is placed in self.datadir # changes in the file dialogs used in the application will # be stored in the config file in its proper location # when I am ready to deal with changes to that config file # that my code doesn't use, I will probably move the main # config file to the regular config location def _setup_standard_directories(self): self._std_dirs = KStandardDirs() self.tmpdir_parent = str(self._std_dirs.findResourceDir('tmp', '/')) self.datadir_parent = str(self._std_dirs.findResourceDir('data', '/')) self.tmpdir = os.path.join(self.tmpdir_parent, 'dosbox-pykde') self.datadir = os.path.join(self.datadir_parent, 'dosbox-pykde') # we need this in dosbox object (for now) self.main_config_dir = self.datadir if not os.path.exists(self.datadir): os.mkdir(self.datadir) def _generate_data_directories(self): directories = {}.fromkeys(['games', 'configs', 'screenshots', 'capture', 'profiles']) for dir_key in directories: path = os.path.join(self.datadir, dir_key) directories[dir_key] = path if not os.path.exists(path): os.mkdir(path) self.data_directories = directories # this method sets attributes for the main directories # used in the application. # it also creates the directories if they aren't already there # except for the main_dosbox_path # I should probably use a yes/no dialog before actually creating the # directories. def _generate_archive_directories(self): cfg = self.myconfig installed_archives_path = cfg.get('filemanagement', 'installed_archives_path') extras_archives_path = cfg.get('filemanagement', 'extras_archives_path') makepaths(installed_archives_path, extras_archives_path) self.installed_archives_path = installed_archives_path self.extras_archives_path = extras_archives_path self.main_dosbox_path = cfg.get('dosbox', 'main_dosbox_path') # This method is currently useless, but may be useful later # if some house cleaning needs doing before quitting def quit(self): # house cleaning chores go here KApplication.quit(self)
class MainApplication(KApplication): def __init__(self): KApplication.__init__(self) # in case something needs done before quitting self.connect(self, SIGNAL("aboutToQuit()"), self.quit) # place dcop object here self.dcop = DosboxHandler() self._setup_standard_directories() self._generate_data_directories() self.mainconfigfilename = os.path.join(self.datadir, "dosbox-pykde.conf") if os.path.isfile(self.mainconfigfilename): self.generate_default_config() self.generate_main_objects() def setMainWidget(self, win): self.mainwindow = win KApplication.setMainWidget(self, win) def generate_default_config(self): default_dbox_cfilename = os.path.join(self.datadir, "dosbox.conf.default") # generate default config files if not already present # this would be a good time to use a wizard to setup config options generate_default_config(self.mainconfigfilename) generate_default_dosbox_config(default_dbox_cfilename) self.load_main_config() def generate_main_objects(self): # setup objects self.upgrade_to_db_required = False self._dbfile = self.datadir / "main.db" if not self._dbfile.exists(): print "no database file exists." # test for xml files gdh = GameDataHandlerCompat(self) games = gdh.get_game_names() if games: self.upgrade_to_db_required = True self.conn = Connection(self._dbfile) # self.game_datahandler = self.make_new_datahandler() self.game_datahandler = GameDataBaseCompat(self.conn) self.game_datahandler.set_screenshots_path(self.data_directories["screenshots"]) self.game_fileshandler = self.make_new_fileshandler() self.dosbox = self.make_new_dosbox_object() def convert_all_xml_to_db(self): gdh = GameDataHandlerCompat(self) games = gdh.get_game_names() for game in games: print "converting", game gamedata = gdh.get_game_data(game) installed = gdh.get_installed_files(game) self.game_datahandler.add_new_game(gamedata, installed) # we call self._generate_data_directories() in this method # because changes to the config may affect these options # and the corresponding application attributes def load_main_config(self): self.myconfig = MyConfig() self.myconfig.read([self.mainconfigfilename]) self._generate_archive_directories() def update_main_config(self, configobj): cfile = StringIO() configobj.write(cfile) cfile.seek(0) self.myconfig.readfp(cfile) self.save_main_config(self.mainconfigfilename) self.load_main_config() def save_main_config(self, filename=None): if filename is None: filename = self.mainconfigfilename cfile = file(filename, "w") self.myconfig.write(cfile) cfile.close() def make_new_datahandler(self): return GameDataHandler(self) def make_new_fileshandler(self): return GameFilesHandler(self) def make_new_dosbox_object(self): return Dosbox(self) # this method sets up the directories used by the application # with respect to the KDE environment # currently the main config file is placed in self.datadir # changes in the file dialogs used in the application will # be stored in the config file in its proper location # when I am ready to deal with changes to that config file # that my code doesn't use, I will probably move the main # config file to the regular config location def _setup_standard_directories(self): self._std_dirs = KStandardDirs() self.tmpdir_parent = path(str(self._std_dirs.findResourceDir("tmp", "/"))) self.datadir_parent = path(str(self._std_dirs.findResourceDir("data", "/"))) self.tmpdir = self.tmpdir_parent / "dosbox-pykde" self.datadir = self.datadir_parent / "dosbox-pykde" # we need this in dosbox object (for now) self.main_config_dir = self.datadir if not self.datadir.exists(): self.datadir.mkdir() def _generate_data_directories(self): directories = {}.fromkeys(["games", "configs", "screenshots", "capture", "profiles", "cache"]) for dir_key in directories: newdir = self.datadir / dir_key directories[dir_key] = newdir if not newdir.exists(): try: newdir.mkdir() except OSError, inst: # print inst, dir(inst), inst.errno if inst.errno != 17: raise OSError(inst) self.data_directories = directories