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