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"
27 #include <konqpixmapprovider.h>
29 #include <kiconloader.h>
31 #include <kapplication.h>
34 #include "konqsettingsxt.h"
35 #include <kauthorized.h>
38 #include <QtGui/QBoxLayout>
40 template class QList
<KonqHistoryEntry
*>;
44 //static - used by back/forward popups in KonqMainWindow
45 void KonqActions::fillHistoryPopup(const QList
<HistoryEntry
*> &history
, int historyIndex
,
50 assert ( popup
); // kill me if this 0... :/
52 //kDebug(1202) << "fillHistoryPopup position: " << history.at();
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();
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
);
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
,
85 : KActionMenu( KIcon("go-jump"), text
, parent
),
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
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(),
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();
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();
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();
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() :
185 QAction
* action
= new QAction(
186 KIcon(KonqPixmapProvider::self()->iconNameFor(entry
.url
)),
188 action
->setData(entry
.url
);
189 menu
->addAction(action
);
192 void KonqMostOftenURLSAction::slotFillMenu()
194 if (!m_parsingDone
) { // first time
196 m_parsingDone
= true;
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
)
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()
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"