First commit
[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"), tr("Name"), this);
46         streetButton_ = new QMaemo5ValueButton(tr("Street"), this);
47         cityButton_ = new QMaemo5ValueButton(tr("City"), this);
48         numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"), tr("Phone number"), this);
49
50         connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
51
52         top->addWidget(nameButton_);
53         bottom->addWidget(streetButton_);
54         top->addWidget(numberButton_);
55         bottom->addWidget(cityButton_);
56         layout_->addLayout(top);
57         layout_->addLayout(bottom);
58         layout_->addWidget(addButton);
59         layout_->addWidget(copyButton);
60         area_->setLayout(layout_);
61         setCentralWidget(area_);
62 }
63
64 void DetailWindow::loadData(Eniro::Result const& details)
65 {
66         setWindowTitle(details.name);
67         nameButton_->setValueText(details.name);
68         streetButton_->setValueText(details.street);
69         cityButton_->setValueText(details.city);
70         numberButton_->setValueText(details.number);
71         layout_->update();
72         show();
73 }
74
75 void DetailWindow::makeCall()
76 {
77         QString number = numberButton_->valueText();
78
79         qDebug() << number;
80
81         if(number.isEmpty())
82         {
83                 return;
84         }
85
86         QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
87                                                                   "/com/nokia/csd/call",
88                                                                   "com.nokia.csd.Call",
89                                                                   "CreateWith");
90         QList<QVariant> arguments;
91
92         arguments.append(QVariant(number));
93         arguments.append(QVariant(0));
94
95         msg.setArguments(arguments);
96
97         QDBusConnection::systemBus().send(msg);
98
99 }
100
101 void DetailWindow::showAddToContactsDialog()
102 {
103         if(!addDialog_)
104         {
105                 addDialog_ = new QDialog(this);
106                 addDialog_->setWindowTitle(tr("Add to contacts"));
107                 addContactInput_ = new QLineEdit();
108                 QHBoxLayout* layout = new QHBoxLayout();
109                 QLabel* name = new QLabel(tr("Name"));
110                 QPushButton* button = new QPushButton(tr("Add"));
111                 connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
112                 layout->addWidget(name);
113                 layout->addWidget(addContactInput_);
114                 layout->addWidget(button);
115                 addDialog_->setLayout(layout);
116         }
117
118         addContactInput_->setText(nameButton_->valueText());
119         addDialog_->show();
120 }
121
122 void DetailWindow::addToContacts()
123 {
124         ContactManager cm;
125         ContactManager::Contact contact;
126         contact.name = addContactInput_->text();
127         contact.number = numberButton_->valueText();
128
129         addDialog_->hide();
130
131         if(cm.addContact(contact))
132         {
133                 QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
134         }
135         else
136         {
137                 QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
138         }
139
140 }
141
142 void DetailWindow::copyToClipboard()
143 {
144         QApplication::clipboard()->setText(numberButton_->valueText());
145         QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
146 }