forked from saa7go/qDebDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.cpp
More file actions
889 lines (787 loc) · 31.4 KB
/
form.cpp
File metadata and controls
889 lines (787 loc) · 31.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/* This file is part of qDebDownloader.
*
* Copyright (c) 2011 - Christian Kurniawan Ginting S. <saa7_go@terralinux.org>
*
* qDebDownloader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qDebDownloader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qDebDownloader. If not, see <http://www.gnu.org/licenses/>. */
#include "form.h"
#include "ui_form.h"
#include <QTimer>
#include <QDesktopWidget>
#include <QMessageBox>
#include <QActionGroup>
#include <QFile>
#include <QInputDialog>
#include <QFileDialog>
#include <QSettings>
#include <QNetworkAccessManager>
#include <QDebug>
#include "aptweb.h"
#include "downloadtablemodel.h"
#include "downloader.h"
#include "progressbardelegate.h"
#include "proxydialog.h"
#include "aboutdialog.h"
#include "filesizedownloader.h"
#include "helper.h"
#include "sortproxymodel.h"
#define FILESIZE_GETTER_COUNT 6
Form::Form(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Form),
m_manager(new QNetworkAccessManager(this)),
m_model(new DownloadTableModel(this)),
m_proxyModel(new SortProxyModel(this)),
m_timer(new QTimer(this)),
m_maksFileSizeCount(FILESIZE_GETTER_COUNT)
{
ui->setupUi(this);
initRepoComboBoxes();
initActions();
initProxy();
initConcurrentConnection();
// inisialiasi model pada treeview
m_proxyModel->setSourceModel(m_model);
ui->treeView->setModel(m_proxyModel);
ui->treeView->header()->setResizeMode(0, QHeaderView::ResizeToContents);
ui->treeView->setItemDelegateForColumn(DownloadTableModel::COL_PROGRESS, new ProgressBarDelegate(this)); // progress
ui->treeView->header()->hideSection(DownloadTableModel::COL_URL); // url
ui->treeView->header()->hideSection(DownloadTableModel::COL_STATUS); // status
ui->treeView->header()->setMovable(false);
m_selectionModel = ui->treeView->selectionModel();
ui->cariPaketLineEdit->setFocus();
ui->buttonWidget->hide();
m_timer->setInterval(500);
connect(ui->actionTentang_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), SLOT(setTotalFileSize(QModelIndex)));
connect(m_timer, SIGNAL(timeout()), SLOT(timerTimeout()));
// ganti font
#ifdef Q_WS_WIN
// note: di windows xp default font terlihat sangat kecil
// jadi saya coba set ke 10
QFont tmpFont = qApp->font();
tmpFont.setPointSize(10);
qApp->setFont(tmpFont);
#endif
}
Form::~Form()
{
writeSettings();
delete ui;
qDebug() << this << "destroyed!";
}
void Form::initRepoComboBoxes()
{
QComboBox *cb = ui->distribusiComboBox;
cb->addItem("Ubuntu 10.10 \"Maverick Meerkat\" i386", 0);
cb->addItem("Ubuntu 10.10 \"Maverick Meerkat\" amd64", 1);
cb->addItem("Ubuntu 10.04 \"Lucid Lynx\" i386", 2);
cb->addItem("Ubuntu 10.04 \"Lucid Lynx\" amd64", 3);
cb->addItem("Ubuntu 9.10 \"Karmic Koala\" i386", 4);
cb->addItem("Ubuntu 9.04 \"Jaunty Jackalope\" i386", 5);
cb->addItem("Ubuntu 8.10 \"Intrepid Ibex\" i386", 6);
cb->addItem( "Ubuntu 8.04 \"Hardy Heron\" i386", 7);
cb->addItem("Ubuntu 7.10 \"Gutsy Gibbon\" i386", 8);
cb->addItem("XUbuntu 7.10 \"Gutsy Gibbon\" i386", 9);
cb->addItem("Ubuntu 7.04 \"Feisty Fawn\" i386", 10);
cb->addItem("Ubuntu 6.10 \"Edgy Eftt\" Server i386", 11);
cb->addItem("Ubuntu 6.06 \"Dapper Drake\" i386", 12);
}
void Form::initActions()
{
QAction *action = new QAction(ui->cariPaketLineEdit);
action->setAutoRepeat(false);
action->setShortcut(QKeySequence(Qt::Key_Return));
connect(action, SIGNAL(triggered()), this, SLOT(on_cariButton_clicked()));
ui->cariPaketLineEdit->addAction(action);
repoGroup = new QActionGroup(this);
QAction *kambingUIAction = repoGroup->addAction("Kambing UI");
kambingUIAction->setData(0);
QAction *fossIDAction = repoGroup->addAction("FOSS-id");
fossIDAction->setData(1);
QAction *komoVlsmAction = repoGroup->addAction("Komo vlsm.org");
komoVlsmAction->setData(2);
QAction *indikaNetAction = repoGroup->addAction("Indika.Net");
indikaNetAction->setData(3);
QAction *unejAction = repoGroup->addAction("Unej");
unejAction->setData(4);
QAction *ugmAction = repoGroup->addAction("UGM");
ugmAction->setData(5);
QAction *itsAction = repoGroup->addAction("ITS");
itsAction->setData(6);
QAction *buayaKlasAction = repoGroup->addAction("Buaya KLAS");
buayaKlasAction->setData(7);
QAction *keboVlsmAction = repoGroup->addAction("Kebo vlsm.org");
keboVlsmAction->setData(8);
QAction *itbAction = repoGroup->addAction("ITB");
itbAction->setData(9);
QAction *shloVlsmAction = repoGroup->addAction("SHLO vlsm.org");
shloVlsmAction->setData(10);
for(int i = 0; i < repoGroup->actions().count(); i++)
repoGroup->actions()[i]->setCheckable(true);
QMenu *menuRepositori = new QMenu("Repositori", this);
ui->menu_Pengaturan->insertMenu(ui->actionFolderUnduhan, menuRepositori);
menuRepositori->addActions(repoGroup->actions());
QSettings settings("./apt-web.ini", QSettings::IniFormat);
m_repoID = settings.value("mirror-repo", 0).toInt();
if(m_repoID < 0)
m_repoID = 1;
else if(m_repoID > (repoGroup->actions().count() - 1))
m_repoID = repoGroup->actions().count() - 1;
for(int i = 0; i < repoGroup->actions().count(); i++)
{
if(repoGroup->actions().at(i)->data().toInt() == m_repoID)
{
repoGroup->actions()[i]->setChecked(true);
break;
}
}
connect(repoGroup, SIGNAL(triggered(QAction*)), SLOT(repoChanged(QAction*)));
}
void Form::initProxy()
{
QSettings settings("./apt-web.ini", QSettings::IniFormat);
int idxProxy = settings.value("Proxy/Type", 0).toInt();
if(idxProxy < 0 || idxProxy > 3)
idxProxy = 0;
QNetworkProxy proxy;
if(idxProxy == 0)
proxy.setType(QNetworkProxy::NoProxy);
else if(idxProxy == 1)
proxy.setType(QNetworkProxy::DefaultProxy);
else {
if(idxProxy == 2)
proxy.setType(QNetworkProxy::HttpProxy);
else if(idxProxy == 3)
proxy.setType(QNetworkProxy::Socks5Proxy);
else
return;
proxy.setHostName(settings.value("Proxy/Host").toString());
proxy.setPort(settings.value("Proxy/Port", 0).toInt());
proxy.setUser(settings.value("Proxy/Username").toString());
proxy.setPassword(settings.value("Proxy/Password").toString());
}
m_manager->setProxy(proxy);
}
void Form::initConcurrentConnection()
{
QSettings settings("./apt-web.ini", QSettings::IniFormat);
int maks_concurrent = settings.value("maks-concurrent", 1).toInt();
if(maks_concurrent < 1)
maks_concurrent = 1;
else if (maks_concurrent > 6)
maks_concurrent = 6;
m_maks_concurrent = maks_concurrent;
}
void Form::on_cariButton_clicked()
{
if(ui->cariPaketLineEdit->text().isEmpty())
return;
m_model->setUrlList(QStringList());
ui->terunduhLabel->setText("0 B");
ui->cariButton->setEnabled(false);
ui->unduhButton->setEnabled(false);
ui->cariPaketLineEdit->setEnabled(false);
ui->statusbar->showMessage(tr("Mencari paket-paket yg dibutuhkan..."));
ui->pilihSemuaButton->setEnabled(false);
ui->kosongkanSemuaButton->setEnabled(false);
AptWeb::Distribution dist;
switch(ui->distribusiComboBox->itemData(ui->distribusiComboBox->currentIndex()).toInt())
{
case 0:
dist = AptWeb::Ubuntu_MaverickMeerkat_i386; break;
case 1:
dist = AptWeb::Ubuntu_MaverickMeerkat_amd64; break;
case 2:
dist = AptWeb::Ubuntu_LucidLynx_i386; break;
case 3:
dist = AptWeb::Ubuntu_LucidLynx_amd64; break;
case 4:
dist = AptWeb::Ubuntu_KarmicKoala_i386; break;
case 5:
dist = AptWeb::Ubuntu_JauntyJackalope_i386; break;
case 6:
dist = AptWeb::Ubuntu_IntrepidIbex_i386; break;
case 7:
dist = AptWeb::Ubuntu_HardyHeron_i386; break;
case 8:
dist = AptWeb::Ubuntu_GutsyGibbon_i386; break;
case 9:
dist = AptWeb::XUbuntu_GutsyGibbon_i386; break;
case 10:
dist = AptWeb::Ubuntu_FeistyFawn_i386; break;
case 11:
dist = AptWeb::Ubuntu_EdgyEft_Server_i386; break;
case 12:
dist = AptWeb::Ubuntu_DapperDrake_i386; break;
}
AptWeb *aptWeb = new AptWeb(m_manager, this);
aptWeb->setRepoId(m_repoID);
connect(aptWeb, SIGNAL(downloadList(QStringList)), this, SLOT(downloadList(QStringList)));
connect(aptWeb, SIGNAL(error(QString)), this, SLOT(error(QString)));
aptWeb->findPackage(ui->cariPaketLineEdit->text().trimmed(), dist);
ui->totalLabel->setText("0 B");
}
void Form::downloadList(const QStringList &packages)
{
sender()->setParent(0);
sender()->deleteLater();
m_model->setUrlList(packages);
if(packages.count() > 0)
{
getFileSize();
ui->statusbar->showMessage("Mendapatkan ukuran file yang akan diunduh...");
}
else
{
ui->unduhButton->setEnabled(false);
ui->pilihSemuaButton->setEnabled(false);
ui->kosongkanSemuaButton->setEnabled(false);
ui->cariButton->setEnabled(true);
ui->cariPaketLineEdit->setEnabled(true);
ui->statusbar->showMessage(tr("Paket telah terinstall atau anda salah memasukkan nama paket yang dicari"), 5000);
}
}
void Form::on_unduhButton_clicked()
{
if(m_proxyModel->rowCount() == 0)
return;
m_queue.clear();
m_downloaderList.clear();
for(int i = 0; i < m_proxyModel->rowCount(); i++)
{
QModelIndex idx = m_proxyModel->mapToSource(m_proxyModel->index(i, DownloadTableModel::COL_CHECHED)); // checked or not
int val = idx.data(Qt::CheckStateRole).toInt();
if(val > 0)
m_queue.enqueue(idx.row());
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_STATUS), DownloadData::Nothing);
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_PROGRESS), 0); // progress
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_CURRENT_SIZE), QString(), Qt::DisplayRole); // pastikan kalau ada info error, info tersebut dihapus
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_CURRENT_SIZE), 0); // current filesize
}
// jika antrian unduhan tidak ada, keluar dari fungsi ini
if(m_queue.count() == 0)
{
QMessageBox::warning(this, tr("Unduhan"), tr("Harap pilih minimal satu paket yang ingin anda untuh."));
return;
}
statusBar()->clearMessage();
m_model->setCheckEnabled(false);
ui->cariButton->setEnabled(false);
ui->unduhButton->setEnabled(false);
ui->pilihSemuaButton->setEnabled(false);
ui->kosongkanSemuaButton->setEnabled(false);
ui->cariPaketLineEdit->setEnabled(false);
ui->buttonWidget->show();
qApp->processEvents();
downloaderMap.clear(); // kosongkan pemetaan objek kelas Downloader dengan QModelIndex sebelumnya
for(int i = 0; i < m_maks_concurrent; i++)
{
Downloader *downloader = new Downloader(m_manager, this);
downloader->setObjectName(QString("Downloader-%1").arg(i));
m_downloaderList.enqueue(downloader);
connect(downloader, SIGNAL(error(QModelIndex,QString)), SLOT(slotError(QModelIndex,QString)));
connect(downloader, SIGNAL(downloadSkipped(QModelIndex,QString)), SLOT(slotDownloadSkipped(QModelIndex,QString)));
connect(downloader, SIGNAL(progressSize(QModelIndex,qint64)), SLOT(slotProgressSize(QModelIndex,qint64)));
connect(downloader, SIGNAL(progressDownload(QModelIndex,int)), SLOT(slotProgressDownload(QModelIndex,int)));
connect(downloader, SIGNAL(downloadFinish()), SLOT(slotDownloadFinished()));
}
ui->treeView->header()->hideSection(DownloadTableModel::COL_CHECHED);
m_proxyModel->setHideUncheked(true);
m_timer->start();
while(m_queue.count() > 0)
{
if(m_downloaderList.count() > 0)
{
int curr = m_queue.dequeue();
Downloader *dl = m_downloaderList.dequeue();
QModelIndex idx = m_model->index(curr, DownloadTableModel::COL_URL); // url
downloaderMap[dl] = idx;
dl->download(idx);
}
else {
// jika Downloader telah terpakai semua
// keluar dari loop
break;
}
}
qDebug() << "RunVerify Downloads";
verifyDownloads(); // cek lagi unduhannya
qDebug() << "Verify Downloads?";
}
void Form::slotDownloadFinished()
{
Downloader *downloader = qobject_cast<Downloader*>(sender());
if(downloader) // periksa jika fungsi ini diterima dari objek Downloader
{
QModelIndex idx = downloaderMap.value(downloader);
if(!idx.isValid()) // tidak mengunduh
{
// seharusnya tidak sampai pada baris ini
qDebug() << "slotDownloadFinished: sedang tidak mengunduh!";
}
else {
// downloaderMap.remove(downloader);
downloaderMap[downloader] = QModelIndex();
m_downloaderList.enqueue(downloader);
}
}
qDebug() << "jumlah downloder tersisa:" << m_downloaderList.count();
verifyDownloads();
}
void Form::slotProgressDownload(const QModelIndex &idx, int progress)
{
if(!idx.isValid())
return;
QModelIndex progIdx = m_model->index(idx.row(), DownloadTableModel::COL_PROGRESS); // progress
bool retVal = m_model->setData(progIdx, progress);
if(!retVal)
qDebug() << "Error";
}
void Form::slotProgressSize(const QModelIndex &idx, qint64 size)
{
if(!idx.isValid())
return;
QModelIndex sizeIdx = m_model->index(idx.row(), DownloadTableModel::COL_CURRENT_SIZE); // current file size
bool retVal = m_model->setData(sizeIdx, size, Qt::EditRole);
if(!retVal)
qDebug() << "Error";
}
void Form::on_pilihSemuaButton_clicked()
{
for(int i = 0; i < m_model->rowCount(); i++)
m_model->setData(m_model->index(i, DownloadTableModel::COL_CHECHED), Qt::Checked, Qt::CheckStateRole); // checked?
}
void Form::on_kosongkanSemuaButton_clicked()
{
for(int i = 0; i < m_model->rowCount(); i++)
m_model->setData(m_model->index(i, DownloadTableModel::COL_CHECHED), Qt::Unchecked, Qt::CheckStateRole); // checked ?
}
void Form::slotError(const QModelIndex &idx, const QString &error)
{
// kembalikan objek Downloader ke dalam antrian
Downloader *dl = qobject_cast<Downloader*>(sender());
if(dl) {
downloaderMap[dl] = QModelIndex();
m_downloaderList.enqueue(dl);
}
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_STATUS), DownloadData::Canceled); // status
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_PROGRESS), 100); // progress
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_CURRENT_SIZE), error, Qt::DisplayRole); // current file size info
verifyDownloads();
}
void Form::slotDownloadSkipped(const QModelIndex &idx, const QString &reason)
{
qDebug() << idx.data(Qt::EditRole).toString() << "skipped!";
Downloader *dl = qobject_cast<Downloader*>(sender());
if(dl) {
downloaderMap[dl] = QModelIndex();
m_downloaderList.enqueue(dl);
}
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_PROGRESS), 100);
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_STATUS), DownloadData::Finished);
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_CURRENT_SIZE), reason, Qt::DisplayRole);
verifyDownloads();
}
void Form::repoChanged(QAction *action)
{
m_repoID = action->data().toInt();
}
void Form::setMaksConcurrent(int val)
{
m_maks_concurrent = val;
}
void Form::writeSettings()
{
QSettings settings("./apt-web.ini", QSettings::IniFormat);
settings.setValue("mirror-repo", m_repoID);
}
void Form::on_actionJumlahUnduhan_triggered()
{
bool ok;
int val = QInputDialog::getInt(this, "Jumlah unduhan", "Nilai(1-6)",
m_maks_concurrent, 1, 6, 1, &ok);
if(ok)
{
setMaksConcurrent(val);
QSettings settings("./apt-web.ini", QSettings::IniFormat);
settings.setValue("maks-concurrent", val);
}
}
void Form::on_actionFolderUnduhan_triggered()
{
QSettings settings("./apt-web.ini", QSettings::IniFormat);
QString dir = QFileDialog::getExistingDirectory(this, "Direktori Unduhan", settings.value("lokasi-folder-unduhan", QString()).toString());
if(dir.isEmpty())
return;
settings.setValue("lokasi-folder-unduhan", dir);
}
void Form::on_actionProxy_triggered()
{
ProxyDialog dialog(this);
int val = dialog.exec();
if(val == QDialog::Accepted)
m_manager->setProxy(dialog.proxy());
}
void Form::error(const QString &error)
{
sender()->deleteLater();
QMessageBox::critical(this, "Error", error);
ui->cariButton->setEnabled(true);
ui->cariPaketLineEdit->setEnabled(true);
ui->statusbar->clearMessage();
}
void Form::on_actionTentang_activated()
{
AboutDialog dlg(this);
dlg.exec();
}
void Form::getFileSize()
{
for(int i = 0; i < m_model->rowCount(); i++)
m_sizeQueue.enqueue(m_model->index(i, DownloadTableModel::COL_URL)); // url
m_maksFileSizeCount = (m_sizeQueue.count() < FILESIZE_GETTER_COUNT ? m_sizeQueue.count() : FILESIZE_GETTER_COUNT);
for(int i = 0; i < m_maksFileSizeCount; i++)
{
FileSizeDownloader *fileSizeDownloader = new FileSizeDownloader(m_manager, this);
fileSizeDownloader->setObjectName(QString("FileSizeDownloader-%1").arg(i));
connect(fileSizeDownloader, SIGNAL(sizeReceived(QModelIndex,qint64)), SLOT(slotSizeReceived(QModelIndex,qint64)));
m_fileSizeQueue.enqueue(fileSizeDownloader);
}
while(m_sizeQueue.count() > 0)
{
if(m_fileSizeQueue.count() > 0)
m_fileSizeQueue.dequeue()->getSize(m_sizeQueue.dequeue());
else
break;
}
}
void Form::slotSizeReceived(const QModelIndex &idx, qint64 size)
{
FileSizeDownloader *fileSizeDownloader = qobject_cast<FileSizeDownloader*>(sender());
m_fileSizeQueue.enqueue(fileSizeDownloader);
QModelIndex fileSizeIndex = m_model->index(idx.row(), DownloadTableModel::COL_TARGET_SIZE); // real file size
m_model->setData(fileSizeIndex, size);
if(m_sizeQueue.count() > 0)
m_fileSizeQueue.dequeue()->getSize(m_sizeQueue.dequeue());
if(m_fileSizeQueue.count() == m_maksFileSizeCount
&& m_sizeQueue.count() == 0) // sudah selesai
{
while(m_fileSizeQueue.count() > 0)
{
QObject *obj = m_fileSizeQueue.dequeue();
obj->setParent(0);
obj->deleteLater();
}
ui->cariPaketLineEdit->setEnabled(true);
ui->cariButton->setEnabled(true);
ui->unduhButton->setEnabled(true);
ui->pilihSemuaButton->setEnabled(true);
ui->kosongkanSemuaButton->setEnabled(true);
ui->statusbar->showMessage(QString("Daftar paket yang dibutuhkan berjumlah %1 dengan total ukuran %2").arg(m_model->rowCount()).arg(ui->totalLabel->text()), 5000);
}
}
// perlu dibenahi
void Form::setTotalFileSize(const QModelIndex &idx)
{
if (idx.column() == DownloadTableModel::COL_TARGET_SIZE
|| idx.column() == DownloadTableModel::COL_CURRENT_SIZE
|| idx.column() == DownloadTableModel::COL_CHECHED)
{
int column;
qint64 total = 0;
double dTotal;
QString ukuran;
if(idx.column() == DownloadTableModel::COL_TARGET_SIZE || idx.column() == DownloadTableModel::COL_CHECHED)
column = 6;
else
column = 5;
for(int i = 0; i < m_model->rowCount(); i++)
{
if(m_model->index(i, DownloadTableModel::COL_CHECHED).data(Qt::CheckStateRole).toInt() > 0)
total += m_model->index(i, column).data(Qt::EditRole).toLongLong();
}
AptOffline::caculateSize(total, &dTotal, &ukuran);
if(column == DownloadTableModel::COL_CURRENT_SIZE)
ui->terunduhLabel->setText(QString("%1 %2").arg(QString::number(dTotal, 'f', 2)).arg(ukuran));
if(column == DownloadTableModel::COL_TARGET_SIZE)
ui->totalLabel->setText(QString("%1 %2").arg(QString::number(dTotal, 'f', 2)).arg(ukuran));
}
}
void Form::on_lanjutkanButton_clicked()
{
// if(!ui->treeView->currentIndex().isValid())
// return;
if(!m_selectionModel->hasSelection())
return;
if(m_selectionModel->selectedIndexes().count() == 0)
return;
qDebug() << "Lanjutkan";
ui->lanjutkanButton->setEnabled(false);
ui->hentikanButton->setEnabled(true);
ui->batalButton->setEnabled(true);
// QModelIndex idx = m_proxyModel->mapToSource(ui->treeView->currentIndex());
QModelIndex idx = m_proxyModel->mapToSource(m_selectionModel->selectedIndexes().at(0));
if(!idx.isValid())
return;
idx = m_model->index(idx.row(), DownloadTableModel::COL_STATUS); // status
if(idx.data(Qt::EditRole).toInt() != DownloadData::Paused) // cek jika status unduha dalam keadaan PAUSE atau tidak
return;
idx = m_model->index(idx.row(), DownloadTableModel::COL_URL); // url
if(!idx.isValid())
{
qDebug() << "Tidak valid!";
return;
}
// cek jika ada Downloader yg nganggur
if(m_downloaderList.count() > 0)
{
// jika ada, melanjutkan unduhan
Downloader *dl = m_downloaderList.dequeue();
dl->download(idx);
downloaderMap[dl] = idx;
}
else // jika Downloader terpakai semua
{
// ganti status menjadi WAITING
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_STATUS), DownloadData::Waiting); // status
// masukkan kembali ke antrian unduhan
m_queue.prepend(idx.row());
}
}
void Form::on_hentikanButton_clicked()
{
// if(!ui->treeView->currentIndex().isValid())
// return;
if(!m_selectionModel->hasSelection())
return;
if(m_selectionModel->selectedIndexes().count() == 0)
return;
ui->hentikanButton->setEnabled(false);
ui->lanjutkanButton->setEnabled(true);
ui->batalButton->setEnabled(true);
QModelIndex idx = m_proxyModel->mapToSource(m_selectionModel->selectedIndexes().at(0));
idx = idx.model()->index(idx.row(), DownloadTableModel::COL_STATUS); // status
// cek apakah benar sedang mengunduh
if(idx.data(Qt::EditRole).toInt() != DownloadData::Downloading)
return;
idx = m_model->index(idx.row(), DownloadTableModel::COL_URL); // url
if(!idx.isValid())
{
qDebug() << "Ada error";
return;
}
Downloader *dl = downloaderMap.key(idx);
if(dl == 0)
{
qDebug() << "Downloader tidak ditemukan saat melakukan pause!";
return;
}
qDebug() << "Pause unduhan" << idx.data().toString();
dl->pause();
}
void Form::on_batalButton_clicked()
{
if(m_selectionModel->hasSelection() && m_selectionModel->selectedIndexes().count() > 0)
{
QModelIndex idx = m_proxyModel->mapToSource(m_selectionModel->selectedIndexes().at(0));
idx = m_model->index(idx.row(), DownloadTableModel::COL_STATUS); // status
int status = idx.data(Qt::EditRole).toInt();
idx = m_model->index(idx.row(), DownloadTableModel::COL_URL); // url
// jika unduhan dalam keadaan sedang mengunduh
if(status == DownloadData::Downloading)
{
Downloader *dl = downloaderMap.key(idx);
// pastikan Downloader valid
if(dl != 0)
{
qDebug() << "Batal";
dl->cancel(); // batalkan unduhan, pada kondisi ini penghapusan file dikelola oleh objek Downloader
}
else
{
qDebug() << "Downloader tidak ditemukan!";
}
}
// jika unduhan dalam keadaan PAUSED atau menunggu untuk diunduh
else if(status == DownloadData::Waiting || status == DownloadData::Paused)
{
QSettings settings("./apt-web.ini", QSettings::IniFormat);
QDir downloadDir(settings.value("lokasi-folder-unduhan", QString()).toString());
QString filePath = m_model->index(idx.row(), DownloadTableModel::COL_PACKAGE_NAME).data().toString(); // default di current dir
if(downloadDir.exists())
filePath = QDir::toNativeSeparators(downloadDir.path()) + QDir::separator() + filePath;
QFile file(filePath);
if(file.exists())
{
// jika status unduhan dalam keadaan menunggu
// maka antrian yg sebelumnya ditambahkan harus dihapus
if(status == DownloadData::Waiting)
m_queue.removeAll(idx.row());
// file.remove(); // hapus unduhan yg dibatalkan
// ganti status unduhan menjadi CANCELED
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_STATUS), DownloadData::Canceled); // status
m_model->setData(m_model->index(idx.row(), DownloadTableModel::COL_PROGRESS), 100); // progress
}
// cek jika sudah tidak ada unduhan
// if(m_queue.count() == 0)
verifyDownloads();
}
}
}
void Form::slotCurrentRowChanged(const QModelIndex ¤t, const QModelIndex &)
{
if(current.isValid())
changeStatusButton(m_proxyModel->mapToSource(current));
}
void Form::changeStatusButton(const QModelIndex &idx)
{
if(idx.isValid())
{
int status = m_model->index(idx.row(), DownloadTableModel::COL_STATUS).data(Qt::EditRole).toInt(); // status
if(status == 0 || status == 2 || status == 4)
{
ui->lanjutkanButton->setEnabled(false);
ui->hentikanButton->setEnabled(false);
ui->batalButton->setEnabled(false);
}
else if(status == 1)
{
ui->lanjutkanButton->setEnabled(false);
ui->hentikanButton->setEnabled(true);
ui->batalButton->setEnabled(true);
}
else if(status == 3)
{
ui->lanjutkanButton->setEnabled(true);
ui->hentikanButton->setEnabled(false);
ui->batalButton->setEnabled(true);
}
}
}
void Form::timerTimeout()
{
if(!m_selectionModel->hasSelection())
return;
if(m_selectionModel->selectedRows().count() == 0)
return;
QModelIndex idx = m_selectionModel->selectedRows().at(0);
idx = m_proxyModel->mapToSource(idx);
changeStatusButton(idx);
}
void Form::makeCenter()
{
QSize sz = qApp->desktop()->size();
int x = (sz.width() - width()) / 2;
int y = (sz.height() - height()) / 2;
move(x, y);
}
void Form::verifyDownloads()
{
qDebug() << "queue:" << m_queue;
/*
// ================
QSettings settings("./apt-web.ini", QSettings::IniFormat);
QString dirPath = settings.value("lokasi-folder-unduhan", QString()).toString();
QString fileName;
// ================
*/
if(m_queue.count() > 0)
{
if(m_downloaderList.count() > 0)
{
int curr = m_queue.dequeue();
/*
// ========================
fileName = m_model->index(curr, DownloadTableModel::COL_PACKAGE_NAME).data().toString();
if(!dirPath.isEmpty())
{
QDir dir(dirPath);
if(dir.exists())
fileName = QDir::toNativeSeparators(dir.path()) + QDir::separator() + fileName;
}
QFileInfo fInfo(fileName);
if(fInfo.exists()) // cek jika file ada atau tidak
{
qDebug() << fInfo.filePath() << ":" << (fInfo.exists() ? "Exist" : "Not Exists");
qint64 fSize = fInfo.size();
qint64 tSize = m_model->index(curr, DownloadTableModel::COL_TARGET_SIZE).data(Qt::EditRole).toLongLong();
qDebug() << fSize << ":" << tSize;
if(fSize == tSize) // bandingkan ukuran file di lokal dan ukuran di url
{
qDebug() << "File udah terdonlot, Skipped!";
m_model->setData(m_model->index(curr, DownloadTableModel::COL_STATUS), DownloadData::Finished);
m_model->setData(m_model->index(curr, DownloadTableModel::COL_PROGRESS), 100);
m_model->setData(m_model->index(curr, DownloadTableModel::COL_CURRENT_SIZE), fSize);
m_model->setData(m_model->index(curr, DownloadTableModel::COL_CURRENT_SIZE), "File Sudah Terdownload Sebelumnya", Qt::DisplayRole);
return;
}
else if (fSize < tSize){ // file sudah diunduh tapi belum kelar (anggapannya seperti itu)
qDebug() << "Belum kelar donlot!";
m_model->setData(m_model->index(curr, DownloadTableModel::COL_STATUS), DownloadData::Waiting);
m_model->setData(m_model->index(curr, DownloadTableModel::COL_CURRENT_SIZE), fSize);
}
}
// =========================
*/
QModelIndex idx = m_model->index(curr, DownloadTableModel::COL_URL); // url
Downloader *dl = m_downloaderList.dequeue();
// m_model->setData(
// m_model->index(curr, DownloadTableModel::COL_STATUS),
// DownloadData::Downloading); // status
downloaderMap[dl] = idx;
dl->download(idx);
qDebug() << dl->objectName() << idx;
qDebug() << "jumlah downloader sekarang:" << m_downloaderList.count();
}
}
else
{
// kondisi di mana objek unduhan sedang nganggur
// semua, cek bila ada unduhan yang sedang di-paused.
if(m_downloaderList.count() == m_maks_concurrent)
{
// loop untuk memastikan tidak ada unduhan yg masih PAUSED
for(int i = 0; i < m_model->rowCount(); i++)
{
if(m_model->index(i, DownloadTableModel::COL_CHECHED).data(Qt::CheckStateRole).toInt() == 0) // checked
continue; // abaikan index yg tidak dipilih untuk diunduh
else {
// cek jika masih ada unduhan yang di-paused
if(m_model->index(i, DownloadTableModel::COL_STATUS).data(Qt::EditRole).toInt() == DownloadData::Paused) // status
return; // keluar dari fungsi ini
}
}
// semua unduhan telah telah selesai diunduh
while(m_downloaderList.count() > 0)
{
// saatnya menhancurkan objek unduhan
QObject *obj = m_downloaderList.dequeue();
obj->setParent(0);
obj->deleteLater();
}
// hentikan timer untuk mengecek status unduhan
m_timer->stop();
ui->unduhButton->setEnabled(true);
ui->cariButton->setEnabled(true);
m_model->setCheckEnabled(true);
ui->pilihSemuaButton->setEnabled(true);
ui->kosongkanSemuaButton->setEnabled(true);
ui->cariPaketLineEdit->setEnabled(true);
ui->buttonWidget->hide();
ui->statusbar->showMessage(tr("Semua paket telah diunduh..."));
m_proxyModel->setHideUncheked(false);
ui->treeView->header()->showSection(DownloadTableModel::COL_CHECHED);
}
}
}