summaryrefslogtreecommitdiff
path: root/src/broken_indicator.cc
blob: 67e32e93f90b130793dfe460ec77e8afd27ab347 (plain)
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
// broken_indicator.cc
//
//   Copyright (C) 2005, 2007 Daniel Burrows
//
//   This program 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 2 of
//   the License, or (at your option) any later version.
//
//   This program 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 this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.

#include "broken_indicator.h"

#include <aptitude.h>

#include <generic/apt/apt.h>
#include <generic/apt/aptcache.h>
#include <generic/apt/aptitude_resolver_universe.h>
#include <generic/apt/config_signal.h>
#include <generic/apt/resolver_manager.h>

#include <generic/problemresolver/exceptions.h>
#include <generic/problemresolver/solution.h>

#include <generic/util/util.h>

#include <vscreen/config/colors.h>
#include <vscreen/config/keybindings.h>
#include <vscreen/fragment.h>
#include <vscreen/vs_text_layout.h>
#include <vscreen/vscreen.h>

#include <apt-pkg/pkgsystem.h>

#include <string>
#include <vector>

using namespace std;

typedef generic_solution<aptitude_universe> aptitude_solution;

/** A simple indicator, usually placed at the bottom of the screen,
 *  that describes the current state of the problem resolver.  Hidden
 *  if no problem resolver is active.
 *
 *  \todo write a simple table fragment class and use that to
 *  right-justify the text that obviously should be
 */
class broken_indicator:public vs_text_layout
{
  aptitude_solution last_sol;

  /** Records whether we had generated all solutions at the time of
   *  the last update.
   */
  bool last_complete : 1;

  /** Records whether the background thread was active at the time of
   *  the last update.
   */
  bool last_background_active : 1;

  /** Tracks the phase of the visual "spinner". */
  int spin_count;

  void handle_cache_reload()
  {
    if(resman != NULL)
      resman->state_changed.connect(sigc::mem_fun(*this, &broken_indicator::post_update));

    update();
  }

protected:
  broken_indicator()
    :spin_count(0)
  {
    if(resman != NULL)
      resman->state_changed.connect(sigc::mem_fun(*this, &broken_indicator::post_update));

    cache_closed.connect(sigc::mem_fun(*this, &broken_indicator::update));
    cache_reloaded.connect(sigc::mem_fun(*this, &broken_indicator::handle_cache_reload));

    set_bg_style(get_style("Error"));

    update();

    vscreen_addtimeout(new slot_event(sigc::mem_fun(this, &broken_indicator::tick_timeout)),
		       aptcfg->FindI(PACKAGE "::Spin-Interval", 500));
  }

private:
  static fragment *key_hint_fragment(const resolver_manager::state &state)
  {
    wstring next=global_bindings.readable_keyname("NextSolution");
    wstring prev=global_bindings.readable_keyname("PrevSolution");
    wstring examine=global_bindings.readable_keyname("ExamineSolution");
    wstring apply=global_bindings.readable_keyname("ApplySolution");



    style st_normal;
    style st_disabled;
    st_disabled.attrs_off(A_BOLD);
    st_disabled.attrs_on(A_DIM);
    st_disabled.set_fg(COLOR_BLACK);

    vector<fragment *> key_hints;

    key_hints.push_back(fragf(_("%ls: Examine"),
			      examine.c_str()));


    bool can_apply = (state.selected_solution < state.generated_solutions);
    bool can_next = (state.selected_solution < state.generated_solutions &&
		     !(state.selected_solution + 1 == state.generated_solutions && state.solutions_exhausted));

    key_hints.push_back(style_fragment(fragf(_("%ls: Apply"),
					     apply.c_str()),
				       can_apply ? st_normal : st_disabled));
    key_hints.push_back(style_fragment(fragf(_("%ls: Next"),
					     next.c_str()),
				       can_next ? st_normal : st_disabled));

    bool can_prev = (state.selected_solution > 0);
    key_hints.push_back(style_fragment(fragf(_("%ls: Previous"),
					     prev.c_str()),
				       can_prev ? st_normal : st_disabled));

    return join_fragments(key_hints, L"  ");
  }

  void tick_timeout()
  {
    vs_widget_ref tmpref(this);

    if(resman != NULL && resman->background_thread_active())
      {
 	++spin_count;
	update();
	vscreen_update();
      }

    vscreen_addtimeout(new slot_event(sigc::mem_fun(this, &broken_indicator::tick_timeout)),
		       aptcfg->FindI(PACKAGE "::Spin-Interval", 500));
  }

  std::string spin_string(const resolver_manager::state &state) const
  {
    if(!state.background_thread_active)
      return " ";

    switch(spin_count % 4)
      {
      case 0:
	return ".";
      case 1:
	return "o";
      case 2:
	return "O";
      case 3:
	return "o";
      default:
	return "?";
      }
  }

  struct update_event : public vscreen_event
  {
    broken_indicator *b;
  public:
    update_event(broken_indicator *_b)
      : b(_b)
    {
    }

    void dispatch()
    {
      b->update();
    }
  };

  /** Post an update to run in the main thread; needed since the
   *  selected_signal_changed signal might theoretically run from a
   *  background thread.  (at the moment it shouldn't, but this will
   *  help avoid nasty surprises)
   */
  void post_update()
  {
    vscreen_post_event(new update_event(this));
  }
public:
  static ref_ptr<broken_indicator> create()
  {
    ref_ptr<broken_indicator> rval(new broken_indicator);
    rval->decref();
    return rval;
  }

  // TODO: split this monster up.
  void update()
  {
    vs_widget_ref tmpref(this);

    if(resman == NULL || !resman->resolver_exists())
      {
	set_fragment(fragf(""));
	last_sol.nullify();
	hide();
	return;
      }

    // Take a snapshot of the state.
    resolver_manager::state state = resman->state_snapshot();

    if(state.solutions_exhausted && state.generated_solutions == 0)
      {
	set_fragment(fragf(_("Unable to resolve dependencies.")));
	last_sol.nullify();
	show();
	return;
      }

    // Handle the case where the resolver is churning away.
    if(state.selected_solution >= state.generated_solutions)
      {
	if(state.background_thread_aborted)
	  {
	    set_fragment(fragf(_("Fatal error in resolver")));
	    last_sol.nullify();
	    show();
	    return;
	  }

	// TODO: add a column-generating fragment that can
	//       left/right justify stuff.

	vector<fragment_column_entry> columns;

	columns.push_back(fragment_column_entry(true, false, 1, fragment_column_entry::top, flowbox(text_fragment(ssprintf(_("[%d(%d)/...] Resolving dependencies"),
														    state.selected_solution + 1,
														    state.generated_solutions)))));

	columns.push_back(fragment_column_entry(false, false, 1, fragment_column_entry::top, NULL));

	columns.push_back(fragment_column_entry(false, false, 1, fragment_column_entry::top, text_fragment(spin_string(state))));

	set_fragment(sequence_fragment(fragment_columns(columns),
				       key_hint_fragment(state),
				       NULL));
	last_sol.nullify();
	show();
	return;
      }

    aptitude_solution sol = resman->get_solution(state.selected_solution, 0);

    // This test always fails the first time update() is called, since
    // sol is never NULL and last_sol is initialized to NULL.
    if(sol == last_sol && state.solutions_exhausted == last_complete &&
       // If there's an active thread we need to redraw the widget to
       // include the spinner.
       !last_background_active &&
       state.background_thread_active == last_background_active)
      return;

    last_sol = sol;
    last_complete = state.solutions_exhausted;
    last_background_active = state.background_thread_active;

    if(sol.get_actions().empty())
      {
	set_fragment(fragf("%s", _("Internal error: unexpected null solution.")));
	show();
	return;
      }

    int install_count=0, remove_count=0, keep_count=0, upgrade_count=0, downgrade_count=0;

    for(imm::map<aptitude_universe::package, aptitude_solution::action>::const_iterator
	  i = sol.get_actions().begin();
	i != sol.get_actions().end(); ++i)
      {
	pkgCache::PkgIterator pkg=i->first.get_pkg();
	pkgCache::VerIterator curver=pkg.CurrentVer();
	pkgCache::VerIterator instver=(*apt_cache_file)[pkg].InstVerIter(*apt_cache_file);
	pkgCache::VerIterator newver=i->second.ver.get_ver();

	// If not, we have a problem.
	eassert(instver!=newver);

	if(newver == curver)
	  ++keep_count;
	else if(curver.end())
	  ++install_count;
	else if(newver.end())
	  ++remove_count;
	else
	  {
	    int cmp=_system->VS->CmpVersion(curver.VerStr(),
					    newver.VerStr());

	    // The versions shouldn't be equal -- otherwise
	    // something is majorly wrong.
	    // eassert(cmp!=0);
	    //
	    // The above is not true: consider, eg, the case of a
	    // locally compiled package and a standard package.

	    /** \todo indicate "sidegrades" separately? */
	    if(cmp<=0)
	      ++upgrade_count;
	    else if(cmp>0)
	      ++downgrade_count;
	  }
      }

    vector<fragment *> fragments;

    string countstr
      = ssprintf(state.solutions_exhausted?"[%d/%d]":"[%d(%d)/...]",
		 state.selected_solution + 1,
		 state.generated_solutions);
    fragments.push_back(fragf("%s ", countstr.c_str()));


    vector<fragment *> suggestions;

    if(install_count>0)
      suggestions.push_back(text_fragment(ssprintf(ngettext("%d install",
							    "%d installs",
							    install_count),
						   install_count)));

    if(remove_count>0)
      suggestions.push_back(text_fragment(ssprintf(ngettext("%d removal",
							    "%d removals",
							    remove_count),
						   remove_count)));

    if(keep_count>0)
      suggestions.push_back(text_fragment(ssprintf(ngettext("%d keep",
							    "%d keeps",
							    keep_count),
						   keep_count)));

    if(upgrade_count>0)
      suggestions.push_back(text_fragment(ssprintf(ngettext("%d upgrade",
							    "%d upgrades",
							    upgrade_count),
						   upgrade_count)));

    if(downgrade_count>0)
      suggestions.push_back(text_fragment(ssprintf(ngettext("%d downgrade",
							    "%d downgrades",
							    downgrade_count),
						   downgrade_count)));

    /* ForTranslators: %F is replaced with a comma separated list such as
       "n1 installs, n2 removals", ...
     */
    fragments.push_back(fragf(_("Suggest %F"), join_fragments(suggestions, L", ")));

    if(state.background_thread_active)
      {
	vector<fragment_column_entry> columns;
	columns.push_back(fragment_column_entry(true, false, 1, fragment_column_entry::top, hardwrapbox(sequence_fragment(fragments))));

	columns.push_back(fragment_column_entry(false, false, 1, fragment_column_entry::top, NULL));

	columns.push_back(fragment_column_entry(false, false, 1, fragment_column_entry::top, text_fragment(spin_string(state))));


	fragments.clear();
	fragments.push_back(fragment_columns(columns));
      }
    else
      fragments.push_back(newline_fragment());
    fragments.push_back(hardwrapbox(key_hint_fragment(state)));

    fragment *f=sequence_fragment(fragments);
    set_fragment(f);

    show();
  }
};

ref_ptr<vscreen_widget> make_broken_indicator()
{
  return broken_indicator::create();
}