Exemple #1
0
class Application(Gtk.Application):
    simplename = "video-games"
    fullname = "Video Games"
    application_id = 'org.gnome.' + simplename
    
    def __init__(self):
        print (time.time()-start_time, "start init application")
        Gtk.Application.__init__(self, application_id = self.application_id, flags = Gio.ApplicationFlags.FLAGS_NONE)
        GLib.threads_init()
        Gdk.threads_init()
        
        self.datadir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))
        
        self.savedatadir = Environement.save_data_path()
        
        self.systems = Badnik.SystemCollection.full ()
        
        self.gamesdb = Badnik.Library(db_name = "games", db_dir = self.savedatadir, systems = self.systems)
        #self.gamesdb.app = self
        self.gamesdb.path = os.path.join(self.savedatadir, "games.db")
        
        self.focused_game = None
        
        self.connect("activate", self.on_activate)
        
        self.register(None)
        
        self.settings = Gio.Settings.new(self.application_id)
        
        self.builder = Gtk.Builder()
        self.builder.add_from_file(Environement.get_resource("AppMenu.ui"))
        self.builder.connect_signals(self)
        
        self.menumodel = self.builder.get_object("AppMenu")
        self.set_app_menu(self.menumodel)
        
        self._action_entries = [ { 'name': 'quit', 'callback': self.on_quit, 'accel': '<Primary>q' },
                                 { 'name': 'about', 'callback': self.on_about },
                                 { 'name': 'help', 'callback': self.on_help, 'accel': 'F1' },
                                 #{ 'name': 'fullscreen', 'callback': self.on_fullscreen, 'accel': 'F11' },
                                 #{ 'name': 'view-as', 'callback': self.on_view_as, 'create_hook': self._view_as_create_hook,
                                 #  'parameter_type': 's', 'state': self.settings.get_value('view-as') },
                                 { 'name': 'add-games', 'callback': self.on_add_games },
                                 #{ 'name': 'download-metadata', 'callback': self.on_download_metadata, 'accel': '<Primary>m' }
                               ]
        self._add_actions()
        
        settings = Gtk.Settings.get_default()
        settings.set_property("gtk-application-prefer-dark-theme", True)
        settings.set_property("gtk-shell-shows-app-menu", True)
        print (time.time()-start_time, "end init application")
        
        self.running_games = {}
        
        self.systems.connect("game_found", self.on_game_found)
        self.gamesdb.connect("game_added", self.on_game_added)
    
    def on_activate(self, data=None):
        #print (time.time()-start_time, "start activating application")
        #print("start window")
        self.window = Window(self)
        self.window.show ()
        self.window.connect('play_clicked', self.on_play_clicked)
        #print("stop  window")
        self.window.connect("destroy", self.on_quit, None)
        self.window.show()
        self.add_window(self.window)
        
        self.update_library_async()
        #print (time.time()-start_time, "end activating application")
    
    def on_play_clicked(self, window, game):
        self.play(game)
    
    def _add_actions(self):
        for action_entry in self._action_entries:
            state = action_entry['state'] if 'state' in action_entry else None
            parameterType = GLib.VariantType.new(action_entry['parameter_type']) if 'parameter_type' in action_entry else None
            
            action = None
            if (state):
                action = Gio.SimpleAction.new_stateful(action_entry['name'], parameterType, action_entry['state'])
            else:
                action = Gio.SimpleAction.new(action_entry['name'], None)
            
            if 'create_hook' in action_entry:
                action_entry['create_hook'](action)
            
            if 'callback' in action_entry:
                action.connect('activate', action_entry['callback'])
            
            if 'accel' in action_entry:
                self.add_accelerator(action_entry['accel'], 'app.' + action_entry['name'], None)
            
            self.add_action(action)
    
    def on_quit(self, action, data):
        print("Quitting Badnik")
        self.window.hide()
        self.systems.set_property("stop_searches", True)
        #self.gamesdb.set_property("stop_searches", True)
        self.quit()
    
    def on_about(self, action, data):
        aboutdialog = Gtk.AboutDialog()
        aboutdialog.set_destroy_with_parent(True)
        aboutdialog.set_transient_for(self.window)
        aboutdialog.set_modal(True)
        
        #aboutdialog.set_artists([])
        aboutdialog.set_authors(["Adrien Plazas<*****@*****.**>"])
        aboutdialog.set_comments("A video game launcher")
        aboutdialog.set_copyright("Copyright © 2013 Adrien Plazas")
        #aboutdialog.set_documenters([])
        aboutdialog.set_license_type(Gtk.License.GPL_3_0)
        aboutdialog.set_logo_icon_name(self.simplename)
        aboutdialog.set_program_name(self.fullname)
        #aboutdialog.set_translator_credits()
        aboutdialog.set_version("0.1.0")
        aboutdialog.set_website("http://adrienplazas.net")
        
        def on_close(action, parameter):
            action.destroy()
        aboutdialog.connect("response", on_close)
        aboutdialog.show()
    
    def on_help(self, action, data):
        print("help")
    
    def on_fullscreen(self, action, data):
        print("fullscreen")
    
    def on_view_as(self, action, parameter):
        self.settings.set_value('view-as', parameter)
    
    def on_add_games(self, action, data):
        print("add games")
    
    def on_game_found (self, systems, game):
        self.gamesdb.add (game)
    
    def on_game_added (self, library, game):
        self.window.gamelist.add_game (game)
    
    def on_download_metadata(self, action, data):
        if (self.focused_game):
            #self.gamesdb.download_game_metadata(self.focused_game)
            pass
    
    def _view_as_create_hook(self, action):
        def _changed_view_as(settings, setting):
            action.state = settings.get_value('view-as')
        self.settings.connect('changed::view-as', _changed_view_as)
    
    def update_library(self):
        self.systems.search_games()
        #self.gamesdb.search_games()
    
    def update_library_async(self):
        Thread(target=self.update_library, args=(), kwargs={}).start()
        
    def play(self, game):
        def stopped (runner, result, game_id):
            self.running_games[game_id] = False
            
        id = Application._get_game_id (game)
        if id and (not (id in self.running_games) or self.running_games[id] == False):
            self.running_games[id] = True
            p = Badnik.Runner (game = game)
            #p.run(callback, data)
            p.run(stopped, id)
    
    def _get_game_id (game):
        return game.get_system_reference () + ":" + game.get_reference ()
    
    def focus_game(self, id):
        self.focused_game = id
Exemple #2
0
class App(KApplication):
    def __init__(self):
        KApplication.__init__(self)
        self.setQuitOnLastWindowClosed(False)

        self.sni = KStatusNotifierItem()
        self.sni.setTitle(i18n("Deveba"))
        self.sni.setCategory(KStatusNotifierItem.SystemServices)
        self.sni.setStatus(KStatusNotifierItem.Active)

        self.sni.setIconByName("task-accepted")

        self.sni.setToolTipTitle(i18n("Deveba"))

        self.window = Window(self)
        self.window.hide()
        self.window.finished.connect(self.slotWindowClosed)
        self.sni.setAssociatedWidget(self.window)

        self.logMessages = []
        self.success = True
        self.workerThread = None

    @staticmethod
    def options():
        options = KCmdLineOptions()
        options.add("log <file>", ki18n("Write log to file"), "-")
        options.add("c").add("config <file>", ki18n("Config file to use"), core.CONFIG_FILE)
        options.add("+[group]", ki18n("Start backup of $group"))
        return options

    def exec_(self):
        args = KCmdLineArgs.parsedArgs()

        core.setup_logger(str(args.getOption("log")))

        config_file = args.getOption("config")
        config = core.load_config(config_file)

        if args.count() > 0:
            group_names = [str(args.arg(x)) for x in range(args.count())]
            groups = core.get_group_list(config, group_names)
            self.startSync(groups)
        else:
            self.window.show()

        return KApplication.exec_()

    def startSync(self, groups):
        self.success = True
        self.workerThread = WorkerThread(groups)
        self.workerThread.logCalled.connect(self.addLog, Qt.QueuedConnection)

        self.sni.setIconByName("task-ongoing")
        self.workerThread.start()
        self.workerThread.finished.connect(self.slotSyncFinished)

    def slotSyncFinished(self):
        if self.success:
            self.sni.setIconByName("task-accepted")
        self.workerThread = None
        QTimer.singleShot(2 * 60 * 1000, self.quitIfNoWindow)

    def slotWindowClosed(self):
        if self.workerThread is None:
            self.quit()

    def quitIfNoWindow(self):
        if not self.window.isVisible():
            self.quit()

    def addLog(self, level, msg):
        if level < UserInterface.LOG_INFO:
            return

        timestamp = time.localtime()
        message = LogMessage(timestamp, level, msg)
        self.logMessages.append(message)

        texts = [x.formatted() for x in self.logMessages[-MAX_TEXTS_LENGTH:]]
        html = "<ul>" + "".join(["<li>%s</li>" % x for x in texts]) + "</ul>"
        self.sni.setToolTipSubTitle(html)

        if level >= UserInterface.LOG_WARNING:
            self.success = False
            self.sni.setIconByName("dialog-error")

        self.window.addLog(message)