Danish Eniro search fixed.
[jenirok] / src / gui / detailwindow.cpp
index 71cffce..4d6107d 100644 (file)
  *
  */
 
-#include <QMaemo5ValueButton>
-#include <QMaemo5InformationBox>
-#include <QApplication>
+#include <QtCore/QDebug>
+#include <QtCore/QList>
 #include <QtDBus/QDBusConnection>
 #include <QtDBus/QDBusMessage>
+#include <QtDBus/QDBusReply>
+#include <QtDBus/QDBusArgument>
+#include <QtDBus/QDBusMetaType>
 #include <QtGui/QMessageBox>
 #include <QtGui/QLabel>
 #include <QtGui/QClipboard>
-#include <QDebug>
+#include <QtGui/QDialogButtonBox>
+#include <QtGui/QApplication>
+#include <QMaemo5ValueButton>
+#include <QMaemo5InformationBox>
 #include "detailwindow.h"
 #include "contactmanager.h"
+#include "ovimaps.h"
 
 DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0)
 {
     setAttribute(Qt::WA_Maemo5StackedWindow);
-    area_ = new QScrollArea(this);
+    area_ = new QWidget(this);
     layout_ = new QVBoxLayout;
     QHBoxLayout* top = new QHBoxLayout;
     QHBoxLayout* bottom = new QHBoxLayout;
+    QHBoxLayout* actions1 = new QHBoxLayout;
+    QHBoxLayout* actions2 = new QHBoxLayout;
 
     QPushButton* addButton = new QPushButton(QIcon::fromTheme("general_contacts"), tr("Add to contacts"));
     QPushButton* copyButton = new QPushButton(tr("Copy number to clipboard"));
+    QPushButton* callButton = new QPushButton(QIcon::fromTheme("general_call"), tr("Call"));
+    QPushButton* smsButton = new QPushButton(QIcon::fromTheme("general_sms"), tr("Send SMS"));
 
     connect(addButton, SIGNAL(pressed()), this, SLOT(showAddToContactsDialog()));
     connect(copyButton, SIGNAL(pressed()), this, SLOT(copyToClipboard()));
+    connect(callButton, SIGNAL(pressed()), this, SLOT(makeCall()));
+    connect(smsButton, SIGNAL(pressed()), this, SLOT(sendSMS()));
 
     nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
                                          tr("Name"), this);
-    streetButton_ = new QMaemo5ValueButton(tr("Street"), this);
+    streetButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_map"),
+                                           tr("Street"), this);
     cityButton_ = new QMaemo5ValueButton(tr("City"), this);
     numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
                                            tr("Phone number"), this);
 
     connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
+    connect(streetButton_, SIGNAL(pressed()), this, SLOT(openMaps()));
 
     top->addWidget(nameButton_);
     bottom->addWidget(streetButton_);
     top->addWidget(numberButton_);
     bottom->addWidget(cityButton_);
+    actions1->addWidget(callButton);
+    actions1->addWidget(smsButton);
+    actions2->addWidget(addButton);
+    actions2->addWidget(copyButton);
     layout_->addLayout(top);
     layout_->addLayout(bottom);
-    layout_->addWidget(addButton);
-    layout_->addWidget(copyButton);
+    layout_->addLayout(actions1);
+    layout_->addLayout(actions2);
     area_->setLayout(layout_);
     setCentralWidget(area_);
 }
 
-void DetailWindow::loadData(Eniro::Result const& details)
+void DetailWindow::loadData(Source::Result const& details)
 {
     setWindowTitle(details.name);
     nameButton_->setValueText(details.name);
     streetButton_->setValueText(details.street);
     cityButton_->setValueText(details.city);
     numberButton_->setValueText(details.number);
-    layout_->update();
+    country_ = details.country;
     show();
 }
 
@@ -78,8 +96,6 @@ void DetailWindow::makeCall()
 {
     QString number = numberButton_->valueText();
 
-    qDebug() << number;
-
     if(number.isEmpty())
     {
         return;
@@ -96,7 +112,10 @@ void DetailWindow::makeCall()
 
     msg.setArguments(arguments);
 
-    QDBusConnection::systemBus().send(msg);
+    if(!QDBusConnection::systemBus().send(msg))
+    {
+        QMessageBox::critical(this, tr("Error"), tr("Unable make call"));
+    }
 
 }
 
@@ -110,10 +129,18 @@ void DetailWindow::showAddToContactsDialog()
         QHBoxLayout* layout = new QHBoxLayout();
         QLabel* name = new QLabel(tr("Name"));
         QPushButton* button = new QPushButton(tr("Add"));
+
+        QDialogButtonBox* buttons = new QDialogButtonBox;
+        buttons->setCenterButtons(false);
+        buttons->addButton(button, QDialogButtonBox::AcceptRole);
         connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
-        layout->addWidget(name);
-        layout->addWidget(addContactInput_);
-        layout->addWidget(button);
+
+        QHBoxLayout* left = new QHBoxLayout();
+        left->addWidget(name);
+        left->addWidget(addContactInput_);
+
+        layout->addLayout(left, Qt::AlignLeft);
+        layout->addWidget(buttons);
         addDialog_->setLayout(layout);
     }
 
@@ -123,10 +150,17 @@ void DetailWindow::showAddToContactsDialog()
 
 void DetailWindow::addToContacts()
 {
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+
     ContactManager cm;
     ContactManager::Contact contact;
-    contact.name = addContactInput_->text();
+    QString number;
+    QString street;
+    getDetails(street, number,
+               contact.zipCode, contact.city, contact.street);
+    ContactManager::stringToName(addContactInput_->text(), contact.name);
     contact.number = numberButton_->valueText();
+    contact.country = country_;
 
     addDialog_->hide();
 
@@ -139,6 +173,8 @@ void DetailWindow::addToContacts()
         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
     }
 
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+
 }
 
 void DetailWindow::copyToClipboard()
@@ -146,3 +182,124 @@ void DetailWindow::copyToClipboard()
     QApplication::clipboard()->setText(numberButton_->valueText());
     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
 }
+
+void DetailWindow::sendSMS()
+{
+    QString number = numberButton_->valueText();
+
+    if(number.isEmpty())
+    {
+        return;
+    }
+
+    QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.MessagingUI",
+                                                      "/com/nokia/MessagingUI",
+                                                      "com.nokia.MessagingUI",
+                                                      "messaging_ui_interface_start_sms");
+    QList<QVariant> arguments;
+
+    arguments.append(QVariant("sms:" + number));
+
+    msg.setArguments(arguments);
+
+    if(!QDBusConnection::systemBus().send(msg))
+    {
+        QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
+    }
+
+}
+
+void DetailWindow::openMaps()
+{
+    QString street = streetButton_->valueText();
+    QString city = cityButton_->valueText();
+
+    if(street.isEmpty() && city.isEmpty())
+    {
+        return;
+    }
+
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+
+    OviMaps maps;
+
+    OviMaps::Address addr;
+    QString streetAndNumber;
+    getDetails(addr.street, addr.number,
+               addr.zipCode, addr.city, streetAndNumber);
+    addr.country = country_;
+
+    //qDebug() << addr.street << addr.number << addr.zipCode << addr.city << addr.country;
+
+    if(!maps.openMaps(addr))
+    {
+        QMaemo5InformationBox::information(this, tr("Unable to find coordinates for address."));
+    }
+
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+}
+
+void DetailWindow::getDetails(QString& street, QString& streetNumber,
+                              QString& zip, QString& city, QString& streetAndNumber)
+{
+    int pos = 0;
+
+    QString streetVal = streetButton_->valueText();
+    streetVal = streetVal.replace("Str.", QString::fromUtf8("Straße"));
+    streetVal = streetVal.replace("str.", QString::fromUtf8("straße"));
+    streetAndNumber = streetVal;
+    QString cityVal = cityButton_->valueText();
+    city = cityVal;
+
+    QStringList words = streetVal.split(" ", QString::SkipEmptyParts);
+
+    static QRegExp numberCheck("([0-9-]+)");
+
+    bool numberFound = false;
+    bool numberSet = false;
+
+    for(int i = 0; i < words.size(); i++)
+    {
+        if(i > 0 && numberCheck.exactMatch(words.at(i)))
+        {
+            numberFound = true;
+        }
+
+        if(numberFound)
+        {
+            if(!numberSet)
+            {
+                streetNumber = words.at(i);
+                numberSet = true;
+            }
+        }
+        else
+        {
+            street += words.at(i) + " ";
+        }
+    }
+
+    if(streetNumber.isEmpty())
+    {
+        static QRegExp addrCheck(" ([0-9]+)");
+
+        if((pos = addrCheck.indexIn(street)) != -1)
+        {
+            streetNumber = addrCheck.cap(1);
+            street = street.left(pos);
+        }
+    }
+
+    streetNumber = streetNumber.trimmed();
+    street = street.trimmed();
+
+    if((pos = cityVal.indexOf(" ")) > 0)
+    {
+        if(numberCheck.exactMatch(cityVal.left(pos)))
+        {
+            zip = cityVal.left(pos);
+            city = cityVal.mid(pos + 1);
+        }
+    }
+
+}