f7088c2baace112d19f86cabc8f9d469621e3b19
[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 <QtCore/QDebug>
20 #include <QtCore/QList>
21 #include <QtDBus/QDBusConnection>
22 #include <QtDBus/QDBusMessage>
23 #include <QtDBus/QDBusReply>
24 #include <QtDBus/QDBusArgument>
25 #include <QtDBus/QDBusMetaType>
26 #include <QtGui/QMessageBox>
27 #include <QtGui/QLabel>
28 #include <QtGui/QClipboard>
29 #include <QtGui/QDialogButtonBox>
30 #include <QtGui/QApplication>
31 #include <QMaemo5ValueButton>
32 #include <QMaemo5InformationBox>
33 #include "detailwindow.h"
34 #include "contactmanager.h"
35 #include "ovimaps.h"
36
37 DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0)
38 {
39     setAttribute(Qt::WA_Maemo5StackedWindow);
40     area_ = new QWidget(this);
41     layout_ = new QVBoxLayout;
42     QHBoxLayout* top = new QHBoxLayout;
43     QHBoxLayout* bottom = new QHBoxLayout;
44     QHBoxLayout* actions1 = new QHBoxLayout;
45     QHBoxLayout* actions2 = new QHBoxLayout;
46
47     QPushButton* addButton = new QPushButton(QIcon::fromTheme("general_contacts"), tr("Add to contacts"));
48     QPushButton* copyButton = new QPushButton(tr("Copy number to clipboard"));
49     QPushButton* callButton = new QPushButton(QIcon::fromTheme("general_call"), tr("Call"));
50     QPushButton* smsButton = new QPushButton(QIcon::fromTheme("general_sms"), tr("Send SMS"));
51
52     connect(addButton, SIGNAL(pressed()), this, SLOT(showAddToContactsDialog()));
53     connect(copyButton, SIGNAL(pressed()), this, SLOT(copyToClipboard()));
54     connect(callButton, SIGNAL(pressed()), this, SLOT(makeCall()));
55     connect(smsButton, SIGNAL(pressed()), this, SLOT(sendSMS()));
56
57     nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
58                                          tr("Name"), this);
59     streetButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_map"),
60                                            tr("Street"), this);
61     cityButton_ = new QMaemo5ValueButton(tr("City"), this);
62     numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
63                                            tr("Phone number"), this);
64
65     connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
66     connect(streetButton_, SIGNAL(pressed()), this, SLOT(openMaps()));
67
68     top->addWidget(nameButton_);
69     bottom->addWidget(streetButton_);
70     top->addWidget(numberButton_);
71     bottom->addWidget(cityButton_);
72     actions1->addWidget(callButton);
73     actions1->addWidget(smsButton);
74     actions2->addWidget(addButton);
75     actions2->addWidget(copyButton);
76     layout_->addLayout(top);
77     layout_->addLayout(bottom);
78     layout_->addLayout(actions1);
79     layout_->addLayout(actions2);
80     area_->setLayout(layout_);
81     setCentralWidget(area_);
82 }
83
84 void DetailWindow::loadData(Source::Result const& details)
85 {
86     setWindowTitle(details.name);
87     nameButton_->setValueText(details.name);
88     streetButton_->setValueText(details.street);
89     cityButton_->setValueText(details.city);
90     numberButton_->setValueText(details.number);
91     country_ = details.country;
92     show();
93 }
94
95 void DetailWindow::makeCall()
96 {
97     QString number = numberButton_->valueText();
98
99     if(number.isEmpty())
100     {
101         return;
102     }
103
104     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
105                                                       "/com/nokia/csd/call",
106                                                       "com.nokia.csd.Call",
107                                                       "CreateWith");
108     QList<QVariant> arguments;
109
110     arguments.append(QVariant(number));
111     arguments.append(QVariant(0));
112
113     msg.setArguments(arguments);
114
115     if(!QDBusConnection::systemBus().send(msg))
116     {
117         QMessageBox::critical(this, tr("Error"), tr("Unable make call"));
118     }
119
120 }
121
122 void DetailWindow::showAddToContactsDialog()
123 {
124     if(!addDialog_)
125     {
126         addDialog_ = new QDialog(this);
127         addDialog_->setWindowTitle(tr("Add to contacts"));
128         addContactInput_ = new QLineEdit();
129         QHBoxLayout* layout = new QHBoxLayout();
130         QLabel* name = new QLabel(tr("Name"));
131         QPushButton* button = new QPushButton(tr("Add"));
132
133         QDialogButtonBox* buttons = new QDialogButtonBox;
134         buttons->setCenterButtons(false);
135         buttons->addButton(button, QDialogButtonBox::AcceptRole);
136         connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
137
138         QHBoxLayout* left = new QHBoxLayout();
139         left->addWidget(name);
140         left->addWidget(addContactInput_);
141
142         layout->addLayout(left, Qt::AlignLeft);
143         layout->addWidget(buttons);
144         addDialog_->setLayout(layout);
145     }
146
147     addContactInput_->setText(nameButton_->valueText());
148     addDialog_->show();
149 }
150
151 void DetailWindow::addToContacts()
152 {
153     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
154
155     ContactManager cm;
156     ContactManager::Contact contact;
157     getDetails(contact.street, contact.streetNumber,
158                contact.zipCode, contact.city);
159     contact.name = addContactInput_->text();
160     contact.number = numberButton_->valueText();
161     contact.country = country_;
162
163     addDialog_->hide();
164
165     if(cm.addContact(contact))
166     {
167         QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
168     }
169     else
170     {
171         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
172     }
173
174     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
175
176 }
177
178 void DetailWindow::copyToClipboard()
179 {
180     QApplication::clipboard()->setText(numberButton_->valueText());
181     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
182 }
183
184 void DetailWindow::sendSMS()
185 {
186     QString number = numberButton_->valueText();
187
188     if(number.isEmpty())
189     {
190         return;
191     }
192
193     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.MessagingUI",
194                                                       "/com/nokia/MessagingUI",
195                                                       "com.nokia.MessagingUI",
196                                                       "messaging_ui_interface_start_sms");
197     QList<QVariant> arguments;
198
199     arguments.append(QVariant("sms:" + number));
200
201     msg.setArguments(arguments);
202
203     if(!QDBusConnection::systemBus().send(msg))
204     {
205         QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
206     }
207
208 }
209
210 void DetailWindow::openMaps()
211 {
212     QString street = streetButton_->valueText();
213     QString city = cityButton_->valueText();
214
215     if(street.isEmpty() && city.isEmpty())
216     {
217         return;
218     }
219
220     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
221
222     OviMaps maps;
223
224     OviMaps::Address addr;
225     getDetails(addr.street, addr.number,
226                addr.zipCode, addr.city);
227     addr.country = country_;
228
229     if(!maps.openMaps(addr))
230     {
231         QMaemo5InformationBox::information(this, tr("Unable to find coordinates for address."));
232     }
233
234     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
235 }
236
237 void DetailWindow::getDetails(QString& street, QString& streetNumber,
238                               QString& zip, QString& city)
239 {
240     int pos = 0;
241
242     QString streetVal = streetButton_->valueText();
243     QString cityVal = cityButton_->valueText();
244
245     QStringList words = streetVal.split(" ", QString::SkipEmptyParts);
246
247     static QRegExp numberCheck("([0-9-]+)");
248
249     bool numberFound = false;
250     bool numberSet = false;
251
252     for(int i = 0; i < words.size(); i++)
253     {
254         if(i > 0 && numberCheck.exactMatch(words.at(i)))
255         {
256             numberFound = true;
257         }
258
259         if(numberFound)
260         {
261             if(!numberSet)
262             {
263                 streetNumber = words.at(i);
264                 numberSet = true;
265             }
266         }
267         else
268         {
269             street += words.at(i) + " ";
270         }
271     }
272
273     streetNumber = streetNumber.trimmed();
274     street = street.trimmed();
275
276     if((pos = cityVal.indexOf(" ")) > 0)
277     {
278         if(numberCheck.exactMatch(cityVal.left(pos)))
279         {
280             zip = cityVal.left(pos);
281             city = cityVal.mid(pos + 1);
282         }
283     }
284
285 }