src/download.cpp

Thu, 03 Oct 2013 20:56:20 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 03 Oct 2013 20:56:20 +0300
changeset 493
16766ac1bbd9
parent 491
7d1b5ecd76c0
child 503
bebe09014dd6
permissions
-rw-r--r--

last code reformatting i swear

426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 Santeri Piippo
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 452
diff changeset
4 *
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 452
diff changeset
9 *
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 452
diff changeset
14 *
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
19 #include <QNetworkAccessManager>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
20 #include <QNetworkRequest>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
21 #include <QNetworkReply>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
22 #include <QDir>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
23 #include <QProgressBar>
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
24 #include <QPushButton>
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include "download.h"
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include "ui_downloadfrom.h"
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #include "types.h"
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 #include "gui.h"
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
29 #include "file.h"
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
30 #include "gldraw.h"
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
31 #include "configDialog.h"
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
32 #include "moc_download.cpp"
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
461
fbcc91ae1dd2 - added "Go to line" action, renamed the config classes to proper camelcase
Santeri Piippo <crimsondusk64@gmail.com>
parents: 460
diff changeset
34 cfg (String, net_downloadpath, "");
fbcc91ae1dd2 - added "Go to line" action, renamed the config classes to proper camelcase
Santeri Piippo <crimsondusk64@gmail.com>
parents: 460
diff changeset
35 cfg (Bool, net_guesspaths, true);
fbcc91ae1dd2 - added "Go to line" action, renamed the config classes to proper camelcase
Santeri Piippo <crimsondusk64@gmail.com>
parents: 460
diff changeset
36 cfg (Bool, net_autoclose, false);
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
37
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
38 constexpr const char *PartDownloader::k_UnofficialURL;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
42 void PartDownloader::k_download()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
43 { str path = getDownloadPath();
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
44
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
45 if (path == "" || QDir (path).exists() == false)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
46 { critical (PartDownloader::tr ("You need to specify a valid path for "
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
47 "downloaded files in the configuration to download paths."));
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
48
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
49 (new ConfigDialog (ConfigDialog::DownloadTab, null))->exec();
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
50 return;
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
51 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
52
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
53 PartDownloader* dlg = new PartDownloader;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 dlg->exec();
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
59 str PartDownloader::getDownloadPath()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
60 { str path = net_downloadpath;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
61
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
62 #if DIRSLASH_CHAR != '/'
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
63 path.replace (DIRSLASH, "/");
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
64 #endif
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
65
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
66 return path;
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
67 }
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
68
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
69 // =============================================================================
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
70 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
71 PartDownloader::PartDownloader (QWidget* parent) : QDialog (parent)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
72 { ui = new Ui_DownloadFrom;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 ui->setupUi (this);
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
74 ui->fname->setFocus();
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
75 ui->progress->horizontalHeader()->setResizeMode (PartLabelColumn, QHeaderView::Stretch);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
76
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
77 m_downloadButton = new QPushButton (tr ("Download"));
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
78 ui->buttonBox->addButton (m_downloadButton, QDialogButtonBox::ActionRole);
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
79 getButton (Abort)->setEnabled (false);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
80
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 connect (ui->source, SIGNAL (currentIndexChanged (int)), this, SLOT (sourceChanged (int)));
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
82 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, SLOT (buttonClicked (QAbstractButton*)));
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
87 PartDownloader::~PartDownloader()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
88 { delete ui;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
93 str PartDownloader::getURL() const
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
94 { const Source src = getSource();
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
95 str dest;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
96
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
97 switch (src)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
98 { case PartsTracker:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
99 dest = ui->fname->text();
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
100 modifyDest (dest);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
101 return str (k_UnofficialURL) + dest;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
102
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
103 case CustomURL:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
104 return ui->fname->text();
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
106
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 // Shouldn't happen
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 return "";
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
113 void PartDownloader::modifyDest (str& dest) const
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
114 { dest = dest.simplified();
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
115
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
116 // If the user doesn't want us to guess, stop right here.
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
117 if (net_guesspaths == false)
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
118 return;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
119
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
120 // Ensure .dat extension
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
121 if (dest.right (4) != ".dat")
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
122 { // Remove the existing extension, if any. It may be we're here over a
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
123 // typo in the .dat extension.
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
124 const int dotpos = dest.lastIndexOf (".");
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
125
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
126 if (dotpos != -1 && dotpos >= dest.length() - 4)
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
127 dest.chop (dest.length() - dotpos);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
128
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
129 dest += ".dat";
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
130 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
131
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
132 // If the part starts with s\ or s/, then use parts/s/. Same goes with
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
133 // 48\ and p/48/.
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
134 if (dest.left (2) == "s\\" || dest.left (2) == "s/")
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
135 { dest.remove (0, 2);
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
136 dest.prepend ("parts/s/");
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
137 } elif (dest.left (3) == "48\\" || dest.left (3) == "48/")
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
138
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
139 { dest.remove (0, 3);
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
140 dest.prepend ("p/48/");
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
141 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
142
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
143 /* Try determine where to put this part. We have four directories:
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
144 parts/, parts/s/, p/, and p/48/. If we haven't already specified
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
145 either parts/ or p/, we need to add it automatically. Part files
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
146 are numbers wit a possible u prefix for parts with unknown number
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
147 which can be followed by any of:
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
148 - c** (composites)
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
149 - d** (formed stickers)
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
150 - p** (patterns)
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
151 - a lowercase alphabetic letter for variants
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
152
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
153 Subfiles (usually) have an s** prefix, in which case we use parts/s/.
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
154 Note that the regex starts with a '^' so it won't catch already fully
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
155 given part file names. */
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
156 str partRegex = "^u?[0-9]+(c[0-9][0-9]+)*(d[0-9][0-9]+)*[a-z]?(p[0-9a-z][0-9a-z]+)*";
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
157 str subpartRegex = partRegex + "s[0-9][0-9]+";
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
158
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
159 partRegex += "\\.dat$";
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
160 subpartRegex += "\\.dat$";
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
161
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
162 if (QRegExp (subpartRegex).exactMatch (dest))
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
163 dest.prepend ("parts/s/");
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
164
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
165 elif (QRegExp (partRegex).exactMatch (dest))
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
166 dest.prepend ("parts/");
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
167 elif (dest.left (6) != "parts/" && dest.left (2) != "p/")
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
168 dest.prepend ("p/");
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
169 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
170
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
171 // =============================================================================
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
172 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
173 PartDownloader::Source PartDownloader::getSource() const
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
174 { return (Source) ui->source->currentIndex();
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
175 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
176
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
177 // =============================================================================
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
178 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
179 void PartDownloader::sourceChanged (int i)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
180 { if (i == CustomURL)
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 ui->fileNameLabel->setText (tr ("URL:"));
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 else
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 ui->fileNameLabel->setText (tr ("File name:"));
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
186 // =============================================================================
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
187 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
188 void PartDownloader::buttonClicked (QAbstractButton* btn)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
189 { if (btn == getButton (Close))
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
190 { reject();
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
191 } elif (btn == getButton (Abort))
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
192
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
193 { setAborted (true);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
194
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
195 for (PartDownloadRequest * req : m_requests)
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
196 req->abort();
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
197 } elif (btn == getButton (Download))
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
198 { str dest = ui->fname->text();
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
199 setPrimaryFile (null);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
200 setAborted (false);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
201
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
202 if (getSource() == CustomURL)
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
203 dest = basename (getURL());
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
204
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
205 modifyDest (dest);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
206
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
207 if (QFile::exists (PartDownloader::getDownloadPath() + DIRSLASH + dest))
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
208 { const str overwritemsg = fmt (tr ("%1 already exists in download directory. Overwrite?"), dest);
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
209 if (!confirm (tr ("Overwrite?"), overwritemsg))
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
210 return;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
211 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
212
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
213 m_downloadButton->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
214 ui->progress->setEnabled (true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
215 ui->fname->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
216 ui->source->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
217 downloadFile (dest, getURL(), true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
218 getButton (Close)->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
219 getButton (Abort)->setEnabled (true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
220 getButton (Download)->setEnabled (false);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
221 }
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
222 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
223
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
224 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
225 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
226 void PartDownloader::downloadFile (str dest, str url, bool primary)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
227 { const int row = ui->progress->rowCount();
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
228
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
229 // Don't download files repeadetly.
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
230 if (m_filesToDownload.find (dest) != -1u)
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
231 return;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
232
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
233 modifyDest (dest);
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
234 print ("DOWNLOAD: %1 -> %2\n", url, PartDownloader::getDownloadPath() + DIRSLASH + dest);
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
235 PartDownloadRequest* req = new PartDownloadRequest (url, dest, primary, this);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
236
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
237 m_filesToDownload << dest;
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
238 m_requests << req;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
239 ui->progress->insertRow (row);
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 req->setTableRow (row);
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 req->updateToTable();
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
246 void PartDownloader::checkIfFinished()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
247 { bool failed = aborted();
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
248
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
249 // If there is some download still working, we're not finished.
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
250 for (PartDownloadRequest* req : m_requests)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
251 { if (!req->isFinished())
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
252 return;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
253
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
254 if (req->state() == PartDownloadRequest::Failed)
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
255 failed = true;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
256 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
257
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
258 for (PartDownloadRequest* req : m_requests)
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
259 delete req;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
260
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
261 m_requests.clear();
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
262
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
263 // Update everything now
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
264 if (primaryFile())
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
265 { LDFile::setCurrent (primaryFile());
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
266 reloadAllSubfiles();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
267 g_win->fullRefresh();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
268 g_win->R()->resetAngles();
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
269 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
270
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
271 if (net_autoclose && !failed)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
272 { // Close automatically if desired.
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
273 accept();
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
274 }
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
275 else
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
276 { // Allow the prompt be closed now.
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
277 getButton (Abort)->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
278 getButton (Close)->setEnabled (true);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
279 }
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
280 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
281
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
282 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
283 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
284 QPushButton* PartDownloader::getButton (PartDownloader::Button i)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
285 { typedef QDialogButtonBox QDBB;
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
286 alias btnbox = ui->buttonBox;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
287
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
288 switch (i)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
289 { case Download:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
290 return m_downloadButton;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
291
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
292 case Abort:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
293 return qobject_cast<QPushButton*> (btnbox->button (QDBB::Abort));
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
294
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
295 case Close:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
296 return qobject_cast<QPushButton*> (btnbox->button (QDBB::Close));
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
297 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
298
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
299 return null;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
300 }
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
301
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
302 // =============================================================================
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
303 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
304 PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloader* parent) :
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
305 QObject (parent),
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
306 m_prompt (parent),
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
307 m_url (url),
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
308 m_dest (dest),
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
309 m_fpath (PartDownloader::getDownloadPath() + DIRSLASH + dest),
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
310 m_nam (new QNetworkAccessManager),
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
311 m_firstUpdate (true),
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
312 m_state (Requesting),
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
313 m_primary (primary),
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
314 m_fp (null)
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
315 {
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
316 // Make sure that we have a valid destination.
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
317 str dirpath = dirname (m_fpath);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
318
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
319 QDir dir (dirpath);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
320
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
321 if (!dir.exists())
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
322 { print ("Creating %1...\n", dirpath);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
323
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
324 if (!dir.mkpath (dirpath))
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
325 critical (fmt (tr ("Couldn't create the directory %1!"), dirpath));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
326 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
327
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
328 m_reply = m_nam->get (QNetworkRequest (QUrl (url)));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
329 connect (m_reply, SIGNAL (finished()), this, SLOT (downloadFinished()));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
330 connect (m_reply, SIGNAL (readyRead()), this, SLOT (readyRead()));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
331 connect (m_reply, SIGNAL (downloadProgress (qint64, qint64)), this, SLOT (downloadProgress (qint64, qint64)));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
332 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
333
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
334 // =============================================================================
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
335 // -----------------------------------------------------------------------------
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
336 PartDownloadRequest::~PartDownloadRequest() {}
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
337
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
338 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
339 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
340 void PartDownloadRequest::updateToTable()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
341 { const int labelcol = PartDownloader::PartLabelColumn,
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
342 progcol = PartDownloader::ProgressColumn;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
343 QTableWidget* table = m_prompt->ui->progress;
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
344 QProgressBar* prog;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
345
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
346 switch (m_state)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
347 { case Requesting:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
348 case Downloading:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
349 prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), progcol));
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
350
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
351 if (!prog)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
352 { prog = new QProgressBar;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
353 table->setCellWidget (tableRow(), progcol, prog);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
354 }
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
355
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
356 prog->setRange (0, m_bytesTotal);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
357 prog->setValue (m_bytesRead);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
358 break;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
359
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
360 case Finished:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
361 case Failed:
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
362 { QLabel* lb = new QLabel ( (m_state == Finished) ? "<b><span style=\"color: #080\">FINISHED</span></b>" :
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
363 "<b><span style=\"color: #800\">FAILED</span></b>");
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
364 lb->setAlignment (Qt::AlignCenter);
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
365 table->setCellWidget (tableRow(), progcol, lb);
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
366 }
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
367 break;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
368 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
369
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
370 QLabel* lb = qobject_cast<QLabel*> (table->cellWidget (tableRow(), labelcol));
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
371
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
372 if (m_firstUpdate)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
373 { lb = new QLabel (fmt ("<b>%1</b>", m_dest), table);
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
374 table->setCellWidget (tableRow(), labelcol, lb);
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
375 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
376
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
377 // Make sure that the cell is big enough to contain the label
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
378 if (table->columnWidth (labelcol) < lb->width())
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
379 table->setColumnWidth (labelcol, lb->width());
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
380
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
381 m_firstUpdate = false;
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
382 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
383
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
384 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
385 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
386 void PartDownloadRequest::downloadFinished()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
387 { if (m_reply->error() != QNetworkReply::NoError)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
388 { if (m_primary && !m_prompt->aborted())
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
389 critical (m_reply->errorString());
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
390
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
391 m_state = Failed;
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
392 } elif (state() != Failed)
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
393
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
394 m_state = Finished;
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
395
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
396 m_bytesRead = m_bytesTotal;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
397 updateToTable();
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
398
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
399 if (m_fp)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
400 { m_fp->close();
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
401 delete m_fp;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
402 m_fp = null;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
403
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
404 if (m_state == Failed)
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
405 QFile::remove (m_fpath);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
406 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
407
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
408 if (m_state != Finished)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
409 { m_prompt->checkIfFinished();
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
410 return;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
411 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
412
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
413 // Try to load this file now.
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
414 LDFile* f = openDATFile (m_fpath, false);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
415
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
416 if (!f)
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
417 return;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
418
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
419 f->setImplicit (!m_primary);
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
420
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
421 // Iterate through this file and check for errors. If there's any that stems
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
422 // from unknown file references, try resolve that by downloading the reference.
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
423 // This is why downloading a part may end up downloading multiple files, as
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
424 // it resolves dependencies.
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
425 for (LDObject* obj : f->objects())
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
426 { LDError* err = dynamic_cast<LDError*> (obj);
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
427
451
7ef26b45ed55 minor change
Santeri Piippo <crimsondusk64@gmail.com>
parents: 449
diff changeset
428 if (!err || err->fileRef().isEmpty())
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
429 continue;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
430
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
431 str dest = err->fileRef();
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
432 m_prompt->modifyDest (dest);
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
433 m_prompt->downloadFile (dest, str (PartDownloader::k_UnofficialURL) + dest, false);
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
434 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
435
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
436 if (m_primary)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
437 { addRecentFile (m_fpath);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
438 m_prompt->setPrimaryFile (f);
449
f462f0bd3c51 made downloaded files be stored into recent files
Santeri Piippo <crimsondusk64@gmail.com>
parents: 435
diff changeset
439 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
440
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
441 m_prompt->checkIfFinished();
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
442 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
443
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
444 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
445 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
446 void PartDownloadRequest::downloadProgress (int64 recv, int64 total)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
447 { m_bytesRead = recv;
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
448 m_bytesTotal = total;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
449 m_state = Downloading;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
450 updateToTable();
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
451 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
452
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
453 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
454 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
455 void PartDownloadRequest::readyRead()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
456 { if (state() == Failed)
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
457 return;
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
458
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
459 if (m_fp == null)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
460 { m_fpath.replace ("\\", "/");
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
461
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
462 // We have already asked the user whether we can overwrite so we're good
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
463 // to go here.
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
464 m_fp = new QFile (m_fpath.toLocal8Bit());
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
465
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
466 if (!m_fp->open (QIODevice::WriteOnly))
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
467 { critical (fmt (tr ("Couldn't open %1 for writing: %2"), m_fpath, strerror (errno)));
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
468 m_state = Failed;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
469 m_reply->abort();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
470 updateToTable();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
471 m_prompt->checkIfFinished();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
472 return;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
473 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
474 }
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
475
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
476 m_fp->write (m_reply->readAll());
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
477 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
478
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
479 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
480 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
481 bool PartDownloadRequest::isFinished() const
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
482 { return m_state == Finished || m_state == Failed;
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
483 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
484
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
485 // =============================================================================
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
486 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
487 const PartDownloadRequest::State& PartDownloadRequest::state() const
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
488 { return m_state;
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
489 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
490
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
491 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
492 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
493 void PartDownloadRequest::abort()
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
494 { m_reply->abort();
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
495 }
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
496
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
497 // =============================================================================
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
498 // -----------------------------------------------------------------------------
493
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
499 DEFINE_ACTION (DownloadFrom, 0)
16766ac1bbd9 last code reformatting i swear
Santeri Piippo <crimsondusk64@gmail.com>
parents: 491
diff changeset
500 { PartDownloader::k_download();
491
7d1b5ecd76c0 it's LDForge's 1st birthday!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 484
diff changeset
501 }

mercurial