#! /usr/bin/python3 -s
# -*- coding: utf-8 -*-

################################################################################
#
# ROSA Device Manager
#
# Copyright (c) 2020   LLC "STC IT ROSA"
# License: GPLv3+
# Authors:
#     Ural Mullabaev <u.mullabaev@ntcit-rosa.ru>
#
# 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,
# 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
################################################################################


import sys
import os.path
import argparse
import subprocess

from PyQt5.QtCore import QCoreApplication, QLocale, QTranslator
from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtGui import QIcon

#import rosa_device_manager as manager


if __name__ == '__main__':

    app = QApplication(sys.argv)

    # Load translations
    locale_dir = os.path.join("/usr/share", "rosa-device-manager", "locale")
    translator = QTranslator()
    translator.load(QLocale.system().name(), locale_dir)
    app.installTranslator(translator)

    # Define attributes from program argument
    if len(sys.argv) < 5:
        print(QCoreApplication.translate("Notifier", "Argument with attributes not set"))
        sys.exit(1)

    removable = sys.argv[1]
    manufacturer = sys.argv[2]
    product = sys.argv[3]
    args = sys.argv[4]

    QIcon.setThemeSearchPaths([u'/usr/share/icons', u':/icons'])
    QIcon.setThemeName("rosa")

    if removable:
        msg_box = QMessageBox()
        msg_box.setWindowTitle("ROSA Device Manager")
        msg_box.setText(QCoreApplication.translate("Notifier", "Device not identified"))
        msg_box.setInformativeText(QCoreApplication.translate("Notifier", "Do you want to set access parameters for the device?"))
        msg_box.setDetailedText("{} {}".format(manufacturer, product))
        msg_box.setIcon(QMessageBox.Question)
        msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        button =  msg_box.exec_()

        if button == QMessageBox.Yes:
            subprocess.call(["/usr/bin/gksudo", "/usr/sbin/rosa-removable-drive-manager --add"], shell = False)

    else:
	subprocess.call(["/usr/bin/kdialog", "--passivepopup", "ROSA Device Manager\n{}\n{} {}".format(QCoreApplication.translate("Notifier", "Device not identified").encode('utf-8'), manufacturer, product)], shell = False)
