~mvo/software-center/qml

« back to all changes in this revision

Viewing changes to softwarecenter/ui/gtk/widgets/weblivedialog.py

  • Committer: Michael Vogt
  • Date: 2011-10-05 13:08:09 UTC
  • mfrom: (1887.1.603 software-center)
  • Revision ID: michael.vogt@ubuntu.com-20111005130809-0tin9nr00f0uw65b
mergedĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2011 Canonical
2
 
#
3
 
# Authors:
4
 
#  Michael Vogt
5
 
#  Stephane Graber
6
 
#
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.
10
 
#
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
14
 
# details.
15
 
#
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
19
 
 
20
 
import gtk
21
 
import sys
22
 
 
23
 
from gettext import gettext as _
24
 
 
25
 
class ShowWebLiveServerChooserDialog(gtk.Dialog):
26
 
    """A dialog to choose between multiple server"""
27
 
 
28
 
    def __init__(self, supplied_servers, pkgname, parent=None):
29
 
        gtk.Dialog.__init__(self)
30
 
        self.set_has_separator(False)
31
 
 
32
 
        # find parent window for the dialog
33
 
        if not parent:
34
 
            parent = self.get_parent()
35
 
            while parent:
36
 
                parent = parent.get_parent()
37
 
 
38
 
        # servers
39
 
        self.servers_vbox=gtk.VBox(False, 0)
40
 
 
41
 
        # Merge duplicate servers, keep the one with the most space
42
 
        servers=[]
43
 
        for server in supplied_servers:
44
 
            duplicate=False
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
52
 
 
53
 
                    for package in otherserver.packages:
54
 
                        if package.pkgname == pkgname:
55
 
                            autoinstall_otherserver=package.autoinstall
56
 
 
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)
66
 
                    duplicate=True
67
 
 
68
 
            if duplicate:
69
 
                continue
70
 
 
71
 
            servers.append(server)
72
 
 
73
 
        if len(servers) == 1:
74
 
            self.show_dialog=False
75
 
        else:
76
 
            self.show_dialog=True
77
 
 
78
 
        button=None
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)
83
 
 
84
 
        # dialog
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)
93
 
 
94
 
    def run(self):
95
 
        if self.show_dialog == False:
96
 
            return gtk.RESPONSE_OK
97
 
 
98
 
        self.show_all()
99
 
 
100
 
        # and run the real thing
101
 
        return gtk.Dialog.run(self)
102
 
 
103
 
if __name__ == "__main__":
104
 
    sys.path.append('../../../')
105
 
 
106
 
    from softwarecenter.backend.weblive_pristine import WebLive
107
 
    weblive=WebLive('https://weblive.stgraber.org/weblive/json',True)
108
 
    servers=weblive.list_everything()
109
 
 
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
115
 
                break
116
 
    d.destroy()