...
[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
94     if(data["name"].toString().isEmpty())
95     {
96         emit openSearch(data["number"].toString());
97     }
98     else
99     {
100         Source::Result details;
101         details.name = data["name"].toString();
102         details.street = data["street"].toString();
103         details.city = data["city"].toString();
104         details.number = data["number"].toString();
105         details.country = data["country"].toString();
106
107         emit logItemSelected(details);
108     }
109 }
110
111 QWidget* LogWindow::createWidget(Cache::LogDetails const& details)
112 {
113     QWidget* widget = new QWidget;
114     QHBoxLayout* layout = new QHBoxLayout;
115
116     QIcon icon;
117
118     if(details.missed)
119     {
120         icon = QIcon::fromTheme("general_missed");
121     }
122     else
123     {
124         icon = QIcon::fromTheme("general_received");
125     }
126
127     QLabel* label = new QLabel;
128     label->setPixmap(icon.pixmap(48, 48));
129
130     layout->addWidget(label);
131
132     QVBoxLayout* content = new QVBoxLayout;
133
134     QString text;
135
136     if(details.result.name.isEmpty())
137     {
138         text = tr("%1 (no search results)").arg(details.result.number);
139     }
140     else
141     {
142         text = details.result.name;
143
144         if(!details.result.street.isEmpty())
145         {
146             text += ", " + details.result.street;
147         }
148
149         if(!details.result.city.isEmpty())
150         {
151             text += ", " + details.result.city;
152         }
153     }
154
155     QLabel* nameLabel = new QLabel(text);
156
157     QDateTime date = QDateTime::fromTime_t(details.time);
158     QLabel* dateLabel = new QLabel(date.toString(Qt::SystemLocaleShortDate));
159     QFont font;
160     font.setPointSize(11);
161     dateLabel->setFont(font);
162
163     content->addWidget(nameLabel);
164     content->addWidget(dateLabel);
165
166     layout->addLayout(content, Qt::AlignLeft);
167
168     widget->setLayout(layout);
169
170     return widget;
171 }
172
173 void LogWindow::clearLog()
174 {
175     Cache::instance().clearLog();
176
177     loadLogItems();
178 }