5 #include <QMaemo5InformationBox>
7 #include "feedswindow.h"
8 #include "entrieswindow.h"
11 #define APPVERSION "0.0.4"
13 FeedsWindow::FeedsWindow(QWidget *) : QMainWindow(0) {
14 setAttribute(Qt::WA_Maemo5StackedWindow);
16 setWindowIcon(QIcon("/usr/share/icons/hicolor/64x64/apps/grr.png"));
18 settings = new QSettings("dufo.be", "grr");
20 menuBar()->addAction(tr("Settings"), this, SLOT(showSettings()));
21 menuBar()->addAction(tr("Sync now"), this, SLOT(sync()));
22 menuBar()->addAction(tr("About"), this, SLOT(about()));
24 QActionGroup *filter_group = new QActionGroup(this);
25 show_all = new QAction(tr("Show All"), filter_group);
26 show_all->setCheckable(true);
27 show_updated= new QAction(tr("Show Updated"), filter_group);
28 show_updated->setCheckable(true);
30 if(settings->value("show_updated", false).toBool())
31 show_updated->setChecked(true);
33 show_all->setChecked(true);
35 menuBar()->addActions(filter_group->actions());
37 setWindowTitle(APPNAME);
39 list = new QListView();
40 setCentralWidget(list);
41 list->setItemDelegate(new FeedListDelegate(this));
43 connect(list, SIGNAL(activated(const QModelIndex &)),
44 SLOT(feedSelected(const QModelIndex &)));
46 reader = new GoogleReader();
48 connect(reader, SIGNAL(updateUnreadComplete()), SLOT(feedsUpdated()));
49 connect(reader, SIGNAL(allReadChanged()), SLOT(refreshModel()));
50 connect(reader, SIGNAL(loginFailed(QString)), SLOT(loginFailed(QString)));
52 connect(show_updated, SIGNAL(toggled(bool)), SLOT(showUpdated(bool)));
54 if((settings->value("login", "").toString() == "") ||
55 (settings->value("passwd", "").toString() == "")) {
56 emit loginFailed("Incomplete username or password");
59 reader->setLogin(settings->value("login", "").toString());
60 reader->setPasswd(settings->value("passwd", "").toString());
65 FeedsWindow::~FeedsWindow() {
68 void FeedsWindow::showSettings() {
69 SettingsDialog settingsDialog(this, settings);
70 if(settingsDialog.exec() == QDialog::Accepted) {
72 reader->setLogin(settings->value("login", "").toString());
73 reader->setPasswd(settings->value("passwd", "").toString());
78 void FeedsWindow::sync() {
79 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
80 reader->updateSubscriptions();
81 reader->updateUnread();
84 void FeedsWindow::refreshModel() {
85 QList<Feed *>feeds = reader->getFeeds();
86 QAbstractItemModel *old_model = list->model();
87 FeedListModel *new_model = new FeedListModel(this, feeds, show_updated->isChecked());
88 list->setModel(new_model);
89 connect(show_updated, SIGNAL(toggled(bool)), new_model, SLOT(showUpdated(bool)));
93 void FeedsWindow::feedsUpdated() {
95 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
98 void FeedsWindow::feedSelected(const QModelIndex &index) {
99 Feed *f = qVariantValue<Feed *>(index.data());
100 EntriesWindow *w = new EntriesWindow(this, f);
104 void FeedsWindow::showUpdated(bool updated) {
105 settings->setValue("show_updated", updated);
108 void FeedsWindow::loginFailed(QString message) {
109 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
110 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
114 void FeedsWindow::about() {
115 QMessageBox::about(this, APPNAME " " APPVERSION, "");
118 int FeedListModel::rowCount(const QModelIndex &) const {
122 for(int i = 0; i < feed_list.size(); i++) {
123 if(feed_list.at(i)->unread || feed_list.at(i)->special)
128 size = feed_list.size();
133 QVariant FeedListModel::data(const QModelIndex &index, int role) const {
137 if(index.row() >= rowCount() || index.row() < 0) {
141 if(role == Qt::DisplayRole) {
144 for(i = 0, j = 0; i < feed_list.size(); i++) {
145 if(feed_list.at(i)->unread || feed_list.at(i)->special) j++;
147 return qVariantFromValue(feed_list.at(i));
151 return qVariantFromValue(feed_list.at(index.row()));
157 void FeedListModel::showUpdated(bool updated) {
159 show_updated = updated;
163 void FeedListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
164 const QModelIndex &index) const {
166 QStyledItemDelegate::paint(painter, option, index);
168 Feed *f = qVariantValue<Feed *>(index.data());
170 QRect rect = option.rect;
171 rect.adjust(20, 8, -20, -8);
172 QFont font = option.font;
173 int flags = Qt::AlignVCenter;
177 if(f->cat_label != "")
178 flags = Qt::AlignTop;
181 if(!(option.state & QStyle::State_Selected))
182 painter->setPen(option.palette.highlight().color());
184 painter->drawText(rect, flags | Qt::AlignRight, QString("%1").arg(f->unread));
187 painter->drawText(rect, flags | Qt::AlignLeft, f->title);
189 if(f->cat_label != "") {
190 painter->setPen(option.palette.mid().color());
191 font.setPointSizeF(font.pointSizeF() * 0.70);
192 painter->setFont(font);
193 painter->drawText(rect, Qt::AlignBottom | Qt::AlignLeft, f->cat_label);
199 SettingsDialog::SettingsDialog(QWidget *parent, QSettings *s) : QDialog(parent) {
202 loginLabel = new QLabel(tr("Login:"));
203 loginEdit = new QLineEdit(settings->value("login", "").toString());
204 loginLabel->setBuddy(loginEdit);
206 passwdLabel = new QLabel(tr("Password:"));
207 passwdEdit = new QLineEdit(settings->value("passwd", "").toString());
208 passwdEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
209 passwdLabel->setBuddy(passwdEdit);
211 saveButton = new QPushButton(tr("Save"));
212 saveButton->setDefault(true);
213 cancelButton = new QPushButton(tr("Cancel"));
214 cancelButton->setDefault(false);
216 buttonBox = new QDialogButtonBox(Qt::Vertical);
217 buttonBox->addButton(cancelButton, QDialogButtonBox::ActionRole);
218 buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
220 QHBoxLayout *loginLayout = new QHBoxLayout;
221 loginLayout->addWidget(loginLabel);
222 loginLayout->addWidget(loginEdit);
224 QHBoxLayout *passwdLayout = new QHBoxLayout;
225 passwdLayout->addWidget(passwdLabel);
226 passwdLayout->addWidget(passwdEdit);
228 QVBoxLayout *leftLayout = new QVBoxLayout;
229 leftLayout->addLayout(loginLayout);
230 leftLayout->addLayout(passwdLayout);
231 leftLayout->addStretch(1);
233 QGridLayout *mainLayout = new QGridLayout;
234 mainLayout->setSizeConstraint(QLayout::SetFixedSize);
235 mainLayout->addLayout(leftLayout, 0, 0);
236 mainLayout->addWidget(buttonBox, 0, 1);
237 setLayout(mainLayout);
239 setWindowTitle(tr("Settings"));
241 connect(saveButton, SIGNAL(clicked()), SLOT(accept()));
242 connect(cancelButton, SIGNAL(clicked()), SLOT(reject()));
245 void SettingsDialog::accept() {
246 settings->setValue("login", loginEdit->text());
247 settings->setValue("passwd", passwdEdit->text());