2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
10 #include "backgroundlistmodel.h"
16 #include <KStandardDirs>
18 #include "backgroundpackage.h"
19 #include "backgrounddelegate.h"
21 BackgroundListModel::BackgroundListModel(float ratio
, QObject
*listener
)
22 : m_listener(listener
)
25 connect(&m_dirwatch
, SIGNAL(deleted(QString
)), listener
, SLOT(removeBackground(QString
)));
28 void BackgroundListModel::removeBackground(const QString
&path
)
31 while ((index
= indexOf(path
)) != -1) {
32 beginRemoveRows(QModelIndex(), index
, index
);
33 m_packages
.removeAt(index
);
38 void BackgroundListModel::reload()
40 reload(QStringList());
43 void BackgroundListModel::reload(const QStringList
& selected
)
45 QStringList dirs
= KGlobal::dirs()->findDirs("wallpaper", "");
46 QList
<Background
*> tmp
;
47 foreach (const QString
&file
, selected
) {
48 if (!contains(file
) && QFile::exists(file
)) {
49 tmp
<< new BackgroundFile(file
, m_ratio
);
52 foreach (const QString
&dir
, dirs
) {
53 tmp
+= findAllBackgrounds(this, dir
, m_ratio
);
56 // add new files to dirwatch
57 foreach (Background
*b
, tmp
) {
58 //TODO: packages need to be added to the dir watch as well
59 if (!m_dirwatch
.contains(b
->path())) {
60 m_dirwatch
.addFile(b
->path());
65 beginInsertRows(QModelIndex(), 0, tmp
.size() - 1);
66 m_packages
= tmp
+ m_packages
;
71 void BackgroundListModel::addBackground(const QString
& path
) {
72 if (!contains(path
)) {
73 if (!m_dirwatch
.contains(path
)) {
74 m_dirwatch
.addFile(path
);
76 beginInsertRows(QModelIndex(), 0, 0);
77 m_packages
.prepend(new BackgroundFile(path
, m_ratio
));
82 int BackgroundListModel::indexOf(const QString
&path
) const
84 for (int i
= 0; i
< m_packages
.size(); i
++) {
85 if (path
.startsWith(m_packages
[i
]->path())) {
92 bool BackgroundListModel::contains(const QString
&path
) const
94 return indexOf(path
) != -1;
97 BackgroundListModel::~BackgroundListModel()
99 foreach (Background
* pkg
, m_packages
) {
104 int BackgroundListModel::rowCount(const QModelIndex
&) const
106 return m_packages
.size();
109 QVariant
BackgroundListModel::data(const QModelIndex
&index
, int role
) const
111 if (!index
.isValid()) {
115 if (index
.row() >= m_packages
.size()) {
119 Background
*b
= package(index
.row());
125 case Qt::DisplayRole
:
127 case BackgroundDelegate::ScreenshotRole
: {
128 QPixmap pix
= b
->screenshot();
129 if (pix
.isNull() && !b
->screenshotGenerationStarted()) {
130 connect(b
, SIGNAL(screenshotDone(QPersistentModelIndex
)),
131 m_listener
, SLOT(updateScreenshot(QPersistentModelIndex
)),
132 Qt::QueuedConnection
);
133 b
->generateScreenshot(index
);
137 case BackgroundDelegate::AuthorRole
:
144 Background
* BackgroundListModel::package(int index
) const
146 return m_packages
.at(index
);
149 QList
<Background
*> BackgroundListModel::findAllBackgrounds(const BackgroundContainer
*container
,
150 const QString
&path
, float ratio
)
152 //kDebug() << "looking for" << path;
153 QList
<Background
*> res
;
155 // get all packages in this directory
156 //kDebug() << "getting packages";
157 QStringList packages
= Plasma::Package::listInstalledPaths(path
);
158 QSet
<QString
> validPackages
;
159 foreach (const QString
&packagePath
, packages
) {
160 std::auto_ptr
<Background
> pkg(new BackgroundPackage(path
+ packagePath
, ratio
));
161 if (pkg
->isValid() &&
162 (!container
|| !container
->contains(pkg
->path()))) {
163 res
.append(pkg
.release());
164 //kDebug() << " adding valid package:" << packagePath;
165 validPackages
<< packagePath
;
169 // search normal wallpapers
170 //kDebug() << "listing normal files";
173 filters
<< "*.png" << "*.jpeg" << "*.jpg" << "*.svg" << "*.svgz";
174 dir
.setNameFilters(filters
);
175 dir
.setFilter(QDir::Files
| QDir::Hidden
| QDir::Readable
);
176 QFileInfoList files
= dir
.entryInfoList();
177 foreach (const QFileInfo
&wp
, files
) {
178 if (!container
|| !container
->contains(wp
.filePath())) {
179 //kDebug() << " adding image file" << wp.filePath();
180 res
.append(new BackgroundFile(wp
.filePath(), ratio
));
184 // now recurse the dirs, skipping ones we found packages in
185 //kDebug() << "recursing dirs";
186 dir
.setFilter(QDir::AllDirs
| QDir::Readable
);
187 files
= dir
.entryInfoList();
188 //TODO: we should show a KProgressDialog here as this can take a while if someone
189 // indexes, say, their entire home directory!
190 foreach (const QFileInfo
&wp
, files
) {
191 QString name
= wp
.fileName();
192 if (name
!= "." && name
!= ".." && !validPackages
.contains(wp
.fileName())) {
193 //kDebug() << " " << name << wp.filePath();
194 res
+= findAllBackgrounds(container
, wp
.filePath(), ratio
);
198 //kDebug() << "completed.";