summaryrefslogtreecommitdiffstats
path: root/src/cite/actionmanager.cpp
blob: 905f81af3a97646dbe355ab7054189ea1a22b23c (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
/***************************************************************************
    copyright            : (C) 2005-2006 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 "actionmanager.h"
#include "lyxpipe.h"
#include "clipboard.h"
#include "openoffice.h"
#include "../entry.h"
#include "../tellico_debug.h"

using Tellico::Cite::ActionManager;

ActionManager::ActionManager* ActionManager::self() {
  static ActionManager self;
  return &self;
}

ActionManager::ActionManager() : m_action(0) {
}

ActionManager::~ActionManager() {
  delete m_action;
}

bool ActionManager::connect(CiteAction action_) {
  if(m_action && m_action->type() == action_) {
    return m_action->connect();
  } else if(m_action) {
    delete m_action;
    m_action = 0;
  }

  switch(action_) {
    case Cite::CiteClipboard:
       m_action = new Clipboard();
       break;

    case Cite::CiteLyxpipe:
      m_action = new Lyxpipe();
      break;

    case Cite::CiteOpenOffice:
      m_action = new OpenOffice();
      break;
  }
  return m_action ? m_action->connect() : false;
}

bool ActionManager::cite(CiteAction action_, Data::EntryVec entries_) {
  if(entries_.isEmpty()) {
    myDebug() << "ActionManager::cite() - no entries to cite" << endl;
    return false;
  }
  if(m_action && m_action->type() != action_) {
    delete m_action;
    m_action = 0;
  }
  if(!m_action && !connect(action_)) {
    myDebug() << "ActionManager::cite() - unable to connect" << endl;
    return false;
  }
  if(!m_action) {
    myDebug() << "ActionManager::cite() - no action found" << endl;
    return false;
  }

  return m_action->cite(entries_);
}

bool ActionManager::isEnabled(CiteAction action_) {
  if(action_ == CiteOpenOffice) {
    return OpenOffice::hasLibrary();
  }
  return true;
}