Added call log and German translation.
[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 "settings.h"
27
28 SearchDialog::SearchDialog(QWidget* parent): QDialog(parent),
29 numberInput_(0), locationInput_(0), selector_(0)
30 {
31     setWindowTitle(tr("Search"));
32
33     QHBoxLayout* numberLayout = new QHBoxLayout;
34     QLabel* numberLabel = new QLabel(tr("Name/number"));
35     numberInput_ = new QLineEdit;
36     numberLayout->addWidget(numberLabel);
37     numberLayout->addWidget(numberInput_);
38
39     QHBoxLayout* locationLayout = new QHBoxLayout;
40     QLabel* locationLabel = new QLabel(tr("Location"));
41     locationInput_ = new QLineEdit;
42     locationLayout->addWidget(locationLabel);
43     locationLayout->addWidget(locationInput_);
44
45     selector_ = new ButtonSelector(tr("Type"), this);
46     loadSearchTypes();
47
48     QVBoxLayout* leftLayout = new QVBoxLayout;
49     leftLayout->addLayout(numberLayout);
50     leftLayout->addLayout(locationLayout);
51     leftLayout->addWidget(selector_);
52
53     QDialogButtonBox* buttons = new QDialogButtonBox;
54     buttons->setCenterButtons(false);
55     QPushButton* submitButton = new QPushButton(tr("Search"));
56     buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
57     connect(submitButton, SIGNAL(pressed()), this, SLOT(searchPressed()));
58
59     QHBoxLayout* mainLayout = new QHBoxLayout;
60     mainLayout->addLayout(leftLayout, Qt::AlignLeft);
61     mainLayout->addWidget(buttons);
62
63     setLayout(mainLayout);
64 }
65
66 void SearchDialog::searchPressed()
67 {
68     SearchDetails details;
69     details.name = numberInput_->text();
70
71     if(details.name.isEmpty())
72     {
73         numberInput_->setFocus();
74         return;
75     }
76
77     details.location = locationInput_->text();
78
79     int type = 0;
80
81     if(selector_->isVisible())
82     {
83         type = selector_->value().toInt();
84     }
85
86     details.type = static_cast<Source::SearchType>(type);
87
88     emit search(details);
89     hide();
90 }
91
92 void SearchDialog::setVisible(bool visible)
93 {
94     QDialog::setVisible(visible);
95
96     if(visible)
97     {
98         numberInput_->setFocus();
99     }
100 }
101
102 void SearchDialog::loadSearchTypes()
103 {
104     selector_->clear();
105
106     Source* source = Source::getSource(Source::stringToId(Settings::instance()->get("source")));
107
108     QList<Source::SearchType> types;
109     source->getSearchTypes(types);
110
111     if(types.size() > 1)
112     {
113         for(int i = 0; i < types.size(); i++)
114         {
115             switch(types.at(i))
116             {
117             case Source::PERSONS:
118                 selector_->addItem(tr("Persons"), static_cast<int>(Source::PERSONS));
119                 break;
120             case Source::YELLOW_PAGES:
121                 selector_->addItem(tr("Companies"), static_cast<int>(Source::YELLOW_PAGES));
122                 break;
123             case Source::BOTH:
124                 break;
125             }
126         }
127
128         if(!selector_->isVisible())
129         {
130             selector_->show();
131         }
132     }
133     else
134     {
135         selector_->hide();
136     }
137 }