summaryrefslogtreecommitdiffstats
path: root/frontend/frontend_gtk.py
blob: 7bdfc318003da5cf99834f9010d3c53209f6fcad (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File name: main.py
# Date:      2008/04/21
# Author:    Martin Sivak <msivak at redhat dot com>
#
# Copyright (C) Red Hat 2008
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# in a file called COPYING along with this program; if not, write to
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
# 02139, USA.

import gtk
import gtk.glade
import gobject #we need gobject.idle_add
import copy
import logging
from pyfirstaidkit import reporting
import pprint
import os.path
import thread
import hashlib

def _o(func, *args, **kwargs):
    """Always return False -> remove from the idle queue after first
    execution"""
    
    func(*args, **kwargs)
    return False


class CallbacksMainWindow(object):
    def __init__(self, dialog, cfg, tasker, glade, data):
        self._dialog = dialog
        self._tasker = tasker
        self._glade = glade
        self._cfg = cfg
        self._data = data
        self._running_lock = thread.allocate_lock()

    def execute(self):
        if not self._running_lock.acquire(0):
            return

        def _o2(pages, stopbutton):
            """Always return False -> remove from the idle queue after first
            execution"""
            
            for i in range(pages.get_n_pages()):
                pages.get_nth_page(i).set_sensitive(True)
            stopbutton.set_sensitive(False)
            
            return False

        def worker(*args):
            self._cfg.lock()
            self._tasker.run()
            self._cfg.unlock()
            gobject.idle_add(_o2, *args)
            self._running_lock.release()

        self._data.pages.set_current_page(-1)
        for i in range(self._data.pages.get_n_pages())[:-1]:
            self._data.pages.get_nth_page(i).set_sensitive(False)
        self.on_b_ResetResults_activate(None)

        stopbutton = self._glade.get_widget("b_StopResults")
        stopbutton.set_sensitive(True)
        thread.start_new_thread(worker, (self._data.pages, stopbutton))

    #menu callbacks
    def on_mainmenu_open_activate(self, widget, *args):
        print("on_mainmenu_open_activate")
        d = gtk.FileChooserDialog(title="Load the configuration file",
                parent=self._dialog, action=gtk.FILE_CHOOSER_ACTION_OPEN,
                buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        print(d.run())
        d.destroy()
        return True

    def on_mainmenu_save_activate(self, widget, *args):
        print("on_mainmenu_save_activate")
        d = gtk.FileChooserDialog(title="Save the configuration file",
                parent=self._dialog, action=gtk.FILE_CHOOSER_ACTION_SAVE,
                buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK,
                    gtk.RESPONSE_ACCEPT))
        ret=d.run()

        if ret==gtk.RESPONSE_ACCEPT:
            try:
                filename = d.get_filename()
                fd = open(filename, "w")
                self._cfg.write(fd)
            except IOError, e:
                pass

        d.destroy()
        return True

    def on_quit_activate(self, widget, *args):
        print("on_quit_activate")
        #XXX destroy right now, but warn, in final code we have to wait until
        #plugin finishes
        if True:
            print("!!! You should wait until the running plugin finishes!!")
            self._dialog.destroy()
        return True

    def on_destroy(self, widget, *args):
        print("on_destroy")
        self._tasker.end()
        del self._tasker
        del self._cfg
        del self._data.result_list_iter
        gtk.main_quit()

    def on_mainmenu_about_activate(self, widget, *args):
        print("on_mainmenu_about_activate")
        AboutDialog(self._cfg,
                dir = os.path.dirname(self._glade.relative_file(".")))
        return True

    def on_mainmenu_expert_activate(self, widget, *args):
        self._glade.get_widget("expert_page").show()
        self._data.pages.set_current_page(1)
        return True
        
    #advanced mode callbacks
    def on_b_StartAdvanced_activate(self, widget, *args):
        print("on_b_StartAdvanced_activate")

        flags = set(self._cfg.operation._list("flags"))

        #set the auto-flow
        self._cfg.operation.mode = "auto-flow"

        idx = self._data.flow_list.get_active_iter()
        if idx is None:
            return True
        self._cfg.operation.flow = self._data.flow_list_store.get_value(idx,0)

        #check verbose
        if self._glade.get_widget("check_Advanced_Verbose").get_active():
            self._cfg.operation.verbose = "True"
        else:
            self._cfg.operation.verbose = "False"

        #check experimental
        if self._glade.get_widget("check_Advanced_Experimental").get_active():
            flags.add("experimental")
        else:
            try:
                flags.remove("experimental")
            except KeyError, e:
                pass

        #check interactive
        if self._glade.get_widget("check_Advanced_Interactive").get_active():
            self._cfg.operation.interactive = "True"
        else:
            self._cfg.operation.interactive = "False"

        #check dependency
        if self._glade.get_widget("check_Advanced_NoDependency").get_active():
            self._cfg.operation.dependencies = "False"
        else:
            self._cfg.operation.dependencies = "True"

        self._cfg.operation.flags = " ".join(
                map(lambda x: x.encode("string-escape"), flags))

        #reset params
        if self._cfg.has_section("plugin-args"):
            self._cfg.remove_section("plugin-args")
        self._cfg.add_section("plugin-args")

        self.execute()
        return True

    #expert mode callbacks
    def on_b_FlagsExpert_activate(self, widget, *args):
        print("on_b_FlagsExpert_activate")
        FlagList(self._cfg, self._tasker.flags(),
                dir = os.path.dirname(self._glade.relative_file(".")))
        return True

    def on_b_InfoExpert_activate(self, widget, *args):
        print("on_b_InfoExpert_activate")

        path,focus = self._data.plugin_list.get_cursor()
        if path is None:
            return True

        iter = self._data.plugin_list_store.get_iter(path)
        pluginname = self._data.plugin_list_store.get_value(iter, 4)
        print("Selected: %s"% pluginname)

        PluginInfo(self._tasker.pluginsystem().getplugin(pluginname),
                dir = os.path.dirname(self._glade.relative_file(".")))

        return True

    def on_b_StartExpert_activate(self, widget, *args):
        print("on_b_StartExpert_activate")

        #check verbose
        if self._glade.get_widget("check_Expert_Verbose").get_active():
            self._cfg.operation.verbose = "True"
        else:
            self._cfg.operation.verbose = "False"

        #check interactive
        if self._glade.get_widget("check_Expert_Interactive").get_active():
            self._cfg.operation.interactive = "True"
        else:
            self._cfg.operation.interactive = "False"

        #check dependency
        if self._glade.get_widget("check_Expert_NoDependency").get_active():
            self._cfg.operation.dependencies = "False"
        else:
            self._cfg.operation.dependencies = "True"

        #get the plugin & flow list
        plugins = []
        flows = []

        #reset params
        if self._cfg.has_section("plugin-args"):
            self._cfg.remove_section("plugin-args")
        self._cfg.add_section("plugin-args")

        for pname,iter in self._data.plugin_iter.iteritems():
            #get/set plugin params
            param = self._data.plugin_list_store.get_value(iter, 3)
            if param.strip()!="":
                val = "%s %s" % (pname, param)
                self._cfg.set("plugin-args", hashlib.sha1(val).hexdigest(), val)

            #get list of flows
            childiter = self._data.plugin_list_store.iter_children(iter)
            while childiter is not None:
                #checkbox is checked
                if self._data.plugin_list_store.get_value(childiter, 0):
                    plugins.append(pname)
                    fname = self._data.plugin_list_store.get_value(
                        childiter, 1)
                    flows.append(fname)

                    #get/set flow params
                    param = self._data.plugin_list_store.get_value(childiter, 3)
                    if param.strip()!="":
                        val = "%s/%s %s" % (pname, fname, param)
                        self._cfg.set("plugin-args",
                                hashlib.sha1(val).hexdigest(), val)
                childiter = self._data.plugin_list_store.iter_next(childiter)

        plugins = map(lambda x: x.encode("string-escape"), plugins)
        flows = map(lambda x: x.encode("string-escape"), flows)

        #set the flow mode
        self._cfg.operation.mode = "flow"
        self._cfg.operation.flow = " ".join(flows)
        self._cfg.operation.plugin = " ".join(plugins)

        self.execute()
        return True

    #results callbacks
    def on_b_ResetResults_activate(self, widget, *args):
        print("on_b_ResetResults_activate")
        self._data.result_list_store.clear()
        del self._data.result_list_iter
        self._data.result_list_iter = dict()
        return True

    def on_b_StopResults_activate(self, widget, *args):
        print("on_b_StopResults_activate")
        self._tasker.interrupt()
        return True

class CallbacksFlagList(object):
    def __init__(self, dialog, cfg, flags):
        self._dialog = dialog
        self._flags = flags
        self._cfg = cfg

    def on_b_OK_activate(self, widget, *args):
        print("on_b_OK_activate")

        f = set()
        for k,w in self._flags.iteritems():
            if w.get_active():
                f.add(k)

        if len(f)==0:
            self._cfg.operation.flags = ""
        else:
            self._cfg.operation.flags = " ".join(
                    map(lambda x: x.encode("string-escape"), f))

        self._dialog.destroy()
        return True

    def on_b_Cancel_activate(self, widget, *args):
        print("on_b_Cancel_activate")
        self._dialog.destroy()
        return True

class ListDialog(object):
    def __init__(self, title, description, items, dir=""):
        gtkb = gtk.Builder()
        gtkb.add_from_file(os.path.join(dir, "gtk-list.xml"))
        self._dialog = gtkb.get_object("listdialog")
        self._dialog.set_title(title)
        self._store = gtk.ListStore(gobject.TYPE_STRING,
                                    gobject.TYPE_STRING,
                                    gobject.TYPE_STRING,
                                    gobject.TYPE_STRING,
                                    gobject.TYPE_PYOBJECT,
                                    gobject.TYPE_STRING)
        self._label = gtkb.get_object("label")
        self._label.set_text(description)
        self._view = gtkb.get_object("view")
        self._view.set_model(self._store)

        rend_text = gtk.CellRendererText()
        rend_text_edit = gtk.CellRendererText()
        rend_text_edit.set_property("editable", True)
        rend_text_edit.connect('edited', self.edited_cb, self._store)

        col_0 = gtk.TreeViewColumn('Key', rend_text, text = 1)
        col_0.set_resizable(True)
        col_0.set_expand(False)
        col_1 = gtk.TreeViewColumn('Value', rend_text_edit, text = 2)
        col_1.set_resizable(True)
        col_1.set_expand(True)
        self._view.append_column(col_0)
        self._view.append_column(col_1)

        map(self._store.append, items)

        gtkb.connect_signals(self)

    def items(self):
        F = lambda row: (row[0], row[2])
        return map(F, self._store)

    def run(self):
        self._dialog.show_all()
        return self._dialog.run()

    def edited_cb(self, cell, path, new_data, store):
        res = store[path][4].match(new_data)
        if res is not None:
            store[path][2] = new_data
        else:
            err = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
            err.set_property("text", store[path][5])
            err.run()
            err.destroy()

    def destroy(self):
        self._dialog.destroy()

    def cb_ok(self, data):
        self._dialog.response(gtk.RESPONSE_ACCEPT)

    def cb_cancel(self, data):
        self._dialog.response(gtk.RESPONSE_CANCEL)

    def cb_close(self, data):
        self._dialog.response(gtk.RESPONSE_CANCEL)

class MainWindow(object):
    _cancel_answer = object()
    _no_answer = object()

    def __init__(self, cfg, tasker, importance = logging.INFO, dir=""):
        self._importance = importance
        self._cfg = cfg
        self._glade = gtk.glade.XML(os.path.join(dir, "firstaidkit.glade"),
                "MainWindow")
        self._window = self._glade.get_widget("MainWindow")
        self._cb = CallbacksMainWindow(self._window, cfg, tasker, self._glade,
                self)
        self._glade.signal_autoconnect(self._cb)
        self._window.connect("destroy", self._cb.on_destroy)

        self.pages = self._glade.get_widget("pages")
        self.status_text = self._glade.get_widget("status_text")
        self.status_progress = self._glade.get_widget("status_progress")

        self.plugin_list_store = gtk.TreeStore(gobject.TYPE_BOOLEAN,
                gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING,
                gobject.TYPE_STRING)
        self.plugin_list = self._glade.get_widget("tree_Expert")
        self.plugin_list.set_model(self.plugin_list_store)

        self.plugin_rend_text = gtk.CellRendererText()
        self.plugin_rend_text_edit = gtk.CellRendererText()
        self.plugin_rend_text_edit.set_property("editable", True)
        self.plugin_rend_toggle = gtk.CellRendererToggle()
        self.plugin_rend_toggle.set_radio(False)
        self.plugin_rend_toggle.set_property("activatable", False)

        def plugin_rend_text_func(column, cell_renderer, tree_model, iter,
                user_data):
            if tree_model.iter_depth(iter)==0:
                cell_renderer.set_property("cell-background-set", True)
                cell_renderer.set_property("cell-background-gdk",
                        gtk.gdk.Color(red=50000, green=50000, blue=50000))
                cell_renderer.set_property("markup",
                        "<b>" + tree_model.get_value(iter, user_data) + "</b>")
            else:
                cell_renderer.set_property("cell-background-set", False)
                cell_renderer.set_property("text",
                        tree_model.get_value(iter, user_data))
            return

        def plugin_rend_toggle_func(column, cell_renderer, tree_model, iter,
                user_data = None):
            if tree_model.iter_depth(iter)==0:
                cell_renderer.set_property("activatable", False)
                cell_renderer.set_property("active", False)
                cell_renderer.set_property("visible", False)
                cell_renderer.set_property("cell-background-set", True)
                cell_renderer.set_property("cell-background-gdk",
                        gtk.gdk.Color(red=40000, green=40000, blue=40000))
            else:
                cell_renderer.set_property("activatable", True)
                cell_renderer.set_property("active",
                        tree_model.get_value(iter,0))
                cell_renderer.set_property("visible", True)
                cell_renderer.set_property("cell-background-set", False)
            return

        def plugin_rend_toggle_cb(cell, path, data):
            model, col = data
            model[path][col] = not model[path][col]
            return

        def plugin_rend_edited_cb(cell, path, text, data):
            model, col = data
            model[path][col] = text
            return

        self.plugin_list_col_0 = gtk.TreeViewColumn('Use')
        self.plugin_list_col_0.pack_start(self.plugin_rend_toggle, False)
        self.plugin_list_col_0.set_cell_data_func(self.plugin_rend_toggle,
                plugin_rend_toggle_func)
        self.plugin_rend_toggle.connect("toggled", plugin_rend_toggle_cb,
                (self.plugin_list_store, 0))

        self.plugin_list_col_1 = gtk.TreeViewColumn('Name')
        self.plugin_list_col_1.pack_start(self.plugin_rend_text, True)
        self.plugin_list_col_1.set_cell_data_func(self.plugin_rend_text,
                plugin_rend_text_func, 1)

        self.plugin_list_col_2 = gtk.TreeViewColumn('Description')
        self.plugin_list_col_2.pack_start(self.plugin_rend_text, True)
        self.plugin_list_col_2.set_cell_data_func(self.plugin_rend_text,
                plugin_rend_text_func, 2)

        self.plugin_list_col_3 = gtk.TreeViewColumn('Parameters')
        self.plugin_list_col_3.pack_start(self.plugin_rend_text_edit, True)
        self.plugin_list_col_3.set_cell_data_func(self.plugin_rend_text_edit,
                plugin_rend_text_func, 3)
        self.plugin_rend_text_edit.connect("edited", plugin_rend_edited_cb,
                (self.plugin_list_store, 3))

        self.plugin_list.append_column(self.plugin_list_col_0)
        self.plugin_list.append_column(self.plugin_list_col_1)
        self.plugin_list.append_column(self.plugin_list_col_2)
        self.plugin_list.append_column(self.plugin_list_col_3)
        self.plugin_list.set_search_column(1)

        pluginsystem = tasker.pluginsystem()
        self.plugin_iter = {}
        self.flow_list_data = set()

        #flow combobox
        for plname in pluginsystem.list():
            p = pluginsystem.getplugin(plname)
            piter = self.plugin_list_store.append(None,
                    [False, "%s (%s)" % (p.name, p.version), p.description,
                        "", plname])
            self.plugin_iter[plname] = piter
            for n,d in [ (f, p.getFlow(f).description) for f in p.getFlows() ]:
                self.plugin_list_store.append(piter, [False, n, d, "", plname])
                self.flow_list_data.add(n)

        self.flow_list_rend_text = gtk.CellRendererText()
        self.flow_list_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
        self.flow_list_store_diagnose = -1
        for idx,n in enumerate(sorted(self.flow_list_data)):
            self.flow_list_store.append([n, pluginsystem.get_title(n)])
            if n=="diagnose":
                self.flow_list_store_diagnose = idx
        self.flow_list = self._glade.get_widget("combo_Advanced_Flows")
        self.flow_list.set_model(self.flow_list_store)
        self.flow_list.pack_start(self.flow_list_rend_text, True)
        self.flow_list.add_attribute(self.flow_list_rend_text, 'text', 1)
        self.flow_list.set_active(self.flow_list_store_diagnose)

        # results
        self.result_list_store = gtk.ListStore(gobject.TYPE_STRING,
                gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_INT)
        self.result_list = self._glade.get_widget("tree_Results")
        self.result_list.set_model(self.result_list_store)
        self.result_list_iter = {}

        def result_rend_text_func(column, cell_renderer, tree_model, iter,
                (use_state_fg, use_state_bg, col)):
            colors = [
                    None,
                    gtk.gdk.Color(red=40000, green=62000, blue=40000),
                    gtk.gdk.Color(red=62000, green=40000, blue=40000),
                    gtk.gdk.Color(red=50000, green=55000, blue=65500),
                    gtk.gdk.Color(red=0, green=0, blue=0)
                    ]

            state = tree_model.get_value(iter, 3)

            if colors[state] and use_state_bg:
                cell_renderer.set_property("cell-background-set", True)
                cell_renderer.set_property("cell-background-gdk", colors[state])
                if state==4:
                    cell_renderer.set_property("foreground-set", True)
                    cell_renderer.set_property("foreground-gdk",
                        gtk.gdk.Color(red=50000, green=50000, blue=50000))
            else:
                cell_renderer.set_property("cell-background-set", False)

            if use_state_fg and state!=2:
                cell_renderer.set_property("foreground-set", True)
                cell_renderer.set_property("foreground-gdk",
                        gtk.gdk.Color(red=40000, green=40000, blue=40000))
            else:
                cell_renderer.set_property("foreground-set", False)

            cell_renderer.set_property("text",
                    tree_model.get_value(iter, col))
            return

        self.result_rend_text = gtk.CellRendererText()

        self.result_list_col_0 = gtk.TreeViewColumn('Name')
        self.result_list_col_0.pack_start(self.result_rend_text, True)
        self.result_list_col_0.set_cell_data_func(self.result_rend_text,
                result_rend_text_func, (False, False, 0))

        self.result_list_col_1 = gtk.TreeViewColumn('Status')
        self.result_list_col_1.pack_start(self.result_rend_text, True)
        self.result_list_col_1.set_cell_data_func(self.result_rend_text,
                result_rend_text_func, (False, True, 1))

        self.result_list_col_2 = gtk.TreeViewColumn('Description')
        self.result_list_col_2.pack_start(self.result_rend_text, True)
        self.result_list_col_2.set_cell_data_func(self.result_rend_text,
                result_rend_text_func, (True, False, 2))

        self.result_list_col_3 = gtk.TreeViewColumn('Status ID')
        self.result_list_col_3.pack_start(self.result_rend_text, True)
        self.result_list_col_3.add_attribute(self.result_rend_text, 'text', 3)
        self.result_list_col_3.set_property("visible", False)

        self.result_list.append_column(self.result_list_col_0)
        self.result_list.append_column(self.result_list_col_1)
        self.result_list.append_column(self.result_list_col_2)
        self.result_list.append_column(self.result_list_col_3)
        self.result_list.set_search_column(0)

    def update(self, mailbox, message):

        def issue_state(self):
            if self._exception:
                return ("Exception", 4)
            elif self._fixed:
                return ("Fixed", 3)
            elif self._happened and self._checked:
                return ("Detected", 2)
            elif self._checked:
                return ("No problem", 1)
            else:
                return ("Waiting for check", 0)


        if self._cfg.operation.verbose == "True":
            self._importance = logging.DEBUG
        else:
            self._importance = logging.INFO

        """Read the reporting system message and schedule a call to update
        stuff in the gui using gobject.idle_add(_o, func, params...)"""
        if message["action"]==reporting.END:
            gobject.idle_add(_o, self._window.destroy)

        elif message["action"]==reporting.CHOICE_QUESTION:
            gobject.idle_add(_o, self.choice_question, message)

        elif message["action"]==reporting.CONFIG_QUESTION:
            gobject.idle_add(_o, self.config_question, message)

        elif message["action"]==reporting.FILENAME_QUESTION:
            gobject.idle_add(_o, self.filename_question, message)

        elif message["action"]==reporting.PASSWORD_QUESTION and message["message"].confirm:
            gobject.idle_add(_o, self.password_question, message)

        elif message["action"] in (reporting.TEXT_QUESTION, reporting.PASSWORD_QUESTION):
            gobject.idle_add(_o, self.text_question, message)

        elif message["action"]==reporting.START:
            if self._importance<=message["importance"]:
                ctx = self.status_text.get_context_id(message["origin"].name)
                gobject.idle_add(_o, self.status_text.push, ctx,
                        "START: %s (%s)" % (message["origin"].name,
                            message["message"]))

        elif message["action"]==reporting.STOP:
            if self._importance<=message["importance"]:
                ctx = self.status_text.get_context_id(message["origin"].name)
                gobject.idle_add(_o, self.status_text.push, ctx,
                        "STOP: %s (%s)" % (message["origin"].name,
                            message["message"]))

        elif message["action"]==reporting.PROGRESS:
            if self._importance<=message["importance"]:
                if message["message"] is None:
                  gobject.idle_add(self.status_progress.hide)
                else:
                  gobject.idle_add(_o, self.status_progress.set_text,
                          "%d/%d - %s" % (message["message"][0],
                              message["message"][1], message["origin"].name))
                  gobject.idle_add(_o, self.status_progress.set_fraction,
                          float(message["message"][0])/message["message"][1])
                  gobject.idle_add(_o, self.status_progress.show)

        elif message["action"]==reporting.INFO:
            if self._importance<=message["importance"]:
                ctx = self.status_text.get_context_id(message["origin"].name)
                gobject.idle_add(_o, self.status_text.push, ctx,
                        "INFO: %s (%s)" % (message["message"],
                            message["origin"].name))

        elif message["action"]==reporting.ALERT:
            if self._importance<=message["importance"]:
                ctx = self.status_text.get_context_id(message["origin"].name)
                gobject.idle_add(_o, self.status_text.push, ctx,
                        "ALERT: %s (%s)" % (message["message"],
                            message["origin"].name))

        elif message["action"]==reporting.EXCEPTION:
            ctx = self.status_text.get_context_id(message["origin"].name)
            gobject.idle_add(_o, self.status_text.push, ctx,
                    "EXCEPTION: %s (%s)" % (message["message"],
                        message["origin"].name))

        elif message["action"]==reporting.TABLE:
            if self._importance<=message["importance"]:
                print("TABLE %s FROM %s" % (message["title"],
                    message["origin"].name,))
                pprint.pprint(message["message"])

        elif message["action"]==reporting.TREE:
            if self._importance<=message["importance"]:
                print("TREE %s FROM %s" % (message["title"],
                    message["origin"].name,))
                pprint.pprint(message["message"])

        elif message["action"]==reporting.ISSUE:
                i = message["message"]
                t,ids = issue_state(i)
                if not self.result_list_iter.has_key(i):
                    self.result_list_iter[i] = self.result_list_store.append(
                            [i.name, t, i.description, ids])
                else:
                    for idx,val in enumerate([i.name, t, i.description, ids]):
                        gobject.idle_add(_o, self.result_list_store.set,
                                self.result_list_iter[i], idx, val)

        else:
            print("FIXME: Unknown message action %d!!" % (message["action"],))
            print(message)

    def run(self):
        gtk.gdk.threads_init()
        gtk.gdk.threads_enter()
        gtk.main()
        gtk.gdk.threads_leave()

    def _get_dialog(self, Id):
        dir = os.path.dirname(self._glade.relative_file("."))
        glade = gtk.glade.XML(os.path.join(dir, "firstaidkit.glade"), Id)
        dlg = glade.get_widget(Id)
        return glade, dlg

    def choice_question(self, message):
        """Return the user's answer.

        Return self._no_answer on invalid answer,
        self._cancel_answer if the user wants to cancel.

        """
        try:
            gtk.gdk.threads_enter()
            
            # prepare the dialog content
            glade, dlg = self._get_dialog("ChoiceQuestionDialog")
            question = message["message"]
            glade.get_widget("choice_question_label").set_text(question.prompt)
            vbox = glade.get_widget("choice_question_vbox")
            radio_map = {}
            group = None
            for (value, name) in question.options:
                r = gtk.RadioButton(group, name, False)
                radio_map[r] = value
                r.show()
                vbox.pack_start(r)
                if group is None:
                    group = r

            # run the dialog and send the response
            while True:
                if dlg.run()!=gtk.RESPONSE_OK:
                    message["reply"].end(level = reporting.FIRSTAIDKIT)
                    break
                else:
                    res = self._no_answer
                    for r in radio_map:
                        if r.get_active():
                            res = radio_map[r]
                            break
                    if res!=self._no_answer:
                        question.send_answer(message, res, origin = self)
                        break
                            
        finally:
            # schedule dialog destroy
            dlg.destroy()
            gtk.gdk.flush()
            gtk.gdk.threads_leave()
                            
    def filename_question(self, message):
        """Return the user's answer.

        Return self._no_answer on invalid answer,
        self._cancel_answer if the user wants to cancel.

        """
        # STOCK_OK is neutral enough so that we don't need to distinguish
        # between "open" and "save" mode for now.
        try:
            gtk.gdk.threads_enter()

            question = message["message"]
            dlg = gtk.FileChooserDialog(title = question.prompt,
                                        parent = self._window,
                                        action = gtk.FILE_CHOOSER_ACTION_SAVE,
                                        buttons = (gtk.STOCK_CANCEL,
                                                   gtk.RESPONSE_REJECT,
                                                   gtk.STOCK_OK,
                                                   gtk.RESPONSE_ACCEPT))
            
            if dlg.run()==gtk.RESPONSE_ACCEPT:
                question.send_answer(message, dlg.get_filename(), origin = self)
            else:
                message["reply"].end(level = reporting.FIRSTAIDKIT)
        finally:
            # schedule dialog destroy
            dlg.destroy()
            gtk.gdk.flush()
            gtk.gdk.threads_leave()
        
    def config_question(self, message):
        """Return the user's answers.

        Return self._no_answer on invalid answer,
        self._cancel_answer if the user wants to cancel.

        """

        try:
            gtk.gdk.threads_enter()

            question = message["message"]
            dlg = ListDialog(title = question.title,
                             description = question.description,
                             items = question.items,
                             dir = os.path.dirname(self._glade.relative_file("."))
                             )

            res = dlg.run()

            if res==gtk.RESPONSE_ACCEPT:
                question.send_answer(message, dlg.items(), origin = self)
            elif res==gtk.RESPONSE_CANCEL:
                question.send_answer(message, [], origin = self)
            else:
                raise Exception("Unknown value %s")

        finally:
            # schedule dialog destroy
            dlg.destroy()
            gtk.gdk.flush()
            gtk.gdk.threads_leave()

    def text_question(self, message):
        """Return the user's answer.

        Return self._no_answer on invalid answer,
        self._cancel_answer if the user wants to cancel.

        """
        
        try:
            gtk.gdk.threads_enter()
            
            glade, dlg = self._get_dialog("TextQuestionDialog")
            question = message["message"]

            glade.get_widget("text_question_label"). \
                                                     set_text(question.prompt)
            entry = glade.get_widget("text_question_entry")
            if isinstance(question, reporting.PasswordQuestion):
                entry.set_visibility(False)
                
            if dlg.run()==gtk.RESPONSE_OK:
                question.send_answer(message, entry.get_text(), origin = self)
            else:
                message["reply"].end(level = reporting.FIRSTAIDKIT)
        finally:
            # schedule dialog destroy
            dlg.destroy()
            gtk.gdk.flush()
            gtk.gdk.threads_leave()
            
    def password_question(self, message):
        """Return the user's answer.

        Return self._no_answer on invalid answer,
        self._cancel_answer if the user wants to cancel.

        """

        try:
            gtk.gdk.threads_enter()

            glade, dlg = self._get_dialog("TwoPasswordDialog")
            question = message["message"]
            assert question.confirm

            glade.get_widget("two_password_label1"). \
                                                     set_text(question.prompt)
            glade.get_widget("two_password_label2"). \
                                                     set_text("Confirm: %s " % (question.prompt,))
            entry1 = glade.get_widget("two_password_entry1")
            entry2 = glade.get_widget("two_password_entry2")
            while True:
                if dlg.run()!=gtk.RESPONSE_OK:
                    message["reply"].end(level = reporting.FIRSTAIDKIT)
                    break
                elif entry1.get_text()==entry2.get_text():
                    question.send_answer(message, entry1.get_text(), origin = self)
                    break
                else:
                    continue
        finally:
            # schedule dialog destroy
            dlg.destroy()
            gtk.gdk.flush()
            gtk.gdk.threads_leave()

class FlagList(object):
    def __init__(self, cfg, flags, dir=""):
        self._glade = gtk.glade.XML(os.path.join(dir, "firstaidkit.glade"),
                "FlagList")
        self._window = self._glade.get_widget("FlagList")
        self._window.set_modal(True)
        self.flags = {}
        self._cb = CallbacksFlagList(self._window, cfg, self.flags)
        self._glade.signal_autoconnect(self._cb)
        fl_gui = self._glade.get_widget("box_flags")
        flags_set = cfg.operation._list("flags")
        for f in sorted(flags.known()):
            b = gtk.CheckButton(label=f)
            self.flags[f] = b
            b.set_active(f in flags_set)
            b.show()
            fl_gui.pack_start(b, expand=False, fill=True)
        l = gtk.Label("")
        l.show()

        fl_gui.pack_end(l, expand=True, fill=True)

class AboutDialog(object):
    def close(self, widget, *args):
        self._window.destroy()

    def __init__(self, cfg, dir=""):
        self._glade = gtk.glade.XML(os.path.join(dir, "firstaidkit.glade"),
                "AboutDialog")
        self._window = self._glade.get_widget("AboutDialog")
        self._window.connect("response", self.close)

        try:
            cfg = cfg.getConfigBits("about")
            version = cfg.about.version
            license = cfg.about.copying
        except:
            version = "development"
            license = os.path.join(os.path.dirname(dir), "COPYING")

        self._window.set_version(version)
        try:
            self._window.set_license(open(license, "r").read())
        except IOError:
            self._window.set_license(None)

class PluginInfo(object):
    def close(self, widget):
        self._window.destroy()

    def __init__(self, plugin, dir=""):
        self._glade = gtk.glade.XML(os.path.join(dir, "firstaidkit.glade"),
                "PluginInfo")
        self._window = self._glade.get_widget("PluginInfo")
        self._window.set_modal(True)
        self._window.set_title(self._window.get_title()+plugin.name)

        self._close = self._glade.get_widget("CloseButton")
        self._close.connect("clicked", self.close)

        self._name = self._glade.get_widget("Info_name")
        self._name.set_label(plugin.name)

        self._version = self._glade.get_widget("Info_version")
        self._version.set_label(plugin.version)

        self._author = self._glade.get_widget("Info_author")
        self._author.set_label(plugin.author)

        self._description = self._glade.get_widget("Info_description")
        self._description.set_label(plugin.description)

        self._table = self._glade.get_widget("Table")

        for n,d in [ (f, plugin.getFlow(f).description)
                for f in plugin.getFlows() ]:
            if n==plugin.default_flow:
                lname = gtk.Label("<b>"+n+"</b>")
                lname.set_property("use-markup", True)
            else:
                lname = gtk.Label(n)
            lname.show()
            ldesc = gtk.Label(d)
            ldesc.show()
            ldesc.set_line_wrap(True)

            sy = self._table.get_property("n-rows")
            sx = self._table.get_property("n-columns")
            sy += 1
            self._table.resize(sy, sx)
            self._table.attach(lname, 0, 1, sy-1, sy, yoptions = gtk.FILL,
                    xoptions = gtk.FILL)
            lname.set_alignment(0, 0)
            self._table.attach(ldesc, 1, 2, sy-1, sy, yoptions = gtk.FILL,
                    xoptions = gtk.FILL)
            ldesc.set_alignment(0, 0)


if __name__=="__main__":
    w = MainWindow(None, None, None)
    w.run()