Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / sidebar / trees / history_module / history_item.cpp
blobf064c36ca9f86d030b756ca18960839a2674a169
1 /* This file is part of the KDE project
2 Copyright (C) 2000,2001 Carsten Pfeiffer <pfeiffer@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 version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 // Own
20 #include "history_item.h"
22 // std
23 #include <assert.h>
25 // Qt
26 #include <QtGui/QPainter>
28 // KDE
29 #include <kbookmark.h>
30 #include <kprotocolinfo.h>
31 #include <kiconloader.h>
33 // Local
34 #include "history_module.h"
35 #include "history_settings.h"
37 #define MYMODULE static_cast<KonqSidebarHistoryModule*>(module())
38 #define MYGROUP static_cast<KonqSidebarHistoryGroupItem*>(parent())
40 KonqSidebarHistorySettings * KonqSidebarHistoryItem::s_settings = 0L;
42 KonqSidebarHistoryItem::KonqSidebarHistoryItem( const KonqHistoryEntry& entry,
43 KonqSidebarTreeItem * parentItem,
44 KonqSidebarTreeTopLevelItem *topLevelItem )
45 : KonqSidebarTreeItem( parentItem, topLevelItem )
47 setExpandable( false );
48 update( entry );
51 KonqSidebarHistoryItem::~KonqSidebarHistoryItem()
55 void KonqSidebarHistoryItem::update( const KonqHistoryEntry& entry )
57 m_entry = entry;
59 QString title( entry.title );
60 if ( !title.trimmed().isEmpty() &&
61 title != entry.url.url() )
62 setText( 0, title );
63 else {
64 QString path( entry.url.path() );
65 if ( path.isEmpty() )
66 path += '/';
67 setText( 0, path );
70 KonqSidebarHistoryGroupItem *group = MYGROUP;
71 assert(group);
72 QString path = entry.url.path();
73 if ( group->hasFavIcon() && (path.isNull() || path == "/") )
75 const QPixmap *pm = group->pixmap(0);
76 if (pm)
77 setPixmap( 0, *pm );
79 else
81 setPixmap( 0, SmallIcon(KProtocolInfo::icon( entry.url.protocol() )));
84 group->itemUpdated( this ); // update for sorting
87 void KonqSidebarHistoryItem::itemSelected()
89 tree()->enableActions( true, true, false, false, false, false );
92 void KonqSidebarHistoryItem::rightButtonPressed()
94 MYMODULE->showPopupMenu();
97 bool KonqSidebarHistoryItem::populateMimeData( QMimeData* mimeData, bool /*move*/ )
99 QString icon = KMimeType::favIconForUrl( m_entry.url );
100 KBookmark bookmark = KBookmark::standaloneBookmark( m_entry.title,
101 m_entry.url, icon );
102 bookmark.populateMimeData( mimeData );
103 return true;
106 // new items go on top
107 QString KonqSidebarHistoryItem::key( int column, bool ascending ) const
109 if ( MYMODULE->sortsByName() )
110 return KonqSidebarTreeItem::key( column, ascending );
112 QString tmp;
113 tmp.sprintf( "%08x", m_entry.lastVisited.secsTo(MYMODULE->currentTime()));
114 return tmp;
117 QString KonqSidebarHistoryItem::toolTipText() const
119 if ( s_settings->m_detailedTips ) {
120 return i18n("<qt><center><b>%1</b></center><hr />Last visited: %2<br />"
121 "First visited: %3<br />Number of times visited: %4</qt>",
122 m_entry.url.url(),
123 KGlobal::locale()->formatDateTime( m_entry.lastVisited ),
124 KGlobal::locale()->formatDateTime( m_entry.firstVisited ),
125 m_entry.numberOfTimesVisited);
128 return m_entry.url.url();
131 void KonqSidebarHistoryItem::paintCell( QPainter *p, const QColorGroup & cg,
132 int column, int width, int alignment )
134 QDateTime dt;
135 QDateTime current = QDateTime::currentDateTime();
137 if ( s_settings->m_metricYoungerThan == KonqSidebarHistorySettings::DAYS )
138 dt = current.addDays( - s_settings->m_valueYoungerThan );
139 else
140 dt = current.addSecs( - (s_settings->m_valueYoungerThan * 60) );
142 if ( m_entry.lastVisited > dt )
143 p->setFont( s_settings->m_fontYoungerThan );
145 else {
146 if ( s_settings->m_metricOlderThan == KonqSidebarHistorySettings::DAYS )
147 dt = current.addDays( - s_settings->m_valueOlderThan );
148 else
149 dt = current.addSecs( - (s_settings->m_valueOlderThan * 60) );
151 if ( m_entry.lastVisited < dt )
152 p->setFont( s_settings->m_fontOlderThan );
155 KonqSidebarTreeItem::paintCell( p, cg, column, width, alignment );
158 ///////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////
162 KonqSidebarHistoryGroupItem::KonqSidebarHistoryGroupItem( const KUrl& url,
163 KonqSidebarTreeTopLevelItem *topLevelItem)
164 : KonqSidebarTreeItem( topLevelItem, topLevelItem ),
165 m_hasFavIcon( false ),
166 m_url( url )
170 void KonqSidebarHistoryGroupItem::setFavIcon( const QPixmap& pix )
172 setPixmap( 0, pix );
173 m_hasFavIcon = true;
176 // the group item itself will be removed automatically,
177 // when the last child is removed
178 void KonqSidebarHistoryGroupItem::remove()
180 KUrl::List list;
181 KonqSidebarHistoryItem *child = static_cast<KonqSidebarHistoryItem*>( firstChild() );
182 while( child ) {
183 list.append( child->externalURL() );
184 child = static_cast<KonqSidebarHistoryItem*>( child->nextSibling() );
187 if ( !list.isEmpty() )
188 KonqHistoryManager::kself()->emitRemoveListFromHistory( list );
191 KonqSidebarHistoryItem * KonqSidebarHistoryGroupItem::findChild(const KonqHistoryEntry& entry) const
193 Q3ListViewItem *child = firstChild();
194 KonqSidebarHistoryItem *item = 0L;
196 while ( child ) {
197 item = static_cast<KonqSidebarHistoryItem *>( child );
198 if ( item->entry().url == entry.url )
199 return item;
201 child = child->nextSibling();
204 return 0L;
207 void KonqSidebarHistoryGroupItem::itemSelected()
209 tree()->enableActions( false, false, false,
210 false, false, false );
213 void KonqSidebarHistoryGroupItem::rightButtonPressed()
215 MYMODULE->showPopupMenu();
218 // let the module change our pixmap (opened/closed)
219 void KonqSidebarHistoryGroupItem::setOpen( bool open )
221 MYMODULE->groupOpened( this, open );
222 KonqSidebarTreeItem::setOpen( open );
225 // new items go on top
226 QString KonqSidebarHistoryGroupItem::key( int column, bool ascending ) const
228 if ( !m_lastVisited.isValid() || MYMODULE->sortsByName() )
229 return KonqSidebarTreeItem::key( column, ascending );
231 QString tmp;
232 tmp.sprintf( "%08x", m_lastVisited.secsTo( MYMODULE->currentTime() ));
233 return tmp;
236 void KonqSidebarHistoryGroupItem::itemUpdated( KonqSidebarHistoryItem *item )
238 if ( !m_lastVisited.isValid() || m_lastVisited < item->lastVisited() )
239 m_lastVisited = item->lastVisited();
242 bool KonqSidebarHistoryGroupItem::populateMimeData( QMimeData* mimeData, bool /*move*/ )
244 QString icon = KMimeType::favIconForUrl( m_url );
245 KBookmark bookmark = KBookmark::standaloneBookmark( QString(), m_url,
246 icon );
247 bookmark.populateMimeData( mimeData );
248 return true;