#-*- coding: utf-8 -*-
#
# Indicator-Privacy - Unity appindicator to switch privacy settings
#                     http://www.florian-diesch.de/software/indicator-privacy/
#
# Copyright (C) 2013 Florian Diesch <devel@florian-diesch.de>
#
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

from gi.repository import Gtk, Gio, GLib, Gdk
from gettext import gettext as _


def error(text):    
    dlg = Gtk.MessageDialog(None, 0,  Gtk.MessageType.ERROR,
                            Gtk.ButtonsType.OK,
                            text
                            )
    dlg.set_title(_('Error'))
    result = dlg.run()
    dlg.destroy()
    return result    




def cleaning_dlg(func, text, explanation=None):
    dlg = Gtk.MessageDialog(None, 0,  Gtk.MessageType.WARNING,
                            Gtk.ButtonsType.NONE,
                            text
                            )
 
    dlg.set_title(_('Confirm'))    
    dlg.set_property('use-markup', True)
    dlg.add_buttons(
        Gtk.STOCK_OK, Gtk.ResponseType.OK,
        Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
        )
    dlg.props.skip_taskbar_hint = False
    dlg.props.skip_pager_hint = False
    dlg.props.type_hint = Gdk.WindowTypeHint.NORMAL

    if explanation:
        box = dlg.get_message_area()
        expander = Gtk.Expander.new(_('What does this mean?'))
        label = Gtk.Label()
        label.set_markup(explanation)
        label.set_property('max-width-chars', 55)
        label.set_property('wrap', True)
        expander.add(label)
        expander.show_all()
        box.add(expander)
    dlg.show_all()
        
    print('DLG 1')
    result = dlg.run()
    print('DLG 2')
    
    dlg.destroy()
    if result != Gtk.ResponseType.OK:
        return
    

    #  Just to show we are doing something
    dlg = Gtk.MessageDialog(None, 0,  Gtk.MessageType.INFO,
                            Gtk.ButtonsType.NONE, _('Cleaning...'))
                            
    pbar = Gtk.ProgressBar()
    pbar.show()
    dlg.get_action_area().add(pbar)
    def pulse(*args):
        pbar.pulse()
        while Gtk.events_pending():
            Gtk.main_iteration()
        if dlg.get_visible():
            return True
        else:
            dlg.destroy()
            return False

    dlg.show()
    GLib.timeout_add(40, pulse)
    if func(): # an error occured: hide dlg
        timeout = 0
    else: # no error: visible at least 1 second
        timeout = 1000
    GLib.timeout_add(timeout, lambda *args: dlg.destroy()) #visible at least 1 second

