2 * Copyright (C) 2010 Toni Gundogdu.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "Preferences.h"
24 #include "DownloadDiag.h"
27 extern SharedPreferences shPrefs
;
29 DownloadDialog::DownloadDialog (QWidget
*parent
/*=NULL*/)
30 : QProgressDialog (parent
), _canceled (false)
33 do { connect (&_proc, SIGNAL(s), this, SLOT(sl)); } while (0)
35 _wrap (started (), onCurlStarted ());
37 _wrap (error (QProcess::ProcessError
),
38 onCurlError (QProcess::ProcessError
));
40 _wrap (readyRead (), onCurlReadyRead ());
42 _wrap (finished (int, QProcess::ExitStatus
),
43 onCurlFinished (int, QProcess::ExitStatus
));
47 connect (this, SIGNAL(canceled()), this, SLOT (onCanceled()));
49 setWindowModality (Qt::WindowModal
);
52 _proc
.setProcessChannelMode (QProcess::MergedChannels
);
56 DownloadDialog::start (const QString
& cmd
, const QString
& fpath
, Media
*media
)
58 Q_ASSERT (!cmd
.isEmpty ());
59 Q_ASSERT (!fpath
.isEmpty ());
60 Q_ASSERT (media
!= NULL
);
64 QStringList args
= cmd
.split (" ");
66 args
.replaceInStrings ("%u", media
->get (Media::Link
).toString ());
67 args
.replaceInStrings ("%f", fpath
);
72 _proc
.start (args
.takeFirst (), args
);
77 DownloadDialog::onCurlStarted ()
79 setLabelText (tr ("Starting download ..."));
80 if (shPrefs
.get (SharedPreferences::MinWhenStarts
).toBool ())
81 parentWidget ()->showMinimized ();
85 DownloadDialog::onCurlError (QProcess::ProcessError n
)
90 NomNom::crit (parentWidget (), NomNom::to_process_errmsg (n
));
96 static const QRegExp
rx_err ("curl:\\s+(.*)$");
97 static const QRegExp
rx_rate ("(\\D+)"); // rate unit
100 DownloadDialog::onCurlReadyRead ()
103 static char data
[1024];
105 while (_proc
.readLine (data
, sizeof (data
)))
107 QString ln
= QString::fromLocal8Bit (data
).simplified ();
109 if (rx_err
.indexIn (ln
) != -1)
111 _lastError
= "curl: " +rx_err
.cap (1);
115 QStringList lst
= ln
.split (" ");
117 if (lst
.count () < 12)
118 continue; // Full line updates only.
131 setValue (lst
[PERCENT
].toInt ());
133 QString rate
= lst
[RATE
];
135 if (rx_rate
.indexIn (rate
) == -1)
136 rate
= QString ("%1k").arg (rate
.toLongLong ()/1024.0,2,'f',1);
138 const QString s
= tr("Copying at %1, %2")
148 DownloadDialog::onCurlFinished (int exitCode
, QProcess::ExitStatus exitStatus
)
150 if (exitStatus
== QProcess::NormalExit
&& exitCode
== 0)
157 NomNom::crit (parentWidget (), _lastError
);
166 DownloadDialog::onCanceled ()
170 if (_proc
.state () == QProcess::Running
)
174 // vim: set ts=2 sw=2 tw=72 expandtab: