1#!/usr/bin/python
2"""A simple GTK3 graphical dialog helper that can be used as a fallback if zenity is not available
3   Compatible with python 2 or 3 with the gi bindings.
4
5   Three modes are available:
6       test: accepts no arguments, returns 0 if the python dependencies are available, or 1 if not
7       error: show a gtk-3 error dialog
8              accepts arguments --title and --text
9       question: show a gtk-3 question dialog
10              accepts arguments --title and --text
11              returns 0 on Yes, or 1 on No
12"""
13
14import sys
15
16try:
17    import argparse
18    import gi
19    gi.require_version('Gtk', '3.0')
20    from gi.repository import Gtk
21except ImportError:
22    sys.exit(1)
23
24class Error():
25    def __init__(self, title, text):
26        dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, title)
27        dialog.format_secondary_text(text)
28        dialog.run()
29        dialog.destroy()
30
31class Question():
32    def __init__(self, title, text):
33        dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, title)
34        dialog.format_secondary_text(text)
35        response = dialog.run()
36        dialog.destroy()
37        sys.exit(0 if response == Gtk.ResponseType.YES else 1)
38
39if __name__ == "__main__":
40    parser = argparse.ArgumentParser()
41    parser.add_argument('type', choices=['error', 'question', 'test'])
42    parser.add_argument('--title', type=str, required=False, default='')
43    parser.add_argument('--text', type=str, required=False, default='')
44    args = parser.parse_args()
45    if args.type == 'question':
46        Question(args.title, args.text.replace('\\n', '\n'))
47    elif args.type == 'error':
48        Error(args.title, args.text.replace('\\n', '\n'))
49