not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / klipper / history.cpp
blobb9a4eab6b39c1a1b861c8a3678e604ab490a984f
1 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8; -*-
2 /* This file is part of the KDE project
3 Copyright (C) 2004 Esben Mose Hansen <kde@mosehansen.dk>
4 Copyright (C) by Andrew Stanley-Jones
5 Copyright (C) 2000 by Carsten Pfeiffer <pfeiffer@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 #include <kdebug.h>
24 #include "history.h"
25 #include "historystringitem.h"
26 #include "klipperpopup.h"
28 History::History( QObject* parent )
29 : QObject( parent ),
30 m_popup( new KlipperPopup( this ) ),
31 m_topIsUserSelected( false )
33 connect( this, SIGNAL( changed() ), m_popup, SLOT( slotHistoryChanged() ) );
38 History::~History() {
39 qDeleteAll(itemList);
42 History::iterator History::youngest() {
43 return iterator( itemList );
46 void History::insert( const HistoryItem* item ) {
47 if ( !item )
48 return;
50 m_topIsUserSelected = false;
52 // Optimization: Compare with top item.
53 if ( !itemList.isEmpty() && *itemList.first() == *item ) {
54 delete item;
55 return;
58 remove( item );
59 forceInsert( item );
61 emit topChanged();
65 void History::forceInsert( const HistoryItem* item ) {
66 if ( !item )
67 return;
68 itemList.prepend( item );
69 emit changed();
70 trim();
73 void History::trim() {
74 int i = itemList.count() - max_size();
75 if ( i <= 0 )
76 return;
78 while ( i-- ) {
79 itemList.removeLast();
81 emit changed();
84 void History::remove( const HistoryItem* newItem ) {
85 if ( !newItem )
86 return;
88 if (itemList.contains(newItem)) {
89 itemList.removeAll(newItem);
90 emit changed();
95 void History::slotClear() {
96 itemList.clear();
97 emit changed();
100 void History::slotMoveToTop(QAction *action) {
101 bool ok = false;
102 int pos = action->data().toInt(&ok);
103 if (!ok) // not an action from popupproxy
104 return;
106 if ( pos < 0 || pos >= itemList.count() ) {
107 kDebug() << "Argument pos out of range: " << pos;
108 return;
111 m_topIsUserSelected = true;
113 itemList.move(pos, 0);
114 emit changed();
115 emit topChanged();
118 void History::max_size( unsigned max_size ) {
119 m_max_size = max_size;
120 trim();
124 KlipperPopup* History::popup() {
125 return m_popup;
128 #include "history.moc"