1
/***************************************************************** Copyright 2008 Christian Mollekopf <chrigi_1@hotmail.com>
2
Permission is hereby granted, free of charge, to any person obtaining a copy
3
of this software and associated documentation files (the "Software"), to deal
4
in the Software without restriction, including without limitation the rights
5
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6
copies of the Software, and to permit persons to whom the Software is
7
furnished to do so, subject to the following conditions:
9
The above copyright notice and this permission notice shall be included in
10
all copies or substantial portions of the Software.
12
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
16
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
******************************************************************/
21
#include "programgroupingstrategy.h"
24
#include <QWeakPointer>
29
#include <KConfigGroup>
31
#include "abstractgroupingstrategy.h"
32
#include "groupmanager.h"
37
class ProgramGroupingStrategy::Private
41
:editableGroupProperties(AbstractGroupingStrategy::None)
45
AbstractGroupingStrategy::EditableGroupProperties editableGroupProperties;
46
QWeakPointer<AbstractGroupableItem> tempItem;
47
QStringList blackList; //Programs in this list should not be grouped
51
ProgramGroupingStrategy::ProgramGroupingStrategy(GroupManager *groupManager)
52
:AbstractGroupingStrategy(groupManager),
55
setType(GroupManager::ProgramGrouping);
57
KConfig groupBlacklist( "taskbargroupblacklistrc", KConfig::NoGlobals );
58
KConfigGroup blackGroup( &groupBlacklist, "Blacklist" );
59
d->blackList = blackGroup.readEntry( "Applications", QStringList() );
62
ProgramGroupingStrategy::~ProgramGroupingStrategy()
67
QList<QAction*> ProgramGroupingStrategy::strategyActions(QObject *parent, AbstractGroupableItem *item)
69
QAction *a = new QAction(parent);
70
QString name = className(item);
71
if (d->blackList.contains(name)) {
72
a->setText(i18n("Allow this program to be grouped"));
74
a->setText(i18n("Do not allow this program to be grouped"));
76
connect(a, SIGNAL(triggered()), this, SLOT(toggleGrouping()));
78
QList<QAction*> actionList;
84
QString ProgramGroupingStrategy::className(AbstractGroupableItem *item)
87
if (item->itemType() == GroupItemType) { //maybe add the condition that the subgroup was created by programGrouping
88
TaskGroup *group = qobject_cast<TaskGroup*>(item);
89
TaskItem *task = qobject_cast<TaskItem*>(group->members().first()); //There are only TaskItems in programGrouping groups
90
return task->task()->classClass();
93
return (qobject_cast<TaskItem*>(item))->task()->classClass();
97
void ProgramGroupingStrategy::toggleGrouping()
99
AbstractGroupableItem *tempItem = d->tempItem.data();
104
QString name = className(tempItem);
106
if (d->blackList.contains(name)) {
107
d->blackList.removeAll(name);
108
if (tempItem->itemType() == GroupItemType) {
109
foreach (AbstractGroupableItem *item, (qobject_cast<TaskGroup*>(tempItem))->members()) {
113
handleItem(tempItem);
116
d->blackList.append(name);
117
GroupPtr root = rootGroup();
118
if (tempItem->itemType() == GroupItemType) {
119
closeGroup(qobject_cast<TaskGroup*>(tempItem));
125
foreach (AbstractGroupableItem *item, root->members()) {
126
if (item->itemType() == GroupItemType) {
127
untoggleGroupingOn(static_cast<TaskGroup*>(item), name);
135
// Save immediately. Much better than saving in the destructor,
136
// since this class is deleted at every change of virtual desktop (!)
137
// So when doing it from the destructor we were constantly sync'ing
138
// (and triggering KDirWatch) even when the blacklist hadn't changed at all...
139
KConfig groupBlacklist("taskbargroupblacklistrc", KConfig::NoGlobals);
140
KConfigGroup blackGroup(&groupBlacklist, "Blacklist");
141
blackGroup.writeEntry("Applications", d->blackList);
142
blackGroup.config()->sync();
145
void ProgramGroupingStrategy::untoggleGroupingOn(TaskGroup *group, const QString &name)
147
if (!group->parentGroup()) {
151
foreach (AbstractGroupableItem *item, group->members()) {
152
if (item->itemType() == GroupItemType) {
153
untoggleGroupingOn(static_cast<TaskGroup*>(item), name);
157
foreach (AbstractGroupableItem *item, group->members()) {
158
if (!item->itemType() == GroupItemType && name == static_cast<TaskItem *>(item)->task()->classClass()) {
159
if (group->parentGroup()) {
160
group->parentGroup()->add(item);
165
if (group->members().isEmpty()) {
170
void ProgramGroupingStrategy::handleItem(AbstractGroupableItem *item)
172
GroupPtr root = rootGroup();
178
if (item->itemType() == GroupItemType) {
179
//kDebug() << item->name() << "item is groupitem";
182
} else if (d->blackList.contains((static_cast<TaskItem*>(item))->task()->classClass())) {
183
//kDebug() << item->name() << "item is in blacklist";
188
TaskItem *task = dynamic_cast<TaskItem*>(item);
189
if (task && !programGrouping(task, root)) {
190
//kDebug() << item->name() << "joined rootGroup ";
195
bool ProgramGroupingStrategy::programGrouping(TaskItem* taskItem, TaskGroup* groupItem)
197
//kDebug() << "===== Task:" << taskItem->name() << " <=> Group:" << groupItem->name() << "=====";
198
QList<AbstractGroupableItem *> list;
199
QString name = taskItem->task()->classClass();
201
//search for an existing group
202
foreach (AbstractGroupableItem *item, groupItem->members()) {
203
if (item->itemType() == GroupItemType) {
204
//TODO: maybe add the condition that the subgroup was created by programGrouping?
205
if (programGrouping(taskItem, static_cast<TaskGroup*>(item))) {
206
//kDebug() << " joined subGroup";
210
TaskItem *task = static_cast<TaskItem*>(item);
211
//kDebug() << " testing" << (task->task() ? task->task()->classClass() : "No task!") << "==" << name;
212
if (task != taskItem && task->task() && task->task()->classClass() == name) { //omit startup tasks
218
if (!list.isEmpty()) {
219
if (groupItem->isRootGroup()) {
220
//kDebug() << " create Group root group";
221
QIcon icon = taskItem->icon();
222
list.append(taskItem);
223
TaskGroup* group = createGroup(list);
224
group->setName(name);
225
group->setColor(Qt::red);
226
group->setIcon(icon);
227
connect(group, SIGNAL(checkIcon(TaskGroup*)), this, SLOT(updateIcon(TaskGroup*)));
229
//kDebug() << " joined this Group";
230
groupItem->add(taskItem);
236
//kDebug() << " failed";
240
void ProgramGroupingStrategy::checkGroup()
242
TaskGroup *group = qobject_cast<TaskGroup*>(sender());
244
if (group->members().size() < 2) {
252
void ProgramGroupingStrategy::updateIcon(TaskGroup *group)
254
foreach (AbstractGroupableItem *taskItem, group->members()) {
255
if (!taskItem->icon().isNull()) {
256
QIcon icon = taskItem->icon();
257
group->setIcon(icon);
265
#include "programgroupingstrategy.moc"