xref: /haiku/src/servers/package/ResultWindow.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ResultWindow.h"
8 
9 #include <Button.h>
10 #include <GroupView.h>
11 #include <LayoutBuilder.h>
12 #include <ScrollView.h>
13 #include <StringView.h>
14 #include <package/solver/SolverPackage.h>
15 #include <package/solver/SolverRepository.h>
16 
17 #include <AutoDeleter.h>
18 #include <AutoLocker.h>
19 #include <ViewPort.h>
20 
21 
22 using namespace BPackageKit;
23 
24 
25 static const uint32 kApplyMessage = 'rtry';
26 
27 
28 ResultWindow::ResultWindow()
29 	:
30 	BWindow(BRect(0, 0, 400, 300), "Package changes", B_TITLED_WINDOW_LOOK,
31 		B_NORMAL_WINDOW_FEEL,
32 		B_ASYNCHRONOUS_CONTROLS | B_NOT_MINIMIZABLE | B_AUTO_UPDATE_SIZE_LIMITS,
33 		B_ALL_WORKSPACES),
34 	fDoneSemaphore(-1),
35 	fClientWaiting(false),
36 	fAccepted(false),
37 	fContainerView(NULL),
38 	fCancelButton(NULL),
39 	fApplyButton(NULL)
40 
41 {
42 	fDoneSemaphore = create_sem(0, "package changes");
43 	if (fDoneSemaphore < 0)
44 		throw std::bad_alloc();
45 
46 	BStringView* topTextView = NULL;
47 	BViewPort* viewPort = NULL;
48 
49 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
50 		.SetInsets(B_USE_SMALL_INSETS)
51 		.Add(topTextView = new BStringView(NULL,
52 			"The following additional package changes have to be made:"))
53 		.Add(new BScrollView(NULL, viewPort = new BViewPort(), 0, false, true))
54 		.AddGroup(B_HORIZONTAL)
55 			.Add(fCancelButton = new BButton("Cancel", new BMessage(B_CANCEL)))
56 			.AddGlue()
57 			.Add(fApplyButton = new BButton("Apply changes",
58 				new BMessage(kApplyMessage)))
59 		.End();
60 
61 	topTextView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
62 
63 	viewPort->SetChildView(fContainerView = new BGroupView(B_VERTICAL, 0));
64 
65 	// set small scroll step (large step will be set by the view port)
66 	font_height fontHeight;
67 	topTextView->GetFontHeight(&fontHeight);
68 	float smallStep = ceilf(fontHeight.ascent + fontHeight.descent);
69 	viewPort->ScrollBar(B_VERTICAL)->SetSteps(smallStep, smallStep);
70 }
71 
72 
73 ResultWindow::~ResultWindow()
74 {
75 	if (fDoneSemaphore >= 0)
76 		delete_sem(fDoneSemaphore);
77 }
78 
79 
80 bool
81 ResultWindow::AddLocationChanges(const char* location,
82 	const PackageList& packagesToInstall,
83 	const PackageSet& packagesAlreadyAdded,
84 	const PackageList& packagesToUninstall,
85 	const PackageSet& packagesAlreadyRemoved)
86 {
87 	BGroupView* locationGroup = new BGroupView(B_VERTICAL);
88 	ObjectDeleter<BGroupView> locationGroupDeleter(locationGroup);
89 
90 	locationGroup->GroupLayout()->SetInsets(B_USE_SMALL_INSETS);
91 
92 	rgb_color background = ui_color(B_LIST_BACKGROUND_COLOR);
93 	if ((fContainerView->CountChildren() & 1) != 0)
94 		background = tint_color(background, 1.04);
95 	locationGroup->SetViewColor(background);
96 
97 	BStringView* locationView = new BStringView(NULL,
98 		BString().SetToFormat("in %s:", location));
99 	locationGroup->AddChild(locationView);
100 	locationView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
101 	BFont locationFont;
102 	locationView->GetFont(&locationFont);
103 	locationFont.SetFace(B_BOLD_FACE);
104 	locationView->SetFont(&locationFont);
105 
106 	BGroupLayout* packagesGroup = new BGroupLayout(B_VERTICAL);
107 	locationGroup->GroupLayout()->AddItem(packagesGroup);
108 	packagesGroup->SetInsets(20, 0, 0, 0);
109 
110 	bool packagesAdded = _AddPackages(packagesGroup, packagesToInstall,
111 		packagesAlreadyAdded, true);
112 	packagesAdded |= _AddPackages(packagesGroup, packagesToUninstall,
113 		packagesAlreadyRemoved, false);
114 
115 	if (!packagesAdded)
116 		return false;
117 
118 	fContainerView->AddChild(locationGroup);
119 	locationGroupDeleter.Detach();
120 
121 	return true;
122 }
123 
124 
125 bool
126 ResultWindow::Go()
127 {
128 	AutoLocker<ResultWindow> locker(this);
129 
130 	CenterOnScreen();
131 	Show();
132 
133 	fAccepted = false;
134 	fClientWaiting = true;
135 
136 	locker.Unlock();
137 
138 	while (acquire_sem(fDoneSemaphore) == B_INTERRUPTED) {
139 	}
140 
141 	locker.Lock();
142 	bool result = false;
143 	if (locker.IsLocked()) {
144 		result = fAccepted;
145 		Quit();
146 		locker.Detach();
147 	} else
148 		PostMessage(B_QUIT_REQUESTED);
149 
150 	return result;
151 }
152 
153 
154 bool
155 ResultWindow::QuitRequested()
156 {
157 	if (fClientWaiting) {
158 		Hide();
159 		fClientWaiting = false;
160 		release_sem(fDoneSemaphore);
161 		return false;
162 	}
163 
164 	return true;
165 }
166 
167 
168 void
169 ResultWindow::MessageReceived(BMessage* message)
170 {
171 	switch (message->what) {
172 		case B_CANCEL:
173 		case kApplyMessage:
174 			Hide();
175 			fAccepted = message->what == kApplyMessage;
176 			fClientWaiting = false;
177 			release_sem(fDoneSemaphore);
178 			break;
179 		default:
180 			BWindow::MessageReceived(message);
181 			break;
182 	}
183 }
184 
185 
186 bool
187 ResultWindow::_AddPackages(BGroupLayout* packagesGroup,
188 	const PackageList& packages, const PackageSet& ignorePackages, bool install)
189 {
190 	bool packagesAdded = false;
191 
192 	for (int32 i = 0; BSolverPackage* package = packages.ItemAt(i);
193 		i++) {
194 		if (ignorePackages.find(package) != ignorePackages.end())
195 			continue;
196 
197 		BString text;
198 		if (install) {
199 			text.SetToFormat("install package %s from repository %s\n",
200 				package->Info().FileName().String(),
201 				package->Repository()->Name().String());
202 		} else {
203 			text.SetToFormat("uninstall package %s\n",
204 				package->VersionedName().String());
205 		}
206 
207 		BStringView* packageView = new BStringView(NULL, text);
208 		packagesGroup->AddView(packageView);
209 		packageView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
210 
211 		packagesAdded = true;
212 	}
213 
214 	return packagesAdded;
215 }
216