예제 #1
0
    def on_remote_game_activate(self, widget):
        # Ask the user for an URL
        widgets = gamewidget.getWidgets()
        url_dialog = widgets["url_path_dialog"]
        widgets["url_edit"].set_text('')
        widgets["url_edit"].grab_focus()
        answer = url_dialog.run()
        url_dialog.hide()
        if answer != Gtk.ResponseType.OK.real:
            return

        # Download the game
        url = widgets["url_edit"].get_text().strip()
        if len(url) == 0:
            return
        remdata = get_internet_game_as_pgn(url)
        if remdata is None:
            dlg = Gtk.MessageDialog(
                mainwindow(),
                type=Gtk.MessageType.ERROR,
                buttons=Gtk.ButtonsType.OK,
                message_format=
                _("The provided link does not lead to a meaningful chess content."
                  ))
            dlg.run()
            dlg.destroy()
            return

        # Load the game
        newGameDialog.loadPgnAndRun(remdata)
예제 #2
0
 def on_remote_game_activate(self, widget):
     url = getUserTextDialog(mainwindow(), _('Load a remote game'), _('Paste the link to download:'))
     if url is not None:
         remdata = get_internet_game_as_pgn(url.strip())
         if remdata is None:
             dlg = Gtk.MessageDialog(mainwindow(),
                                     type=Gtk.MessageType.ERROR,
                                     buttons=Gtk.ButtonsType.OK,
                                     message_format=_("The provided link does not lead to a meaningful chess content."))
             dlg.run()
             dlg.destroy()
         else:
             newGameDialog.loadPgnAndRun(remdata)
예제 #3
0
 def on_enter_game_clipboard_activate(self, widget):
     ok = False
     clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
     text = clipboard.wait_for_text()
     if text not in [None, '']:
         try:
             pgn = PGNFile(StringIO(text))
             pgn.loadToModel(None)
             newGameDialog.loadPgnAndRun(text)
             ok = True
         except Exception:
             pass
     if not ok:
         dialog = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK)
         dialog.set_markup(_('The clipboard contains no relevant chess data.'))
         dialog.run()
         dialog.destroy()
예제 #4
0
    def on_drag_received(self, widget, context, x, y, selection, target_type,
                         timestamp):
        if target_type == TARGET_TYPE_URI_LIST:
            NOTPROC, PARTIAL, FULL = range(3)
            status = NOTPROC
            uris = selection.get_uris()
            for uri in uris:
                fn, fext = os.path.splitext(uri.lower())
                b = False

                # Chess position
                if fext == '.fen':
                    b = newGameDialog.loadFileAndRun(uri)

                # Shortcut
                elif fext in ['.url', '.desktop']:
                    # Preconf
                    if fext == '.url':
                        sectname = 'InternetShortcut'
                        typeok = True
                    elif fext == '.desktop':
                        sectname = 'Desktop Entry'
                        typeok = False
                    else:
                        assert (False)

                    # Read the shortcut
                    filename = splitUri(uri)[1]
                    with open(filename, 'r') as file:
                        content = file.read()
                    lines = content.replace("\r", '').split("\n")

                    # Extract the link
                    section = False
                    link = ''
                    for item in lines:
                        # Header
                        if item.startswith('['):
                            if section:
                                break
                            section = item.startswith('[%s]' % sectname)
                        if not section:
                            continue

                        # Item
                        if item.startswith('URL='):
                            link = item[4:]
                        if item.startswith('Type=Link') and fext == '.desktop':
                            typeok = True

                    # Load the link
                    if typeok and link != '':
                        pgn = get_internet_game_as_pgn(link)
                        b = newGameDialog.loadPgnAndRun(pgn)

                # Database
                else:
                    perspective = perspective_manager.get_perspective(
                        "database")
                    perspective.open_chessfile(uri)
                    b = True

                # Update the global status
                if b:
                    if status == NOTPROC:
                        status = FULL
                else:
                    if status != NOTPROC:
                        status = PARTIAL

            # Feedback about the load
            msg = ''
            if status == NOTPROC:
                msg = _(
                    'All the links failed to fetch a relevant chess content.')
                msgtype = Gtk.MessageType.ERROR
            elif status == PARTIAL:
                msg = _('Some links were invalid.')
                msgtype = Gtk.MessageType.WARNING
            if msg != '':
                dlg = Gtk.MessageDialog(mainwindow(),
                                        type=msgtype,
                                        buttons=Gtk.ButtonsType.OK,
                                        message_format=msg)
                dlg.run()
                dlg.destroy()