~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/taskmanager/strategies/kustodiangroupingstrategy.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>
 
4
Copyright 2009 CasperBoemann <cbr@boemann.dk>
 
5
 
 
6
Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
of this software and associated documentation files (the "Software"), to deal
 
8
in the Software without restriction, including without limitation the rights
 
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
copies of the Software, and to permit persons to whom the Software is
 
11
furnished to do so, subject to the following conditions:
 
12
 
 
13
The above copyright notice and this permission notice shall be included in
 
14
all copies or substantial portions of the Software.
 
15
 
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
19
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
22
 
 
23
******************************************************************/
 
24
 
 
25
#include "kustodiangroupingstrategy.h"
 
26
 
 
27
#include <QAction>
 
28
 
 
29
#include <KDebug>
 
30
#include <KLocale>
 
31
#include <KIcon>
 
32
#include <KService>
 
33
 
 
34
#include "abstractgroupingstrategy.h"
 
35
#include "groupmanager.h"
 
36
 
 
37
namespace TaskManager
 
38
{
 
39
 
 
40
class KustodianGroupingStrategy::Private
 
41
{
 
42
public:
 
43
    Private()
 
44
        :editableGroupProperties(AbstractGroupingStrategy::None)
 
45
    {
 
46
    }
 
47
    AbstractGroupingStrategy::EditableGroupProperties editableGroupProperties;
 
48
};
 
49
 
 
50
 
 
51
KustodianGroupingStrategy::KustodianGroupingStrategy(GroupManager *groupManager)
 
52
    :AbstractGroupingStrategy(groupManager),
 
53
     d(new Private)
 
54
{
 
55
    setType(GroupManager::KustodianGrouping);
 
56
 
 
57
    QStringList defaultApps;
 
58
    defaultApps << "dolphin" << "krita" << "konqueror" << "kwrite" << "konsole" << "gwenview" << "kontact" << "konversation" << "amarok" << "kword";
 
59
    foreach (const QString &name, defaultApps) {
 
60
        QList <AbstractGroupableItem *> list;
 
61
        TaskGroup* group = createGroup(list);
 
62
        group->setName(name);
 
63
        group->setPersistentWithLauncher(true);
 
64
        KService::Ptr service = KService::serviceByDesktopName(name);
 
65
        if (service && service->isValid()) {
 
66
            QIcon icon = KIcon(service->icon());
 
67
            group->setIcon(icon);
 
68
        }
 
69
    }
 
70
}
 
71
 
 
72
KustodianGroupingStrategy::~KustodianGroupingStrategy()
 
73
{
 
74
    delete d;
 
75
}
 
76
 
 
77
QList<QAction*> KustodianGroupingStrategy::strategyActions(QObject *parent, AbstractGroupableItem *item)
 
78
{
 
79
    Q_UNUSED(parent);
 
80
    Q_UNUSED(item);
 
81
    QList<QAction*> actionList;
 
82
    return actionList;
 
83
}
 
84
 
 
85
void KustodianGroupingStrategy::handleItem(AbstractGroupableItem *item)
 
86
{
 
87
    if (!rootGroup()) {
 
88
        return;
 
89
    }
 
90
 
 
91
    if (item->itemType() == GroupItemType) {
 
92
        rootGroup()->add(item);
 
93
        return;
 
94
    }
 
95
 
 
96
    TaskItem *task = dynamic_cast<TaskItem*>(item);
 
97
    if (task && !programGrouping(task, rootGroup())) {
 
98
        QString name = desktopNameFromClassName(task->task()->classClass());
 
99
        //kDebug() << "create new subgroup in root as this classname doesn't have a group " << name;
 
100
 
 
101
        QList <AbstractGroupableItem *> list;
 
102
        list.append(task);
 
103
        TaskGroup* group = createGroup(list);
 
104
        group->setName(name);
 
105
        group->setColor(Qt::red);
 
106
        KService::Ptr service = KService::serviceByDesktopName(name);
 
107
        if (service && service->isValid()) {
 
108
            QIcon icon = KIcon(service->icon());
 
109
            group->setIcon(icon);
 
110
        }
 
111
    }
 
112
}
 
113
 
 
114
bool KustodianGroupingStrategy::programGrouping(TaskItem* taskItem, TaskGroup* groupItem)
 
115
{
 
116
    //kDebug();
 
117
    QHash <QString,AbstractGroupableItem *> itemMap;
 
118
 
 
119
    foreach (AbstractGroupableItem *item, groupItem->members()) { //search for an existing group
 
120
        if (item->itemType() == GroupItemType && programGrouping(taskItem, static_cast<TaskGroup*>(item))) {
 
121
            //maybe add the condition that the subgroup was created by programGrouping
 
122
            //kDebug() << "joined subGroup";
 
123
            return true;
 
124
        }
 
125
    }
 
126
 
 
127
    QString name = desktopNameFromClassName(taskItem->task()->classClass());
 
128
    if (groupItem->name() == name) { //join this group
 
129
        //kDebug() << "joined this Group";
 
130
        groupItem->add(taskItem);
 
131
        return true;
 
132
    } else {
 
133
        return false;
 
134
    }
 
135
}
 
136
 
 
137
QString KustodianGroupingStrategy::desktopNameFromClassName(const QString & name)
 
138
{
 
139
    KService::Ptr service = KService::serviceByDesktopName(name.toLower());
 
140
    if (service && service->isValid()) {
 
141
        return name.toLower();
 
142
    }
 
143
    service = KService::serviceByDesktopName(name);
 
144
    if (service && service->isValid()) {
 
145
        return name;
 
146
    }
 
147
    service = KService::serviceByDesktopName(name.toLower().remove(QChar(' ')));
 
148
    if (service && service->isValid()) {
 
149
        return name.toLower().remove(' ');
 
150
    }
 
151
 
 
152
    return "???";
 
153
}
 
154
 
 
155
void KustodianGroupingStrategy::checkGroup()
 
156
{
 
157
    TaskGroup *group = qobject_cast<TaskGroup*>(sender()); 
 
158
    if (!group) {
 
159
        return;
 
160
    }
 
161
 
 
162
    if (group->members().isEmpty() && !group->isPersistentWithLauncher()) {
 
163
        closeGroup(group);
 
164
    }
 
165
}
 
166
 
 
167
}//namespace
 
168
 
 
169
#include "kustodiangroupingstrategy.moc"
 
170