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 "historyitem.h"
20
21#include <QtCore/QMap>
22#include <QtGui/QPixmap>
23
24#include <KDebug>
25
26#include "historystringitem.h"
27#include "historyimageitem.h"
28#include "historyurlitem.h"
29
30HistoryItem::HistoryItem(const QByteArray& uuid) : m_uuid(uuid) {
31
32}
33
34HistoryItem::~HistoryItem() {
35
36}
37
38HistoryItem* HistoryItem::create( const QMimeData* data )
39{
40#if 0
41 int i=0;
42 foreach ( QString format, data->formats() ) {
43 kDebug() << "format(" << i++ <<"): " << format;
44 }
45#endif
46 if (KUrl::List::canDecode(data))
47 {
48 KUrl::MetaDataMap metaData;
49 KUrl::List urls = KUrl::List::fromMimeData(data, &metaData);
50 QByteArray bytes = data->data("application/x-kde-cutselection");
51 bool cut = !bytes.isEmpty() && (bytes.at(0) == '1'); // true if 1
52 return new HistoryURLItem(urls, metaData, cut);
53 }
54 if (data->hasText())
55 {
56 return new HistoryStringItem(data->text());
57 }
58 if (data->hasImage())
59 {
60 QImage image = qvariant_cast<QImage>(data->imageData());
61 return new HistoryImageItem(QPixmap::fromImage(image));
62 }
63
64 return 0; // Failed.
65}
66
67HistoryItem* HistoryItem::create( QDataStream& dataStream ) {
68 if ( dataStream.atEnd() ) {
69 return 0;
70 }
71 QString type;
72 dataStream >> type;
73 if ( type == "url" ) {
74 KUrl::List urls;
75 QMap< QString, QString > metaData;
76 int cut;
77 dataStream >> urls;
78 dataStream >> metaData;
79 dataStream >> cut;
80 return new HistoryURLItem( urls, metaData, cut );
81 }
82 if ( type == "string" ) {
83 QString text;
84 dataStream >> text;
85 return new HistoryStringItem( text );
86 }
87 if ( type == "image" ) {
88 QPixmap image;
89 dataStream >> image;
90 return new HistoryImageItem( image );
91 }
92 kWarning() << "Failed to restore history item: Unknown type \"" << type << "\"" ;
93 return 0;
94}
95
96
97
98void HistoryItem::chain(HistoryItem* next)
99{
100 m_next_uuid = next->uuid();
101 next->m_previous_uuid = uuid();
102}
103
104void HistoryItem::insertBetweeen(HistoryItem* prev, HistoryItem* next)
105{
106 if (prev && next) {
107 prev->chain(this);
108 chain(next);
109 } else {
110 Q_ASSERT(!prev && !next);
111 // First item in chain
112 m_next_uuid = m_uuid;
113 m_previous_uuid = m_uuid;
114 }
115#if 0 // Extra checks, if anyone ever needs them
116 Q_ASSERT(prev->uuid() == m_previous_uuid);
117 Q_ASSERT(prev->next_uuid() == m_uuid);
118 Q_ASSERT(next->previous_uuid() == m_uuid);
119 Q_ASSERT(next->uuid() == m_next_uuid);
120 Q_ASSERT(prev->uuid() != uuid());
121 Q_ASSERT(next->uuid() != uuid());
122#endif
123}
124
125