~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/qrcodes/QRCodeURLGtk.py

  • Committer: Stefan Schwarzburg
  • Date: 2012-11-22 08:43:25 UTC
  • mfrom: (89.12.25 qreator)
  • mto: This revision was merged to the branch mainline in revision 101.
  • Revision ID: stefan.schwarzburg@googlemail.com-20121122084325-xubvujmrf0tz7d6r
merged with latest trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
### END LICENSE
16
16
 
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 _
 
21
import requests
 
22
import json
 
23
from urllib import urlencode
 
24
 
 
25
 
 
26
class IsdgShortener(object):
 
27
    name = _("Isdg")
 
28
    def __init__(self, url=None):
 
29
        self.api_url= "http://is.gd/create.php"
 
30
        self.url = url
 
31
 
 
32
    def short_url(self):
 
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)
 
36
        return self.r.content
 
37
 
 
38
 
 
39
class TinyUrlShortener(object):
 
40
    name = _("TinyUrl")
 
41
    def __init__(self, url=None):
 
42
        self.api_url = "http://tinyurl.com/api-create.php"
 
43
        self.url = url
 
44
 
 
45
    def short_url(self):
 
46
        self.r = requests.get("{0}?url={1}".format(self.api_url, self.url))
 
47
        return self.r.content
 
48
 
 
49
 
 
50
class BitlyShortener(object):
 
51
    name = _("Bitly")
 
52
    def __init__(self, url=None):
 
53
        self.api_url = 'http://api.bit.ly/v3/shorten'
 
54
        self.url = url
 
55
        self.data = {"login": "", # TODO
 
56
                     "apiKey": "", # TODO
 
57
                     "longUrl": self.url}
 
58
 
 
59
    def short_url(self):
 
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"]
 
63
 
 
64
 
 
65
class GoogleShortener(object):
 
66
    name = _("Google")
 
67
    def __init__(self, url=None):
 
68
        self.api_key = "AIzaSyCU_pFNpACW4luWEZRgNnfWMEUdlnZyYQI"
 
69
        self.api_url = 'https://www.googleapis.com/urlshortener/v1/url'
 
70
        self.url = url
 
71
        self.params = {"key": self.api_key,
 
72
                       'Content-Type' : 'application/json'}
 
73
        self.data = {"longUrl": self.url}
 
74
 
 
75
    def short_url(self):
 
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"]
 
79
 
 
80
 
 
81
SHORTENER_TYPES = [IsdgShortener, TinyUrlShortener, BitlyShortener, GoogleShortener,]
21
82
 
22
83
 
23
84
class QRCodeURLGtk(object):
30
91
        self.builder.connect_signals(self)
31
92
        self.grid = self.builder.get_object('qr_code_url')
32
93
 
 
94
        self.messagedialog = self.builder.get_object('messagedialog1')
 
95
 
33
96
        self.entry = self.builder.get_object('entryURL')
34
97
        self.combobox = self.builder.get_object('comboboxtextProtocol')
35
98
 
38
101
        self.entry.set_placeholder_text(_('[URL]'))
39
102
        self.combobox.set_active(0)
40
103
 
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
 
106
        # in preferences.
 
107
        self.Shortener = SHORTENER_TYPES[0]
 
108
 
 
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)
 
112
        
 
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)
43
117
 
 
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)
 
131
        else:
 
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)
 
139
 
 
140
    def on_messagedialog1_response(self, widget, data=None):
 
141
        widget.hide()
 
142
 
 
143
    def _reset_shortener(self):
 
144
        self.builder.get_object('togglebuttonShorten').set_active(False)
 
145
 
 
146
    def _shorten_and_display(self):
 
147
        self.entry.set_sensitive(True)
 
148
        s = self.Shortener(url=self.address)
 
149
        try:
 
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)
 
155
            return()
 
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)
 
160
            return()
 
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)
 
166
 
44
167
    def on_activated(self):
45
168
        pass
46
169
 
53
176
        if not protocol or www == '':
54
177
            return
55
178
 
56
 
        self.qr_code_update_func(protocol + www)
 
179
        self.address = protocol + www
 
180
 
 
181
        self.qr_code_update_func(self.address)
 
182
        return False
57
183
 
58
184
    def on_entryURL_icon_press(self, widget, icon, mouse_button):
59
185
        clear_text_entry(widget, icon)
73
199
    def on_entryURL_changed(self, widget, data=None):
74
200
        show_clear_icon(widget)
75
201
        self.check_and_remove_protocol(widget)
76
 
        self.update_url_qr_code(www=widget.get_text())
 
202
        self.update_url_qr_code()
77
203
 
78
204
    def on_comboboxtextProtocol_changed(self, widget, data=None):
79
205
        self.update_url_qr_code(protocol=widget.get_active_text())