summaryrefslogtreecommitdiff
path: root/plugins/externaltools/tools/manager.py
blob: 4da0deb9f274e86cb213381d10da0270b5356496 (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
# -*- coding: utf-8 -*-
#    Pluma External Tools plugin
#    Copyright (C) 2005-2006  Steve Frécinaux <steve@istique.net>
#
#    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
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

__all__ = ('Manager', )

import os.path
from library import *
from functions import *
import hashlib
from xml.sax import saxutils
from gi.repository import GObject, Gio, Gdk, Gtk, GtkSource, Pluma

class LanguagesPopup(Gtk.Window):
    __gtype_name__ = "LanguagePopup"

    COLUMN_NAME = 0
    COLUMN_ID = 1
    COLUMN_ENABLED = 2

    def __init__(self, languages):
        Gtk.Window.__init__(self, type=Gtk.WindowType.POPUP)

        self.set_default_size(200, 200)
        self.props.can_focus = True

        self.build()
        self.init_languages(languages)

        self.show()

        self.grab_add()

        self.keyboard = None
        device_manager = Gdk.Display.get_device_manager(self.get_window().get_display())
        for device in device_manager.list_devices(Gdk.DeviceType.MASTER):
            if device.get_source() == Gdk.InputSource.KEYBOARD:
                self.keyboard = device
                break

        self.pointer = device_manager.get_client_pointer()

        if self.keyboard is not None:
            self.keyboard.grab(self.get_window(),
                               Gdk.GrabOwnership.WINDOW, False,
                               Gdk.EventMask.KEY_PRESS_MASK |
                               Gdk.EventMask.KEY_RELEASE_MASK,
                               None, Gdk.CURRENT_TIME)
        self.pointer.grab(self.get_window(),
                          Gdk.GrabOwnership.WINDOW, False,
                          Gdk.EventMask.BUTTON_PRESS_MASK |
                          Gdk.EventMask.BUTTON_RELEASE_MASK |
                          Gdk.EventMask.POINTER_MOTION_MASK |
                          Gdk.EventMask.ENTER_NOTIFY_MASK |
                          Gdk.EventMask.LEAVE_NOTIFY_MASK |
                          Gdk.EventMask.PROXIMITY_IN_MASK |
                          Gdk.EventMask.PROXIMITY_OUT_MASK |
                          Gdk.EventMask.SCROLL_MASK,
                          None, Gdk.CURRENT_TIME)

        self.view.get_selection().select_path((0,))

    def build(self):
        self.model = Gtk.ListStore(str, str, bool)

        self.sw = Gtk.ScrolledWindow()
        self.sw.show()

        self.sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        self.sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)

        self.view = Gtk.TreeView(self.model)
        self.view.show()

        self.view.set_headers_visible(False)

        column = Gtk.TreeViewColumn()

        renderer = Gtk.CellRendererToggle()
        column.pack_start(renderer, False)
        column.set_attributes(renderer, active=self.COLUMN_ENABLED)

        renderer.connect('toggled', self.on_language_toggled)

        renderer = Gtk.CellRendererText()
        column.pack_start(renderer, True)
        column.set_attributes(renderer, text=self.COLUMN_NAME)

        self.view.append_column(column)
        self.view.set_row_separator_func(self.on_separator)

        self.sw.add(self.view)

        self.add(self.sw)

    def enabled_languages(self, model, path, piter, ret):
        enabled = model.get_value(piter, self.COLUMN_ENABLED)

        if path.get_indices()[0] == 0 and enabled:
            return True

        if enabled:
            ret.append(model.get_value(piter, self.COLUMN_ID))

        return False

    def languages(self):
        ret = []

        self.model.foreach(self.enabled_languages, ret)
        return ret

    def on_separator(self, model, piter):
        val = model.get_value(piter, self.COLUMN_NAME)
        return val == '-'

    def init_languages(self, languages):
        manager = GtkSource.LanguageManager()
        langs = [manager.get_language(x) for x in manager.get_language_ids()]
        langs.sort(key=lambda x: x.get_name())

        self.model.append([_('All languages'), None, not languages])
        self.model.append(['-', None, False])
        self.model.append([_('Plain Text'), 'plain', 'plain' in languages])
        self.model.append(['-', None, False])

        for lang in langs:
            self.model.append([lang.get_name(), lang.get_id(), lang.get_id() in languages])

    def correct_all(self, model, path, piter, enabled):
        if path == (0,):
            return False

        model.set_value(piter, self.COLUMN_ENABLED, enabled)

    def on_language_toggled(self, renderer, path):
        piter = self.model.get_iter(path)

        enabled = self.model.get_value(piter, self.COLUMN_ENABLED)
        self.model.set_value(piter, self.COLUMN_ENABLED, not enabled)

        if path == '0':
            self.model.foreach(self.correct_all, False)
        else:
            self.model.set_value(self.model.get_iter_first(), self.COLUMN_ENABLED, False)

    def do_key_press_event(self, event):
        if event.keyval == Gdk.KEY_Escape:
            self.destroy()
            return True
        else:
            event.window = self.view.get_bin_window()
            return self.view.event(event)

    def do_key_release_event(self, event):
        event.window = self.view.get_bin_window()
        return self.view.event(event)

    def in_window(self, event, window=None):
        if not window:
            window = self.get_window()

        geometry = window.get_geometry()
        origin = window.get_origin()

        return event.x_root >= origin[1] and \
               event.x_root <= origin[1] + geometry[2] and \
               event.y_root >= origin[2] and \
               event.y_root <= origin[2] + geometry[3]

    def do_destroy(self):
        if self.keyboard:
            self.keyboard.ungrab(Gdk.CURRENT_TIME)
        self.pointer.ungrab(Gdk.CURRENT_TIME)

        return Gtk.Window.do_destroy(self)

    def setup_event(self, event, window):
        fr = event.window.get_origin()
        to = window.get_origin()

        event.window = window
        event.x += fr[1] - to[1]
        event.y += fr[2] - to[2]

    def resolve_widgets(self, root):
        res = [root]

        if isinstance(root, Gtk.Container):
            root.forall(lambda x, y: res.extend(self.resolve_widgets(x)), None)

        return res

    def resolve_windows(self, window):
        if not window:
            return []

        res = [window]
        res.extend(window.get_children())

        return res

    def propagate_mouse_event(self, event, reverse=True):
        allwidgets = self.resolve_widgets(self.get_child())

        if reverse:
            allwidgets.reverse()

        for widget in allwidgets:
            windows = self.resolve_windows(widget.get_window())
            windows.reverse()

            for window in windows:
                if not (window.get_events() & event.type):
                    continue

                if self.in_window(event, window):
                    self.setup_event(event, window)

                    if widget.event(event):
                        return True

        return False

    def do_button_press_event(self, event):
        if not self.in_window(event):
            self.destroy()
        else:
            return self.propagate_mouse_event(event)

    def do_button_release_event(self, event):
        if not self.in_window(event):
            self.destroy()
        else:
            return self.propagate_mouse_event(event)

    def do_scroll_event(self, event):
        return self.propagate_mouse_event(event, False)

    def do_motion_notify_event(self, event):
        return self.propagate_mouse_event(event)

    def do_enter_notify_event(self, event):
        return self.propagate_mouse_event(event)

    def do_leave_notify_event(self, event):
        return self.propagate_mouse_event(event)

    def do_proximity_in_event(self, event):
        return self.propagate_mouse_event(event)

    def do_proximity_out_event(self, event):
        return self.propagate_mouse_event(event)

class Manager(GObject.Object):
    TOOL_COLUMN = 0 # For Tree
    NAME_COLUMN = 1 # For Combo

    __gsignals__ = {
        'tools-updated': (GObject.SignalFlags.RUN_LAST, None, ())
    }

    def __init__(self, datadir):
        GObject.Object.__init__(self)
        self.datadir = datadir
        self.dialog = None
        self._size = (0, 0)
        self._languages = {}
        self._tool_rows = {}

        self.build()

    def get_final_size(self):
        return self._size

    def build(self):
        callbacks = {
            'on_new_tool_button_clicked'      : self.on_new_tool_button_clicked,
            'on_remove_tool_button_clicked'   : self.on_remove_tool_button_clicked,
            'on_tool_manager_dialog_response' : self.on_tool_manager_dialog_response,
            'on_tool_manager_dialog_configure_event': self.on_tool_manager_dialog_configure_event,
            'on_tool_manager_dialog_focus_out': self.on_tool_manager_dialog_focus_out,
            'on_accelerator_key_press'        : self.on_accelerator_key_press,
            'on_accelerator_focus_in'         : self.on_accelerator_focus_in,
            'on_accelerator_focus_out'        : self.on_accelerator_focus_out,
            'on_languages_button_clicked'     : self.on_languages_button_clicked
        }

        # Load the "main-window" widget from the ui file.
        self.ui = Gtk.Builder()
        self.ui.add_from_file(os.path.join(self.datadir, 'ui', 'tools.ui'))
        self.ui.connect_signals(callbacks)
        self.dialog = self.ui.get_object('tool-manager-dialog')

        self.view = self.ui.get_object('view')

        self.__init_tools_model()
        self.__init_tools_view()

        for name in ['input', 'output', 'applicability', 'save-files']:
            self.__init_combobox(name)

        self.do_update()

    def expand_from_doc(self, doc):
        row = None

        if doc:
            if doc.get_language():
                lid = doc.get_language().get_id()

                if lid in self._languages:
                    row = self._languages[lid]
            elif 'plain' in self._languages:
                row = self._languages['plain']

        if not row and None in self._languages:
            row = self._languages[None]

        if not row:
            return

        self.view.expand_row(row.get_path(), False)
        self.view.get_selection().select_path(row.get_path())

    def run(self, window):
        if self.dialog == None:
            self.build()

        # Open up language
        self.expand_from_doc(window.get_active_document())

        self.dialog.set_transient_for(window)
        window.get_group().add_window(self.dialog)
        self.dialog.present()

    def add_accelerator(self, item):
        if not item.shortcut:
            return

        if item.shortcut in self.accelerators:
            if not item in self.accelerators[item.shortcut]:
                self.accelerators[item.shortcut].append(item)
        else:
            self.accelerators[item.shortcut] = [item]

    def remove_accelerator(self, item, shortcut=None):
        if not shortcut:
            shortcut = item.shortcut

        if not shortcut in self.accelerators:
            return

        self.accelerators[shortcut].remove(item)

        if not self.accelerators[shortcut]:
            del self.accelerators[shortcut]

    def add_tool_to_language(self, tool, language):
        if isinstance(language, GtkSource.Language):
            lid = language.get_id()
        else:
            lid = language

        if not lid in self._languages:
            piter = self.model.append(None, [language])

            parent = Gtk.TreeRowReference.new(self.model, self.model.get_path(piter))
            self._languages[lid] = parent
        else:
            parent = self._languages[lid]

        piter = self.model.get_iter(parent.get_path())
        child = self.model.append(piter, [tool])

        if not tool in self._tool_rows:
            self._tool_rows[tool] = []

        self._tool_rows[tool].append(Gtk.TreeRowReference.new(self.model, self.model.get_path(child)))
        return child

    def add_tool(self, tool):
        manager = GtkSource.LanguageManager()
        ret = None

        for lang in tool.languages:
            l = manager.get_language(lang)

            if l:
                ret = self.add_tool_to_language(tool, l)
            elif lang == 'plain':
                ret = self.add_tool_to_language(tool, 'plain')

        if not ret:
            ret = self.add_tool_to_language(tool, None)

        self.add_accelerator(tool)
        return ret

    def __init_tools_model(self):
        self.tools = ToolLibrary()
        self.current_node = None
        self.script_hash = None
        self.accelerators = dict()

        self.model = Gtk.TreeStore(object)
        self.view.set_model(self.model)

        for tool in self.tools.tree.tools:
            self.add_tool(tool)

        self.model.set_default_sort_func(self.sort_tools)
        self.model.set_sort_column_id(-1, Gtk.SortType.ASCENDING)

    def sort_tools(self, model, iter1, iter2, user_data=None):
        # For languages, sort All before everything else, otherwise alphabetical
        t1 = model.get_value(iter1, self.TOOL_COLUMN)
        t2 = model.get_value(iter2, self.TOOL_COLUMN)

        if model.iter_parent(iter1) == None:
            if t1 == None:
                return -1

            if t2 == None:
                return 1

            def lang_name(lang):
                if isinstance(lang, GtkSource.Language):
                    return lang.get_name()
                else:
                    return _('Plain Text')

            n1 = lang_name(t1)
            n2 = lang_name(t2)
        else:
            n1 = t1.name
            n2 = t2.name

        return cmp(n1.lower(), n2.lower())

    def __init_tools_view(self):
        # Tools column
        column = Gtk.TreeViewColumn('Tools')
        renderer = Gtk.CellRendererText()
        column.pack_start(renderer, False)
        renderer.set_property('editable', True)
        self.view.append_column(column)

        column.set_cell_data_func(renderer, self.get_cell_data_cb, None)

        renderer.connect('edited', self.on_view_label_cell_edited)
        renderer.connect('editing-started', self.on_view_label_cell_editing_started)

        self.selection_changed_id = self.view.get_selection().connect('changed', self.on_view_selection_changed, None)

    def __init_combobox(self, name):
        combo = self[name]
        combo.set_active(0)

    # Convenience function to get an object from its name
    def __getitem__(self, key):
        return self.ui.get_object(key)

    def set_active_by_name(self, combo_name, option_name):
        combo = self[combo_name]
        model = combo.get_model()
        piter = model.get_iter_first()
        while piter is not None:
            if model.get_value(piter, self.NAME_COLUMN) == option_name:
                combo.set_active_iter(piter)
                return True
            piter = model.iter_next(piter)
        return False

    def get_selected_tool(self):
        model, piter = self.view.get_selection().get_selected()

        if piter is not None:
            tool = model.get_value(piter, self.TOOL_COLUMN)

            if not isinstance(tool, Tool):
                tool = None

            return piter, tool
        else:
            return None, None

    def compute_hash(self, string):
        return hashlib.md5(string).hexdigest()

    def save_current_tool(self):
        if self.current_node is None:
             return

        if self.current_node.filename is None:
            self.current_node.autoset_filename()

        def combo_value(o, name):
            combo = o[name]
            return combo.get_model().get_value(combo.get_active_iter(), self.NAME_COLUMN)

        self.current_node.input = combo_value(self, 'input')
        self.current_node.output = combo_value(self, 'output')
        self.current_node.applicability = combo_value(self, 'applicability')
        self.current_node.save_files = combo_value(self, 'save-files')

        buf = self['commands'].get_buffer()
        (start, end) = buf.get_bounds()
        script  = buf.get_text(start, end, False)
        h = self.compute_hash(script)
        if h != self.script_hash:
            # script has changed -> save it
            self.current_node.save_with_script([line + "\n" for line in script.splitlines()])
            self.script_hash = h
        else:
            self.current_node.save()

        self.update_remove_revert()

    def clear_fields(self):
        self['accelerator'].set_text('')

        buf = self['commands'].get_buffer()
        buf.begin_not_undoable_action()
        buf.set_text('')
        buf.end_not_undoable_action()

        for nm in ('input', 'output', 'applicability', 'save-files'):
            self[nm].set_active(0)

        self['languages_label'].set_text(_('All Languages'))

    def fill_languages_button(self):
        if not self.current_node or not self.current_node.languages:
            self['languages_label'].set_text(_('All Languages'))
        else:
            manager = GtkSource.LanguageManager()
            langs = []

            for lang in self.current_node.languages:
                if lang == 'plain':
                    langs.append(_('Plain Text'))
                else:
                    l = manager.get_language(lang)

                    if l:
                        langs.append(l.get_name())

            self['languages_label'].set_text(', '.join(langs))

    def fill_fields(self):
        node = self.current_node
        self['accelerator'].set_text(default(node.shortcut, ''))

        buf = self['commands'].get_buffer()
        script = default(''.join(node.get_script()), '')

        buf.begin_not_undoable_action()
        buf.set_text(script)
        buf.end_not_undoable_action()

        self.script_hash = self.compute_hash(script)
        contenttype, uncertain = Gio.content_type_guess(None, script)
        lmanager = GtkSource.LanguageManager.get_default()
        language = lmanager.guess_language(content_type=contenttype)

        if language is not None:
            buf.set_language(language)
            buf.set_highlight_syntax(True)
        else:
            buf.set_highlight_syntax(False)

        for nm in ('input', 'output', 'applicability', 'save-files'):
            model = self[nm].get_model()
            piter = model.get_iter_first()

            self.set_active_by_name(nm,
                                    default(node.__getattribute__(nm.replace('-', '_')),
                                    model.get_value(piter, self.NAME_COLUMN)))

        self.fill_languages_button()

    def update_remove_revert(self):
        piter, node = self.get_selected_tool()

        removable = node is not None and node.is_local()

        self['remove-tool-button'].set_sensitive(removable)
        self['revert-tool-button'].set_sensitive(removable)

        if node is not None and node.is_global():
            self['remove-tool-button'].hide()
            self['revert-tool-button'].show()
        else:
            self['remove-tool-button'].show()
            self['revert-tool-button'].hide()

    def do_update(self):
        self.update_remove_revert()

        piter, node = self.get_selected_tool()
        self.current_node = node

        if node is not None:
            self.fill_fields()
            self['tool-table'].set_sensitive(True)
        else:
            self.clear_fields()
            self['tool-table'].set_sensitive(False)

    def language_id_from_iter(self, piter):
        if not piter:
            return None

        tool = self.model.get_value(piter, self.TOOL_COLUMN)

        if isinstance(tool, Tool):
            piter = self.model.iter_parent(piter)
            tool = self.model.get_value(piter, self.TOOL_COLUMN)

        if isinstance(tool, GtkSource.Language):
            return tool.get_id()
        elif tool:
            return 'plain'

        return None

    def selected_language_id(self):
        # Find current language if there is any
        model, piter = self.view.get_selection().get_selected()

        return self.language_id_from_iter(piter)

    def on_new_tool_button_clicked(self, button):
        self.save_current_tool()

        # block handlers while inserting a new item
        self.view.get_selection().handler_block(self.selection_changed_id)

        self.current_node = Tool(self.tools.tree);
        self.current_node.name = _('New tool')
        self.tools.tree.tools.append(self.current_node)

        lang = self.selected_language_id()

        if lang:
            self.current_node.languages = [lang]

        piter = self.add_tool(self.current_node)

        self.view.set_cursor(self.model.get_path(piter), self.view.get_column(self.TOOL_COLUMN), True)
        self.fill_fields()

        self['tool-table'].set_sensitive(True)
        self.view.get_selection().handler_unblock(self.selection_changed_id)

    def tool_changed(self, tool, refresh=False):
        for row in self._tool_rows[tool]:
            self.model.row_changed(row.get_path(), self.model.get_iter(row.get_path()))

        if refresh and tool == self.current_node:
            self.fill_fields()

        self.update_remove_revert()

    def on_remove_tool_button_clicked(self, button):
        piter, node = self.get_selected_tool()

        if not node:
            return

        if node.is_global():
            shortcut = node.shortcut

            if node.parent.revert_tool(node):
                self.remove_accelerator(node, shortcut)
                self.add_accelerator(node)

                self['revert-tool-button'].set_sensitive(False)
                self.fill_fields()

                self.tool_changed(node)
        else:
            parent = self.model.iter_parent(piter)
            language = self.language_id_from_iter(parent)

            self.model.remove(piter)

            if language in node.languages:
                node.languages.remove(language)

            self._tool_rows[node] = filter(lambda x: x.valid(), self._tool_rows[node])

            if not self._tool_rows[node]:
                del self._tool_rows[node]

                if node.parent.delete_tool(node):
                    self.remove_accelerator(node)
                    self.current_node = None
                    self.script_hash = None

                    if self.model.iter_is_valid(piter):
                        self.view.set_cursor(self.model.get_path(piter), self.view.get_column(self.TOOL_COLUMN), False)

                self.view.grab_focus()

            path = self._languages[language].get_path()
            parent = self.model.get_iter(path)

            if not self.model.iter_has_child(parent):
                self.model.remove(parent)
                del self._languages[language]

    def on_view_label_cell_edited(self, cell, path, new_text):
        if new_text != '':
            piter = self.model.get_iter(path)
            tool = self.model.get_value(piter, self.TOOL_COLUMN)

            tool.name = new_text

            self.save_current_tool()
            self.tool_changed(tool)

    def on_view_label_cell_editing_started(self, renderer, editable, path):
            piter = self.model.get_iter(path)
            tool = self.model.get_value(piter, self.TOOL_COLUMN)

            if isinstance(editable, Gtk.Entry):
                editable.set_text(tool.name)
                editable.grab_focus()

    def on_view_selection_changed(self, selection, userdata):
        self.save_current_tool()
        self.do_update()

    def accelerator_collision(self, name, node):
        if not name in self.accelerators:
            return []

        ret = []

        for other in self.accelerators[name]:
            if not other.languages or not node.languages:
                ret.append(other)
                continue

            for lang in other.languages:
                if lang in node.languages:
                    ret.append(other)
                    continue

        return ret

    def set_accelerator(self, keyval, mod):
        # Check whether accelerator already exists
        self.remove_accelerator(self.current_node)

        name = Gtk.accelerator_name(keyval, mod)

        if name == '':
            self.current_node.shorcut = None
            self.save_current_tool()
            return True

        col = self.accelerator_collision(name, self.current_node)

        if col:
            dialog = Gtk.MessageDialog(self.dialog,
                                       Gtk.DialogFlags.MODAL,
                                       Gtk.MessageType.ERROR,
                                       Gtk.ButtonsType.CLOSE,
                                       _('This accelerator is already bound to %s') % (', '.join(map(lambda x: x.name, col)),))

            dialog.run()
            dialog.destroy()

            self.add_accelerator(self.current_node)
            return False

        self.current_node.shortcut = name
        self.add_accelerator(self.current_node)
        self.save_current_tool()

        return True

    def on_accelerator_key_press(self, entry, event):
        mask = event.state & Gtk.accelerator_get_default_mod_mask()

        if event.keyval == Gdk.KEY_Escape:
            entry.set_text(default(self.current_node.shortcut, ''))
            self['commands'].grab_focus()
            return True
        elif event.keyval == Gdk.KEY_Delete \
          or event.keyval == Gdk.KEY_BackSpace:
            entry.set_text('')
            self.remove_accelerator(self.current_node)
            self.current_node.shortcut = None
            self['commands'].grab_focus()
            return True
        elif event.keyval in range(Gdk.KEY_F1, Gdk.KEY_F12 + 1):
            # New accelerator
            if self.set_accelerator(event.keyval, mask):
                entry.set_text(default(self.current_node.shortcut, ''))
                self['commands'].grab_focus()

            # Capture all `normal characters`
            return True
        elif Gdk.keyval_to_unicode(event.keyval):
            if mask:
                # New accelerator
                if self.set_accelerator(event.keyval, mask):
                    entry.set_text(default(self.current_node.shortcut, ''))
                    self['commands'].grab_focus()
            # Capture all `normal characters`
            return True
        else:
            return False

    def on_accelerator_focus_in(self, entry, event):
        if self.current_node is None:
            return
        if self.current_node.shortcut:
            entry.set_text(_('Type a new accelerator, or press Backspace to clear'))
        else:
            entry.set_text(_('Type a new accelerator'))

    def on_accelerator_focus_out(self, entry, event):
        if self.current_node is not None:
            entry.set_text(default(self.current_node.shortcut, ''))
            self.tool_changed(self.current_node)

    def on_tool_manager_dialog_response(self, dialog, response):
        if response == Gtk.ResponseType.HELP:
            Pluma.help_display(self.dialog, 'pluma', 'pluma-external-tools-plugin')
            return

        self.on_tool_manager_dialog_focus_out(dialog, None)

        self.dialog.destroy()
        self.dialog = None
        self.tools = None

    def on_tool_manager_dialog_configure_event(self, dialog, event):
        if dialog.get_realized():
            alloc = dialog.get_allocation()
            self._size = (alloc.width, alloc.height)

    def on_tool_manager_dialog_focus_out(self, dialog, event):
        self.save_current_tool()
        self.emit('tools-updated')

    def get_cell_data_cb(self, column, cell, model, piter, user_data=None):
        tool = model.get_value(piter, self.TOOL_COLUMN)

        if tool == None or not isinstance(tool, Tool):
            if tool == None:
                label = _('All Languages')
            elif not isinstance(tool, GtkSource.Language):
                label = _('Plain Text')
            else:
                label = tool.get_name()

            markup = saxutils.escape(label)
            editable = False
        else:
            escaped = saxutils.escape(tool.name)

            if tool.shortcut:
                markup = '%s (<b>%s</b>)' % (escaped, saxutils.escape(tool.shortcut))
            else:
                markup = escaped

            editable = True

        cell.set_properties(markup=markup, editable=editable)

    def tool_in_language(self, tool, lang):
        if not lang in self._languages:
            return False

        ref = self._languages[lang]
        parent = ref.get_path()

        for row in self._tool_rows[tool]:
            path = row.get_path()

            if path.get_indices()[0] == parent.get_indices()[0]:
                return True

        return False

    def update_languages(self, popup):
        self.current_node.languages = popup.languages()
        self.fill_languages_button()

        piter, node = self.get_selected_tool()
        ret = None

        if node:
            ref = Gtk.TreeRowReference(self.model, self.model.get_path(piter))

        # Update languages, make sure to inhibit selection change stuff
        self.view.get_selection().handler_block(self.selection_changed_id)

        # Remove all rows that are no longer
        for row in list(self._tool_rows[self.current_node]):
            piter = self.model.get_iter(row.get_path())
            language = self.language_id_from_iter(piter)

            if (not language and not self.current_node.languages) or \
               (language in self.current_node.languages):
                continue

            # Remove from language
            self.model.remove(piter)
            self._tool_rows[self.current_node].remove(row)

            # If language is empty, remove it
            parent = self.model.get_iter(self._languages[language].get_path())

            if not self.model.iter_has_child(parent):
                self.model.remove(parent)
                del self._languages[language]

        # Now, add for any that are new
        manager = GtkSource.LanguageManager()

        for lang in self.current_node.languages:
            if not self.tool_in_language(self.current_node, lang):
                l = manager.get_language(lang)

                if not l:
                    l = 'plain'

                self.add_tool_to_language(self.current_node, l)

        if not self.current_node.languages and not self.tool_in_language(self.current_node, None):
            self.add_tool_to_language(self.current_node, None)

        # Check if we can still keep the current
        if not ref or not ref.valid():
            # Change selection to first language
            path = self._tool_rows[self.current_node][0].get_path()
            piter = self.model.get_iter(path)
            parent = self.model.iter_parent(piter)

            # Expand parent, select child and scroll to it
            self.view.expand_row(self.model.get_path(parent), False)
            self.view.get_selection().select_path(path)
            self.view.set_cursor(path, self.view.get_column(self.TOOL_COLUMN), False)

        self.view.get_selection().handler_unblock(self.selection_changed_id)

    def on_languages_button_clicked(self, button):
        popup = LanguagesPopup(self.current_node.languages)
        popup.set_transient_for(self.dialog)

        origin = button.get_window().get_origin()
        popup.move(origin[1], origin[2] - popup.get_allocation().height)

        popup.connect('destroy', self.update_languages)

# ex:et:ts=4: