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.
20 #include "history_item.h"
26 #include <QtGui/QPainter>
29 #include <kbookmark.h>
30 #include <kprotocolinfo.h>
31 #include <kiconloader.h>
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 );
51 KonqSidebarHistoryItem::~KonqSidebarHistoryItem()
55 void KonqSidebarHistoryItem::update( const KonqHistoryEntry
& entry
)
59 QString
title( entry
.title
);
60 if ( !title
.trimmed().isEmpty() &&
61 title
!= entry
.url
.url() )
64 QString
path( entry
.url
.path() );
70 KonqSidebarHistoryGroupItem
*group
= MYGROUP
;
72 QString path
= entry
.url
.path();
73 if ( group
->hasFavIcon() && (path
.isNull() || path
== "/") )
75 const QPixmap
*pm
= group
->pixmap(0);
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
,
102 bookmark
.populateMimeData( mimeData
);
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
);
113 tmp
.sprintf( "%08x", m_entry
.lastVisited
.secsTo(MYMODULE
->currentTime()));
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>",
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
)
135 QDateTime current
= QDateTime::currentDateTime();
137 if ( s_settings
->m_metricYoungerThan
== KonqSidebarHistorySettings::DAYS
)
138 dt
= current
.addDays( - s_settings
->m_valueYoungerThan
);
140 dt
= current
.addSecs( - (s_settings
->m_valueYoungerThan
* 60) );
142 if ( m_entry
.lastVisited
> dt
)
143 p
->setFont( s_settings
->m_fontYoungerThan
);
146 if ( s_settings
->m_metricOlderThan
== KonqSidebarHistorySettings::DAYS
)
147 dt
= current
.addDays( - s_settings
->m_valueOlderThan
);
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 ),
170 void KonqSidebarHistoryGroupItem::setFavIcon( const QPixmap
& pix
)
176 // the group item itself will be removed automatically,
177 // when the last child is removed
178 void KonqSidebarHistoryGroupItem::remove()
181 KonqSidebarHistoryItem
*child
= static_cast<KonqSidebarHistoryItem
*>( firstChild() );
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;
197 item
= static_cast<KonqSidebarHistoryItem
*>( child
);
198 if ( item
->entry().url
== entry
.url
)
201 child
= child
->nextSibling();
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
);
232 tmp
.sprintf( "%08x", m_lastVisited
.secsTo( MYMODULE
->currentTime() ));
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
,
247 bookmark
.populateMimeData( mimeData
);