~versable/elementary-community/variety

« back to all changes in this revision

Viewing changes to variety/AddWallbaseDialog.py

  • Committer: Versable
  • Date: 2013-06-18 11:21:16 UTC
  • Revision ID: versable@gmail.com-20130618112116-1zevbh7bsfxvc7cq
Clean up for merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
### BEGIN LICENSE
3
 
# Copyright (c) 2012, Peter Levi <peterlevi@peterlevi.com>
4
 
# This program is free software: you can redistribute it and/or modify it 
5
 
# under the terms of the GNU General Public License version 3, as published 
6
 
# by the Free Software Foundation.
7
 
8
 
# This program is distributed in the hope that it will be useful, but 
9
 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
10
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
11
 
# PURPOSE.  See the GNU General Public License for more details.
12
 
13
 
# You should have received a copy of the GNU General Public License along 
14
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
### END LICENSE
16
 
 
17
 
from gi.repository import Gtk, Gdk # pylint: disable=E0611
18
 
from variety.WallbaseDownloader import WallbaseDownloader
19
 
 
20
 
from variety_lib.helpers import get_builder
21
 
 
22
 
import gettext
23
 
from gettext import gettext as _
24
 
gettext.textdomain('variety')
25
 
 
26
 
import threading
27
 
import urllib
28
 
 
29
 
class AddWallbaseDialog(Gtk.Dialog):
30
 
    __gtype_name__ = "AddWallbaseDialog"
31
 
 
32
 
    def __new__(cls):
33
 
        """Special static method that's automatically called by Python when 
34
 
        constructing a new instance of this class.
35
 
        
36
 
        Returns a fully instantiated AddWallbaseDialog object.
37
 
        """
38
 
        builder = get_builder('AddWallbaseDialog')
39
 
        new_object = builder.get_object('add_wallbase_dialog')
40
 
        new_object.finish_initializing(builder)
41
 
        return new_object
42
 
 
43
 
    def finish_initializing(self, builder):
44
 
        """Called when we're finished initializing.
45
 
 
46
 
        finish_initalizing should be called after parsing the ui definition
47
 
        and creating a AddWallbaseDialog object with it in order to
48
 
        finish initializing the start of the new AddWallbaseDialog
49
 
        instance.
50
 
        """
51
 
        # Get a reference to the builder and set up the signals.
52
 
        self.builder = builder
53
 
        self.ui = builder.get_ui(self)
54
 
        self.edited_row = None
55
 
 
56
 
        self.on_radio_toggled()
57
 
        self.ui.sfw.set_active(0)
58
 
        self.ui.manga.set_active(2)
59
 
        self.ui.favs_count.set_active(1)
60
 
 
61
 
        self.ui.radio_all.set_active(True)
62
 
        self.ui.order_random.set_active(True)
63
 
 
64
 
    def set_edited_row(self, edited_row):
65
 
        self.edited_row = edited_row
66
 
 
67
 
        location = edited_row[2]
68
 
        s = location.split(';')
69
 
        params = {}
70
 
        for x in s:
71
 
            if len(x) and x.find(':') > 0:
72
 
                k, v = x.split(':')
73
 
                params[k.lower()] = urllib.unquote_plus(v)
74
 
 
75
 
        if params["type"] == "text":
76
 
            self.ui.radio_text.set_active(True)
77
 
            self.ui.query.set_text(urllib.unquote_plus(params["query"]))
78
 
        elif params["type"] == "color":
79
 
            self.ui.radio_color.set_active(True)
80
 
            c = map(int, params["color"].split('/'))
81
 
            self.ui.color.set_color(Gdk.Color(red = c[0] * 256, green = c[1] * 256, blue = c[2] * 256))
82
 
        else:
83
 
            self.ui.radio_all.set_active(True)
84
 
 
85
 
        if params["order"] == "random":
86
 
            self.ui.order_random.set_active(True)
87
 
        else:
88
 
            self.ui.order_favs.set_active(True)
89
 
            for i, x in enumerate(self.ui.favs_count.get_model()):
90
 
                if int(x[0]) >= int(params["favs_count"]):
91
 
                    self.ui.favs_count.set_active(i)
92
 
                    break
93
 
 
94
 
        if params["nsfw"] == "110":
95
 
            self.ui.sfw.set_active(2)
96
 
        elif params["nsfw"] == "010":
97
 
            self.ui.sfw.set_active(1)
98
 
        else:
99
 
            self.ui.sfw.set_active(0)
100
 
 
101
 
        if params["board"] == "2":
102
 
            self.ui.manga.set_active(0)
103
 
        elif params["board"] == "1":
104
 
            self.ui.manga.set_active(1)
105
 
        else:
106
 
            self.ui.manga.set_active(2)
107
 
 
108
 
    def on_btn_ok_clicked(self, widget, data=None):
109
 
        """The user has elected to save the changes.
110
 
 
111
 
        Called before the dialog returns Gtk.ResponseType.OK from run().
112
 
        """
113
 
        threading.Timer(0.1, self.ok_thread).start()
114
 
 
115
 
    def on_radio_toggled(self, widget = None):
116
 
        pass
117
 
#        self.ui.query.set_sensitive(self.ui.radio_text.get_active())
118
 
#        self.ui.color.set_sensitive(self.ui.radio_color.get_active())
119
 
#        self.ui.favs_count.set_sensitive(self.ui.order_favs.get_active())
120
 
 
121
 
    def on_color_set(self, widget = None):
122
 
        self.ui.radio_color.set_active(True)
123
 
 
124
 
    def on_query_changed(self, widget = None):
125
 
        self.ui.radio_text.set_active(True)
126
 
 
127
 
    def on_favs_count_changed(self, widget = None):
128
 
        self.ui.order_favs.set_active(True)
129
 
 
130
 
    def show_spinner(self):
131
 
        try:
132
 
            Gdk.threads_enter()
133
 
            self.ui.buttonbox.set_sensitive(False)
134
 
            self.ui.message.set_visible(True)
135
 
            self.ui.spinner.set_visible(True)
136
 
            self.ui.spinner.start()
137
 
            self.ui.error.set_label("")
138
 
        finally:
139
 
            Gdk.threads_leave()
140
 
 
141
 
    def ok_thread(self):
142
 
        search = ""
143
 
 
144
 
        if self.ui.radio_text.get_active():
145
 
            search += "type:text;"
146
 
            query = self.ui.query.get_text().strip()
147
 
            if not len(query):
148
 
                try:
149
 
                    Gdk.threads_enter()
150
 
                    self.ui.query_error.set_visible(True)
151
 
                finally:
152
 
                    Gdk.threads_leave()
153
 
                return
154
 
            search += "query:" + urllib.quote_plus(query) + ";"
155
 
 
156
 
        elif self.ui.radio_color.get_active():
157
 
            search += "type:color;"
158
 
            c = self.ui.color.get_color()
159
 
            color = "%d/%d/%d" % (c.red // 256, c.green // 256, c.blue // 256)
160
 
            search += "color:" + color + ";"
161
 
 
162
 
        else:
163
 
            search += "type:all;"
164
 
 
165
 
        if self.ui.order_favs.get_active():
166
 
            search += "order:favs;favs_count:%d;" % int(self.ui.favs_count.get_active_text())
167
 
        else:
168
 
            search += "order:random;"
169
 
 
170
 
        if self.ui.sfw.get_active() == 0:
171
 
            search += "nsfw:100;"
172
 
        elif self.ui.sfw.get_active() == 1:
173
 
            search += "nsfw:010;"
174
 
        else:
175
 
            search += "nsfw:110;"
176
 
 
177
 
        if self.ui.manga.get_active() == 0:
178
 
            search += "board:2"
179
 
        elif self.ui.manga.get_active() == 1:
180
 
            search += "board:1"
181
 
        else:
182
 
            search += "board:12"
183
 
 
184
 
        self.error = ""
185
 
        self.show_spinner()
186
 
        if not WallbaseDownloader.validate(search):
187
 
            self.error = _("No images found")
188
 
 
189
 
        try:
190
 
            Gdk.threads_enter()
191
 
 
192
 
            self.ui.buttonbox.set_sensitive(True)
193
 
            self.ui.spinner.stop()
194
 
            self.ui.spinner.set_visible(False)
195
 
            self.ui.message.set_visible(False)
196
 
 
197
 
            if len(self.error) > 0:
198
 
                self.ui.error.set_label(self.error)
199
 
                self.ui.query.grab_focus()
200
 
            else:
201
 
                if len(search):
202
 
                    self.parent.on_wallbase_dialog_okay(search, self.edited_row)
203
 
                self.destroy()
204
 
 
205
 
        finally:
206
 
            Gdk.threads_leave()
207
 
 
208
 
    def on_btn_cancel_clicked(self, widget, data=None):
209
 
        """The user has elected cancel changes.
210
 
 
211
 
        Called before the dialog returns Gtk.ResponseType.CANCEL for run()
212
 
        """
213
 
        self.destroy()
214
 
 
215
 
if __name__ == "__main__":
216
 
    dialog = AddWallbaseDialog()
217
 
    dialog.show()
218
 
    Gtk.main()