~ubuntu-branches/ubuntu/wily/kdebase/wily

« back to all changes in this revision

Viewing changes to konqueror/src/konqactions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac, Jonathan Riddell, Felix Geyer
  • Date: 2011-03-03 16:25:47 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20110303162547-2zf9j33cu6j5gj0a
Tags: 4:4.6.1a-0ubuntu1
[ Jonathan Riddell ]
* New upstream release
* Update kde-sc-dev-latest version

[ Felix Geyer ]
* Reduce the x-www-browser priority for konqueror to 30 as rekonq is the
  default Kubuntu browser.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
 
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
#include "konqactions.h"
 
21
 
 
22
#include <assert.h>
 
23
 
 
24
#include <ktoolbar.h>
 
25
#include <kdebug.h>
 
26
 
 
27
#include <konqpixmapprovider.h>
 
28
#include <kicon.h>
 
29
#include <kiconloader.h>
 
30
#include <kmenu.h>
 
31
#include <kapplication.h>
 
32
 
 
33
#include "konqview.h"
 
34
#include "konqsettingsxt.h"
 
35
#include <kauthorized.h>
 
36
 
 
37
#include <algorithm>
 
38
#include <QtGui/QBoxLayout>
 
39
 
 
40
template class QList<KonqHistoryEntry*>;
 
41
 
 
42
/////////////////
 
43
 
 
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)
 
49
{
 
50
  assert ( popup ); // kill me if this 0... :/
 
51
 
 
52
  //kDebug() << "fillHistoryPopup position: " << history.at();
 
53
  int index = 0;
 
54
  if (onlyBack || onlyForward) // this if() is always true nowadays.
 
55
  {
 
56
      index += historyIndex; // Jump to current item
 
57
      if ( !onlyForward ) --index; else ++index; // And move off it
 
58
  }
 
59
 
 
60
  QFontMetrics fm = popup->fontMetrics();
 
61
  int i = 0;
 
62
  while ( index < history.count() && index >= 0 )
 
63
  {
 
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() << text << index - historyIndex;
 
71
        popup->addAction(action);
 
72
        if (++i > 10)
 
73
            break;
 
74
        if (!onlyForward) --index; else ++index;
 
75
  }
 
76
  //kDebug() << "After fillHistoryPopup position: " << history.at();
 
77
}
 
78
 
 
79
///////////////////////////////
 
80
 
 
81
static int s_maxEntries = 0;
 
82
 
 
83
KonqMostOftenURLSAction::KonqMostOftenURLSAction( const QString& text,
 
84
                                                  QObject* parent )
 
85
    : KActionMenu( KIcon("go-jump"), text, parent ),
 
86
      m_parsingDone(false)
 
87
{
 
88
    setDelayed( false );
 
89
 
 
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();
 
94
}
 
95
 
 
96
KonqMostOftenURLSAction::~KonqMostOftenURLSAction()
 
97
{
 
98
}
 
99
 
 
100
void KonqMostOftenURLSAction::init()
 
101
{
 
102
    s_maxEntries = KonqSettings::numberofmostvisitedURLs();
 
103
 
 
104
    KonqHistoryManager *mgr = KonqHistoryManager::kself();
 
105
    setEnabled( !mgr->entries().isEmpty() && s_maxEntries > 0 );
 
106
}
 
107
 
 
108
K_GLOBAL_STATIC( KonqHistoryList, s_mostEntries )
 
109
 
 
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 );
 
116
}
 
117
 
 
118
void KonqMostOftenURLSAction::parseHistory() // only ever called once
 
119
{
 
120
    KonqHistoryManager *mgr = KonqHistoryManager::kself();
 
121
 
 
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() ));
 
127
 
 
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 );
 
133
    }
 
134
    qSort( s_mostEntries->begin(), s_mostEntries->end(), numberOfVisitOrder );
 
135
 
 
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 );
 
142
        }
 
143
 
 
144
        ++it;
 
145
    }
 
146
}
 
147
 
 
148
void KonqMostOftenURLSAction::slotEntryAdded( const KonqHistoryEntry& entry )
 
149
{
 
150
    // if it's already present, remove it, and inSort it
 
151
    s_mostEntries->removeEntry( entry.url );
 
152
 
 
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 );
 
158
        }
 
159
    }
 
160
 
 
161
    else
 
162
        inSort( entry );
 
163
    setEnabled( !s_mostEntries->isEmpty() );
 
164
}
 
165
 
 
166
void KonqMostOftenURLSAction::slotEntryRemoved( const KonqHistoryEntry& entry )
 
167
{
 
168
    s_mostEntries->removeEntry( entry.url );
 
169
    setEnabled( !s_mostEntries->isEmpty() );
 
170
}
 
171
 
 
172
void KonqMostOftenURLSAction::slotHistoryCleared()
 
173
{
 
174
    s_mostEntries->clear();
 
175
    setEnabled( false );
 
176
}
 
177
 
 
178
static void createHistoryAction(const KonqHistoryEntry& entry, QMenu* menu)
 
179
{
 
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);
 
190
}
 
191
 
 
192
void KonqMostOftenURLSAction::slotFillMenu()
 
193
{
 
194
    if (!m_parsingDone) { // first time
 
195
        parseHistory();
 
196
        m_parsingDone = true;
 
197
    }
 
198
 
 
199
    menu()->clear();
 
200
 
 
201
    for (int id = s_mostEntries->count() - 1; id >= 0; --id) {
 
202
        createHistoryAction(s_mostEntries->at(id), menu());
 
203
    }
 
204
    setEnabled( !s_mostEntries->isEmpty() );
 
205
}
 
206
 
 
207
void KonqMostOftenURLSAction::slotActivated(QAction* action)
 
208
{
 
209
    emit activated(action->data().value<KUrl>());
 
210
}
 
211
 
 
212
///////////////////////////////
 
213
 
 
214
KonqHistoryAction::KonqHistoryAction(const QString& text, QObject* parent)
 
215
    : KActionMenu(KIcon("go-jump"), text, parent)
 
216
{
 
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());
 
221
}
 
222
 
 
223
KonqHistoryAction::~KonqHistoryAction()
 
224
{
 
225
}
 
226
 
 
227
void KonqHistoryAction::slotFillMenu()
 
228
{
 
229
    menu()->clear();
 
230
 
 
231
    // Use the same configuration as the "most visited urls" action
 
232
    s_maxEntries = KonqSettings::numberofmostvisitedURLs();
 
233
 
 
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());
 
240
    }
 
241
}
 
242
 
 
243
void KonqHistoryAction::slotActivated(QAction* action)
 
244
{
 
245
    emit activated(action->data().value<KUrl>());
 
246
}
 
247
 
 
248
#include "konqactions.moc"