summaryrefslogtreecommitdiff
path: root/src/gtk/dependency_chains_tab.cc
blob: 5a7975dc3164d5ed347c4742ea70c3d6cd966417 (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
// dependency_chains_tab.cc
//
//   Copyright (C) 2008 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.

/** \file dependency_chains_tab.cc */

#include "dependency_chains_tab.h"

#include "aptitude.h"

#include "entityview.h"
#include "gui.h"
#include "packagestab.h"
#include "pkgview.h"

#include <cmdline/cmdline_why.h>

#include <cwidget/generic/util/ssprintf.h>

#include <generic/apt/matchers.h>

namespace gui
{
  // \todo let the user adjust the parameters of the search
  // interactively.  First we have to figure out what the best axes of
  // twiddling are...
  DependencyChainsTab::DependencyChainsTab(const Glib::ustring &label)
    : Tab(DependencyChains, label, Gnome::Glade::Xml::create(glade_main_file, "dependency_path_main"), "dependency_path_main")
  {
    get_xml()->get_widget("dependency_path_start_search_pattern",
			  start_package_entry);
    get_xml()->get_widget("dependency_path_end_search_pattern",
			  end_package_entry);

    get_widget()->show();

    Gtk::Button *start_search_button;
    Gtk::Button *end_search_button;

    get_xml()->get_widget("dependency_path_start_find_button",
			  start_search_button);
    get_xml()->get_widget("dependency_path_end_find_button",
			  end_search_button);

    using cwidget::util::ref_ptr;
    start_package_view = ref_ptr<PkgView>(new PkgView(get_xml(), "dependency_path_start_packages_treeview"));
    end_package_view = ref_ptr<PkgView>(new PkgView(get_xml(), "dependency_path_end_packages_treeview"));
    results_view = ref_ptr<EntityView>(new EntityView(get_xml(), "dependency_path_results_treeview"));

    end_package_view->get_treeview()->get_selection()->set_mode(Gtk::SELECTION_BROWSE);

    start_package_view->get_treeview()->get_selection()->signal_changed()
      .connect(sigc::mem_fun(this, &DependencyChainsTab::selection_changed));
    end_package_view->get_treeview()->get_selection()->signal_changed()
      .connect(sigc::mem_fun(this, &DependencyChainsTab::selection_changed));

    setup_searchable_view(start_package_entry, start_search_button, start_package_view,
			  sigc::slot0<void>());
    setup_searchable_view(end_package_entry, end_search_button, end_package_view,
			  sigc::slot0<void>());

    results_view->get_version_column()->set_visible(false);
    selection_changed();

    // \todo do something sensible on "closed".
    cache_reloaded.connect(sigc::mem_fun(this, &DependencyChainsTab::selection_changed));
  }

  namespace
  {
    pkgCache::PkgIterator get_path_package(const Glib::RefPtr<Gtk::TreeModel> &model,
					   const EntityColumns &columns,
					   const Gtk::TreeModel::Path &path)
    {
      pkgCache::PkgIterator rval(*apt_cache_file, 0);
      Gtk::TreeModel::iterator iter = model->get_iter(path);
      if(iter != model->children().end())
	{
	  cwidget::util::ref_ptr<Entity> ent = (*iter)[columns.EntObject];
	  cwidget::util::ref_ptr<PkgEntity> pkg_ent = ent.dyn_downcast<PkgEntity>();
	  rval = pkg_ent->get_pkg();
	}

      return rval;
    }
  }

  /** \brief An entity class that's responsible for displaying rows in
   *  a dependency chain view.
   *
   *  \todo Each row represents a single package.  Will this make
   *  sense to the user?  Should I just ditch the PkgView entirely and
   *  use a straight TreeView?
   */
  class DependencyChainActionEntity : public PkgEntity
  {
    aptitude::why::action act;
  public:
    DependencyChainActionEntity(const aptitude::why::action &_act)
      : PkgEntity(!_act.get_dep().end()
		    ? _act.get_dep().ParentPkg()
		    : _act.get_prv().OwnerPkg()),
	act(_act)
    {
    }

    void fill_row(const EntityColumns *cols,
		  Gtk::TreeModel::Row &row)
    {
      PkgEntity::fill_row(cols, row);

      Glib::ustring text;
      if(!act.get_dep().end())
	{
	  pkgCache::DepIterator dep = act.get_dep();

	  pkgCache::DepIterator start, end;
	  surrounding_or(dep, start, end);

	  text = dep.ParentPkg().Name();
	  text += " ";
	  text += dep.ParentVer().VerStr();
	  text += " ";
	  text += dep.DepType();
	  text += " ";
	  text += dep.TargetPkg().Name();

	  if(dep.TargetVer() != NULL)
	    {
	      text += " (";
	      text += dep.CompType();
	      text += " ";
	      text += dep.TargetVer();
	      text += ")";
	    }
	}
      else
	{
	  pkgCache::PrvIterator prv = act.get_prv();

	  text = prv.OwnerPkg().Name();
	  text += " ";
	  text += prv.OwnerVer().VerStr();
	  text += " ";
	  text += _("Provides");
	  text += " ";
	  text += prv.ParentPkg().Name();
	}

      row[cols->NameMarkup] = Glib::Markup::escape_text(text);
    }
  };

  Glib::RefPtr<Gtk::TreeModel> DependencyChainsTab::get_results()
  {
    using namespace aptitude;

    Glib::RefPtr<Gtk::ListStore> store = Gtk::ListStore::create(*results_view->get_columns());

    // Just add the error row up-front so we know it always appears if
    // we break out.
    {
      Gtk::TreeModel::iterator iter = store->append();
      Gtk::TreeModel::Row row = *iter;
      (new HeaderEntity(_("Select one or more starting packages and an ending package to search.")))->fill_row(results_view->get_columns(), row);
    }

    Glib::RefPtr<Gtk::TreeView::Selection> start_selection =
      start_package_view->get_treeview()->get_selection();
    Glib::RefPtr<Gtk::TreeView::Selection> end_selection =
      end_package_view->get_treeview()->get_selection();

    if(!start_selection || !end_selection)
      return store;

    Gtk::TreeSelection::ListHandle_Path start_rows = start_selection->get_selected_rows();
    Gtk::TreeSelection::ListHandle_Path end_rows = end_selection->get_selected_rows();

    if(start_rows.empty())
      return store;

    const Gtk::TreeSelection::ListHandle_Path::const_iterator end_begin =
      end_rows.begin();
    if(end_begin == end_rows.end())
      return store;
    const pkgCache::PkgIterator target = get_path_package(end_package_view->get_model(),
							  *end_package_view->get_columns(),
							  *end_begin);
    if(target.end())
      return store;

    std::vector<matching::pkg_matcher *> leaves;
    for(Gtk::TreeSelection::ListHandle_Path::const_iterator
	  it = start_rows.begin(); it != start_rows.end(); ++it)
      {
	const pkgCache::PkgIterator leaf = get_path_package(start_package_view->get_model(),
							    *start_package_view->get_columns(),
							    *it);
	if(!leaf.end())
	  leaves.push_back(matching::make_const_matcher(leaf));
      }

    if(leaves.empty())
      return store;

    // Clear the error message.
    store->clear();

    // Now we run the "why" algorithm.
    //
    // TODO: hoist the "strongest tightest narrowest" logic out of
    // cmdline_why and expose it to this code, then use it.
    std::vector<std::vector<why::action> > results;
    if(!why::find_justification(why::target::Install(target),
				leaves,
				why::search_params(why::search_params::Install,
						   why::search_params::Recommends,
						   true),
				false,
				results) ||
       results.empty() || results[0].empty())
      {
	Gtk::TreeModel::iterator iter = store->append();
	Gtk::TreeModel::Row row = *iter;
	(new HeaderEntity(_("No dependency chain found.")))->fill_row(results_view->get_columns(), row);
      }
    else
      {
	for(std::vector<why::action>::const_iterator act_it = results[0].begin();
	    act_it != results[0].end(); ++act_it)
	  {
	    // TODO: add support for viewing more than one
	    // result. (left/right arrow buttons?)
	    Gtk::TreeModel::iterator iter = store->append();
	    Gtk::TreeModel::Row row = *iter;
	    (new DependencyChainActionEntity(*act_it))->fill_row(results_view->get_columns(),
								 row);
	  }
      }

    return store;
  }

  void DependencyChainsTab::selection_changed()
  {
    results_view->set_model(get_results());
  }
}