Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / src / konqactions.cpp
blob3bb5f60e4f976702f7a323a3bc95328a0d0befb2
1 /* This file is part of the KDE project
2 Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
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.
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.
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.
20 #include "konqactions.h"
22 #include <assert.h>
24 #include <ktoolbar.h>
25 #include <kdebug.h>
27 #include <konqpixmapprovider.h>
28 #include <kicon.h>
29 #include <kiconloader.h>
30 #include <kmenu.h>
31 #include <kapplication.h>
33 #include "konqview.h"
34 #include "konqsettingsxt.h"
35 #include <kauthorized.h>
37 #include <algorithm>
38 #include <QtGui/QBoxLayout>
40 template class QList<KonqHistoryEntry*>;
42 /////////////////
44 //static - used by back/forward popups in KonqMainWindow
45 void KonqActions::fillHistoryPopup(const QList<HistoryEntry*> &history, int historyIndex,
46 QMenu * popup,
47 bool onlyBack,
48 bool onlyForward)
50 assert ( popup ); // kill me if this 0... :/
52 //kDebug(1202) << "fillHistoryPopup position: " << history.at();
53 int index = 0;
54 if (onlyBack || onlyForward) // this if() is always true nowadays.
56 index += historyIndex; // Jump to current item
57 if ( !onlyForward ) --index; else ++index; // And move off it
60 QFontMetrics fm = popup->fontMetrics();
61 int i = 0;
62 while ( index < history.count() && index >= 0 )
64 QString text = history[ index ]->title;
65 text = fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 30);
66 text.replace( '&', "&&" );
67 const QString iconName = KonqPixmapProvider::self()->iconNameFor(history[index]->url);
68 QAction* action = new QAction(KIcon(iconName), text, popup);
69 action->setData(index - historyIndex);
70 //kDebug(1202) << text << index - historyIndex;
71 popup->addAction(action);
72 if (++i > 10)
73 break;
74 if (!onlyForward) --index; else ++index;
76 //kDebug(1202) << "After fillHistoryPopup position: " << history.at();
79 ///////////////////////////////
81 static int s_maxEntries = 0;
83 KonqMostOftenURLSAction::KonqMostOftenURLSAction( const QString& text,
84 QObject* parent )
85 : KActionMenu( KIcon("go-jump"), text, parent ),
86 m_parsingDone(false)
88 setDelayed( false );
90 connect(menu(), SIGNAL(aboutToShow()), SLOT(slotFillMenu()));
91 connect(menu(), SIGNAL(triggered(QAction*)), SLOT(slotActivated(QAction*)));
92 // Need to do all this upfront for a correct initial state
93 init();
96 KonqMostOftenURLSAction::~KonqMostOftenURLSAction()
100 void KonqMostOftenURLSAction::init()
102 s_maxEntries = KonqSettings::numberofmostvisitedURLs();
104 KonqHistoryManager *mgr = KonqHistoryManager::kself();
105 setEnabled( !mgr->entries().isEmpty() && s_maxEntries > 0 );
108 K_GLOBAL_STATIC( KonqHistoryList, s_mostEntries )
110 void KonqMostOftenURLSAction::inSort( const KonqHistoryEntry& entry ) {
111 KonqHistoryList::iterator it = std::lower_bound( s_mostEntries->begin(),
112 s_mostEntries->end(),
113 entry,
114 numberOfVisitOrder );
115 s_mostEntries->insert( it, entry );
118 void KonqMostOftenURLSAction::parseHistory() // only ever called once
120 KonqHistoryManager *mgr = KonqHistoryManager::kself();
122 connect( mgr, SIGNAL( entryAdded( const KonqHistoryEntry& )),
123 SLOT( slotEntryAdded( const KonqHistoryEntry& )));
124 connect( mgr, SIGNAL( entryRemoved( const KonqHistoryEntry& )),
125 SLOT( slotEntryRemoved( const KonqHistoryEntry& )));
126 connect( mgr, SIGNAL( cleared() ), SLOT( slotHistoryCleared() ));
128 const KonqHistoryList mgrEntries = mgr->entries();
129 KonqHistoryList::const_iterator it = mgrEntries.begin();
130 const KonqHistoryList::const_iterator end = mgrEntries.end();
131 for ( int i = 0; it != end && i < s_maxEntries; ++i, ++it ) {
132 s_mostEntries->append( *it );
134 qSort( s_mostEntries->begin(), s_mostEntries->end(), numberOfVisitOrder );
136 while ( it != end ) {
137 const KonqHistoryEntry& leastOften = s_mostEntries->first();
138 const KonqHistoryEntry& entry = *it;
139 if ( leastOften.numberOfTimesVisited < entry.numberOfTimesVisited ) {
140 s_mostEntries->removeFirst();
141 inSort( entry );
144 ++it;
148 void KonqMostOftenURLSAction::slotEntryAdded( const KonqHistoryEntry& entry )
150 // if it's already present, remove it, and inSort it
151 s_mostEntries->removeEntry( entry.url );
153 if ( s_mostEntries->count() >= s_maxEntries ) {
154 const KonqHistoryEntry& leastOften = s_mostEntries->first();
155 if ( leastOften.numberOfTimesVisited < entry.numberOfTimesVisited ) {
156 s_mostEntries->removeFirst();
157 inSort( entry );
161 else
162 inSort( entry );
163 setEnabled( !s_mostEntries->isEmpty() );
166 void KonqMostOftenURLSAction::slotEntryRemoved( const KonqHistoryEntry& entry )
168 s_mostEntries->removeEntry( entry.url );
169 setEnabled( !s_mostEntries->isEmpty() );
172 void KonqMostOftenURLSAction::slotHistoryCleared()
174 s_mostEntries->clear();
175 setEnabled( false );
178 static void createHistoryAction(const KonqHistoryEntry& entry, QMenu* menu)
180 // we take either title, typedUrl or URL (in this order)
181 const QString text = entry.title.isEmpty() ? (entry.typedUrl.isEmpty() ?
182 entry.url.prettyUrl() :
183 entry.typedUrl) :
184 entry.title;
185 QAction* action = new QAction(
186 KIcon(KonqPixmapProvider::self()->iconNameFor(entry.url)),
187 text, menu);
188 action->setData(entry.url);
189 menu->addAction(action);
192 void KonqMostOftenURLSAction::slotFillMenu()
194 if (!m_parsingDone) { // first time
195 parseHistory();
196 m_parsingDone = true;
199 menu()->clear();
201 for (int id = s_mostEntries->count() - 1; id >= 0; --id) {
202 createHistoryAction(s_mostEntries->at(id), menu());
204 setEnabled( !s_mostEntries->isEmpty() );
207 void KonqMostOftenURLSAction::slotActivated(QAction* action)
209 emit activated(action->data().value<KUrl>());
212 ///////////////////////////////
214 KonqHistoryAction::KonqHistoryAction(const QString& text, QObject* parent)
215 : KActionMenu(KIcon("go-jump"), text, parent)
217 setDelayed(false);
218 connect(menu(), SIGNAL(aboutToShow()), SLOT(slotFillMenu()));
219 connect(menu(), SIGNAL(triggered(QAction*)), SLOT(slotActivated(QAction*)));
220 setEnabled(!KonqHistoryManager::kself()->entries().isEmpty());
223 KonqHistoryAction::~KonqHistoryAction()
227 void KonqHistoryAction::slotFillMenu()
229 menu()->clear();
231 // Use the same configuration as the "most visited urls" action
232 s_maxEntries = KonqSettings::numberofmostvisitedURLs();
234 KonqHistoryManager *mgr = KonqHistoryManager::kself();
235 const KonqHistoryList mgrEntries = mgr->entries();
236 int idx = mgrEntries.count() - 1;
237 // mgrEntries is "oldest first", so take the last s_maxEntries entries.
238 for (int n = 0; idx >= 0 && n < s_maxEntries; --idx, ++n) {
239 createHistoryAction(mgrEntries.at(idx), menu());
243 void KonqHistoryAction::slotActivated(QAction* action)
245 emit activated(action->data().value<KUrl>());
248 #include "konqactions.moc"