summaryrefslogtreecommitdiffstats
path: root/src/core/dcopinterface.cpp
blob: bd7014c84ac8381822037bd157173b4b7c6196a5 (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                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 TQString& actionName) {
  TQString name = actionName.lower();
  if(name == Latin1Literal("append")) {
    return Import::Append;
  } else if(name == Latin1Literal("merge")) {
    return Import::Merge;
  }
  return Import::Replace;
}

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

TQValueList<long> ApplicationInterface::filteredEntries() const {
  TQValueList<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;
}

TQStringList CollectionInterface::values(const TQString& fieldName_) const {
  TQStringList 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;
}

TQStringList CollectionInterface::values(long id_, const TQString& fieldName_) const {
  TQStringList 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;
}

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

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

bool CollectionInterface::setFieldValue(long id_, const TQString& fieldName_, const TQString& 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 TQString& fieldName_, const TQString& 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);
  TQStringList values = entry->fields(fieldName_, false);
  TQStringList newValues = values;
  newValues << value_;
  if(!entry->setField(fieldName_, newValues.join(TQString::tqfromLatin1("; ")))) {
    return false;
  }
  Kernel::self()->modifyEntries(oldEntry, entry);
  return true;
}