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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/***************************************************************************
copyright : (C) 2003-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 "tellico_kernel.h"
#include "mainwindow.h"
#include "document.h"
#include "collection.h"
#include "entryitem.h"
#include "controller.h"
#include "filter.h"
#include "filterdialog.h"
#include "loandialog.h"
#include "calendarhandler.h"
#include "tellico_utils.h"
#include "tellico_debug.h"
#include "commands/group.h"
#include "commands/collectioncommand.h"
#include "commands/fieldcommand.h"
#include "commands/filtercommand.h"
#include "commands/addentries.h"
#include "commands/modifyentries.h"
#include "commands/updateentries.h"
#include "commands/removeentries.h"
#include "commands/removeloans.h"
#include "commands/reorderfields.h"
#include "commands/renamecollection.h"
#include "collectionfactory.h"
#include "stringset.h"
#include <kmessagebox.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <klocale.h>
using Tellico::Kernel;
Kernel* Kernel::s_self = 0;
Kernel::Kernel(MainWindow* parent) : m_widget(parent)
, m_commandHistory(parent->actionCollection())
, m_commandGroup(0) {
}
const KURL& Kernel::URL() const {
return Data::Document::self()->URL();
}
const TQStringList& Kernel::fieldTitles() const {
return Data::Document::self()->collection()->fieldTitles();
}
TQString Kernel::fieldNameByTitle(const TQString& title_) const {
return Data::Document::self()->collection()->fieldNameByTitle(title_);
}
TQString Kernel::fieldTitleByName(const TQString& name_) const {
return Data::Document::self()->collection()->fieldTitleByName(name_);
}
TQStringList Kernel::valuesByFieldName(const TQString& name_) const {
return Data::Document::self()->collection()->valuesByFieldName(name_);
}
int Kernel::collectionType() const {
return Data::Document::self()->collection()->type();
}
TQString Kernel::collectionTypeName() const {
return Data::Document::self()->collection()->typeName();
}
void Kernel::sorry(const TQString& text_, TQWidget* widget_/* =0 */) {
if(text_.isEmpty()) {
return;
}
GUI::CursorSaver cs(TQt::arrowCursor);
KMessageBox::sorry(widget_ ? widget_ : m_widget, text_);
}
void Kernel::beginCommandGroup(const TQString& name_) {
if(m_commandGroup) {
myDebug() << "Kernel::beginCommandGroup() - there's an uncommitted group!" << endl;
delete m_commandGroup;
}
m_commandGroup = new Command::Group(name_);
}
void Kernel::endCommandGroup() {
if(!m_commandGroup) {
myDebug() << "Kernel::endCommandGroup() - beginCommandGroup() must be called first!" << endl;
return;
}
if(m_commandGroup->isEmpty()) {
delete m_commandGroup;
} else {
m_commandHistory.addCommand(m_commandGroup);
Data::Document::self()->slotSetModified(true);
}
m_commandGroup = 0;
}
void Kernel::resetHistory() {
m_commandHistory.clear();
m_commandHistory.documentSaved();
}
bool Kernel::addField(Data::FieldPtr field_) {
if(!field_) {
return false;
}
doCommand(new Command::FieldCommand(Command::FieldCommand::FieldAdd,
Data::Document::self()->collection(),
field_));
return true;
}
bool Kernel::modifyField(Data::FieldPtr field_) {
if(!field_) {
return false;
}
Data::FieldPtr oldField = Data::Document::self()->collection()->fieldByName(field_->name());
if(!oldField) {
return false;
}
doCommand(new Command::FieldCommand(Command::FieldCommand::FieldModify,
Data::Document::self()->collection(),
field_,
oldField));
return true;
}
bool Kernel::removeField(Data::FieldPtr field_) {
if(!field_) {
return false;
}
doCommand(new Command::FieldCommand(Command::FieldCommand::FieldRemove,
Data::Document::self()->collection(),
field_));
return true;
}
void Kernel::addEntries(Data::EntryVec entries_, bool checkFields_) {
if(entries_.isEmpty()) {
return;
}
KCommand* cmd = new Command::AddEntries(Data::Document::self()->collection(), entries_);
if(checkFields_) {
beginCommandGroup(cmd->name());
// this is the same as in Command::UpdateEntries::execute()
Data::CollPtr c = Data::Document::self()->collection();
Data::FieldVec fields = entries_[0]->collection()->fields();
TQPair<Data::FieldVec, Data::FieldVec> p = mergeFields(c, fields, entries_);
Data::FieldVec modifiedFields = p.first;
Data::FieldVec addedFields = p.second;
for(Data::FieldVec::Iterator field = modifiedFields.begin(); field != modifiedFields.end(); ++field) {
if(c->hasField(field->name())) {
doCommand(new Command::FieldCommand(Command::FieldCommand::FieldModify, c,
field, c->fieldByName(field->name())));
}
}
for(Data::FieldVec::Iterator field = addedFields.begin(); field != addedFields.end(); ++field) {
doCommand(new Command::FieldCommand(Command::FieldCommand::FieldAdd, c, field));
}
}
doCommand(cmd);
if(checkFields_) {
endCommandGroup();
}
}
void Kernel::modifyEntries(Data::EntryVec oldEntries_, Data::EntryVec newEntries_) {
if(newEntries_.isEmpty()) {
return;
}
doCommand(new Command::ModifyEntries(Data::Document::self()->collection(), oldEntries_, newEntries_));
}
void Kernel::updateEntry(Data::EntryPtr oldEntry_, Data::EntryPtr newEntry_, bool overWrite_) {
if(!newEntry_) {
return;
}
doCommand(new Command::UpdateEntries(Data::Document::self()->collection(), oldEntry_, newEntry_, overWrite_));
}
void Kernel::removeEntries(Data::EntryVec entries_) {
if(entries_.isEmpty()) {
return;
}
doCommand(new Command::RemoveEntries(Data::Document::self()->collection(), entries_));
}
bool Kernel::addLoans(Data::EntryVec entries_) {
if(entries_.isEmpty()) {
return false;
}
LoanDialog dlg(entries_, m_widget);
if(dlg.exec() != TQDialog::Accepted) {
return false;
}
KCommand* cmd = dlg.createCommand();
if(!cmd) {
return false;
}
doCommand(cmd);
return true;
}
bool Kernel::modifyLoan(Data::LoanPtr loan_) {
if(!loan_) {
return false;
}
LoanDialog dlg(loan_, m_widget);
if(dlg.exec() != TQDialog::Accepted) {
return false;
}
KCommand* cmd = dlg.createCommand();
if(!cmd) {
return false;
}
doCommand(cmd);
return true;
}
bool Kernel::removeLoans(Data::LoanVec loans_) {
if(loans_.isEmpty()) {
return true;
}
doCommand(new Command::RemoveLoans(loans_));
return true;
}
void Kernel::addFilter(FilterPtr filter_) {
if(!filter_) {
return;
}
doCommand(new Command::FilterCommand(Command::FilterCommand::FilterAdd, filter_));
}
bool Kernel::modifyFilter(FilterPtr filter_) {
if(!filter_) {
return false;
}
FilterDialog filterDlg(FilterDialog::ModifyFilter, m_widget);
// need to create a new filter object
FilterPtr newFilter = new Filter(*filter_);
filterDlg.setFilter(newFilter);
if(filterDlg.exec() != TQDialog::Accepted) {
return false;
}
doCommand(new Command::FilterCommand(Command::FilterCommand::FilterModify, newFilter, filter_));
return true;
}
bool Kernel::removeFilter(FilterPtr filter_) {
if(!filter_) {
return false;
}
TQString str = i18n("Do you really want to delete this filter?");
TQString dontAsk = TQString::tqfromLatin1("DeleteFilter");
int ret = KMessageBox::questionYesNo(m_widget, str, i18n("Delete Filter?"),
KStdGuiItem::yes(), KStdGuiItem::no(), dontAsk);
if(ret != KMessageBox::Yes) {
return false;
}
doCommand(new Command::FilterCommand(Command::FilterCommand::FilterRemove, filter_));
return true;
}
void Kernel::reorderFields(const Data::FieldVec& fields_) {
doCommand(new Command::ReorderFields(Data::Document::self()->collection(),
Data::Document::self()->collection()->fields(),
fields_));
}
void Kernel::appendCollection(Data::CollPtr coll_) {
doCommand(new Command::CollectionCommand(Command::CollectionCommand::Append,
Data::Document::self()->collection(),
coll_));
}
void Kernel::mergeCollection(Data::CollPtr coll_) {
doCommand(new Command::CollectionCommand(Command::CollectionCommand::Merge,
Data::Document::self()->collection(),
coll_));
}
void Kernel::replaceCollection(Data::CollPtr coll_) {
doCommand(new Command::CollectionCommand(Command::CollectionCommand::Replace,
Data::Document::self()->collection(),
coll_));
}
void Kernel::renameCollection() {
bool ok;
TQString newTitle = KInputDialog::getText(i18n("Rename Collection"), i18n("New collection name:"),
Data::Document::self()->collection()->title(), &ok, m_widget);
if(ok) {
doCommand(new Command::RenameCollection(Data::Document::self()->collection(), newTitle));
}
}
void Kernel::doCommand(KCommand* command_) {
if(m_commandGroup) {
m_commandGroup->addCommand(command_);
} else {
m_commandHistory.addCommand(command_);
Data::Document::self()->slotSetModified(true);
}
}
TQPair<Tellico::Data::FieldVec, Tellico::Data::FieldVec> Kernel::mergeFields(Data::CollPtr coll_,
Data::FieldVec fields_,
Data::EntryVec entries_) {
Data::FieldVec modified, created;
for(Data::FieldVec::Iterator field = fields_.begin(); field != fields_.end(); ++field) {
// don't add a field if it's a default field and not in the current collection
if(coll_->hasField(field->name()) || CollectionFactory::isDefaultField(coll_->type(), field->name())) {
// special case for choice fields, since we might want to add a value
if(field->type() == Data::Field::Choice && coll_->hasField(field->name())) {
TQStringList a1 = field->allowed();
TQStringList a2 = coll_->fieldByName(field->name())->allowed();
if(a1 != a2) {
StringSet a;
a.add(a1);
a.add(a2);
Data::FieldPtr f = new Data::Field(*coll_->fieldByName(field->name()));
f->setAllowed(a.toList());
modified.append(f);
}
}
continue;
}
// add field if any values are not empty
for(Data::EntryVec::Iterator entry = entries_.begin(); entry != entries_.end(); ++entry) {
if(!entry->field(field).isEmpty()) {
created.append(new Data::Field(*field));
break;
}
}
}
return tqMakePair(modified, created);
}
int Kernel::askAndMerge(Data::EntryPtr entry1_, Data::EntryPtr entry2_, Data::FieldPtr field_,
TQString value1_, TQString value2_) {
TQString title1 = entry1_->field(TQString::tqfromLatin1("title"));
TQString title2 = entry2_->field(TQString::tqfromLatin1("title"));
if(title1 == title2) {
title1 = i18n("Entry 1");
title2 = i18n("Entry 2");
}
if(value1_.isEmpty()) {
value1_ = entry1_->field(field_);
}
if(value2_.isEmpty()) {
value2_ = entry2_->field(field_);
}
TQString text = TQString::tqfromLatin1("<qt>")
+ i18n("Conflicting values for %1 were found while merging entries.").tqarg(field_->title())
+ TQString::tqfromLatin1("<br/><center><table><tr>"
"<th>%1</th>"
"<th>%2</th></tr>").tqarg(title1, title2)
+ TQString::tqfromLatin1("<tr><td><em>%1</em></td>").tqarg(value1_)
+ TQString::tqfromLatin1("<td><em>%1</em></td></tr></table></center>").tqarg(value2_)
+ i18n("Please choose which value to keep.")
+ TQString::tqfromLatin1("</qt>");
int ret = KMessageBox::warningYesNoCancel(Kernel::self()->widget(),
text,
i18n("Merge Entries"),
i18n("Select value from %1").tqarg(title1),
i18n("Select value from %1").tqarg(title2));
switch(ret) {
case KMessageBox::Cancel: return 0;
case KMessageBox::Yes: return -1; // keep original value
case KMessageBox::No: return 1; // use newer value
}
return 0;
}
|