#-*- 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
from gettext import gettext as _
import textwrap
from urllib.parse import urlparse

from . import settings


def add_about_item(submenu):
    menu_item = Gtk.ImageMenuItem(label=_('About'))
    menu_item.connect('activate', lambda *args: show_about_dialog())
    submenu.append(menu_item)

def add_help_menu(submenu):
    
    menu_item = Gtk.MenuItem(label=_('Go to Web Page'))
    menu_item.connect('activate', on_menuitem_goto_webpage)
    submenu.append(menu_item)

    menu_item = Gtk.SeparatorMenuItem()
    submenu.append(menu_item)

    menu_item = Gtk.MenuItem(label=_('Ask a Question'))
    menu_item.connect('activate', on_menuitem_goto_question_page)
    submenu.append(menu_item)

    menu_item = Gtk.SeparatorMenuItem()
    submenu.append(menu_item)
                   
    menu_item = Gtk.MenuItem(label=_('Report a Bug'))
    menu_item.connect('activate', on_menuitem_bug)
    submenu.append(menu_item)

    menu_item = Gtk.MenuItem(label=_('Help with Translations'))
    menu_item.connect('activate', on_menuitem_translations)
    submenu.append(menu_item)

    menu_item = Gtk.MenuItem(label=_('Get Source Code'))
    menu_item.connect('activate', on_menuitem_sourcecode)
    submenu.append(menu_item)
        
    menu_item = Gtk.MenuItem(label=_('Donate via Flattr'))
    menu_item.connect('activate', on_menuitem_flattr)
    submenu.append(menu_item)

    menu_item = Gtk.MenuItem(label=_('Donate via PayPal'))
    menu_item.connect('activate', on_menuitem_donate)
    submenu.append(menu_item)
    
    submenu.show_all()

def open_url(url):
    u = urlparse(url)
    appinfo=Gio.app_info_get_default_for_uri_scheme(u.scheme)
    appinfo.launch_uris([url], None)

def on_menuitem_about_activate(menuitem):
    about.show_about_dialog()

def on_menuitem_docs(menuitem):
    open_url(settings.LOCAL_DOCS_URL)

def on_menuitem_goto_webpage(menuitem):
    open_url(settings.WEB_URL)

def on_menuitem_goto_question_page(menuitem):
    open_url(settings.QUESTION_URL)

def on_menuitem_donate(menuitem):
    open_url(settings.PAYPAL_URL)

def on_menuitem_flattr(menuitem):
    open_url(settings.FLATTR_URL)

def on_menuitem_translations(menuitem):
    open_url(settings.TRANSLATIONS_URL)

def on_menuitem_sourcecode(menuitem):
    open_url(settings.SOURCECODE_URL)

def on_menuitem_bug(menuitem):
    open_url(settings.BUGREPORT_URL)


def show_about_dialog():
    dlg = Gtk.AboutDialog()
    dlg.set_program_name(settings.APP_NAME)
    dlg.set_version(settings.APP_VERSION)
    dlg.set_website(settings.WEB_URL)
    dlg.set_authors(['Florian Diesch <devel@florian-diesch.de>'])
    dlg.set_wrap_license(True)
    dlg.set_logo_icon_name(settings.LOGO)
    dlg.set_copyright('Copyright (c) 2013 Florian Diesch')
    dlg.set_translator_credits(_("translator-credits"));
    dlg.set_license(textwrap.dedent(
            """
            Privacy Indicator - Unity appindicator to switch privacy settings

            Copyright (c) 2013 Florian Diesch <devel@florian-diesch.de>
           
            Homepage: http://www.florian-diesch.de/software/indicator-privacy/
           
            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/>.
            ------------------------------------------------------------------- 
 
            The Privacy Indicator icon is taken from gemicon http://gemicon.net/
            Handcrafted by Turqois.
 
            You are free to use and edit these icons for any personal or
            commercial work, (the linkback will very appreciated.
            You may NOT REDISTRIBUTE OR SELL these icon set on any other 
            website or source.

            We also exciting and happy to know if you use this icon, please
            don't hestitate to contact us dturqois@gmail.com

            Kind Regards,
            Turqois
            http://turqois.com
            twitter.com/dturqois
            dribbble.com/dturqois
            """))

    dlg.run()
    dlg.destroy()


