~paulbrianstewart/ubuntu/oneiric/tellico/852247-Formatting-Fix

« back to all changes in this revision

Viewing changes to src/core/drophandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-31 19:33:05 UTC
  • mfrom: (0.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080131193305-9l01m5gfhykl6pkl
Tags: 1.3-1ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: build-dep on kdepim-dev
  - debian/control: drop versioned python from tellico-data suggests
  - debian/rules: call dh_icons

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    copyright            : (C) 2007 by Robby Stephenson
 
3
    email                : robby@periapsis.org
 
4
 ***************************************************************************/
 
5
 
 
6
/***************************************************************************
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or modify  *
 
9
 *   it under the terms of version 2 of the GNU General Public License as  *
 
10
 *   published by the Free Software Foundation;                            *
 
11
 *                                                                         *
 
12
 ***************************************************************************/
 
13
 
 
14
#include "drophandler.h"
 
15
#include "../mainwindow.h"
 
16
#include "../tellico_kernel.h"
 
17
#include "../tellico_debug.h"
 
18
 
 
19
#include <kurldrag.h>
 
20
#include <kmimetype.h>
 
21
 
 
22
using Tellico::DropHandler;
 
23
 
 
24
DropHandler::DropHandler(QObject* parent_) : QObject(parent_) {
 
25
}
 
26
 
 
27
DropHandler::~DropHandler() {
 
28
}
 
29
 
 
30
// assume the object is always the main window, that's the
 
31
// only object with this event filter
 
32
bool DropHandler::eventFilter(QObject* obj_, QEvent* ev_) {
 
33
  Q_UNUSED(obj_);
 
34
  if(ev_->type() == QEvent::DragEnter) {
 
35
    return dragEnter(static_cast<QDragEnterEvent*>(ev_));
 
36
  } else if(ev_->type() == QEvent::Drop) {
 
37
    return drop(static_cast<QDropEvent*>(ev_));
 
38
  }
 
39
  return false;
 
40
}
 
41
 
 
42
bool DropHandler::dragEnter(QDragEnterEvent* event_) {
 
43
  bool accept = KURLDrag::canDecode(event_) || QTextDrag::canDecode(event_);
 
44
  if(accept) {
 
45
    event_->accept(accept);
 
46
  }
 
47
  return accept;
 
48
}
 
49
 
 
50
bool DropHandler::drop(QDropEvent* event_) {
 
51
  KURL::List urls;
 
52
  QString text;
 
53
 
 
54
  if(KURLDrag::decode(event_, urls)) {
 
55
  } else if(QTextDrag::decode(event_, text) && !text.isEmpty()) {
 
56
    urls << KURL(text);
 
57
  }
 
58
  return !urls.isEmpty() && handleURL(urls);
 
59
}
 
60
 
 
61
bool DropHandler::handleURL(const KURL::List& urls_) {
 
62
  bool hasUnknown = false;
 
63
  KURL::List tc, pdf, bib, ris;
 
64
  for(KURL::List::ConstIterator it = urls_.begin(); it != urls_.end(); ++it) {
 
65
    KMimeType::Ptr ptr = KMimeType::findByURL(*it);
 
66
    if(ptr->is(QString::fromLatin1("application/x-tellico"))) {
 
67
      tc << *it;
 
68
    } else if(ptr->is(QString::fromLatin1("application/pdf"))) {
 
69
      pdf << *it;
 
70
    } else if(ptr->is(QString::fromLatin1("text/x-bibtex")) ||
 
71
              ptr->is(QString::fromLatin1("application/x-bibtex"))) {
 
72
      bib << *it;
 
73
    } else if(ptr->is(QString::fromLatin1("application/x-research-info-systems"))) {
 
74
      ris << *it;
 
75
    } else {
 
76
      myDebug() << "DropHandler::handleURL() - unrecognized type: " << ptr->name() << " (" << *it << ")" << endl;
 
77
      hasUnknown = true;
 
78
    }
 
79
  }
 
80
  MainWindow* mainWindow = ::qt_cast<MainWindow*>(Kernel::self()->widget());
 
81
  if(!mainWindow) {
 
82
    myDebug() << "DropHandler::handleURL() - no main window!" << endl;
 
83
    return !hasUnknown;
 
84
  }
 
85
  if(!tc.isEmpty()) {
 
86
    mainWindow->importFile(Import::TellicoXML, tc);
 
87
  }
 
88
  if(!pdf.isEmpty()) {
 
89
    mainWindow->importFile(Import::PDF, pdf);
 
90
  }
 
91
  if(!bib.isEmpty()) {
 
92
    mainWindow->importFile(Import::Bibtex, bib);
 
93
  }
 
94
  if(!ris.isEmpty()) {
 
95
    mainWindow->importFile(Import::RIS, ris);
 
96
  }
 
97
  // any unknown urls get passed
 
98
  return !hasUnknown;
 
99
}
 
100
 
 
101
#include "drophandler.moc"