summaryrefslogtreecommitdiffstats
path: root/src/core/dcopinterface.cpp
blob: f375b6b6926d6a6147e6a071928ceecb9223d5d8 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/***************************************************************************
    copyright            : (C) 2004-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 "dcopinterface.h"
#include "../latin1literal.h"
#include "../controller.h"
#include "../tellico_kernel.h"
#include "../document.h"
#include "../collection.h"
#include "../translators/bibtexhandler.h"

using Tellico::ApplicationInterface;
using Tellico::CollectionInterface;

Tellico::Import::Action ApplicationInterface::actionType(const QString& actionName) {
  QString name = actionName.lower();
  if(name == Latin1Literal("append")) {
    return Import::Append;
  } else if(name == Latin1Literal("merge")) {
    return Import::Merge;
  }
  return Import::Replace;
}

QValueList<long> ApplicationInterface::selectedEntries() const {
  QValueList<long> ids;
  Data::EntryVec entries = Controller::self()->selectedEntries();
  for(Data::EntryVecIt entry = entries.begin(); entry != entries.end(); ++entry) {
    ids << entry->id();
  }
  return ids;
}

QValueList<long> ApplicationInterface::filteredEntries() const {
  QValueList<long> ids;
  Data::EntryVec entries = Controller::self()->visibleEntries();
  for(Data::EntryVecIt entry = entries.begin(); entry != entries.end(); ++entry) {
    ids << entry->id();
  }
  return ids;
}

long CollectionInterface::addEntry() {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return -1;
  }
  Data::EntryPtr entry = new Data::Entry(coll);
  Kernel::self()->addEntries(entry, false);
  return entry->id();
}

bool CollectionInterface::removeEntry(long id_) {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return false;
  }
  Data::EntryPtr entry = coll->entryById(id_);
  if(!entry) {
    return false;
  }
  Kernel::self()->removeEntries(entry);
  return coll->entryById(id_) == 0;
}

QStringList CollectionInterface::values(const QString& fieldName_) const {
  QStringList results;
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return results;
  }
  Data::FieldPtr field = coll->fieldByName(fieldName_);
  if(!field) {
    field = coll->fieldByTitle(fieldName_);
  }
  if(!field) {
    return results;
  }
  Data::EntryVec entries = Controller::self()->selectedEntries();
  for(Data::EntryVecIt entry = entries.begin(); entry != entries.end(); ++entry) {
    results += entry->fields(field, false);
  }
  return results;
}

QStringList CollectionInterface::values(long id_, const QString& fieldName_) const {
  QStringList results;
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return results;
  }
  Data::FieldPtr field = coll->fieldByName(fieldName_);
  if(!field) {
    field = coll->fieldByTitle(fieldName_);
  }
  if(!field) {
    return results;
  }
  Data::EntryPtr entry = coll->entryById(id_);
  if(entry) {
    results += entry->fields(field, false);
  }
  return results;
}

QStringList CollectionInterface::bibtexKeys() const {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll || coll->type() != Data::Collection::Bibtex) {
    return QStringList();
  }
  return BibtexHandler::bibtexKeys(Controller::self()->selectedEntries());
}

QString CollectionInterface::bibtexKey(long id_) const {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll || coll->type() != Data::Collection::Bibtex) {
    return QString();
  }
  Data::EntryPtr entry = coll->entryById(id_);
  if(!entry) {
    return QString();
  }
  return BibtexHandler::bibtexKeys(entry).first();
}

bool CollectionInterface::setFieldValue(long id_, const QString& fieldName_, const QString& value_) {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return false;
  }
  Data::EntryPtr entry = coll->entryById(id_);
  if(!entry) {
    return false;
  }
  Data::EntryPtr oldEntry = new Data::Entry(*entry);
  if(!entry->setField(fieldName_, value_)) {
    return false;
  }
  Kernel::self()->modifyEntries(oldEntry, entry);
  return true;
}

bool CollectionInterface::addFieldValue(long id_, const QString& fieldName_, const QString& value_) {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return false;
  }
  Data::EntryPtr entry = coll->entryById(id_);
  if(!entry) {
    return false;
  }
  Data::EntryPtr oldEntry = new Data::Entry(*entry);
  QStringList values = entry->fields(fieldName_, false);
  QStringList newValues = values;
  newValues << value_;
  if(!entry->setField(fieldName_, newValues.join(QString::fromLatin1("; ")))) {
    return false;
  }
  Kernel::self()->modifyEntries(oldEntry, entry);
  return true;
}