~dpm/qreator/snap

« back to all changes in this revision

Viewing changes to qreator/qrcodes/QRCodeURLGtk.py

  • Committer: David Planella
  • Date: 2012-11-23 14:07:08 UTC
  • mfrom: (107.1.1 qreator)
  • Revision ID: david.planella@ubuntu.com-20121123140708-ef2ny921lpwo99eq
Merged i18n fixes and updated .pot file

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
from qreator_lib.i18n import _
21
21
import requests
22
22
import json
23
 
from urllib import urlencode
24
23
 
25
24
 
26
25
class IsdgShortener(object):
 
26
    # TRANSLATORS: this refers to the is.dg URL shortening service
27
27
    name = _("Isdg")
 
28
 
28
29
    def __init__(self, url=None):
29
 
        self.api_url= "http://is.gd/create.php"
 
30
        self.api_url = "http://is.gd/create.php"
30
31
        self.url = url
31
32
 
32
33
    def short_url(self):
33
 
        self.r = requests.get("{0}?format=simple&url={1}".format(self.api_url, self.url))
 
34
        self.r = requests.get("{0}?format=simple&url={1}".format(self.api_url,
 
35
                                                                 self.url))
34
36
        if self.r.content.startswith("Error"):
35
37
            raise Exception(self.r.content)
36
38
        return self.r.content
37
39
 
38
40
 
39
41
class TinyUrlShortener(object):
 
42
    # TRANSLATORS: this refers to the tinyurl.com URL shortening service
40
43
    name = _("TinyUrl")
 
44
 
41
45
    def __init__(self, url=None):
42
46
        self.api_url = "http://tinyurl.com/api-create.php"
43
47
        self.url = url
48
52
 
49
53
 
50
54
class BitlyShortener(object):
 
55
    # TRANSLATORS: this refers to the bit.ly URL shortening service
51
56
    name = _("Bitly")
 
57
 
52
58
    def __init__(self, url=None):
53
59
        self.api_url = 'http://api.bit.ly/v3/shorten'
54
60
        self.url = url
55
 
        self.data = {"login": "", # TODO
56
 
                     "apiKey": "", # TODO
 
61
        self.data = {"login": "",  # TODO
 
62
                     "apiKey": "",  # TODO
57
63
                     "longUrl": self.url}
58
64
 
59
65
    def short_url(self):
63
69
 
64
70
 
65
71
class GoogleShortener(object):
 
72
    # TRANSLATORS: this refers to the goo.gl URL shortening service
66
73
    name = _("Google")
 
74
 
67
75
    def __init__(self, url=None):
68
76
        self.api_key = "AIzaSyCU_pFNpACW4luWEZRgNnfWMEUdlnZyYQI"
69
77
        self.api_url = 'https://www.googleapis.com/urlshortener/v1/url'
70
78
        self.url = url
71
79
        self.params = {"key": self.api_key,
72
 
                       'Content-Type' : 'application/json'}
 
80
                       'Content-Type': 'application/json'}
73
81
        self.data = {"longUrl": self.url}
74
82
 
75
83
    def short_url(self):
76
 
        self.r = requests.post(self.api_url, headers=self.params, data=json.dumps(self.data))
 
84
        self.r = requests.post(self.api_url, headers=self.params,
 
85
                               data=json.dumps(self.data))
77
86
        self.results = json.loads(self.r.content)
78
87
        return self.results["id"]
79
88
 
80
89
 
81
 
SHORTENER_TYPES = [IsdgShortener, TinyUrlShortener, BitlyShortener, GoogleShortener,]
 
90
SHORTENER_TYPES = [
 
91
    IsdgShortener,
 
92
    TinyUrlShortener,
 
93
    BitlyShortener,
 
94
    GoogleShortener,
 
95
    ]
82
96
 
83
97
 
84
98
class QRCodeURLGtk(object):
101
115
        self.entry.set_placeholder_text(_('[URL]'))
102
116
        self.combobox.set_active(0)
103
117
 
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.
 
118
        # Currently four shortener options are available: isdg, google,
 
119
        # tinyurl, bitly
 
120
        # We are choosing isdg at the moment. Later we will allow to define
 
121
        # this in preferences.
107
122
        self.Shortener = SHORTENER_TYPES[0]
108
123
 
 
124
        # TRANSLATORS: leave '{0}' as it is, it will be replaced by the
 
125
        # chosen URL shortening service
109
126
        self.builder.get_object("togglebuttonShorten").set_tooltip_text(
110
 
            "Use the {0} online service to generate a short URL.".format(self.Shortener.name))
 
127
            "Use the {0} online service to generate a short URL.".format(
 
128
                self.Shortener.name))
111
129
        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)
 
130
 
 
131
        self.entryhandler = self.entry.connect("changed",
 
132
                                               self.on_entryURL_changed)
 
133
        # Moved this signal from the glade file to here, to be able to block
 
134
        # it later
 
135
        self.iconhandler = self.entry.connect("icon-press",
 
136
                                              self.on_entryURL_icon_press)
116
137
        self.combobox.connect("changed", self.on_comboboxtextProtocol_changed)
117
138
 
118
139
    def on_togglebuttonShorten_toggled(self, widget):
124
145
            self.entry.handler_block(self.iconhandler)
125
146
            self.combobox.set_sensitive(False)
126
147
            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
 
148
            self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY,
 
149
                                            False)
 
150
            # If the request takes longer, the interface should in the
 
151
            # meantime change to unsensitive button and entry, to show at
 
152
            # least some behaviour
130
153
            GObject.idle_add(self._shorten_and_display)
131
154
        else:
132
155
            self.entry.set_has_tooltip(False)
135
158
            self.entry.handler_unblock(self.iconhandler)
136
159
            self.combobox.set_sensitive(True)
137
160
            self.qr_code_update_func(self.address)
138
 
            self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY, True)
 
161
            self.entry.set_icon_activatable(Gtk.EntryIconPosition.SECONDARY,
 
162
                                            True)
139
163
 
140
164
    def on_messagedialog1_response(self, widget, data=None):
141
165
        widget.hide()
149
173
        try:
150
174
            self.short_address = s.short_url()
151
175
        except requests.exceptions.ConnectionError as e:
152
 
            self.messagedialog.set_markup(_("A network connection error occured: {0}".format(e)))
 
176
            # TRANSLATORS: leave '{0}' as it is, it will be replaced by the
 
177
            # exception's error message
 
178
            self.messagedialog.set_markup(
 
179
                _("A network connection error occured: {0}".format(e)))
153
180
            self.messagedialog.show()
154
181
            GObject.idle_add(self._reset_shortener)
155
182
            return()
156
183
        except Exception as e:
157
 
            self.messagedialog.set_markup(_("An error occured while trying to shorten the url: {0}".format(e)))
 
184
            # TRANSLATORS: leave '{0}' as it is, it will be replaced by the
 
185
            # exception's error message
 
186
            self.messagedialog.set_markup(
 
187
                _("An error occured while trying to shorten the URL: {0}".format(e)))   # pylint: disable=E0501
158
188
            self.messagedialog.show()
159
189
            GObject.idle_add(self._reset_shortener)
160
190
            return()
190
220
    def check_and_remove_protocol(self, widget):
191
221
        """
192
222
        Creates a list of protocols (as given by the combobox).
193
 
        Checks if beginning of the text entry is in that list. If so, remove the protocol
194
 
        and select the appropriate protocol in the combobox.
 
223
        Checks if the beginning of the text entry is in that list. If so,
 
224
        remove the protocol and select the appropriate protocol in the
 
225
        combobox.
195
226
        """
196
 
        for n, protocol in enumerate([mr[0] for mr in self.combobox.get_model()]):
 
227
        for n, protocol in enumerate(
 
228
            [mr[0] for mr in self.combobox.get_model()]):
197
229
            if widget.get_text().strip().startswith(protocol):
198
230
                widget.set_text(widget.get_text().strip().strip(protocol))
199
231
                self.combobox.set_active(n)