summaryrefslogtreecommitdiffstats
path: root/src/fetcherconfigdialog.cpp
blob: f307420dee53a0e451bac5ec1e95703bd69b366b (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
/***************************************************************************
    copyright            : (C) 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 "fetcherconfigdialog.h"
#include "fetch/fetchmanager.h"
#include "gui/combobox.h"

#include <tdelocale.h>
#include <klineedit.h>
#include <kcombobox.h>
#include <kiconloader.h>

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqhgroupbox.h>
#include <tqwidgetstack.h>
#include <tqwhatsthis.h>
#include <tqhbox.h>
#include <tqvgroupbox.h>
#include <tqcheckbox.h>

namespace {
  static const int FETCHER_CONFIG_MIN_WIDTH = 600;
}

using Tellico::FetcherConfigDialog;

FetcherConfigDialog::FetcherConfigDialog(TQWidget* parent_)
    : KDialogBase(parent_, "fetcher dialog", true, i18n("Data Source Properties"),
               KDialogBase::Ok | KDialogBase::Cancel | KDialogBase::Help)
    , m_newSource(true)
    , m_useDefaultName(true)
    , m_configWidget(0) {
  init(Fetch::Unknown);
}

FetcherConfigDialog::FetcherConfigDialog(const TQString& sourceName_, Fetch::Type type_, bool updateOverwrite_,
                                         Fetch::ConfigWidget* configWidget_, TQWidget* parent_)
    : KDialogBase(parent_, "fetcher dialog", true, i18n("Data Source Properties"),
               KDialogBase::Ok | KDialogBase::Cancel | KDialogBase::Help)
    , m_newSource(false)
    , m_useDefaultName(false)
    , m_configWidget(configWidget_) {
  init(type_);
  m_nameEdit->setText(sourceName_);
  m_cbOverwrite->setChecked(updateOverwrite_);
}

void FetcherConfigDialog::init(Fetch::Type type_) {
  setMinimumWidth(FETCHER_CONFIG_MIN_WIDTH);
  setHelp(TQString::fromLatin1("data-sources-options"));

  TQWidget* widget = new TQWidget(this);
  TQBoxLayout* topLayout = new TQHBoxLayout(widget, KDialog::spacingHint());

  TQBoxLayout* vlay1 = new TQVBoxLayout(topLayout, KDialog::spacingHint());
  m_iconLabel = new TQLabel(widget);
  if(type_ == Fetch::Unknown) {
    m_iconLabel->setPixmap(TDEGlobal::iconLoader()->loadIcon(TQString::fromLatin1("network"), TDEIcon::Panel, 64));
  } else {
    m_iconLabel->setPixmap(Fetch::Manager::self()->fetcherIcon(type_, TDEIcon::Panel, 64));
  }
  vlay1->addWidget(m_iconLabel);
  vlay1->addStretch(1);

  TQBoxLayout* vlay2 = new TQVBoxLayout(topLayout, KDialog::spacingHint());

  TQGridLayout* gl = new TQGridLayout(vlay2, 2, 2, KDialog::spacingHint());
  int row = -1;

  TQLabel* label = new TQLabel(i18n("&Source name: "), widget);
  gl->addWidget(label, ++row, 0);
  TQString w = i18n("The name identifies the data source and should be unique and informative.");
  TQWhatsThis::add(label, w);

  m_nameEdit = new KLineEdit(widget);
  gl->addWidget(m_nameEdit, row, 1);
  m_nameEdit->setFocus();
  TQWhatsThis::add(m_nameEdit, w);
  label->setBuddy(m_nameEdit);
  connect(m_nameEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotNameChanged(const TQString&)));

  if(m_newSource) {
    label = new TQLabel(i18n("Source &type: "), widget);
  } else {
    // since the label doesn't have a buddy, we don't want an accel,
    // but also want to reuse string we already have
    label = new TQLabel(i18n("Source &type: ").remove('&'), widget);
  }
  gl->addWidget(label, ++row, 0);
  w = i18n("Tellico supports several different data sources.");
  TQWhatsThis::add(label, w);

  if(m_newSource) {
    m_typeCombo = new GUI::ComboBox(widget);
    gl->addWidget(m_typeCombo, row, 1);
    TQWhatsThis::add(m_typeCombo, w);
    label->setBuddy(m_typeCombo);
  } else {
    m_typeCombo = 0;
    TQLabel* lab = new TQLabel(Fetch::Manager::typeName(type_), widget);
    gl->addWidget(lab, row, 1);
    TQWhatsThis::add(lab, w);
  }
  m_cbOverwrite = new TQCheckBox(i18n("Updating from source should overwrite user data"), widget);
  ++row;
  gl->addMultiCellWidget(m_cbOverwrite, row, row, 0, 1);
  w = i18n("If checked, updating entries will overwrite any existing information.");
  TQWhatsThis::add(m_cbOverwrite, w);

  if(m_newSource) {
    m_stack = new TQWidgetStack(widget);
    vlay2->addWidget(m_stack);
    connect(m_typeCombo, TQ_SIGNAL(activated(int)), TQ_SLOT(slotNewSourceSelected(int)));

    int z3950_idx = 0;
    const Fetch::TypePairList typeList = Fetch::Manager::self()->typeList();
    for(Fetch::TypePairList::ConstIterator it = typeList.begin(); it != typeList.end(); ++it) {
      const Fetch::TypePair& type = *it;
      m_typeCombo->insertItem(type.index(), type.value());
      if(type.value() == Fetch::Z3950) {
        z3950_idx = m_typeCombo->count()-1;
      }
    }
    // make sure first widget gets initialized
    // I'd like it to be the z39.50 widget
    m_typeCombo->setCurrentItem(z3950_idx);
    slotNewSourceSelected(z3950_idx);
  } else {
    m_stack = 0;
    // just add config widget and reparent
    m_configWidget->reparent(widget, TQPoint());
    vlay2->addWidget(m_configWidget);
    connect(m_configWidget, TQ_SIGNAL(signalName(const TQString&)), TQ_SLOT(slotPossibleNewName(const TQString&)));
  }

  setMainWidget(widget);
}

TQString FetcherConfigDialog::sourceName() const {
  return m_nameEdit->text();
}

bool FetcherConfigDialog::updateOverwrite() const {
  return m_cbOverwrite->isChecked();
}

Tellico::Fetch::ConfigWidget* FetcherConfigDialog::configWidget() const {
  if(m_newSource) {
    return dynamic_cast<Fetch::ConfigWidget*>(m_stack->visibleWidget());
  }
  kdWarning() << "FetcherConfigDialog::configWidget() called for modifying existing fetcher!" << endl;
  return m_configWidget;
}

Tellico::Fetch::Type FetcherConfigDialog::sourceType() const {
  if(!m_newSource || m_typeCombo->count() == 0) {
    kdWarning() << "FetcherConfigDialog::sourceType() called for modifying existing fetcher!" << endl;
    return Fetch::Unknown;
  }
  return static_cast<Fetch::Type>(m_typeCombo->currentData().toInt());
}

void FetcherConfigDialog::slotNewSourceSelected(int idx_) {
  if(!m_newSource) {
    return;
  }

  // always change to default name
  m_useDefaultName = true;

  Fetch::ConfigWidget* cw = m_configWidgets[idx_];
  if(cw) {
    m_stack->raiseWidget(cw);
    slotPossibleNewName(cw->preferredName());
    return;
  }

  Fetch::Type type = sourceType();
  if(type == Fetch::Unknown) {
    kdWarning() << "FetcherConfigDialog::slotNewSourceSelected() - unknown source type" << endl;
    return;
  }
  m_iconLabel->setPixmap(Fetch::Manager::self()->fetcherIcon(type, TDEIcon::Panel, 64));
  cw = Fetch::Manager::self()->configWidget(m_stack, type, m_typeCombo->currentText());
  if(!cw) {
    // bad bad bad!
    kdWarning() << "FetcherConfigDialog::slotNewSourceSelected() - no config widget found for type " << type << endl;
    m_typeCombo->setCurrentItem(0);
    slotNewSourceSelected(0);
    return;
  }
  connect(cw, TQ_SIGNAL(signalName(const TQString&)), TQ_SLOT(slotPossibleNewName(const TQString&)));
  m_configWidgets.insert(idx_, cw);
  m_stack->addWidget(cw);
  m_stack->raiseWidget(cw);
  slotPossibleNewName(cw->preferredName());
}

void FetcherConfigDialog::slotNameChanged(const TQString&) {
  m_useDefaultName = false;
}

void FetcherConfigDialog::slotPossibleNewName(const TQString& name_) {
  if(name_.isEmpty()) {
    return;
  }
  Fetch::ConfigWidget* cw = m_stack ? static_cast<Fetch::ConfigWidget*>(m_stack->visibleWidget()) : m_configWidget;
  if(m_useDefaultName || (cw && m_nameEdit->text() == cw->preferredName())) {
    m_nameEdit->setText(name_);
    m_useDefaultName = true; // it gets reset in slotNameChanged()
  }
}

#include "fetcherconfigdialog.moc"