Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/backgroundlistmodel.cpp
Show All 14 Lines | |||||
15 | * along with this program; if not, write to the * | 15 | * along with this program; if not, write to the * | ||
16 | * Free Software Foundation, Inc., * | 16 | * Free Software Foundation, Inc., * | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #ifndef BACKGROUNDLISTMODEL_CPP | 20 | #ifndef BACKGROUNDLISTMODEL_CPP | ||
21 | #define BACKGROUNDLISTMODEL_CPP | 21 | #define BACKGROUNDLISTMODEL_CPP | ||
22 | 22 | | |||
23 | #include "debug.h" | ||||
23 | #include "backgroundlistmodel.h" | 24 | #include "backgroundlistmodel.h" | ||
24 | 25 | | |||
25 | #include <QFile> | 26 | #include <QFile> | ||
26 | #include <QDir> | 27 | #include <QDir> | ||
27 | #include <QStandardPaths> | 28 | #include <QStandardPaths> | ||
28 | #include <QThreadPool> | 29 | #include <QThreadPool> | ||
29 | #include <QUuid> | 30 | #include <QUuid> | ||
30 | #include <QGuiApplication> | 31 | #include <QGuiApplication> | ||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Line(s) | 115 | if (!m_packages.isEmpty()) { | |||
118 | emit countChanged(); | 119 | emit countChanged(); | ||
119 | } | 120 | } | ||
120 | 121 | | |||
121 | if (!m_wallpaper) { | 122 | if (!m_wallpaper) { | ||
122 | return; | 123 | return; | ||
123 | } | 124 | } | ||
124 | 125 | | |||
125 | if (!selected.isEmpty()) { | 126 | if (!selected.isEmpty()) { | ||
126 | qDebug() << "selected" << selected; | 127 | qCDebug(IMAGEWALLPAPER) << "selected" << selected; | ||
127 | processPaths(selected); | 128 | processPaths(selected); | ||
128 | } | 129 | } | ||
129 | 130 | | |||
130 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("wallpapers/"), QStandardPaths::LocateDirectory); | 131 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("wallpapers/"), QStandardPaths::LocateDirectory); | ||
131 | qDebug() << " WP : -------" << dirs; | 132 | qCDebug(IMAGEWALLPAPER) << "Looking into" << dirs << "for wallpapers"; | ||
132 | 133 | | |||
133 | BackgroundFinder *finder = new BackgroundFinder(m_wallpaper.data(), dirs); | 134 | BackgroundFinder *finder = new BackgroundFinder(m_wallpaper.data(), dirs); | ||
134 | connect(finder, &BackgroundFinder::backgroundsFound, this, &BackgroundListModel::backgroundsFound); | 135 | connect(finder, &BackgroundFinder::backgroundsFound, this, &BackgroundListModel::backgroundsFound); | ||
135 | m_findToken = finder->token(); | 136 | m_findToken = finder->token(); | ||
136 | finder->start(); | 137 | finder->start(); | ||
137 | m_removableWallpapers = QSet<QString>::fromList(selected); | 138 | m_removableWallpapers = QSet<QString>::fromList(selected); | ||
138 | } | 139 | } | ||
139 | 140 | | |||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Line(s) | 149 | { | |||
201 | 202 | | |||
202 | if (!newPackages.isEmpty()) { | 203 | if (!newPackages.isEmpty()) { | ||
203 | const int start = rowCount(); | 204 | const int start = rowCount(); | ||
204 | beginInsertRows(QModelIndex(), start, start + newPackages.size() - 1); | 205 | beginInsertRows(QModelIndex(), start, start + newPackages.size() - 1); | ||
205 | m_packages.append(newPackages); | 206 | m_packages.append(newPackages); | ||
206 | endInsertRows(); | 207 | endInsertRows(); | ||
207 | emit countChanged(); | 208 | emit countChanged(); | ||
208 | } | 209 | } | ||
209 | //qDebug() << t.elapsed(); | 210 | //qCDebug(IMAGEWALLPAPER) << t.elapsed(); | ||
210 | } | 211 | } | ||
211 | 212 | | |||
212 | void BackgroundListModel::addBackground(const QString& path) | 213 | void BackgroundListModel::addBackground(const QString& path) | ||
213 | { | 214 | { | ||
214 | if (!m_wallpaper || !contains(path)) { | 215 | if (!m_wallpaper || !contains(path)) { | ||
215 | if (!m_dirwatch.contains(path)) { | 216 | if (!m_dirwatch.contains(path)) { | ||
216 | m_dirwatch.addFile(path); | 217 | m_dirwatch.addFile(path); | ||
217 | } | 218 | } | ||
218 | beginInsertRows(QModelIndex(), 0, 0); | 219 | beginInsertRows(QModelIndex(), 0, 0); | ||
219 | KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); | 220 | KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images")); | ||
220 | 221 | | |||
221 | m_removableWallpapers.insert(path); | 222 | m_removableWallpapers.insert(path); | ||
222 | package.setPath(path); | 223 | package.setPath(path); | ||
223 | m_wallpaper->findPreferedImageInPackage(package); | 224 | m_wallpaper->findPreferedImageInPackage(package); | ||
224 | qDebug() << "WP Bckground added " << path << package.isValid(); | 225 | qCDebug(IMAGEWALLPAPER) << "Background added " << path << package.isValid(); | ||
225 | m_packages.prepend(package); | 226 | m_packages.prepend(package); | ||
226 | endInsertRows(); | 227 | endInsertRows(); | ||
227 | emit countChanged(); | 228 | emit countChanged(); | ||
228 | } | 229 | } | ||
229 | } | 230 | } | ||
230 | 231 | | |||
231 | int BackgroundListModel::indexOf(const QString &path) const | 232 | int BackgroundListModel::indexOf(const QString &path) const | ||
232 | { | 233 | { | ||
233 | for (int i = 0; i < m_packages.size(); i++) { | 234 | for (int i = 0; i < m_packages.size(); i++) { | ||
234 | // packages will end with a '/', but the path passed in may not | 235 | // packages will end with a '/', but the path passed in may not | ||
235 | QString package = m_packages[i].path(); | 236 | QString package = m_packages[i].path(); | ||
236 | if (package.at(package.length() - 1) == QChar::fromLatin1('/')) { | 237 | if (package.at(package.length() - 1) == QChar::fromLatin1('/')) { | ||
237 | package.truncate(package.length() - 1); | 238 | package.truncate(package.length() - 1); | ||
238 | } | 239 | } | ||
239 | //remove eventual file:/// | 240 | //remove eventual file:/// | ||
240 | const QString filteredPath = QUrl(path).path(); | 241 | const QString filteredPath = QUrl(path).path(); | ||
241 | 242 | | |||
242 | if (filteredPath.startsWith(package)) { | 243 | if (filteredPath.startsWith(package)) { | ||
243 | // FIXME: ugly hack to make a difference between local files in the same dir | 244 | // FIXME: ugly hack to make a difference between local files in the same dir | ||
244 | // package->path does not contain the actual file name | 245 | // package->path does not contain the actual file name | ||
245 | qDebug() << "WP prefix" << m_packages[i].contentsPrefixPaths() << m_packages[i].filePath("preferred") << package << filteredPath; | 246 | qCDebug(IMAGEWALLPAPER) << "prefix" << m_packages[i].contentsPrefixPaths() << m_packages[i].filePath("preferred") << package << filteredPath; | ||
246 | QStringList ps = m_packages[i].contentsPrefixPaths(); | 247 | QStringList ps = m_packages[i].contentsPrefixPaths(); | ||
247 | bool prefixempty = ps.count() == 0; | 248 | bool prefixempty = ps.count() == 0; | ||
248 | if (!prefixempty) { | 249 | if (!prefixempty) { | ||
249 | prefixempty = ps[0].isEmpty(); | 250 | prefixempty = ps[0].isEmpty(); | ||
250 | } | 251 | } | ||
251 | 252 | | |||
252 | //For local files (user wallpapers) filteredPath == m_packages[i].filePath("preferred") | 253 | //For local files (user wallpapers) filteredPath == m_packages[i].filePath("preferred") | ||
253 | //E.X. filteredPath = "/home/kde/next.png" | 254 | //E.X. filteredPath = "/home/kde/next.png" | ||
254 | //m_packages[i].filePath("preferred") = "/home/kde/next.png" | 255 | //m_packages[i].filePath("preferred") = "/home/kde/next.png" | ||
255 | // | 256 | // | ||
256 | //But for the system wallpapers this is not the case. filteredPath != m_packages[i].filePath("preferred") | 257 | //But for the system wallpapers this is not the case. filteredPath != m_packages[i].filePath("preferred") | ||
257 | //E.X. filteredPath = /usr/share/wallpapers/Next/" | 258 | //E.X. filteredPath = /usr/share/wallpapers/Next/" | ||
258 | //m_packages[i].filePath("preferred") = "/usr/share/wallpapers/Next/contents/images/1920x1080.png" | 259 | //m_packages[i].filePath("preferred") = "/usr/share/wallpapers/Next/contents/images/1920x1080.png" | ||
259 | if ((filteredPath == m_packages[i].filePath("preferred")) || m_packages[i].filePath("preferred").contains(filteredPath)) { | 260 | if ((filteredPath == m_packages[i].filePath("preferred")) || m_packages[i].filePath("preferred").contains(filteredPath)) { | ||
260 | qDebug() << "WP TRUE" << (!m_packages[i].contentsPrefixPaths().isEmpty()) << (filteredPath == m_packages[i].filePath("preferred")); | | |||
261 | return i; | 261 | return i; | ||
262 | } | 262 | } | ||
263 | } | 263 | } | ||
264 | } | 264 | } | ||
265 | return -1; | 265 | return -1; | ||
266 | } | 266 | } | ||
267 | 267 | | |||
268 | bool BackgroundListModel::contains(const QString &path) const | 268 | bool BackgroundListModel::contains(const QString &path) const | ||
269 | { | 269 | { | ||
270 | //qDebug() << "WP contains: " << path << indexOf(path).isValid(); | 270 | //qCDebug(IMAGEWALLPAPER) << "WP contains: " << path << indexOf(path).isValid(); | ||
271 | return indexOf(path) >= 0; | 271 | return indexOf(path) >= 0; | ||
272 | } | 272 | } | ||
273 | 273 | | |||
274 | int BackgroundListModel::rowCount(const QModelIndex &) const | 274 | int BackgroundListModel::rowCount(const QModelIndex &) const | ||
275 | { | 275 | { | ||
276 | return m_packages.size(); | 276 | return m_packages.size(); | ||
277 | } | 277 | } | ||
278 | 278 | | |||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | 315 | { | |||
339 | break; | 339 | break; | ||
340 | 340 | | |||
341 | case ScreenshotRole: { | 341 | case ScreenshotRole: { | ||
342 | QPixmap preview = QPixmap(QSize(m_screenshotSize*1.6, | 342 | QPixmap preview = QPixmap(QSize(m_screenshotSize*1.6, | ||
343 | m_screenshotSize)); | 343 | m_screenshotSize)); | ||
344 | if (m_imageCache->findPixmap(b.filePath("preferred"), &preview)) { | 344 | if (m_imageCache->findPixmap(b.filePath("preferred"), &preview)) { | ||
345 | return preview; | 345 | return preview; | ||
346 | } | 346 | } | ||
347 | // qDebug() << "WP preferred: " << b.filePath("preferred"); | 347 | // qCDebug(IMAGEWALLPAPER) << "WP preferred: " << b.filePath("preferred"); | ||
348 | // qDebug() << "WP screenshot: " << b.filePath("screenshot"); | 348 | // qCDebug(IMAGEWALLPAPER) << "WP screenshot: " << b.filePath("screenshot"); | ||
349 | QUrl file = QUrl::fromLocalFile(b.filePath("preferred")); | 349 | QUrl file = QUrl::fromLocalFile(b.filePath("preferred")); | ||
350 | if (!m_previewJobs.contains(file) && file.isValid()) { | 350 | if (!m_previewJobs.contains(file) && file.isValid()) { | ||
351 | 351 | | |||
352 | KFileItemList list; | 352 | KFileItemList list; | ||
353 | list.append(KFileItem(file, QString(), 0)); | 353 | list.append(KFileItem(file, QString(), 0)); | ||
354 | QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); | 354 | QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); | ||
355 | KIO::PreviewJob* job = KIO::filePreview(list, | 355 | KIO::PreviewJob* job = KIO::filePreview(list, | ||
356 | QSize(m_screenshotSize*1.6, | 356 | QSize(m_screenshotSize*1.6, | ||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Line(s) | 441 | { | |||
451 | } | 451 | } | ||
452 | 452 | | |||
453 | KPackage::Package b = package(index.row()); | 453 | KPackage::Package b = package(index.row()); | ||
454 | if (!b.isValid()) { | 454 | if (!b.isValid()) { | ||
455 | return; | 455 | return; | ||
456 | } | 456 | } | ||
457 | 457 | | |||
458 | m_imageCache->insertPixmap(b.filePath("preferred"), preview); | 458 | m_imageCache->insertPixmap(b.filePath("preferred"), preview); | ||
459 | //qDebug() << "WP preview size:" << preview.size(); | 459 | //qCDebug(IMAGEWALLPAPER) << "WP preview size:" << preview.size(); | ||
460 | emit dataChanged(index, index); | 460 | emit dataChanged(index, index); | ||
461 | } | 461 | } | ||
462 | 462 | | |||
463 | void BackgroundListModel::previewFailed(const KFileItem &item) | 463 | void BackgroundListModel::previewFailed(const KFileItem &item) | ||
464 | { | 464 | { | ||
465 | m_previewJobs.remove(item.url()); | 465 | m_previewJobs.remove(item.url()); | ||
466 | } | 466 | } | ||
467 | 467 | | |||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 536 | { | |||
546 | 546 | | |||
547 | int i; | 547 | int i; | ||
548 | for (i = 0; i < m_paths.count(); ++i) { | 548 | for (i = 0; i < m_paths.count(); ++i) { | ||
549 | const QString path = m_paths.at(i); | 549 | const QString path = m_paths.at(i); | ||
550 | dir.setPath(path); | 550 | dir.setPath(path); | ||
551 | const QFileInfoList files = dir.entryInfoList(); | 551 | const QFileInfoList files = dir.entryInfoList(); | ||
552 | Q_FOREACH (const QFileInfo &wp, files) { | 552 | Q_FOREACH (const QFileInfo &wp, files) { | ||
553 | if (wp.isDir()) { | 553 | if (wp.isDir()) { | ||
554 | //qDebug() << "scanning directory" << wp.fileName(); | 554 | //qCDebug(IMAGEWALLPAPER) << "scanning directory" << wp.fileName(); | ||
555 | 555 | | |||
556 | const QString name = wp.fileName(); | 556 | const QString name = wp.fileName(); | ||
557 | if (name == QString::fromLatin1(".") || name == QString::fromLatin1("..")) { | 557 | if (name == QString::fromLatin1(".") || name == QString::fromLatin1("..")) { | ||
558 | // do nothing | 558 | // do nothing | ||
559 | continue; | 559 | continue; | ||
560 | } | 560 | } | ||
561 | 561 | | |||
562 | const QString filePath = wp.filePath(); | 562 | const QString filePath = wp.filePath(); | ||
563 | if (QFile::exists(filePath + QString::fromLatin1("/metadata.desktop")) || QFile::exists(filePath + QString::fromLatin1("/metadata.json"))) { | 563 | if (QFile::exists(filePath + QString::fromLatin1("/metadata.desktop")) || QFile::exists(filePath + QString::fromLatin1("/metadata.json"))) { | ||
564 | package.setPath(filePath); | 564 | package.setPath(filePath); | ||
565 | if (package.isValid()) { | 565 | if (package.isValid()) { | ||
566 | if (!package.filePath("images").isEmpty()) { | 566 | if (!package.filePath("images").isEmpty()) { | ||
567 | papersFound << package.path(); | 567 | papersFound << package.path(); | ||
568 | } | 568 | } | ||
569 | //qDebug() << "adding package" << wp.filePath(); | 569 | //qCDebug(IMAGEWALLPAPER) << "adding package" << wp.filePath(); | ||
570 | continue; | 570 | continue; | ||
571 | } | 571 | } | ||
572 | } | 572 | } | ||
573 | 573 | | |||
574 | // add this to the directories we should be looking at | 574 | // add this to the directories we should be looking at | ||
575 | m_paths.append(filePath); | 575 | m_paths.append(filePath); | ||
576 | } else { | 576 | } else { | ||
577 | //qDebug() << "adding image file" << wp.filePath(); | 577 | //qCDebug(IMAGEWALLPAPER) << "adding image file" << wp.filePath(); | ||
578 | papersFound << wp.filePath(); | 578 | papersFound << wp.filePath(); | ||
579 | } | 579 | } | ||
580 | } | 580 | } | ||
581 | } | 581 | } | ||
582 | 582 | | |||
583 | //qDebug() << "WP background found!" << papersFound.size() << "in" << i << "dirs, taking" << t.elapsed() << "ms"; | 583 | //qCDebug(IMAGEWALLPAPER) << "WP background found!" << papersFound.size() << "in" << i << "dirs, taking" << t.elapsed() << "ms"; | ||
584 | Q_EMIT backgroundsFound(papersFound, m_token); | 584 | Q_EMIT backgroundsFound(papersFound, m_token); | ||
585 | deleteLater(); | 585 | deleteLater(); | ||
586 | } | 586 | } | ||
587 | 587 | | |||
588 | 588 | | |||
589 | 589 | | |||
590 | #endif // BACKGROUNDLISTMODEL_CPP | 590 | #endif // BACKGROUNDLISTMODEL_CPP |