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/>.
21 #include <QMessageBox>
23 #include <QTranslator>
25 #include <QTreeWidget>
28 #include <QListWidget>
30 #include <QCoreApplication>
31 #include <QApplication>
32 #include <QMessageBox>
33 #include <QTextStream>
39 check_path(const QStringList
& paths
, const QString
& exec
) {
40 QList
<QString
>::const_iterator iter
;
43 for (iter
=paths
.constBegin();
44 iter
!=paths
.constEnd();
49 .arg(QDir::fromNativeSeparators(*iter
))
52 tmp
= QDir::toNativeSeparators(tmp
);
54 if (QFile::exists(tmp
)) {
63 verify_version_output(
76 proc
.setEnvironment(QStringList() << "CCLIVE_NO_CONFIG=1");
77 proc
.setProcessChannelMode(QProcess::MergedChannels
);
78 proc
.start(path
, QStringList() << "--version");
81 *isCcliveFlag
= false;
83 if (!proc
.waitForFinished()) {
84 throw NoCcliveException(path
,
85 proc
.exitCode(), proc
.errorString());
88 const QString output
=
89 QString::fromLocal8Bit( proc
.readAll() );
95 const QString versionOutput
= output
;
96 // qDebug() << versionOutput;
98 const QStringList tmp
=
99 versionOutput
.split("\n", QString::SkipEmptyParts
);
101 bool enoughOutputFlag
= false;
103 if (tmp
.size() >= 1) {
105 const QStringList lst
=
106 tmp
[0].split(" ", QString::SkipEmptyParts
);
108 if (lst
.size() >= 6) {
110 *isCcliveFlag
= (lst
[0] == "cclive");
116 enoughOutputFlag
= true;
120 if (!enoughOutputFlag
) {
121 throw NoCcliveException(path
,
123 tr("Not c/clive or it is an unsupported "
128 throw NoCcliveException(path
, exitCode
, output
);
141 const QStringList env
=
142 QProcess::systemEnvironment();
144 QRegExp
re("^PATH=(.*)", Qt::CaseInsensitive
);
147 const QString capt
= re
.capturedTexts()[1].simplified();
148 QStringList paths
= capt
.split(":");
150 if (paths
.size() < 2) // w32
151 paths
= capt
.split(";");
153 if (paths
.size() < 2)
156 path
= check_path(paths
, "cclive");
158 path
= check_path(paths
, "clive");
160 if (path
.isEmpty()) {
161 // Detection from $PATH failed. Prompt user
162 // to specify the path in preferences manually.
163 throw NoCcliveException(
165 "Neither cclive or clive not was found in the path.\n"
166 "Please specify the path to either command in the\n"
172 // Check --version output.
173 verify_version_output(
184 Util::verifyCclivePath(
189 bool *isCcliveFlag
/*=NULL*/)
191 verify_version_output(
202 const QTreeWidget
*w
,
203 const Qt::CheckState
& st
,
204 const int column
/*=0*/)
206 QTreeWidgetItemIterator
iter(const_cast<QTreeWidget
*>(w
));
209 if ((*iter
)->childCount() == 0)
210 (*iter
)->setCheckState(column
, st
);
216 Util::invertAllCheckableItems(
217 const QTreeWidget
*w
,
218 const int column
/*=0*/)
220 QTreeWidgetItemIterator
iter(const_cast<QTreeWidget
*>(w
));
222 if ((*iter
)->childCount() == 0) {
223 (*iter
)->setCheckState(column
,
224 (*iter
)->checkState(column
) == Qt::Checked
234 Util::appendLog(const QTextEdit
* w
, const QString
& s
) {
235 QDateTime now
= QDateTime::currentDateTime();
236 QString dt
= now
.toString(Qt::ISODate
);
237 const_cast<QTextEdit
*>(w
)->append(dt
+": "+s
);
241 Util::countItems(const QTreeWidget
*w
) {
243 QTreeWidgetItemIterator
iter(const_cast<QTreeWidget
*>(w
));
252 Util::addItem(const QListWidget
*w
, QString lnk
) {
253 lnk
= lnk
.simplified();
258 if (!lnk
.startsWith("http://", Qt::CaseInsensitive
))
259 lnk
.insert(0, "http://");
261 QList
<QListWidgetItem
*> matched
=
262 w
->findItems(lnk
, Qt::MatchExactly
);
264 if (matched
.size() == 0)
265 const_cast<QListWidget
*>(w
)->addItem(lnk
);
269 Util::removeSelectedItems(const QWidget
*parent
, const QListWidget
*w
) {
270 QList
<QListWidgetItem
*> s
= w
->selectedItems();
272 const int size
= s
.size();
277 if (QMessageBox::warning(
278 const_cast<QWidget
*>(parent
),
279 QCoreApplication::applicationName(),
280 QObject::tr("Really remove the selected links?"),
281 QMessageBox::Yes
|QMessageBox::No
, QMessageBox::No
)
287 for (int i
=0; i
<size
; ++i
)
288 delete const_cast<QListWidget
*>(w
)->takeItem( w
->row(s
[i
]) );
292 Util::clearItems(const QWidget
*parent
, const QListWidget
*w
) {
296 if (QMessageBox::warning(
297 const_cast<QWidget
*>(parent
),
298 QCoreApplication::applicationName(),
299 QObject::tr("Really clear list?"),
300 QMessageBox::Yes
|QMessageBox::No
, QMessageBox::No
)
306 const_cast<QListWidget
*>(w
)->clear();
310 Util::paste(const QListWidget
*w
) {
311 QClipboard
*cb
= QApplication::clipboard();
312 QStringList lst
= cb
->text().split("\n");
313 const int size
= lst
.size();
315 for (int i
=0; i
<size
; ++i
)
316 Util::addItem(w
, lst
[i
]);
321 const QWidget
*parent
,
324 QMessageBox::critical(
325 const_cast<QWidget
*>(parent
),
326 QCoreApplication::applicationName(),
332 const QWidget
*parent
,
338 if (!file
.open(QFile::ReadOnly
| QIODevice::Text
)) {
340 QString( QObject::tr("File open error %1") ).arg( file
.error() ));
344 QTextStream
f(&file
);
347 lst
.append(f
.readLine().simplified());
354 const QWidget
*parent
,
355 const QListWidget
*w
,
361 QFlags
<QIODevice::OpenModeFlag
> flags
=
362 (QFile::WriteOnly
| QIODevice::Text
);
367 : QIODevice::Truncate
;
369 if (!file
.open(flags
)) {
371 QString( QObject::tr("File open error %1") ).arg( file
.error() ));
375 QTextStream
out(&file
);
377 const int size
= w
->count();
378 for (int i
=0; i
<size
; ++i
)
379 out
<< w
->item(i
)->text() << "\n";
384 NoCcliveException::NoCcliveException(const QString
& errmsg
)
389 static const QString defmsg
=
390 QObject::tr("Undefined path to c/clive command");
392 NoCcliveException::NoCcliveException(
394 const QString
& errmsg
)
397 if (!path
.isEmpty()) {
399 QString("error: %1:\n%2").arg(path
).arg(errmsg
);
403 NoCcliveException::NoCcliveException(
406 const QString
& output
)
409 if (!path
.isEmpty()) {
410 this->errmsg
= QString(
412 tr("%1 exited with %2:\n%3")
421 NoCcliveException::what() const {