~ubuntu-branches/ubuntu/intrepid/bibletime/intrepid

« back to all changes in this revision

Viewing changes to bibletime/frontend/cdragdropmgr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralph Janke
  • Date: 2008-05-10 15:18:16 UTC
  • mfrom: (1.1.6 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080510151816-bqp8y1to705zd0fm
Tags: 1.6.5.1-1
* New upstream version (Closes: #441161, #271502)
* fixes for new autotools and gcc 4.3 (Closes: #407291)
* added poxml to Build-Depends
* No DFSG necessary anymore since biblestudy howto has 
  now Commons Licence 
* Added libclucene-dev to dev-depends (Closes: #436677)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********
 
2
*
 
3
* This file is part of BibleTime's source code, http://www.bibletime.info/.
 
4
*
 
5
* Copyright 1999-2006 by the BibleTime developers.
 
6
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
 
7
*
 
8
**********/
 
9
 
 
10
 
 
11
 
 
12
#include "cdragdropmgr.h"
 
13
 
 
14
#include "backend/cswordmoduleinfo.h"
 
15
#include "backend/cswordbackend.h"
 
16
#include "backend/cswordversekey.h"
 
17
#include "util/cpointers.h"
 
18
 
 
19
//Sword includes
 
20
#include "versekey.h"
 
21
 
 
22
//Qt includes
 
23
#include <qevent.h>
 
24
#include <qdom.h>
 
25
 
 
26
CDragDropMgr::BTDrag::BTDrag( const QString& xml, QWidget* dragSource, const char* name)
 
27
: QTextDrag(xml, dragSource, name) {}
 
28
;
 
29
 
 
30
//static function to see whether we can decode tje given mime type
 
31
bool CDragDropMgr::BTDrag::canDecode( const QMimeSource * mime ) {
 
32
        if ( mime->provides("BibleTime/DND") ) { //we can decode this type!
 
33
                return true;
 
34
        }
 
35
        return false; //not yet implemented
 
36
};
 
37
 
 
38
bool CDragDropMgr::BTDrag::provides( const char* type ) const {
 
39
        return (type == "BibleTime/DND"); //return only true if the type is BibleTime/DND
 
40
};
 
41
 
 
42
const char* CDragDropMgr::BTDrag::format( int i ) const {
 
43
        if ( i == 0) { //we support only one format!
 
44
        return "BibleTime/DND";
 
45
        };
 
46
        return 0;
 
47
};
 
48
 
 
49
bool CDragDropMgr::BTDrag::decode(const QMimeSource* e, QString& str) {
 
50
        if (canDecode(e)) {
 
51
                str = QString( e->encodedData( "BibleTime/DND" ) );
 
52
                return true;
 
53
        }
 
54
        return false;
 
55
};
 
56
 
 
57
bool CDragDropMgr::BTDrag::decode(const QMimeSource* e, QString& str, QCString& /*subtype*/) {
 
58
        return decode(e, str);
 
59
};
 
60
 
 
61
QByteArray CDragDropMgr::BTDrag::encodedData( const char* /*type*/ ) const {
 
62
        return QTextDrag::encodedData("text/plain"); //hack because QTextDrag only accepts text/plainand not our BibleTime/DND type
 
63
};
 
64
 
 
65
///////////////////////////// new class //////////////////////
 
66
 
 
67
CDragDropMgr::Item::Item( const QString& text )
 
68
: m_type(Text),
 
69
m_bookmarkModuleName(QString::null),
 
70
m_bookmarkKey(QString::null),
 
71
m_bookmarkDescription(QString::null),
 
72
m_text(text) {}
 
73
 
 
74
CDragDropMgr::Item::Item( const QString& moduleName, const QString& key, const QString& description  )
 
75
: m_type(Bookmark),
 
76
m_bookmarkModuleName(moduleName),
 
77
m_bookmarkKey(key),
 
78
m_bookmarkDescription(description),
 
79
m_text(QString::null) {
 
80
        //we have to make sure the key is saved in it's english representation, so we convert it
 
81
        if (CSwordModuleInfo* mod = CPointers::backend()->findModuleByName( moduleName )) {
 
82
                if (mod->type() == CSwordModuleInfo::Bible || mod->type() == CSwordModuleInfo::Commentary) {
 
83
                        CSwordVerseKey vk(0);
 
84
                        vk.key( key );
 
85
                        vk.setLocale("en");
 
86
 
 
87
                        m_bookmarkKey = vk.key();
 
88
                        //      qWarning("english key of %s is %s", key.latin1(), m_bookmarkKey.latin1());
 
89
                }
 
90
        }
 
91
}
 
92
 
 
93
CDragDropMgr::Item::~Item() {}
 
94
 
 
95
const CDragDropMgr::Item::Type& CDragDropMgr::Item::type() const {
 
96
        //returns the type of drag & drop action this item represents
 
97
        return m_type;
 
98
}
 
99
 
 
100
/** Returns the text which is used by this DragDrop Item, only valid if type() == Text */
 
101
const QString& CDragDropMgr::Item::text() const {
 
102
        //  Q_ASSERT(!m_text.isEmpty());
 
103
        return m_text;
 
104
}
 
105
 
 
106
/** Returns the key, ony valid if type() == Bookmark */
 
107
const QString& CDragDropMgr::Item::bookmarkKey() const {
 
108
        //  Q_ASSERT(!m_bookmarkKey.isEmpty());
 
109
        return m_bookmarkKey;
 
110
}
 
111
 
 
112
/** Returns the bookmark module, ony valid if type() == Bookmark */
 
113
const QString& CDragDropMgr::Item::bookmarkModule() const {
 
114
        //  Q_ASSERT(!m_bookmarkModuleName.isEmpty());
 
115
        return m_bookmarkModuleName;
 
116
}
 
117
 
 
118
/** Returns the bookmark description, ony valid if type() == Bookmark */
 
119
const QString& CDragDropMgr::Item::bookmarkDescription() const {
 
120
        //  Q_ASSERT(!m_bookmarkDescription.isEmpty());
 
121
        return m_bookmarkDescription;
 
122
}
 
123
 
 
124
////////////////////////////////// NEW CLASS //////////////////////////
 
125
 
 
126
CDragDropMgr::CDragDropMgr() {}
 
127
 
 
128
CDragDropMgr::~CDragDropMgr() {}
 
129
 
 
130
const bool CDragDropMgr::canDecode( const QMimeSource* const mime ) {
 
131
        if (CDragDropMgr::BTDrag::canDecode(mime)) {
 
132
                return true;
 
133
        }
 
134
        else if( QTextDrag::canDecode(mime) ) {
 
135
                qWarning("QTextDrag can decode this mime!");
 
136
                return true;
 
137
        };
 
138
        return false;
 
139
};
 
140
 
 
141
QDragObject* const CDragDropMgr::dragObject( CDragDropMgr::ItemList& items, QWidget* dragSource ) {
 
142
        if ( items.count() ) {
 
143
                //process the items and set the data to the dragobject we return later
 
144
                QDomDocument doc("DOC");
 
145
                doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
 
146
                QDomElement content = doc.createElement("BibleTimeDND");
 
147
                content.setAttribute("syntaxVersion", "1.0");
 
148
                doc.appendChild(content);
 
149
 
 
150
                CDragDropMgr::ItemList::iterator it;
 
151
                for ( it = items.begin(); it != items.end(); ++it ) {
 
152
                        Item item = (*it);
 
153
                        if (item.type() == Item::Bookmark) { //a bookmark was dragged
 
154
                                //append the XML stuff for a bookmark
 
155
                                QDomElement bookmark = doc.createElement("BOOKMARK");
 
156
                                bookmark.setAttribute("key", item.bookmarkKey());
 
157
                                bookmark.setAttribute("description", item.bookmarkDescription());
 
158
                                bookmark.setAttribute("moduleName", item.bookmarkModule());
 
159
 
 
160
                                content.appendChild(bookmark);
 
161
                        }
 
162
                        else if (item.type() == Item::Text) { //plain text was dragged
 
163
                                //append the XML stuff for plain text
 
164
                                QDomElement plainText = doc.createElement("TEXT");
 
165
                                plainText.setAttribute("text", item.text());
 
166
 
 
167
                                content.appendChild(plainText);
 
168
                        }
 
169
                }
 
170
 
 
171
                BTDrag* dragObject = new BTDrag( doc.toString(), dragSource );
 
172
                //    qWarning("DND data created: %s", (const char*)doc.toString().utf8());
 
173
                return dragObject;
 
174
        };
 
175
        return 0;
 
176
};
 
177
 
 
178
CDragDropMgr::ItemList CDragDropMgr::decode( const QMimeSource* const  src) {
 
179
        //if the drag was started by another widget which doesn't use CDragDropMgr (a drag created by QTextDrag)
 
180
        if (canDecode(src) && QTextDrag::canDecode(src)) { //if we can decode but it's a QTextDrag and not a BTDrag object
 
181
                QString text;
 
182
                QTextDrag::decode(src, text);
 
183
                //    qWarning(text.latin1());
 
184
 
 
185
                CDragDropMgr::ItemList dndItems;
 
186
                dndItems.append( Item(text) );
 
187
                return dndItems;
 
188
        }
 
189
        else if (!canDecode(src)) { //if we can't decode it
 
190
                return CDragDropMgr::ItemList();
 
191
        };
 
192
 
 
193
        QString xmlData;
 
194
        BTDrag::decode(src, xmlData);
 
195
 
 
196
        if (xmlData.isEmpty()) { //something went wrong!
 
197
                //    qWarning("CDragDropMgr::decode: empty xml data!");
 
198
                return CDragDropMgr::ItemList();
 
199
        }
 
200
        //  else {
 
201
        //    qWarning("Drag&Drop data is: %s", xmlData.latin1());
 
202
        //  }
 
203
 
 
204
        //we can handle the dropEvent and have xml data to work on!
 
205
        ItemList dndItems;
 
206
 
 
207
        QDomDocument doc;
 
208
        doc.setContent( xmlData );
 
209
 
 
210
        QDomElement document = doc.documentElement();
 
211
        if( document.tagName() != "BibleTimeDND" ) { //BibleTime was used in syntax version 1.0
 
212
                qWarning("DragDropMgr::decode: Missing BibleTimeDND doc");
 
213
                return CDragDropMgr::ItemList();
 
214
        }
 
215
        // see if there's a section with the name MAINWINDOW
 
216
        QDomElement elem = document.firstChild().toElement();
 
217
        while (!elem.isNull()) {
 
218
                if (elem.tagName() == "BOOKMARK") { //we found a bookmark!
 
219
                        //        qWarning("found a bookmark!");
 
220
                        const QString key = elem.hasAttribute("key") ? elem.attribute("key") : QString::null;
 
221
                        const QString moduleName = elem.hasAttribute("moduleName") ? elem.attribute("moduleName") : QString::null;
 
222
                        const QString description = elem.hasAttribute("description") ? elem.attribute("description") : QString::null;
 
223
 
 
224
                        dndItems.append( CDragDropMgr::Item(moduleName, key, description) );
 
225
                }
 
226
                else if (elem.tagName() == "TEXT") { //we found a plain text passage!
 
227
                        const QString text = elem.hasAttribute("text") ? elem.attribute("text") : QString::null;
 
228
                        dndItems.append( CDragDropMgr::Item(text) );
 
229
                };
 
230
                elem = elem.nextSibling().toElement();
 
231
        };
 
232
 
 
233
        return dndItems;
 
234
};
 
235
 
 
236
/** 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. */
 
237
CDragDropMgr::Item::Type CDragDropMgr::dndType( const QMimeSource* e ) {
 
238
        ItemList dndItems = decode(e);
 
239
        if (dndItems.isEmpty()) {//wrong dropEvent or something strange
 
240
                return Item::Unknown;
 
241
        };
 
242
 
 
243
        //check whether all items have the ssame type, if they do return the type
 
244
        //as soon as two items have different types return Item::Unknown
 
245
        ItemList::Iterator it;
 
246
        Item::Type type = Item::Unknown;
 
247
        for( it = dndItems.begin(); it != dndItems.end(); ++it ) {
 
248
                if( type == Item::Unknown) { //if Unknown is set this is the first loop, don't return Unknown
 
249
                        type = (*it).type();
 
250
                }
 
251
                else if (type != (*it).type() ) {//items have different type, return Item::Unknown
 
252
                        return Item::Unknown;
 
253
                };
 
254
        };
 
255
        return type;
 
256
}