Cosmetic fixes to detail view and other windows.
[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 <QtDBus/QDBusConnection>
20 #include <QtDBus/QDBusMessage>
21 #include <QtGui/QMessageBox>
22 #include <QtGui/QLabel>
23 #include <QtGui/QClipboard>
24 #include <QtGui/QDialogButtonBox>
25 #include <QMaemo5ValueButton>
26 #include <QMaemo5InformationBox>
27 #include <QApplication>
28 #include <QDebug>
29 #include "detailwindow.h"
30 #include "contactmanager.h"
31
32 DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0)
33 {
34     setAttribute(Qt::WA_Maemo5StackedWindow);
35     area_ = new QWidget(this);
36     layout_ = new QVBoxLayout;
37     QHBoxLayout* top = new QHBoxLayout;
38     QHBoxLayout* bottom = new QHBoxLayout;
39     QHBoxLayout* actions1 = new QHBoxLayout;
40     QHBoxLayout* actions2 = new QHBoxLayout;
41
42     QPushButton* addButton = new QPushButton(QIcon::fromTheme("general_contacts"), tr("Add to contacts"));
43     QPushButton* copyButton = new QPushButton(tr("Copy number to clipboard"));
44     QPushButton* callButton = new QPushButton(QIcon::fromTheme("general_call"), tr("Call"));
45     QPushButton* smsButton = new QPushButton(QIcon::fromTheme("general_sms"), tr("Send SMS"));
46
47     connect(addButton, SIGNAL(pressed()), this, SLOT(showAddToContactsDialog()));
48     connect(copyButton, SIGNAL(pressed()), this, SLOT(copyToClipboard()));
49     connect(callButton, SIGNAL(pressed()), this, SLOT(makeCall()));
50     connect(smsButton, SIGNAL(pressed()), this, SLOT(sendSMS()));
51
52     nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
53                                          tr("Name"), this);
54     streetButton_ = new QMaemo5ValueButton(tr("Street"), this);
55     cityButton_ = new QMaemo5ValueButton(tr("City"), this);
56     numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
57                                            tr("Phone number"), this);
58
59     connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
60
61     top->addWidget(nameButton_);
62     bottom->addWidget(streetButton_);
63     top->addWidget(numberButton_);
64     bottom->addWidget(cityButton_);
65     actions1->addWidget(callButton);
66     actions1->addWidget(smsButton);
67     actions2->addWidget(addButton);
68     actions2->addWidget(copyButton);
69     layout_->addLayout(top);
70     layout_->addLayout(bottom);
71     layout_->addLayout(actions1);
72     layout_->addLayout(actions2);
73     area_->setLayout(layout_);
74     setCentralWidget(area_);
75 }
76
77 void DetailWindow::loadData(Eniro::Result const& details)
78 {
79     setWindowTitle(details.name);
80     nameButton_->setValueText(details.name);
81     streetButton_->setValueText(details.street);
82     cityButton_->setValueText(details.city);
83     numberButton_->setValueText(details.number);
84     show();
85 }
86
87 void DetailWindow::makeCall()
88 {
89     QString number = numberButton_->valueText();
90
91     if(number.isEmpty())
92     {
93         return;
94     }
95
96     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
97                                                       "/com/nokia/csd/call",
98                                                       "com.nokia.csd.Call",
99                                                       "CreateWith");
100     QList<QVariant> arguments;
101
102     arguments.append(QVariant(number));
103     arguments.append(QVariant(0));
104
105     msg.setArguments(arguments);
106
107     if(!QDBusConnection::systemBus().send(msg))
108     {
109         QMessageBox::critical(this, tr("Error"), tr("Unable make call"));
110     }
111
112 }
113
114 void DetailWindow::showAddToContactsDialog()
115 {
116     if(!addDialog_)
117     {
118         addDialog_ = new QDialog(this);
119         addDialog_->setWindowTitle(tr("Add to contacts"));
120         addContactInput_ = new QLineEdit();
121         QHBoxLayout* layout = new QHBoxLayout();
122         QLabel* name = new QLabel(tr("Name"));
123         QPushButton* button = new QPushButton(tr("Add"));
124
125         QDialogButtonBox* buttons = new QDialogButtonBox;
126         buttons->setCenterButtons(false);
127         buttons->addButton(button, QDialogButtonBox::AcceptRole);
128         connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
129
130         QHBoxLayout* left = new QHBoxLayout();
131         left->addWidget(name);
132         left->addWidget(addContactInput_);
133
134         layout->addLayout(left, Qt::AlignLeft);
135         layout->addWidget(buttons);
136         addDialog_->setLayout(layout);
137     }
138
139     addContactInput_->setText(nameButton_->valueText());
140     addDialog_->show();
141 }
142
143 void DetailWindow::addToContacts()
144 {
145     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
146
147     ContactManager cm;
148     ContactManager::Contact contact;
149     contact.name = addContactInput_->text();
150     contact.number = numberButton_->valueText();
151
152     addDialog_->hide();
153
154     if(cm.addContact(contact))
155     {
156         QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
157     }
158     else
159     {
160         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
161     }
162
163     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
164
165 }
166
167 void DetailWindow::copyToClipboard()
168 {
169     QApplication::clipboard()->setText(numberButton_->valueText());
170     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
171 }
172
173 void DetailWindow::sendSMS()
174 {
175     QString number = numberButton_->valueText();
176
177     if(number.isEmpty())
178     {
179         return;
180     }
181
182     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.MessagingUI",
183                                                       "/com/nokia/MessagingUI",
184                                                       "com.nokia.MessagingUI",
185                                                       "messaging_ui_interface_start_sms");
186     QList<QVariant> arguments;
187
188     arguments.append(QVariant("sms:" + number));
189
190     msg.setArguments(arguments);
191
192     if(!QDBusConnection::systemBus().send(msg))
193     {
194         QMessageBox::critical(this, tr("Error"), tr("Unable to open SMS application"));
195     }
196
197 }