1
# Copyright (C) 2011 Canonical
7
# This program is free software; you can redistribute it and/or modify it under
8
# the terms of the GNU General Public License as published by the Free Software
9
# Foundation; version 3.
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
from gettext import gettext as _
25
class ShowWebLiveServerChooserDialog(gtk.Dialog):
26
"""A dialog to choose between multiple server"""
28
def __init__(self, supplied_servers, pkgname, parent=None):
29
gtk.Dialog.__init__(self)
30
self.set_has_separator(False)
32
# find parent window for the dialog
34
parent = self.get_parent()
36
parent = parent.get_parent()
39
self.servers_vbox=gtk.VBox(False, 0)
41
# Merge duplicate servers, keep the one with the most space
43
for server in supplied_servers:
45
for otherserver in servers:
46
if server.title == otherserver.title:
47
percent_server=((float(server.current_users)/float(server.userlimit))*100.0)
48
percent_otherserver=((float(otherserver.current_users)/float(otherserver.userlimit))*100.0)
49
for package in server.packages:
50
if package.pkgname == pkgname:
51
autoinstall_server=package.autoinstall
53
for package in otherserver.packages:
54
if package.pkgname == pkgname:
55
autoinstall_otherserver=package.autoinstall
57
# Replace existing server if:
58
# current server has more free slots and we don't switch
59
# to a server requiring autoinstall
60
# or doesn't need autoinstall but existing one does
61
if ( (percent_otherserver > percent_server and
62
not autoinstall_otherserver < autoinstall_server) or
63
autoinstall_otherserver > autoinstall_server ):
64
servers.remove(otherserver)
65
servers.append(server)
71
servers.append(server)
74
self.show_dialog=False
79
for server in sorted(servers, key=lambda server: server.title):
80
button=gtk.RadioButton(button, "%s - %s" % (server.title, server.description))
81
button.serverid=server.name
82
self.servers_vbox.pack_start(button, True, True, 0)
85
self.set_transient_for(parent)
86
self.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
87
self.get_content_area().add(self.servers_vbox)
88
self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
89
self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
90
self.set_resizable(False)
91
self.set_title(_("Choose your distribution"))
92
self.set_border_width(8)
95
if self.show_dialog == False:
96
return gtk.RESPONSE_OK
100
# and run the real thing
101
return gtk.Dialog.run(self)
103
if __name__ == "__main__":
104
sys.path.append('../../../')
106
from softwarecenter.backend.weblive_pristine import WebLive
107
weblive=WebLive('https://weblive.stgraber.org/weblive/json',True)
108
servers=weblive.list_everything()
110
d = ShowWebLiveServerChooserDialog(servers)
111
if d.run() == gtk.RESPONSE_OK:
112
for server in d.servers_vbox:
113
if server.get_active():
114
print server.serverid