summaryrefslogtreecommitdiffstats
path: root/src/fetchdialog.cpp
blob: 77f04d54941fad294fc2b45338c3cc6e7c1a4f6c (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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/***************************************************************************
    copyright            : (C) 2003-2008 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 "fetchdialog.h"
#include "fetch/fetchmanager.h"
#include "fetch/fetcher.h"
#include "entryview.h"
#include "isbnvalidator.h"
#include "upcvalidator.h"
#include "tellico_kernel.h"
#include "filehandler.h"
#include "collection.h"
#include "entry.h"
#include "document.h"
#include "tellico_debug.h"
#include "gui/combobox.h"
#include "gui/listview.h"
#include "tellico_utils.h"
#include "stringset.h"

#include <klocale.h>
#include <klineedit.h>
#include <kpushbutton.h>
#include <kstatusbar.h>
#include <khtmlview.h>
#include <kprogress.h>
#include <kconfig.h>
#include <kdialogbase.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <kaccelmanager.h>
#include <ktextedit.h>

#include <layout.h>
#include <tqhbox.h>
#include <tqvgroupbox.h>
#include <tqsplitter.h>
#include <tqtimer.h>
#include <tqwhatsthis.h>
#include <tqcheckbox.h>
#include <tqvbox.h>
#include <tqtimer.h>
#include <tqimage.h>

#include <config.h>
#ifdef ENABLE_WEBCAM
#include "barcode/barcode.h"
#endif

namespace {
  static const int FETCH_STATUS_ID = 0;
  static const int FETCH_PROGRESS_ID = 0;
  static const int FETCH_MIN_WIDTH = 600;

  static const char* FETCH_STRING_SEARCH = I18N_NOOP("&Search");
  static const char* FETCH_STRING_STOP   = I18N_NOOP("&Stop");
}

using Tellico::FetchDialog;
using barcodeRecognition::barcodeRecognitionThread;

class FetchDialog::SearchResultItem : public Tellico::GUI::ListViewItem {
  friend class FetchDialog;
  // always add to end
  SearchResultItem(GUI::ListView* lv, Fetch::SearchResult* r)
      : GUI::ListViewItem(lv, lv->lastItem()), m_result(r) {
    setText(1, r->title);
    setText(2, r->desc);
    setPixmap(3, Fetch::Manager::self()->fetcherIcon(r->fetcher.data()));
    setText(3, r->fetcher->source());
  }
  Fetch::SearchResult* m_result;
};

FetchDialog::FetchDialog(TQWidget* parent_, const char* name_)
    : KDialogBase(parent_, name_, false, i18n("Internet Search"), 0),
      m_timer(new TQTimer(this)), m_started(false) {
  m_collType = Kernel::self()->collectionType();

  TQWidget* mainWidget = new TQWidget(this, "FetchDialog mainWidget");
  setMainWidget(mainWidget);
  TQVBoxLayout* topLayout = new TQVBoxLayout(mainWidget, 0, KDialog::spacingHint());

  TQVGroupBox* queryBox = new TQVGroupBox(i18n("Search Query"), mainWidget, "FetchDialog queryBox");
  topLayout->addWidget(queryBox);

  TQHBox* box1 = new TQHBox(queryBox, "FetchDialog box1");
  box1->setSpacing(KDialog::spacingHint());

  TQLabel* label = new TQLabel(i18n("Start the search", "S&earch:"), box1);

  m_valueLineEdit = new KLineEdit(box1);
  label->setBuddy(m_valueLineEdit);
  TQWhatsThis::add(m_valueLineEdit, i18n("Enter a search value. An ISBN search must include the full ISBN."));
  m_keyCombo = new GUI::ComboBox(box1);
  Fetch::KeyMap map = Fetch::Manager::self()->keyMap();
  for(Fetch::KeyMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
    m_keyCombo->insertItem(it.data(), it.key());
  }
  connect(m_keyCombo, TQT_SIGNAL(activated(int)), TQT_SLOT(slotKeyChanged(int)));
  TQWhatsThis::add(m_keyCombo, i18n("Choose the type of search"));

  m_searchButton = new KPushButton(box1);
  m_searchButton->setGuiItem(KGuiItem(i18n(FETCH_STRING_STOP),
                                      SmallIconSet(TQString::fromLatin1("cancel"))));
  connect(m_searchButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotSearchClicked()));
  TQWhatsThis::add(m_searchButton, i18n("Click to start or stop the search"));

  // the search button's text changes from search to stop
  // I don't want it resizing, so figure out the maximum size and set that
  m_searchButton->polish();
  int maxWidth = m_searchButton->sizeHint().width();
  int maxHeight = m_searchButton->sizeHint().height();
  m_searchButton->setGuiItem(KGuiItem(i18n(FETCH_STRING_SEARCH),
                                      SmallIconSet(TQString::fromLatin1("find"))));
  maxWidth = TQMAX(maxWidth, m_searchButton->sizeHint().width());
  maxHeight = TQMAX(maxHeight, m_searchButton->sizeHint().height());
  m_searchButton->setMinimumWidth(maxWidth);
  m_searchButton->setMinimumHeight(maxHeight);

  TQHBox* box2 = new TQHBox(queryBox);
  box2->setSpacing(KDialog::spacingHint());

  m_multipleISBN = new TQCheckBox(i18n("&Multiple ISBN/UPC search"), box2);
  TQWhatsThis::add(m_multipleISBN, i18n("Check this box to search for multiple ISBN or UPC values."));
  connect(m_multipleISBN, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotMultipleISBN(bool)));

  m_editISBN = new KPushButton(KGuiItem(i18n("Edit List..."), TQString::fromLatin1("text_block")), box2);
  m_editISBN->setEnabled(false);
  TQWhatsThis::add(m_editISBN, i18n("Click to open a text edit box for entering or editing multiple ISBN values."));
  connect(m_editISBN, TQT_SIGNAL(clicked()), TQT_SLOT(slotEditMultipleISBN()));

  // add for spacing
  box2->setStretchFactor(new TQWidget(box2), 10);

  label = new TQLabel(i18n("Search s&ource:"), box2);
  m_sourceCombo = new KComboBox(box2);
  label->setBuddy(m_sourceCombo);
  Fetch::FetcherVec sources = Fetch::Manager::self()->fetchers(m_collType);
  for(Fetch::FetcherVec::Iterator it = sources.begin(); it != sources.end(); ++it) {
    m_sourceCombo->insertItem(Fetch::Manager::self()->fetcherIcon(it.data()), (*it).source());
  }
  connect(m_sourceCombo, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(slotSourceChanged(const TQString&)));
  TQWhatsThis::add(m_sourceCombo, i18n("Select the database to search"));

  TQSplitter* split = new TQSplitter(Qt::Vertical, mainWidget);
  topLayout->addWidget(split);

  m_listView = new GUI::ListView(split);
//  topLayout->addWidget(m_listView);
//  topLayout->setStretchFactor(m_listView, 1);
  m_listView->setSorting(10); // greater than number of columns, so not sorting until user clicks column header
  m_listView->setShowSortIndicator(true);
  m_listView->setAllColumnsShowFocus(true);
  m_listView->setSelectionMode(TQListView::Extended);
  m_listView->addColumn(TQString(), 20); // will show a check mark when added
  m_listView->setColumnAlignment(0, TQt::AlignHCenter); // align checkmark in middle
//  m_listView->setColumnWidthMode(0, TQListView::Manual);
  m_listView->addColumn(i18n("Title"));
  m_listView->addColumn(i18n("Description"));
  m_listView->addColumn(i18n("Source"));
  m_listView->viewport()->installEventFilter(this);

  connect(m_listView, TQT_SIGNAL(selectionChanged()), TQT_SLOT(slotShowEntry()));
  // double clicking should add the entry
  connect(m_listView, TQT_SIGNAL(doubleClicked(TQListViewItem*)), TQT_SLOT(slotAddEntry()));
  TQWhatsThis::add(m_listView, i18n("As results are found, they are added to this list. Selecting one "
                                   "will fetch the complete entry and show it in the view below."));

  m_entryView = new EntryView(split, "entry_view");
  // don't bother creating funky gradient images for compact view
  m_entryView->setUseGradientImages(false);
  // set the xslt file AFTER setting the gradient image option
  m_entryView->setXSLTFile(TQString::fromLatin1("Compact.xsl"));
  TQWhatsThis::add(m_entryView->view(), i18n("An entry may be shown here before adding it to the "
                                            "current collection by selecting it in the list above"));

  TQHBox* box3 = new TQHBox(mainWidget);
  topLayout->addWidget(box3);
  box3->setSpacing(KDialog::spacingHint());

  m_addButton = new KPushButton(i18n("&Add Entry"), box3);
  m_addButton->setEnabled(false);
  m_addButton->setIconSet(UserIconSet(Kernel::self()->collectionTypeName()));
  connect(m_addButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotAddEntry()));
  TQWhatsThis::add(m_addButton, i18n("Add the selected entry to the current collection"));

  m_moreButton = new KPushButton(KGuiItem(i18n("Get More Results"), SmallIconSet(TQString::fromLatin1("find"))), box3);
  m_moreButton->setEnabled(false);
  connect(m_moreButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotMoreClicked()));
  TQWhatsThis::add(m_moreButton, i18n("Fetch more results from the current data source"));

  KPushButton* clearButton = new KPushButton(KStdGuiItem::clear(), box3);
  connect(clearButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotClearClicked()));
  TQWhatsThis::add(clearButton, i18n("Clear all search fields and results"));

  TQHBox* bottombox = new TQHBox(mainWidget, "box");
  topLayout->addWidget(bottombox);
  bottombox->setSpacing(KDialog::spacingHint());

  m_statusBar = new KStatusBar(bottombox, "statusbar");
  m_statusBar->insertItem(TQString(), FETCH_STATUS_ID, 1, false);
  m_statusBar->setItemAlignment(FETCH_STATUS_ID, AlignLeft | AlignVCenter);
  m_progress = new TQProgressBar(m_statusBar, "progress");
  m_progress->setTotalSteps(0);
  m_progress->setFixedHeight(fontMetrics().height()+2);
  m_progress->hide();
  m_statusBar->addWidget(m_progress, 0, true);

  KPushButton* closeButton = new KPushButton(KStdGuiItem::close(), bottombox);
  connect(closeButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotClose()));

  connect(m_timer, TQT_SIGNAL(timeout()), TQT_SLOT(slotMoveProgress()));

  setMinimumWidth(TQMAX(minimumWidth(), FETCH_MIN_WIDTH));
  setStatus(i18n("Ready."));

  resize(configDialogSize(TQString::fromLatin1("Fetch Dialog Options")));

  KConfigGroup config(kapp->config(), "Fetch Dialog Options");
  TQValueList<int> splitList = config.readIntListEntry("Splitter Sizes");
  if(!splitList.empty()) {
    split->setSizes(splitList);
  }

  connect(Fetch::Manager::self(), TQT_SIGNAL(signalResultFound(Tellico::Fetch::SearchResult*)),
                                  TQT_SLOT(slotResultFound(Tellico::Fetch::SearchResult*)));
  connect(Fetch::Manager::self(), TQT_SIGNAL(signalStatus(const TQString&)),
                                  TQT_SLOT(slotStatus(const TQString&)));
  connect(Fetch::Manager::self(), TQT_SIGNAL(signalDone()),
                                  TQT_SLOT(slotFetchDone()));

  // make sure to delete results afterwards
  m_results.setAutoDelete(true);

  KAcceleratorManager::manage(this);
  // initialize combos
  TQTimer::singleShot(0, this, TQT_SLOT(slotInit()));

#ifdef ENABLE_WEBCAM
  // barcode recognition
  m_barcodeRecognitionThread = new barcodeRecognitionThread();
  if (m_barcodeRecognitionThread->isWebcamAvailable()) {
    m_barcodePreview = new TQLabel(0);
    m_barcodePreview->move( TQApplication::desktop()->width() - 350, 30 );
    m_barcodePreview->setAutoResize( true );
    m_barcodePreview->show();
  } else {
    m_barcodePreview = 0;
  }
  connect( m_barcodeRecognitionThread, TQT_SIGNAL(recognized(TQString)), this, TQT_SLOT(slotBarcodeRecognized(TQString)) );
  connect( m_barcodeRecognitionThread, TQT_SIGNAL(gotImage(TQImage&)), this, TQT_SLOT(slotBarcodeGotImage(TQImage&)) );
  m_barcodeRecognitionThread->start();
/*  //DEBUG
  TQImage img( "/home/sebastian/white.png", "PNG" );
  m_barcodeRecognitionThread->recognizeBarcode( img );*/
#endif
}

FetchDialog::~FetchDialog() {
#ifdef ENABLE_WEBCAM
  m_barcodeRecognitionThread->stop();
  if (!m_barcodeRecognitionThread->wait( 1000 ))
    m_barcodeRecognitionThread->terminate();
  delete m_barcodeRecognitionThread;
  if (m_barcodePreview)
    delete m_barcodePreview;
#endif

  // we might have downloaded a lot of images we don't need to keep
  Data::EntryVec entriesToCheck;
  for(TQMap<int, Data::EntryPtr>::Iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
    entriesToCheck.append(it.data());
  }
  // no additional entries to check images to keep though
  Data::Document::self()->removeImagesNotInCollection(entriesToCheck, Data::EntryVec());

  saveDialogSize(TQString::fromLatin1("Fetch Dialog Options"));

  KConfigGroup config(kapp->config(), "Fetch Dialog Options");
  config.writeEntry("Splitter Sizes", static_cast<TQSplitter*>(m_listView->parentWidget())->sizes());
  config.writeEntry("Search Key", m_keyCombo->currentData().toInt());
  config.writeEntry("Search Source", m_sourceCombo->currentText());
}

void FetchDialog::slotSearchClicked() {
  m_valueLineEdit->selectAll();
  if(m_started) {
    setStatus(i18n("Cancelling the search..."));
    Fetch::Manager::self()->stop();
    slotFetchDone();
  } else {
    TQString value = m_valueLineEdit->text().simplifyWhiteSpace();
    if(value != m_oldSearch) {
      m_listView->clear();
      m_entryView->clear();
    }
    m_resultCount = 0;
    m_oldSearch = value;
    m_started = true;
    m_searchButton->setGuiItem(KGuiItem(i18n(FETCH_STRING_STOP),
                                        SmallIconSet(TQString::fromLatin1("cancel"))));
    startProgress();
    setStatus(i18n("Searching..."));
    kapp->processEvents();
    Fetch::Manager::self()->startSearch(m_sourceCombo->currentText(),
                                        static_cast<Fetch::FetchKey>(m_keyCombo->currentData().toInt()),
                                        value);
  }
}

void FetchDialog::slotClearClicked() {
  slotFetchDone(false);
  m_listView->clear();
  m_entryView->clear();
  Fetch::Manager::self()->stop();
  m_multipleISBN->setChecked(false);
  m_valueLineEdit->clear();
  m_valueLineEdit->setFocus();
  m_addButton->setEnabled(false);
  m_moreButton->setEnabled(false);
  m_isbnList.clear();
  m_statusMessages.clear();
  setStatus(i18n("Ready.")); // because slotFetchDone() writes text
}

void FetchDialog::slotStatus(const TQString& status_) {
  m_statusMessages.push_back(status_);
  // if the queue was empty, start the timer
  if(m_statusMessages.count() == 1) {
    // wait 2 seconds
    TQTimer::singleShot(2000, this, TQT_SLOT(slotUpdateStatus()));
  }
}

void FetchDialog::slotUpdateStatus() {
  if(m_statusMessages.isEmpty()) {
    return;
  }
  setStatus(m_statusMessages.front());
  m_statusMessages.pop_front();
  if(!m_statusMessages.isEmpty()) {
    // wait 2 seconds
    TQTimer::singleShot(2000, this, TQT_SLOT(slotUpdateStatus()));
  }
}

void FetchDialog::setStatus(const TQString& text_) {
  m_statusBar->changeItem(TQChar(' ') + text_, FETCH_STATUS_ID);
}

void FetchDialog::slotFetchDone(bool checkISBN /* = true */) {
//  myDebug() << "FetchDialog::slotFetchDone()" << endl;
  m_started = false;
  m_searchButton->setGuiItem(KGuiItem(i18n(FETCH_STRING_SEARCH),
                                      SmallIconSet(TQString::fromLatin1("find"))));
  stopProgress();
  if(m_resultCount == 0) {
    slotStatus(i18n("The search returned no items."));
  } else {
    /* TRANSLATORS: This is a plural form, you need to translate both lines (except "_n: ") */
    slotStatus(i18n("The search returned 1 item.",
                    "The search returned %n items.",
                    m_resultCount));
  }
  m_moreButton->setEnabled(Fetch::Manager::self()->hasMoreResults());

  // if we're not checking isbn values, then, ok to return
  if(!checkISBN) {
    return;
  }

  const Fetch::FetchKey key = static_cast<Fetch::FetchKey>(m_keyCombo->currentData().toInt());
  // no way to currently check EAN/UPC values for non-book items
  if(m_collType & (Data::Collection::Book | Data::Collection::Bibtex) &&
     m_multipleISBN->isChecked() &&
     (key == Fetch::ISBN || key == Fetch::UPC)) {
    TQStringList values = TQStringList::split(TQString::fromLatin1("; "),
                                            m_oldSearch.simplifyWhiteSpace());
    for(TQStringList::Iterator it = values.begin(); it != values.end(); ++it) {
      *it = ISBNValidator::cleanValue(*it);
    }
    for(TQListViewItemIterator it(m_listView); it.current(); ++it) {
      TQString i = ISBNValidator::cleanValue(static_cast<SearchResultItem*>(it.current())->m_result->isbn);
      values.remove(i);
      if(i.length() > 10 && i.startsWith(TQString::fromLatin1("978"))) {
        values.remove(ISBNValidator::cleanValue(ISBNValidator::isbn10(i)));
      }
    }
    if(!values.isEmpty()) {
      for(TQStringList::Iterator it = values.begin(); it != values.end(); ++it) {
        ISBNValidator::staticFixup(*it);
      }
      // TODO dialog caption
      KDialogBase* dlg = new KDialogBase(this, "isbn not found dialog", false, TQString(), KDialogBase::Ok);
      TQWidget* box = new TQWidget(dlg);
      TQVBoxLayout* lay = new TQVBoxLayout(box, KDialog::marginHint(), KDialog::spacingHint()*2);
      TQHBoxLayout* lay2 = new TQHBoxLayout(lay);
      TQLabel* lab = new TQLabel(box);
      lab->setPixmap(KGlobal::iconLoader()->loadIcon(TQString::fromLatin1("messagebox_info"),
                                                     KIcon::NoGroup,
                                                     KIcon::SizeMedium));
      lay2->addWidget(lab);
      TQString s = i18n("No results were found for the following ISBN values:");
      lay2->addWidget(new TQLabel(s, box), 10);
      KTextEdit* edit = new KTextEdit(box, "isbn list edit");
      lay->addWidget(edit);
      edit->setText(values.join(TQChar('\n')));
      TQWhatsThis::add(edit, s);
      connect(dlg, TQT_SIGNAL(okClicked()), dlg, TQT_SLOT(deleteLater()));
      dlg->setMainWidget(box);
      dlg->setMinimumWidth(TQMAX(dlg->minimumWidth(), FETCH_MIN_WIDTH*2/3));
      dlg->show();
    }
  }
}

void FetchDialog::slotResultFound(Tellico::Fetch::SearchResult* result_) {
  m_results.append(result_);
  (void) new SearchResultItem(m_listView, result_);
  ++m_resultCount;
  adjustColumnWidth();
  kapp->processEvents();
}

void FetchDialog::slotAddEntry() {
  GUI::CursorSaver cs;
  Data::EntryVec vec;
  for(TQListViewItemIterator it(m_listView, TQListViewItemIterator::Selected); it.current(); ++it) {
    SearchResultItem* item = static_cast<SearchResultItem*>(it.current());

    Fetch::SearchResult* r = item->m_result;
    Data::EntryPtr entry = m_entries[r->uid];
    if(!entry) {
      setStatus(i18n("Fetching %1...").arg(r->title));
      startProgress();
      entry = r->fetchEntry();
      if(!entry) {
        continue;
      }
      m_entries.insert(r->uid, entry);
      stopProgress();
      setStatus(i18n("Ready."));
    }
    // add a copy, intentionally allowing multiple copies to be added
    vec.append(new Data::Entry(*entry));
    item->setPixmap(0, UserIcon(TQString::fromLatin1("checkmark")));
  }
  if(!vec.isEmpty()) {
    Kernel::self()->addEntries(vec, true);
  }
}

void FetchDialog::slotMoreClicked() {
  if(m_started) {
    myDebug() << "FetchDialog::slotMoreClicked() - can't continue while running" << endl;
    return;
  }

  m_started = true;
  m_searchButton->setGuiItem(KGuiItem(i18n(FETCH_STRING_STOP),
                                      SmallIconSet(TQString::fromLatin1("cancel"))));
  startProgress();
  setStatus(i18n("Searching..."));
  kapp->processEvents();
  Fetch::Manager::self()->continueSearch();
}

void FetchDialog::slotShowEntry() {
  // just in case
  m_statusMessages.clear();

  const GUI::ListViewItemList& items = m_listView->selectedItems();
  if(items.isEmpty()) {
    m_addButton->setEnabled(false);
    return;
  }

  m_addButton->setEnabled(true);
  if(items.count() > 1) {
    m_entryView->clear();
    return;
  }

  SearchResultItem* item = static_cast<SearchResultItem*>(items.getFirst());
  Fetch::SearchResult* r = item->m_result;
  setStatus(i18n("Fetching %1...").arg(r->title));
  Data::EntryPtr entry = m_entries[r->uid];
  if(!entry) {
    GUI::CursorSaver cs;
    startProgress();
    entry = r->fetchEntry();
    if(entry) { // might conceivably be null
      m_entries.insert(r->uid, entry);
    }
    stopProgress();
  }
  setStatus(i18n("Ready."));

  m_entryView->showEntry(entry);
}

void FetchDialog::startProgress() {
  m_progress->show();
  m_timer->start(100);
}

void FetchDialog::slotMoveProgress() {
  m_progress->setProgress(m_progress->progress()+5);
}

void FetchDialog::stopProgress() {
  m_timer->stop();
  m_progress->hide();
}

void FetchDialog::slotInit() {
  if(!Fetch::Manager::self()->canFetch()) {
    m_searchButton->setEnabled(false);
    Kernel::self()->sorry(i18n("No Internet sources are available for your current collection type."), this);
  }

  KConfigGroup config(kapp->config(), "Fetch Dialog Options");
  int key = config.readNumEntry("Search Key", Fetch::FetchFirst);
  // only change key if valid
  if(key > Fetch::FetchFirst) {
    m_keyCombo->setCurrentData(key);
  }
  slotKeyChanged(m_keyCombo->currentItem());

  TQString source = config.readEntry("Search Source");
  if(!source.isEmpty()) {
    m_sourceCombo->setCurrentItem(source);
  }
  slotSourceChanged(m_sourceCombo->currentText());

  m_valueLineEdit->setFocus();
  m_searchButton->setDefault(true);
}

void FetchDialog::slotKeyChanged(int idx_) {
  int key = m_keyCombo->data(idx_).toInt();
  if(key == Fetch::ISBN || key == Fetch::UPC || key == Fetch::LCCN) {
    m_multipleISBN->setEnabled(true);
    if(key == Fetch::ISBN) {
      m_valueLineEdit->setValidator(new ISBNValidator(TQT_TQOBJECT(this)));
    } else {
      UPCValidator* upc = new UPCValidator(TQT_TQOBJECT(this));
      connect(upc, TQT_SIGNAL(signalISBN()), TQT_SLOT(slotUPC2ISBN()));
      m_valueLineEdit->setValidator(upc);
      // only want to convert to ISBN if ISBN is accepted by the fetcher
      Fetch::KeyMap map = Fetch::Manager::self()->keyMap(m_sourceCombo->currentText());
      upc->setCheckISBN(map.contains(Fetch::ISBN));
    }
  } else {
    m_multipleISBN->setChecked(false);
    m_multipleISBN->setEnabled(false);
//    slotMultipleISBN(false);
    m_valueLineEdit->setValidator(0);
  }
}

void FetchDialog::slotSourceChanged(const TQString& source_) {
  int curr = m_keyCombo->currentData().toInt();
  m_keyCombo->clear();
  Fetch::KeyMap map = Fetch::Manager::self()->keyMap(source_);
  for(Fetch::KeyMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
    m_keyCombo->insertItem(it.data(), it.key());
  }
  m_keyCombo->setCurrentData(curr);
  slotKeyChanged(m_keyCombo->currentItem());
}

void FetchDialog::slotMultipleISBN(bool toggle_) {
  bool wasEnabled = m_valueLineEdit->isEnabled();
  m_valueLineEdit->setEnabled(!toggle_);
  if(!wasEnabled && m_valueLineEdit->isEnabled()) {
    // if we enable it, it probably had multiple isbn values
    // the validator doesn't like that, so only keep the first value
    TQString val = m_valueLineEdit->text().section(';', 0, 0);
    m_valueLineEdit->setText(val);
  }
  m_editISBN->setEnabled(toggle_);
}

void FetchDialog::slotEditMultipleISBN() {
  KDialogBase dlg(this, "isbn edit dialog", true, i18n("Edit ISBN/UPC Values"),
                  KDialogBase::Ok|KDialogBase::Cancel);
  TQVBox* box = new TQVBox(&dlg);
  box->setSpacing(10);
  TQString s = i18n("<qt>Enter the ISBN or UPC values, one per line.</qt>");
  (void) new TQLabel(s, box);
  m_isbnTextEdit = new KTextEdit(box, "isbn text edit");
  m_isbnTextEdit->setText(m_isbnList.join(TQChar('\n')));
  TQWhatsThis::add(m_isbnTextEdit, s);
  KPushButton* fromFileBtn = new KPushButton(SmallIconSet(TQString::fromLatin1("fileopen")),
                                             i18n("&Load From File..."), box);
  TQWhatsThis::add(fromFileBtn, i18n("<qt>Load the list from a text file.</qt>"));
  connect(fromFileBtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotLoadISBNList()));
  dlg.setMainWidget(box);
  dlg.setMinimumWidth(TQMAX(dlg.minimumWidth(), FETCH_MIN_WIDTH*2/3));

  if(dlg.exec() == TQDialog::Accepted) {
    m_isbnList = TQStringList::split('\n', m_isbnTextEdit->text());
    const TQValidator* val = m_valueLineEdit->validator();
    if(val) {
      for(TQStringList::Iterator it = m_isbnList.begin(); it != m_isbnList.end(); ++it) {
        val->fixup(*it);
        if((*it).isEmpty()) {
          it = m_isbnList.remove(it);
          // this is next item, shift backward
          --it;
        }
      }
    }
    if(m_isbnList.count() > 100) {
      Kernel::self()->sorry(i18n("<qt>An ISBN search can contain a maximum of 100 ISBN values. Only the "
                                 "first 100 values in your list will be used.</qt>"), this);
    }
    while(m_isbnList.count() > 100) {
      m_isbnList.pop_back();
    }
    m_valueLineEdit->setText(m_isbnList.join(TQString::fromLatin1("; ")));
  }
  m_isbnTextEdit = 0; // gets auto-deleted
}

void FetchDialog::slotLoadISBNList() {
  if(!m_isbnTextEdit) {
    return;
  }
  KURL u = KFileDialog::getOpenURL(TQString(), TQString(), this);
  if(u.isValid()) {
    m_isbnTextEdit->setText(m_isbnTextEdit->text() + FileHandler::readTextFile(u));
    m_isbnTextEdit->moveCursor(TQTextEdit::MoveEnd, false);
    m_isbnTextEdit->scrollToBottom();
  }
}

void FetchDialog::slotUPC2ISBN() {
  int key = m_keyCombo->currentData().toInt();
  if(key == Fetch::UPC) {
    m_keyCombo->setCurrentData(Fetch::ISBN);
    slotKeyChanged(m_keyCombo->currentItem());
  }
}

bool FetchDialog::eventFilter(TQObject* obj_, TQEvent* ev_) {
  if(TQT_BASE_OBJECT(obj_) == TQT_BASE_OBJECT(m_listView->viewport()) && ev_->type() == TQEvent::Resize) {
    adjustColumnWidth();
  }
  return false;
}

void FetchDialog::adjustColumnWidth() {
  if(!m_listView || m_listView->childCount() == 0) {
    return;
  }

  int sum1 = 0;
  int sum2 = 0;
  int w3 = 0;
  for(TQListViewItemIterator it(m_listView); it.current(); ++it) {
    sum1 += it.current()->width(m_listView->fontMetrics(), m_listView, 1);
    sum2 += it.current()->width(m_listView->fontMetrics(), m_listView, 2);
    w3 = TQMAX(w3, it.current()->width(m_listView->fontMetrics(), m_listView, 3));
  }
  // try to be smart about column width
  // column 0 is fixed, the checkmark icon, give it 20
  const int w0 = 20;
  // column 3 is the source, say max is 25% of viewport
  const int vw = m_listView->visibleWidth();
  w3 = static_cast<int>(TQMIN(1.1 * w3, 0.25 * vw));
  // scale averages of col 1 and col 2
  const int avg1 = sum1 / m_listView->childCount();
  const int avg2 = sum2 / m_listView->childCount();
  const int w2 = (vw - w0 - w3) * avg2 / (avg1 + avg2);
  const int w1 = vw - w0 - w3 - w2;
  m_listView->setColumnWidth(1, w1);
  m_listView->setColumnWidth(2, w2);
  m_listView->setColumnWidth(3, w3);
}

void FetchDialog::slotResetCollection() {
  if(m_collType == Kernel::self()->collectionType()) {
    return;
  }
  m_collType = Kernel::self()->collectionType();
  m_sourceCombo->clear();
  Fetch::FetcherVec sources = Fetch::Manager::self()->fetchers(m_collType);
  for(Fetch::FetcherVec::Iterator it = sources.begin(); it != sources.end(); ++it) {
    m_sourceCombo->insertItem(Fetch::Manager::self()->fetcherIcon(it.data()), (*it).source());
  }

  m_addButton->setIconSet(UserIconSet(Kernel::self()->collectionTypeName()));

  if(Fetch::Manager::self()->canFetch()) {
    m_searchButton->setEnabled(true);
  } else {
    m_searchButton->setEnabled(false);
    Kernel::self()->sorry(i18n("No Internet sources are available for your current collection type."), this);
  }
}

void FetchDialog::slotBarcodeRecognized( TQString string )
{
  // attention: this slot is called in the context of another thread => do not use GUI-functions!
  TQCustomEvent *e = new TQCustomEvent( TQEvent::User );
  TQString *data = new TQString( string );
  e->setData( data );
  tqApp->postEvent( this, e ); // the event loop will call FetchDialog::customEvent() in the context of the GUI thread
}

void FetchDialog::slotBarcodeGotImage( TQImage &img )
{
  // attention: this slot is called in the context of another thread => do not use GUI-functions!
  TQCustomEvent *e = new TQCustomEvent( TQEvent::User+1 );
  TQImage *data = new TQImage( img.copy() );
  e->setData( data );
  tqApp->postEvent( this, e ); // the event loop will call FetchDialog::customEvent() in the context of the GUI thread
}

void FetchDialog::customEvent( TQCustomEvent *e )
{
  if (!e)
    return;
  if ((e->type() == TQEvent::User) && e->data()) {
    // slotBarcodeRecognized() queued call
    TQString temp = *(TQString*)(e->data());
    delete (TQString*)(e->data());
    tqApp->beep();
    m_valueLineEdit->setText( temp );
    m_searchButton->animateClick();
  }
  if ((e->type() == TQEvent::User+1) && e->data()) {
    // slotBarcodegotImage() queued call
    TQImage temp = *(TQImage*)(e->data());
    delete (TQImage*)(e->data());
    m_barcodePreview->setPixmap( temp );
  }
}

#include "fetchdialog.moc"