| 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2014 Canonical Ltd. |
| 4 | * Copyright 2014 Carlos J Mazieri <[email protected]> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Lesser General Public License as published by |
| 8 | * the Free Software Foundation; version 3. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * File: placesmodel.cpp |
| 19 | * Date: 21/04/2014 |
| 20 | */ |
| 21 | |
| 22 | #include "placesmodel.h" |
| 23 | #include "locationurl.h" |
| 24 | #include <QDir> |
| 25 | #include <QIcon> |
| 26 | #include <QStandardPaths> |
| 27 | |
| 28 | |
| 29 | PlacesModel::PlacesModel(QObject *parent) : |
| 30 | QAbstractListModel(parent) |
| 31 | { |
| 32 | QLatin1String doc("Documents"); |
| 33 | QLatin1String dow("Downloads"); |
| 34 | |
| 35 | addPlace("Home", ":/resources/resources/home-page.png", QStringList(QDir::homePath())); |
| 36 | |
| 37 | addPlace(doc, ":/resources/resources/document_folder.png", |
| 38 | QStringList() << QStandardPaths::displayName(QStandardPaths::DocumentsLocation) |
| 39 | << QDir::homePath() + QDir::separator() + doc |
| 40 | ); |
| 41 | |
| 42 | addPlace(dow, ":/resources/resources/downloads_folder.png", |
| 43 | QStringList() << QStandardPaths::displayName(QStandardPaths::DownloadLocation) |
| 44 | << QDir::homePath() + QDir::separator() + dow |
| 45 | ); |
| 46 | |
| 47 | addPlace("Temp", ":/resources/resources/temp_folder.png", QStringList(QDir::tempPath())); |
| 48 | |
| 49 | addPlace("Root", ":/resources/resources/red_folder.png", QStringList(QDir::rootPath())); |
| 50 | |
| 51 | addPlace("Trash", ":/resources/resources/recyclebin_full.png", QStringList(LocationUrl::TrashRootURL)); |
| 52 | |
| 53 | addPlace("samba/cifs", ":/resources/resources/workgroup.png", QStringList(LocationUrl::SmbURL)); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | QVariant PlacesModel::data(const QModelIndex &index, int role) const |
| 58 | { |
| 59 | if ( role == Qt::DisplayRole) |
| 60 | { |
| 61 | return m_places.at(index.row()).name; |
| 62 | } |
| 63 | if (role == Qt::DecorationRole) |
| 64 | { |
| 65 | return m_places.at(index.row()).icon; |
| 66 | } |
| 67 | return QVariant(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | QString PlacesModel::pathFrom(int row) const |
| 72 | { |
| 73 | return m_places.at(row).urlPath; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void PlacesModel::addPlace(const QString &n, const QString &i, const QStringList &u) |
| 78 | { |
| 79 | Place place(n,i); |
| 80 | if (u.count() == 1) |
| 81 | { |
| 82 | place.urlPath = u.at(0); |
| 83 | m_places.append(place); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | for (int counter = 0 ; counter < u.count(); ++counter) |
| 88 | { |
| 89 | if (QFileInfo(u.at(counter)).exists()) |
| 90 | { |
| 91 | place.urlPath = u.at(counter); |
| 92 | m_places.append(place); |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | } |