41971115d392f3e55805af4ecc94a9aef4cf000d
[jenirok] / src / gui / logwindow.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 <QtCore/QDateTime>
20 #include <QtCore/QDebug>
21 #include <QtGui/QLabel>
22 #include <QtGui/QHBoxLayout>
23 #include <QtGui/QVBoxLayout>
24 #include <QtGui/QMenuBar>
25 #include "logwindow.h"
26
27 LogWindow::LogWindow(QWidget* parent): QMainWindow(parent), list_(0)
28 {
29     setAttribute(Qt::WA_Maemo5StackedWindow);
30     setWindowTitle(tr("Incoming call log"));
31     menuBar()->addAction(tr("Clear log"), this, SLOT(clearLog()));
32 }
33
34 void LogWindow::setVisible(bool visible)
35 {
36     if(visible)
37     {
38         loadLogItems();
39     }
40
41     QMainWindow::setVisible(visible);
42 }
43
44 void LogWindow::loadLogItems()
45 {
46     if(!list_)
47     {
48         list_ = new ListWidget(this);
49         setCentralWidget(list_);
50         connect(list_, SIGNAL(itemClicked(int)), this,
51                 SLOT(itemClicked(int)));
52     }
53     else
54     {
55         list_->clear();
56     }
57
58     QList<Cache::LogDetails> logList;
59
60     Cache::instance().getLogItems(logList, LOG_MAX_ITEMS);
61
62     if(logList.size() == 0)
63     {
64         QLabel* info = new QLabel(tr("There are currently no logged calls"));
65         info->setAlignment(Qt::AlignCenter);
66         setCentralWidget(info);
67         list_ = 0;
68     }
69     else
70     {
71         for(int i = 0; i < logList.size(); i++)
72         {
73             QMap <QString, QVariant> data;
74             data["name"] = QVariant(logList.at(i).result.name);
75             data["street"] = QVariant(logList.at(i).result.street);
76             data["city"] = QVariant(logList.at(i).result.city);
77             data["number"] = QVariant(logList.at(i).result.number);
78             data["country"] = QVariant(logList.at(i).result.country);
79
80             list_->addWidget(createWidget(logList.at(i)), data);
81         }
82     }
83 }
84
85 void LogWindow::itemClicked(int index)
86 {
87     if(!list_)
88     {
89         return;
90     }
91
92     QMap <QString, QVariant> data = list_->getData(index).toMap();
93     Source::Result details;
94     details.name = data["name"].toString();
95
96     if(details.name.isEmpty())
97     {
98         return;
99     }
100
101     details.street = data["street"].toString();
102     details.city = data["city"].toString();
103     details.number = data["number"].toString();
104     details.country = data["country"].toString();
105
106     emit logItemSelected(details);
107 }
108
109 QWidget* LogWindow::createWidget(Cache::LogDetails const& details)
110 {
111     QWidget* widget = new QWidget;
112     QHBoxLayout* layout = new QHBoxLayout;
113
114     QIcon icon;
115
116     if(details.missed)
117     {
118         icon = QIcon::fromTheme("general_missed");
119     }
120     else
121     {
122         icon = QIcon::fromTheme("general_received");
123     }
124
125     QLabel* label = new QLabel;
126     label->setPixmap(icon.pixmap(48, 48));
127
128     layout->addWidget(label);
129
130     QVBoxLayout* content = new QVBoxLayout;
131
132     QString text;
133
134     if(details.result.name.isEmpty())
135     {
136         text = tr("%1 (no search results)").arg(details.result.number);
137     }
138     else
139     {
140         text = details.result.name;
141
142         if(!details.result.street.isEmpty())
143         {
144             text += ", " + details.result.street;
145         }
146
147         if(!details.result.city.isEmpty())
148         {
149             text += ", " + details.result.city;
150         }
151     }
152
153     QLabel* nameLabel = new QLabel(text);
154
155     QDateTime date = QDateTime::fromTime_t(details.time);
156     QLabel* dateLabel = new QLabel(date.toString(Qt::SystemLocaleShortDate));
157     QFont font;
158     font.setPointSize(11);
159     dateLabel->setFont(font);
160
161     content->addWidget(nameLabel);
162     content->addWidget(dateLabel);
163
164     layout->addLayout(content, Qt::AlignLeft);
165
166     widget->setLayout(layout);
167
168     return widget;
169 }
170
171 void LogWindow::clearLog()
172 {
173     Cache::instance().clearLog();
174
175     loadLogItems();
176 }