1 | /* $Id: UIDownloaderUserManual.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * UIDownloaderUserManual class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Global includes */
|
---|
21 | #include <QAction>
|
---|
22 | #include <QDir>
|
---|
23 | #include <QFile>
|
---|
24 |
|
---|
25 | /* Local includes */
|
---|
26 | #include "UIDownloaderUserManual.h"
|
---|
27 | #include "QIFileDialog.h"
|
---|
28 | #include "QIHttp.h"
|
---|
29 | #include "VBoxProblemReporter.h"
|
---|
30 |
|
---|
31 | UIDownloaderUserManual *UIDownloaderUserManual::m_pInstance = 0;
|
---|
32 |
|
---|
33 | UIDownloaderUserManual *UIDownloaderUserManual::create()
|
---|
34 | {
|
---|
35 | if (!m_pInstance)
|
---|
36 | m_pInstance = new UIDownloaderUserManual;
|
---|
37 | return m_pInstance;
|
---|
38 | }
|
---|
39 |
|
---|
40 | UIDownloaderUserManual *UIDownloaderUserManual::current()
|
---|
41 | {
|
---|
42 | return m_pInstance;
|
---|
43 | }
|
---|
44 |
|
---|
45 | void UIDownloaderUserManual::destroy()
|
---|
46 | {
|
---|
47 | if (m_pInstance)
|
---|
48 | delete m_pInstance;
|
---|
49 | m_pInstance = 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void UIDownloaderUserManual::setSource(const QString &strSource)
|
---|
53 | {
|
---|
54 | /* Erase the list first: */
|
---|
55 | m_sourcesList.clear();
|
---|
56 | /* And add there passed value: */
|
---|
57 | addSource(strSource);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void UIDownloaderUserManual::addSource(const QString &strSource)
|
---|
61 | {
|
---|
62 | /* Append passed value: */
|
---|
63 | m_sourcesList << strSource;
|
---|
64 | }
|
---|
65 |
|
---|
66 | void UIDownloaderUserManual::setParentWidget(QWidget *pParent)
|
---|
67 | {
|
---|
68 | m_pParent = pParent;
|
---|
69 | }
|
---|
70 |
|
---|
71 | QWidget* UIDownloaderUserManual::parentWidget() const
|
---|
72 | {
|
---|
73 | return m_pParent;
|
---|
74 | }
|
---|
75 |
|
---|
76 | UIMiniProcessWidgetUserManual* UIDownloaderUserManual::processWidget(QWidget *pParent /* = 0 */) const
|
---|
77 | {
|
---|
78 | UIMiniProcessWidgetUserManual *pWidget = new UIMiniProcessWidgetUserManual(pParent);
|
---|
79 |
|
---|
80 | /* Connect the cancel signal: */
|
---|
81 | connect(pWidget, SIGNAL(sigCancel()), this, SLOT(cancelDownloading()));
|
---|
82 | /* Connect the signal to notify about source changed: */
|
---|
83 | connect(this, SIGNAL(sigSourceChanged(const QString&)), pWidget, SLOT(sltSetSource(const QString&)));
|
---|
84 | /* Connect the signal to notify about the download process: */
|
---|
85 | connect(this, SIGNAL(sigDownloadProcess(int, int)), pWidget, SLOT(sltProcess(int, int)));
|
---|
86 | /* Make sure the widget is destroyed when this class is deleted: */
|
---|
87 | connect(this, SIGNAL(destroyed(QObject*)), pWidget, SLOT(close()));
|
---|
88 |
|
---|
89 | return pWidget;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void UIDownloaderUserManual::startDownload()
|
---|
93 | {
|
---|
94 | /* If at least one source to try left: */
|
---|
95 | if (!m_sourcesList.isEmpty())
|
---|
96 | {
|
---|
97 | /* Set the first of left sources as current one: */
|
---|
98 | UIDownloader::setSource(m_sourcesList.takeFirst());
|
---|
99 | /* Warn process-bar(s) about source was changed: */
|
---|
100 | emit sigSourceChanged(source());
|
---|
101 | /* Try to download: */
|
---|
102 | acknowledgeStart();
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | void UIDownloaderUserManual::acknowledgeFinished(bool fError)
|
---|
107 | {
|
---|
108 | /* If current source was wrong but other is present
|
---|
109 | * we will try other source else we should finish: */
|
---|
110 | if (m_pHttp->errorCode() != QIHttp::Aborted && m_pHttp->errorCode() != QIHttp::NoError && !m_sourcesList.isEmpty())
|
---|
111 | startDownload();
|
---|
112 | else
|
---|
113 | UIDownloader::acknowledgeFinished(fError);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void UIDownloaderUserManual::downloadFinished(bool fError)
|
---|
117 | {
|
---|
118 | if (fError)
|
---|
119 | UIDownloader::downloadFinished(fError);
|
---|
120 | else
|
---|
121 | {
|
---|
122 | /* Read all received data: */
|
---|
123 | QByteArray receivedData(m_pHttp->readAll());
|
---|
124 |
|
---|
125 | /* Serialize the incoming buffer into the User Manual file: */
|
---|
126 | while (true)
|
---|
127 | {
|
---|
128 | /* Try to open file to save document: */
|
---|
129 | QFile file(m_strTarget);
|
---|
130 | if (file.open(QIODevice::WriteOnly))
|
---|
131 | {
|
---|
132 | /* Write received data into file: */
|
---|
133 | file.write(receivedData);
|
---|
134 | file.close();
|
---|
135 | /* Warn user about User Manual document loaded and saved: */
|
---|
136 | vboxProblem().warnAboutUserManualDownloaded(m_source.toString(), QDir::toNativeSeparators(m_strTarget));
|
---|
137 | /* Warn listener about User Manual was downloaded: */
|
---|
138 | emit sigDownloadFinished(m_strTarget);
|
---|
139 | /* Close the downloader: */
|
---|
140 | QTimer::singleShot(0, this, SLOT(suicide()));
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | else
|
---|
144 | {
|
---|
145 | /* Warn user about User Manual document loaded but was not saved: */
|
---|
146 | vboxProblem().warnAboutUserManualCantBeSaved(m_source.toString(), QDir::toNativeSeparators(m_strTarget));
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Ask the user about User Manual file save location: */
|
---|
150 | QString target = QIFileDialog::getExistingDirectory(QFileInfo(m_strTarget).absolutePath(), m_pParent,
|
---|
151 | tr("Select folder to save User Manual to"), true);
|
---|
152 | /* If user reject to set save point: */
|
---|
153 | if (target.isNull())
|
---|
154 | /* Just close the downloader: */
|
---|
155 | QTimer::singleShot(0, this, SLOT(suicide()));
|
---|
156 | /* If user set correct save point: */
|
---|
157 | else
|
---|
158 | /* Store it and try to save User Manual document there: */
|
---|
159 | m_strTarget = QDir(target).absoluteFilePath(QFileInfo(m_strTarget).fileName());
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void UIDownloaderUserManual::suicide()
|
---|
165 | {
|
---|
166 | UIDownloaderUserManual::destroy();
|
---|
167 | }
|
---|
168 |
|
---|
169 | UIDownloaderUserManual::UIDownloaderUserManual()
|
---|
170 | : UIDownloader()
|
---|
171 | , m_pParent(0)
|
---|
172 | {
|
---|
173 | }
|
---|
174 |
|
---|
175 | bool UIDownloaderUserManual::confirmDownload()
|
---|
176 | {
|
---|
177 | return vboxProblem().confirmUserManualDownload(m_source.toString(), m_pHttp->lastResponse().contentLength());
|
---|
178 | }
|
---|
179 |
|
---|
180 | void UIDownloaderUserManual::warnAboutError(const QString &strError)
|
---|
181 | {
|
---|
182 | return vboxProblem().warnAboutUserManualCantBeDownloaded(m_source.toString(), strError);
|
---|
183 | }
|
---|
184 |
|
---|