• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

backgroundlistmodel.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
00003 
00004   This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008 */
00009 
00010 #include "backgroundlistmodel.h"
00011 
00012 #include <QFile>
00013 #include <QDir>
00014 
00015 #include <KGlobal>
00016 #include <KStandardDirs>
00017 
00018 #include "backgroundpackage.h"
00019 #include "backgrounddelegate.h"
00020 
00021 BackgroundListModel::BackgroundListModel(float ratio, QObject *listener)
00022 : m_listener(listener)
00023 , m_ratio(ratio)
00024 {
00025     connect(&m_dirwatch, SIGNAL(deleted(QString)), listener, SLOT(removeBackground(QString)));
00026 }
00027 
00028 void BackgroundListModel::removeBackground(const QString &path)
00029 {
00030     int index;
00031     while ((index = indexOf(path)) != -1) {
00032         beginRemoveRows(QModelIndex(), index, index);
00033         m_packages.removeAt(index);
00034         endRemoveRows();
00035     }
00036 }
00037 
00038 void BackgroundListModel::reload()
00039 {
00040     reload(QStringList());
00041 }
00042 
00043 void BackgroundListModel::reload(const QStringList& selected)
00044 {
00045     QStringList dirs = KGlobal::dirs()->findDirs("wallpaper", "");
00046     QList<Background *> tmp;
00047     foreach (const QString &file, selected) {
00048         if (!contains(file) && QFile::exists(file)) {
00049             tmp << new BackgroundFile(file, m_ratio);
00050         }
00051     }
00052     foreach (const QString &dir, dirs) {
00053         tmp += findAllBackgrounds(this, dir, m_ratio);
00054     }
00055 
00056     // add new files to dirwatch
00057     foreach (Background *b, tmp) {
00058         //TODO: packages need to be added to the dir watch as well
00059         if (!m_dirwatch.contains(b->path())) {
00060             m_dirwatch.addFile(b->path());
00061         }
00062     }
00063 
00064     if (!tmp.isEmpty()) {
00065         beginInsertRows(QModelIndex(), 0, tmp.size() - 1);
00066         m_packages = tmp + m_packages;
00067         endInsertRows();
00068     }
00069 }
00070 
00071 void BackgroundListModel::addBackground(const QString& path) {
00072     if (!contains(path)) {
00073         if (!m_dirwatch.contains(path)) {
00074             m_dirwatch.addFile(path);
00075         }
00076         beginInsertRows(QModelIndex(), 0, 0);
00077         m_packages.prepend(new BackgroundFile(path, m_ratio));
00078         endInsertRows();
00079     }
00080 }
00081 
00082 int BackgroundListModel::indexOf(const QString &path) const
00083 {
00084     for (int i = 0; i < m_packages.size(); i++) {
00085         if (path.startsWith(m_packages[i]->path())) {
00086             return i;
00087         }
00088     }
00089     return -1;
00090 }
00091 
00092 bool BackgroundListModel::contains(const QString &path) const
00093 {
00094     return indexOf(path) != -1;
00095 }
00096 
00097 BackgroundListModel::~BackgroundListModel()
00098 {
00099     foreach (Background* pkg, m_packages) {
00100         delete pkg;
00101     }
00102 }
00103 
00104 int BackgroundListModel::rowCount(const QModelIndex &) const
00105 {
00106     return m_packages.size();
00107 }
00108 
00109 QVariant BackgroundListModel::data(const QModelIndex &index, int role) const
00110 {
00111     if (!index.isValid()) {
00112         return QVariant();
00113     }
00114 
00115     if (index.row() >= m_packages.size()) {
00116         return QVariant();
00117     }
00118 
00119     Background *b = package(index.row());
00120     if (!b) {
00121         return QVariant();
00122     }
00123 
00124     switch (role) {
00125     case Qt::DisplayRole:
00126         return b->title();
00127     case BackgroundDelegate::ScreenshotRole: {
00128         QPixmap pix = b->screenshot();
00129         if (pix.isNull() && !b->screenshotGenerationStarted()) {
00130             connect(b, SIGNAL(screenshotDone(QPersistentModelIndex)),
00131                     m_listener, SLOT(updateScreenshot(QPersistentModelIndex)),
00132                     Qt::QueuedConnection);
00133             b->generateScreenshot(index);
00134         }
00135         return pix;
00136     }
00137     case BackgroundDelegate::AuthorRole:
00138         return b->author();
00139     default:
00140         return QVariant();
00141     }
00142 }
00143 
00144 Background* BackgroundListModel::package(int index) const
00145 {
00146     return m_packages.at(index);
00147 }
00148 
00149 QList<Background *> BackgroundListModel::findAllBackgrounds(const BackgroundContainer *container,
00150                                                             const QString &path, float ratio)
00151 {
00152     //kDebug() << "looking for" << path;
00153     QList<Background *> res;
00154 
00155     // get all packages in this directory
00156     //kDebug() << "getting packages";
00157     QStringList packages = Plasma::Package::listInstalledPaths(path);
00158     QSet<QString> validPackages;
00159     foreach (const QString &packagePath, packages) {
00160         std::auto_ptr<Background> pkg(new BackgroundPackage(path + packagePath, ratio));
00161         if (pkg->isValid() &&
00162             (!container || !container->contains(pkg->path()))) {
00163             res.append(pkg.release());
00164             //kDebug() << "    adding valid package:" << packagePath;
00165             validPackages << packagePath;
00166         }
00167     }
00168 
00169     // search normal wallpapers
00170     //kDebug() << "listing normal files";
00171     QDir dir(path);
00172     QStringList filters;
00173     filters << "*.png" << "*.jpeg" << "*.jpg" << "*.svg" << "*.svgz";
00174     dir.setNameFilters(filters);
00175     dir.setFilter(QDir::Files | QDir::Hidden | QDir::Readable);
00176     QFileInfoList files = dir.entryInfoList();
00177     foreach (const QFileInfo &wp, files) {
00178         if (!container || !container->contains(wp.filePath())) {
00179             //kDebug() << "     adding image file" << wp.filePath();
00180             res.append(new BackgroundFile(wp.filePath(), ratio));
00181         }
00182     }
00183 
00184     // now recurse the dirs, skipping ones we found packages in
00185     //kDebug() << "recursing dirs";
00186     dir.setFilter(QDir::AllDirs | QDir::Readable);
00187     files = dir.entryInfoList();
00188     //TODO: we should show a KProgressDialog here as this can take a while if someone
00189     //      indexes, say, their entire home directory!
00190     foreach (const QFileInfo &wp, files) {
00191         QString name = wp.fileName();
00192         if (name != "." && name != ".." && !validPackages.contains(wp.fileName())) {
00193             //kDebug() << "    " << name << wp.filePath();
00194             res += findAllBackgrounds(container, wp.filePath(), ratio);
00195         }
00196     }
00197 
00198     //kDebug() << "completed.";
00199     return res;
00200 }
00201 
00202 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal