summaryrefslogtreecommitdiffstats
path: root/konqueror/sidebar/trees/history_module/history_item.cpp
blob: 0d31551f9fae7ad637d824d07144ee5150c67c56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* This file is part of the KDE project
   Copyright (C) 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <kbookmarkdrag.h>
#include <kprotocolinfo.h>
#include <konq_faviconmgr.h>
#include <tqpainter.h>

#include <assert.h>

#include "history_item.h"
#include "history_module.h"
#include "history_settings.h"
#include <kiconloader.h>

#define MYMODULE static_cast<KonqSidebarHistoryModule*>(module())
#define MYGROUP static_cast<KonqSidebarHistoryGroupItem*>(parent())

KonqSidebarHistorySettings * KonqSidebarHistoryItem::s_settings = 0L;

KonqSidebarHistoryItem::KonqSidebarHistoryItem( const KonqHistoryEntry *entry,
				  KonqSidebarTreeItem * parentItem,
				  KonqSidebarTreeTopLevelItem *topLevelItem )
    : KonqSidebarTreeItem( parentItem, topLevelItem )
{
    setExpandable( false );
    update( entry );
}

KonqSidebarHistoryItem::~KonqSidebarHistoryItem()
{
}

void KonqSidebarHistoryItem::update( const KonqHistoryEntry *entry )
{
    m_entry = entry;

    if (!entry)
        return;

    TQString title( entry->title );
    if ( !title.stripWhiteSpace().isEmpty() &&
	 title != entry->url.url() )
	setText( 0, title );
    else {
	TQString path( entry->url.path() );
	if ( path.isEmpty() )
	    path += '/';
	setText( 0, path );
    }

    KonqSidebarHistoryGroupItem *group = MYGROUP;
    assert(group);
    TQString path = entry->url.path();
    if ( group->hasFavIcon() && (path.isNull() || path == "/") )
    {
        const TQPixmap *pm = group->pixmap(0);
        if (pm)
	    setPixmap( 0, *pm );
    }
    else
    {
	setPixmap( 0, SmallIcon(KProtocolInfo::icon( entry->url.protocol() )));
    }

    group->itemUpdated( this ); // update for sorting
}

void KonqSidebarHistoryItem::itemSelected()
{
    tree()->enableActions( true, true, false, false, false, false );
}

void KonqSidebarHistoryItem::rightButtonPressed()
{
    MYMODULE->showPopupMenu();
}

TQDragObject * KonqSidebarHistoryItem::dragObject( TQWidget * parent, bool /*move*/ )
{
    TQString icon = KonqFavIconMgr::iconForURL( m_entry->url.url() );
    KBookmark bookmark = KBookmark::standaloneBookmark( m_entry->title,
                                                        m_entry->url, icon );
    KBookmarkDrag *drag = KBookmarkDrag::newDrag( bookmark, parent );
    return drag;
}

// new items go on top
TQString KonqSidebarHistoryItem::key( int column, bool ascending ) const
{
    if ( MYMODULE->sortsByName() )
	return KonqSidebarTreeItem::key( column, ascending );

    TQString tmp;
    tmp.sprintf( "%08x", m_entry->lastVisited.secsTo(MYMODULE->currentTime()));
    return tmp;
}

TQString KonqSidebarHistoryItem::toolTipText() const
{
    if ( s_settings->m_detailedTips ) {
        // this weird ordering of %4, %1, %2, %3 is due to the reason, that some
        // urls seem to contain %N, which would get substituted in the next
        // .arg() calls. So to fix this, we first substitute the last items
        // and then put in the url.
	TQString tip = i18n("<qt><center><b>%4</b></center><hr>Last visited: %1<br>First visited: %2<br>Number of times visited: %3</qt>");
	return tip.arg( KGlobal::locale()->formatDateTime( m_entry->lastVisited ) ).arg( KGlobal::locale()->formatDateTime( m_entry->firstVisited ) ).arg( m_entry->numberOfTimesVisited ).arg( m_entry->url.url() );
    }

    return m_entry->url.url();
}

void KonqSidebarHistoryItem::paintCell( TQPainter *p, const TQColorGroup & cg,
				 int column, int width, int alignment )
{
    TQDateTime dt;
    TQDateTime current = TQDateTime::currentDateTime();

    if ( s_settings->m_metricYoungerThan == KonqSidebarHistorySettings::DAYS )
	dt = TQT_TQDATETIME_OBJECT(current.addDays( - s_settings->m_valueYoungerThan ));
    else
	dt = TQT_TQDATETIME_OBJECT(current.addSecs( - (s_settings->m_valueYoungerThan * 60) ));

    if ( m_entry->lastVisited > dt )
	p->setFont( s_settings->m_fontYoungerThan );

    else {
	if ( s_settings->m_metricOlderThan == KonqSidebarHistorySettings::DAYS )
	    dt = TQT_TQDATETIME_OBJECT(current.addDays( - s_settings->m_valueOlderThan ));
	else
	    dt = TQT_TQDATETIME_OBJECT(current.addSecs( - (s_settings->m_valueOlderThan * 60) ));

	if ( m_entry->lastVisited < dt )
	    p->setFont( s_settings->m_fontOlderThan );
    }

    KonqSidebarTreeItem::paintCell( p, cg, column, width, alignment );
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////


KonqSidebarHistoryGroupItem::KonqSidebarHistoryGroupItem( const KURL& url,
					    KonqSidebarTreeTopLevelItem *topLevelItem)
    : KonqSidebarTreeItem( topLevelItem, topLevelItem ),
      m_hasFavIcon( false ),
      m_url( url )
{
}

void KonqSidebarHistoryGroupItem::setFavIcon( const TQPixmap& pix )
{
    setPixmap( 0, pix );
    m_hasFavIcon = true;
}

// the group item itself will be removed automatically,
// when the last child is removed
void KonqSidebarHistoryGroupItem::remove()
{
    KURL::List list;
    KonqSidebarHistoryItem *child = static_cast<KonqSidebarHistoryItem*>( firstChild() );
    while( child ) {
	list.append( child->externalURL() );
	child = static_cast<KonqSidebarHistoryItem*>( child->nextSibling() );
    }

    if ( !list.isEmpty() )
	KonqHistoryManager::kself()->emitRemoveFromHistory( list );
}

KonqSidebarHistoryItem * KonqSidebarHistoryGroupItem::findChild(const KonqHistoryEntry *entry) const
{
    TQListViewItem *child = firstChild();
    KonqSidebarHistoryItem *item = 0L;

    while ( child ) {
	item = static_cast<KonqSidebarHistoryItem *>( child );
	if ( item->entry() == entry )
	    return item;

	child = child->nextSibling();
    }

    return 0L;
}

void KonqSidebarHistoryGroupItem::itemSelected()
{
    tree()->enableActions( false, false, false,
                                                false, false, false );
}

void KonqSidebarHistoryGroupItem::rightButtonPressed()
{
    MYMODULE->showPopupMenu();
}

// let the module change our pixmap (opened/closed)
void KonqSidebarHistoryGroupItem::setOpen( bool open )
{
    MYMODULE->groupOpened( this, open );
    KonqSidebarTreeItem::setOpen( open );
}

// new items go on top
TQString KonqSidebarHistoryGroupItem::key( int column, bool ascending ) const
{
    if ( !m_lastVisited.isValid() || MYMODULE->sortsByName() )
	return KonqSidebarTreeItem::key( column, ascending );

    TQString tmp;
    tmp.sprintf( "%08x", m_lastVisited.secsTo( MYMODULE->currentTime() ));
    return tmp;
}

void KonqSidebarHistoryGroupItem::itemUpdated( KonqSidebarHistoryItem *item )
{
    if ( !m_lastVisited.isValid() || m_lastVisited < item->lastVisited() )
	m_lastVisited = item->lastVisited();
}

TQDragObject * KonqSidebarHistoryGroupItem::dragObject( TQWidget *parent, bool /*move*/)
{
    TQString icon = KonqFavIconMgr::iconForURL( m_url.url() );
    KBookmark bookmark = KBookmark::standaloneBookmark( TQString::null, m_url,
							icon );
    KBookmarkDrag *drag = KBookmarkDrag::newDrag( bookmark, parent );
    return drag;
}