Fixed some inconsistencies in code indentation
[jenirok] / src / gui / detailwindow.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 <QMaemo5ValueButton>
20 #include <QMaemo5InformationBox>
21 #include <QApplication>
22 #include <QtDBus/QDBusConnection>
23 #include <QtDBus/QDBusMessage>
24 #include <QtGui/QMessageBox>
25 #include <QtGui/QLabel>
26 #include <QtGui/QClipboard>
27 #include <QDebug>
28 #include "detailwindow.h"
29 #include "contactmanager.h"
30
31 DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0)
32 {
33     setAttribute(Qt::WA_Maemo5StackedWindow);
34     area_ = new QScrollArea(this);
35     layout_ = new QVBoxLayout;
36     QHBoxLayout* top = new QHBoxLayout;
37     QHBoxLayout* bottom = new QHBoxLayout;
38
39     QPushButton* addButton = new QPushButton(QIcon::fromTheme("general_contacts"), tr("Add to contacts"));
40     QPushButton* copyButton = new QPushButton(tr("Copy number to clipboard"));
41
42     connect(addButton, SIGNAL(pressed()), this, SLOT(showAddToContactsDialog()));
43     connect(copyButton, SIGNAL(pressed()), this, SLOT(copyToClipboard()));
44
45     nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
46                                          tr("Name"), this);
47     streetButton_ = new QMaemo5ValueButton(tr("Street"), this);
48     cityButton_ = new QMaemo5ValueButton(tr("City"), this);
49     numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
50                                            tr("Phone number"), this);
51
52     connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
53
54     top->addWidget(nameButton_);
55     bottom->addWidget(streetButton_);
56     top->addWidget(numberButton_);
57     bottom->addWidget(cityButton_);
58     layout_->addLayout(top);
59     layout_->addLayout(bottom);
60     layout_->addWidget(addButton);
61     layout_->addWidget(copyButton);
62     area_->setLayout(layout_);
63     setCentralWidget(area_);
64 }
65
66 void DetailWindow::loadData(Eniro::Result const& details)
67 {
68     setWindowTitle(details.name);
69     nameButton_->setValueText(details.name);
70     streetButton_->setValueText(details.street);
71     cityButton_->setValueText(details.city);
72     numberButton_->setValueText(details.number);
73     layout_->update();
74     show();
75 }
76
77 void DetailWindow::makeCall()
78 {
79     QString number = numberButton_->valueText();
80
81     qDebug() << number;
82
83     if(number.isEmpty())
84     {
85         return;
86     }
87
88     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
89                                                       "/com/nokia/csd/call",
90                                                       "com.nokia.csd.Call",
91                                                       "CreateWith");
92     QList<QVariant> arguments;
93
94     arguments.append(QVariant(number));
95     arguments.append(QVariant(0));
96
97     msg.setArguments(arguments);
98
99     QDBusConnection::systemBus().send(msg);
100
101 }
102
103 void DetailWindow::showAddToContactsDialog()
104 {
105     if(!addDialog_)
106     {
107         addDialog_ = new QDialog(this);
108         addDialog_->setWindowTitle(tr("Add to contacts"));
109         addContactInput_ = new QLineEdit();
110         QHBoxLayout* layout = new QHBoxLayout();
111         QLabel* name = new QLabel(tr("Name"));
112         QPushButton* button = new QPushButton(tr("Add"));
113         connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
114         layout->addWidget(name);
115         layout->addWidget(addContactInput_);
116         layout->addWidget(button);
117         addDialog_->setLayout(layout);
118     }
119
120     addContactInput_->setText(nameButton_->valueText());
121     addDialog_->show();
122 }
123
124 void DetailWindow::addToContacts()
125 {
126     ContactManager cm;
127     ContactManager::Contact contact;
128     contact.name = addContactInput_->text();
129     contact.number = numberButton_->valueText();
130
131     addDialog_->hide();
132
133     if(cm.addContact(contact))
134     {
135         QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
136     }
137     else
138     {
139         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
140     }
141
142 }
143
144 void DetailWindow::copyToClipboard()
145 {
146     QApplication::clipboard()->setText(numberButton_->valueText());
147     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
148 }