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

« back to all changes in this revision

Viewing changes to libs/ksysguard/processui/ProcessFilter.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
    KSysGuard, the KDE System Guard
 
3
 
 
4
        Copyright (c) 2006-2007 John Tapsell <john.tapsell@kde.org>
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of the License, or (at your option) any later version.
 
10
 
 
11
    This library is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
    Library General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
    Boston, MA 02110-1301, USA.
 
20
 
 
21
*/
 
22
 
 
23
 
 
24
/* For getuid() */
 
25
#include <unistd.h>
 
26
#include <sys/types.h>
 
27
 
 
28
#include <QVariant>
 
29
 
 
30
#include <kdebug.h>
 
31
 
 
32
#include "ProcessModel.h"
 
33
#include "ProcessModel_p.h"
 
34
#include "ProcessFilter.h"
 
35
 
 
36
bool ProcessFilter::filterAcceptsRow( int source_row, const QModelIndex & source_parent ) const
 
37
{
 
38
        if( (mFilter == AllProcesses || mFilter == AllProcessesInTreeForm)
 
39
                        && filterRegExp().isEmpty()) return true; //Shortcut for common case
 
40
 
 
41
        ProcessModel *model = static_cast<ProcessModel *>(sourceModel());
 
42
        const KSysGuard::Process *process;
 
43
        if(model->isSimpleMode()) {
 
44
                if(source_parent.isValid()) {
 
45
                        kDebug() << "Serious error with data.  In simple mode, there should be no children";
 
46
                        return true;
 
47
                }
 
48
                process = model->getProcessAtIndex(source_row);
 
49
        } else {
 
50
                KSysGuard::Process *parent_process = NULL;
 
51
                if(source_parent.isValid()) {
 
52
                        parent_process = reinterpret_cast<KSysGuard::Process *>(source_parent.internalPointer());
 
53
                        Q_ASSERT(parent_process);
 
54
                } else {
 
55
                        //if(!model->isSimpleMode()) {
 
56
                                parent_process = model->getProcess(-1); //Get our 'special' process which should have the root init child
 
57
                                Q_ASSERT(parent_process);
 
58
                        //}
 
59
                }
 
60
                if(!model->isSimpleMode() && source_row >= parent_process->children.size()) {
 
61
                        kDebug() << "Serious error with data.  Source row requested for a non existent row. Requested " << source_row << " of " << parent_process->children.size() << " for " << parent_process->pid;
 
62
                        return true;
 
63
                }
 
64
 
 
65
                process = parent_process->children.at(source_row);
 
66
        }
 
67
        Q_ASSERT(process);
 
68
        long uid = process->uid;
 
69
        long euid = process->euid;
 
70
 
 
71
        bool accepted = true;
 
72
        switch(mFilter) {
 
73
        case AllProcesses:
 
74
        case AllProcessesInTreeForm:
 
75
                break;
 
76
        case SystemProcesses:
 
77
                if( uid >= 100 && model->canUserLogin(uid))
 
78
                        accepted = false;
 
79
                break;
 
80
        case UserProcesses:
 
81
                if( (uid < 100 || !model->canUserLogin(uid)) && (euid < 100 || !model->canUserLogin(euid)))
 
82
                        accepted = false;
 
83
                break;
 
84
        case OwnProcesses: {
 
85
                long ownuid = getuid();
 
86
                if(uid != ownuid && process->suid != ownuid && process->fsuid != ownuid && euid != ownuid)
 
87
                        accepted = false;
 
88
                break;
 
89
        }
 
90
        case ProgramsOnly:
 
91
                if(process->tty.isEmpty()) {
 
92
                        if(!model->hasGUIWindow(process->pid))
 
93
                                accepted = false;
 
94
                } else {
 
95
                        // login and getty kinda _are_ the tty, so I do not really count them as 'programs'. So make a special case and hide them
 
96
                        // Their ppid are 1 (init) so by checking we try to avoid false matches, and speed up checking overall
 
97
                        QString name = process->name.section(' ', 0,0);
 
98
                        if(process->parent_pid == 1 && (name == "login" || name.endsWith("getty")))
 
99
                                accepted = false;
 
100
                }
 
101
                break;
 
102
        default:
 
103
                break;
 
104
        }
 
105
 
 
106
        if(accepted) {
 
107
                if(filterRegExp().isEmpty()) return true;
 
108
 
 
109
                //Allow the user to search by PID
 
110
                if(QString::number(process->pid).contains(filterRegExp())) return true;
 
111
 
 
112
                //None of our tests have rejected it.  Pass it on to qsortfilterproxymodel's filter
 
113
                if(QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent))
 
114
                        return true;
 
115
        }
 
116
 
 
117
 
 
118
        //We did not accept this row at all.
 
119
 
 
120
        //If we are in flat mode, then give up now
 
121
        if(mFilter != AllProcessesInTreeForm)
 
122
                return false;
 
123
 
 
124
        //one of our children might be accepted, so accept this row if our children are accepted.
 
125
        QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
 
126
        for(int i = 0 ; i < sourceModel()->rowCount(source_index); i++) {
 
127
                if(filterAcceptsRow(i, source_index)) return true;
 
128
        }
 
129
        return false;
 
130
}
 
131
 
 
132
bool ProcessFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const
 
133
{
 
134
        if(right.isValid() && left.isValid()) {
 
135
                Q_ASSERT(left.model());
 
136
                Q_ASSERT(right.model());
 
137
        const ProcessModel *model = static_cast<const ProcessModel *>(left.model());
 
138
        return model->lessThan(left, right);
 
139
        }
 
140
        return QSortFilterProxyModel::lessThan(left,right);
 
141
}
 
142
 
 
143
 
 
144
void ProcessFilter::setFilter(State filter) {
 
145
        mFilter = filter;
 
146
        filterChanged();//Tell the proxy view to refresh all its information
 
147
}
 
148
#include "ProcessFilter.moc"