2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "ResultWindow.h"
11 #include <GroupView.h>
12 #include <LayoutBuilder.h>
13 #include <ScrollView.h>
14 #include <StringView.h>
15 #include <package/solver/SolverPackage.h>
16 #include <package/solver/SolverRepository.h>
18 #include <AutoDeleter.h>
19 #include <AutoLocker.h>
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "PackageResult"
26 using namespace BPackageKit
;
29 static const uint32 kApplyMessage
= 'rtry';
32 ResultWindow::ResultWindow()
34 BWindow(BRect(0, 0, 400, 300), B_TRANSLATE_COMMENT("Package changes",
35 "Window title"), B_TITLED_WINDOW_LOOK
,
37 B_ASYNCHRONOUS_CONTROLS
| B_NOT_MINIMIZABLE
| B_AUTO_UPDATE_SIZE_LIMITS
,
40 fClientWaiting(false),
47 fDoneSemaphore
= create_sem(0, "package changes");
48 if (fDoneSemaphore
< 0)
49 throw std::bad_alloc();
51 BStringView
* topTextView
= NULL
;
52 BViewPort
* viewPort
= NULL
;
54 BLayoutBuilder::Group
<>(this, B_VERTICAL
, B_USE_DEFAULT_SPACING
)
55 .SetInsets(B_USE_SMALL_INSETS
)
56 .Add(topTextView
= new BStringView(NULL
, B_TRANSLATE(
57 "The following additional package changes have to be made:")))
58 .Add(new BScrollView(NULL
, viewPort
= new BViewPort(), 0, false, true))
59 .AddGroup(B_HORIZONTAL
)
60 .Add(fCancelButton
= new BButton(B_TRANSLATE("Cancel"),
61 new BMessage(B_CANCEL
)))
63 .Add(fApplyButton
= new BButton(B_TRANSLATE("Apply changes"),
64 new BMessage(kApplyMessage
)))
67 topTextView
->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
69 viewPort
->SetChildView(fContainerView
= new BGroupView(B_VERTICAL
, 0));
71 // set small scroll step (large step will be set by the view port)
72 font_height fontHeight
;
73 topTextView
->GetFontHeight(&fontHeight
);
74 float smallStep
= ceilf(fontHeight
.ascent
+ fontHeight
.descent
);
75 viewPort
->ScrollBar(B_VERTICAL
)->SetSteps(smallStep
, smallStep
);
79 ResultWindow::~ResultWindow()
81 if (fDoneSemaphore
>= 0)
82 delete_sem(fDoneSemaphore
);
87 ResultWindow::AddLocationChanges(const char* location
,
88 const PackageList
& packagesToInstall
,
89 const PackageSet
& packagesAlreadyAdded
,
90 const PackageList
& packagesToUninstall
,
91 const PackageSet
& packagesAlreadyRemoved
)
93 BGroupView
* locationGroup
= new BGroupView(B_VERTICAL
);
94 ObjectDeleter
<BGroupView
> locationGroupDeleter(locationGroup
);
96 locationGroup
->GroupLayout()->SetInsets(B_USE_SMALL_INSETS
);
98 float backgroundTint
= B_NO_TINT
;
99 if ((fContainerView
->CountChildren() & 1) != 0)
100 backgroundTint
= 1.04;
101 locationGroup
->SetViewUIColor(B_LIST_BACKGROUND_COLOR
, backgroundTint
);
103 BStringView
* locationView
= new BStringView(NULL
,
104 BString().SetToFormat("in %s:", location
));
105 locationGroup
->AddChild(locationView
);
106 locationView
->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
107 locationView
->AdoptParentColors();
109 locationView
->GetFont(&locationFont
);
110 locationFont
.SetFace(B_BOLD_FACE
);
111 locationView
->SetFont(&locationFont
);
113 BGroupLayout
* packagesGroup
= new BGroupLayout(B_VERTICAL
);
114 locationGroup
->GroupLayout()->AddItem(packagesGroup
);
115 packagesGroup
->SetInsets(20, 0, 0, 0);
117 bool packagesAdded
= _AddPackages(packagesGroup
, packagesToInstall
,
118 packagesAlreadyAdded
, true);
119 packagesAdded
|= _AddPackages(packagesGroup
, packagesToUninstall
,
120 packagesAlreadyRemoved
, false);
125 fContainerView
->AddChild(locationGroup
);
126 locationGroupDeleter
.Detach();
135 AutoLocker
<ResultWindow
> locker(this);
141 fClientWaiting
= true;
145 while (acquire_sem(fDoneSemaphore
) == B_INTERRUPTED
) {
150 if (locker
.IsLocked()) {
155 PostMessage(B_QUIT_REQUESTED
);
162 ResultWindow::QuitRequested()
164 if (fClientWaiting
) {
166 fClientWaiting
= false;
167 release_sem(fDoneSemaphore
);
176 ResultWindow::MessageReceived(BMessage
* message
)
178 switch (message
->what
) {
182 fAccepted
= message
->what
== kApplyMessage
;
183 fClientWaiting
= false;
184 release_sem(fDoneSemaphore
);
187 BWindow::MessageReceived(message
);
194 ResultWindow::_AddPackages(BGroupLayout
* packagesGroup
,
195 const PackageList
& packages
, const PackageSet
& ignorePackages
, bool install
)
197 bool packagesAdded
= false;
199 for (int32 i
= 0; BSolverPackage
* package
= packages
.ItemAt(i
);
201 if (ignorePackages
.find(package
) != ignorePackages
.end())
206 text
.SetToFormat(B_TRANSLATE_COMMENT("install package %s from "
207 "repository %s\n", "Don't change '%s' variables"),
208 package
->Info().FileName().String(),
209 package
->Repository()->Name().String());
211 text
.SetToFormat(B_TRANSLATE_COMMENT("uninstall package %s\n",
212 "Don't change '%s' variable"),
213 package
->VersionedName().String());
216 BStringView
* packageView
= new BStringView(NULL
, text
);
217 packagesGroup
->AddView(packageView
);
218 packageView
->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
219 packageView
->AdoptParentColors();
221 packagesAdded
= true;
224 return packagesAdded
;