Skip to content

Commit

Permalink
Allowed drag&droping links to import profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kozec committed Mar 9, 2017
1 parent 8cf4b89 commit a843ddf
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 160 deletions.
5 changes: 0 additions & 5 deletions profile_examples/Portal 2 coop.sccprofile
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
"doubleclick": {},
"hold": {
"action": "menu('Default.menu')"
},
"modes": {
"RT": {
"action": "shell('sc-controller')"
}
}
},
"LB": {
Expand Down
2 changes: 1 addition & 1 deletion profile_examples/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Copy to ~/.config/scc/profiles/
Drag and drop link on main window to import profile.
64 changes: 0 additions & 64 deletions profile_examples/Retro City Rampage - in vehicle.sccprofile

This file was deleted.

62 changes: 0 additions & 62 deletions profile_examples/Retro City Rampage - on foot.sccprofile

This file was deleted.

1 change: 1 addition & 0 deletions profile_examples/Tomb Raider 2013.sccprofile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"name": "Melee"
}
},
"gyro": {},
"left_pad": {
"action": "dpad(button(Keys.KEY_1), button(Keys.KEY_2), button(Keys.KEY_3), button(Keys.KEY_4))",
"name": "Weapon Switch"
Expand Down
5 changes: 0 additions & 5 deletions profile_examples/firefox.sccprofile
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"doubleclick": {},
"hold": {
"action": "menu('Default.menu')"
},
"modes": {
"RT": {
"action": "shell('sc-controller')"
}
}
},
"LB": {
Expand Down
58 changes: 48 additions & 10 deletions scc/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from scc.config import Config

import scc.osd.menu_generators
import os, sys, platform, json, logging
import os, sys, platform, json, urllib, logging
log = logging.getLogger("App")

class App(Gtk.Application, UserDataManager, BindingEditor):
Expand Down Expand Up @@ -102,7 +102,8 @@ def setup_widgets(self):

# Drag&drop target
self.builder.get_object("content").drag_dest_set(Gtk.DestDefaults.ALL, [
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags.OTHER_APP, 0)
Gtk.TargetEntry.new("text/uri-list", Gtk.TargetFlags.OTHER_APP, 0),
Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags.OTHER_APP, 0)
], Gdk.DragAction.COPY
)

Expand Down Expand Up @@ -1084,19 +1085,56 @@ def load_profile_selection(self):

def on_drag_data_received(self, widget, context, x, y, data, info, time):
""" Drag-n-drop handler """
uri = None
if str(data.get_data_type()) == "text/uri-list":
# Only file can be dropped here
if len(data.get_uris()):
uri = data.get_uris()[0]
elif str(data.get_data_type()) == "text/plain":
# This can be anything, so try to extract uri from it
lines = str(data.get_data()).split("\n")
if len(lines) > 0:
first = lines[0]
if first.startswith("http://") or first.startswith("https://") or first.startswith("ftp://"):
# I don't like other protocols
uri = first
if uri:
from scc.gui.importexport.dialog import Dialog
giofile = None
if uri.startswith("file://"):
giofile = Gio.File.new_for_uri(uri)
if giofile.get_path():
path = giofile.get_path()
from scc.gui.importexport.dialog import Dialog
if Dialog.is_supported(path):
ied = Dialog(self)
ied.show(self.window)
# Skip first screen and try to import this file
ied.import_file(giofile.get_path())
else:
# Local file can be used directly, remote has to
# be downloaded first
if uri.startswith("https://github.com/"):
# Convert link to repository display to link to raw file
uri = (uri
.replace("https://github.com/", "https://raw.githubusercontent.com/")
.replace("/blob/", "/")
)
name = urllib.unquote(".".join(uri.split("/")[-1].split(".")[0:-1]))
remote = Gio.File.new_for_uri(uri)
tmp, stream = Gio.File.new_tmp("%s.XXXXXX" % (name,))
stream.close()
if remote.copy(tmp, Gio.FileCopyFlags.OVERWRITE, None, None):
# Sucessfully downloaded
log.info("Downloaded '%s'" % (uri,))
giofile = tmp
else:
# Failed. Just do nothing
return
if giofile.get_path():
path = giofile.get_path()
filetype = Dialog.determine_type(path)
if filetype:
log.info("Importing '%s'..." % (filetype))
log.debug("(type %s)" % (filetype,))
ied = Dialog(self)
ied.show(self.window)
# Skip first screen and try to import this file
ied.import_file(path, filetype = filetype)
else:
log.error("Unknown file type: '%s'..." % (path,))


class UndoRedo(object):
Expand Down
59 changes: 46 additions & 13 deletions scc/gui/importexport/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from import_vdf import ImportVdf
from import_sccprofile import ImportSccprofile

import sys, os, logging
import sys, os, tarfile, logging, json
log = logging.getLogger("IE.Dialog")

class Dialog(Editor, ComboSetter, Export, ImportVdf, ImportSccprofile):
Expand All @@ -29,16 +29,48 @@ def __init__(self, app):


@staticmethod
def is_supported(filename):
def determine_type(filename):
"""
Returns True if passed file can be imported.
Detects and returns type of passed file, if it can be imported.
Returns one of 'sccprofile', 'sccprofile.tar.gz', 'vdf', 'vdffz'
or None if type is not supported.
"""
# Currently decided base on extension
return (filename.endswith(".sccprofile")
or filename.endswith(".sccprofile.tar.gz")
or filename.endswith(".vdf")
or filename.endswith(".vdffz")
)
f = file(filename, 'rb').read(1024)
try:
if f.decode("utf-8").strip(" \t\r\n").startswith("{"):
# Looks like json
data = json.loads(open(filename, "r").read())
if "buttons" in data and "gyro" in data:
return 'sccprofile'
if "GameName" in data and "FileName" in data:
return 'vdffz'
except:
# Definitelly not json
pass

if f[0:2] == b"\x1f\x8b":
# gzip, hopefully tar.gz
try:
tar = tarfile.open(filename, "r:gz")
names = [ x.name for x in tar ]
any_profile = any([ x.endswith(".sccprofile") for x in names ])
if any_profile and "profile-name" in names:
return "sccprofile.tar.gz"
except:
# Not a tarball
pass

# Rest is decided by extension
if filename.endswith(".sccprofile.tar.gz"):
return "sccprofile.tar.gz"
if filename.endswith(".vdf"):
return "vdf"
# Fallbacks if above fails
if filename.endswith(".sccprofile"):
return "sccprofile"
if filename.endswith(".vdffz"):
return "vdffz"
return None


@staticmethod
Expand All @@ -54,18 +86,19 @@ def check_name(name):
return True


def import_file(self, filename):
def import_file(self, filename, filetype = None):
"""
Attempts to import passed file.
Switches to apropriate page automatically, or, if file cannot be
imported, does nothing.
"""
if filename.endswith(".sccprofile"):
filetype = filetype or Dialog.determine_type(filename)
if filetype == "sccprofile":
self.import_scc(filename=filename)
elif filename.endswith(".sccprofile.tar.gz"):
elif filetype == "sccprofile.tar.gz":
self.import_scc_tar(filename=filename)
elif filename.endswith(".vdf") or filename.endswith(".vdffz"):
elif filetype in ("vdf", "vdffz"):
self.import_vdf(filename=filename)


Expand Down

0 comments on commit a843ddf

Please sign in to comment.