File indexing completed on 2024-05-05 17:04:27
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2012 Boudewijn Rempt <boud@kogmbh.com> 0003 * 0004 * This program is free software; you can redistribute it and/or modify 0005 * it under the terms of the GNU General Public License as published by 0006 * the Free Software Foundation; either version 2 of the License, or 0007 * (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program; if not, write to the Free Software 0016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0017 */ 0018 #include "RecentFilesModel.h" 0019 0020 #include "RecentFileManager.h" 0021 0022 #include <QFile> 0023 #include <QFileInfo> 0024 #include <QDateTime> 0025 0026 class RecentFilesModel::Private { 0027 public: 0028 0029 RecentFileManager *recentFileManager; 0030 }; 0031 0032 0033 RecentFilesModel::RecentFilesModel(QObject *parent) 0034 : QAbstractListModel(parent) 0035 , d(new Private()) 0036 { 0037 d->recentFileManager = 0; 0038 0039 QHash<int, QByteArray> roles; 0040 roles[ImageRole] = "image"; 0041 roles[TextRole] = "text"; 0042 roles[UrlRole] = "url"; 0043 roles[NameRole] = "name"; 0044 roles[DateRole] = "filedate"; 0045 setRoleNames(roles); 0046 } 0047 0048 RecentFilesModel::~RecentFilesModel() 0049 { 0050 delete d; 0051 } 0052 0053 int RecentFilesModel::rowCount(const QModelIndex &/*parent*/) const 0054 { 0055 if (d->recentFileManager) 0056 return d->recentFileManager->size(); 0057 else 0058 return 0; 0059 } 0060 0061 QVariant RecentFilesModel::data(const QModelIndex &index, int role) const 0062 { 0063 QVariant result; 0064 if (!d->recentFileManager) return result; 0065 if (index.isValid()) 0066 { 0067 Q_ASSERT(index.row() < d->recentFileManager->size()); 0068 0069 QString key = d->recentFileManager->recentFileName(index.row()); 0070 QString value = d->recentFileManager->recentFile(index.row()); 0071 0072 switch(role) 0073 { 0074 case ImageRole: 0075 result = QString("image://recentimage/%1").arg(value); 0076 break; 0077 case TextRole: 0078 result = QFileInfo(value).completeBaseName(); 0079 break; 0080 case UrlRole: 0081 result = value; 0082 break; 0083 case NameRole: 0084 result = key; 0085 case DateRole: 0086 { 0087 QFile f(value); 0088 if (f.exists()) { 0089 QFileInfo fi(value); 0090 result = fi.lastModified().toString("dd-mm-yyyy (hh:mm)"); 0091 } 0092 } 0093 default: 0094 result = ""; 0095 break; 0096 } 0097 } 0098 return result; 0099 } 0100 0101 QVariant RecentFilesModel::headerData(int section, Qt::Orientation orientation, int role) const 0102 { 0103 Q_UNUSED(orientation); 0104 QVariant result; 0105 if (section == 0) 0106 { 0107 switch(role) 0108 { 0109 case ImageRole: 0110 result = QString("Thumbnail"); 0111 break; 0112 case TextRole: 0113 result = QString("Name"); 0114 break; 0115 case UrlRole: 0116 case NameRole: 0117 case DateRole: 0118 default: 0119 result = ""; 0120 break; 0121 } 0122 } 0123 return result; 0124 } 0125 0126 QObject *RecentFilesModel::recentFileManager() const 0127 { 0128 return d->recentFileManager; 0129 } 0130 0131 void RecentFilesModel::setRecentFileManager(QObject *recentFileManager) 0132 { 0133 disconnect(d->recentFileManager); 0134 d->recentFileManager = qobject_cast<RecentFileManager*>(recentFileManager); 0135 connect(d->recentFileManager, SIGNAL(recentFilesListChanged()), SLOT(recentFilesListChanged())); 0136 emit recentFileManagerChanged(); 0137 } 0138 0139 void RecentFilesModel::recentFilesListChanged() 0140 { 0141 reset(); 0142 } 0143 0144 void RecentFilesModel::addRecent(const QString &_url) 0145 { 0146 if (d->recentFileManager) 0147 d->recentFileManager->addRecent(_url); 0148 }