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;
157 contact.name = addContactInput_->text();
158 contact.number = numberButton_->valueText();
162 if(cm.addContact(contact))
164 QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
168 QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
171 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
175 void DetailWindow::copyToClipboard()
177 QApplication::clipboard()->setText(numberButton_->valueText());
178 QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
181 void DetailWindow::sendSMS()
183 QString number = numberButton_->valueText();
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;
196 arguments.append(QVariant("sms:" + number));
198 msg.setArguments(arguments);
200 if(!QDBusConnection::systemBus().send(msg))
202 QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
207 void DetailWindow::openMaps()
209 QString street = streetButton_->valueText();
210 QString city = cityButton_->valueText();
214 if(street.isEmpty() && city.isEmpty())
219 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
223 QStringList words = street.split(" ", QString::SkipEmptyParts);
225 static QRegExp numberCheck("([0-9-]+)");
227 bool numberFound = false;
228 bool numberSet = false;
233 for(int i = 0; i < words.size(); i++)
235 if(i > 0 && numberCheck.exactMatch(words.at(i)))
244 numberStr = words.at(i);
250 streetStr += words.at(i) + " ";
254 number = numberStr.trimmed();
255 street = streetStr.trimmed();
257 if((pos = city.indexOf(" ")) > 0)
259 if(numberCheck.exactMatch(city.left(pos)))
261 zip = city.left(pos);
262 city = city.mid(pos + 1);
268 OviMaps::Address addr;
269 addr.street = street;
270 addr.number = number;
273 addr.country = country_;
275 if(!maps.openMaps(addr))
277 QMaemo5InformationBox::information(this, tr("Unable to find coordinates for address."));
280 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);