1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* SPDX-FileCopyrightText: 2024 Bohdan Onofriichuk <bogdan.onofriuchuk@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "devicefiltercontrol.h"
#include <devicenotifier_debug.h>
#include "devicecontrol.h"
#include "devicestatemonitor_p.h"
#include <Solid/Device>
#include <Solid/OpticalDrive>
DeviceFilterControl::DeviceFilterControl(QObject *parent)
: QSortFilterProxyModel(parent)
, m_filterType(Removable)
, m_isVisible(false)
, m_modelReset(false)
, m_spaceMonitor(SpaceMonitor::instance())
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Begin initializing Device Filter Control";
setSourceModel(new DeviceControl(this));
setDynamicSortFilter(false);
onModelReset();
connect(this, &DeviceFilterControl::rowsInserted, this, &DeviceFilterControl::onDeviceAdded);
connect(this, &DeviceFilterControl::rowsAboutToBeRemoved, this, &DeviceFilterControl::onDeviceRemoved);
connect(this, &DeviceFilterControl::modelReset, this, &DeviceFilterControl::onModelReset);
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control successfully initialized";
}
DeviceFilterControl::~DeviceFilterControl()
{
}
void DeviceFilterControl::unmountAllRemovables()
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: unmount all removables function invoked";
for (int position = 0; position < rowCount(); ++position) {
auto index = DeviceFilterControl::index(position, 0);
auto actionData = data(index, {DeviceControl::Actions});
if (!actionData.isNull()) {
auto actions = qvariant_cast<ActionsControl *>(actionData);
if (actions->isUnmountable()) {
actions->unmount();
}
}
}
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: unmount all removables function finished";
}
QBindable<QString> DeviceFilterControl::bindableLastUdi()
{
return &m_lastUdi;
}
QBindable<QString> DeviceFilterControl::bindableLastDescription()
{
return &m_lastDescription;
}
QBindable<QString> DeviceFilterControl::bindableLastIcon()
{
return &m_lastIcon;
}
QBindable<bool> DeviceFilterControl::bindableLastDeviceAdded()
{
return &m_lastDeviceAdded;
}
QBindable<qsizetype> DeviceFilterControl::bindableDeviceCount()
{
return &m_deviceCount;
}
QBindable<qsizetype> DeviceFilterControl::bindableUnmountableCount()
{
return &m_unmountableCount;
}
DeviceFilterControl::DevicesType DeviceFilterControl::filterType() const
{
return m_filterType;
}
void DeviceFilterControl::setFilterType(DevicesType type)
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: filter type: "
<< (type == Removable ? "Removable"
: type == Unremovable ? "Not removable"
: "All");
if (type != m_filterType) {
m_filterType = type;
m_modelReset = true;
invalidateRowsFilter();
onModelReset();
m_modelReset = false;
}
}
bool DeviceFilterControl::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
if (!index.isValid()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: index is invalid";
return false;
}
switch (m_filterType) {
case All:
return true;
case Removable:
return sourceModel()->data(index, DeviceControl::IsRemovable).toBool();
case Unremovable:
return !sourceModel()->data(index, DeviceControl::IsRemovable).toBool();
}
return false;
}
bool DeviceFilterControl::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
if (!source_left.isValid()) {
return true;
}
if (!source_right.isValid()) {
return false;
}
QString leftType = sourceModel()->data(source_left, DeviceControl::Type).toString();
QString rightType = sourceModel()->data(source_right, DeviceControl::Type).toString();
if (leftType < rightType) {
return true;
}
if (leftType > rightType) {
return false;
}
QDateTime leftTime = sourceModel()->data(source_left, DeviceControl::Timestamp).toDateTime();
QDateTime rightTime = sourceModel()->data(source_right, DeviceControl::Timestamp).toDateTime();
if (leftTime < rightTime) {
return false;
}
return true;
}
bool DeviceFilterControl::isVisible() const
{
return m_isVisible;
}
void DeviceFilterControl::setIsVisible(bool status)
{
m_isVisible = status;
m_spaceMonitor->setIsVisible(status);
}
void DeviceFilterControl::onDeviceAdded(const QModelIndex &parent, int first, int last)
{
Q_UNUSED(last);
if (m_modelReset) {
return;
}
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: rowInserted signal arrived";
m_deviceCount = rowCount(parent);
m_lastDeviceAdded = true;
QModelIndex index = DeviceFilterControl::index(first, 0, parent);
handleDeviceAdded(index);
sort(0, Qt::AscendingOrder);
}
void DeviceFilterControl::onDeviceRemoved(const QModelIndex &parent, int first, int last)
{
Q_UNUSED(last);
if (m_modelReset) {
return;
}
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: rowRemoved signal arrived";
m_deviceCount = rowCount() - 1;
QModelIndex index = DeviceFilterControl::index(first, 0, parent);
if (m_filterType != Unremovable) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: filter type is not Unremovable. updating unmountAll Action";
if (auto it = m_unmountableDevices.constFind(data(index, {DeviceControl::Udi}).toString()); it != m_unmountableDevices.constEnd()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: remove device " << data(index, {DeviceControl::Udi}).toString()
<< " from unmountable devices";
m_unmountableDevices.erase(it);
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << data(index, {DeviceControl::Udi}).toString()
<< "device is not unmountable. Skipping";
}
}
m_unmountableCount = m_unmountableDevices.count();
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: Unmountable count updated: " << m_unmountableCount.value();
if (m_lastUdi.value() == data(index, {DeviceControl::Udi}).toString()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << data(index, {DeviceControl::Udi}).toString()
<< "was last added device. Set new last device";
if (!m_deviceOrder.isEmpty()) {
m_lastDeviceAdded = false;
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << m_lastUdi.value() << "become new last device";
Solid::Device device = Solid::Device(m_deviceOrder.top());
m_lastIcon = device.icon();
m_lastDescription = device.description();
m_lastUdi = m_deviceOrder.pop();
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: no last devices found. Clear last device";
m_lastDeviceAdded = false;
m_lastIcon = QString();
m_lastDescription = QString();
m_lastUdi = QString();
}
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << data(index, {DeviceControl::Udi}).toString()
<< "is not last. Begin removing from device order";
for (int position = 0; position < m_deviceOrder.size(); ++position) {
if (m_deviceOrder[position] == data(index, {DeviceControl::Udi}).toString()) {
m_deviceOrder.removeAt(position);
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << data(index, {DeviceControl::Udi}).toString() << "removed at position "
<< position;
break;
}
}
}
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << data(index, {DeviceControl::Udi}).toString() << "successfully removed";
}
void DeviceFilterControl::onModelReset()
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: modelResetSignal arrived. Begin resetting model";
m_deviceOrder.clear();
m_unmountableDevices.clear();
m_deviceCount = rowCount();
m_lastDeviceAdded = false;
m_lastIcon = QString();
m_lastDescription = QString();
m_lastUdi = QString();
for (int modelPosition = 0; modelPosition < rowCount(); ++modelPosition) {
handleDeviceAdded(DeviceFilterControl::index(modelPosition, 0));
}
sort(0, Qt::AscendingOrder);
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: modelResetSignal arrived. Resetting model finished";
}
void DeviceFilterControl::onDeviceActionUnmountableChanged(const QString &udi, bool status)
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: DeviceActionUnmountable arrived for device" << udi;
if (status) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << udi << "added to unmountable devices";
m_unmountableDevices.insert(udi);
} else {
if (auto it = m_unmountableDevices.constFind(udi); it != m_unmountableDevices.constEnd()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << udi << "removed from unmountable devices";
m_unmountableDevices.erase(it);
}
}
m_unmountableCount = m_unmountableDevices.count();
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: Unmountable count updated: " << m_unmountableCount.value();
}
void DeviceFilterControl::handleDeviceAdded(const QModelIndex &index)
{
if (!index.isValid()) {
return;
}
if (!m_lastUdi.value().isEmpty()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: save last udi " << m_lastUdi.value();
m_deviceOrder.push(m_lastUdi);
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: no last udi present. Skipping";
}
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: Set new last Device " << data(index, {DeviceControl::Udi}).toString();
m_lastIcon = data(index, {DeviceControl::Icon}).toString();
m_lastDescription = data(index, {DeviceControl::Description}).toString();
m_lastUdi = data(index, {DeviceControl::Udi}).toString();
if (m_filterType != Unremovable) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: filter type is not Unremovable. updating unmountAll Action";
auto actionData = data(index, {DeviceControl::Actions});
if (!actionData.isNull()) {
auto actions = qvariant_cast<ActionsControl *>(actionData);
connect(actions, &ActionsControl::unmountActionIsValidChanged, this, &DeviceFilterControl::onDeviceActionUnmountableChanged);
if (actions->isUnmountable()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: add device " << data(index, {DeviceControl::Udi}).toString()
<< " to unmountable devices";
m_unmountableDevices.insert(data(index, {DeviceControl::Udi}).toString());
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Device Filter Control: device " << data(index, {DeviceControl::Udi}).toString()
<< "device is not unmountable. Skipping";
}
}
}
m_unmountableCount = m_unmountableDevices.count();
}
#include "moc_devicefiltercontrol.cpp"
|