Added very basic sharing functionality.
[grr] / src / entrieswindow.cpp
1 #include <QMenuBar>
2 #include <QDebug>
3 #include <QPainter>
4 #include "entrieswindow.h"
5 #include "contentwindow.h"
6
7 EntriesWindow::EntriesWindow(QWidget *parent, Feed *f) : QMainWindow(parent) {
8         setAttribute(Qt::WA_Maemo5StackedWindow);
9
10         feed = f;
11
12         menuBar()->addAction(tr("Fetch more"), this, SLOT(sync()));
13         menuBar()->addAction(tr("Mark all as read"), this, SLOT(markRead()));
14
15         QActionGroup *filter_group = new QActionGroup(this);
16         show_all = new QAction(tr("Show All"), filter_group);
17         show_all->setCheckable(true);
18         show_all->setChecked(true);
19         show_updated = new QAction(tr("Show Updated"), filter_group);
20         show_updated->setCheckable(true);
21         menuBar()->addActions(filter_group->actions());
22
23         setWindowTitle(f->title);
24
25         list = new QListView();
26         setCentralWidget(list);
27         list->setItemDelegate(new EntryListDelegate(this));
28
29         connect(list, SIGNAL(activated(const QModelIndex &)),
30                 SLOT(entrySelected(const QModelIndex &)));
31
32         connect(feed, SIGNAL(updateFeedComplete()),
33                 SLOT(entriesUpdated()));
34
35         if(feed->getEntriesSize() == 0 || feed->lastUpdated < feed->reader->lastUpdated)
36                 sync();
37         else
38                 entriesUpdated();
39 }
40
41 EntriesWindow::~EntriesWindow() {
42 }
43
44 void EntriesWindow::sync() {
45         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
46         feed->fetch(feed->lastUpdated > feed->reader->lastUpdated);
47 }
48
49 void EntriesWindow::markRead() {
50         feed->markRead();
51 }
52
53 void EntriesWindow::entriesUpdated() {
54         QList<Entry *>entries = feed->getEntries();
55         QAbstractItemModel *old_model = list->model();
56         EntryListModel *new_model = new EntryListModel(this, entries, show_updated->isChecked());
57         list->setModel(new_model);
58         connect(show_updated, SIGNAL(toggled(bool)), new_model, SLOT(showUpdated(bool)));
59         delete(old_model);
60         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
61 }
62
63 void EntriesWindow::entrySelected(const QModelIndex &index) {
64         Entry *e = qVariantValue<Entry *>(index.data());
65
66         ContentWindow *w = new ContentWindow(this, e);
67         w->show();
68 }
69
70 int EntryListModel::rowCount(const QModelIndex &) const {
71         int size = 0;
72         
73         if(show_updated) {
74                 for(int i = 0; i < entry_list.size(); i++) {
75                         if((entry_list.at(i)->flags & ENTRY_FLAG_READ) == 0)
76                                 size++;
77                 }
78         }
79         else
80                 size = entry_list.size();
81
82         return size;;
83 }
84
85 QVariant EntryListModel::data(const QModelIndex &index, int role) const {
86         if(!index.isValid())
87                 return QVariant();
88
89         if(index.row() >= rowCount() || index.row() < 0) {
90                 return QVariant();
91         }
92
93         if(role == Qt::DisplayRole) {
94                 if(show_updated) {
95                         int i, j;
96                         for(i = 0, j = 0; i < entry_list.size(); i++) {
97                                 if((entry_list.at(i)->flags & ENTRY_FLAG_READ) == 0) j++;
98                                 if(j > index.row())
99                                         return qVariantFromValue(entry_list.at(i));
100                         }
101                 }
102                 else
103                         return qVariantFromValue(entry_list.at(index.row()));
104         }
105
106         return QVariant();
107 }
108
109 void EntryListModel::showUpdated(bool updated) {
110         beginResetModel();
111         show_updated = updated;
112         endResetModel();
113 }
114
115 void EntryListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
116         const QModelIndex &index) const {
117     
118         QStyledItemDelegate::paint(painter, option, index);
119
120         Entry *e = qVariantValue<Entry *>(index.data());
121
122         QFont font = option.font;
123         QRect rect = option.rect;
124         rect.adjust(20, 8, -20, -8);
125         QPoint topleft = rect.topLeft();
126         QPoint bottomleft = rect.bottomLeft();
127         rect.adjust(36, 0, 0, 0);
128
129         painter->save();
130
131         if(((e->flags & ENTRY_FLAG_READ) == 0) &&
132            !(option.state & QStyle::State_Selected)) {
133                 painter->setPen(option.palette.highlight().color());
134         }
135
136         painter->drawText(rect, Qt::AlignTop | Qt::AlignLeft, e->title);
137
138         painter->setPen(option.palette.mid().color());
139         font.setPointSizeF(font.pointSizeF() * 0.70);
140         painter->setFont(font);
141
142         painter->drawText(rect, Qt::AlignBottom | Qt::AlignLeft, e->author);
143
144         QString date;
145         if(e->published.date() == QDateTime::currentDateTime().date())
146                 date = e->published.time().toString(Qt::DefaultLocaleShortDate);
147         else
148                 date = e->published.date().toString();
149
150         painter->drawText(rect, Qt::AlignBottom | Qt::AlignRight, date);
151
152         if(e->flags & ENTRY_FLAG_STARRED) {
153                 QImage img = QImage(QLatin1String(":/images/star-1"));
154                 painter->drawImage(topleft, img);
155         }
156
157         if(e->flags & ENTRY_FLAG_SHARED) {
158                 QImage img = QImage(QLatin1String(":/images/shared-1"));
159                 bottomleft.ry() -= img.height();
160                 painter->drawImage(bottomleft, img);
161         }
162
163         painter->restore();
164 }
165