1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
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.
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.
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/>.
17
from gi.repository import Gtk, Gdk # pylint: disable=E0611
18
from variety.WallbaseDownloader import WallbaseDownloader
20
from variety_lib.helpers import get_builder
23
from gettext import gettext as _
24
gettext.textdomain('variety')
29
class AddWallbaseDialog(Gtk.Dialog):
30
__gtype_name__ = "AddWallbaseDialog"
33
"""Special static method that's automatically called by Python when
34
constructing a new instance of this class.
36
Returns a fully instantiated AddWallbaseDialog object.
38
builder = get_builder('AddWallbaseDialog')
39
new_object = builder.get_object('add_wallbase_dialog')
40
new_object.finish_initializing(builder)
43
def finish_initializing(self, builder):
44
"""Called when we're finished initializing.
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
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
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)
61
self.ui.radio_all.set_active(True)
62
self.ui.order_random.set_active(True)
64
def set_edited_row(self, edited_row):
65
self.edited_row = edited_row
67
location = edited_row[2]
68
s = location.split(';')
71
if len(x) and x.find(':') > 0:
73
params[k.lower()] = urllib.unquote_plus(v)
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))
83
self.ui.radio_all.set_active(True)
85
if params["order"] == "random":
86
self.ui.order_random.set_active(True)
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)
94
if params["nsfw"] == "110":
95
self.ui.sfw.set_active(2)
96
elif params["nsfw"] == "010":
97
self.ui.sfw.set_active(1)
99
self.ui.sfw.set_active(0)
101
if params["board"] == "2":
102
self.ui.manga.set_active(0)
103
elif params["board"] == "1":
104
self.ui.manga.set_active(1)
106
self.ui.manga.set_active(2)
108
def on_btn_ok_clicked(self, widget, data=None):
109
"""The user has elected to save the changes.
111
Called before the dialog returns Gtk.ResponseType.OK from run().
113
threading.Timer(0.1, self.ok_thread).start()
115
def on_radio_toggled(self, widget = None):
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())
121
def on_color_set(self, widget = None):
122
self.ui.radio_color.set_active(True)
124
def on_query_changed(self, widget = None):
125
self.ui.radio_text.set_active(True)
127
def on_favs_count_changed(self, widget = None):
128
self.ui.order_favs.set_active(True)
130
def show_spinner(self):
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("")
144
if self.ui.radio_text.get_active():
145
search += "type:text;"
146
query = self.ui.query.get_text().strip()
150
self.ui.query_error.set_visible(True)
154
search += "query:" + urllib.quote_plus(query) + ";"
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 + ";"
163
search += "type:all;"
165
if self.ui.order_favs.get_active():
166
search += "order:favs;favs_count:%d;" % int(self.ui.favs_count.get_active_text())
168
search += "order:random;"
170
if self.ui.sfw.get_active() == 0:
171
search += "nsfw:100;"
172
elif self.ui.sfw.get_active() == 1:
173
search += "nsfw:010;"
175
search += "nsfw:110;"
177
if self.ui.manga.get_active() == 0:
179
elif self.ui.manga.get_active() == 1:
186
if not WallbaseDownloader.validate(search):
187
self.error = _("No images found")
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)
197
if len(self.error) > 0:
198
self.ui.error.set_label(self.error)
199
self.ui.query.grab_focus()
202
self.parent.on_wallbase_dialog_okay(search, self.edited_row)
208
def on_btn_cancel_clicked(self, widget, data=None):
209
"""The user has elected cancel changes.
211
Called before the dialog returns Gtk.ResponseType.CANCEL for run()
215
if __name__ == "__main__":
216
dialog = AddWallbaseDialog()