update todo.
[abby.git] / src / feedmgrdlg.cpp
blob46bad96e6863a437bdc97a26dfe4994037da5855
1 /*
2 * abby Copyright (C) 2009 Toni Gundogdu.
3 * This file is part of abby.
5 * abby is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * abby is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <QDialog>
19 #include <QSettings>
20 #include <QInputDialog>
21 #include <QMessageBox>
22 //#include <QDebug>
24 #include "util.h"
25 #include "feededitdlg.h"
26 #include "feedmgrdlg.h"
28 typedef unsigned int _uint;
30 FeedMgrDialog::FeedMgrDialog(QWidget *parent)
31 : QDialog(parent)
33 setupUi(this);
34 readSettings();
36 connect(itemsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
37 this, SLOT(onItemDoubleClicked(QTreeWidgetItem*,int)));
39 itemsTree->setColumnHidden(1, true);
42 void
43 FeedMgrDialog::onAdd() {
44 FeedEditDialog dlg(this);
45 if (dlg.exec() == QDialog::Accepted)
46 addLink(dlg.nameEdit->text(), dlg.linkEdit->text());
49 static int
50 confirm_remove(QWidget *parent) {
51 return QMessageBox::warning(
52 parent,
53 QObject::tr("Warning"),
54 QObject::tr("Really remove the selected links?"),
55 QMessageBox::Yes|QMessageBox::No,
56 QMessageBox::No
60 void
61 FeedMgrDialog::onRemove() {
63 bool ok = false;
65 QTreeWidgetItemIterator iter(itemsTree);
66 QList<QTreeWidgetItem*> lst;
67 while (*iter) {
68 if ((*iter)->checkState(0) == Qt::Checked) {
69 if (!ok) {
70 if (confirm_remove(this) == QMessageBox::No)
71 return;
72 ok = true;
74 lst << (*iter);
76 ++iter;
79 QList<QTreeWidgetItem*>::const_iterator i;
80 for (i=lst.constBegin(); i!=lst.constEnd(); ++i) {
81 itemsTree->removeItemWidget((*i), 0);
82 delete (*i);
86 void
87 FeedMgrDialog::onClear() {
89 if (itemsTree->topLevelItemCount() == 0)
90 return;
92 if (QMessageBox::warning(this, QCoreApplication::applicationName(),
93 tr("Really clear list?"),
94 QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
95 == QMessageBox::No)
97 return;
100 itemsTree->clear();
103 void
104 FeedMgrDialog::writeSettings() {
105 QSettings s;
107 s.beginGroup("FeedMgrDialog");
108 s.setValue("size", size());
110 s.beginWriteArray("feeds");
111 QTreeWidgetItemIterator iter(itemsTree);
112 for (register _uint i=0; (*iter); ++iter, ++i) {
113 s.setArrayIndex(i);
114 s.setValue("link",
115 QString("%1||%2") // Note: replaces "||" which we'll use as delim
116 .arg((*iter)->text(0).replace("||","|"))
117 .arg((*iter)->text(1).replace("||","|"))
120 s.endArray();
122 s.endGroup();
125 void
126 FeedMgrDialog::readSettings() {
128 QSettings s;
129 s.beginGroup("FeedMgrDialog");
130 resize( s.value("size", QSize(505,255)).toSize() );
132 const register _uint size = s.beginReadArray("feeds");
133 register _uint unnamed_count = 1;
135 for (register _uint i=0; i<size; ++i) {
136 s.setArrayIndex(i);
138 const QString entry =
139 s.value("link").toString();
141 QStringList tmp = entry.split("||");
143 if (tmp.size() < 2) {
144 tmp.prepend( // Name column was added in 0.4.5.
145 QString( tr("Unnamed feed #%1") )
146 .arg(unnamed_count++)
150 addLink(tmp[0], tmp[1]);
152 s.endArray();
154 s.endGroup();
157 void
158 FeedMgrDialog::onItemDoubleClicked(
159 QTreeWidgetItem *item,
160 int /*column*/)
162 FeedEditDialog dlg(this, item->text(0), item->text(1));
163 if (dlg.exec() == QDialog::Accepted) {
164 item->setText(0, dlg.nameEdit->text());
165 item->setText(1, dlg.linkEdit->text());
169 void
170 FeedMgrDialog::addLink(QString name, QString lnk) {
172 name = name.simplified();
173 if (name.isEmpty())
174 return;
176 lnk = lnk.simplified();
177 if (lnk.isEmpty())
178 return;
180 if (!lnk.startsWith("http://",Qt::CaseInsensitive))
181 lnk.insert(0,"http://");
183 // 1=url
184 if (itemsTree->findItems(lnk, Qt::MatchExactly, 1).size() == 0) {
185 QTreeWidgetItem *item = new QTreeWidgetItem(itemsTree);
186 item->setCheckState(0, Qt::Unchecked);
187 item->setText(0, name);
188 item->setText(1, lnk);
192 void
193 FeedMgrDialog::onSelectAll() {
194 Util::checkAllItems(itemsTree, Qt::Checked);
197 void
198 FeedMgrDialog::onInvert() {
199 Util::invertAllCheckableItems(itemsTree);