<commit_before>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Copyright (C) 2012 Jolla Ltd.
** Contact: <NAME> <<EMAIL>>
**
** This file is part of lipstick.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include <QGuiApplication>
#include "homewindow.h"
#include <QQmlContext>
#include <QScreen>
#include "utilities/closeeventeater.h"
#include <qmlocks.h>
#include "notifications/notificationmanager.h"
#include "usbmodeselector.h"

QMap<QString, QString> USBModeSelector::errorCodeToTranslationID;

USBModeSelector::USBModeSelector(QObject *parent) :
    QObject(parent),
    window(0)
    ,usbMode(new MeeGo::QmUSBMode(this)),
    locks(new MeeGo::QmLocks(this))
{
    if (errorCodeToTranslationID.isEmpty()) {
        errorCodeToTranslationID.insert("qtn_usb_filessystem_inuse", "qtn_usb_filessystem_inuse");
        errorCodeToTranslationID.insert("mount_failed", "qtn_usb_mount_failed");
    }

    connect(usbMode, SIGNAL(modeChanged(MeeGo::QmUSBMode::Mode)), this, SLOT(applyUSBMode(MeeGo::QmUSBMode::Mode)));
    connect(usbMode, SIGNAL(error(const QString &)), this, SLOT(showError(const QString &)));

    // Lazy initialize to improve startup time
    QTimer::singleShot(500, this, SLOT(applyCurrentUSBMode()));
}

void USBModeSelector::applyCurrentUSBMode()
{
    foreach (MeeGo::QmUSBMode::Mode supportedMode, usbMode->getSupportedModes()) {
        supportedUSBModeList.append(supportedMode);
    }
    emit supportedUSBModesChanged();

    applyUSBMode(usbMode->getMode());
}

void USBModeSelector::setWindowVisible(bool visible)
{
    if (visible) {
        emit dialogShown();

        if (window == 0) {
            window = new HomeWindow();
            window->setGeometry(QRect(QPoint(), QGuiApplication::primaryScreen()->size()));
            window->setCategory(QLatin1String("dialog"));
            window->setWindowTitle("USB Mode");
            window->setContextProperty("initialSize", QGuiApplication::primaryScreen()->size());
            window->setContextProperty("usbModeSelector", this);
            window->setSource(QUrl("qrc:/qml/USBModeSelector.qml"));
            window->installEventFilter(new CloseEventEater(this));
        }

        if (!window->isVisible()) {
            window->show();
            emit windowVisibleChanged();
        }
    } else if (window != 0 && window->isVisible()) {
        window->hide();
        emit windowVisibleChanged();
    }
}

bool USBModeSelector::windowVisible() const
{
    return window != 0 && window->isVisible();
}

QList<int> USBModeSelector::supportedUSBModes() const
{
    return supportedUSBModeList;
}

void USBModeSelector::applyUSBMode(MeeGo::QmUSBMode::Mode mode)
{
    switch (mode) {
    case MeeGo::QmUSBMode::Connected:
        if (locks->getState(MeeGo::QmLocks::Device) == MeeGo::QmLocks::Locked) {
            // When the device lock is on and USB is connected, always pretend that the USB mode selection dialog is shown to unlock the touch screen lock
            emit dialogShown();
        }
        break;
    case MeeGo::QmUSBMode::Ask:
    case MeeGo::QmUSBMode::ModeRequest:
        setWindowVisible(true);
        break;
    case MeeGo::QmUSBMode::Disconnected:
    case MeeGo::QmUSBMode::OviSuite:
    case MeeGo::QmUSBMode::MassStorage:
    case MeeGo::QmUSBMode::SDK:
    case MeeGo::QmUSBMode::MTP:
    case MeeGo::QmUSBMode::Developer:
    case MeeGo::QmUSBMode::Adb:
    case MeeGo::QmUSBMode::Diag:
    case MeeGo::QmUSBMode::ConnectionSharing:
        // Hide the mode selection dialog and show a mode notification
        setWindowVisible(false);
        showNotification(mode);
        break;
    case MeeGo::QmUSBMode::ChargingOnly:
    case MeeGo::QmUSBMode::Undefined:
        // no-op
        break;
    default:
        // Hide the mode selection dialog
        setWindowVisible(false);
        break;
    }
}

void USBModeSelector::showNotification(MeeGo::QmUSBMode::Mode mode)
{
    QString category = "device.added";
    QString body;
    switch (mode) {
    case MeeGo::QmUSBMode::OviSuite:
        //% "Sync and connect in use"
        body = qtTrId("qtn_usb_sync_active");
        break;
    case MeeGo::QmUSBMode::MassStorage:
        //% "Mass storage in use"
        body = qtTrId("qtn_usb_storage_active");
        break;
    case MeeGo::QmUSBMode::SDK:
    case MeeGo::QmUSBMode::Developer:
        //% "SDK mode in use"
        body = qtTrId("qtn_usb_sdk_active");
        break;
    case MeeGo::QmUSBMode::MTP:
        //% "MTP mode in use"
        body = qtTrId("qtn_usb_mtp_active");
        break;
    case MeeGo::QmUSBMode::Adb:
        //% "Adb mode in use"
        body = qtTrId("qtn_usb_adb_active");
        break;
    case MeeGo::QmUSBMode::Diag:
        //% "Diag mode in use"
        body = qtTrId("qtn_usb_diag_active");
        break;
    case MeeGo::QmUSBMode::Disconnected:
        category = "device.removed";
        //% "USB cable disconnected"
        body = qtTrId("qtn_usb_disconnected");
        break;
    case MeeGo::QmUSBMode::ConnectionSharing:
        //% "USB tethering in use"
        body = qtTrId("qtn_usb_connection_sharing_active");
        break;
    default:
        return;
    }

    NotificationManager *manager = NotificationManager::instance();
    QVariantHash hints;
    hints.insert(NotificationManager::HINT_CATEGORY, category);
    hints.insert(NotificationManager::HINT_PREVIEW_BODY, body);
    manager->Notify(qApp->applicationName(), 0, QString(), QString(), QString(), QStringList(), hints, -1);
}

void USBModeSelector::showError(const QString &errorCode)
{
    if (errorCodeToTranslationID.contains(errorCode)) {
        NotificationManager *manager = NotificationManager::instance();
        QVariantHash hints;
        hints.insert(NotificationManager::HINT_CATEGORY, "device.error");
        //% "USB connection error occurred"
        hints.insert(NotificationManager::HINT_PREVIEW_BODY, qtTrId(errorCodeToTranslationID.value(errorCode).toUtf8().constData()));
        manager->Notify(qApp->applicationName(), 0, QString(), QString(), QString(), QStringList(), hints, -1);
    }
}

void USBModeSelector::setUSBMode(int mode)
{
    usbMode->setMode((MeeGo::QmUSBMode::Mode)mode);
}
<commit_msg>[lipstick] Unlock device first system banner when the device is locked and the USB cable is connected<commit_after>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Copyright (C) 2012 Jolla Ltd.
** Contact: <NAME> <<EMAIL>>
**
** This file is part of lipstick.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include <QGuiApplication>
#include "homewindow.h"
#include <QQmlContext>
#include <QScreen>
#include "utilities/closeeventeater.h"
#include <qmlocks.h>
#include "notifications/notificationmanager.h"
#include "usbmodeselector.h"

QMap<QString, QString> USBModeSelector::errorCodeToTranslationID;

USBModeSelector::USBModeSelector(QObject *parent) :
    QObject(parent),
    window(0)
    ,usbMode(new MeeGo::QmUSBMode(this)),
    locks(new MeeGo::QmLocks(this))
{
    if (errorCodeToTranslationID.isEmpty()) {
        errorCodeToTranslationID.insert("qtn_usb_filessystem_inuse", "qtn_usb_filessystem_inuse");
        errorCodeToTranslationID.insert("mount_failed", "qtn_usb_mount_failed");
    }

    connect(usbMode, SIGNAL(modeChanged(MeeGo::QmUSBMode::Mode)), this, SLOT(applyUSBMode(MeeGo::QmUSBMode::Mode)));
    connect(usbMode, SIGNAL(error(const QString &)), this, SLOT(showError(const QString &)));

    // Lazy initialize to improve startup time
    QTimer::singleShot(500, this, SLOT(applyCurrentUSBMode()));
}

void USBModeSelector::applyCurrentUSBMode()
{
    foreach (MeeGo::QmUSBMode::Mode supportedMode, usbMode->getSupportedModes()) {
        supportedUSBModeList.append(supportedMode);
    }
    emit supportedUSBModesChanged();

    applyUSBMode(usbMode->getMode());
}

void USBModeSelector::setWindowVisible(bool visible)
{
    if (visible) {
        emit dialogShown();

        if (window == 0) {
            window = new HomeWindow();
            window->setGeometry(QRect(QPoint(), QGuiApplication::primaryScreen()->size()));
            window->setCategory(QLatin1String("dialog"));
            window->setWindowTitle("USB Mode");
            window->setContextProperty("initialSize", QGuiApplication::primaryScreen()->size());
            window->setContextProperty("usbModeSelector", this);
            window->setSource(QUrl("qrc:/qml/USBModeSelector.qml"));
            window->installEventFilter(new CloseEventEater(this));
        }

        if (!window->isVisible()) {
            window->show();
            emit windowVisibleChanged();
        }
    } else if (window != 0 && window->isVisible()) {
        window->hide();
        emit windowVisibleChanged();
    }
}

bool USBModeSelector::windowVisible() const
{
    return window != 0 && window->isVisible();
}

QList<int> USBModeSelector::supportedUSBModes() const
{
    return supportedUSBModeList;
}

void USBModeSelector::applyUSBMode(MeeGo::QmUSBMode::Mode mode)
{
    switch (mode) {
    case MeeGo::QmUSBMode::Connected:
        if (locks->getState(MeeGo::QmLocks::Device) == MeeGo::QmLocks::Locked) {
            // When the device lock is on and USB is connected, always pretend that the USB mode selection dialog is shown to unlock the touch screen lock
            emit dialogShown();

            // Show a notification instead
            NotificationManager *manager = NotificationManager::instance();
            QVariantHash hints;
            hints.insert(NotificationManager::HINT_CATEGORY, "x-nemo.device.locked");
            //% "Unlock device first"
            hints.insert(NotificationManager::HINT_PREVIEW_BODY, qtTrId("qtn_usb_device_locked"));
            manager->Notify(qApp->applicationName(), 0, QString(), QString(), QString(), QStringList(), hints, -1);
        }
        break;
    case MeeGo::QmUSBMode::Ask:
    case MeeGo::QmUSBMode::ModeRequest:
        setWindowVisible(true);
        break;
    case MeeGo::QmUSBMode::Disconnected:
    case MeeGo::QmUSBMode::OviSuite:
    case MeeGo::QmUSBMode::MassStorage:
    case MeeGo::QmUSBMode::SDK:
    case MeeGo::QmUSBMode::MTP:
    case MeeGo::QmUSBMode::Developer:
    case MeeGo::QmUSBMode::Adb:
    case MeeGo::QmUSBMode::Diag:
    case MeeGo::QmUSBMode::ConnectionSharing:
        // Hide the mode selection dialog and show a mode notification
        setWindowVisible(false);
        showNotification(mode);
        break;
    case MeeGo::QmUSBMode::ChargingOnly:
    case MeeGo::QmUSBMode::Undefined:
        // no-op
        break;
    default:
        // Hide the mode selection dialog
        setWindowVisible(false);
        break;
    }
}

void USBModeSelector::showNotification(MeeGo::QmUSBMode::Mode mode)
{
    QString category = "device.added";
    QString body;
    switch (mode) {
    case MeeGo::QmUSBMode::OviSuite:
        //% "Sync and connect in use"
        body = qtTrId("qtn_usb_sync_active");
        break;
    case MeeGo::QmUSBMode::MassStorage:
        //% "Mass storage in use"
        body = qtTrId("qtn_usb_storage_active");
        break;
    case MeeGo::QmUSBMode::SDK:
    case MeeGo::QmUSBMode::Developer:
        //% "SDK mode in use"
        body = qtTrId("qtn_usb_sdk_active");
        break;
    case MeeGo::QmUSBMode::MTP:
        //% "MTP mode in use"
        body = qtTrId("qtn_usb_mtp_active");
        break;
    case MeeGo::QmUSBMode::Adb:
        //% "Adb mode in use"
        body = qtTrId("qtn_usb_adb_active");
        break;
    case MeeGo::QmUSBMode::Diag:
        //% "Diag mode in use"
        body = qtTrId("qtn_usb_diag_active");
        break;
    case MeeGo::QmUSBMode::Disconnected:
        category = "device.removed";
        //% "USB cable disconnected"
        body = qtTrId("qtn_usb_disconnected");
        break;
    case MeeGo::QmUSBMode::ConnectionSharing:
        //% "USB tethering in use"
        body = qtTrId("qtn_usb_connection_sharing_active");
        break;
    default:
        return;
    }

    NotificationManager *manager = NotificationManager::instance();
    QVariantHash hints;
    hints.insert(NotificationManager::HINT_CATEGORY, category);
    hints.insert(NotificationManager::HINT_PREVIEW_BODY, body);
    manager->Notify(qApp->applicationName(), 0, QString(), QString(), QString(), QStringList(), hints, -1);
}

void USBModeSelector::showError(const QString &errorCode)
{
    if (errorCodeToTranslationID.contains(errorCode)) {
        NotificationManager *manager = NotificationManager::instance();
        QVariantHash hints;
        hints.insert(NotificationManager::HINT_CATEGORY, "device.error");
        //% "USB connection error occurred"
        hints.insert(NotificationManager::HINT_PREVIEW_BODY, qtTrId(errorCodeToTranslationID.value(errorCode).toUtf8().constData()));
        manager->Notify(qApp->applicationName(), 0, QString(), QString(), QString(), QStringList(), hints, -1);
    }
}

void USBModeSelector::setUSBMode(int mode)
{
    usbMode->setMode((MeeGo::QmUSBMode::Mode)mode);
}

{'fraction_non_alphanumeric': 0.14749583242733927, 'fraction_numerical': 0.0034790171776473144, 'mean_word_length': 3.6224455611390285, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6402831', 'n_tokens_mistral': 4282, 'n_tokens_neox': 4243, 'n_words': 850}