summaryrefslogtreecommitdiff
path: root/src/edit_pkg_hier.cc
blob: f0f13cc8f985d82fbf7966f66f37166a30493d7a (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
// edit_pkg_hier.cc
//
//   Copyright (C) 2000-2001, 2004-2006 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 "edit_pkg_hier.h"

#include "aptitude.h"
#include "ui.h"

#include <apt-pkg/error.h>

#include <sigc++/adaptors/bind.h>
#include <sigc++/functors/mem_fun.h>
#include <sigc++/trackable.h>

#include <generic/apt/apt.h>

#include <generic/util/util.h>

#include <cwidget/config/keybindings.h>
#include <cwidget/widgets/subtree.h>
#include <cwidget/generic/util/transcode.h>

using namespace std;
using namespace __gnu_cxx;

// Stores a group (name) and a Y/N state
class widgets::hier_editor::widgets::hier_item:public sigc::trackable, public widgets::treeitem
{
  bool selected;
  pkg_hier::group *group;
  pkg_hier::item *item;
  // whatever
  wstring group_name;
public:
  widgets::hier_item(pkg_hier::group *_group, pkg_hier::item *_item)
    :widgets::treeitem(true), group(_group), group_name(cw::util::transcode(group->name, "ASCII"))
  {
    set_item(_item);
  }

  bool dispatch_key(const cwi::key &k, widgets::tree *owner)
  {
    if(cw::global_bindings.key_matches(k, "PushButton") ||
       cw::global_bindings.key_matches(k, "Confirm"))
      {
	selected=!selected;
	toplevel::update();
      }
    else
      return widgets::treeitem::dispatch_key(k, owner);

    return true;
  }

  void dispatch_mouse(short id, int x, mmask_t bstate, widgets::tree *owner)
  {
    if(bstate&BUTTON1_DOUBLE_CLICKED || bstate & BUTTON3_PRESSED ||
       bstate & BUTTON3_CLICKED)
      {
	selected=!selected;
	toplevel::update();
      }
    else
      widgets::treeitem::dispatch_mouse(id, x, bstate, owner);
  }

  void paint(widgets::tree *win, int y, bool hierarchical, const cw::style const style &st)
  {
    string::size_type width=win->get_width();
    string todisp=" ";

    if(selected)
      todisp+='*';
    else
      todisp+=' ';

    todisp+=" ";
    todisp+=group->name;
    todisp+=" : ";
    todisp+=group->description;

    while(todisp.size()<width)
      todisp+=" ";

    win->mvaddnstr(y, 0, cw::util::transcode(todisp, "ASCII"), width);
  }

  const wchar_t *tag()
  {
    return group_name.c_str();
  }

  const wchar_t *label()
  {
    return group_name.c_str();
  }

  void commit()
  {
    if(selected)
      item->parents.insert(group->name);
    else
      item->parents.erase(group->name);
  }

  void set_item(pkg_hier::item *_item)
  {
    if(item!=_item)
      // Don't unnecessarily clobber our state.
      {
	item=_item;

	if(item)
	  selected=item->parents.find(group->name)!=item->parents.end();
	else
	  selected=false;
      }
  }
};

// FIXME: I shouldn't have to do this.
class silly_subtree:public widgets::subtree_generic
{
  wstring txt;
public:
  silly_subtree(bool expanded, const wstring &_txt)
    :widgets::subtree_generic(expanded), txt(_txt) {}

  void paint(widgets::tree *win, int y, bool hierarchical, const cw::style const style &st)
  {
    widgets::subtree_generic::paint(win, y, hierarchical, txt);
  }

  const wchar_t *tag() {return txt.c_str();}
  const wchar_t *label() {return txt.c_str();}
};

widgets::hier_editor::widgets::hier_editor():item(NULL)
{
  hier_reloaded.connect(sigc::mem_fun(*this, &widgets::hier_editor::handle_reload));
}

void widgets::hier_editor::handle_reload()
{
  widgets::widget_ref tmpref(this);

  set_root(NULL);
  items.clear();

  item=NULL;

  set_package(pkgCache::PkgIterator(),
	      pkgCache::VerIterator(*apt_cache_file));
}

bool widgets::hier_editor::get_cursorvisible()
{
  if(!item)
    return false;
  else
    return true;
}

void widgets::hier_editor::paint(const cw::style const style &st)
{
  if(!item)
    mvaddnstr(0, 0, _("No hierarchy information to edit"), get_width());
  else
    widgets::tree::paint(st);
}

// Creates a new list of edit-widgets if pkg isn't an end iterator.
//
// (yes, it would be nicer in some ways to not recreate the tree continually)
void widgets::hier_editor::set_package(const pkgCache::PkgIterator &pkg)
{
  widgets::widget_ref tmpref(this);

  shown_conn.disconnect();
  if(get_visible())
    {
      pkg_hier *user_pkg_hier=get_user_pkg_hier();

      if(user_pkg_hier && !pkg.end())
	{
	  // Get a reference to the item stored in the hierarchy, or
	  // create it if it isn't there.
	  pkg_hier::pkgmap::iterator found=user_pkg_hier->pkgs.find(pkg.Name());
	  item=NULL;

	  if(found==user_pkg_hier->pkgs.end())
	    {
	      user_pkg_hier->pkgs[pkg.Name()]=pkg_hier::item(pkg.Name());
	      item=&user_pkg_hier->pkgs[pkg.Name()];
	    }
	  else
	    item=&found->second;

	  if(items.empty())
	    {
	      silly_subtree *newroot=new silly_subtree(true, L"All groups");

	      // Add all available groups to the list.
	      for(pkg_hier::groupmap::iterator i=user_pkg_hier->groups.begin();
		  i!=user_pkg_hier->groups.end();
		  ++i)
		{
		  widgets::hier_item *tmp=new widgets::hier_item(&i->second, item);
		  commit_changes.connect(sigc::mem_fun(*tmp, &widgets::hier_item::commit));

		  newroot->add_child(tmp);
		  items.push_back(tmp);
		}

	      newroot->sort();

	      set_root(newroot);
	    }

	  for(vector<widgets::hier_item *>::iterator i=items.begin();
	      i!=items.end(); ++i)
	    (*i)->set_item(item);
	}
      else
	item=NULL;
    }
  else
    {
      string name=pkg.end()?"":pkg.Name();

      shown_conn=shown_sig.connect(sigc::bind(sigc::mem_fun(*this,
							    (void (widgets::hier_editor::*) (string)) &widgets::hier_editor::set_package),
					      name));
    }
}

void widgets::hier_editor::set_package(const pkgCache::PkgIterator &pkg,
				 const pkgCache::VerIterator &ver)
{
  set_package(pkg);
}

void widgets::hier_editor::set_package(std::string name)
{
  if(apt_cache_file)
    set_package((*apt_cache_file)->FindPkg(name));
}

// Lets us sort stuff by name:
struct item_cmp
{
  bool operator()(const pkg_hier::item *a, const pkg_hier::item *b)
  {
    return a->name<b->name;
  }
};

void widgets::hier_editor::save_hier(string file)
{
  // We copy references to items into a list, then sort it (so that the
  // output is in a predictable order -- this makes diffs against an
  // autogenerated file much more useful)
  vector<pkg_hier::item *> items;

  FILE *f=fopen(file.c_str(), "w");

  if(!f)
    {
      _error->Errno("save_hier", _("Couldn't open \"%s\" for writing"), file.c_str());
      return;
    }

  for(pkg_hier::pkgmap::iterator i=get_user_pkg_hier()->pkgs.begin();
      i!=get_user_pkg_hier()->pkgs.end();
      ++i)
    // Don't save items with no parents, it's pointless.
    if(!i->second.parents.empty())
      items.push_back(&i->second);

  sort(items.begin(), items.end(), item_cmp());

  for(vector<pkg_hier::item *>::iterator i=items.begin();
      i!=items.end();
      ++i)
    {
      string parentsstr;

      for(hash_set<string>::iterator j=(*i)->parents.begin();
	  j!=(*i)->parents.end(); ++j)
	{
	  if(j==(*i)->parents.begin())
	    parentsstr+=*j;
	  else
	    parentsstr+=", "+*j;
	}

      fprintf(f, "%sPackage: %s\nParents: %s\n",
	      i==items.begin()?"":"\n",
	      (*i)->name.c_str(), parentsstr.c_str());
    }

  fclose(f);
}

bool widgets::hier_editor::handle_key(const cwi::key &k)
{
  if(cw::global_bindings.key_matches(k, "SaveHier"))
    {
      string homedir = get_homedir();
      string cfgfile;

      if(homedir.empty())
	{
	  show_message(_("Unable to look up your home directory, saving to /tmp/function_pkgs!"),
		       NULL,
		       get_style("Error"));
	  cfgfile = "/tmp/function_pkgs";
	}
      else
	cfgfile = homedir + "/.aptitude/function_pkgs";
      save_hier(cfgfile);
    }
  else if(cw::global_bindings.key_matches(k, "Quit"))
    {
      if(item)
	{
	  item->parents.clear();

	  for(vector<widgets::hier_item *>::iterator i=items.begin();
	      i!=items.end(); ++i)
	    (*i)->commit();
	}

      hide();
    }
  else if(cw::global_bindings.key_matches(k, "Commit"))
    {
      if(item)
	{
	  item->parents.clear();

	  for(vector<widgets::hier_item *>::iterator i=items.begin();
	      i!=items.end(); ++i)
	    (*i)->commit();
	}

      commit_changes();
    }
  else if(cw::global_bindings.key_matches(k, "Abort"))
    hide();
  else
    return widgets::tree::handle_key(k);

  return true;
}