scan: log: flip switches to r/o etc.
[abby.git] / src / feedmgrdlg.cpp
blob446c8c3e0a19d300ed96398741490071ca659b2b
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 "feededitdlg.h"
25 #include "feedmgrdlg.h"
27 typedef unsigned int _uint;
29 FeedMgrDialog::FeedMgrDialog(QWidget *parent)
30 : QDialog(parent)
32 setupUi(this);
33 readSettings();
35 connect(itemsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
36 this, SLOT(onItemDoubleClicked(QTreeWidgetItem*,int)));
38 itemsTree->setColumnHidden(1, true);
41 void
42 FeedMgrDialog::onAdd() {
43 FeedEditDialog dlg(this);
44 if (dlg.exec() == QDialog::Accepted)
45 addLink(dlg.nameEdit->text(), dlg.linkEdit->text());
48 static int
49 confirm_remove(QWidget *parent) {
50 return QMessageBox::warning(
51 parent,
52 QObject::tr("Warning"),
53 QObject::tr("Really remove the selected links?"),
54 QMessageBox::Yes|QMessageBox::No,
55 QMessageBox::No
59 void
60 FeedMgrDialog::onRemove() {
62 bool ok = false;
64 QTreeWidgetItemIterator iter(itemsTree);
65 QList<QTreeWidgetItem*> lst;
66 while (*iter) {
67 if ((*iter)->checkState(0) == Qt::Checked) {
68 if (!ok) {
69 if (confirm_remove(this) == QMessageBox::No)
70 return;
71 ok = true;
73 lst << (*iter);
75 ++iter;
78 QList<QTreeWidgetItem*>::const_iterator i;
79 for (i=lst.constBegin(); i!=lst.constEnd(); ++i) {
80 itemsTree->removeItemWidget((*i), 0);
81 delete (*i);
85 void
86 FeedMgrDialog::onClear() {
88 if (itemsTree->topLevelItemCount() == 0)
89 return;
91 if (QMessageBox::warning(this, QCoreApplication::applicationName(),
92 tr("Really clear list?"),
93 QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
94 == QMessageBox::No)
96 return;
99 itemsTree->clear();
102 void
103 FeedMgrDialog::writeSettings() {
104 QSettings s;
106 s.beginGroup("FeedMgrDialog");
107 s.setValue("size", size());
109 s.beginWriteArray("feeds");
110 QTreeWidgetItemIterator iter(itemsTree);
111 for (register _uint i=0; (*iter); ++iter, ++i) {
112 s.setArrayIndex(i);
113 s.setValue("link",
114 QString("%1||%2") // Note: replaces "||" which we'll use as delim
115 .arg((*iter)->text(0).replace("||","|"))
116 .arg((*iter)->text(1).replace("||","|"))
119 s.endArray();
121 s.endGroup();
124 void
125 FeedMgrDialog::readSettings() {
127 QSettings s;
128 s.beginGroup("FeedMgrDialog");
129 resize( s.value("size", QSize(505,255)).toSize() );
131 const register _uint size = s.beginReadArray("feeds");
132 register _uint unnamed_count = 1;
134 for (register _uint i=0; i<size; ++i) {
135 s.setArrayIndex(i);
137 const QString entry =
138 s.value("link").toString();
140 QStringList tmp = entry.split("||");
142 if (tmp.size() < 2) {
143 tmp.prepend( // Name column was added in 0.4.5.
144 QString( tr("Unnamed feed #%1") )
145 .arg(unnamed_count++)
149 addLink(tmp[0], tmp[1]);
151 s.endArray();
153 s.endGroup();
156 void
157 FeedMgrDialog::onItemDoubleClicked(
158 QTreeWidgetItem *item,
159 int /*column*/)
161 FeedEditDialog dlg(this, item->text(0), item->text(1));
162 if (dlg.exec() == QDialog::Accepted) {
163 item->setText(0, dlg.nameEdit->text());
164 item->setText(1, dlg.linkEdit->text());
168 void
169 FeedMgrDialog::addLink(QString name, QString lnk) {
171 if (name.isEmpty())
172 return;
174 name = name.trimmed();
176 if (lnk.isEmpty())
177 return;
179 lnk = lnk.trimmed();
181 if (!lnk.startsWith("http://",Qt::CaseInsensitive))
182 lnk.insert(0,"http://");
184 QList<QTreeWidgetItem *> found =
185 itemsTree->findItems(lnk, Qt::MatchExactly, 1); // 1=url column
187 if (found.size() == 0) {
188 QTreeWidgetItem *item = new QTreeWidgetItem;
189 item->setCheckState(0, Qt::Unchecked);
190 item->setText(0, name);
191 item->setText(1, lnk);
192 itemsTree->addTopLevelItem(item);