-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathfiber_data_hub.cpp
More file actions
1009 lines (899 loc) · 37.5 KB
/
Copy pathfiber_data_hub.cpp
File metadata and controls
1009 lines (899 loc) · 37.5 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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QDir>
#include <QDate>
#include <QDateTime>
#include <QCoreApplication>
#include <QEventLoop>
#include <QFile>
#include <QFileDialog>
#include <QHeaderView>
#include <QInputDialog>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkReply>
#include <QProgressDialog>
#include <QRegularExpression>
#include <QSignalBlocker>
#include <QStandardPaths>
#include <QTextStream>
#include <QTimer>
#include <QThread>
#include <QUrlQuery>
#include <QVBoxLayout>
#include <QTimer>
#include <set>
#include "fiber_data_hub.hpp"
#include "ui_fiber_data_hub.h"
#include "mainwindow.h"
#include "connectometry/db_window.h"
#include "connectometry/group_connectometry.hpp"
#include "connectometry/group_connectometry_analysis.h"
FiberDataHub::FiberDataHub(MainWindow* parent):
QMainWindow(parent),main_window(*parent),ui(new Ui::FiberDataHub)
{
// Initialize interface
{
ui->setupUi(this);
ui->github_release_note->setCurrentIndex(0);
ui->github_open_file->setVisible(false);
ui->github_open_file_mode->setVisible(false);
ui->download_dir->setText(main_window.work_dir());
ui->github_note->setReadOnly(true);
ui->github_note->setOpenExternalLinks(true);
}
if(!initialize())
QMessageBox::warning(this,"Fiber Data Hub",QString::fromStdString(error_msg));
}
FiberDataHub::~FiberDataHub()
{
delete ui;
}
bool FiberDataHub::initialize()
{
if(fetch_github)
return ui->github_repo->count();
auto fail = [&](const QString& msg)
{
fetch_github = false;
error_msg = msg.toStdString();
return false;
};
fetch_github = true;
try
{
QString content = settings.value("hub_content").toString();
QString url = main_window.fiber_data_hub_url();
// Download or update hub content
{
if(url.isEmpty() && content.isEmpty())
return fail("Fiber Data Hub URL is unavailable");
if(!url.isEmpty())
{
auto reply = main_window.get(url);
if(!reply)
return fail("Fiber Data Hub network request failed");
if(content.isEmpty())
{
QEventLoop loop;
connect(reply.get(),&QNetworkReply::finished,&loop,&QEventLoop::quit);
QTimer::singleShot(30000,&loop,&QEventLoop::quit);
loop.exec();
if(!reply->isFinished())
return reply->abort(),fail("Fiber Data Hub network timeout");
if(reply->error() != QNetworkReply::NoError)
return fail("Fiber Data Hub network error: "+reply->errorString());
content = QString::fromUtf8(reply->readAll());
if(content.isEmpty())
return fail("Fiber Data Hub returned empty content");
settings.setValue("hub_content",content);
}
else
connect(reply.get(),&QNetworkReply::finished,this,[this,reply]
{
if(reply->error() == QNetworkReply::NoError)
settings.setValue("hub_content",QString::fromUtf8(reply->readAll()));
});
}
}
// Parse repository list and build hub note
{
QString md,line;
md.reserve(content.size());
ui->github_repo->clear();
QSignalBlocker block(ui->github_repo);
QTextStream in(&content);
const QString mark = "](https://github.com/";
for(bool first = true;in.readLineInto(&line);first = false)
{
if(first)
continue;
int p = 0;
while(p < line.size() && line[p].isSpace())
++p;
if(line.mid(p).startsWith("<img src"))
continue;
md += line+"\n";
if(!line.startsWith("- "))
continue;
int m = line.indexOf(mark);
if(m < 0)
continue;
int b = line.lastIndexOf('[',m);
int r = m+mark.size();
int s = line.indexOf('/',r);
int e = s < 0 ? -1 : line.indexOf('/',s+1);
if(e < 0 && s >= 0)
e = line.indexOf(')',s+1);
if(b >= 0 && e > s)
ui->github_repo->addItem(line.mid(b+1,m-b-1),line.mid(r,e-r));
}
if(!ui->github_repo->count())
return fail("No Fiber Data Hub repositories found");
ui->github_note->setMarkdown(md);
on_github_repo_currentIndexChanged(0);
}
return true;
}
catch(const std::exception& e)
{
return fail("Fiber Data Hub initialization failed: "+QString::fromUtf8(e.what()));
}
catch(...)
{
return fail("Fiber Data Hub initialization failed");
}
}
bool FiberDataHub::command(const std::vector<std::string>& cmd)
{
error_msg.clear();
if(cmd.empty() || cmd[0].compare(0,4,"hub_"))
return false;
const std::string usage =
"hub_repo | hub_tags <repo> | hub_files <repo> [tag] [text] [offset] [limit] | "
"hub_open <repo> <tag> <file> | hub_show <repo> <tag> [file] | hub_download <repo> [tag] <file> <dir> "
"([tag] and [text] empty means match all; [tag] and [text] are treated as regular expressions; "
"hub_download's <file> is a wildcard pattern (*, ?, [...]), e.g. \"*.qsdr.fz\", matching every "
"file in every matched tag, so one call can download many files; hub_open and hub_show take "
"<tag> as an exact, single tag and <file> as an exact filename or the row index returned by hub_files)";
auto fail = [&](const std::string& msg){error_msg = msg;return false;};
auto arg = [&](size_t i){return QString::fromStdString(cmd[i]);};
if(!initialize())
return false;
auto* repos = ui->github_repo;
auto* tags = ui->github_tags;
auto* files = ui->github_release_files;
auto select_repo = [&]()
{
if(cmd.size() < 2)
return fail(usage);
int row = repos->findData(arg(1));
if(row < 0)
return fail("repository not found");
repos->setCurrentIndex(row);
on_github_repo_currentIndexChanged(row);
return true;
};
auto select_tag = [&]()
{
if(cmd.size() < 3)
return fail(usage);
for(int row = 0;row < tags->rowCount();++row)
if(tags->item(row,0)->text() == arg(2))
{
tags->setCurrentCell(row,0);
on_github_tags_itemSelectionChanged();
return true;
}
return fail("tag not found or still loading");
};
auto select_file = [&]()
{
if(cmd.size() < 4)
return fail(usage);
QString value = arg(3);
int row = -1;
for(int i = 0;i < files->rowCount();++i)
if(files->item(i,0)->text() == value)
{
row = i;
break;
}
if(row < 0)
{
bool ok;
int index = value.toInt(&ok);
if(ok && index >= 0 && index < files->rowCount())
row = index;
}
if(row < 0)
return fail("file not found: use the exact filename or index returned by hub_files");
files->setCurrentCell(row,0);
files->selectRow(row);
on_github_release_files_itemSelectionChanged();
return true;
};
// an empty pattern is itself a valid regex that matches every string,
// so no separate "match all" case is needed
auto make_re = [&](const QString& pattern,const char* what,bool& ok)
{
QRegularExpression re(pattern,QRegularExpression::CaseInsensitiveOption);
if(!(ok = re.isValid()))
error_msg = std::string("invalid ")+what+" pattern: "+pattern.toStdString();
return re;
};
// select and visit every tag whose name matches tag_pattern (regex, empty = all);
// fun returns false to stop iterating early (not an error)
auto for_each_tag = [&](const QString& tag_pattern,auto&& fun)->bool
{
if(!tags->rowCount())
return fail("repository data is loading; retry");
bool ok = true;
auto tag_re = make_re(tag_pattern,"tag",ok);
if(!ok)
return false;
for(int trow = 0;trow < tags->rowCount();++trow)
{
QString tag_name = tags->item(trow,0)->text();
if(!tag_re.match(tag_name).hasMatch())
continue;
tags->setCurrentCell(trow,0);
on_github_tags_itemSelectionChanged();
if(!fun(tag_name))
break;
}
return true;
};
if(cmd[0] == "hub_repo")
{
for(int row = 0;row < repos->count();++row)
tipl::out() << row << "\t" << repos->itemData(row).toString().toStdString();
return true;
}
if(cmd[0] == "hub_tags")
{
if(!select_repo())
return false;
if(!tags->rowCount())
return fail("repository data is loading; retry");
for(int row = 0;row < tags->rowCount();++row)
tipl::out() << row << "\t" << tags->item(row,0)->text().toStdString();
return true;
}
if(cmd[0] == "hub_files")
{
if(!select_repo())
return false;
bool ok = true;
auto text_re = make_re(cmd.size() > 3 ? arg(3) : QString(),"text",ok);
if(!ok)
return false;
int offset = cmd.size() > 4 ? arg(4).toInt(&ok) : 0;
if(!ok || offset < 0)
return fail("invalid offset");
bool has_limit = cmd.size() > 5;
int limit = has_limit ? arg(5).toInt(&ok) : 0;
if(!ok || limit < 0)
return fail("invalid limit");
tipl::out() << "index\ttag\tfile\tsize\tdownloaded";
return for_each_tag(cmd.size() > 2 ? arg(2) : QString(),[&](const QString& tag_name)
{
QString path = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+"/"+cur_tag+"/";
for(int row = 0;row < files->rowCount();++row)
{
QString name = files->item(row,0)->text();
if(!text_re.match(name).hasMatch())
continue;
if(offset)
{
--offset;
continue;
}
if(has_limit && !limit)
return false;
tipl::out() << row << "\t" << tag_name.toStdString() << "\t" << name.toStdString() << "\t"
<< files->item(row,1)->text().toStdString() << "\t"
<< QFile::exists(path+name);
if(has_limit)
--limit;
}
return true;
});
}
if(cmd[0] == "hub_open")
{
if(!select_repo() || !select_tag() || !select_file())
return false;
on_github_open_file_clicked();
return true;
}
if(cmd[0] == "hub_show")
{
if(!select_repo() || !select_tag())
return false;
if(cmd.size() < 4) // no file given: show the release note explaining what this tag's dataset is
{
tipl::out() << (notes[cur_tag].isEmpty() ?
QString("(no release note for this tag)") : notes[cur_tag]).toStdString();
return true;
}
if(!select_file())
return false;
auto row = files->currentRow();
auto file_name = files->item(row,0)->text();
if(!file_name.endsWith(".tsv"))
return fail("not a .tsv file: "+file_name.toStdString());
auto url = files->item(row,4)->text();
tipl::out() << "downloading " << url.toStdString();
auto reply = main_window.get(url);
QEventLoop loop;
QObject::connect(reply.get(),&QNetworkReply::finished,&loop,&QEventLoop::quit);
loop.exec();
if(reply->error() != QNetworkReply::NoError)
return fail(("cannot download "+file_name+": "+reply->errorString()).toStdString());
tipl::out() << QString::fromUtf8(reply->readAll()).toStdString();
return true;
}
if(cmd[0] == "hub_download")
{
if(cmd.size() != 5)
return fail(usage);
if(!select_repo())
return false;
QDir dir(arg(4));
if(!dir.exists())
{
if(!dir.mkpath("."))
return fail("cannot create download directory");
tipl::out() << "directory_created\t"
<< QDir::fromNativeSeparators(dir.absolutePath()).toStdString();
}
ui->download_dir->setText(dir.path());
ui->download_overwrite->setChecked(false);
bool ok = true;
auto file_re = make_re(QRegularExpression::wildcardToRegularExpression(arg(3)),"file",ok);
if(!ok)
return false;
bool any = false;
if(!for_each_tag(arg(2),[&](const QString& tag_name)
{
files->selectionModel()->clearSelection();
for(int row = 0;row < files->rowCount();++row)
if(file_re.match(files->item(row,0)->text()).hasMatch())
files->selectionModel()->select(files->model()->index(row,0),
QItemSelectionModel::Select | QItemSelectionModel::Rows);
on_github_release_files_itemSelectionChanged();
if(!files->selectionModel()->selectedRows().size())
{
tipl::out() << "skip\t" << tag_name.toStdString() << "\tno file matched: " << arg(3).toStdString();
return true;
}
on_github_download_clicked(); // downloads every currently selected row
any = true;
return true;
}))
return false;
return any || fail("no matching tag with a matching file found");
}
return fail(usage);
}
void FiberDataHub::on_github_repo_currentIndexChanged(int index)
{
if(ui->github_repo->currentIndex() < 0 || !fetch_github)
return;
QString repo = ui->github_repo->currentData().toString();
if(tags.find(repo) == tags.end())
{
QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/fiber_data_hub");
QFile f(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) +
"/fiber_data_hub/" + QString(repo).replace('/','_') + ".json");
if(f.open(QFile::ReadOnly))
{
auto root = QJsonDocument::fromJson(f.readAll()).object();
dates[repo] = root["date"].toString();
tags[repo] = root["tags"].toArray();
}
else
{
tags[repo] = QJsonArray();
dates[repo] = QString();
on_load_tags_clicked();
return;
}
}
notes.clear();
assets.clear();
ui->github_tags->setSortingEnabled(false);
ui->github_tags->setRowCount(0);
std::map<QString, std::pair<QString,std::set<std::string> > > agg;
foreach (const QJsonValue& release, tags[repo])
{
auto object = release.toObject();
auto tag = object.value("tag_name").toString();
if(tag.length() > 2 && tag[tag.length()-2] == '_' && tag[tag.length()-1] >= '1' && tag[tag.length()-1] <= '9')
tag.chop(2);
notes[tag] = object.value("body").toString();
auto& agg_at_tag = agg[tag];
agg_at_tag.first = object.value("name").toString();
auto& names = agg_at_tag.second;
foreach (const auto& each, object.value("assets").toArray())
{
assets[tag].append(each);
auto fn = each.toObject().value("name").toString().toStdString();
if (fn.empty() || fn.back()!='z' || tipl::ends_with(fn,{".db.fz",".dz"}))
continue;
names.insert(fn.substr(0, std::min(fn.find('_'), fn.find('.'))));
}
}
if(dates[repo].isEmpty())
ui->tag_date->setText("Loading...");
else
ui->tag_date->setText("Last sync:" + dates[repo]);
for (const auto& each : agg)
{
int row=ui->github_tags->rowCount();
ui->github_tags->insertRow(row);
ui->github_tags->setItem(row,0,new QTableWidgetItem(each.first));
ui->github_tags->setItem(row,1,new QTableWidgetItem(QString::number(each.second.second.size())));
ui->github_tags->setItem(row,2,new QTableWidgetItem(QString::number(assets[each.first].size())));
ui->github_tags->setItem(row,3,new QTableWidgetItem(each.second.first));
}
ui->github_tags->sortByColumn(0,Qt::AscendingOrder);
ui->github_tags->setSortingEnabled(true);
ui->github_tags->resizeRowsToContents();
ui->github_tags->resizeColumnToContents(0);
ui->github_tags->resizeColumnToContents(1);
ui->github_tags->resizeColumnToContents(2);
}
void FiberDataHub::on_load_tags_clicked()
{
if(ui->github_repo->currentIndex() < 0 || !fetch_github)
return;
QString repo = ui->github_repo->currentData().toString();
QString url = QString("https://api.github.com/repos/%1/releases").arg(repo);
ui->github_tags->setSortingEnabled(false);
ui->github_tags->setRowCount(0);
ui->tag_date->setText("Loading...");
ui->load_tags->setEnabled(false);
notes.clear();
assets.clear();
std::vector<int> per_page = {64,32,16,8,4};
QTimer::singleShot(0,this, [=](){loadTags(QUrl(url), repo, QJsonArray(), per_page[std::min<int>(per_page.size()-1,github_api_rate_limit/15)]);});
}
QString showQNetworkReplyError(QNetworkReply* reply)
{
int http_error = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(http_error)
return QMap<int, QString>({
{301, "Moved Permanently - The requested resource has been permanently moved to a new location."},
{302, "Found - The requested resource resides temporarily under a different URI."},
{304, "Not Modified - The server has fulfilled the request, but the document has not been modified."},
{400, "Bad Request - The request was invalid."},
{401, "Unauthorized - Valid authentication credentials are required."},
{404, "Permission Needed - The resource requires access permission."},
{405, "Method Not Allowed - The request method is not supported for the requested resource."},
{408, "Request Timeout - The server timed out waiting for the request."},
{500, "Internal Server Error - The server encountered an unexpected condition."},
{502, "Bad Gateway - The server received an invalid response from an upstream server."},
{503, "Service Unavailable - The server is currently unable to handle the request."},
{504, "Gateway Timeout - The server did not receive a timely response from an upstream server."},
}).value(http_error,"error code: " + QString::number(http_error));
return reply->errorString();
}
void FiberDataHub::update_rate_limit(QSharedPointer<QNetworkReply> reply)
{
if(reply->rawHeader("X-RateLimit-Remaining").toInt() == 0)
return;
tipl::out() << "api rate limit: " << (github_api_rate_limit = reply->rawHeader("X-RateLimit-Remaining").toInt());
}
void FiberDataHub::loadTags(QUrl url,QString repo,QJsonArray array,int per_page)
{
static int retryCount = 0;
{
QUrlQuery q(url.query());
q.removeAllQueryItems("per_page");
q.addQueryItem("per_page", repo.contains("restricted") ? "64" : QString::number(per_page).toStdString().c_str());
url.setQuery(q);
}
tags[repo] = array;
if (!array.isEmpty() && repo == ui->github_repo->currentData().toString())
QTimer::singleShot(0, this, [this]() {on_github_repo_currentIndexChanged(0);});
tipl::out() << "loading " << url.toString().toStdString();
auto reply = main_window.get(url);
connect(reply.get(), &QNetworkReply::finished, this, [=]() mutable {
if (reply->error() != QNetworkReply::NoError)
{
if (reply->error() != QNetworkReply::OperationCanceledError) {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status!=401 && status!=404 && status!=403 && retryCount<5) {
int waitTime = 2 << retryCount; // 2,4,8,16,32s
QTimer::singleShot(waitTime*1000, this, [=]() {
++retryCount;
loadTags(url, repo, array,per_page);
});
} else {
QMessageBox::critical(this, "ERROR", showQNetworkReplyError(reply.get()));
}
}
}
else
{
update_rate_limit(reply);
retryCount = 0;
foreach (const QJsonValue& release , QJsonDocument::fromJson(QString(reply->readAll()).toUtf8()).array())
array.append(release);
// next page?
auto m = QRegularExpression("<([^>]+)>; rel=\"next\"").match(reply->rawHeader("Link"));
if (m.hasMatch())
{
QUrl nextPg = m.captured(1);
if (nextPg.isValid())
{
int delay_time = 0;
if(github_api_rate_limit < 40)
delay_time = 1000;
if(github_api_rate_limit < 20)
delay_time = 5000;
QTimer::singleShot(delay_time, this, [=]() {loadTags(nextPg, repo, array , per_page);});
return;
}
}
tags[repo] = array;
dates[repo] = QDate::currentDate().toString("yyyy/MM/dd");
{
tipl::out() << "saving file list of " << repo.toStdString();
QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/fiber_data_hub");
QJsonObject root;
root["date"] = dates[repo];
root["tags"] = tags[repo];
QFile f(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) +
"/fiber_data_hub/" + QString(repo).replace('/','_') + ".json");
if(f.open(QFile::WriteOnly))
f.write(QJsonDocument(root).toJson(QJsonDocument::Compact));
}
if (!array.isEmpty() && repo == ui->github_repo->currentData().toString())
QTimer::singleShot(0, this, [this]() {on_github_repo_currentIndexChanged(0);});
}
ui->load_tags->setEnabled(true);
reply->deleteLater();
});
}
void FiberDataHub::loadFiles()
{
bool is_restricted = ui->github_repo->currentText().contains("restricted");
ui->github_release_files->setSortingEnabled(false);
ui->github_release_files->setUpdatesEnabled(false);
ui->github_release_files->setRowCount(0);
for(int tab = ui->github_release_note->count()-1;tab > 0;--tab)
ui->github_release_note->removeTab(tab);
github_tsv_link.resize(1);
QStringList units = {" b", " kb", " mb", " gb"};
foreach (const QJsonValue& asset,assets[cur_tag])
{
QJsonObject assetObject = asset.toObject();
size_t size = assetObject.value("size").toInteger();
int i = 0;
while (size >= 1024 && i < units.size() - 1)
{
size /= 1024;
i++;
}
int row = ui->github_release_files->rowCount();
auto file_name = assetObject.value("name").toString();
ui->github_release_files->insertRow(row);
ui->github_release_files->setItem(row, 0, new QTableWidgetItem(file_name));
ui->github_release_files->setItem(row, 1, new QTableWidgetItem(QString::number(size)+units[i]));
ui->github_release_files->setItem(row, 2, new QTableWidgetItem(assetObject.value("created_at").toString()));
ui->github_release_files->setItem(row, 3, new QTableWidgetItem(QString::number(assetObject.value("download_count").toInteger())));
if(is_restricted)
ui->github_release_files->setItem(row, 4, new QTableWidgetItem(assetObject.value("url").toString()));
else
ui->github_release_files->setItem(row, 4, new QTableWidgetItem(assetObject.value("browser_download_url").toString()));
ui->github_release_files->item(row,1)->setData(Qt::UserRole, assetObject.value("size").toInteger()); // Save the original size
if(file_name.contains(".tsv"))
{
ui->github_release_note->addTab(new QWidget(ui->github_release_note),file_name.remove(".tsv"));
github_tsv_link.push_back(assetObject.value("browser_download_url").toString());
}
}
ui->github_release_files->sortByColumn(0,Qt::AscendingOrder);
ui->github_release_files->setUpdatesEnabled(true);
ui->github_release_files->resizeColumnToContents(0);
ui->github_release_files->resizeColumnToContents(1);
ui->github_release_files->resizeColumnToContents(2);
ui->github_release_files->setColumnWidth(3,50);
ui->github_release_files->setSortingEnabled(true);
ui->file_count->setText(QString("%1 files").arg(ui->github_release_files->rowCount()));
}
void FiberDataHub::on_github_release_note_currentChanged(int index)
{
if(index && index < github_tsv_link.size())
{
if(!github_tsv_link[index].isEmpty())
{
tipl::out() << "downloading " << github_tsv_link[index].toStdString().c_str();
auto reply = main_window.get(github_tsv_link[index]);
QEventLoop loop;
QObject::connect(reply.get(), &QNetworkReply::finished, this, [&loop, this, reply, index]()
{
loop.quit();
if (reply->error() == QNetworkReply::NoError &&
index < github_tsv_link.size() &&
!github_tsv_link[index].isEmpty())
{
github_tsv_link[index].clear();
auto tableWidget = new QTableWidget(ui->github_release_note->widget(index));
auto layout = new QVBoxLayout(ui->github_release_note->widget(index));
layout->addWidget(tableWidget);
QString data = reply->readAll();
QStringList rows = data.split("\n");
while(rows.count() && rows.back().isEmpty())
rows.pop_back();
QStringList headers = rows.takeFirst().split("\t");
tableWidget->setRowCount(rows.size());
tableWidget->setColumnCount(headers.size());
tableWidget->setHorizontalHeaderLabels(headers);
for (int i = 0; i < rows.size(); ++i) {
QStringList cols = rows.at(i).split("\t");
for (int j = 0; j < cols.size(); ++j) {
QTableWidgetItem* item = new QTableWidgetItem;
bool ok;
double val = cols.at(j).toDouble(&ok);
if (ok)
item->setData(Qt::DisplayRole, val);
else
item->setText(cols.at(j));
tableWidget->setItem(i, j, item);
}
}
tableWidget->setSortingEnabled(true);
}
});
loop.exec();
}
}
}
void FiberDataHub::on_github_tags_itemSelectionChanged()
{
if(ui->github_tags->currentRow() >= 0 && ui->github_tags->rowCount())
{
cur_tag = ui->github_tags->item(ui->github_tags->currentRow(), 0)->text();
QString title = ui->github_tags->item(ui->github_tags->currentRow(), 3)->text();
ui->github_repo_title->setText(title);
auto content = notes[cur_tag].split('\n');
if(!content.empty() && content[0].contains(title))
content.remove(0);
ui->github_note->setMarkdown(content.join('\n'));
ui->github_release_note->setCurrentIndex(0);
loadFiles();
}
ui->github_tags->setColumnWidth(3,50);
}
void FiberDataHub::on_browseDownloadDir_clicked()
{
QString filename =
QFileDialog::getExistingDirectory(this,"Browse Download Directory",
ui->download_dir->text());
if ( filename.isEmpty() )
return;
ui->download_dir->setText(filename);
}
void FiberDataHub::on_github_release_files_itemSelectionChanged()
{
int selectedRows = ui->github_release_files->selectionModel()->selectedRows().size();
ui->github_release_files->setColumnWidth(3,50);
ui->github_download->setEnabled(selectedRows > 0);
if(selectedRows == 1 && ui->github_release_files->currentRow() >= 0)
{
auto file_name = ui->github_release_files->item(ui->github_release_files->currentRow(),0)->text();
ui->github_open_file->setText(QString("Open %1").arg(file_name));
ui->github_open_file->setVisible(true);
ui->github_open_file_mode->setVisible(true);
ui->github_open_file_mode->clear();
ui->github_open_file_mode->addItem("O1: View Image");
ui->github_open_file_mode->addItem(file_name.endsWith(".src.gz") || file_name.endsWith(".sz") ? "T2: Reconstruction": "T3: Fiber Tracking");
ui->github_open_file_mode->setCurrentIndex(file_name.endsWith(".nii.gz") || file_name.endsWith(".nii") ? 0 : 1 );
if(file_name.endsWith(".db.fz") || file_name.endsWith(".db.fib.gz") || file_name.endsWith(".dz"))
{
ui->github_open_file_mode->addItems({"C2: View Database","C3: Correlational Tracking"});
ui->github_open_file_mode->setCurrentIndex(2);
}
}
else
{
ui->github_open_file->setVisible(false);
ui->github_open_file_mode->setVisible(false);
}
ui->github_download->setText(selectedRows > 0 ? QString("Download %1 File(s)...").arg(selectedRows) : QString("Download"));
ui->file_count->setText(QString("%1/%2 files").arg(selectedRows).arg(ui->github_release_files->rowCount()));
}
void FiberDataHub::on_github_select_all_clicked()
{
ui->github_release_files->selectAll();
}
void FiberDataHub::on_github_download_clicked()
{
QList<QTableWidgetSelectionRange> ranges = ui->github_release_files->selectedRanges();
if (ranges.isEmpty()){
QMessageBox::critical(this, "ERROR", "No files selected for download");
return;
}
std::vector<int> row_list;
for (int i = 0; i < ranges.size();++i)
for (int row = ranges[i].topRow(); row <= ranges[i].bottomRow(); ++row)
row_list.push_back(row);
tipl::progress p("downloading...",true);
for (int i = 0; p(i,row_list.size());++i)
{
QString url = ui->github_release_files->item(row_list[i], 4)->text();
QString filePath = ui->download_dir->text() + "/" + ui->github_release_files->item(row_list[i], 0)->text();
if (QFile::exists(filePath) && !ui->download_overwrite->isChecked())
{
tipl::out() << filePath.toStdString() << " exists...skipping";
continue;
}
tipl::out() << url.toStdString();
QSharedPointer<QNetworkReply> reply;
int retry = 0;
const int max_retry = 5;
while (retry < max_retry)
{
reply = main_window.get(url);
while (!reply->isFinished() && !p.aborted())
{
QCoreApplication::processEvents();
QThread::msleep(100); // Check every 100ms
}
if (reply->error() == QNetworkReply::NoError)
break;
retry++;
QThread::sleep(3);
}
if (retry >= max_retry)
{
QMessageBox::critical(this, "ERROR", showQNetworkReplyError(reply.get()));
return;
}
if (p.aborted())
return;
{
QFile file(filePath);
auto data = reply->readAll();
if(!file.open(QFile::WriteOnly) ||file.write(data) != data.size())
{
QMessageBox::critical(this,"ERROR","Failed to save file to disk");
return;
}
}
}
}
void FiberDataHub::on_github_select_matching_clicked()
{
tipl::progress p("select matching");
QString pattern = QInputDialog::getText(this, "Select Matching", "Enter a sub text (fib.gz), wild card (*.fib.gz) or regex pattern:");
if (pattern.isEmpty())
return;
Qt::MatchFlag flags = Qt::MatchContains;
if(pattern.contains("*"))
flags = Qt::MatchWildcard;
else
if(pattern.contains(QRegularExpression("[.^$|()\\[\\]{}*+?\\\\]")))
{
QRegularExpression regex(pattern);
if (regex.isValid())
flags = Qt::MatchRegularExpression;
else
{
QMessageBox::critical(this,"ERROR","Invalid regular expression pattern");
return;
}
}
QList<QTableWidgetItem*> items = ui->github_release_files->findItems(pattern, flags);
ui->github_release_files->blockSignals(true);
ui->github_release_files->clearSelection();
for (int i = 0; p(i, items.size()); ++i)
ui->github_release_files->setRangeSelected(QTableWidgetSelectionRange(items[i]->row(), 0, items[i]->row(), ui->github_release_files->columnCount() - 1), true);
ui->github_release_files->blockSignals(false);
on_github_release_files_itemSelectionChanged();
}
void FiberDataHub::on_github_open_file_clicked()
{
auto row = ui->github_release_files->currentRow();
if(row < 0)
return;
QDir dir(QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + cur_tag);
if (!dir.exists() && !dir.mkpath("."))
return QMessageBox::critical(this,"ERROR","cannot create a temporary directory to store file"),void();
QString filePath = dir.path()+ "/" + ui->github_release_files->item(row, 0)->text();
auto git_open = [this,filePath](void)
{
if(filePath.endsWith(".nii.gz") || filePath.endsWith(".nii") ||
filePath.endsWith(".fib.gz") || filePath.endsWith(".fz") || filePath.endsWith(".dz"))
{
if(ui->github_open_file_mode->currentIndex() == 0)
main_window.loadNii(QStringList() << filePath);
else
if(ui->github_open_file_mode->currentIndex() == 1)
main_window.loadFib(filePath);
else
if(ui->github_open_file_mode->currentIndex() > 1) // open db
{
auto database = std::make_shared<group_connectometry_analysis>();
tipl::progress prog("reading connectometry db");
if(!database->load_database(filePath.toStdString().c_str()))
{
QMessageBox::critical(this,"ERROR",database->error_msg.c_str());
return;
}
if(ui->github_open_file_mode->currentIndex() == 2)
{
auto db = new db_window(&main_window,database);
db->setWindowTitle(filePath);
db->setAttribute(Qt::WA_DeleteOnClose);
db->show();
}
else
{
auto group_cnt = new group_connectometry(&main_window,database,filePath);
group_cnt->setWindowTitle(filePath);
group_cnt->setAttribute(Qt::WA_DeleteOnClose);
group_cnt->show();
}
}
}
else
{
if(ui->github_open_file_mode->currentIndex() == 0)
main_window.loadNii(QStringList() << filePath);
else
main_window.openFile(QStringList() << filePath);
}
};
qint64 bytesTotal = ui->github_release_files->item(row, 1)->data(Qt::UserRole).toLongLong();
if (QFile::exists(filePath) && !ui->download_overwrite->isChecked())
{
git_open();
return;
}
tipl::out() << "download file to " << filePath.toStdString();
auto reply = main_window.get(ui->github_release_files->item(row, 4)->text());
// Create a progress dialog
QProgressDialog progressDialog("Downloading...", "Cancel", 0, 100, this);
progressDialog.setModal(true);
progressDialog.show();
qint64 bytesReceived = 0;
QEventLoop loop;
QObject::connect(reply.get(), &QNetworkReply::readyRead, this,
[this, &progressDialog, &bytesReceived, bytesTotal,reply]()
{
progressDialog.setValue((reply->bytesAvailable() * 100) / (bytesTotal));
});
QObject::connect(reply.get(), &QNetworkReply::finished, this,
[this, filePath, git_open, &progressDialog, &loop,reply]() // Pass the loop to the lambda
{
if (reply->error() != QNetworkReply::NoError)
{
if(reply->error() != QNetworkReply::OperationCanceledError)
QMessageBox::critical(this, "ERROR", showQNetworkReplyError(reply.get()));
}
else
{
auto downloadFile = std::make_shared<QFile>(filePath);
if (!downloadFile->open(QFile::WriteOnly))
{
QMessageBox::critical(this, "ERROR", "Failed to open file for writing");
return;
}
downloadFile->write(reply->readAll());
downloadFile->close();
QTimer::singleShot(0, this, [git_open](){git_open();});
}
progressDialog.close();
loop.quit();
});