hwmanager: add LMB menu based on storage devices.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
pull/308/head
Michele Calgaro 2 years ago
parent 8c95671998
commit d196e674e1
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -54,11 +54,16 @@
#include "hwdevicetray_configdialog.h" #include "hwdevicetray_configdialog.h"
// Storage Device Action Type #include <map>
// use 'int' as underlying type to avoid exposing a bunch of unnecessary using namespace std;
// enums/structs in the class header file private methods' signature
map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
// Storage Device Action
namespace SDActions namespace SDActions
{ {
// use 'int' as underlying type to avoid exposing a bunch of unnecessary
// enums/structs in the class header file private methods' signature
enum Type : int enum Type : int
{ {
Open = 1, Open = 1,
@ -71,8 +76,31 @@ namespace SDActions
Properties Properties
}; };
struct Details
{
const char *iconName;
const char *actionName;
const char *collectionName;
};
// Allows to use a for loop to iterate over all action types // Allows to use a for loop to iterate over all action types
static const Type All[] = { Open, Mount, Unmount, Unlock, Lock, Eject, SafeRemove, Properties }; static const Type All[] = { Open, Mount, Unmount, Unlock, Lock, Eject, SafeRemove, Properties };
static const TQMap<Type, Details> populateMap()
{
TQMap<Type, Details> map;
map[Open] = { "window-new", I18N_NOOP("Open"), "open_menu" };
map[Mount] = { "drive-harddisk-mounted", I18N_NOOP("Mount"), "mount_menu" };
map[Unmount] = { "drive-harddisk-unmounted", I18N_NOOP("Unmount"), "unmount_menu" };
map[Unlock] = { "decrypted", I18N_NOOP("Unlock"), "unlock_menu" };
map[Lock] = { "encrypted", I18N_NOOP("Lock"), "lock_menu" };
map[Eject] = { "player_eject", I18N_NOOP("Eject"), "eject_menu" };
map[SafeRemove] = { "player_safe_removal", I18N_NOOP("Safe remove"), "safe_remove_menu" };
map[Properties] = { "edit", I18N_NOOP("Properties"), "properties_menu" };
return map;
}
static const TQMap<Type, Details> Data = populateMap();
} }
// Storage Device Action Menu Entry, representing an action // Storage Device Action Menu Entry, representing an action
@ -106,11 +134,11 @@ public:
TDEAction *m_quitAction; TDEAction *m_quitAction;
TDEAction *m_shortcutKeysAction; TDEAction *m_shortcutKeysAction;
KHelpMenu *m_helpMenu; KHelpMenu *m_helpMenu;
TDEPopupMenu *m_LMBMenu;
TDEPopupMenu *m_RMBMenu; TDEPopupMenu *m_RMBMenu;
int m_menuEntryIdx;
TQMap<int, SDActionMenuEntry> m_actionMenuEntryMap; TQMap<int, SDActionMenuEntry> m_actionMenuEntryMap;
TQMap<SDActions::Type, TDEActionMenu*> m_RMBActionMap; TQMap<SDActions::Type, TDEActionMenu*> m_RMBActionMenuMap;
TQMap<TQString, KnownDiskDeviceInfo> m_knownDiskDevices; TQMap<TQString, KnownDiskDeviceInfo> m_knownDiskDevices;
@ -125,7 +153,7 @@ HwDeviceSystemTray::HwDeviceSystemTray(TQWidget *parent, const char *name)
d->m_hardwareNotifierContainer = new TDEPassivePopupStackContainer(); d->m_hardwareNotifierContainer = new TDEPassivePopupStackContainer();
connect(d->m_hardwareNotifierContainer, TQT_SIGNAL(popupClicked(KPassivePopup*, TQPoint, TQString)), this, TQT_SLOT(devicePopupClicked(KPassivePopup*, TQPoint, TQString))); connect(d->m_hardwareNotifierContainer, TQT_SIGNAL(popupClicked(KPassivePopup*, TQPoint, TQString)), this, TQT_SLOT(devicePopupClicked(KPassivePopup*, TQPoint, TQString)));
InitRMBMenu(); initMenus();
setPixmap(KSystemTray::loadIcon("hwinfo")); setPixmap(KSystemTray::loadIcon("hwinfo"));
setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
@ -200,7 +228,8 @@ void HwDeviceSystemTray::mousePressEvent(TQMouseEvent* e)
switch (e->button()) switch (e->button())
{ {
case Qt::LeftButton: case Qt::LeftButton:
populateLMBMenu();
d->m_LMBMenu->popup(e->globalPos());
break; break;
case Qt::MidButton: case Qt::MidButton:
@ -259,31 +288,18 @@ bool HwDeviceSystemTray::isMonitoredDevice(TDEStorageDevice* sdevice)
sdevice->isDiskOfType(TDEDiskDeviceType::Camera))); sdevice->isDiskOfType(TDEDiskDeviceType::Camera)));
} }
void HwDeviceSystemTray::configChanged() { void HwDeviceSystemTray::initMenus()
//
}
void HwDeviceSystemTray::InitRMBMenu()
{ {
// RMB menu
d->m_RMBMenu = contextMenu(); d->m_RMBMenu = contextMenu();
// Device actions // Device actions
d->m_RMBActionMap.insert(SDActions::Open, new TDEActionMenu(i18n("Open"), for (const SDActions::Type &actionType : SDActions::All)
SmallIcon("window-new", TQIconSet::Automatic), actionCollection(), "open_menu")); {
d->m_RMBActionMap.insert(SDActions::Mount, new TDEActionMenu(i18n("Mount"), SDActions::Details ad = SDActions::Data[actionType];
SmallIcon("drive-harddisk-mounted", TQIconSet::Automatic), actionCollection(), "mount_menu")); d->m_RMBActionMenuMap.insert(actionType, new TDEActionMenu(i18n(ad.actionName),
d->m_RMBActionMap.insert(SDActions::Unmount, new TDEActionMenu(i18n("Unmount"), SmallIcon(ad.iconName, TQIconSet::Automatic), actionCollection(), ad.collectionName));
SmallIcon("drive-harddisk-unmounted", TQIconSet::Automatic), actionCollection(), "unmount_menu")); }
d->m_RMBActionMap.insert(SDActions::Unlock, new TDEActionMenu(i18n("Unlock"),
SmallIcon("decrypted", TQIconSet::Automatic), actionCollection(), "unlock_menu"));
d->m_RMBActionMap.insert(SDActions::Lock, new TDEActionMenu(i18n("Lock"),
SmallIcon("encrypted", TQIconSet::Automatic), actionCollection(), "lock_menu"));
d->m_RMBActionMap.insert(SDActions::Eject, new TDEActionMenu(i18n("Eject"),
SmallIcon("player_eject", TQIconSet::Automatic), actionCollection(), "eject_menu"));
d->m_RMBActionMap.insert(SDActions::SafeRemove, new TDEActionMenu(i18n("Safe remove"),
SmallIcon("player_safe_removal", TQIconSet::Automatic), actionCollection(), "safe_remove_menu"));
d->m_RMBActionMap.insert(SDActions::Properties, new TDEActionMenu(i18n("Properties"),
SmallIcon("edit", TQIconSet::Automatic), actionCollection(), "properties_menu"));
// Global Configuration // Global Configuration
d->m_deviceManagerAction = new TDEAction(i18n("Show Device Manager..."), SmallIconSet("kcmpci"), d->m_deviceManagerAction = new TDEAction(i18n("Show Device Manager..."), SmallIconSet("kcmpci"),
@ -295,19 +311,38 @@ void HwDeviceSystemTray::InitRMBMenu()
d->m_helpMenu = new KHelpMenu(this, TDEGlobal::instance()->aboutData(), false, actionCollection()); d->m_helpMenu = new KHelpMenu(this, TDEGlobal::instance()->aboutData(), false, actionCollection());
d->m_helpMenu->menu()->connectItem(KHelpMenu::menuHelpContents, this, TQT_SLOT(slotHelpContents())); d->m_helpMenu->menu()->connectItem(KHelpMenu::menuHelpContents, this, TQT_SLOT(slotHelpContents()));
d->m_quitAction = actionCollection()->action(KStdAction::name(KStdAction::Quit)); d->m_quitAction = actionCollection()->action(KStdAction::name(KStdAction::Quit));
// LMB menu
d->m_LMBMenu = new TDEPopupMenu(this);
} }
void HwDeviceSystemTray::AddDeviceToRMBMenu(TDEStorageDevice *sdevice, const int type) void HwDeviceSystemTray::AddDeviceToLMBMenu(TDEStorageDevice *sdevice, const int type,
TDEActionMenu *actionMenu, int &actionMenuIdx)
{ {
TQString friendlyName = !sdevice->diskLabel().isEmpty() ? sdevice->diskLabel() : sdevice->friendlyName(); TQString friendlyName = !sdevice->diskLabel().isEmpty() ?
sdevice->diskLabel() : sdevice->friendlyName();
TQString uuid = !sdevice->diskUUID().isEmpty() ? sdevice->diskUUID() : sdevice->systemPath(); TQString uuid = !sdevice->diskUUID().isEmpty() ? sdevice->diskUUID() : sdevice->systemPath();
SDActions::Type actionType = (SDActions::Type)type; SDActions::Type actionType = (SDActions::Type)type;
TDEActionMenu *actionMenu = d->m_RMBActionMap[actionType]; SDActions::Details ad = SDActions::Data[actionType];
actionMenu->popupMenu()->insertItem(SmallIcon(ad.iconName), i18n(ad.actionName), actionMenuIdx);
actionMenu->popupMenu()->connectItem(actionMenuIdx, this,
TQT_SLOT(slotExecuteDeviceAction(int)));
d->m_actionMenuEntryMap[actionMenuIdx++] = { actionType, uuid };
}
void HwDeviceSystemTray::AddDeviceToRMBMenu(TDEStorageDevice *sdevice, const int type, int &actionMenuIdx)
{
TQString friendlyName = !sdevice->diskLabel().isEmpty() ?
sdevice->diskLabel() : sdevice->friendlyName();
TQString uuid = !sdevice->diskUUID().isEmpty() ? sdevice->diskUUID() : sdevice->systemPath();
SDActions::Type actionType = (SDActions::Type)type;
TDEActionMenu *actionMenu = d->m_RMBActionMenuMap[actionType];
actionMenu->popupMenu()->insertItem(sdevice->icon(TDEIcon::SizeSmall), actionMenu->popupMenu()->insertItem(sdevice->icon(TDEIcon::SizeSmall),
i18n("%1 (%2)").arg(friendlyName, sdevice->deviceNode()), d->m_menuEntryIdx); i18n("%1 (%2)").arg(friendlyName, sdevice->deviceNode()), actionMenuIdx);
actionMenu->popupMenu()->connectItem(d->m_menuEntryIdx, this, TQT_SLOT(slotExecuteDeviceAction(int))); actionMenu->popupMenu()->connectItem(actionMenuIdx, this,
TQT_SLOT(slotExecuteDeviceAction(int)));
actionMenu->setEnabled(true); actionMenu->setEnabled(true);
d->m_actionMenuEntryMap[d->m_menuEntryIdx++] = { actionType, uuid }; d->m_actionMenuEntryMap[actionMenuIdx++] = { actionType, uuid };
} }
void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu) void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu)
@ -317,14 +352,14 @@ void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu)
for (const SDActions::Type &actionType : SDActions::All) for (const SDActions::Type &actionType : SDActions::All)
{ {
TDEActionMenu *actionMenu = d->m_RMBActionMap[actionType]; TDEActionMenu *actionMenu = d->m_RMBActionMenuMap[actionType];
actionMenu->popupMenu()->clear(); actionMenu->popupMenu()->clear();
actionMenu->setEnabled(false); actionMenu->setEnabled(false);
actionMenu->unplug(d->m_RMBMenu); actionMenu->unplug(d->m_RMBMenu);
} }
d->m_actionMenuEntryMap.clear(); d->m_actionMenuEntryMap.clear();
d->m_menuEntryIdx = 0; int actionMenuIdx = 0;
// Find all storage devices and add them to the popup menus // Find all storage devices and add them to the popup menus
TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices(); TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices();
@ -334,15 +369,16 @@ void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu)
TDEStorageDevice *sdevice = static_cast<TDEStorageDevice*>(hwdevice); TDEStorageDevice *sdevice = static_cast<TDEStorageDevice*>(hwdevice);
if (isMonitoredDevice(sdevice)) if (isMonitoredDevice(sdevice))
{ {
if (sdevice->isDiskOfType(TDEDiskDeviceType::LUKS) || sdevice->isDiskOfType(TDEDiskDeviceType::OtherCrypted)) if (sdevice->isDiskOfType(TDEDiskDeviceType::LUKS) ||
sdevice->isDiskOfType(TDEDiskDeviceType::OtherCrypted))
{ {
if (sdevice->isDiskOfType(TDEDiskDeviceType::UnlockedCrypt)) if (sdevice->isDiskOfType(TDEDiskDeviceType::UnlockedCrypt))
{ {
AddDeviceToRMBMenu(sdevice, SDActions::Lock); AddDeviceToRMBMenu(sdevice, SDActions::Lock, actionMenuIdx);
} }
else else
{ {
AddDeviceToRMBMenu(sdevice, SDActions::Unlock); AddDeviceToRMBMenu(sdevice, SDActions::Unlock, actionMenuIdx);
} }
} }
@ -350,27 +386,27 @@ void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu)
{ {
if (sdevice->mountPath().isEmpty()) if (sdevice->mountPath().isEmpty())
{ {
AddDeviceToRMBMenu(sdevice, SDActions::Mount); AddDeviceToRMBMenu(sdevice, SDActions::Mount, actionMenuIdx);
} }
else else
{ {
AddDeviceToRMBMenu(sdevice, SDActions::Unmount); AddDeviceToRMBMenu(sdevice, SDActions::Unmount, actionMenuIdx);
} }
// Mounted and unmounted disks can also be opened // Mounted and unmounted disks can also be opened
AddDeviceToRMBMenu(sdevice, SDActions::Open); AddDeviceToRMBMenu(sdevice, SDActions::Open, actionMenuIdx);
} }
if (sdevice->checkDiskStatus(TDEDiskDeviceStatus::Removable) || if (sdevice->checkDiskStatus(TDEDiskDeviceStatus::Removable) ||
sdevice->checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) sdevice->checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable))
{ {
AddDeviceToRMBMenu(sdevice, SDActions::Eject); AddDeviceToRMBMenu(sdevice, SDActions::Eject, actionMenuIdx);
AddDeviceToRMBMenu(sdevice, SDActions::SafeRemove); AddDeviceToRMBMenu(sdevice, SDActions::SafeRemove, actionMenuIdx);
} }
AddDeviceToRMBMenu(sdevice, SDActions::Properties); AddDeviceToRMBMenu(sdevice, SDActions::Properties, actionMenuIdx);
} }
} }
@ -378,10 +414,10 @@ void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu)
d->m_RMBMenu->insertTitle(SmallIcon("drive-harddisk-unmounted"), i18n("Storage Device Actions"), 0); d->m_RMBMenu->insertTitle(SmallIcon("drive-harddisk-unmounted"), i18n("Storage Device Actions"), 0);
for (const SDActions::Type &actionType : SDActions::All) for (const SDActions::Type &actionType : SDActions::All)
{ {
TDEActionMenu *actionMenu = d->m_RMBActionMap[actionType]; TDEActionMenu *actionMenu = d->m_RMBActionMenuMap[actionType];
if (actionMenu->isEnabled()) if (actionMenu->isEnabled())
{ {
actionMenu->plug(d->m_RMBMenu, (int)actionType); actionMenu->plug(d->m_RMBMenu);
} }
} }
@ -397,6 +433,76 @@ void HwDeviceSystemTray::contextMenuAboutToShow(TDEPopupMenu *menu)
d->m_quitAction->plug(menu); d->m_quitAction->plug(menu);
} }
void HwDeviceSystemTray::populateLMBMenu()
{
d->m_LMBMenu->clear();
d->m_LMBMenu->insertTitle(SmallIcon("drive-harddisk-unmounted"), i18n("Storage Devices"), 0);
d->m_actionMenuEntryMap.clear();
int actionMenuIdx = 0;
// Find all storage devices and add them to the popup menus
TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices();
TDEGenericHardwareList diskDeviceList = hwdevices->listByDeviceClass(TDEGenericDeviceType::Disk);
for (TDEGenericDevice *hwdevice = diskDeviceList.first(); hwdevice; hwdevice = diskDeviceList.next())
{
TDEStorageDevice *sdevice = static_cast<TDEStorageDevice*>(hwdevice);
if (isMonitoredDevice(sdevice) &&
(sdevice->isDiskOfType(TDEDiskDeviceType::LUKS) ||
sdevice->isDiskOfType(TDEDiskDeviceType::OtherCrypted) ||
sdevice->checkDiskStatus(TDEDiskDeviceStatus::Mountable) ||
sdevice->checkDiskStatus(TDEDiskDeviceStatus::Removable) ||
sdevice->checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)))
{
TQString friendlyName = !sdevice->diskLabel().isEmpty() ?
sdevice->diskLabel() : sdevice->friendlyName();
TDEActionMenu *actionMenu = new TDEActionMenu(i18n("%1 (%2)").arg(friendlyName,
sdevice->deviceNode()), sdevice->icon(TDEIcon::SizeSmall));
if (sdevice->checkDiskStatus(TDEDiskDeviceStatus::Mountable))
{
// Mounted and unmounted disks can also be opened
AddDeviceToLMBMenu(sdevice, SDActions::Open, actionMenu, actionMenuIdx);
if (sdevice->mountPath().isEmpty())
{
AddDeviceToLMBMenu(sdevice, SDActions::Mount, actionMenu, actionMenuIdx);
}
else
{
AddDeviceToLMBMenu(sdevice, SDActions::Unmount, actionMenu, actionMenuIdx);
}
}
if (sdevice->isDiskOfType(TDEDiskDeviceType::LUKS) ||
sdevice->isDiskOfType(TDEDiskDeviceType::OtherCrypted))
{
if (sdevice->isDiskOfType(TDEDiskDeviceType::UnlockedCrypt))
{
AddDeviceToLMBMenu(sdevice, SDActions::Lock, actionMenu, actionMenuIdx);
}
else
{
AddDeviceToLMBMenu(sdevice, SDActions::Unlock, actionMenu, actionMenuIdx);
}
}
if (sdevice->checkDiskStatus(TDEDiskDeviceStatus::Removable) ||
sdevice->checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable))
{
AddDeviceToLMBMenu(sdevice, SDActions::Eject, actionMenu, actionMenuIdx);
AddDeviceToLMBMenu(sdevice, SDActions::SafeRemove, actionMenu, actionMenuIdx);
}
AddDeviceToLMBMenu(sdevice, SDActions::Properties, actionMenu, actionMenuIdx);
actionMenu->plug(d->m_LMBMenu);
}
}
}
void HwDeviceSystemTray::slotExecuteDeviceAction(int parameter) void HwDeviceSystemTray::slotExecuteDeviceAction(int parameter)
{ {
TQString uuid = d->m_actionMenuEntryMap[parameter].uuid; TQString uuid = d->m_actionMenuEntryMap[parameter].uuid;

@ -42,8 +42,6 @@ public:
virtual void contextMenuAboutToShow(TDEPopupMenu *menu); virtual void contextMenuAboutToShow(TDEPopupMenu *menu);
void configChanged();
protected slots: protected slots:
void slotHardwareConfig(); void slotHardwareConfig();
void slotEditShortcutKeys(); void slotEditShortcutKeys();
@ -59,15 +57,18 @@ protected slots:
void doDiskNotifications(bool scanOnly); void doDiskNotifications(bool scanOnly);
protected: protected:
void initMenus();
void mousePressEvent(TQMouseEvent *e); void mousePressEvent(TQMouseEvent *e);
void populateLMBMenu();
void resizeEvent(TQResizeEvent *); void resizeEvent(TQResizeEvent *);
void resizeTrayIcon(); void resizeTrayIcon();
void showEvent(TQShowEvent *); void showEvent(TQShowEvent *);
static bool isMonitoredDevice(TDEStorageDevice *sdevice); static bool isMonitoredDevice(TDEStorageDevice *sdevice);
void InitRMBMenu(); void AddDeviceToLMBMenu(TDEStorageDevice *sdevice, const int type, TDEActionMenu *actionMenu,
void AddDeviceToRMBMenu(TDEStorageDevice *sdevice, const int type); int &actionMenuIdx);
void AddDeviceToRMBMenu(TDEStorageDevice *sdevice, const int type, int &actionMenuIdx);
HwDeviceSystemTrayPrivate *d; HwDeviceSystemTrayPrivate *d;
}; };

Loading…
Cancel
Save