File: ActionModel.cpp

package info (click to toggle)
kde-workspace 4%3A4.11.13-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,756 kB
  • ctags: 47,705
  • sloc: cpp: 358,638; ansic: 34,695; xml: 5,231; perl: 1,598; sh: 1,307; ruby: 1,135; python: 651; asm: 566; makefile: 37
file content (121 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**************************************************************************
 * Copyright (C) 2009 Ben Cooksley <ben@eclipse.endoftheinternet.org>     *
 * Copyright (C) 2007 Will Stephenson <wstephenson@kde.org>               *
 *                                                                        *
 * This program is free software; you can redistribute it and/or          *
 * modify it under the terms of the GNU General Public License            *
 * as published by the Free Software Foundation; either version 2         *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * This program is distributed in the hope that it will be useful,        *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 * GNU General Public License for more details.                           *
 *                                                                        *
 * You should have received a copy of the GNU General Public License      *
 * along with this program; if not, write to the Free Software            *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA          *
 * 02110-1301, USA.                                                       *
***************************************************************************/

#include "ActionModel.h"
#include "ActionItem.h"

#include <kdesktopfileactions.h>
#include <KStandardDirs>
#include <KIcon>

class ActionModel::Private {
public:
    Private() {}

    QList<ActionItem*> actions;
};

static bool sortAction( ActionItem * left, ActionItem * right )
{
    return left->name() > right->name();
}

ActionModel::ActionModel( QObject *parent )
    : QAbstractTableModel( parent )
    , d( new Private() )
{
}

ActionModel::~ActionModel()
{
    qDeleteAll( d->actions );
    d->actions.clear();
    delete d;
}

int ActionModel::columnCount( const QModelIndex &parent ) const
{
    Q_UNUSED( parent );
    return 2;
}

int ActionModel::rowCount( const QModelIndex &parent ) const
{
    if( !parent.isValid() ) {
        return d->actions.count();
    }
    return 0;
}

QVariant ActionModel::data( const QModelIndex &index, int role ) const
{
    QVariant theData;
    if ( !index.isValid() ) {
        return QVariant();
    }

    ActionItem * mi = d->actions.at( index.row() );
    switch ( role ) {
        case Qt::DisplayRole:
            if( index.column() == 0 ) {
                theData.setValue( mi->name() );
            } else if( index.column() == 1 ) {
                theData.setValue( mi->involvedTypes() );
            }
            break;
        case Qt::DecorationRole:
            if( index.column() == 0 ) {
                theData = QVariant( KIcon(mi->icon()) );
            }
            break;
        case Qt::UserRole:
            theData.setValue( mi );
            break;
        default:
            break;
    }
    return theData;
}

void ActionModel::buildActionList()
{
    qDeleteAll( d->actions );
    d->actions.clear();
    // Prepare to search for possible actions -> we only want solid types
    QStringList allPossibleActions = KGlobal::dirs()->findAllResources("data", "solid/actions/");
    // Get service objects for those actions and add them to the display
    foreach(const QString &desktop, allPossibleActions) {
        // Get contained services list
        QList<KServiceAction> services = KDesktopFileActions::userDefinedServices(desktop, true);
        foreach( const KServiceAction &deviceAction, services ) {
            ActionItem * actionItem = new ActionItem( desktop, deviceAction.name(), this ); // Create an action
            d->actions.append( actionItem );
        }
    }
    qSort( d->actions.begin(), d->actions.end(), sortAction );
    reset();
}

QList<ActionItem*> ActionModel::actionList() const
{
    return d->actions;
}

#include "ActionModel.moc"