File indexing completed on 2024-05-12 05:09:47
0001 /*************************************************************************** 0002 Copyright (C) 2007-2012 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "drophandler.h" 0026 #include "../mainwindow.h" 0027 #include "../utils/guiproxy.h" 0028 #include "../translators/bibteximporter.h" 0029 #include "../translators/risimporter.h" 0030 #include "../translators/ciwimporter.h" 0031 #include "../tellico_debug.h" 0032 0033 #include <KIO/Job> 0034 #include <KJobWidgets> 0035 0036 #include <QEvent> 0037 #include <QDragEnterEvent> 0038 #include <QDropEvent> 0039 #include <QMimeData> 0040 #include <QMimeDatabase> 0041 #include <QMimeType> 0042 0043 using Tellico::DropHandler; 0044 0045 DropHandler::DropHandler(QObject* parent_) : QObject(parent_) { 0046 } 0047 0048 DropHandler::~DropHandler() { 0049 } 0050 0051 // assume the object is always the main window, that's the 0052 // only object with this event filter 0053 bool DropHandler::eventFilter(QObject* obj_, QEvent* ev_) { 0054 Q_UNUSED(obj_); 0055 if(ev_->type() == QEvent::DragEnter) { 0056 return dragEnter(static_cast<QDragEnterEvent*>(ev_)); 0057 } else if(ev_->type() == QEvent::Drop) { 0058 return drop(static_cast<QDropEvent*>(ev_)); 0059 } 0060 return false; 0061 } 0062 0063 bool DropHandler::dragEnter(QDragEnterEvent* event_) { 0064 bool accept = event_->mimeData()->hasUrls() || event_->mimeData()->hasText(); 0065 if(accept) { 0066 event_->acceptProposedAction(); 0067 } 0068 return accept; 0069 } 0070 0071 bool DropHandler::drop(QDropEvent* event_) { 0072 QList<QUrl> urls = event_->mimeData()->urls(); 0073 0074 if(urls.isEmpty() && event_->mimeData()->hasText()) { 0075 QUrl u(event_->mimeData()->text()); 0076 if(!u.isRelative() && (u.isLocalFile() || !u.host().isEmpty())) { 0077 urls << u; 0078 } else { 0079 return handleText(event_->mimeData()->text()); 0080 } 0081 } 0082 return !urls.isEmpty() && handleURL(urls); 0083 } 0084 0085 bool DropHandler::handleURL(const QList<QUrl>& urls_) { 0086 bool hasUnknown = false; 0087 QMimeDatabase db; 0088 QList<QUrl> tc, pdf, bib, ris, ciw, ebook; 0089 foreach(const QUrl& url, urls_) { 0090 QMimeType ptr; 0091 // findByURL doesn't work for http, so actually query 0092 // the url itself 0093 if(url.scheme() != QLatin1String("http")) { 0094 ptr = db.mimeTypeForUrl(url); 0095 } else { 0096 KIO::MimetypeJob* job = KIO::mimetype(url, KIO::HideProgressInfo); 0097 KJobWidgets::setWindow(job, GUI::Proxy::widget()); 0098 job->exec(); 0099 ptr = db.mimeTypeForName(job->mimetype()); 0100 } 0101 if(ptr.inherits(QStringLiteral("application/x-tellico"))) { 0102 tc << url; 0103 } else if(ptr.inherits(QStringLiteral("application/pdf"))) { 0104 pdf << url; 0105 } else if(ptr.inherits(QStringLiteral("text/x-bibtex")) || 0106 ptr.inherits(QStringLiteral("application/x-bibtex")) || 0107 ptr.inherits(QStringLiteral("application/bibtex"))) { 0108 bib << url; 0109 } else if(ptr.inherits(QStringLiteral("application/x-research-info-systems"))) { 0110 ris << url; 0111 } else if(ptr.inherits(QStringLiteral("application/epub+zip")) || 0112 ptr.inherits(QStringLiteral("application/x-mobipocket-ebook")) || 0113 ptr.inherits(QStringLiteral("application/x-fictionbook+xml")) || 0114 ptr.inherits(QStringLiteral("application/x-zip-compressed-fb2"))) { 0115 ebook << url; 0116 } else if(url.fileName().endsWith(QLatin1String(".bib"))) { 0117 bib << url; 0118 } else if(url.fileName().endsWith(QLatin1String(".ris"))) { 0119 ris << url; 0120 } else if(url.fileName().endsWith(QLatin1String(".ciw"))) { 0121 ciw << url; 0122 } else if(ptr.inherits(QStringLiteral("text/plain")) && Import::BibtexImporter::maybeBibtex(url)) { 0123 bib << url; 0124 } else if(ptr.inherits(QStringLiteral("text/plain")) && Import::RISImporter::maybeRIS(url)) { 0125 ris << url; 0126 } else if(ptr.inherits(QStringLiteral("text/plain")) && Import::CIWImporter::maybeCIW(url)) { 0127 ciw << url; 0128 } else { 0129 myDebug() << "unrecognized type: " << ptr.name() << " (" << url << ")"; 0130 hasUnknown = true; 0131 } 0132 } 0133 MainWindow* mainWindow = ::qobject_cast<MainWindow*>(GUI::Proxy::widget()); 0134 if(!mainWindow) { 0135 myDebug() << "no main window!"; 0136 return !hasUnknown; 0137 } 0138 if(!tc.isEmpty()) { 0139 mainWindow->importFile(Import::TellicoXML, tc); 0140 } 0141 if(!pdf.isEmpty()) { 0142 mainWindow->importFile(Import::PDF, pdf); 0143 } 0144 if(!bib.isEmpty()) { 0145 mainWindow->importFile(Import::Bibtex, bib); 0146 } 0147 if(!ris.isEmpty()) { 0148 mainWindow->importFile(Import::RIS, ris); 0149 } 0150 if(!ciw.isEmpty()) { 0151 mainWindow->importFile(Import::CIW, ciw); 0152 } 0153 if(!ebook.isEmpty()) { 0154 mainWindow->importFile(Import::EBook, ebook); 0155 } 0156 // any unknown urls get passed 0157 return !hasUnknown; 0158 } 0159 0160 bool DropHandler::handleText(const QString& text_) { 0161 MainWindow* mainWindow = ::qobject_cast<MainWindow*>(GUI::Proxy::widget()); 0162 if(!mainWindow) { 0163 myDebug() << "no main window!"; 0164 return false; 0165 } 0166 0167 //TODO: handle TellicoXML, RIS, and CIW too 0168 if(Import::BibtexImporter::maybeBibtex(text_)) { 0169 mainWindow->importText(Import::Bibtex, text_); 0170 } 0171 return true; 0172 }