Applets
models.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "core/models.h"
00022 #include "core/leavemodel.h"
00023
00024
00025 #include <QFileInfo>
00026 #include <QStandardItem>
00027 #include <QDir>
00028
00029
00030 #include <KDebug>
00031 #include <KConfigGroup>
00032 #include <KDesktopFile>
00033 #include <KIcon>
00034 #include <KGlobal>
00035 #include <KMimeType>
00036 #include <KUrl>
00037 #include <Solid/Device>
00038 #include <Solid/StorageAccess>
00039 #include <Solid/StorageDrive>
00040
00041 using namespace Kickoff;
00042
00043 namespace Kickoff
00044 {
00045
00046 Q_GLOBAL_STATIC_WITH_ARGS(KUrl, homeUrl, (QDir::homePath()))
00047 Q_GLOBAL_STATIC_WITH_ARGS(KUrl, remoteUrl, ("remote:/"))
00048 K_GLOBAL_STATIC(StandardItemFactoryData, factoryData)
00049
00050 StandardItemFactoryData* deviceFactoryData()
00051 {
00052 return factoryData;
00053 }
00054 }
00055
00056 QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString)
00057 {
00058 KUrl url(urlString);
00059
00060 QStandardItem *item = 0;
00061
00062 if (url.isLocalFile() && urlString.endsWith(".desktop")) {
00063
00064
00065
00066
00067
00068 KService::Ptr service = KService::serviceByDesktopPath(url.path());
00069 if (service) {
00070 return createItemForService(service);
00071 }
00072
00073 item = new QStandardItem;
00074 KDesktopFile desktopFile(url.path());
00075 item->setText(QFileInfo(urlString.mid(0, urlString.lastIndexOf('.'))).completeBaseName());
00076 item->setIcon(KIcon(desktopFile.readIcon()));
00077
00078
00079
00080 KUrl desktopUrl(desktopFile.desktopGroup().readPathEntry("URL", QString()));
00081 if (!desktopUrl.url().isEmpty()) {
00082 item->setData(desktopUrl.url(), Kickoff::UrlRole);
00083 } else {
00084
00085
00086
00087 item->setData(urlString, Kickoff::UrlRole);
00088 }
00089
00090 QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.path() : desktopUrl.prettyUrl();
00091 item->setData(subTitle, Kickoff::SubTitleRole);
00092
00093 setSpecialUrlProperties(desktopUrl, item);
00094 }
00095 else if (url.scheme() == "leave") {
00096 item = LeaveModel::createStandardItem(urlString);
00097 }
00098 else {
00099 item = new QStandardItem;
00100 const QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
00101 QString basename = QFileInfo(urlString).completeBaseName();
00102 if (basename.isNull())
00103 basename = subTitle;
00104 item->setText(basename);
00105 item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
00106 item->setData(url.url(), Kickoff::UrlRole);
00107 item->setData(subTitle, Kickoff::SubTitleRole);
00108
00109 setSpecialUrlProperties(url, item);
00110 }
00111
00112 return item;
00113 }
00114
00115 void StandardItemFactory::setSpecialUrlProperties(const KUrl& url,QStandardItem *item)
00116 {
00117
00118 if (homeUrl() && url == *homeUrl()) {
00119 item->setText(i18n("Home Folder"));
00120 item->setIcon(KIcon("user-home"));
00121 } else if (remoteUrl() && url == *remoteUrl()) {
00122 item->setText(i18n("Network Folders"));
00123 }
00124 }
00125
00126 QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
00127 {
00128 QStandardItem *appItem = new QStandardItem;
00129
00130 QString genericName = service->genericName();
00131 QString appName = service->name();
00132
00133 appItem->setText(genericName.isEmpty() ? appName : genericName);
00134 appItem->setIcon(KIcon(service->icon()));
00135 appItem->setData(service->entryPath(),Kickoff::UrlRole);
00136
00137 if (!genericName.isEmpty()) {
00138 appItem->setData(service->name(),Kickoff::SubTitleRole);
00139 }
00140
00141 return appItem;
00142 }
00143
00144 bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
00145 {
00146
00147
00148 bool firstIsKde4 = first->entryPath().contains("kde4");
00149 bool secondIsKde4 = second->entryPath().contains("kde4");
00150
00151 return firstIsKde4 && !secondIsKde4;
00152 }
00153
00154 QStringList Kickoff::systemApplicationList()
00155 {
00156 KConfigGroup appsGroup = componentData().config()->group("SystemApplications");
00157 QStringList apps;
00158 apps << "systemsettings";
00159 apps = appsGroup.readEntry("DesktopFiles", apps);
00160 return apps;
00161 }
00162
00163 #if 0
00164 void Kickoff::swapModelIndexes(QModelIndex& first,QModelIndex& second)
00165 {
00166 Q_ASSERT(first.isValid());
00167 Q_ASSERT(second.isValid());
00168
00169 QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
00170 QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
00171
00172 Q_ASSERT(firstModel && secondModel);
00173
00174 QMap<int,QVariant> firstIndexData = firstModel->itemData(first);
00175 QMap<int,QVariant> secondIndexData = secondModel->itemData(second);
00176
00177 firstModel->setItemData(first,secondIndexData);
00178 secondModel->setItemData(second,firstIndexData);
00179 }
00180 #endif
00181
00182 K_GLOBAL_STATIC_WITH_ARGS(KComponentData,kickoffComponent,("kickoff",QByteArray(),KComponentData::SkipMainComponentRegistration))
00183 KComponentData Kickoff::componentData()
00184 {
00185 return *kickoffComponent;
00186 }
00187
00188
00189