-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
RepositoriesView.cpp
777 lines (697 loc) · 21.5 KB
/
RepositoriesView.cpp
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
/*
* Copyright 2017 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Brian Hill
*/
#include "RepositoriesView.h"
#include <stdlib.h>
#include <Alert.h>
#include <Button.h>
#include <Catalog.h>
#include <ColumnTypes.h>
#include <LayoutBuilder.h>
#include <MessageRunner.h>
#include <ScrollBar.h>
#include <SeparatorView.h>
#include <Url.h>
#include <package/PackageRoster.h>
#include <package/RepositoryConfig.h>
#include "constants.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "RepositoriesView"
static const BString kTitleEnabled =
B_TRANSLATE_COMMENT("Status", "Column title");
static const BString kTitleName = B_TRANSLATE_COMMENT("Name", "Column title");
static const BString kTitleUrl = B_TRANSLATE_COMMENT("URL", "Column title");
static const BString kLabelRemove =
B_TRANSLATE_COMMENT("Remove", "Button label");
static const BString kLabelRemoveAll =
B_TRANSLATE_COMMENT("Remove All", "Button label");
static const BString kLabelEnable =
B_TRANSLATE_COMMENT("Enable", "Button label");
static const BString kLabelEnableAll =
B_TRANSLATE_COMMENT("Enable All", "Button label");
static const BString kLabelDisable =
B_TRANSLATE_COMMENT("Disable", "Button label");
static const BString kLabelDisableAll =
B_TRANSLATE_COMMENT("Disable All", "Button label");
static const BString kStatusViewText =
B_TRANSLATE_COMMENT("Changes pending:", "Status view text");
static const BString kStatusCompletedText =
B_TRANSLATE_COMMENT("Changes completed", "Status view text");
RepositoriesListView::RepositoriesListView(const char* name)
:
BColumnListView(name, B_NAVIGABLE, B_PLAIN_BORDER)
{
}
void
RepositoriesListView::KeyDown(const char* bytes, int32 numBytes)
{
switch (bytes[0]) {
case B_DELETE:
Window()->PostMessage(DELETE_KEY_PRESSED);
break;
default:
BColumnListView::KeyDown(bytes, numBytes);
}
}
RepositoriesView::RepositoriesView()
:
BGroupView("RepositoriesView"),
fTaskLooper(NULL),
fShowCompletedStatus(false),
fRunningTaskCount(0),
fLastCompletedTimerId(0)
{
// Column list view with 3 columns
fListView = new RepositoriesListView("list");
fListView->SetSelectionMessage(new BMessage(LIST_SELECTION_CHANGED));
float col0width = be_plain_font->StringWidth(kTitleEnabled) + 15;
float col1width = be_plain_font->StringWidth(kTitleName) + 15;
float col2width = be_plain_font->StringWidth(kTitleUrl) + 15;
fListView->AddColumn(new BStringColumn(kTitleEnabled, col0width, col0width,
2 * col0width, B_TRUNCATE_END), kEnabledColumn);
fListView->AddColumn(new BStringColumn(kTitleName, 90, col1width, 300,
B_TRUNCATE_END), kNameColumn);
fListView->AddColumn(new BStringColumn(kTitleUrl, 500, col2width, 5000,
B_TRUNCATE_END), kUrlColumn);
fListView->SetInvocationMessage(new BMessage(ITEM_INVOKED));
// Repository list status view
fStatusContainerView = new BView("status", B_SUPPORTS_LAYOUT);
BString templateText(kStatusViewText);
templateText.Append(" 88");
// Simulate a status text with two digit queue count
fListStatusView = new BStringView("status", templateText);
// Set a smaller fixed font size and slightly lighten text color
BFont font(be_plain_font);
font.SetSize(10.0f);
fListStatusView->SetFont(&font, B_FONT_SIZE);
fListStatusView->SetHighUIColor(fListStatusView->HighUIColor(), .9f);
// Set appropriate explicit view sizes
float viewWidth = std::max(fListStatusView->StringWidth(templateText),
fListStatusView->StringWidth(kStatusCompletedText));
BSize statusViewSize(viewWidth + 3, B_H_SCROLL_BAR_HEIGHT - 2);
fListStatusView->SetExplicitSize(statusViewSize);
statusViewSize.height += 1;
fStatusContainerView->SetExplicitSize(statusViewSize);
BLayoutBuilder::Group<>(fStatusContainerView, B_HORIZONTAL, 0)
.Add(new BSeparatorView(B_VERTICAL))
.AddGroup(B_VERTICAL, 0)
.AddGlue()
.AddGroup(B_HORIZONTAL, 0)
.SetInsets(2, 0, 0, 0)
.Add(fListStatusView)
.AddGlue()
.End()
.Add(new BSeparatorView(B_HORIZONTAL))
.End()
.End();
fListView->AddStatusView(fStatusContainerView);
// Standard buttons
fEnableButton = new BButton(kLabelEnable,
new BMessage(ENABLE_BUTTON_PRESSED));
fDisableButton = new BButton(kLabelDisable,
new BMessage(DISABLE_BUTTON_PRESSED));
// Create buttons with fixed size
font_height fontHeight;
GetFontHeight(&fontHeight);
int16 buttonHeight = int16(fontHeight.ascent + fontHeight.descent + 12);
// button size determined by font size
BSize btnSize(buttonHeight, buttonHeight);
fAddButton = new BButton("plus", "+", new BMessage(ADD_REPO_WINDOW));
fAddButton->SetExplicitSize(btnSize);
fRemoveButton = new BButton("minus", "-", new BMessage(REMOVE_REPOS));
fRemoveButton->SetExplicitSize(btnSize);
// Layout
int16 buttonSpacing = 1;
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(B_USE_WINDOW_SPACING)
.AddGroup(B_HORIZONTAL, 0, 0.0)
.Add(new BStringView("instruction", B_TRANSLATE_COMMENT("Enable"
" repositories to use with package management:",
"Label text")), 0.0)
.AddGlue()
.End()
.AddStrut(B_USE_DEFAULT_SPACING)
.Add(fListView, 1)
.AddGroup(B_HORIZONTAL, 0, 0.0)
// Add and Remove buttons
.AddGroup(B_VERTICAL, 0, 0.0)
.AddGroup(B_HORIZONTAL, 0, 0.0)
.Add(new BSeparatorView(B_VERTICAL))
.AddGroup(B_VERTICAL, 0, 0.0)
.AddGroup(B_HORIZONTAL, buttonSpacing, 0.0)
.SetInsets(buttonSpacing)
.Add(fAddButton)
.Add(fRemoveButton)
.End()
.Add(new BSeparatorView(B_HORIZONTAL))
.End()
.Add(new BSeparatorView(B_VERTICAL))
.End()
.AddGlue()
.End()
// Enable and Disable buttons
.AddGroup(B_HORIZONTAL)
.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING, 0)
.AddGlue()
.Add(fEnableButton)
.Add(fDisableButton)
.End()
.End()
.End();
}
RepositoriesView::~RepositoriesView()
{
if (fTaskLooper) {
fTaskLooper->Lock();
fTaskLooper->Quit();
}
_EmptyList();
}
void
RepositoriesView::AllAttached()
{
BView::AllAttached();
fRemoveButton->SetTarget(this);
fEnableButton->SetTarget(this);
fDisableButton->SetTarget(this);
fListView->SetTarget(this);
fRemoveButton->SetEnabled(false);
fEnableButton->SetEnabled(false);
fDisableButton->SetEnabled(false);
_UpdateStatusView();
_InitList();
}
void
RepositoriesView::AttachedToWindow()
{
fTaskLooper = new TaskLooper(BMessenger(this));
}
void
RepositoriesView::MessageReceived(BMessage* message)
{
switch (message->what)
{
case REMOVE_REPOS:
{
RepoRow* rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection());
if (!rowItem || !fRemoveButton->IsEnabled())
break;
BString text;
// More than one selected row
if (fListView->CurrentSelection(rowItem)) {
text.SetTo(B_TRANSLATE_COMMENT("Remove these repositories?",
"Removal alert confirmation message"));
text.Append("\n");
}
// Only one selected row
else {
text.SetTo(B_TRANSLATE_COMMENT("Remove this repository?",
"Removal alert confirmation message"));
text.Append("\n");
}
float minWidth = 0;
while (rowItem) {
BString repoText;
repoText.Append("\n").Append(rowItem->Name())
.Append(" (").Append(rowItem->Url()).Append(")");
minWidth = std::max(minWidth, StringWidth(repoText.String()));
text.Append(repoText);
rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection(rowItem));
}
minWidth = std::min(minWidth, Frame().Width());
// Ensure alert window isn't much larger than the main window
BAlert* alert = new BAlert("confirm", text, kRemoveLabel,
kCancelLabel, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
alert->TextView()->SetExplicitMinSize(BSize(minWidth, B_SIZE_UNSET));
alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
int32 answer = alert->Go();
// User presses Cancel button
if (answer)
break;
rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection());
while (rowItem) {
RepoRow* oldRow = rowItem;
rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection(rowItem));
fListView->RemoveRow(oldRow);
delete oldRow;
}
_SaveList();
break;
}
case LIST_SELECTION_CHANGED:
_UpdateButtons();
break;
case ITEM_INVOKED:
{
// Simulates pressing whichever is the enabled button
if (fEnableButton->IsEnabled()) {
BMessage invokeMessage(ENABLE_BUTTON_PRESSED);
MessageReceived(&invokeMessage);
} else if (fDisableButton->IsEnabled()) {
BMessage invokeMessage(DISABLE_BUTTON_PRESSED);
MessageReceived(&invokeMessage);
}
break;
}
case ENABLE_BUTTON_PRESSED:
{
BStringList names;
bool paramsOK = true;
// Check if there are multiple selections of the same repository,
// pkgman won't like that
RepoRow* rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection());
while (rowItem) {
if (names.HasString(rowItem->Name())
&& kNewRepoDefaultName.Compare(rowItem->Name()) != 0) {
(new BAlert("duplicate",
B_TRANSLATE_COMMENT("Only one URL for each repository can "
"be enabled. Please change your selections.",
"Error message"),
kOKLabel, NULL, NULL,
B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go(NULL);
paramsOK = false;
break;
} else
names.Add(rowItem->Name());
rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection(rowItem));
}
if (paramsOK) {
_AddSelectedRowsToQueue();
_UpdateButtons();
}
break;
}
case DISABLE_BUTTON_PRESSED:
_AddSelectedRowsToQueue();
_UpdateButtons();
break;
case TASK_STARTED:
{
int16 count;
status_t result1 = message->FindInt16(key_count, &count);
RepoRow* rowItem;
status_t result2 = message->FindPointer(key_rowptr, (void**)&rowItem);
if (result1 == B_OK && result2 == B_OK)
_TaskStarted(rowItem, count);
break;
}
case TASK_COMPLETED_WITH_ERRORS:
{
BString errorDetails;
status_t result = message->FindString(key_details, &errorDetails);
if (result == B_OK) {
(new BAlert("error", errorDetails, kOKLabel, NULL, NULL,
B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go(NULL);
}
BString repoName = message->GetString(key_name,
kNewRepoDefaultName.String());
int16 count;
status_t result1 = message->FindInt16(key_count, &count);
RepoRow* rowItem;
status_t result2 = message->FindPointer(key_rowptr, (void**)&rowItem);
if (result1 == B_OK && result2 == B_OK) {
_TaskCompleted(rowItem, count, repoName);
// Refresh the enabled status of each row since it is unsure what
// caused the error
_RefreshList();
}
_UpdateButtons();
break;
}
case TASK_COMPLETED:
{
BString repoName = message->GetString(key_name,
kNewRepoDefaultName.String());
int16 count;
status_t result1 = message->FindInt16(key_count, &count);
RepoRow* rowItem;
status_t result2 = message->FindPointer(key_rowptr, (void**)&rowItem);
if (result1 == B_OK && result2 == B_OK) {
_TaskCompleted(rowItem, count, repoName);
// If the completed row has siblings then enabling this row may
// have disabled one of the other siblings, do full refresh.
if (rowItem->HasSiblings() && rowItem->IsEnabled())
_RefreshList();
}
_UpdateButtons();
break;
}
case TASK_CANCELED:
{
int16 count;
status_t result1 = message->FindInt16(key_count, &count);
RepoRow* rowItem;
status_t result2 = message->FindPointer(key_rowptr, (void**)&rowItem);
if (result1 == B_OK && result2 == B_OK)
_TaskCanceled(rowItem, count);
// Refresh the enabled status of each row since it is unsure what
// caused the cancelation
_RefreshList();
_UpdateButtons();
break;
}
case UPDATE_LIST:
_RefreshList();
_UpdateButtons();
break;
case STATUS_VIEW_COMPLETED_TIMEOUT:
{
int32 timerID;
status_t result = message->FindInt32(key_ID, &timerID);
if (result == B_OK && timerID == fLastCompletedTimerId)
_UpdateStatusView();
break;
}
default:
BView::MessageReceived(message);
}
}
void
RepositoriesView::_AddSelectedRowsToQueue()
{
RepoRow* rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection());
while (rowItem) {
rowItem->SetTaskState(STATE_IN_QUEUE_WAITING);
BMessage taskMessage(DO_TASK);
taskMessage.AddPointer(key_rowptr, rowItem);
fTaskLooper->PostMessage(&taskMessage);
rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection(rowItem));
}
}
void
RepositoriesView::_TaskStarted(RepoRow* rowItem, int16 count)
{
fRunningTaskCount = count;
rowItem->SetTaskState(STATE_IN_QUEUE_RUNNING);
// Only present a status count if there is more than one task in queue
if (count > 1) {
_UpdateStatusView();
fShowCompletedStatus = true;
}
}
void
RepositoriesView::_TaskCompleted(RepoRow* rowItem, int16 count, BString& newName)
{
fRunningTaskCount = count;
_ShowCompletedStatusIfDone();
// Update row state and values
rowItem->SetTaskState(STATE_NOT_IN_QUEUE);
if (kNewRepoDefaultName.Compare(rowItem->Name()) == 0
&& newName.Compare("") != 0) {
rowItem->SetName(newName.String());
}
_UpdateFromRepoConfig(rowItem);
}
void
RepositoriesView::_TaskCanceled(RepoRow* rowItem, int16 count)
{
fRunningTaskCount = count;
_ShowCompletedStatusIfDone();
// Update row state and values
rowItem->SetTaskState(STATE_NOT_IN_QUEUE);
_UpdateFromRepoConfig(rowItem);
}
void
RepositoriesView::_ShowCompletedStatusIfDone()
{
// If this is the last task show completed status text for 3 seconds
if (fRunningTaskCount == 0 && fShowCompletedStatus) {
fListStatusView->SetText(kStatusCompletedText);
fLastCompletedTimerId = rand();
BMessage timerMessage(STATUS_VIEW_COMPLETED_TIMEOUT);
timerMessage.AddInt32(key_ID, fLastCompletedTimerId);
new BMessageRunner(this, &timerMessage, 3000000, 1);
fShowCompletedStatus = false;
} else
_UpdateStatusView();
}
void
RepositoriesView::_UpdateFromRepoConfig(RepoRow* rowItem)
{
BPackageKit::BPackageRoster pRoster;
BPackageKit::BRepositoryConfig repoConfig;
BString repoName(rowItem->Name());
status_t result = pRoster.GetRepositoryConfig(repoName, &repoConfig);
// Repo name was found and the URL matches
if (result == B_OK && repoConfig.BaseURL() == rowItem->Url())
rowItem->SetEnabled(true);
else
rowItem->SetEnabled(false);
}
void
RepositoriesView::AddManualRepository(BString url)
{
BUrl newRepoUrl(url);
if (!newRepoUrl.IsValid())
return;
BString name(kNewRepoDefaultName);
BString newPathIdentifier = _GetPathIdentifier(newRepoUrl.Path());
bool foundMatchingRoot = false;
int32 index;
int32 listCount = fListView->CountRows();
for (index = 0; index < listCount; index++) {
RepoRow* repoItem = dynamic_cast<RepoRow*>(fListView->RowAt(index));
BUrl rowRepoUrl(repoItem->Url());
// Find an already existing URL
if (newRepoUrl == rowRepoUrl) {
(new BAlert("duplicate",
B_TRANSLATE_COMMENT("This repository URL already exists.",
"Error message"),
kOKLabel))->Go(NULL);
return;
}
// Predict the repo name from another url with matching path root
if (!foundMatchingRoot) {
BString rowPathIdentifier = _GetPathIdentifier(rowRepoUrl.Path());
if (newPathIdentifier.ICompare(rowPathIdentifier) == 0) {
foundMatchingRoot = true;
name = repoItem->Name();
}
}
}
RepoRow* newRepo = _AddRepo(name, url, false);
_FindSiblings();
fListView->DeselectAll();
fListView->AddToSelection(newRepo);
_UpdateButtons();
_SaveList();
}
BString
RepositoriesView::_GetPathIdentifier(BString urlPath)
{
// Find second /
int32 index = urlPath.FindFirst("/");
if (index == B_ERROR)
return urlPath;
index = urlPath.FindFirst("/", index + 1);
if (index == B_ERROR)
return urlPath;
else
return urlPath.Truncate(index);
}
status_t
RepositoriesView::_EmptyList()
{
BRow* row = fListView->RowAt((int32)0, NULL);
while (row != NULL) {
fListView->RemoveRow(row);
delete row;
row = fListView->RowAt((int32)0, NULL);
}
return B_OK;
}
void
RepositoriesView::_InitList()
{
// Get list of known repositories from the settings file
int32 index, repoCount;
BStringList nameList, urlList;
status_t result = fSettings.GetRepositories(repoCount, nameList, urlList);
if (result == B_OK) {
BString name, url;
for (index = 0; index < repoCount; index++) {
name = nameList.StringAt(index);
url = urlList.StringAt(index);
_AddRepo(name, url, false);
}
}
_UpdateListFromRoster();
fListView->SetSortColumn(fListView->ColumnAt(kUrlColumn), false, true);
fListView->ResizeAllColumnsToPreferred();
}
void
RepositoriesView::_RefreshList()
{
// Clear enabled status on all rows
int32 index, listCount = fListView->CountRows();
for (index = 0; index < listCount; index++) {
RepoRow* repoItem = dynamic_cast<RepoRow*>(fListView->RowAt(index));
if (repoItem->TaskState() == STATE_NOT_IN_QUEUE)
repoItem->SetEnabled(false);
}
// Get current list of enabled repositories
_UpdateListFromRoster();
}
void
RepositoriesView::_UpdateListFromRoster()
{
// Get list of currently enabled repositories
BStringList repositoryNames;
BPackageKit::BPackageRoster pRoster;
status_t result = pRoster.GetRepositoryNames(repositoryNames);
if (result != B_OK) {
(new BAlert("error",
B_TRANSLATE_COMMENT("Repositories could not retrieve the names of "
"the currently enabled repositories.", "Alert error message"),
kOKLabel, NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go(NULL);
return;
}
BPackageKit::BRepositoryConfig repoConfig;
int16 index, count = repositoryNames.CountStrings();
for (index = 0; index < count; index++) {
const BString& repoName = repositoryNames.StringAt(index);
result = pRoster.GetRepositoryConfig(repoName, &repoConfig);
if (result == B_OK)
_AddRepo(repoName, repoConfig.BaseURL(), true);
else {
BString text(B_TRANSLATE_COMMENT("Error getting repository"
" configuration for %name%.", "Alert error message, "
"do not translate %name%"));
text.ReplaceFirst("%name%", repoName);
(new BAlert("error", text, kOKLabel))->Go(NULL);
}
}
_FindSiblings();
_SaveList();
}
void
RepositoriesView::_SaveList()
{
BStringList nameList, urlList;
int32 index;
int32 listCount = fListView->CountRows();
for (index = 0; index < listCount; index++) {
RepoRow* repoItem = dynamic_cast<RepoRow*>(fListView->RowAt(index));
nameList.Add(repoItem->Name());
urlList.Add(repoItem->Url());
}
fSettings.SetRepositories(nameList, urlList);
}
RepoRow*
RepositoriesView::_AddRepo(BString name, BString url, bool enabled)
{
// URL must be valid
BUrl repoUrl(url);
if (!repoUrl.IsValid())
return NULL;
int32 index;
int32 listCount = fListView->CountRows();
// Find if the repo already exists in list
for (index = 0; index < listCount; index++) {
RepoRow* repoItem = dynamic_cast<RepoRow*>(fListView->RowAt(index));
BUrl itemUrl(repoItem->Url());
if (repoUrl == itemUrl) {
// update name and enabled values
if (name != repoItem->Name())
repoItem->SetName(name.String());
repoItem->SetEnabled(enabled);
return repoItem;
}
}
RepoRow* addedRow = new RepoRow(name, url, enabled);
fListView->AddRow(addedRow);
return addedRow;
}
void
RepositoriesView::_FindSiblings()
{
BStringList namesFound, namesWithSiblings;
int32 index, listCount = fListView->CountRows();
// Find repository names that are duplicated
for (index = 0; index < listCount; index++) {
RepoRow* repoItem = dynamic_cast<RepoRow*>(fListView->RowAt(index));
BString name = repoItem->Name();
// Ignore newly added repos since we don't know the real name yet
if (name.Compare(kNewRepoDefaultName)==0)
continue;
// First time a name is found- no sibling (yet)
if (!namesFound.HasString(name))
namesFound.Add(name);
// Name was already found once so this name has 2 or more siblings
else if (!namesWithSiblings.HasString(name))
namesWithSiblings.Add(name);
}
// Set sibling values for each row
for (index = 0; index < listCount; index++) {
RepoRow* repoItem = dynamic_cast<RepoRow*>(fListView->RowAt(index));
BString name = repoItem->Name();
repoItem->SetHasSiblings(namesWithSiblings.HasString(name));
}
}
void
RepositoriesView::_UpdateButtons()
{
RepoRow* rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection());
// At least one row is selected
if (rowItem) {
bool someAreEnabled = false;
bool someAreDisabled = false;
bool someAreInQueue = false;
int32 selectedCount = 0;
RepoRow* rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection());
while (rowItem) {
selectedCount++;
uint32 taskState = rowItem->TaskState();
if ( taskState == STATE_IN_QUEUE_WAITING
|| taskState == STATE_IN_QUEUE_RUNNING) {
someAreInQueue = true;
}
if (rowItem->IsEnabled())
someAreEnabled = true;
else
someAreDisabled = true;
rowItem = dynamic_cast<RepoRow*>(fListView->CurrentSelection(rowItem));
}
// Change button labels depending on which rows are selected
if (selectedCount > 1) {
fEnableButton->SetLabel(kLabelEnableAll);
fDisableButton->SetLabel(kLabelDisableAll);
} else {
fEnableButton->SetLabel(kLabelEnable);
fDisableButton->SetLabel(kLabelDisable);
}
// Set which buttons should be enabled
fRemoveButton->SetEnabled(!someAreEnabled && !someAreInQueue);
if ((someAreEnabled && someAreDisabled) || someAreInQueue) {
// there are a mix of enabled and disabled repositories selected
fEnableButton->SetEnabled(false);
fDisableButton->SetEnabled(false);
} else {
fEnableButton->SetEnabled(someAreDisabled);
fDisableButton->SetEnabled(someAreEnabled);
}
} else {
// No selected rows
fEnableButton->SetLabel(kLabelEnable);
fDisableButton->SetLabel(kLabelDisable);
fEnableButton->SetEnabled(false);
fDisableButton->SetEnabled(false);
fRemoveButton->SetEnabled(false);
}
}
void
RepositoriesView::_UpdateStatusView()
{
if (fRunningTaskCount) {
BString text(kStatusViewText);
text.Append(" ");
text << fRunningTaskCount;
fListStatusView->SetText(text);
} else
fListStatusView->SetText("");
}