2 * This file is part of Jenirok.
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.
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.
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/>.
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"
37 DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0)
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;
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"));
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()));
57 nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
59 streetButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_map"),
61 cityButton_ = new QMaemo5ValueButton(tr("City"), this);
62 numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
63 tr("Phone number"), this);
65 connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
66 connect(streetButton_, SIGNAL(pressed()), this, SLOT(openMaps()));
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_);
84 void DetailWindow::loadData(Source::Result const& details)
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;
95 void DetailWindow::makeCall()
97 QString number = numberButton_->valueText();
104 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
105 "/com/nokia/csd/call",
106 "com.nokia.csd.Call",
108 QList<QVariant> arguments;
110 arguments.append(QVariant(number));
111 arguments.append(QVariant(0));
113 msg.setArguments(arguments);
115 if(!QDBusConnection::systemBus().send(msg))
117 QMessageBox::critical(this, tr("Error"), tr("Unable make call"));
122 void DetailWindow::showAddToContactsDialog()
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"));
133 QDialogButtonBox* buttons = new QDialogButtonBox;
134 buttons->setCenterButtons(false);
135 buttons->addButton(button, QDialogButtonBox::AcceptRole);
136 connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
138 QHBoxLayout* left = new QHBoxLayout();
139 left->addWidget(name);
140 left->addWidget(addContactInput_);
142 layout->addLayout(left, Qt::AlignLeft);
143 layout->addWidget(buttons);
144 addDialog_->setLayout(layout);
147 addContactInput_->setText(nameButton_->valueText());
151 void DetailWindow::addToContacts()
153 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
156 ContactManager::Contact contact;
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_;
167 if(cm.addContact(contact))
169 QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
173 QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
176 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
180 void DetailWindow::copyToClipboard()
182 QApplication::clipboard()->setText(numberButton_->valueText());
183 QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
186 void DetailWindow::sendSMS()
188 QString number = numberButton_->valueText();
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;
201 arguments.append(QVariant("sms:" + number));
203 msg.setArguments(arguments);
205 if(!QDBusConnection::systemBus().send(msg))
207 QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
212 void DetailWindow::openMaps()
214 QString street = streetButton_->valueText();
215 QString city = cityButton_->valueText();
217 if(street.isEmpty() && city.isEmpty())
222 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
226 OviMaps::Address addr;
227 QString streetAndNumber;
228 getDetails(addr.street, addr.number,
229 addr.zipCode, addr.city, streetAndNumber);
230 addr.country = country_;
232 //qDebug() << addr.street << addr.number << addr.zipCode << addr.city << addr.country;
234 if(!maps.openMaps(addr))
236 QMaemo5InformationBox::information(this, tr("Unable to find coordinates for address."));
239 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
242 void DetailWindow::getDetails(QString& street, QString& streetNumber,
243 QString& zip, QString& city, QString& streetAndNumber)
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();
254 QStringList words = streetVal.split(" ", QString::SkipEmptyParts);
256 static QRegExp numberCheck("([0-9-]+)");
258 bool numberFound = false;
259 bool numberSet = false;
261 for(int i = 0; i < words.size(); i++)
263 if(i > 0 && numberCheck.exactMatch(words.at(i)))
272 streetNumber = words.at(i);
278 street += words.at(i) + " ";
282 if(streetNumber.isEmpty())
284 static QRegExp addrCheck(" ([0-9]+)");
286 if((pos = addrCheck.indexIn(street)) != -1)
288 streetNumber = addrCheck.cap(1);
289 street = street.left(pos);
293 streetNumber = streetNumber.trimmed();
294 street = street.trimmed();
296 if((pos = cityVal.indexOf(" ")) > 0)
298 if(numberCheck.exactMatch(cityVal.left(pos)))
300 zip = cityVal.left(pos);
301 city = cityVal.mid(pos + 1);