summaryrefslogtreecommitdiff
path: root/src/view_changelog.cc
blob: 50dbf605515a2eef668546219676cd4248c916eb (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
// view_changelog.cc
//
//   Copyright (C) 2004-2005 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 <vscreen/config/colors.h>
#include <vscreen/config/keybindings.h>
#include <vscreen/transcode.h>
#include <vscreen/vs_pager.h>
#include <vscreen/vs_scrollbar.h>
#include <vscreen/vs_table.h>
#include <vscreen/vs_text_layout.h>

#include "changelog_parse.h"
#include "download_bar.h"
#include "menu_redirect.h"
#include "menu_text_layout.h"
#include "ui.h"
#include "ui_download_manager.h"

#include <generic/apt/apt.h>
#include <generic/apt/pkg_changelog.h>
#include <generic/apt/config_signal.h>
#include <generic/apt/download_signal_log.h>

#include <generic/util/util.h>

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

using namespace std;

class pkg_changelog_screen : public vs_file_pager, public menu_redirect
{
  bool last_search_forwards;

  void do_search()
  {
    last_search_forwards = true;

    prompt_string(transcode(_("Search for: ")),
		  L"",
		  arg(sigc::mem_fun(*this, &vs_pager::search_for)),
		  NULL,
		  NULL,
		  NULL);
  }

  void do_search_back()
  {
    last_search_forwards = false;

    prompt_string(transcode(_("Search backwards for: ")),
		  L"",
		  arg(sigc::mem_fun(*this, &vs_pager::search_back_for)),
		  NULL,
		  NULL,
		  NULL);
  }

  void do_repeat_search()
  {
    if(last_search_forwards)
      search_for(L"");
    else
      search_back_for(L"");
  }

protected:
  pkg_changelog_screen(const temp::name &filename,
		       int x = 0, int y = 0,
		       int width = 0, int height = 0):
    vs_file_pager(filename.get_name()), last_search_forwards(true)
  {
    connect_key("Search", &global_bindings,
		sigc::mem_fun(*this, &pkg_changelog_screen::do_search));
    connect_key("SearchBack", &global_bindings,
		sigc::mem_fun(*this, &pkg_changelog_screen::do_search_back));
    connect_key("ReSearch", &global_bindings,
		sigc::mem_fun(*this, &pkg_changelog_screen::do_repeat_search));
  }

public:
  static ref_ptr<pkg_changelog_screen>
  create(const temp::name &filename,
	 int x = 0, int y = 0, int width = 0, int height = 0)
  {
    ref_ptr<pkg_changelog_screen>
      rval(new pkg_changelog_screen(filename, x, y, width, height));
    rval->decref();
    return rval;
  }

  bool find_search_enabled()
  {
    return true;
  }

  bool find_search()
  {
    do_search();
    return true;
  }

  bool find_search_back_enabled()
  {
    return true;
  }

  bool find_search_back()
  {
    do_search_back();
    return true;
  }

  bool find_research_enabled()
  {
    return !get_last_search().empty();
  }

  bool find_research()
  {
    do_repeat_search();
    return true;
  }
};
typedef ref_ptr<pkg_changelog_screen> pkg_changelog_screen_ref;


static void do_view_changelog(temp::name n,
			      string pkgname,
			      string curverstr)
{
  string menulabel =
    ssprintf(_("ChangeLog of %s"), pkgname.c_str());
  string tablabel = ssprintf(_("%s changes"), pkgname.c_str());
  string desclabel = _("View the list of changes made to this Debian package.");

  fragment *f = make_changelog_fragment(n, curverstr);

  vs_table_ref           t = vs_table::create();
  if(f != NULL)
    {
      vs_scrollbar_ref   s = vs_scrollbar::create(vs_scrollbar::VERTICAL);
      menu_text_layout_ref l = menu_text_layout::create();


      l->location_changed.connect(sigc::mem_fun(s.unsafe_get_ref(), &vs_scrollbar::set_slider));
      s->scrollbar_interaction.connect(sigc::mem_fun(l.unsafe_get_ref(), &vs_text_layout::scroll));
      l->set_fragment(f);

      t->add_widget_opts(l, 0, 0, 1, 1,
			 vs_table::EXPAND|vs_table::SHRINK, vs_table::EXPAND);
      t->add_widget_opts(s, 0, 1, 1, 1, 0,
			 vs_table::EXPAND | vs_table::FILL);

      create_menu_bindings(l.unsafe_get_ref(), t);
    }
  else
    {
      pkg_changelog_screen_ref cs = pkg_changelog_screen::create(n);
      vs_scrollbar_ref          s = vs_scrollbar::create(vs_scrollbar::VERTICAL);

      cs->line_changed.connect(sigc::mem_fun(s.unsafe_get_ref(), &vs_scrollbar::set_slider));
      s->scrollbar_interaction.connect(sigc::mem_fun(cs.unsafe_get_ref(), &pkg_changelog_screen::scroll_page));
      cs->scroll_top();

      t->add_widget_opts(cs, 0, 0, 1, 1,
			 vs_table::EXPAND|vs_table::SHRINK, vs_table::EXPAND);
      t->add_widget_opts(s, 0, 1, 1, 1, 0,
			 vs_table::EXPAND | vs_table::FILL);

      create_menu_bindings(cs.unsafe_get_ref(), t);
    }

  t->show_all();

  insert_main_widget(t, menulabel, desclabel, tablabel);
}

void view_changelog(pkgCache::VerIterator ver)
{
  bool in_debian=false;

  string pkgname = ver.ParentPkg().Name();

  pkgCache::VerIterator curver = ver.ParentPkg().CurrentVer();
  string curverstr;
  if(!curver.end() && curver.VerStr() != NULL)
    curverstr = curver.VerStr();

  // TODO: add a configurable association between origins and changelog URLs.
  for(pkgCache::VerFileIterator vf=ver.FileList();
      !vf.end() && !in_debian; ++vf)
    if(!vf.File().end() && vf.File().Origin()!=NULL &&
       strcmp(vf.File().Origin(), "Debian")==0)
      in_debian=true;

  if(!in_debian)
    {
      show_message(_("You can only view changelogs of official Debian packages."),
		   NULL, get_style("Error"));
      return;
    }

  download_manager *manager = get_changelog(ver,
					    sigc::bind(sigc::ptr_fun(&do_view_changelog), pkgname, curverstr));

  if(manager != NULL)
    (new ui_download_manager(manager, true, false, false,
			     _("Downloading Changelog"),
			     _(""),
			     _("Download Changelog")))->start();
}