Added some sort of Ovi Maps integration. All translations updated.
[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     contact.name = addContactInput_->text();
158     contact.number = numberButton_->valueText();
159
160     addDialog_->hide();
161
162     if(cm.addContact(contact))
163     {
164         QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
165     }
166     else
167     {
168         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
169     }
170
171     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
172
173 }
174
175 void DetailWindow::copyToClipboard()
176 {
177     QApplication::clipboard()->setText(numberButton_->valueText());
178     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
179 }
180
181 void DetailWindow::sendSMS()
182 {
183     QString number = numberButton_->valueText();
184
185     if(number.isEmpty())
186     {
187         return;
188     }
189
190     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.MessagingUI",
191                                                       "/com/nokia/MessagingUI",
192                                                       "com.nokia.MessagingUI",
193                                                       "messaging_ui_interface_start_sms");
194     QList<QVariant> arguments;
195
196     arguments.append(QVariant("sms:" + number));
197
198     msg.setArguments(arguments);
199
200     if(!QDBusConnection::systemBus().send(msg))
201     {
202         QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
203     }
204
205 }
206
207 void DetailWindow::openMaps()
208 {
209     QString street = streetButton_->valueText();
210     QString city = cityButton_->valueText();
211     QString number;
212     QString zip;
213
214     if(street.isEmpty() && city.isEmpty())
215     {
216         return;
217     }
218
219     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
220
221     int pos = 0;
222
223     QStringList words = street.split(" ", QString::SkipEmptyParts);
224
225     static QRegExp numberCheck("([0-9-]+)");
226
227     bool numberFound = false;
228     bool numberSet = false;
229
230     QString streetStr;
231     QString numberStr;
232
233     for(int i = 0; i < words.size(); i++)
234     {
235         if(i > 0 && numberCheck.exactMatch(words.at(i)))
236         {
237             numberFound = true;
238         }
239
240         if(numberFound)
241         {
242             if(!numberSet)
243             {
244                 numberStr = words.at(i);
245                 numberSet = true;
246             }
247         }
248         else
249         {
250             streetStr += words.at(i) + " ";
251         }
252     }
253
254     number = numberStr.trimmed();
255     street = streetStr.trimmed();
256
257     if((pos = city.indexOf(" ")) > 0)
258     {
259         if(numberCheck.exactMatch(city.left(pos)))
260         {
261             zip = city.left(pos);
262             city = city.mid(pos + 1);
263         }
264     }
265
266     OviMaps maps;
267
268     OviMaps::Address addr;
269     addr.street = street;
270     addr.number = number;
271     addr.zipCode = zip;
272     addr.city = city;
273     addr.country = country_;
274
275     if(!maps.openMaps(addr))
276     {
277         QMaemo5InformationBox::information(this, tr("Unable to find coordinates for address."));
278     }
279
280     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
281 }