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