8dc4349e7409a0e0c12a3b148830230219ff4976
[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     QHBoxLayout* actions1 = new QHBoxLayout;
40     QHBoxLayout* actions2 = new QHBoxLayout;
41
42     QPushButton* addButton = new QPushButton(QIcon::fromTheme("general_contacts"), tr("Add to contacts"));
43     QPushButton* copyButton = new QPushButton(tr("Copy number to clipboard"));
44     QPushButton* callButton = new QPushButton(QIcon::fromTheme("general_call"), tr("Call"));
45     QPushButton* smsButton = new QPushButton(QIcon::fromTheme("general_sms"), tr("Send SMS"));
46
47     connect(addButton, SIGNAL(pressed()), this, SLOT(showAddToContactsDialog()));
48     connect(copyButton, SIGNAL(pressed()), this, SLOT(copyToClipboard()));
49     connect(callButton, SIGNAL(pressed()), this, SLOT(makeCall()));
50     connect(smsButton, SIGNAL(pressed()), this, SLOT(sendSMS()));
51
52     nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
53                                          tr("Name"), this);
54     streetButton_ = new QMaemo5ValueButton(tr("Street"), this);
55     cityButton_ = new QMaemo5ValueButton(tr("City"), this);
56     numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
57                                            tr("Phone number"), this);
58
59     connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
60
61     top->addWidget(nameButton_);
62     bottom->addWidget(streetButton_);
63     top->addWidget(numberButton_);
64     bottom->addWidget(cityButton_);
65     actions1->addWidget(callButton);
66     actions1->addWidget(smsButton);
67     actions2->addWidget(addButton);
68     actions2->addWidget(copyButton);
69     layout_->addLayout(top);
70     layout_->addLayout(bottom);
71     layout_->addLayout(actions1);
72     layout_->addLayout(actions2);
73     area_->setLayout(layout_);
74     setCentralWidget(area_);
75 }
76
77 void DetailWindow::loadData(Eniro::Result const& details)
78 {
79     setWindowTitle(details.name);
80     nameButton_->setValueText(details.name);
81     streetButton_->setValueText(details.street);
82     cityButton_->setValueText(details.city);
83     numberButton_->setValueText(details.number);
84     layout_->update();
85     show();
86 }
87
88 void DetailWindow::makeCall()
89 {
90     QString number = numberButton_->valueText();
91
92     if(number.isEmpty())
93     {
94         return;
95     }
96
97     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
98                                                       "/com/nokia/csd/call",
99                                                       "com.nokia.csd.Call",
100                                                       "CreateWith");
101     QList<QVariant> arguments;
102
103     arguments.append(QVariant(number));
104     arguments.append(QVariant(0));
105
106     msg.setArguments(arguments);
107
108     if(!QDBusConnection::systemBus().send(msg))
109     {
110         QMessageBox::critical(this, tr("Error"), tr("Unable make call"));
111     }
112
113 }
114
115 void DetailWindow::showAddToContactsDialog()
116 {
117     if(!addDialog_)
118     {
119         addDialog_ = new QDialog(this);
120         addDialog_->setWindowTitle(tr("Add to contacts"));
121         addContactInput_ = new QLineEdit();
122         QHBoxLayout* layout = new QHBoxLayout();
123         QLabel* name = new QLabel(tr("Name"));
124         QPushButton* button = new QPushButton(tr("Add"));
125
126         QDialogButtonBox* buttons = new QDialogButtonBox;
127         buttons->setCenterButtons(false);
128         buttons->addButton(button, QDialogButtonBox::AcceptRole);
129         connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
130
131         QHBoxLayout* left = new QHBoxLayout();
132         left->addWidget(name);
133         left->addWidget(addContactInput_);
134
135         layout->addLayout(left, Qt::AlignLeft);
136         layout->addWidget(buttons);
137         addDialog_->setLayout(layout);
138     }
139
140     addContactInput_->setText(nameButton_->valueText());
141     addDialog_->show();
142 }
143
144 void DetailWindow::addToContacts()
145 {
146     ContactManager cm;
147     ContactManager::Contact contact;
148     contact.name = addContactInput_->text();
149     contact.number = numberButton_->valueText();
150
151     addDialog_->hide();
152
153     if(cm.addContact(contact))
154     {
155         QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
156     }
157     else
158     {
159         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
160     }
161
162 }
163
164 void DetailWindow::copyToClipboard()
165 {
166     QApplication::clipboard()->setText(numberButton_->valueText());
167     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
168 }
169
170 void DetailWindow::sendSMS()
171 {
172     QString number = numberButton_->valueText();
173
174     if(number.isEmpty())
175     {
176         return;
177     }
178
179     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.MessagingUI",
180                                                       "/com/nokia/MessagingUI",
181                                                       "com.nokia.MessagingUI",
182                                                       "messaging_ui_interface_start_sms");
183     QList<QVariant> arguments;
184
185     arguments.append(QVariant("sms:" + number));
186
187     msg.setArguments(arguments);
188
189     if(!QDBusConnection::systemBus().send(msg))
190     {
191         QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
192     }
193
194 }