~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to plasma/kickoff/src/ui/contextmenufactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-11 14:04:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071011140448-v0eb7lxbb24zagca
Tags: 3.94.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
// Own
 
21
#include "ui/contextmenufactory.h"
 
22
 
 
23
// Qt
 
24
#include <QAbstractItemView>
 
25
#include <QMenu>
 
26
#include <QtDebug>
 
27
#include <QMap>
 
28
 
 
29
// KDE
 
30
#include <KIcon>
 
31
#include <KLocalizedString>
 
32
#include <KMimeType>
 
33
#include <kbookmarkmanager.h>
 
34
 
 
35
// Konqueror
 
36
//#include <konq_popupmenu.h>
 
37
#include <kactioncollection.h>
 
38
#include <kfileitem.h>
 
39
#include <kparts/browserextension.h>
 
40
 
 
41
// Local
 
42
#include "core/favoritesmodel.h"
 
43
#include "core/models.h"
 
44
 
 
45
using namespace Kickoff;
 
46
 
 
47
class ContextMenuFactory::Private
 
48
{
 
49
public:
 
50
    QAction *advancedActionsMenu(const QString& url) const
 
51
    {
 
52
       KUrl kUrl(url);
 
53
       KActionCollection actionCollection((QObject*)0);
 
54
       KFileItemList items;
 
55
       QString mimeType = KMimeType::findByUrl(kUrl,0,false,true)->name();
 
56
       items << KFileItem(url,mimeType,KFileItem::Unknown);
 
57
       KParts::BrowserExtension::PopupFlags browserFlags = KParts::BrowserExtension::DefaultPopupItems;
 
58
       if (items.first().isLocalFile()) {
 
59
           browserFlags |= KParts::BrowserExtension::ShowProperties;
 
60
       }
 
61
       KParts::BrowserExtension::ActionGroupMap actionGroupMap;
 
62
       return 0;
 
63
#if 0
 
64
       KonqPopupMenu *menu = new KonqPopupMenu(items, kUrl,actionCollection,
 
65
                                               0, 0, browserFlags,
 
66
                                               0, KBookmarkManager::userBookmarksManager(), actionGroupMap);
 
67
       
 
68
       if (!menu->isEmpty()) {
 
69
            QAction *action = menu->menuAction();
 
70
            action->setText(i18n("Advanced"));
 
71
            action->setIcon(KIcon("edit-add"));
 
72
            return action;
 
73
       } else {
 
74
            delete menu;
 
75
            return 0;
 
76
       }
 
77
#endif
 
78
    }
 
79
 
 
80
    QMap<QAbstractItemView*,QList<QAction*> > viewActions;
 
81
};
 
82
 
 
83
ContextMenuFactory::ContextMenuFactory(QObject *parent)
 
84
 : QObject(parent)
 
85
 , d(new Private)
 
86
{
 
87
}
 
88
 
 
89
ContextMenuFactory::~ContextMenuFactory()
 
90
{
 
91
  delete d;
 
92
}
 
93
 
 
94
void ContextMenuFactory::showContextMenu(QAbstractItemView *view,const QPoint& pos)
 
95
{
 
96
    Q_ASSERT(view);
 
97
 
 
98
    const QModelIndex index = view->indexAt(pos);
 
99
    const QString url = index.data(UrlRole).value<QString>(); 
 
100
 
 
101
    if (url.isEmpty()) {
 
102
        return;
 
103
    }
 
104
 
 
105
    bool isFavorite = FavoritesModel::isFavorite(url);
 
106
 
 
107
    QList<QAction*> actions;
 
108
    
 
109
    // add to / remove from favorites
 
110
    QAction *favoriteAction = new QAction(this);
 
111
    if (isFavorite) {
 
112
        favoriteAction->setText(i18n("Remove from Favorites"));
 
113
        favoriteAction->setIcon(KIcon("list-remove"));
 
114
    } else {
 
115
        favoriteAction->setText(i18n("Add to Favorites"));
 
116
        favoriteAction->setIcon(KIcon("bookmark-new"));
 
117
    }
 
118
 
 
119
    actions << favoriteAction;
 
120
 
 
121
    // add to desktop 
 
122
    QAction *addToDesktopAction = new QAction(this);
 
123
    addToDesktopAction->setText(i18n("Add to Desktop"));
 
124
    addToDesktopAction->setEnabled(false);
 
125
    actions << addToDesktopAction;
 
126
 
 
127
    // add to main panel
 
128
    QAction *addToPanelAction = new QAction(this);
 
129
    addToPanelAction->setText(i18n("Add to Panel"));
 
130
    addToPanelAction->setEnabled(false);
 
131
    actions << addToPanelAction;
 
132
 
 
133
    // advanced item actions
 
134
    QAction *advancedSeparator = new QAction(this);
 
135
    advancedSeparator->setSeparator(true);
 
136
    actions << advancedSeparator;
 
137
 
 
138
    QAction *advanced = d->advancedActionsMenu(url);
 
139
    if (advanced) {
 
140
        actions << advanced;
 
141
    }    
 
142
    
 
143
    // add view specific actions
 
144
    QAction *viewSeparator = new QAction(this);
 
145
    viewSeparator->setSeparator(true);
 
146
    actions << viewSeparator;
 
147
    actions << viewActions(view); 
 
148
 
 
149
    // display menu
 
150
    QAction *result = QMenu::exec(actions,view->mapToGlobal(pos));
 
151
    
 
152
    if (result == favoriteAction) {
 
153
        if (isFavorite) {
 
154
            FavoritesModel::remove(url);
 
155
        } else {
 
156
            FavoritesModel::add(url);
 
157
        }
 
158
    } 
 
159
 
 
160
    delete favoriteAction;
 
161
    delete addToDesktopAction;
 
162
    delete addToPanelAction;
 
163
    delete viewSeparator;
 
164
}
 
165
void ContextMenuFactory::setViewActions(QAbstractItemView *view,const QList<QAction*>& actions)
 
166
{
 
167
    if (actions.isEmpty()) {
 
168
        d->viewActions.remove(view);
 
169
    } else {
 
170
        d->viewActions.insert(view,actions);
 
171
    }
 
172
}
 
173
QList<QAction*> ContextMenuFactory::viewActions(QAbstractItemView *view) const
 
174
{
 
175
    return d->viewActions[view];
 
176
}
 
177
 
 
178
#include "contextmenufactory.moc"