summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/cdragdropmgr.cpp
blob: 0179cc5c0bdef81ebe1d8ca78a1b09c51eff10e7 (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
247
248
249
250
251
252
253
254
255
256
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2006 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/



#include "cdragdropmgr.h"

#include "backend/cswordmoduleinfo.h"
#include "backend/cswordbackend.h"
#include "backend/cswordversekey.h"
#include "util/cpointers.h"

//Sword includes
#include "versekey.h"

//TQt includes
#include <tqevent.h>
#include <tqdom.h>

CDragDropMgr::BTDrag::BTDrag( const TQString& xml, TQWidget* dragSource, const char* name)
: TQTextDrag(xml, dragSource, name) {}
;

//static function to see whether we can decode tje given mime type
bool CDragDropMgr::BTDrag::canDecode( const TQMimeSource * mime ) {
	if ( mime->provides("BibleTime/DND") ) { //we can decode this type!
		return true;
	}
	return false; //not yet implemented
};

bool CDragDropMgr::BTDrag::provides( const char* type ) const {
	return (type == "BibleTime/DND"); //return only true if the type is BibleTime/DND
};

const char* CDragDropMgr::BTDrag::format( int i ) const {
	if ( i == 0) { //we support only one format!
	return "BibleTime/DND";
	};
	return 0;
};

bool CDragDropMgr::BTDrag::decode(const TQMimeSource* e, TQString& str) {
	if (canDecode(e)) {
		str = TQString( e->encodedData( "BibleTime/DND" ) );
		return true;
	}
	return false;
};

bool CDragDropMgr::BTDrag::decode(const TQMimeSource* e, TQString& str, TQCString& /*subtype*/) {
	return decode(e, str);
};

TQByteArray CDragDropMgr::BTDrag::encodedData( const char* /*type*/ ) const {
	return TQTextDrag::encodedData("text/plain"); //hack because TQTextDrag only accepts text/plainand not our BibleTime/DND type
};

///////////////////////////// new class //////////////////////

CDragDropMgr::Item::Item( const TQString& text )
: m_type(Text),
m_bookmarkModuleName(TQString()),
m_bookmarkKey(TQString()),
m_bookmarkDescription(TQString()),
m_text(text) {}

CDragDropMgr::Item::Item( const TQString& moduleName, const TQString& key, const TQString& description  )
: m_type(Bookmark),
m_bookmarkModuleName(moduleName),
m_bookmarkKey(key),
m_bookmarkDescription(description),
m_text(TQString()) {
	//we have to make sure the key is saved in it's english representation, so we convert it
	if (CSwordModuleInfo* mod = CPointers::backend()->findModuleByName( moduleName )) {
		if (mod->type() == CSwordModuleInfo::Bible || mod->type() == CSwordModuleInfo::Commentary) {
			CSwordVerseKey vk(0);
			vk.key( key );
			vk.setLocale("en");

			m_bookmarkKey = vk.key();
			//      qWarning("english key of %s is %s", key.latin1(), m_bookmarkKey.latin1());
		}
	}
}

CDragDropMgr::Item::~Item() {}

const CDragDropMgr::Item::Type& CDragDropMgr::Item::type() const {
	//returns the type of drag & drop action this item represents
	return m_type;
}

/** Returns the text which is used by this DragDrop Item, only valid if type() == Text */
const TQString& CDragDropMgr::Item::text() const {
	//  Q_ASSERT(!m_text.isEmpty());
	return m_text;
}

/** Returns the key, ony valid if type() == Bookmark */
const TQString& CDragDropMgr::Item::bookmarkKey() const {
	//  Q_ASSERT(!m_bookmarkKey.isEmpty());
	return m_bookmarkKey;
}

/** Returns the bookmark module, ony valid if type() == Bookmark */
const TQString& CDragDropMgr::Item::bookmarkModule() const {
	//  Q_ASSERT(!m_bookmarkModuleName.isEmpty());
	return m_bookmarkModuleName;
}

/** Returns the bookmark description, ony valid if type() == Bookmark */
const TQString& CDragDropMgr::Item::bookmarkDescription() const {
	//  Q_ASSERT(!m_bookmarkDescription.isEmpty());
	return m_bookmarkDescription;
}

////////////////////////////////// NEW CLASS //////////////////////////

CDragDropMgr::CDragDropMgr() {}

CDragDropMgr::~CDragDropMgr() {}

const bool CDragDropMgr::canDecode( const TQMimeSource* const mime ) {
	if (CDragDropMgr::BTDrag::canDecode(mime)) {
		return true;
	}
	else if( TQTextDrag::canDecode(mime) ) {
		qWarning("TQTextDrag can decode this mime!");
		return true;
	};
	return false;
};

TQDragObject* const CDragDropMgr::dragObject( CDragDropMgr::ItemList& items, TQWidget* dragSource ) {
	if ( items.count() ) {
		//process the items and set the data to the dragobject we return later
		TQDomDocument doc("DOC");
		doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
		TQDomElement content = doc.createElement("BibleTimeDND");
		content.setAttribute("syntaxVersion", "1.0");
		doc.appendChild(content);

		CDragDropMgr::ItemList::iterator it;
		for ( it = items.begin(); it != items.end(); ++it ) {
			Item item = (*it);
			if (item.type() == Item::Bookmark) { //a bookmark was dragged
				//append the XML stuff for a bookmark
				TQDomElement bookmark = doc.createElement("BOOKMARK");
				bookmark.setAttribute("key", item.bookmarkKey());
				bookmark.setAttribute("description", item.bookmarkDescription());
				bookmark.setAttribute("moduleName", item.bookmarkModule());

				content.appendChild(bookmark);
			}
			else if (item.type() == Item::Text) { //plain text was dragged
				//append the XML stuff for plain text
				TQDomElement plainText = doc.createElement("TEXT");
				plainText.setAttribute("text", item.text());

				content.appendChild(plainText);
			}
		}

		BTDrag* dragObject = new BTDrag( doc.toString(), dragSource );
		//    qWarning("DND data created: %s", (const char*)doc.toString().utf8());
		return dragObject;
	};
	return 0;
};

CDragDropMgr::ItemList CDragDropMgr::decode( const TQMimeSource* const  src) {
	//if the drag was started by another widget which doesn't use CDragDropMgr (a drag created by TQTextDrag)
	if (canDecode(src) && TQTextDrag::canDecode(src)) { //if we can decode but it's a TQTextDrag and not a BTDrag object
		TQString text;
		TQTextDrag::decode(src, text);
		//    qWarning(text.latin1());

		CDragDropMgr::ItemList dndItems;
		dndItems.append( Item(text) );
		return dndItems;
	}
	else if (!canDecode(src)) { //if we can't decode it
		return CDragDropMgr::ItemList();
	};

	TQString xmlData;
	BTDrag::decode(src, xmlData);

	if (xmlData.isEmpty()) { //something went wrong!
		//    qWarning("CDragDropMgr::decode: empty xml data!");
		return CDragDropMgr::ItemList();
	}
	//  else {
	//    qWarning("Drag&Drop data is: %s", xmlData.latin1());
	//  }

	//we can handle the dropEvent and have xml data to work on!
	ItemList dndItems;

	TQDomDocument doc;
	doc.setContent( xmlData );

	TQDomElement document = doc.documentElement();
	if( document.tagName() != "BibleTimeDND" ) { //BibleTime was used in syntax version 1.0
		qWarning("DragDropMgr::decode: Missing BibleTimeDND doc");
		return CDragDropMgr::ItemList();
	}
	// see if there's a section with the name MAINWINDOW
	TQDomElement elem = document.firstChild().toElement();
	while (!elem.isNull()) {
		if (elem.tagName() == "BOOKMARK") { //we found a bookmark!
			//        qWarning("found a bookmark!");
			const TQString key = elem.hasAttribute("key") ? elem.attribute("key") : TQString();
			const TQString moduleName = elem.hasAttribute("moduleName") ? elem.attribute("moduleName") : TQString();
			const TQString description = elem.hasAttribute("description") ? elem.attribute("description") : TQString();

			dndItems.append( CDragDropMgr::Item(moduleName, key, description) );
		}
		else if (elem.tagName() == "TEXT") { //we found a plain text passage!
			const TQString text = elem.hasAttribute("text") ? elem.attribute("text") : TQString();
			dndItems.append( CDragDropMgr::Item(text) );
		};
		elem = elem.nextSibling().toElement();
	};

	return dndItems;
};

/** Returns which type the given drop event has, if it's a mixed one (both bookmarks and plain text), which shouldn't happen, it return Item::Unknown. */
CDragDropMgr::Item::Type CDragDropMgr::dndType( const TQMimeSource* e ) {
	ItemList dndItems = decode(e);
	if (dndItems.isEmpty()) {//wrong dropEvent or something strange
		return Item::Unknown;
	};

	//check whether all items have the ssame type, if they do return the type
	//as soon as two items have different types return Item::Unknown
	ItemList::Iterator it;
	Item::Type type = Item::Unknown;
	for( it = dndItems.begin(); it != dndItems.end(); ++it ) {
		if( type == Item::Unknown) { //if Unknown is set this is the first loop, don't return Unknown
			type = (*it).type();
		}
		else if (type != (*it).type() ) {//items have different type, return Item::Unknown
			return Item::Unknown;
		};
	};
	return type;
}