From f26b15eb0d97fb288c7edd481b76b5df54750879 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Sat, 1 Dec 2018 20:55:22 +0000 Subject: added removable volume detecion for windows git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@8469 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/General.pro | 4 +- src/plugins/General/rdetect/rdetect.pro | 16 +++ src/plugins/General/rdetect/rdetectfactory.cpp | 59 +++++++++ src/plugins/General/rdetect/rdetectfactory.h | 44 +++++++ src/plugins/General/rdetect/removablehelper.cpp | 166 ++++++++++++++++++++++++ src/plugins/General/rdetect/removablehelper.h | 48 +++++++ 6 files changed, 335 insertions(+), 2 deletions(-) create mode 100644 src/plugins/General/rdetect/rdetect.pro create mode 100644 src/plugins/General/rdetect/rdetectfactory.cpp create mode 100644 src/plugins/General/rdetect/rdetectfactory.h create mode 100644 src/plugins/General/rdetect/removablehelper.cpp create mode 100644 src/plugins/General/rdetect/removablehelper.h diff --git a/src/plugins/General/General.pro b/src/plugins/General/General.pro index bad328b19..cd07a3c37 100644 --- a/src/plugins/General/General.pro +++ b/src/plugins/General/General.pro @@ -29,7 +29,7 @@ contains(CONFIG, HISTORY_PLUGIN){ SUBDIRS += history } -win32:SUBDIRS += taskbar #\ - #rdetect +win32:SUBDIRS += taskbar \ + rdetect TEMPLATE = subdirs diff --git a/src/plugins/General/rdetect/rdetect.pro b/src/plugins/General/rdetect/rdetect.pro new file mode 100644 index 000000000..ebbb4a67e --- /dev/null +++ b/src/plugins/General/rdetect/rdetect.pro @@ -0,0 +1,16 @@ +include(../../plugins.pri) + +TARGET = $$PLUGINS_PREFIX/General/rdetect + +HEADERS += rdetectfactory.h \ + removablehelper.h + +SOURCES += rdetectfactory.cpp \ + removablehelper.cpp + +#RESOURCES = translations/translations.qrc + +LIBS += $$QMMPUI_LIB + +target.path = $$PLUGIN_DIR/General +INSTALLS += target diff --git a/src/plugins/General/rdetect/rdetectfactory.cpp b/src/plugins/General/rdetect/rdetectfactory.cpp new file mode 100644 index 000000000..26e912cd7 --- /dev/null +++ b/src/plugins/General/rdetect/rdetectfactory.cpp @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (C) 2018 by Ilya Kotov * + * forkotov02@ya.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 2 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include +#include "removablehelper.h" +#include "rdetectfactory.h" + +GeneralProperties RDetectFactory::properties() const +{ + GeneralProperties properties; + properties.name = tr("Volume Detection Plugin"); + properties.shortName = "rdetect"; + properties.hasAbout = true; + properties.hasSettings = false; + properties.visibilityControl = false; + return properties; +} + +QObject *RDetectFactory::create(QObject *parent) +{ + return new RemovableHelper(parent); +} + +QDialog *RDetectFactory::createConfigDialog(QWidget *parent) +{ + Q_UNUSED(parent); + return nullptr; +} + +void RDetectFactory::showAbout(QWidget *parent) +{ + QMessageBox::about (parent, tr("About Volume Detection Plugin"), + tr("Qmmp Removable Volume Detection Plugin") + "\n" + + tr("This plugin provides removable volume detection") + "\n" + + tr("Written by: Ilya Kotov ")); +} + +QString RDetectFactory::translation() const +{ + return QLatin1String(":/rdetect_plugin_"); +} diff --git a/src/plugins/General/rdetect/rdetectfactory.h b/src/plugins/General/rdetect/rdetectfactory.h new file mode 100644 index 000000000..8bb811f90 --- /dev/null +++ b/src/plugins/General/rdetect/rdetectfactory.h @@ -0,0 +1,44 @@ +/*************************************************************************** + * Copyright (C) 2018 by Ilya Kotov * + * forkotov02@ya.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 2 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef UDISKS2FACTORY_H +#define UDISKS2FACTORY_H + +/** + @author Ilya Kotov +*/ +#include + +#include +#include + +class RDetectFactory : public QObject, public GeneralFactory +{ +Q_OBJECT +Q_PLUGIN_METADATA(IID "org.qmmp.qmmpui.GeneralFactoryInterface.1.0") +Q_INTERFACES(GeneralFactory) +public: + GeneralProperties properties() const; + QObject *create(QObject *parent); + QDialog *createConfigDialog(QWidget *parent); + void showAbout(QWidget *parent); + QString translation() const; +}; + +#endif diff --git a/src/plugins/General/rdetect/removablehelper.cpp b/src/plugins/General/rdetect/removablehelper.cpp new file mode 100644 index 000000000..b7ec1eeeb --- /dev/null +++ b/src/plugins/General/rdetect/removablehelper.cpp @@ -0,0 +1,166 @@ +/*************************************************************************** + * Copyright (C) 2018 by Ilya Kotov * + * forkotov02@ya.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 2 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "removablehelper.h" + +RemovableHelper::RemovableHelper(QObject *parent): QObject(parent) +{ + qApp->installNativeEventFilter(this); + m_actions = new QActionGroup(this); + connect(m_actions,SIGNAL(triggered(QAction *)), SLOT(processAction(QAction *))); + updateActions(); +} + +RemovableHelper::~RemovableHelper() +{ + qApp->removeNativeEventFilter(this); +} + +bool RemovableHelper::nativeEventFilter(const QByteArray &eventType, void *message, long *result) +{ + Q_UNUSED(result); + + if(eventType == "windows_generic_MSG") + { + MSG *msg = static_cast(message); + if(msg->message == WM_DEVICECHANGE && msg->wParam == DBT_DEVICEARRIVAL) + { + PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam; + + if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME) + { + updateActions(); + return true; + } + } + else if(msg->message == WM_DEVICECHANGE && msg->wParam == DBT_DEVICEREMOVECOMPLETE) + { + PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam; + + if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME) + { + updateActions(); + return true; + } + } + } + return false; +} + +void RemovableHelper::processAction(QAction *action) +{ + qDebug("RemovableHelper: action triggered: %s", qPrintable(action->data().toString())); + QString path = action->data().toString(); + MediaPlayer::instance()->playListManager()->selectedPlayList()->add(path); +} + +void RemovableHelper::updateActions() +{ + QList volumes = QStorageInfo::mountedVolumes(); + + foreach(const QStorageInfo &storage, volumes) + { + if(!storage.isValid() || !storage.isReady()) + continue; + + QString dev_path; + + qDebug() << storage.fileSystemType(); + + if(storage.bytesTotal() < 40000000000LL && + (storage.fileSystemType() == "NTFS" || + storage.fileSystemType() == "FAT32" || + storage.fileSystemType() == "CDFS" || + storage.fileSystemType() == "UDF")) + { + dev_path = storage.rootPath(); + } + else + continue; + + if(!findAction(dev_path)) + { + QAction *action = new QAction(this); + QString actionText; + /*if (device->isAudio()) + { + actionText = QString(tr("Add CD \"%1\"")).arg(storage.displayName()); + } + else*/ + { + actionText = QString(tr("Add Volume \"%1\"")).arg(storage.displayName()); + } + + if(storage.fileSystemType() == "CDFS") + action->setIcon(qApp->style()->standardIcon(QStyle::SP_DriveCDIcon)); + else if(storage.fileSystemType() == "UDF") + action->setIcon(qApp->style()->standardIcon(QStyle::SP_DriveDVDIcon)); + else + action->setIcon(qApp->style()->standardIcon(QStyle::SP_DriveHDIcon)); + qDebug("RemovableHelper: added menu item: \"%s\"", qPrintable(dev_path)); + + action->setText(actionText); + action->setData(dev_path); + m_actions->addAction(action); + UiHelper::instance()->addAction(action, UiHelper::ADD_MENU); + } + } + // remove action if device is unmounted/removed + foreach(QAction *action, m_actions->actions()) + { + bool found = false; + + foreach(const QStorageInfo &storage, volumes) + { + if(storage.rootPath() == action->data().toString()) + { + found = true; + break; + } + } + + if (!found) + { + qDebug("RemovableHelper: removed menu item: \"%s\"", qPrintable(action->data().toString())); + m_actions->removeAction(action); + UiHelper::instance()->removeAction(action); + action->deleteLater(); + } + } +} + +QAction *RemovableHelper::findAction(const QString &dev_path) +{ + foreach(QAction *action, m_actions->actions()) + { + if (action->data().toString() == dev_path) + return action; + } + return nullptr; +} + diff --git a/src/plugins/General/rdetect/removablehelper.h b/src/plugins/General/rdetect/removablehelper.h new file mode 100644 index 000000000..c1740aaa9 --- /dev/null +++ b/src/plugins/General/rdetect/removablehelper.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (C) 2018 by Ilya Kotov * + * forkotov02@ya.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 2 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef REMOVABLEHELPER_H +#define REMOVABLEHELPER_H + +#include +#include + +class QActionGroup; +class QAction; + +class RemovableHelper : public QObject, public QAbstractNativeEventFilter +{ + Q_OBJECT +public: + explicit RemovableHelper(QObject *parent = nullptr); + ~RemovableHelper(); + + bool nativeEventFilter(const QByteArray &eventType, void *message, long *result); + +private slots: + void processAction(QAction *action); + +private: + void updateActions(); + QAction *findAction(const QString &dev_path); + QActionGroup *m_actions; + +}; + +#endif // REMOVABLEHELPER_H -- cgit v1.2.3-13-gbd6f