ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

downloaddialog.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "gui/downloaddialog.h"
00024 #include "backends/cloud/cloudmanager.h"
00025 #include "backends/networking/connection/islimited.h"
00026 #include "common/config-manager.h"
00027 #include "common/translation.h"
00028 #include "engines/metaengine.h"
00029 #include "gui/browser.h"
00030 #include "gui/chooser.h"
00031 #include "gui/editgamedialog.h"
00032 #include "gui/gui-manager.h"
00033 #include "gui/launcher.h"
00034 #include "gui/message.h"
00035 #include "gui/remotebrowser.h"
00036 #include "gui/widgets/edittext.h"
00037 #include "gui/widgets/list.h"
00038 
00039 namespace GUI {
00040 
00041 enum {
00042     kDownloadDialogButtonCmd = 'Dldb'
00043 };
00044 
00045 DownloadDialog::DownloadDialog(uint32 storageId, LauncherDialog *launcher) :
00046     Dialog("GlobalOptions_Cloud_DownloadDialog"), _launcher(launcher), _close(false) {
00047     _backgroundType = GUI::ThemeEngine::kDialogBackgroundPlain;
00048 
00049     _browser = new BrowserDialog(_("Select directory where to download game data"), true);
00050     _remoteBrowser = new RemoteBrowserDialog(_("Select directory with game data"));
00051 
00052     _remoteDirectoryLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.RemoteDirectory", _("From: "));
00053     _localDirectoryLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.LocalDirectory", _("To: "));
00054     uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
00055     _progressBar = new SliderWidget(this, "GlobalOptions_Cloud_DownloadDialog.ProgressBar");
00056     _progressBar->setMinValue(0);
00057     _progressBar->setMaxValue(100);
00058     _progressBar->setValue(progress);
00059     _progressBar->setEnabled(false);
00060     _percentLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.PercentText", Common::String::format("%u %%", progress));
00061     _downloadSizeLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.DownloadSize", "");
00062     _downloadSpeedLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.DownloadSpeed", "");
00063     if (g_system->getOverlayWidth() > 320)
00064         _cancelButton = new ButtonWidget(this, "GlobalOptions_Cloud_DownloadDialog.MainButton", _("Cancel download"), 0, kDownloadDialogButtonCmd);
00065     else
00066         _cancelButton = new ButtonWidget(this, "GlobalOptions_Cloud_DownloadDialog.MainButton", _c("Cancel download", "lowres"), 0, kDownloadDialogButtonCmd);
00067 
00068     _closeButton = new ButtonWidget(this, "GlobalOptions_Cloud_DownloadDialog.CloseButton", _("Hide"), 0, kCloseCmd);
00069     refreshWidgets();
00070 
00071     CloudMan.setDownloadTarget(this);
00072 }
00073 
00074 DownloadDialog::~DownloadDialog() {
00075     CloudMan.setDownloadTarget(nullptr);
00076 }
00077 
00078 void DownloadDialog::open() {
00079     Dialog::open();
00080     CloudMan.setDownloadTarget(this);
00081     if (!CloudMan.isDownloading())
00082         if (!selectDirectories())
00083             close();
00084     reflowLayout();
00085     g_gui.scheduleTopDialogRedraw();
00086 }
00087 
00088 void DownloadDialog::close() {
00089     CloudMan.setDownloadTarget(nullptr);
00090     Dialog::close();
00091 }
00092 
00093 void DownloadDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
00094     switch (cmd) {
00095     case kDownloadDialogButtonCmd:
00096         {
00097             CloudMan.setDownloadTarget(nullptr);
00098             CloudMan.cancelDownload();
00099             close();
00100             break;
00101         }
00102     case kDownloadProgressCmd:
00103         if (!_close) {
00104             refreshWidgets();
00105             g_gui.scheduleTopDialogRedraw();
00106         }
00107         break;
00108     case kDownloadEndedCmd:
00109         _close = true;
00110         break;
00111     default:
00112         Dialog::handleCommand(sender, cmd, data);
00113     }
00114 }
00115 
00116 bool DownloadDialog::selectDirectories() {
00117     if (Networking::Connection::isLimited()) {
00118         MessageDialog alert(_("It looks like your connection is limited. "
00119             "Do you really want to download files with it?"), _("Yes"), _("No"));
00120         if (alert.runModal() != GUI::kMessageOK)
00121             return false;
00122     }
00123 
00124     //first user should select remote directory to download
00125     if (_remoteBrowser->runModal() <= 0)
00126         return false;
00127 
00128     Cloud::StorageFile remoteDirectory = _remoteBrowser->getResult();
00129 
00130     //now user should select local directory to download into
00131     if (_browser->runModal() <= 0)
00132         return false;
00133 
00134     Common::FSNode dir(_browser->getResult());
00135     Common::FSList files;
00136     if (!dir.getChildren(files, Common::FSNode::kListAll)) {
00137         MessageDialog alert(_("ScummVM couldn't open the specified directory!"));
00138         alert.runModal();
00139         return false;
00140     }
00141 
00142     //check that there is no file with the remote directory's name in the local one
00143     for (Common::FSList::iterator i = files.begin(); i != files.end(); ++i) {
00144         if (i->getName().equalsIgnoreCase(remoteDirectory.name())) {
00145             //if there is, ask user whether it's OK
00146             if (!i->isDirectory()) {
00147                 GUI::MessageDialog alert(_("Cannot create a directory to download - the specified directory has a file with the same name."), _("OK"));
00148                 alert.runModal();
00149                 return false;
00150             }
00151             GUI::MessageDialog alert(
00152                 Common::String::format(_("The \"%s\" already exists in the specified directory.\nDo you really want to download files into that directory?"), remoteDirectory.name().c_str()),
00153                 _("Yes"),
00154                 _("No")
00155                 );
00156             if (alert.runModal() != GUI::kMessageOK)
00157                 return false;
00158             break;
00159         }
00160     }
00161 
00162     //make a local path
00163     Common::String localPath = dir.getPath();
00164 
00165     //simple heuristic to determine which path separator to use
00166     if (localPath.size() && localPath.lastChar() != '/' && localPath.lastChar() != '\\') {
00167         int backslashes = 0;
00168         for (uint32 i = 0; i < localPath.size(); ++i)
00169             if (localPath[i] == '/')
00170                 --backslashes;
00171             else if (localPath[i] == '\\')
00172                 ++backslashes;
00173 
00174         if (backslashes > 0)
00175             localPath += '\\' + remoteDirectory.name();
00176         else
00177             localPath += '/' + remoteDirectory.name();
00178     } else {
00179         localPath += remoteDirectory.name();
00180     }
00181 
00182     CloudMan.startDownload(remoteDirectory.path(), localPath);
00183     CloudMan.setDownloadTarget(this);
00184     _localDirectory = localPath;
00185     return true;
00186 }
00187 
00188 void DownloadDialog::handleTickle() {
00189     if (_close) {
00190         if (_launcher)
00191             _launcher->doGameDetection(_localDirectory);
00192         close();
00193         _close = false;
00194         return;
00195     }
00196 
00197     int32 progress = (int32)(100 * CloudMan.getDownloadingProgress());
00198     if (_progressBar->getValue() != progress) {
00199         refreshWidgets();
00200         g_gui.scheduleTopDialogRedraw();
00201     }
00202 
00203     Dialog::handleTickle();
00204 }
00205 
00206 void DownloadDialog::reflowLayout() {
00207     Dialog::reflowLayout();
00208     refreshWidgets();
00209 }
00210 
00211 namespace {
00212 Common::String getHumanReadableBytes(uint64 bytes, Common::String &unitsOut) {
00213     Common::String result = Common::String::format("%lu", bytes);
00214     unitsOut = "B";
00215 
00216     if (bytes >= 1024) {
00217         bytes /= 1024;
00218         result = Common::String::format("%lu", bytes);
00219         unitsOut = "KB";
00220     }
00221 
00222     double floating = bytes;
00223 
00224     if (bytes >= 1024) {
00225         bytes /= 1024;
00226         floating /= 1024.0;
00227         unitsOut = "MB";
00228     }
00229 
00230     if (bytes >= 1024) {
00231         bytes /= 1024;
00232         floating /= 1024.0;
00233         unitsOut = "GB";
00234     }
00235 
00236     if (bytes >= 1024) { // woah
00237         bytes /= 1024;
00238         floating /= 1024.0;
00239         unitsOut = "TB";
00240     }
00241 
00242     // print one digit after floating point
00243     result = Common::String::format("%.1f", floating);
00244     return result;
00245 }
00246 }
00247 
00248 Common::String DownloadDialog::getSizeLabelText() {
00249     Common::String downloaded, downloadedUnits, total, totalUnits;
00250     downloaded = getHumanReadableBytes(CloudMan.getDownloadBytesNumber(), downloadedUnits);
00251     total = getHumanReadableBytes(CloudMan.getDownloadTotalBytesNumber(), totalUnits);
00252     return Common::String::format(_("Downloaded %s %s / %s %s"), downloaded.c_str(), _(downloadedUnits.c_str()), total.c_str(), _(totalUnits.c_str()));
00253 }
00254 
00255 Common::String DownloadDialog::getSpeedLabelText() {
00256     Common::String speed, speedUnits;
00257     speed = getHumanReadableBytes(CloudMan.getDownloadSpeed(), speedUnits);
00258     speedUnits += "/s";
00259     return Common::String::format(_("Download speed: %s %s"), speed.c_str(), _(speedUnits.c_str()));
00260 }
00261 
00262 void DownloadDialog::refreshWidgets() {
00263     _localDirectory = CloudMan.getDownloadLocalDirectory();
00264     _remoteDirectoryLabel->setLabel(_("From: ") + CloudMan.getDownloadRemoteDirectory());
00265     _localDirectoryLabel->setLabel(_("To: ") + _localDirectory);
00266     uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
00267     _percentLabel->setLabel(Common::String::format("%u %%", progress));
00268     _downloadSizeLabel->setLabel(getSizeLabelText());
00269     _downloadSpeedLabel->setLabel(getSpeedLabelText());
00270     _progressBar->setValue(progress);
00271 }
00272 
00273 } // End of namespace GUI


Generated on Sat May 25 2019 05:00:43 for ResidualVM by doxygen 1.7.1
curved edge   curved edge