# /*
#  * Copyright (C) 2020, KylinSoft Co., Ltd.
#  *
#  * 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 <https://www.gnu.org/licenses/>.
#  */

import dbus
import os
import logging
from gettext import gettext as _
from SystemUpdater.Core.errors import *
from SystemUpdater.Core.enums import *
from SystemUpdater.checks.registry import register_check
from SystemUpdater.Core.dbus_helper import get_bus

OFFLINE_SOURCE_FLAG = "/media/OfflineSource/ostree-policy.json"

@register_check("check-device-network", roles=[ROLE_UPDATE_CACHE])
def check_device_power(trans):
    if os.path.exists(OFFLINE_SOURCE_FLAG):
        logging.info("Use to offline source...")
        return

    try:
        bus = get_bus()
        obj = bus.get_object("org.freedesktop.NetworkManager","/org/freedesktop/NetworkManager")
        interface = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
        retval = interface.Get("org.freedesktop.NetworkManager","Connectivity",timeout=0.5)
    except Exception:
        retval = 4
    #1 表示没有网卡可以使用
    if retval == 1:
        raise UpdateBaseError(ERROR_NETWORK_FAILED)