Added Das Telefonbuch support.
[jenirok] / src / gui / searchdialog.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QDebug>
20 #include <QtGui/QHBoxLayout>
21 #include <QtGui/QVBoxLayout>
22 #include <QtGui/QLabel>
23 #include <QtGui/QDialogButtonBox>
24 #include <QMaemo5ValueButton>
25 #include "searchdialog.h"
26 #include "source.h"
27 #include "settings.h"
28
29 SearchDialog::SearchDialog(QWidget* parent): QDialog(parent),
30 numberInput_(0), locationInput_(0), selector_(0)
31 {
32     setWindowTitle(tr("Search"));
33
34     QHBoxLayout* numberLayout = new QHBoxLayout;
35     QLabel* numberLabel = new QLabel(tr("Name/number"));
36     numberInput_ = new QLineEdit;
37     numberLayout->addWidget(numberLabel);
38     numberLayout->addWidget(numberInput_);
39
40     QHBoxLayout* locationLayout = new QHBoxLayout;
41     QLabel* locationLabel = new QLabel(tr("Location"));
42     locationInput_ = new QLineEdit;
43     locationLayout->addWidget(locationLabel);
44     locationLayout->addWidget(locationInput_);
45
46     selector_ = new ButtonSelector(tr("Type"), this);
47     loadSearchTypes();
48
49     QVBoxLayout* leftLayout = new QVBoxLayout;
50     leftLayout->addLayout(numberLayout);
51     leftLayout->addLayout(locationLayout);
52     leftLayout->addWidget(selector_);
53
54     QDialogButtonBox* buttons = new QDialogButtonBox;
55     buttons->setCenterButtons(false);
56     QPushButton* submitButton = new QPushButton(tr("Search"));
57     buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
58     connect(submitButton, SIGNAL(pressed()), this, SLOT(searchPressed()));
59
60     QHBoxLayout* mainLayout = new QHBoxLayout;
61     mainLayout->addLayout(leftLayout, Qt::AlignLeft);
62     mainLayout->addWidget(buttons);
63
64     setLayout(mainLayout);
65 }
66
67 void SearchDialog::searchPressed()
68 {
69     SearchDetails details;
70     details.name = numberInput_->text();
71
72     if(details.name.isEmpty())
73     {
74         numberInput_->setFocus();
75         return;
76     }
77
78     details.location = locationInput_->text();
79     details.type = selector_->value().toInt();
80     emit search(details);
81     hide();
82 }
83
84 void SearchDialog::setVisible(bool visible)
85 {
86     QDialog::setVisible(visible);
87
88     if(visible)
89     {
90         numberInput_->setFocus();
91     }
92 }
93
94 void SearchDialog::loadSearchTypes()
95 {
96     selector_->clear();
97
98     Source* source = Source::getSource(Source::stringToId(Settings::instance()->get("source")));
99
100     QList<Source::SearchType> types;
101     source->getSearchTypes(types);
102
103     if(types.size() > 1)
104     {
105         for(int i = 0; i < types.size(); i++)
106         {
107             switch(types.at(i))
108             {
109             case Source::PERSONS:
110                 selector_->addItem(tr("Persons"), static_cast<int>(Source::PERSONS));
111                 break;
112             case Source::YELLOW_PAGES:
113                 selector_->addItem(tr("Companies"), static_cast<int>(Source::YELLOW_PAGES));
114                 break;
115             case Source::BOTH:
116                 break;
117             }
118         }
119
120         if(!selector_->isVisible())
121         {
122             selector_->show();
123         }
124     }
125     else
126     {
127         selector_->hide();
128     }
129 }