httpmgr: fix: emit finished if != http/200.
[abby.git] / src / util.cpp
blobe9485855f81b1973a1fb609212ff8f77ecb2c1cf
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/>.
19 #include <QString>
20 #include <QProcess>
21 #include <QMessageBox>
22 #include <QFile>
23 #include <QTranslator>
24 #include <QDir>
25 #include <QTreeWidget>
26 #include <QTextEdit>
27 #include <QDateTime>
28 //#include <QDebug>
30 #include "util.h"
32 static QString
33 check_path(const QStringList& paths, const QString& exec) {
34 QList<QString>::const_iterator iter;
35 QString path;
37 for (iter=paths.constBegin();
38 iter!=paths.constEnd();
39 ++iter)
41 QString tmp =
42 QString( "%1/%2" )
43 .arg(QDir::fromNativeSeparators(*iter))
44 .arg(exec);
46 tmp = QDir::toNativeSeparators(tmp);
48 if (QFile::exists(tmp)) {
49 path = tmp;
50 break;
53 return path;
56 static void
57 verify_version_output(
58 const QString& path,
59 QString& version,
60 QString& libVersion,
61 QString& libName,
62 bool *isCcliveFlag)
65 version.clear();
66 libVersion.clear();
67 libName.clear();
69 QProcess proc;
70 proc.setEnvironment(QStringList() << "CCLIVE_NO_CONFIG=1");
71 proc.setProcessChannelMode(QProcess::MergedChannels);
72 proc.start(path, QStringList() << "--version");
74 if (isCcliveFlag)
75 *isCcliveFlag = false;
77 if (!proc.waitForFinished()) {
78 throw NoCcliveException(path,
79 proc.exitCode(), proc.errorString());
81 else {
82 const QString output =
83 QString::fromLocal8Bit( proc.readAll() );
85 const int exitCode =
86 proc.exitCode();
88 if (exitCode == 0) {
89 const QString versionOutput = output;
90 // qDebug() << versionOutput;
92 const QStringList tmp =
93 versionOutput.split("\n", QString::SkipEmptyParts);
95 bool enoughOutputFlag = false;
97 if (tmp.size() >= 1) {
99 const QStringList lst =
100 tmp[0].split(" ", QString::SkipEmptyParts);
102 if (lst.size() >= 6) {
103 if (isCcliveFlag)
104 *isCcliveFlag = (lst[0] == "cclive");
106 version = lst[2];
107 libName = lst[4];
108 libVersion = lst[6];
110 enoughOutputFlag = true;
114 if (!enoughOutputFlag) {
115 throw NoCcliveException(path,
116 QObject::
117 tr("Not c/clive or it is an unsupported "
118 "version of it"));
121 else
122 throw NoCcliveException(path, exitCode, output);
126 void
127 Util::detectCclive(
128 QString& path,
129 QString& version,
130 QString& libVersion,
131 QString& libName,
132 bool *isCcliveFlag)
135 const QStringList env =
136 QProcess::systemEnvironment();
138 QRegExp re("^PATH=(.*)", Qt::CaseInsensitive);
139 env.indexOf(re);
141 const QString capt = re.capturedTexts()[1].simplified();
142 QStringList paths = capt.split(":");
144 if (paths.size() < 2) // w32
145 paths = capt.split(";");
147 if (paths.size() < 2)
148 return;
150 path = check_path(paths, "cclive");
151 if (path.isEmpty())
152 path = check_path(paths, "clive");
154 if (path.isEmpty()) {
155 // Detection from $PATH failed. Prompt user
156 // to specify the path in preferences manually.
157 throw NoCcliveException(
158 QObject::tr(
159 "Neither cclive or clive not was found in the path.\n"
160 "Please specify the path to either command in the\n"
161 "preferences."
165 else {
166 // Check --version output.
167 verify_version_output(
168 path,
169 version,
170 libVersion,
171 libName,
172 isCcliveFlag
177 void
178 Util::verifyCclivePath(
179 const QString& path,
180 QString& version,
181 QString& libVersion,
182 QString& libName,
183 bool *isCcliveFlag/*=NULL*/)
185 verify_version_output(
186 path,
187 version,
188 libVersion,
189 libName,
190 isCcliveFlag
194 void
195 Util::checkAllItems(
196 const QTreeWidget *w,
197 const Qt::CheckState& st,
198 const int column/*=0*/)
200 QTreeWidgetItemIterator iter(const_cast<QTreeWidget*>(w));
201 while (*iter) {
202 (*iter)->setCheckState(column, st);
203 ++iter;
207 void
208 Util::invertAllCheckableItems(
209 const QTreeWidget *w,
210 const int column/*=0*/)
212 QTreeWidgetItemIterator iter(const_cast<QTreeWidget*>(w));
213 while (*iter) {
214 (*iter)->setCheckState(column,
215 (*iter)->checkState(column) == Qt::Checked
216 ? Qt::Unchecked
217 : Qt::Checked
219 ++iter;
223 void
224 Util::appendLog(const QTextEdit* w, const QString& s) {
225 QDateTime now = QDateTime::currentDateTime();
226 QString dt = now.toString(Qt::ISODate);
227 const_cast<QTextEdit*>(w)->append(dt+": "+s);
231 Util::countItems(const QTreeWidget *w) {
232 int count = 0;
233 QTreeWidgetItemIterator iter(const_cast<QTreeWidget*>(w));
234 while (*iter) {
235 ++count;
236 ++iter;
238 return count;
241 NoCcliveException::NoCcliveException(const QString& errmsg)
242 : errmsg(errmsg)
246 static const QString defmsg =
247 QObject::tr("Undefined path to c/clive command");
249 NoCcliveException::NoCcliveException(
250 const QString& path,
251 const QString& errmsg)
252 : errmsg(defmsg)
254 if (!path.isEmpty()) {
255 this->errmsg =
256 QString("error: %1:\n%2").arg(path).arg(errmsg);
260 NoCcliveException::NoCcliveException(
261 const QString& path,
262 const int& exitCode,
263 const QString& output)
264 : errmsg(defmsg)
266 if (!path.isEmpty()) {
267 this->errmsg = QString(
268 QObject::
269 tr("%1 exited with %2:\n%3")
270 .arg(path)
271 .arg(exitCode)
272 .arg(output)
277 const QString&
278 NoCcliveException::what() const {
279 return errmsg;