1/* This file is part of the KDE project
2 Copyright (C) 2004 Esben Mose Hansen <kde@mosehansen.dk>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU 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 program 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 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "historyurlitem.h"
20
21#include <QtCore/QMimeData>
22#include <QtCore/QCryptographicHash>
23
24namespace {
25 QByteArray compute_uuid(const KUrl::List& _urls, KUrl::MetaDataMap _metaData, bool _cut ) {
26 QCryptographicHash hash(QCryptographicHash::Sha1);
27 foreach(const KUrl& url, _urls) {
28 hash.addData(url.toEncoded());
29 hash.addData("\0", 1); // Use binary zero as that is not a valid path character
30 }
31 QByteArray buffer;
32 QDataStream out(&buffer, QIODevice::WriteOnly);
33 out << _metaData << "\0" << _cut;
34 hash.addData(buffer);
35 return hash.result();
36 }
37}
38
39HistoryURLItem::HistoryURLItem( const KUrl::List& _urls, KUrl::MetaDataMap _metaData, bool _cut )
40 : HistoryItem(compute_uuid(_urls, _metaData, _cut))
41 , m_urls( _urls )
42 , m_metaData( _metaData )
43 , m_cut( _cut )
44{
45}
46
47/* virtual */
48void HistoryURLItem::write( QDataStream& stream ) const
49{
50 stream << QString( "url" ) << m_urls << m_metaData << (int)m_cut;
51}
52
53QString HistoryURLItem::text() const {
54 return m_urls.toStringList().join( " " );
55}
56
57QMimeData* HistoryURLItem::mimeData() const {
58 QMimeData *data = new QMimeData();
59 m_urls.populateMimeData(data, m_metaData);
60 data->setData("application/x-kde-cutselection", QByteArray(m_cut ? "1" : "0"));
61 return data;
62}
63
64bool HistoryURLItem::operator==( const HistoryItem& rhs) const
65{
66 if ( const HistoryURLItem* casted_rhs = dynamic_cast<const HistoryURLItem*>( &rhs ) ) {
67 return casted_rhs->m_urls == m_urls
68 && casted_rhs->m_metaData.count() == m_metaData.count()
69 && qEqual( casted_rhs->m_metaData.begin(), casted_rhs->m_metaData.end(), m_metaData.begin())
70 && casted_rhs->m_cut == m_cut;
71 }
72 return false;
73}
74