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