summaryrefslogtreecommitdiffstats
path: root/src/core/drophandler.cpp
blob: 0535e61ece6bf5c05ee8f509303cec3fd3473ab9 (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
/***************************************************************************
    copyright            : (C) 2007 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "drophandler.h"
#include "../mainwindow.h"
#include "../tellico_kernel.h"
#include "../tellico_debug.h"

#include <kurldrag.h>
#include <kmimetype.h>

using Tellico::DropHandler;

DropHandler::DropHandler(TQObject* parent_) : TQObject(parent_) {
}

DropHandler::~DropHandler() {
}

// assume the object is always the main window, that's the
// only object with this event filter
bool DropHandler::eventFilter(TQObject* obj_, TQEvent* ev_) {
  Q_UNUSED(obj_);
  if(ev_->type() == TQEvent::DragEnter) {
    return dragEnter(static_cast<TQDragEnterEvent*>(ev_));
  } else if(ev_->type() == TQEvent::Drop) {
    return drop(static_cast<TQDropEvent*>(ev_));
  }
  return false;
}

bool DropHandler::dragEnter(TQDragEnterEvent* event_) {
  bool accept = KURLDrag::canDecode(event_) || TQTextDrag::canDecode(event_);
  if(accept) {
    event_->accept(accept);
  }
  return accept;
}

bool DropHandler::drop(TQDropEvent* event_) {
  KURL::List urls;
  TQString text;

  if(KURLDrag::decode(event_, urls)) {
  } else if(TQTextDrag::decode(event_, text) && !text.isEmpty()) {
    urls << KURL(text);
  }
  return !urls.isEmpty() && handleURL(urls);
}

bool DropHandler::handleURL(const KURL::List& urls_) {
  bool hasUnknown = false;
  KURL::List tc, pdf, bib, ris;
  for(KURL::List::ConstIterator it = urls_.begin(); it != urls_.end(); ++it) {
    KMimeType::Ptr ptr = KMimeType::findByURL(*it);
    if(ptr->is(TQString::tqfromLatin1("application/x-tellico"))) {
      tc << *it;
    } else if(ptr->is(TQString::tqfromLatin1("application/pdf"))) {
      pdf << *it;
    } else if(ptr->is(TQString::tqfromLatin1("text/x-bibtex")) ||
              ptr->is(TQString::tqfromLatin1("application/x-bibtex"))) {
      bib << *it;
    } else if(ptr->is(TQString::tqfromLatin1("application/x-research-info-systems"))) {
      ris << *it;
    } else {
      myDebug() << "DropHandler::handleURL() - unrecognized type: " << ptr->name() << " (" << *it << ")" << endl;
      hasUnknown = true;
    }
  }
  MainWindow* mainWindow = ::tqqt_cast<MainWindow*>(Kernel::self()->widget());
  if(!mainWindow) {
    myDebug() << "DropHandler::handleURL() - no main window!" << endl;
    return !hasUnknown;
  }
  if(!tc.isEmpty()) {
    mainWindow->importFile(Import::TellicoXML, tc);
  }
  if(!pdf.isEmpty()) {
    mainWindow->importFile(Import::PDF, pdf);
  }
  if(!bib.isEmpty()) {
    mainWindow->importFile(Import::Bibtex, bib);
  }
  if(!ris.isEmpty()) {
    mainWindow->importFile(Import::RIS, ris);
  }
  // any unknown urls get passed
  return !hasUnknown;
}

#include "drophandler.moc"