summaryrefslogtreecommitdiffstats
path: root/src/service-discovery-config.in
blob: b0aa57f5924b2ea3879c8d166fcea8b69cd649d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!@PYTHON@
# -*-python-*-
# Copyright (C) 2005 by Sebastien Estienne
#
# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "COPYING" in the source distribution for more information.
#
# $id$
#

import os
import sys

def error_msg(msg):
    d = gtk.MessageDialog(parent=None, flags=gtk.DIALOG_MODAL,
                          type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
    d.set_markup(msg)
    d.show_all()
    d.run()
    d.destroy()
    
try:
    import gtk
    import gobject
    import gconf
    import avahi
except ImportError, e:
    error_msg("A python module is missing.\n%s" % (e))
    sys.exit()

from avahi.SimpleGladeApp import SimpleGladeApp

(
    COLUMN_ENABLED,
    COLUMN_SERVICE,
    COLUMN_NAME,
    COLUMN_ACTION,
    COLUMN_EDITABLE
) = range(5)

glade_dir = "@interfacesdir@"

gc_options = "/apps/service-discovery-applet/options"
gc_services =  "/apps/service-discovery-applet/services"

class Config_window(SimpleGladeApp):
    def __init__(self, path="service-discovery-config.glade", root="sda-prefs-dialog", domain=None, **kwargs):
        path = os.path.join(glade_dir, path)
        SimpleGladeApp.__init__(self, path, root, domain, **kwargs)

    def __create_model(self):
        lstore = gtk.ListStore(
            gobject.TYPE_BOOLEAN,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_BOOLEAN,
            gobject.TYPE_BOOLEAN)

	gc_entries = self.gc_client.all_entries(gc_services)
	for gc_entry in gc_entries:
	    svc_enabled = self.gc_client.get_bool(gc_entry.key)
	    svc_type = os.path.basename(gc_entry.key)

            iter = lstore.append()
            lstore.set(iter,
		       COLUMN_ENABLED, svc_enabled,
		       COLUMN_SERVICE, svc_type,
		       COLUMN_NAME, None,
		       COLUMN_ACTION, False,
		       COLUMN_EDITABLE, True)
	return lstore

    def __add_columns(self, treeview):
        model = treeview.get_model()
	
        # column for enabled toggles
        renderer = gtk.CellRendererToggle()
        renderer.connect('toggled', self.enabled_toggled, model)

        column = gtk.TreeViewColumn('Enabled', renderer, active=COLUMN_ENABLED)

        # set this column to a fixed sizing(of 50 pixels)
        #column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
        #column.set_fixed_width(50)
        column.set_sort_column_id(COLUMN_ENABLED)
        treeview.append_column(column)

        # column for service type
	
        renderer = gtk.CellRendererText()
        renderer.connect("edited", self.on_cell_edited, model)
        renderer.set_data("column", COLUMN_SERVICE)

        column = gtk.TreeViewColumn('Service type', renderer,
                                    text=COLUMN_SERVICE, editable=COLUMN_EDITABLE)
        treeview.append_column(column)

        # columns for service name
#        column = gtk.TreeViewColumn('Service Name', gtk.CellRendererText(),
#                                    text=COLUMN_NAME)
#        column.set_sort_column_id(COLUMN_NAME)
#        treeview.append_column(column)

        # column for action scripts
#        renderer = gtk.CellRendererToggle()
#        column = gtk.TreeViewColumn('Action', renderer, active=COLUMN_ACTION)
#        column.set_sort_column_id(COLUMN_ACTION)
#        treeview.append_column(column)

    def on_cell_edited(self, cell, path_string, service_type, model):

        iter = model.get_iter_from_string(path_string)
        path = model.get_path(iter)[0]
        column = cell.get_data("column")

        if column == COLUMN_SERVICE:
	    old_service_type = model.get_value(iter, column)
	    old_service_enabled= model.get_value(iter, COLUMN_ENABLED)
	    self.gc_client.unset("%s/%s" % (gc_services, old_service_type))
	    self.gc_client.suggest_sync()
	    model.set(iter, column, service_type)
	    self.gc_client.set_bool("%s/%s" % (gc_services, service_type), old_service_enabled)

    def on_add_service_clicked(self, button, model):
        iter = model.append()
        model.set (iter,
		   COLUMN_ENABLED, False,
		   COLUMN_SERVICE, "_service._protocol",
		   COLUMN_NAME, None,
		   COLUMN_ACTION, False,
		   COLUMN_EDITABLE, True)

    def on_del_service_clicked(self, button, treeview):

        selection = treeview.get_selection()
        model, iter = selection.get_selected()

        if iter:
            path = model.get_path(iter)[0]
	    service_type = model.get_value(iter,COLUMN_SERVICE)
	    self.gc_client.unset("%s/%s" % (gc_services, service_type))
	    self.gc_client.suggest_sync()
            model.remove(iter)
	    #FIXME remove entry from gconf

    def enabled_toggled(self, cell, path, model):
        # get toggled iter
        iter = model.get_iter((int(path),))
        enabled = model.get_value(iter, COLUMN_ENABLED)
	service_type = model.get_value(iter, COLUMN_SERVICE)
        # do something with the value
        enabled = not enabled

        # set new value
        model.set(iter, COLUMN_ENABLED, enabled)

	self.gc_client.set_bool("%s/%s" % (gc_services, service_type), enabled)

    def cb_local_toggled(self,cb,client):
	client.set_bool ("%s/%s" % (gc_options,"show_local_services"), cb.get_active())

    def cb_notifications_toggled(self,cb,client):
	client.set_bool ("%s/%s" % (gc_options,"show_notifications"), cb.get_active())

    def new(self):
        print "A new main_window has been created"

	# Gconf
	self.gc_client = gconf.client_get_default ()
	self.gc_client.add_dir (gc_options, gconf.CLIENT_PRELOAD_NONE)
	
	# images
	self.image_services.set_from_file("@iconsdir@/48x48/service-discovery-applet.png")
	
	# connect advanced checkbuttons
	self.checkbutton_local.set_active(self.gc_client.get_bool ("%s/%s" % (gc_options,"show_local_services")))
	self.checkbutton_local.connect('toggled',self.cb_local_toggled,self.gc_client)

	self.checkbutton_notifications.set_active(self.gc_client.get_bool ("%s/%s" % (gc_options,"show_notifications")))
	self.checkbutton_notifications.connect('toggled',self.cb_notifications_toggled,self.gc_client)


	# The services treeview
	model = self.__create_model()

	self.treeview_services.set_model(model)
	self.__add_columns(self.treeview_services)

	# connect add/remove service buttons
	self.button_add_service.connect("clicked", self.on_add_service_clicked, model)
	self.button_del_service.connect("clicked", self.on_del_service_clicked, self.treeview_services)


def main():
    config_window = Config_window()

    config_window.run()
    
if __name__ == "__main__":
    main()