14
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
from gi.repository import Gtk
17
from gi.repository import Gtk, GObject
18
18
from GtkHelpers import clear_text_entry, show_clear_icon
19
19
from qreator_lib.helpers import get_data_file
20
20
from qreator_lib.i18n import _
23
from urllib import urlencode
26
class IsdgShortener(object):
28
def __init__(self, url=None):
29
self.api_url= "http://is.gd/create.php"
33
self.r = requests.get("{0}?format=simple&url={1}".format(self.api_url, self.url))
34
if self.r.content.startswith("Error"):
35
raise Exception(self.r.content)
39
class TinyUrlShortener(object):
41
def __init__(self, url=None):
42
self.api_url = "http://tinyurl.com/api-create.php"
46
self.r = requests.get("{0}?url={1}".format(self.api_url, self.url))
50
class BitlyShortener(object):
52
def __init__(self, url=None):
53
self.api_url = 'http://api.bit.ly/v3/shorten'
55
self.data = {"login": "", # TODO
60
self.r = requests.get(self.api_url, params=self.data)
61
self.results = json.loads(self.r.content)
62
return self.results["data"]["url"]
65
class GoogleShortener(object):
67
def __init__(self, url=None):
68
self.api_key = "AIzaSyCU_pFNpACW4luWEZRgNnfWMEUdlnZyYQI"
69
self.api_url = 'https://www.googleapis.com/urlshortener/v1/url'
71
self.params = {"key": self.api_key,
72
'Content-Type' : 'application/json'}
73
self.data = {"longUrl": self.url}
76
self.r = requests.post(self.api_url, headers=self.params, data=json.dumps(self.data))
77
self.results = json.loads(self.r.content)
78
return self.results["id"]
81
SHORTENER_TYPES = [IsdgShortener, TinyUrlShortener, BitlyShortener, GoogleShortener,]
23
84
class QRCodeURLGtk(object):
38
101
self.entry.set_placeholder_text(_('[URL]'))
39
102
self.combobox.set_active(0)
41
self.entry.connect("changed", self.on_entryURL_changed)
104
# Currently four shortener options are available: isdg google tinyurl bitly
105
# We are choosing isdg at the moment. Later we will allow to define this
107
self.Shortener = SHORTENER_TYPES[0]
109
self.builder.get_object("togglebuttonShorten").set_tooltip_text(
110
"Use the {0} online service to generate a short URL.".format(self.Shortener.name))
111
self.builder.get_object("togglebuttonShorten").set_has_tooltip(True)
113
self.entryhandler = self.entry.connect("changed", self.on_entryURL_changed)
114
# moved this signal from the glade file to here, to be able to block it later
115
self.iconhandler = self.entry.connect("icon-press", self.on_entryURL_icon_press)
42
116
self.combobox.connect("changed", self.on_comboboxtextProtocol_changed)
118
def on_togglebuttonShorten_toggled(self, widget):
119
if widget.get_active():
120
# not sensitive while shortener is making the network requests
121
# not editable while the shortener button is toggled
122
self.entry.set_sensitive(False)
123
self.entry.set_editable(False)
124
self.entry.handler_block(self.iconhandler)
125
self.combobox.set_sensitive(False)
126
self.long_address = self.address
127
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, False)
128
# if the request takes longer, the interface should in the meantime change to
129
# unsensitive button and entry, to show at least some behaviour
130
GObject.idle_add(self._shorten_and_display)
132
self.entry.set_has_tooltip(False)
133
self.entry.set_text(self.long_address)
134
self.entry.set_editable(True)
135
self.entry.handler_unblock(self.iconhandler)
136
self.combobox.set_sensitive(True)
137
self.qr_code_update_func(self.address)
138
self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, True)
140
def on_messagedialog1_response(self, widget, data=None):
143
def _reset_shortener(self):
144
self.builder.get_object('togglebuttonShorten').set_active(False)
146
def _shorten_and_display(self):
147
self.entry.set_sensitive(True)
148
s = self.Shortener(url=self.address)
150
self.short_address = s.short_url()
151
except requests.exceptions.ConnectionError as e:
152
self.messagedialog.set_markup(_("A network connection error occured: {0}".format(e)))
153
self.messagedialog.show()
154
GObject.idle_add(self._reset_shortener)
156
except Exception as e:
157
self.messagedialog.set_markup(_("An error occured while trying to shorten the url: {0}".format(e)))
158
self.messagedialog.show()
159
GObject.idle_add(self._reset_shortener)
161
self.entry.set_tooltip_text(self.long_address)
162
self.entry.set_has_tooltip(True)
163
self.entry.set_text(self.short_address)
164
self.entry.select_region(0, len(self.entry.get_text()))
165
self.qr_code_update_func(self.short_address)
44
167
def on_activated(self):