Added Das Telefonbuch support.
[jenirok] / src / gui / searchdialog.cpp
index 17fb292..5ee806c 100644 (file)
  *
  */
 
+#include <QtCore/QDebug>
 #include <QtGui/QHBoxLayout>
 #include <QtGui/QVBoxLayout>
 #include <QtGui/QLabel>
 #include <QtGui/QDialogButtonBox>
 #include <QMaemo5ValueButton>
-#include <QDebug>
 #include "searchdialog.h"
+#include "source.h"
+#include "settings.h"
 
 SearchDialog::SearchDialog(QWidget* parent): QDialog(parent),
 numberInput_(0), locationInput_(0), selector_(0)
 {
-       setWindowTitle(tr("Search"));
-
-       QHBoxLayout* numberLayout = new QHBoxLayout;
-       QLabel* numberLabel = new QLabel(tr("Name/number"));
-       numberInput_ = new QLineEdit;
-       numberLayout->addWidget(numberLabel);
-       numberLayout->addWidget(numberInput_);
-
-       QHBoxLayout* locationLayout = new QHBoxLayout;
-       QLabel* locationLabel = new QLabel(tr("Location"));
-       locationInput_ = new QLineEdit;
-       locationLayout->addWidget(locationLabel);
-       locationLayout->addWidget(locationInput_);
-
-       selector_ = new ButtonSelector(tr("Type"), this);
-       selector_->addItem(tr("Persons"));
-       selector_->addItem(tr("Yellow pages"));
-
-       QVBoxLayout* leftLayout = new QVBoxLayout;
-       leftLayout->addLayout(numberLayout);
-       leftLayout->addLayout(locationLayout);
-       leftLayout->addWidget(selector_);
-
-       QDialogButtonBox* buttons = new QDialogButtonBox;
-       QPushButton* submitButton = new QPushButton(tr("Search"));
-       buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
-       connect(submitButton, SIGNAL(pressed()), this, SLOT(searchPressed()));
-
-       QHBoxLayout* mainLayout = new QHBoxLayout;
-       mainLayout->addLayout(leftLayout, Qt::AlignLeft);
-       mainLayout->addWidget(buttons);
-
-       setLayout(mainLayout);
+    setWindowTitle(tr("Search"));
+
+    QHBoxLayout* numberLayout = new QHBoxLayout;
+    QLabel* numberLabel = new QLabel(tr("Name/number"));
+    numberInput_ = new QLineEdit;
+    numberLayout->addWidget(numberLabel);
+    numberLayout->addWidget(numberInput_);
+
+    QHBoxLayout* locationLayout = new QHBoxLayout;
+    QLabel* locationLabel = new QLabel(tr("Location"));
+    locationInput_ = new QLineEdit;
+    locationLayout->addWidget(locationLabel);
+    locationLayout->addWidget(locationInput_);
+
+    selector_ = new ButtonSelector(tr("Type"), this);
+    loadSearchTypes();
+
+    QVBoxLayout* leftLayout = new QVBoxLayout;
+    leftLayout->addLayout(numberLayout);
+    leftLayout->addLayout(locationLayout);
+    leftLayout->addWidget(selector_);
+
+    QDialogButtonBox* buttons = new QDialogButtonBox;
+    buttons->setCenterButtons(false);
+    QPushButton* submitButton = new QPushButton(tr("Search"));
+    buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
+    connect(submitButton, SIGNAL(pressed()), this, SLOT(searchPressed()));
+
+    QHBoxLayout* mainLayout = new QHBoxLayout;
+    mainLayout->addLayout(leftLayout, Qt::AlignLeft);
+    mainLayout->addWidget(buttons);
+
+    setLayout(mainLayout);
 }
 
 void SearchDialog::searchPressed()
 {
-       SearchDetails details;
-       details.name = numberInput_->text();
-
-       if(details.name.isEmpty())
-       {
-               numberInput_->setFocus();
-               return;
-       }
-
-       details.location = locationInput_->text();
-       details.type = selector_->currentIndex();
-       emit search(details);
-       hide();
+    SearchDetails details;
+    details.name = numberInput_->text();
+
+    if(details.name.isEmpty())
+    {
+        numberInput_->setFocus();
+        return;
+    }
+
+    details.location = locationInput_->text();
+    details.type = selector_->value().toInt();
+    emit search(details);
+    hide();
+}
+
+void SearchDialog::setVisible(bool visible)
+{
+    QDialog::setVisible(visible);
+
+    if(visible)
+    {
+        numberInput_->setFocus();
+    }
+}
+
+void SearchDialog::loadSearchTypes()
+{
+    selector_->clear();
+
+    Source* source = Source::getSource(Source::stringToId(Settings::instance()->get("source")));
+
+    QList<Source::SearchType> types;
+    source->getSearchTypes(types);
+
+    if(types.size() > 1)
+    {
+        for(int i = 0; i < types.size(); i++)
+        {
+            switch(types.at(i))
+            {
+            case Source::PERSONS:
+                selector_->addItem(tr("Persons"), static_cast<int>(Source::PERSONS));
+                break;
+            case Source::YELLOW_PAGES:
+                selector_->addItem(tr("Companies"), static_cast<int>(Source::YELLOW_PAGES));
+                break;
+            case Source::BOTH:
+                break;
+            }
+        }
+
+        if(!selector_->isVisible())
+        {
+            selector_->show();
+        }
+    }
+    else
+    {
+        selector_->hide();
+    }
 }