1 | /************************************************************************** |
2 | * Copyright (C) 2009 Ben Cooksley <ben@eclipse.endoftheinternet.org> * |
3 | * Copyright (C) 2007 Will Stephenson <wstephenson@kde.org> * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or * |
6 | * modify it under the terms of the GNU General Public License * |
7 | * as published by the Free Software Foundation; either version 2 * |
8 | * of the License, or (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the Free Software * |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
18 | * 02110-1301, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include "ActionModel.h" |
22 | #include "ActionItem.h" |
23 | |
24 | #include <kdesktopfileactions.h> |
25 | #include <KStandardDirs> |
26 | #include <KIcon> |
27 | |
28 | class ActionModel::Private { |
29 | public: |
30 | Private() {} |
31 | |
32 | QList<ActionItem*> actions; |
33 | }; |
34 | |
35 | static bool sortAction( ActionItem * left, ActionItem * right ) |
36 | { |
37 | return left->name() > right->name(); |
38 | } |
39 | |
40 | ActionModel::ActionModel( QObject *parent ) |
41 | : QAbstractTableModel( parent ) |
42 | , d( new Private() ) |
43 | { |
44 | } |
45 | |
46 | ActionModel::~ActionModel() |
47 | { |
48 | qDeleteAll( d->actions ); |
49 | d->actions.clear(); |
50 | delete d; |
51 | } |
52 | |
53 | int ActionModel::columnCount( const QModelIndex &parent ) const |
54 | { |
55 | Q_UNUSED( parent ); |
56 | return 2; |
57 | } |
58 | |
59 | int ActionModel::rowCount( const QModelIndex &parent ) const |
60 | { |
61 | if( !parent.isValid() ) { |
62 | return d->actions.count(); |
63 | } |
64 | return 0; |
65 | } |
66 | |
67 | QVariant ActionModel::data( const QModelIndex &index, int role ) const |
68 | { |
69 | QVariant theData; |
70 | if ( !index.isValid() ) { |
71 | return QVariant(); |
72 | } |
73 | |
74 | ActionItem * mi = d->actions.at( index.row() ); |
75 | switch ( role ) { |
76 | case Qt::DisplayRole: |
77 | if( index.column() == 0 ) { |
78 | theData.setValue( mi->name() ); |
79 | } else if( index.column() == 1 ) { |
80 | theData.setValue( mi->involvedTypes() ); |
81 | } |
82 | break; |
83 | case Qt::DecorationRole: |
84 | if( index.column() == 0 ) { |
85 | theData = QVariant( KIcon(mi->icon()) ); |
86 | } |
87 | break; |
88 | case Qt::UserRole: |
89 | theData.setValue( mi ); |
90 | break; |
91 | default: |
92 | break; |
93 | } |
94 | return theData; |
95 | } |
96 | |
97 | void ActionModel::buildActionList() |
98 | { |
99 | qDeleteAll( d->actions ); |
100 | d->actions.clear(); |
101 | // Prepare to search for possible actions -> we only want solid types |
102 | QStringList allPossibleActions = KGlobal::dirs()->findAllResources("data" , "solid/actions/" ); |
103 | // Get service objects for those actions and add them to the display |
104 | foreach(const QString &desktop, allPossibleActions) { |
105 | // Get contained services list |
106 | QList<KServiceAction> services = KDesktopFileActions::userDefinedServices(desktop, true); |
107 | foreach( const KServiceAction &deviceAction, services ) { |
108 | ActionItem * actionItem = new ActionItem( desktop, deviceAction.name(), this ); // Create an action |
109 | d->actions.append( actionItem ); |
110 | } |
111 | } |
112 | qSort( d->actions.begin(), d->actions.end(), sortAction ); |
113 | reset(); |
114 | } |
115 | |
116 | QList<ActionItem*> ActionModel::actionList() const |
117 | { |
118 | return d->actions; |
119 | } |
120 | |
121 | #include "ActionModel.moc" |
122 | |