Mercurial > projects > qtd
comparison demos/browser/downloadmanager.d @ 77:0654fc9bac95
more porting
author | mandel |
---|---|
date | Sun, 24 May 2009 13:46:32 +0000 |
parents | 37caa90ce503 |
children | 85c59c4e5f19 |
comparison
equal
deleted
inserted
replaced
76:454e4b4beb59 | 77:0654fc9bac95 |
---|---|
138 m_reply.deleteLater(); | 138 m_reply.deleteLater(); |
139 if (m_output.exists()) | 139 if (m_output.exists()) |
140 m_output.remove(); | 140 m_output.remove(); |
141 m_reply = r; | 141 m_reply = r; |
142 init(); | 142 init(); |
143 emit statusChanged(); | 143 statusChanged.emit(); |
144 } | 144 } |
145 | 145 |
146 void open() | 146 void open() |
147 { | 147 { |
148 QFileInfo info(m_output); | 148 auto info = new QFileInfo(m_output); |
149 QUrl url = QUrl.fromLocalFile(info.absolutePath()); | 149 QUrl url = QUrl.fromLocalFile(info.absolutePath()); |
150 QDesktopServices.openUrl(url); | 150 QDesktopServices.openUrl(url); |
151 } | 151 } |
152 | 152 |
153 void downloadReadyRead() | 153 void downloadReadyRead() |
157 if (!m_output.isOpen()) { | 157 if (!m_output.isOpen()) { |
158 // in case someone else has already put a file there | 158 // in case someone else has already put a file there |
159 if (!m_requestFileName) | 159 if (!m_requestFileName) |
160 getFileName(); | 160 getFileName(); |
161 if (!m_output.open(QIODevice.WriteOnly)) { | 161 if (!m_output.open(QIODevice.WriteOnly)) { |
162 downloadInfoLabel.setText(tr("Error opening save file: %1") | 162 downloadInfoLabel.setText(Format(tr("Error opening save file: {}"), m_output.errorString())); |
163 .arg(m_output.errorString())); | |
164 stopButton.click(); | 163 stopButton.click(); |
165 emit statusChanged(); | 164 statusChanged.emit(); |
166 return; | 165 return; |
167 } | 166 } |
168 emit statusChanged(); | 167 statusChanged.emit(); |
169 } | 168 } |
170 if (-1 == m_output.write(m_reply.readAll())) { | 169 if (-1 == m_output.write(m_reply.readAll())) { |
171 downloadInfoLabel.setText(tr("Error saving: %1") | 170 downloadInfoLabel.setText(Format(tr("Error saving: {}"), m_output.errorString())); |
172 .arg(m_output.errorString())); | |
173 stopButton.click(); | 171 stopButton.click(); |
174 } | 172 } |
175 } | 173 } |
176 | 174 |
177 void error(QNetworkReply.NetworkError code) | 175 void error(QNetworkReply.NetworkError code) |
178 { | 176 { |
179 qDebug() << "DownloadItem::error" << m_reply.errorString() << m_url; | 177 qDebug() << "DownloadItem::error" << m_reply.errorString() << m_url; |
180 downloadInfoLabel.setText(tr("Network Error: %1").arg(m_reply.errorString())); | 178 downloadInfoLabel.setText(Format(tr("Network Error: {}"), m_reply.errorString())); |
181 tryAgainButton.setEnabled(true); | 179 tryAgainButton.setEnabled(true); |
182 tryAgainButton.setVisible(true); | 180 tryAgainButton.setVisible(true); |
183 } | 181 } |
184 | 182 |
185 void downloadProgress(ulong bytesReceived, ulong bytesTotal) | 183 void downloadProgress(ulong bytesReceived, ulong bytesTotal) |
205 progressBar.hide(); | 203 progressBar.hide(); |
206 stopButton.setEnabled(false); | 204 stopButton.setEnabled(false); |
207 stopButton.hide(); | 205 stopButton.hide(); |
208 m_output.close(); | 206 m_output.close(); |
209 updateInfoLabel(); | 207 updateInfoLabel(); |
210 emit statusChanged(); | 208 statusChanged.emit(); |
211 } | 209 } |
212 | 210 |
213 private: | 211 private: |
214 | 212 |
215 void getFileName() | 213 void getFileName() |
216 { | 214 { |
217 QSettings settings; | 215 auto settings = new QSettings; |
218 settings.beginGroup(QLatin1String("downloadmanager")); | 216 settings.beginGroup(QLatin1String("downloadmanager")); |
219 QString defaultLocation = QDesktopServices.storageLocation(QDesktopServices.DesktopLocation); | 217 string defaultLocation = QDesktopServices.storageLocation(QDesktopServices.DesktopLocation); |
220 QString downloadDirectory = settings.value(QLatin1String("downloadDirectory"), defaultLocation).toString(); | 218 string downloadDirectory = settings.value(QLatin1String("downloadDirectory"), defaultLocation).toString(); |
221 if (!downloadDirectory.isEmpty()) | 219 if (!downloadDirectory.isEmpty()) |
222 downloadDirectory += QLatin1Char('/'); | 220 downloadDirectory += QLatin1Char('/'); |
223 | 221 |
224 QString defaultFileName = saveFileName(downloadDirectory); | 222 string defaultFileName = saveFileName(downloadDirectory); |
225 QString fileName = defaultFileName; | 223 string fileName = defaultFileName; |
226 if (m_requestFileName) { | 224 if (m_requestFileName) { |
227 fileName = QFileDialog.getSaveFileName(this, tr("Save File"), defaultFileName); | 225 fileName = QFileDialog.getSaveFileName(this, tr("Save File"), defaultFileName); |
228 if (fileName.isEmpty()) { | 226 if (fileName.isEmpty()) { |
229 m_reply.close(); | 227 m_reply.close(); |
230 fileNameLabel.setText(tr("Download canceled: %1").arg(QFileInfo(defaultFileName).fileName())); | 228 fileNameLabel.setText(Format(tr("Download canceled: {}"), (new QFileInfo(defaultFileName)).fileName())); |
231 return; | 229 return; |
232 } | 230 } |
233 } | 231 } |
234 m_output.setFileName(fileName); | 232 m_output.setFileName(fileName); |
235 fileNameLabel.setText(QFileInfo(m_output.fileName()).fileName()); | 233 fileNameLabel.setText((new QFileInfo(m_output.fileName())).fileName()); |
236 if (m_requestFileName) | 234 if (m_requestFileName) |
237 downloadReadyRead(); | 235 downloadReadyRead(); |
238 } | 236 } |
239 | 237 |
240 void init() | 238 void init() |
274 bool running = !downloadedSuccessfully(); | 272 bool running = !downloadedSuccessfully(); |
275 | 273 |
276 // update info label | 274 // update info label |
277 double speed = m_bytesReceived * 1000.0 / m_downloadTime.elapsed(); | 275 double speed = m_bytesReceived * 1000.0 / m_downloadTime.elapsed(); |
278 double timeRemaining = (cast(double)(bytesTotal - m_bytesReceived)) / speed; | 276 double timeRemaining = (cast(double)(bytesTotal - m_bytesReceived)) / speed; |
279 QString timeRemainingString = tr("seconds"); | 277 string timeRemainingString = tr("seconds"); |
280 if (timeRemaining > 60) { | 278 if (timeRemaining > 60) { |
281 timeRemaining = timeRemaining / 60; | 279 timeRemaining = timeRemaining / 60; |
282 timeRemainingString = tr("minutes"); | 280 timeRemainingString = tr("minutes"); |
283 } | 281 } |
284 timeRemaining = floor(timeRemaining); | 282 timeRemaining = floor(timeRemaining); |
285 | 283 |
286 // When downloading the eta should never be 0 | 284 // When downloading the eta should never be 0 |
287 if (timeRemaining == 0) | 285 if (timeRemaining == 0) |
288 timeRemaining = 1; | 286 timeRemaining = 1; |
289 | 287 |
290 QString info; | 288 string info; |
291 if (running) { | 289 if (running) { |
292 QString remaining; | 290 string remaining; |
293 if (bytesTotal != 0) | 291 if (bytesTotal != 0) |
294 remaining = tr("- %4 %5 remaining") | 292 remaining = Format(tr("- {} {} remaining"), timeRemaining, timeRemainingString); |
295 .arg(timeRemaining) | |
296 .arg(timeRemainingString); | |
297 | 293 |
298 info = QString(tr("%1 of %2 (%3/sec) %4")) | 294 info = Format(tr("{} of {} ({}/sec) {}"), |
299 .arg(dataString(m_bytesReceived)) | 295 dataString(m_bytesReceived), |
300 .arg(bytesTotal == 0 ? tr("?") : dataString(bytesTotal)) | 296 bytesTotal == 0 ? tr("?") : dataString(bytesTotal), |
301 .arg(dataString(cast(int) speed)) | 297 dataString(cast(int) speed), |
302 .arg(remaining); | 298 remaining); |
303 } else { | 299 } else { |
304 if (m_bytesReceived == bytesTotal) | 300 if (m_bytesReceived == bytesTotal) |
305 info = dataString(m_output.size()); | 301 info = dataString(m_output.size()); |
306 else | 302 else |
307 info = tr("%1 of %2 - Stopped") | 303 info = Format(tr("{} of {} - Stopped"), |
308 .arg(dataString(m_bytesReceived)) | 304 dataString(m_bytesReceived), |
309 .arg(dataString(bytesTotal)); | 305 dataString(bytesTotal)); |
310 } | 306 } |
311 downloadInfoLabel.setText(info); | 307 downloadInfoLabel.setText(info); |
312 } | 308 } |
313 | 309 |
314 QString dataString(int size) | 310 string dataString(int size) |
315 { | 311 { |
316 QString unit; | 312 string unit; |
317 if (size < 1024) { | 313 if (size < 1024) { |
318 unit = tr("bytes"); | 314 unit = tr("bytes"); |
319 } else if (size < 1024*1024) { | 315 } else if (size < 1024*1024) { |
320 size /= 1024; | 316 size /= 1024; |
321 unit = tr("kB"); | 317 unit = tr("kB"); |
322 } else { | 318 } else { |
323 size /= 1024*1024; | 319 size /= 1024*1024; |
324 unit = tr("MB"); | 320 unit = tr("MB"); |
325 } | 321 } |
326 return QString(QLatin1String("%1 %2")).arg(size).arg(unit); | 322 return Format(QLatin1String("{} {}"), size, unit); |
327 } | 323 } |
328 | 324 |
329 QString saveFileName(QString directory) | 325 string saveFileName(string directory) |
330 { | 326 { |
331 // Move this function into QNetworkReply to also get file name sent from the server | 327 // Move this function into QNetworkReply to also get file name sent from the server |
332 QString path = m_url.path(); | 328 string path = m_url.path(); |
333 QFileInfo info(path); | 329 auto info = new QFileInfo(path); |
334 QString baseName = info.completeBaseName(); | 330 string baseName = info.completeBaseName(); |
335 QString endName = info.suffix(); | 331 string endName = info.suffix(); |
336 | 332 |
337 if (baseName.isEmpty()) { | 333 if (baseName.isEmpty()) { |
338 baseName = QLatin1String("unnamed_download"); | 334 baseName = QLatin1String("unnamed_download"); |
339 qDebug() << "DownloadManager:: downloading unknown file:" << m_url; | 335 qDebug() << "DownloadManager:: downloading unknown file:" << m_url; |
340 } | 336 } |
341 QString name = directory + baseName + QLatin1Char('.') + endName; | 337 string name = directory ~ baseName ~ QLatin1Char('.') ~ endName; |
342 if (QFile.exists(name)) { | 338 if (QFile.exists(name)) { |
343 // already exists, don't overwrite | 339 // already exists, don't overwrite |
344 int i = 1; | 340 int i = 1; |
345 do { | 341 do { |
346 name = directory + baseName + QLatin1Char('-') + QString.number(i++) + QLatin1Char('.') + endName; | 342 name = directory ~ baseName ~ QLatin1Char('-') ~ QString.number(i++) ~ QLatin1Char('.') ~ endName; |
347 } while (QFile.exists(name)); | 343 } while (QFile.exists(name)); |
348 } | 344 } |
349 return name; | 345 return name; |
350 } | 346 } |
351 | 347 |
401 } | 397 } |
402 | 398 |
403 int activeDownloads() | 399 int activeDownloads() |
404 { | 400 { |
405 int count = 0; | 401 int count = 0; |
406 for (int i = 0; i < m_downloads.count(); ++i) { | 402 for (int i = 0; i < m_downloads.length; ++i) { |
407 if (m_downloads.at(i).stopButton.isEnabled()) | 403 if (m_downloads[i].stopButton.isEnabled()) |
408 ++count; | 404 ++count; |
409 } | 405 } |
410 return count; | 406 return count; |
411 } | 407 } |
412 | 408 |
452 addItem(item); | 448 addItem(item); |
453 } | 449 } |
454 | 450 |
455 void cleanup() | 451 void cleanup() |
456 { | 452 { |
457 if (m_downloads.isEmpty()) | 453 if (m_downloads.length == 0) |
458 return; | 454 return; |
459 m_model.removeRows(0, m_downloads.count()); | 455 m_model.removeRows(0, m_downloads.length); |
460 updateItemCount(); | 456 updateItemCount(); |
461 if (m_downloads.isEmpty() && m_iconProvider) { | 457 if (m_downloads.length == 0 && m_iconProvider) { |
462 delete m_iconProvider; | 458 delete m_iconProvider; |
463 m_iconProvider = 0; | 459 m_iconProvider = 0; |
464 } | 460 } |
465 m_autoSaver.changeOccurred(); | 461 m_autoSaver.changeOccurred(); |
466 } | 462 } |
475 settings.setValue(QLatin1String("removeDownloadsPolicy"), QLatin1String(removePolicyEnum.valueToKey(m_removePolicy))); | 471 settings.setValue(QLatin1String("removeDownloadsPolicy"), QLatin1String(removePolicyEnum.valueToKey(m_removePolicy))); |
476 settings.setValue(QLatin1String("size"), size()); | 472 settings.setValue(QLatin1String("size"), size()); |
477 if (m_removePolicy == Exit) | 473 if (m_removePolicy == Exit) |
478 return; | 474 return; |
479 | 475 |
480 for (int i = 0; i < m_downloads.count(); ++i) { | 476 for (int i = 0; i < m_downloads.length; ++i) { |
481 QString key = QString(QLatin1String("download_%1_")).arg(i); | 477 string key = Format(QLatin1String("download_{}_"), i); |
482 settings.setValue(key + QLatin1String("url"), m_downloads[i].m_url); | 478 settings.setValue(key ~ QLatin1String("url"), m_downloads[i].m_url); |
483 settings.setValue(key + QLatin1String("location"), QFileInfo(m_downloads[i].m_output).filePath()); | 479 settings.setValue(key ~ QLatin1String("location"), (new QFileInfo(m_downloads[i].m_output)).filePath()); |
484 settings.setValue(key + QLatin1String("done"), m_downloads[i].downloadedSuccessfully()); | 480 settings.setValue(key ~ QLatin1String("done"), m_downloads[i].downloadedSuccessfully()); |
485 } | 481 } |
486 int i = m_downloads.count(); | 482 int i = m_downloads.length; |
487 QString key = QString(QLatin1String("download_%1_")).arg(i); | 483 string key = Format(QLatin1String("download_{}_"), i); |
488 while (settings.contains(key + QLatin1String("url"))) { | 484 while (settings.contains(key ~ QLatin1String("url"))) { |
489 settings.remove(key + QLatin1String("url")); | 485 settings.remove(key ~ QLatin1String("url")); |
490 settings.remove(key + QLatin1String("location")); | 486 settings.remove(key ~ QLatin1String("location")); |
491 settings.remove(key + QLatin1String("done")); | 487 settings.remove(key ~ QLatin1String("done")); |
492 key = QString(QLatin1String("download_%1_")).arg(++i); | 488 key = Format(QLatin1String("download_{}_"), ++i); |
493 } | 489 } |
494 } | 490 } |
495 | 491 |
496 void updateRow() | 492 void updateRow() |
497 { | 493 { |
498 DownloadItem item = cast(DownloadItem) sender(); | 494 DownloadItem item = cast(DownloadItem) signalSender(); |
499 int row = m_downloads.indexOf(item); | 495 int row = m_downloads.indexOf(item); |
500 if (-1 == row) | 496 if (-1 == row) |
501 return; | 497 return; |
502 if (!m_iconProvider) | 498 if (!m_iconProvider) |
503 m_iconProvider = new QFileIconProvider(); | 499 m_iconProvider = new QFileIconProvider(); |
516 remove = true; | 512 remove = true; |
517 } | 513 } |
518 if (remove) | 514 if (remove) |
519 m_model.removeRow(row); | 515 m_model.removeRow(row); |
520 | 516 |
521 cleanupButton.setEnabled(m_downloads.count() - activeDownloads() > 0); | 517 cleanupButton.setEnabled(m_downloads.length - activeDownloads() > 0); |
522 } | 518 } |
523 | 519 |
524 private: | 520 private: |
525 | 521 |
526 void addItem(DownloadItem item) | 522 void addItem(DownloadItem item) |
527 { | 523 { |
528 item.statusChanged.connect(&this.updateRow); | 524 item.statusChanged.connect(&this.updateRow); |
529 int row = m_downloads.count(); | 525 int row = m_downloads.length; |
530 m_model.beginInsertRows(QModelIndex(), row, row); | 526 m_model.beginInsertRows(QModelIndex(), row, row); |
531 m_downloads.append(item); | 527 m_downloads ~= item; |
532 m_model.endInsertRows(); | 528 m_model.endInsertRows(); |
533 updateItemCount(); | 529 updateItemCount(); |
534 if (row == 0) | 530 if (row == 0) |
535 show(); | 531 show(); |
536 downloadsView.setIndexWidget(m_model.index(row, 0), item); | 532 downloadsView.setIndexWidget(m_model.index(row, 0), item); |
540 } | 536 } |
541 | 537 |
542 | 538 |
543 void updateItemCount() | 539 void updateItemCount() |
544 { | 540 { |
545 int count = m_downloads.count(); | 541 int count = m_downloads.length; |
546 itemCount.setText(count == 1 ? tr("1 Download") : tr("%1 Downloads").arg(count)); | 542 itemCount.setText(count == 1 ? tr("1 Download") : tr("{} Downloads").arg(count)); |
547 } | 543 } |
548 | 544 |
549 void load() | 545 void load() |
550 { | 546 { |
551 QSettings settings; | 547 QSettings settings; |
557 QMetaEnum removePolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("RemovePolicy")); | 553 QMetaEnum removePolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("RemovePolicy")); |
558 m_removePolicy = removePolicyEnum.keyToValue(value) == -1 ? Never : | 554 m_removePolicy = removePolicyEnum.keyToValue(value) == -1 ? Never : |
559 cast(RemovePolicy) removePolicyEnum.keyToValue(value); | 555 cast(RemovePolicy) removePolicyEnum.keyToValue(value); |
560 | 556 |
561 int i = 0; | 557 int i = 0; |
562 QString key = QString(QLatin1String("download_%1_")).arg(i); | 558 string key = Format(QLatin1String("download_{}_"), i); |
563 while (settings.contains(key + QLatin1String("url"))) { | 559 while (settings.contains(key + QLatin1String("url"))) { |
564 QUrl url = settings.value(key + QLatin1String("url")).toUrl(); | 560 QUrl url = settings.value(key + QLatin1String("url")).toUrl(); |
565 QString fileName = settings.value(key + QLatin1String("location")).toString(); | 561 string fileName = settings.value(key + QLatin1String("location")).toString(); |
566 bool done = settings.value(key + QLatin1String("done"), true).toBool(); | 562 bool done = settings.value(key + QLatin1String("done"), true).toBool(); |
567 if (!url.isEmpty() && !fileName.isEmpty()) { | 563 if (!url.isEmpty() && !fileName.isEmpty()) { |
568 DownloadItem item = new DownloadItem(0, this); | 564 DownloadItem item = new DownloadItem(0, this); |
569 item.m_output.setFileName(fileName); | 565 item.m_output.setFileName(fileName); |
570 item.fileNameLabel.setText(QFileInfo(item.m_output.fileName()).fileName()); | 566 item.fileNameLabel.setText((new QFileInfo(item.m_output.fileName())).fileName()); |
571 item.m_url = url; | 567 item.m_url = url; |
572 item.stopButton.setVisible(false); | 568 item.stopButton.setVisible(false); |
573 item.stopButton.setEnabled(false); | 569 item.stopButton.setEnabled(false); |
574 item.tryAgainButton.setVisible(!done); | 570 item.tryAgainButton.setVisible(!done); |
575 item.tryAgainButton.setEnabled(!done); | 571 item.tryAgainButton.setEnabled(!done); |
576 item.progressBar.setVisible(!done); | 572 item.progressBar.setVisible(!done); |
577 addItem(item); | 573 addItem(item); |
578 } | 574 } |
579 key = QString(QLatin1String("download_%1_")).arg(++i); | 575 key = Format(QLatin1String("download_{}_"), ++i); |
580 } | 576 } |
581 cleanupButton.setEnabled(m_downloads.count() - activeDownloads() > 0); | 577 cleanupButton.setEnabled(m_downloads.length - activeDownloads() > 0); |
582 } | 578 } |
583 | 579 |
584 AutoSaver m_autoSaver; | 580 AutoSaver m_autoSaver; |
585 DownloadModel m_model; | 581 DownloadModel m_model; |
586 QNetworkAccessManager m_manager; | 582 QNetworkAccessManager m_manager; |
604 QVariant data(QModelIndex index, int role = Qt.DisplayRole) | 600 QVariant data(QModelIndex index, int role = Qt.DisplayRole) |
605 { | 601 { |
606 if (index.row() < 0 || index.row() >= rowCount(index.parent())) | 602 if (index.row() < 0 || index.row() >= rowCount(index.parent())) |
607 return QVariant(); | 603 return QVariant(); |
608 if (role == Qt.ToolTipRole) | 604 if (role == Qt.ToolTipRole) |
609 if (!m_downloadManager.m_downloads.at(index.row()).downloadedSuccessfully()) | 605 if (!m_downloadManager.m_downloads[index.row()].downloadedSuccessfully()) |
610 return m_downloadManager.m_downloads.at(index.row()).downloadInfoLabel.text(); | 606 return m_downloadManager.m_downloads[index.row()].downloadInfoLabel.text(); |
611 return QVariant(); | 607 return QVariant(); |
612 } | 608 } |
613 | 609 |
614 int rowCount(QModelIndex parent = QModelIndex()) | 610 int rowCount(QModelIndex parent = QModelIndex()) |
615 { | 611 { |
616 return (parent.isValid()) ? 0 : m_downloadManager.m_downloads.count(); | 612 return (parent.isValid()) ? 0 : m_downloadManager.m_downloads.length; |
617 } | 613 } |
618 | 614 |
619 bool removeRows(int row, int count, QModelIndex parent = QModelIndex()) | 615 bool removeRows(int row, int count, QModelIndex parent = QModelIndex()) |
620 { | 616 { |
621 if (parent.isValid()) | 617 if (parent.isValid()) |
622 return false; | 618 return false; |
623 | 619 |
624 int lastRow = row + count - 1; | 620 int lastRow = row + count - 1; |
625 for (int i = lastRow; i >= row; --i) { | 621 for (int i = lastRow; i >= row; --i) { |
626 if (m_downloadManager.m_downloads.at(i).downloadedSuccessfully() | 622 if (m_downloadManager.m_downloads[i].downloadedSuccessfully() |
627 || m_downloadManager.m_downloads.at(i).tryAgainButton.isEnabled()) { | 623 || m_downloadManager.m_downloads[i].tryAgainButton.isEnabled()) { |
628 beginRemoveRows(parent, i, i); | 624 beginRemoveRows(parent, i, i); |
629 m_downloadManager.m_downloads.takeAt(i).deleteLater(); | 625 m_downloadManager.m_downloads.takeAt(i).deleteLater(); |
630 endRemoveRows(); | 626 endRemoveRows(); |
631 } | 627 } |
632 } | 628 } |