DeviceModel.cpp 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**************************************************************************
*   Copyright (C) 2009-2010 Trever Fischer <tdfischer@fedoraproject.org>  *
*   Copyright (C) 2015 Kai Uwe Broulik <kde@privat.broulik.de>            *
*                                                                         *
*   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 .        *
***************************************************************************/

21 22 23 24 25 26 27 28
#include "DeviceModel.h"

#include <QIcon>

#include <KLocalizedString>
#include <Solid/DeviceNotifier>
#include <Solid/Device>
#include <Solid/StorageVolume>
29
#include <Solid/StorageAccess>
30 31 32

#include "AutomounterSettings.h"

33
DeviceModel::DeviceModel(AutomounterSettings *m_settings, QObject *parent)
34
    : QAbstractItemModel(parent)
35
    , m_settings(m_settings)
36 37 38
{
    reload();
    
39 40
    connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DeviceModel::deviceAttached);
    connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &DeviceModel::deviceRemoved);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

void DeviceModel::forgetDevice(const QString &udi)
{
    if (m_disconnected.contains(udi)) {
        const int deviceIndex = m_disconnected.indexOf(udi);
        beginRemoveRows(index(1, 0), deviceIndex, deviceIndex);
        m_disconnected.removeOne(udi);
        endRemoveRows();
    } else if (m_attached.contains(udi)) {
        const int deviceIndex = m_attached.indexOf(udi);
        beginRemoveRows(index(0, 0), deviceIndex, deviceIndex);
        m_attached.removeOne(udi);
        endRemoveRows();
    }
    m_loginForced.remove(udi);
    m_attachedForced.remove(udi);
}

QVariant DeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch(section) {
        case 0: return i18n("Device");
65 66
        case 1: return i18nc("As in automount on login", "On Login");
        case 2: return i18nc("As in automoount on attach", "On Attach");
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
        }
    }
    return QVariant();
}

void DeviceModel::deviceAttached(const QString &udi)
{
    const Solid::Device devive(udi);
    auto volume = devive.as<Solid::StorageVolume>();

    if (volume && !volume->isIgnored()) {
        if (m_disconnected.contains(udi)) {
            const int deviceIndex = m_disconnected.indexOf(udi);
            beginRemoveRows(index(1, 0), deviceIndex, deviceIndex);
            m_disconnected.removeOne(udi);
            endRemoveRows();
        }

        addNewDevice(udi);
    }
}

void DeviceModel::deviceRemoved(const QString &udi)
{
    if (m_attached.contains(udi)) {
        const int deviceIndex = m_attached.indexOf(udi);

        beginRemoveRows(index(0, 0), deviceIndex, deviceIndex);
        m_attached.removeOne(udi);
        endRemoveRows();

        // NOTE the device is not moved to the "Disconnected" section
        // when removing it while the KCM is opened because we need to check
        // whether the device that just got detached is ignored
        // (don't show partition tables and other garbage) but this information
        // is no longer available when the device is gone
    }
}

void DeviceModel::addNewDevice(const QString &udi)
{
108
    m_settings->load();
109 110

    if (!m_loginForced.contains(udi)) {
111
        m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Login);
112 113
    }
    if (!m_attachedForced.contains(udi)) {
114
        m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Attach);
115 116 117 118
    }

    const Solid::Device dev(udi);
    if (dev.isValid()) {
119 120 121 122 123 124 125 126
        if (dev.is<Solid::StorageAccess>()) {
            const Solid::StorageAccess *access = dev.as<Solid::StorageAccess>();
            if (!access->isIgnored() || !access->isAccessible()) {
                beginInsertRows(index(0, 0), m_attached.size(), m_attached.size());
                m_attached << udi;
                endInsertRows();
            }
        }
127 128 129
    } else {
        beginInsertRows(index(1, 0), m_disconnected.size(), m_disconnected.size());
        m_disconnected << udi;
130
        endInsertRows();
131 132 133 134 135 136 137 138 139 140 141
    }
}

void DeviceModel::reload()
{
    beginResetModel();
    m_loginForced.clear();
    m_attachedForced.clear();
    m_attached.clear();
    m_disconnected.clear();

142 143
    m_automaticLogin = m_settings->automountOnLogin();
    m_automaticAttached = m_settings->automountOnPlugin();
144

145
    const auto knownDevices = m_settings->knownDevices();
146
    for (const QString &dev : knownDevices) {
147 148
        addNewDevice(dev);
    }
149 150
    const auto keys = m_loginForced.keys();
    for (const QString &udi : keys) {
151 152
        m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Login);
        m_attachedForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Attach);
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
    }
    endResetModel();
}

QModelIndex DeviceModel::index(int row, int column, const QModelIndex &parent) const
{
    if (parent.isValid()) {
        if (parent.column() > 0) {
            return QModelIndex();
        }

        if (parent.row() == 0) {
            if (row >= 0 && row < m_attached.size() && column >= 0 && column <= 2) {
                return createIndex(row, column, static_cast<quintptr>(0));
            }
        } else if (parent.row() == 1) {
            if (row >= 0 && row < m_disconnected.size() && column >= 0 && column <= 2) {
                return createIndex(row, column, 1);
            }
        }
    } else {
         if ((row == 0 || row == 1) && column >= 0 && column <= 2) {
            return createIndex(row, column, 3);
         }
    }
    return QModelIndex();
}

QModelIndex DeviceModel::parent(const QModelIndex &index) const
{
    if (index.isValid()) {
        if (index.internalId() == 3)
            return QModelIndex();
        return createIndex(index.internalId(), 0, 3);
    }
    return QModelIndex();
}

Qt::ItemFlags DeviceModel::flags(const QModelIndex &index) const
{
193 194 195 196 197 198 199 200
    if (!index.isValid()) {
        return Qt::NoItemFlags;
    }

    if (!index.parent().isValid()) {
        // first child section elements
        if (m_automaticLogin && m_automaticAttached) {
            return Qt::NoItemFlags;
201 202 203 204 205
        } else {
            return Qt::ItemIsEnabled;
        }
    }

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
    switch (index.column()) {
    case 0:
        // first column
        if (m_automaticLogin && m_automaticAttached) {
            return Qt::NoItemFlags;
        } else {
            return Qt::ItemIsEnabled;
        }
    case 1:
        // on login column
        if (m_automaticLogin) {
            // automount on login was checked
            return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
        }
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
    case 2:
        // on attached column
        if (m_automaticAttached) {
            // automount on attach was checked
            return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
        }
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
    default:
        Q_UNREACHABLE();
    }
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
}

bool DeviceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::CheckStateRole && index.column() > 0) {
        const QString &udi = index.data(Qt::UserRole).toString();
        switch(index.column()) {
        case 1:
            m_loginForced[udi] = (value.toInt() == Qt::Checked) ? true : false;
            break;
        case 2:
            m_attachedForced[udi] = (value.toInt() == Qt::Checked) ? true : false;
            break;
        }
        emit dataChanged(index, index);
        return true;
    }

    return false;
}

QVariant DeviceModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid() && index.parent().isValid()) {
        if (index.parent().row() == 0) {
            if (role == TypeRole) {
				return Attached;
            }

            const QString &udi = m_attached.at(index.row());
            Solid::Device dev(udi);

            if (role == Qt::UserRole) {
                return udi;
            }

            if (index.column() == 0) {
                switch(role) {
                case Qt::DisplayRole:
                    return dev.description();
                case Qt::ToolTipRole:
                    return i18n("UDI: %1", udi);
                case Qt::DecorationRole:
                    return QIcon::fromTheme(dev.icon());
                }
            } else if (index.column() == 1) {
                switch(role) {
                case Qt::CheckStateRole:
                    return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked;
                case Qt::ToolTipRole:
281
                    if (m_loginForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Login))
282 283 284 285 286 287 288 289
                        return i18n("This device will be automatically mounted at login.");
                    return i18n("This device will not be automatically mounted at login.");
                }
            } else if (index.column() == 2) {
                switch(role) {
                case Qt::CheckStateRole:
                    return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked;
                case Qt::ToolTipRole:
290
                    if (m_attachedForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Attach))
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
                        return i18n("This device will be automatically mounted when attached.");
                    return i18n("This device will not be automatically mounted when attached.");
                }
            }
        } else if (index.parent().row() == 1) {
            if (role == TypeRole) {
				return Detatched;
            }

            const QString &udi = m_disconnected[index.row()];

            if (role == Qt::UserRole) {
                return udi;
            }

            if (index.column() == 0) {
                switch(role) {
                case Qt::DisplayRole:
309
                    return m_settings->getDeviceName(udi);
310 311 312
                case Qt::ToolTipRole:
                    return i18n("UDI: %1", udi);
                case Qt::DecorationRole:
313
                    return QIcon::fromTheme(m_settings->getDeviceIcon(udi));
314 315 316 317 318 319
                }
            } else if (index.column() == 1) {
                switch(role) {
                case Qt::CheckStateRole:
                    return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked;
                case Qt::ToolTipRole:
320
                    if (m_loginForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Login))
321 322 323 324 325 326 327 328
                        return i18n("This device will be automatically mounted at login.");
                    return i18n("This device will not be automatically mounted at login.");
                }
            } else if (index.column() == 2) {
                switch(role) {
                case Qt::CheckStateRole:
                    return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked;
                case Qt::ToolTipRole:
329
                    if (m_attachedForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Attach))
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
                        return i18n("This device will be automatically mounted when attached.");
                    return i18n("This device will not be automatically mounted when attached.");
                }
            }
        }
    } else if (index.isValid()) {
        if (role == Qt::DisplayRole && index.column() == 0) {
            if (index.row() == 0) {
                return i18n("Attached Devices");
            } else if (index.row() == 1) {
                return i18n("Disconnected Devices");
            }
        }
    }
    return QVariant();
}

int DeviceModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        if (parent.internalId() < 3 || parent.column() > 0) {
            return 0;
        }
        if (parent.row() == 0) {
            return m_attached.size();
        }

        return m_disconnected.size();
    }

    return 2;
}

int DeviceModel::columnCount(const QModelIndex &parent) const
{
365
    Q_UNUSED(parent)
366 367
    return 3;
}
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

void DeviceModel::setAutomaticMountOnLogin(bool automaticLogin)
{
    if (m_automaticLogin != automaticLogin) {
        m_automaticLogin = automaticLogin;
        for (int parent = 0; parent < rowCount(); parent++) {
            const auto parentIndex = index(parent, 0);
            emit dataChanged(index(0, 1, parentIndex), index(rowCount(parentIndex), 1, parentIndex));
        }
    }
}

void DeviceModel::setAutomaticMountOnPlugin(bool automaticAttached)
{
    if (m_automaticAttached != automaticAttached) {
        m_automaticAttached = automaticAttached;
        for (int parent = 0; parent < rowCount(); parent++) {
            const auto parentIndex = index(parent, 0);
            emit dataChanged(index(0, 2, parentIndex), index(rowCount(parentIndex), 2, parentIndex));
        }
    }
}