void NoteDrag::serializeLinks(NoteSelection *noteList, K3MultipleDrag *multipleDrag, bool cutting) { KUrl::List urls; QStringList titles; KUrl url; QString title; for (NoteSelection *node = noteList->firstStacked(); node; node = node->nextStacked()) { node->note->content()->toLink(&url, &title, node->fullPath); if (!url.isEmpty()) { urls.append(url); titles.append(title); } } if (!urls.isEmpty()) { // First, the standard text/uri-list MIME format: #if KDE_IS_VERSION( 3, 3, 90 ) K3URLDrag *urlsDrag = new K3URLDrag(urls); // ONLY export as text/uri-list, and not as text/plain* as we wil do that better ourself urlsDrag->setExportAsText(false); multipleDrag->addDragObject(urlsDrag); #else K3URLDrag2 *urlsDrag = new K3URLDrag2(urls); QByteArray byteArray = urlsDrag->encodedData2("text/uri-list"); Q3StoredDrag *uriListDrag = new Q3StoredDrag("text/uri-list"); uriListDrag->setEncodedData(byteArray); multipleDrag->addDragObject(uriListDrag); delete urlsDrag; #endif // Then, also provide it in the Mozilla proprietary format (that also allow to add titles to URLs): // A version for Mozilla applications (convert to "theUrl\ntheTitle", into UTF-16): // FIXME: Does Mozilla support the drag of several URLs at once? // FIXME: If no, only provide that if theire is only ONE URL. QString xMozUrl; for (int i = 0; i < urls.count(); ++i) xMozUrl += (xMozUrl.isEmpty() ? "" : "\n") + urls[i].prettyUrl() + "\n" + titles[i]; /* Code for only one: =============== xMozUrl = note->title() + "\n" + note->url().prettyUrl();*/ QByteArray baMozUrl; QTextStream stream(baMozUrl, QIODevice::WriteOnly); stream.setEncoding(QTextStream::RawUnicode); // It's UTF16 (aka UCS2), but with the first two order bytes stream << xMozUrl; Q3StoredDrag *xMozUrlDrag = new Q3StoredDrag("text/x-moz-url"); xMozUrlDrag->setEncodedData(baMozUrl); multipleDrag->addDragObject(xMozUrlDrag); if (cutting) { QByteArray arrayCut(2); Q3StoredDrag *storedDragCut = new Q3StoredDrag("application/x-kde-cutselection"); arrayCut[0] = '1'; arrayCut[1] = 0; storedDragCut->setEncodedData(arrayCut); multipleDrag->addDragObject(storedDragCut); } } }
Q3DragObject* NoteDrag::dragObject(NoteSelection *noteList, bool cutting, QWidget *source) { if (noteList->count() <= 0) return 0; // The MimeSource: K3MultipleDrag *multipleDrag = new K3MultipleDrag(source); // Make sure the temporary folder exists and is empty (we delete previously moved file(s) (if exists) // since we override the content of the clipboard and previous file willn't be accessable anymore): createAndEmptyCuttingTmpFolder(); // The "Native Format" Serialization: QBuffer buffer; if (buffer.open(QIODevice::WriteOnly)) { QDataStream stream(&buffer); // First append a pointer to the basket: stream << (quint64)(noteList->firstStacked()->note->basket()); // Then a list of pointers to all notes, and parent groups: for (NoteSelection *node = noteList->firstStacked(); node; node = node->nextStacked()) stream << (quint64)(node->note); Q3ValueList<Note*> groups = noteList->parentGroups(); for (Q3ValueList<Note*>::iterator it = groups.begin(); it != groups.end(); ++it) stream << (quint64)(*it); stream << (quint64)0; // And finally the notes themselves: serializeNotes(noteList, stream, cutting); // Append the object: buffer.close(); Q3StoredDrag *dragObject = new Q3StoredDrag(NOTE_MIME_STRING, source); dragObject->setEncodedData(buffer.buffer()); multipleDrag->addDragObject(dragObject); } // The "Other Flavours" Serialization: serializeText( noteList, multipleDrag ); serializeHtml( noteList, multipleDrag ); serializeImage( noteList, multipleDrag ); serializeLinks( noteList, multipleDrag, cutting ); // The Alternate Flavours: if (noteList->count() == 1) noteList->firstStacked()->note->content()->addAlternateDragObjects(multipleDrag); // If it is a drag, and not a copy/cut, add the feedback pixmap: if (source) setFeedbackPixmap(noteList, multipleDrag); return multipleDrag; }
void NoteDrag::serializeHtml(NoteSelection *noteList, K3MultipleDrag *multipleDrag) { QString htmlEquivalent; QString html; for (NoteSelection *node = noteList->firstStacked(); node; node = node->nextStacked()) { html = node->note->content()->toHtml("", node->fullPath); if (!html.isEmpty()) htmlEquivalent += (!htmlEquivalent.isEmpty() ? "<br>\n" : "") + html; } if (!htmlEquivalent.isEmpty()) { // Add HTML flavour: Q3TextDrag *htmlDrag = new Q3TextDrag(htmlEquivalent); htmlDrag->setSubtype("html"); multipleDrag->addDragObject(htmlDrag); // But also QTextEdit flavour, to be able to paste several notes to a text edit: QByteArray byteArray = ("<!--StartFragment--><p>" + htmlEquivalent).local8Bit(); Q3StoredDrag *richTextDrag = new Q3StoredDrag("application/x-qrichtext"); richTextDrag->setEncodedData(byteArray); multipleDrag->addDragObject(richTextDrag); } }
QMimeSource* Q3MimeSourceFactory::dataInternal(const QString& abs_name, const QMap<QString, QString> &extensions) const { QMimeSource* r = 0; QStringList attempted_names(abs_name); QFileInfo fi(abs_name); if (fi.isReadable()) { // get the right mimetype QString e = fi.extension(false); QByteArray mimetype("application/octet-stream"); if (extensions.contains(e)) mimetype = extensions[e].latin1(); if (!QImageReader::imageFormat(abs_name).isEmpty()) mimetype = "application/x-qt-image"; QFile f(abs_name); if (f.open(QIODevice::ReadOnly) && f.size()) { QByteArray ba; ba.resize(f.size()); f.readBlock(ba.data(), ba.size()); Q3StoredDrag* sr = new Q3StoredDrag(mimetype); sr->setEncodedData(ba); delete d->last; d->last = r = sr; } } // we didn't find the mime-source, so ask the default factory for // the mime-source (this one will iterate over all installed ones) // // this looks dangerous, as this dataInternal() function will be // called again when the default factory loops over all installed // factories (including this), but the static bool looping in // data() avoids endless recursions if (!r && this != defaultFactory()) r = (QMimeSource*)defaultFactory()->data(abs_name); return r; }