Better cache handling. Added clear cache button to settings.
[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 <QMaemo5ValueButton>
20 #include <QMaemo5InformationBox>
21 #include <QApplication>
22 #include <QtDBus/QDBusConnection>
23 #include <QtDBus/QDBusMessage>
24 #include <QtGui/QMessageBox>
25 #include <QtGui/QLabel>
26 #include <QtGui/QClipboard>
27 #include <QDebug>
28 #include "detailwindow.h"
29 #include "contactmanager.h"
30
31 DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0)
32 {
33     setAttribute(Qt::WA_Maemo5StackedWindow);
34     area_ = new QScrollArea(this);
35     layout_ = new QVBoxLayout;
36     QHBoxLayout* top = new QHBoxLayout;
37     QHBoxLayout* bottom = new QHBoxLayout;
38
39     QPushButton* addButton = new QPushButton(QIcon::fromTheme("general_contacts"), tr("Add to contacts"));
40     QPushButton* copyButton = new QPushButton(tr("Copy number to clipboard"));
41
42     connect(addButton, SIGNAL(pressed()), this, SLOT(showAddToContactsDialog()));
43     connect(copyButton, SIGNAL(pressed()), this, SLOT(copyToClipboard()));
44
45     nameButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_default_avatar"),
46                                          tr("Name"), this);
47     streetButton_ = new QMaemo5ValueButton(tr("Street"), this);
48     cityButton_ = new QMaemo5ValueButton(tr("City"), this);
49     numberButton_ = new QMaemo5ValueButton(QIcon::fromTheme("general_call"),
50                                            tr("Phone number"), this);
51
52     connect(numberButton_, SIGNAL(pressed()), this, SLOT(makeCall()));
53
54     top->addWidget(nameButton_);
55     bottom->addWidget(streetButton_);
56     top->addWidget(numberButton_);
57     bottom->addWidget(cityButton_);
58     layout_->addLayout(top);
59     layout_->addLayout(bottom);
60     layout_->addWidget(addButton);
61     layout_->addWidget(copyButton);
62     area_->setLayout(layout_);
63     setCentralWidget(area_);
64 }
65
66 void DetailWindow::loadData(Eniro::Result const& details)
67 {
68     setWindowTitle(details.name);
69     nameButton_->setValueText(details.name);
70     streetButton_->setValueText(details.street);
71     cityButton_->setValueText(details.city);
72     numberButton_->setValueText(details.number);
73     layout_->update();
74     show();
75 }
76
77 void DetailWindow::makeCall()
78 {
79     QString number = numberButton_->valueText();
80
81     if(number.isEmpty())
82     {
83         return;
84     }
85
86     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.csd",
87                                                       "/com/nokia/csd/call",
88                                                       "com.nokia.csd.Call",
89                                                       "CreateWith");
90     QList<QVariant> arguments;
91
92     arguments.append(QVariant(number));
93     arguments.append(QVariant(0));
94
95     msg.setArguments(arguments);
96
97     QDBusConnection::systemBus().send(msg);
98
99 }
100
101 void DetailWindow::showAddToContactsDialog()
102 {
103     if(!addDialog_)
104     {
105         addDialog_ = new QDialog(this);
106         addDialog_->setWindowTitle(tr("Add to contacts"));
107         addContactInput_ = new QLineEdit();
108         QHBoxLayout* layout = new QHBoxLayout();
109         QLabel* name = new QLabel(tr("Name"));
110         QPushButton* button = new QPushButton(tr("Add"));
111         connect(button, SIGNAL(pressed()), this, SLOT(addToContacts()));
112         layout->addWidget(name);
113         layout->addWidget(addContactInput_);
114         layout->addWidget(button);
115         addDialog_->setLayout(layout);
116     }
117
118     addContactInput_->setText(nameButton_->valueText());
119     addDialog_->show();
120 }
121
122 void DetailWindow::addToContacts()
123 {
124     ContactManager cm;
125     ContactManager::Contact contact;
126     contact.name = addContactInput_->text();
127     contact.number = numberButton_->valueText();
128
129     addDialog_->hide();
130
131     if(cm.addContact(contact))
132     {
133         QMaemo5InformationBox::information(this, tr("Contact was successfully added to contacts."));
134     }
135     else
136     {
137         QMessageBox::critical(this, tr("Error"), tr("Unable to add contact."));
138     }
139
140 }
141
142 void DetailWindow::copyToClipboard()
143 {
144     QApplication::clipboard()->setText(numberButton_->valueText());
145     QMaemo5InformationBox::information(this, tr("Number was successfully copied to clipboard."));
146 }