~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to plasma/kickoff/src/core/models.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-11 14:04:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071011140448-v0eb7lxbb24zagca
Tags: 3.94.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
// Own
 
21
#include "core/models.h"
 
22
 
 
23
// Qt
 
24
#include <QFileInfo>
 
25
#include <QStandardItem>
 
26
 
 
27
// KDE
 
28
#include <KDesktopFile>
 
29
#include <KIcon>
 
30
#include <KGlobal>
 
31
#include <KMimeType>
 
32
#include <KUrl>
 
33
 
 
34
using namespace Kickoff;
 
35
 
 
36
static KUrl homeUrl(getenv("HOME"));
 
37
static KUrl remoteUrl("remote:/");
 
38
 
 
39
QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString)
 
40
{
 
41
    KUrl url(urlString);
 
42
    
 
43
    QStandardItem *item = 0; 
 
44
 
 
45
    if (url.isLocalFile() && QFileInfo(url.path()).suffix() == "desktop") {
 
46
 
 
47
        // .desktop files may be services (type field == 'Application' or 'Service')
 
48
        // or they may be other types such as links.
 
49
        //
 
50
        // first look in the KDE service database to see if this file is a service,
 
51
        // otherwise represent it as a generic .desktop file
 
52
        KService::Ptr service = KService::serviceByDesktopPath(url.path());
 
53
        if (service) {
 
54
            return createItemForService(service);
 
55
        }
 
56
 
 
57
        item = new QStandardItem;
 
58
        KDesktopFile desktopFile(url.path());
 
59
        item->setText(QFileInfo(urlString).baseName());
 
60
        item->setIcon(KIcon(desktopFile.readIcon()));
 
61
 
 
62
        KUrl desktopUrl(desktopFile.readUrl()); 
 
63
        item->setData(desktopUrl.url(),Kickoff::UrlRole);
 
64
 
 
65
        QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.path() : desktopUrl.prettyUrl();
 
66
 
 
67
        item->setData(subTitle,Kickoff::SubTitleRole);
 
68
 
 
69
        setSpecialUrlProperties(desktopUrl,item);
 
70
 
 
71
    } else {
 
72
        item = new QStandardItem;
 
73
        item->setText(QFileInfo(urlString).baseName());
 
74
        item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
 
75
        
 
76
        item->setData(url.url(),Kickoff::UrlRole);
 
77
        QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
 
78
        item->setData(subTitle,Kickoff::SubTitleRole);
 
79
 
 
80
        setSpecialUrlProperties(url,item);
 
81
    }
 
82
 
 
83
    return item;
 
84
}
 
85
void StandardItemFactory::setSpecialUrlProperties(const KUrl& url,QStandardItem *item)
 
86
{        
 
87
    // specially handled URLs
 
88
    if (url == homeUrl) {
 
89
        item->setText(i18n("Home Folder"));
 
90
        item->setIcon(KIcon("user-home"));
 
91
    } else if (url == remoteUrl) {
 
92
        item->setText(i18n("Network Folders"));
 
93
    }
 
94
}
 
95
QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
 
96
{
 
97
    QStandardItem *appItem = new QStandardItem;
 
98
 
 
99
    QString genericName = service->genericName();
 
100
    QString appName = service->name();
 
101
 
 
102
    appItem->setText(genericName.isEmpty() ? appName : genericName);
 
103
    appItem->setIcon(KIcon(service->icon()));
 
104
    appItem->setData(service->entryPath(),Kickoff::UrlRole);
 
105
 
 
106
    if (!genericName.isEmpty()) {
 
107
        appItem->setData(service->name(),Kickoff::SubTitleRole);
 
108
    }
 
109
 
 
110
    return appItem;
 
111
}
 
112
 
 
113
bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
 
114
{
 
115
    // a very crude heuristic using the .desktop path names
 
116
    // which only understands kde3 vs kde4 
 
117
    bool firstIsKde4 = first->entryPath().contains("kde4");
 
118
    bool secondIsKde4 = second->entryPath().contains("kde4");
 
119
 
 
120
    return firstIsKde4 && !secondIsKde4;
 
121
}
 
122
 
 
123
#if 0
 
124
void Kickoff::swapModelIndexes(QModelIndex& first,QModelIndex& second)
 
125
{
 
126
    Q_ASSERT(first.isValid());
 
127
    Q_ASSERT(second.isValid());
 
128
 
 
129
    QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
 
130
    QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
 
131
 
 
132
    Q_ASSERT(firstModel && secondModel);
 
133
 
 
134
    QMap<int,QVariant> firstIndexData = firstModel->itemData(first);
 
135
    QMap<int,QVariant> secondIndexData = secondModel->itemData(second);
 
136
 
 
137
    firstModel->setItemData(first,secondIndexData);
 
138
    secondModel->setItemData(second,firstIndexData);
 
139
}
 
140
#endif
 
141
 
 
142
K_GLOBAL_STATIC_WITH_ARGS(KComponentData,kickoffComponent,("kickoff",QByteArray(),KComponentData::SkipMainComponentRegistration))
 
143
KComponentData Kickoff::componentData()
 
144
{
 
145
    return *kickoffComponent;
 
146
}
 
147
 
 
148
 
 
149