~ubuntu-branches/ubuntu/karmic/kid3/karmic

« back to all changes in this revision

Viewing changes to kid3/downloaddialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-20 16:12:30 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090520161230-qetp532r8ydujkz2
Tags: upstream-1.2
Import upstream version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file downloaddialog.cpp
 
3
 * Dialog displayed during a download.
 
4
 *
 
5
 * \b Project: Kid3
 
6
 * \author Urs Fleisch
 
7
 * \date 31 Dec 2008
 
8
 *
 
9
 * Copyright (C) 2008-2009  Urs Fleisch
 
10
 *
 
11
 * This file is part of Kid3.
 
12
 *
 
13
 * Kid3 is free software; you can redistribute it and/or modify
 
14
 * it under the terms of the GNU General Public License as published by
 
15
 * the Free Software Foundation; either version 2 of the License, or
 
16
 * (at your option) any later version.
 
17
 *
 
18
 * Kid3 is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
 * GNU General Public License for more details.
 
22
 *
 
23
 * You should have received a copy of the GNU General Public License
 
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
25
 */
 
26
 
 
27
#include "downloaddialog.h"
 
28
#include "httpclient.h"
 
29
#include <qstring.h>
 
30
#include "qtcompatmac.h"
 
31
 
 
32
/**
 
33
 * Constructor.
 
34
 *
 
35
 * @param parent  parent widget
 
36
 * @param caption dialog title
 
37
 */
 
38
DownloadDialog::DownloadDialog(QWidget* parent, const QString& caption) :
 
39
        QProgressDialog(parent), m_client(0)
 
40
{
 
41
        QCM_setWindowTitle(caption);
 
42
        connect(this, SIGNAL(canceled()),
 
43
                                        this, SLOT(cancelDownload()));
 
44
}
 
45
 
 
46
/**
 
47
 * Destructor.
 
48
 */
 
49
DownloadDialog::~DownloadDialog()
 
50
{
 
51
        delete m_client;
 
52
        m_client = 0;
 
53
}
 
54
 
 
55
/**
 
56
 * Display message in status bar.
 
57
 *
 
58
 * @param msg           status message
 
59
 * @param receivedBytes bytes received
 
60
 * @param totalBytes    total bytes
 
61
 */
 
62
void DownloadDialog::updateProgressStatus(const QString& msg,
 
63
                                                                                                                                                                        int receivedBytes, int totalBytes)
 
64
{
 
65
        setLabelText(m_url + '\n' + msg);
 
66
        if (receivedBytes >= 0 && totalBytes >= 0) {
 
67
#if QT_VERSION >= 0x040000
 
68
                setRange(0, totalBytes);
 
69
                setValue(receivedBytes);
 
70
#else
 
71
                setProgress(receivedBytes, totalBytes);
 
72
#endif
 
73
        }
 
74
}
 
75
 
 
76
/**
 
77
 * Send a download request.
 
78
 *
 
79
 * @param hostName server
 
80
 * @param path     path on server
 
81
 */
 
82
void DownloadDialog::startDownload(const QString& hostName, const QString& path)
 
83
{
 
84
        if (!m_client) {
 
85
                m_client = new HttpClient;
 
86
                connect(m_client, SIGNAL(bytesReceived(const QByteArray&)),
 
87
                                                this, SLOT(requestFinished(const QByteArray&)));
 
88
                connect(m_client, SIGNAL(progress(const QString&, int, int)),
 
89
                                                this, SLOT(updateProgressStatus(const QString&, int, int)));
 
90
        }
 
91
        if (m_client) {
 
92
                m_url = "http://";
 
93
                m_url += hostName;
 
94
                m_url += path;
 
95
                setLabelText(m_url);
 
96
                updateProgressStatus(i18n("Ready."),
 
97
                                     HttpClient::CS_RequestConnection,
 
98
                                     HttpClient::CS_EstimatedBytes);
 
99
                m_client->sendRequest(hostName, path);
 
100
        }
 
101
}
 
102
 
 
103
/**
 
104
 * Cancel a download.
 
105
 */
 
106
void DownloadDialog::cancelDownload()
 
107
{
 
108
        delete m_client;
 
109
        m_client = 0;
 
110
        reset();
 
111
}
 
112
 
 
113
/**
 
114
 * Handle response when request is finished.
 
115
 * downloadFinished() is emitted.
 
116
 *
 
117
 * @param data received data
 
118
 */
 
119
void DownloadDialog::requestFinished(const QByteArray& data)
 
120
{
 
121
        if (!wasCanceled()) {
 
122
                emit downloadFinished(data, m_client->getContentType(), m_url);
 
123
        }
 
124
}