Changeset View
Changeset View
Standalone View
Standalone View
solid-device-automounter/kcm/DeviceModel.cpp
Show All 24 Lines | |||||
25 | #include <KLocalizedString> | 25 | #include <KLocalizedString> | ||
26 | #include <Solid/DeviceNotifier> | 26 | #include <Solid/DeviceNotifier> | ||
27 | #include <Solid/Device> | 27 | #include <Solid/Device> | ||
28 | #include <Solid/StorageVolume> | 28 | #include <Solid/StorageVolume> | ||
29 | #include <Solid/StorageAccess> | 29 | #include <Solid/StorageAccess> | ||
30 | 30 | | |||
31 | #include "AutomounterSettings.h" | 31 | #include "AutomounterSettings.h" | ||
32 | 32 | | |||
33 | DeviceModel::DeviceModel(QObject *parent) | 33 | DeviceModel::DeviceModel(AutomounterSettings *m_settings, QObject *parent) | ||
34 | : QAbstractItemModel(parent) | 34 | : QAbstractItemModel(parent) | ||
35 | , m_settings(m_settings) | ||||
ervin: m_settings on its own line? | |||||
35 | { | 36 | { | ||
36 | reload(); | 37 | reload(); | ||
37 | 38 | | |||
38 | connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DeviceModel::deviceAttached); | 39 | connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DeviceModel::deviceAttached); | ||
39 | connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &DeviceModel::deviceRemoved); | 40 | connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &DeviceModel::deviceRemoved); | ||
40 | } | 41 | } | ||
41 | 42 | | |||
42 | void DeviceModel::forgetDevice(const QString &udi) | 43 | void DeviceModel::forgetDevice(const QString &udi) | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 91 | if (m_attached.contains(udi)) { | |||
99 | // whether the device that just got detached is ignored | 100 | // whether the device that just got detached is ignored | ||
100 | // (don't show partition tables and other garbage) but this information | 101 | // (don't show partition tables and other garbage) but this information | ||
101 | // is no longer available when the device is gone | 102 | // is no longer available when the device is gone | ||
102 | } | 103 | } | ||
103 | } | 104 | } | ||
104 | 105 | | |||
105 | void DeviceModel::addNewDevice(const QString &udi) | 106 | void DeviceModel::addNewDevice(const QString &udi) | ||
106 | { | 107 | { | ||
107 | AutomounterSettings::self()->load(); | 108 | m_settings->load(); | ||
108 | 109 | | |||
109 | if (!m_loginForced.contains(udi)) { | 110 | if (!m_loginForced.contains(udi)) { | ||
110 | m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Login); | 111 | m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Login); | ||
111 | } | 112 | } | ||
112 | if (!m_attachedForced.contains(udi)) { | 113 | if (!m_attachedForced.contains(udi)) { | ||
113 | m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Attach); | 114 | m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Attach); | ||
114 | } | 115 | } | ||
115 | 116 | | |||
116 | const Solid::Device dev(udi); | 117 | const Solid::Device dev(udi); | ||
117 | if (dev.isValid()) { | 118 | if (dev.isValid()) { | ||
118 | if (dev.is<Solid::StorageAccess>()) { | 119 | if (dev.is<Solid::StorageAccess>()) { | ||
119 | const Solid::StorageAccess *access = dev.as<Solid::StorageAccess>(); | 120 | const Solid::StorageAccess *access = dev.as<Solid::StorageAccess>(); | ||
120 | if (!access->isIgnored() || !access->isAccessible()) { | 121 | if (!access->isIgnored() || !access->isAccessible()) { | ||
121 | beginInsertRows(index(0, 0), m_attached.size(), m_attached.size()); | 122 | beginInsertRows(index(0, 0), m_attached.size(), m_attached.size()); | ||
Show All 11 Lines | |||||
133 | void DeviceModel::reload() | 134 | void DeviceModel::reload() | ||
134 | { | 135 | { | ||
135 | beginResetModel(); | 136 | beginResetModel(); | ||
136 | m_loginForced.clear(); | 137 | m_loginForced.clear(); | ||
137 | m_attachedForced.clear(); | 138 | m_attachedForced.clear(); | ||
138 | m_attached.clear(); | 139 | m_attached.clear(); | ||
139 | m_disconnected.clear(); | 140 | m_disconnected.clear(); | ||
140 | 141 | | |||
141 | m_automaticLogin = AutomounterSettings::automountOnLogin(); | 142 | m_automaticLogin = m_settings->automountOnLogin(); | ||
142 | m_automaticAttached = AutomounterSettings::automountOnPlugin(); | 143 | m_automaticAttached = m_settings->automountOnPlugin(); | ||
143 | 144 | | |||
144 | const auto knownDevices = AutomounterSettings::knownDevices(); | 145 | const auto knownDevices = m_settings->knownDevices(); | ||
145 | for (const QString &dev : knownDevices) { | 146 | for (const QString &dev : knownDevices) { | ||
146 | addNewDevice(dev); | 147 | addNewDevice(dev); | ||
147 | } | 148 | } | ||
148 | const auto keys = m_loginForced.keys(); | 149 | const auto keys = m_loginForced.keys(); | ||
149 | for (const QString &udi : keys) { | 150 | for (const QString &udi : keys) { | ||
150 | m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Login); | 151 | m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Login); | ||
151 | m_attachedForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Attach); | 152 | m_attachedForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Attach); | ||
152 | } | 153 | } | ||
153 | endResetModel(); | 154 | endResetModel(); | ||
154 | } | 155 | } | ||
155 | 156 | | |||
156 | QModelIndex DeviceModel::index(int row, int column, const QModelIndex &parent) const | 157 | QModelIndex DeviceModel::index(int row, int column, const QModelIndex &parent) const | ||
157 | { | 158 | { | ||
158 | if (parent.isValid()) { | 159 | if (parent.isValid()) { | ||
159 | if (parent.column() > 0) { | 160 | if (parent.column() > 0) { | ||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Line(s) | 267 | if (index.column() == 0) { | |||
272 | case Qt::DecorationRole: | 273 | case Qt::DecorationRole: | ||
273 | return QIcon::fromTheme(dev.icon()); | 274 | return QIcon::fromTheme(dev.icon()); | ||
274 | } | 275 | } | ||
275 | } else if (index.column() == 1) { | 276 | } else if (index.column() == 1) { | ||
276 | switch(role) { | 277 | switch(role) { | ||
277 | case Qt::CheckStateRole: | 278 | case Qt::CheckStateRole: | ||
278 | return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked; | 279 | return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked; | ||
279 | case Qt::ToolTipRole: | 280 | case Qt::ToolTipRole: | ||
280 | if (m_loginForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Login)) | 281 | if (m_loginForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Login)) | ||
281 | return i18n("This device will be automatically mounted at login."); | 282 | return i18n("This device will be automatically mounted at login."); | ||
282 | return i18n("This device will not be automatically mounted at login."); | 283 | return i18n("This device will not be automatically mounted at login."); | ||
283 | } | 284 | } | ||
284 | } else if (index.column() == 2) { | 285 | } else if (index.column() == 2) { | ||
285 | switch(role) { | 286 | switch(role) { | ||
286 | case Qt::CheckStateRole: | 287 | case Qt::CheckStateRole: | ||
287 | return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked; | 288 | return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked; | ||
288 | case Qt::ToolTipRole: | 289 | case Qt::ToolTipRole: | ||
289 | if (m_attachedForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Attach)) | 290 | if (m_attachedForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Attach)) | ||
290 | return i18n("This device will be automatically mounted when attached."); | 291 | return i18n("This device will be automatically mounted when attached."); | ||
291 | return i18n("This device will not be automatically mounted when attached."); | 292 | return i18n("This device will not be automatically mounted when attached."); | ||
292 | } | 293 | } | ||
293 | } | 294 | } | ||
294 | } else if (index.parent().row() == 1) { | 295 | } else if (index.parent().row() == 1) { | ||
295 | if (role == TypeRole) { | 296 | if (role == TypeRole) { | ||
296 | return Detatched; | 297 | return Detatched; | ||
297 | } | 298 | } | ||
298 | 299 | | |||
299 | const QString &udi = m_disconnected[index.row()]; | 300 | const QString &udi = m_disconnected[index.row()]; | ||
300 | 301 | | |||
301 | if (role == Qt::UserRole) { | 302 | if (role == Qt::UserRole) { | ||
302 | return udi; | 303 | return udi; | ||
303 | } | 304 | } | ||
304 | 305 | | |||
305 | if (index.column() == 0) { | 306 | if (index.column() == 0) { | ||
306 | switch(role) { | 307 | switch(role) { | ||
307 | case Qt::DisplayRole: | 308 | case Qt::DisplayRole: | ||
308 | return AutomounterSettings::getDeviceName(udi); | 309 | return m_settings->getDeviceName(udi); | ||
309 | case Qt::ToolTipRole: | 310 | case Qt::ToolTipRole: | ||
310 | return i18n("UDI: %1", udi); | 311 | return i18n("UDI: %1", udi); | ||
311 | case Qt::DecorationRole: | 312 | case Qt::DecorationRole: | ||
312 | return QIcon::fromTheme(AutomounterSettings::getDeviceIcon(udi)); | 313 | return QIcon::fromTheme(m_settings->getDeviceIcon(udi)); | ||
313 | } | 314 | } | ||
314 | } else if (index.column() == 1) { | 315 | } else if (index.column() == 1) { | ||
315 | switch(role) { | 316 | switch(role) { | ||
316 | case Qt::CheckStateRole: | 317 | case Qt::CheckStateRole: | ||
317 | return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked; | 318 | return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked; | ||
318 | case Qt::ToolTipRole: | 319 | case Qt::ToolTipRole: | ||
319 | if (m_loginForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Login)) | 320 | if (m_loginForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Login)) | ||
320 | return i18n("This device will be automatically mounted at login."); | 321 | return i18n("This device will be automatically mounted at login."); | ||
321 | return i18n("This device will not be automatically mounted at login."); | 322 | return i18n("This device will not be automatically mounted at login."); | ||
322 | } | 323 | } | ||
323 | } else if (index.column() == 2) { | 324 | } else if (index.column() == 2) { | ||
324 | switch(role) { | 325 | switch(role) { | ||
325 | case Qt::CheckStateRole: | 326 | case Qt::CheckStateRole: | ||
326 | return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked; | 327 | return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked; | ||
327 | case Qt::ToolTipRole: | 328 | case Qt::ToolTipRole: | ||
328 | if (m_attachedForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Attach)) | 329 | if (m_attachedForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Attach)) | ||
329 | return i18n("This device will be automatically mounted when attached."); | 330 | return i18n("This device will be automatically mounted when attached."); | ||
330 | return i18n("This device will not be automatically mounted when attached."); | 331 | return i18n("This device will not be automatically mounted when attached."); | ||
331 | } | 332 | } | ||
332 | } | 333 | } | ||
333 | } | 334 | } | ||
334 | } else if (index.isValid()) { | 335 | } else if (index.isValid()) { | ||
335 | if (role == Qt::DisplayRole && index.column() == 0) { | 336 | if (role == Qt::DisplayRole && index.column() == 0) { | ||
336 | if (index.row() == 0) { | 337 | if (index.row() == 0) { | ||
Show All 19 Lines | 349 | if (parent.isValid()) { | |||
356 | return m_disconnected.size(); | 357 | return m_disconnected.size(); | ||
357 | } | 358 | } | ||
358 | 359 | | |||
359 | return 2; | 360 | return 2; | ||
360 | } | 361 | } | ||
361 | 362 | | |||
362 | int DeviceModel::columnCount(const QModelIndex &parent) const | 363 | int DeviceModel::columnCount(const QModelIndex &parent) const | ||
363 | { | 364 | { | ||
364 | Q_UNUSED(parent); | 365 | Q_UNUSED(parent) | ||
365 | return 3; | 366 | return 3; | ||
366 | } | 367 | } | ||
367 | 368 | | |||
368 | void DeviceModel::setAutomaticMountOnLogin(bool automaticLogin) | 369 | void DeviceModel::setAutomaticMountOnLogin(bool automaticLogin) | ||
369 | { | 370 | { | ||
370 | if (m_automaticLogin != automaticLogin) { | 371 | if (m_automaticLogin != automaticLogin) { | ||
371 | m_automaticLogin = automaticLogin; | 372 | m_automaticLogin = automaticLogin; | ||
372 | for (int parent = 0; parent < rowCount(); parent++) { | 373 | for (int parent = 0; parent < rowCount(); parent++) { | ||
Show All 16 Lines |
m_settings on its own line?