<commit_before>/* BEGIN_COMMON_COPYRIGHT_HEADER
 *
 * Razor - a lightweight, Qt based, desktop toolset
 * http://razor-qt.org
 *
 * Copyright: 2011 Razor team
 * Authors:
 *   <NAME> <<EMAIL>>
 *
 * 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;  only version 2 of
 * the License is valid for this program.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * END_COMMON_COPYRIGHT_HEADER */
/* Based on a "MountTray" project - modified for Razor needs
    http://hatred.homelinux.net

    @date   2010-11-11
    @brief  Main application class: integrate all components

    Copyright (C) 2010 by hatred <<EMAIL>>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the version 2 of GNU General Public License as
    published by the Free Software Foundation.

    For more information see LICENSE and LICENSE.ru files
*/

#include <QtDebug>

#include <QtGui/QMessageBox>
#include <QtGui/QDesktopServices>
#include <QtCore/QUrl>

#include "qtxdg/xdgicon.h"
#include "mountbutton.h"


MountButton::MountButton(QWidget * parent, RazorPanel *panel)
    : QToolButton(parent),
      m_panel(panel)
{
    if (!QDBusConnection::systemBus().isConnected())
    {
        qDebug() << "Can't connect to dbus daemon. Some functions will be omited";
    }

    setIcon(XdgIcon::fromTheme("emblem-mounted"));

    // Display disk menu
    m_menu = new QMenu(this);
    QAction *empty_action = m_menu->addAction(tr("Empty"));
    empty_action->setEnabled(false);
    
//    setMenu(m_menu);

    QDBusConnection conn = QDBusConnection::systemBus();
    // TODO: Check for connection, timer for reconect
    /*bool connected =*/ conn.connect("org.freedesktop.UDisks",
                                  "/org/freedesktop/UDisks",
                                  "org.freedesktop.UDisks",
                                  "DeviceChanged",
                                  this,
                                  SLOT(onDbusDeviceChangesMessage(QDBusObjectPath)));

    // Scan already connected devices
    initialScanDevices();

    connect(this, SIGNAL(clicked()), this, SLOT(showMenu()));

    connect(&_dm, SIGNAL(deviceConnected(DiskInfo)),
            this, SLOT(onDiskAdded(DiskInfo)));
    connect(&_dm, SIGNAL(deviceDisconnected(DiskInfo)),
            this, SLOT(onDiskRemoved(DiskInfo)));
    _dm.start();
}

MountButton::~MountButton()
{
    _dm.exit(0);
    _dm.wait(1000);
    _dm.terminate();
    _dm.wait();
}

void MountButton::initialScanDevices()
{
    QList<DiskInfo*> devices = _dm.scanDevices();
    for (int i = 0; i < devices.count(); i++)
    {
        DiskInfo *disk = devices.at(i);

        // add device
        _sm.addDevice(*disk);
        addMenuItem(*disk);

        StorageItem *sitem = _sm.getDevice(*disk);
        updateMenuItem(disk->device_name, disk->file_system_label, sitem->isMounted());

        // clear
        delete disk;
    }
}

void MountButton::addMenuItem(const DiskInfo &info)
{
    MenuDiskItem  *item   = new MenuDiskItem(info, this);
    QWidgetAction *action = new QWidgetAction(this);
    action->setDefaultWidget(item);

    if (m_menu_items.count() == 0)
    {
        // Clear 'Empty' item
        m_menu->clear();
        m_menu->addAction(action);
    }
    else
    {
        // Insert on Top
        m_menu->insertAction(m_menu->actions().at(0), action);
    }

    m_menu_items.insert(info.device_name, action);

    // Connect signals
    connect(item, SIGNAL(mountMedia(const QString&)),
            this, SLOT(onMediaMount(const QString&)));

    connect(item, SIGNAL(ejectMedia(const QString&)),
            this, SLOT(onMediaEject(const QString&)));
}

void MountButton::removeMenuItem(const QString &device)
{
    QWidgetAction *action = 0;
    if (m_menu_items.contains(device))
    {
        action = m_menu_items[device];
        m_menu->removeAction(action);
        m_menu_items.remove(device);
        delete action;
    }

    if (m_menu_items.count() == 0)
    {
        QAction *empty_action = m_menu->addAction(tr("Empty"));
        empty_action->setEnabled(false);
    }
}

void MountButton::updateMenuItem(const QString &device, const QString &name, bool is_mounted)
{
    QWidgetAction *action = 0;
    if (m_menu_items.contains(device))
    {
        action = m_menu_items[device];
        MenuDiskItem *item = static_cast<MenuDiskItem*>(action->defaultWidget());

        if (item == 0)
        {
            return;
        }

        if (!name.isEmpty())
        {
            item->setLabel(name);
        }

        item->setMountStatus(is_mounted);
    }
}

void MountButton::showMessage(const QString &text)
{
    // TODO/FIXME: messaging!
    //_tray_icon.showMessage("MountTray", text);
    qDebug() << "MESSAGE" << text;
}

void MountButton::showError(const QString &text)
{
    // TODO/FIXME: messaging!
//    _tray_icon.showMessage("MountTray Error", text, QSystemTrayIcon::Critical);
    qDebug() << "ERRMSG" << text;
}

/**************************************************************************************************/
/* Signals ---------------------------------------------------------------------------------------*/
/**************************************************************************************************/

void MountButton::onDiskAdded(DiskInfo info)<commit_msg>fixed (workaround before real event notifications) #42 mount plugin: display notifications when connected<commit_after>/* BEGIN_COMMON_COPYRIGHT_HEADER
 *
 * Razor - a lightweight, Qt based, desktop toolset
 * http://razor-qt.org
 *
 * Copyright: 2011 Razor team
 * Authors:
 *   <NAME> <<EMAIL>>
 *
 * 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;  only version 2 of
 * the License is valid for this program.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * END_COMMON_COPYRIGHT_HEADER */
/* Based on a "MountTray" project - modified for Razor needs
    http://hatred.homelinux.net

    @date   2010-11-11
    @brief  Main application class: integrate all components

    Copyright (C) 2010 by hatred <<EMAIL>>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the version 2 of GNU General Public License as
    published by the Free Software Foundation.

    For more information see LICENSE and LICENSE.ru files
*/

#include <QtDebug>

#include <QtGui/QMessageBox>
#include <QtGui/QDesktopServices>
#include <QtCore/QUrl>
#include <QtGui/QToolTip>

#include "qtxdg/xdgicon.h"
#include "mountbutton.h"


MountButton::MountButton(QWidget * parent, RazorPanel *panel)
    : QToolButton(parent),
      m_panel(panel)
{
    if (!QDBusConnection::systemBus().isConnected())
    {
        qDebug() << "Can't connect to dbus daemon. Some functions will be omited";
    }

    setIcon(XdgIcon::fromTheme("emblem-mounted"));

    // Display disk menu
    m_menu = new QMenu(this);
    QAction *empty_action = m_menu->addAction(tr("Empty"));
    empty_action->setEnabled(false);
    
//    setMenu(m_menu);

    QDBusConnection conn = QDBusConnection::systemBus();
    // TODO: Check for connection, timer for reconect
    /*bool connected =*/ conn.connect("org.freedesktop.UDisks",
                                  "/org/freedesktop/UDisks",
                                  "org.freedesktop.UDisks",
                                  "DeviceChanged",
                                  this,
                                  SLOT(onDbusDeviceChangesMessage(QDBusObjectPath)));

    // Scan already connected devices
    initialScanDevices();

    connect(this, SIGNAL(clicked()), this, SLOT(showMenu()));

    connect(&_dm, SIGNAL(deviceConnected(DiskInfo)),
            this, SLOT(onDiskAdded(DiskInfo)));
    connect(&_dm, SIGNAL(deviceDisconnected(DiskInfo)),
            this, SLOT(onDiskRemoved(DiskInfo)));
    _dm.start();
}

MountButton::~MountButton()
{
    _dm.exit(0);
    _dm.wait(1000);
    _dm.terminate();
    _dm.wait();
}

void MountButton::initialScanDevices()
{
    QList<DiskInfo*> devices = _dm.scanDevices();
    for (int i = 0; i < devices.count(); i++)
    {
        DiskInfo *disk = devices.at(i);

        // add device
        _sm.addDevice(*disk);
        addMenuItem(*disk);

        StorageItem *sitem = _sm.getDevice(*disk);
        updateMenuItem(disk->device_name, disk->file_system_label, sitem->isMounted());

        // clear
        delete disk;
    }
}

void MountButton::addMenuItem(const DiskInfo &info)
{
    MenuDiskItem  *item   = new MenuDiskItem(info, this);
    QWidgetAction *action = new QWidgetAction(this);
    action->setDefaultWidget(item);

    if (m_menu_items.count() == 0)
    {
        // Clear 'Empty' item
        m_menu->clear();
        m_menu->addAction(action);
    }
    else
    {
        // Insert on Top
        m_menu->insertAction(m_menu->actions().at(0), action);
    }

    m_menu_items.insert(info.device_name, action);

    // Connect signals
    connect(item, SIGNAL(mountMedia(const QString&)),
            this, SLOT(onMediaMount(const QString&)));

    connect(item, SIGNAL(ejectMedia(const QString&)),
            this, SLOT(onMediaEject(const QString&)));
}

void MountButton::removeMenuItem(const QString &device)
{
    QWidgetAction *action = 0;
    if (m_menu_items.contains(device))
    {
        action = m_menu_items[device];
        m_menu->removeAction(action);
        m_menu_items.remove(device);
        delete action;
    }

    if (m_menu_items.count() == 0)
    {
        QAction *empty_action = m_menu->addAction(tr("Empty"));
        empty_action->setEnabled(false);
    }
}

void MountButton::updateMenuItem(const QString &device, const QString &name, bool is_mounted)
{
    QWidgetAction *action = 0;
    if (m_menu_items.contains(device))
    {
        action = m_menu_items[device];
        MenuDiskItem *item = static_cast<MenuDiskItem*>(action->defaultWidget());

        if (item == 0)
        {
            return;
        }

        if (!name.isEmpty())
        {
            item->setLabel(name);
        }

        item->setMountStatus(is_mounted);
    }
}

void MountButton::showMessage(const QString &text)
{
    // TODO/FIXME: messaging!
    //_tray_icon.showMessage("MountTray", text);
    qDebug() << "MESSAGE" << text;
    QToolTip::showText(mapToGlobal(pos()), text, this);
}

void MountButton::showError(const QString &text)
{
    // TODO/FIXME: messaging!
//    _tray_icon.showMessage("MountTray Error", text, QSystemTrayIcon::Critical);
    qDebug() << "ERRMSG" << text;
    QToolTip::showText(mapToGlobal(pos()), QString("<b>Error:</b> %1").arg(text), this);
}

/**************************************************************************************************/
/* Signals ---------------------------------------------------------------------------------------*/
/**************************************************************************************************/

void MountButton::onDiskAdded(DiskInfo info)
{'fraction_non_alphanumeric': 0.16278487352867518, 'fraction_numerical': 0.007596627431338175, 'mean_word_length': 2.8311480652382475, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1049611', 'n_tokens_mistral': 3607, 'n_tokens_neox': 3422, 'n_words': 872}