libyui-gtk-pkg  2.42.9
 All Classes
ygtkpkgproductdialog.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 /* YGtkPkgMenuBar, menu bar */
5 // check the header file for information about this widget
6 
7 /*
8  Textdomain "gtk"
9  */
10 
11 #include "YGi18n.h"
12 #include "config.h"
13 #include "YGDialog.h"
14 #include "YGUtils.h"
15 #include "ygtkpkgproductdialog.h"
16 #include <gtk/gtk.h>
17 #include "ygtkrichtext.h"
18 
19 #include <zypp/ui/Status.h>
20 #include <zypp/ui/Selectable.h>
21 #include <zypp/ResObject.h>
22 #include <zypp/Package.h>
23 #include <zypp/Pattern.h>
24 #include <zypp/Patch.h>
25 #include <zypp/Product.h>
26 #include <zypp/ZYppFactory.h>
27 #include <zypp/ResPoolProxy.h>
28 
29 typedef zypp::ResPoolProxy ZyppPool;
30 typedef zypp::ResPoolProxy::const_iterator ZyppPoolIterator;
31 inline ZyppPool zyppPool() { return zypp::getZYpp()->poolProxy(); }
32 template<class T> ZyppPoolIterator zyppBegin() { return zyppPool().byKindBegin<T>(); }
33 template<class T> ZyppPoolIterator zyppEnd() { return zyppPool().byKindEnd<T>(); }
34 inline ZyppPoolIterator zyppProductsBegin() { return zyppBegin<zypp::Product>(); }
35 inline ZyppPoolIterator zyppProductsEnd() { return zyppEnd<zypp::Product>(); }
36 typedef zypp::ui::Selectable::Ptr ZyppSel;
37 typedef zypp::ResObject::constPtr ZyppObj;
38 typedef zypp::Product::constPtr ZyppProduct;
39 inline ZyppProduct tryCastToZyppProduct( ZyppObj zyppObj )
40 { return zypp::dynamic_pointer_cast<const zypp::Product>( zyppObj ); }
41 
42 enum {
43  INSTALLED_COLUMN, TEXT_COLUMN, VERSION_COLUMN, VENDOR_COLUMN, DESCRIPTION_COLUMN, TOTAL_COLUMNS
44 };
45 
46 static void selection_changed_cb (GtkTreeSelection *selection, YGtkRichText *description)
47 {
48  GtkTreeModel *model;
49  GtkTreeIter iter;
50  if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
51  char *text;
52  gtk_tree_model_get (model, &iter, DESCRIPTION_COLUMN, &text, -1);
53  ygtk_rich_text_set_text (description, text);
54  g_free (text);
55  }
56  else
57  ygtk_rich_text_set_plain_text (description, "");
58 }
59 
60 YGtkPkgProductDialog::YGtkPkgProductDialog()
61 {
62  GtkListStore *store = gtk_list_store_new (TOTAL_COLUMNS, G_TYPE_BOOLEAN,
63  G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
64 
65  for (ZyppPoolIterator it = zyppProductsBegin(); it != zyppProductsEnd(); it++) {
66  ZyppSel sel = *it;
67  ZyppProduct prod = tryCastToZyppProduct( sel->theObj() );
68 
69  std::string text (sel->name() + "\n<small>" + prod->summary() + "</small>");
70 
71  ZyppObj available = sel->candidateObj();
72  ZyppObj installed = sel->installedObj();
73 
74  std::string version;
75  if (!!available && !!installed && (available->edition() != installed->edition())) {
76  version = available->edition().asString() + "\n<small>";
77  version += installed->edition().asString() + "</small>";
78  }
79  else if (!!available)
80  version = available->edition().asString();
81  else if (!!installed)
82  version = installed->edition().asString();
83 
84  std::string description;
85  if (!!available) {
86  description += std::string ("<p><b>") + _("Candidate provides:") + " </b>";
87  description += available->dep (zypp::Dep::PROVIDES).begin()->asString();
88  }
89  if (!!installed) {
90  description += std::string ("<p><b>") + _("Installed provides:") + " </b>";
91  description += installed->dep (zypp::Dep::PROVIDES).begin()->asString();
92  }
93 
94 
95  GtkTreeIter iter;
96  gtk_list_store_append (store, &iter);
97  gtk_list_store_set (store, &iter, INSTALLED_COLUMN,
98  sel->candidateObj().isSatisfied() || sel->hasInstalledObj(),
99  TEXT_COLUMN, text.c_str(), VERSION_COLUMN, version.c_str(),
100  VENDOR_COLUMN, prod->vendor().c_str(), DESCRIPTION_COLUMN,
101  description.c_str(), -1);
102  }
103 
104  GtkWidget *description = ygtk_rich_text_new();
105  GtkWidget *description_scroll = gtk_scrolled_window_new (NULL, NULL);
106  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (description_scroll),
107  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
108  gtk_scrolled_window_set_shadow_type (
109  GTK_SCROLLED_WINDOW (description_scroll), GTK_SHADOW_IN);
110  gtk_container_add (GTK_CONTAINER (description_scroll), description);
111 
112  GtkWidget *view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
113  GtkTreeView *tview = GTK_TREE_VIEW (view);
114  g_object_unref (G_OBJECT (store));
115  gtk_tree_view_set_search_column (tview, TEXT_COLUMN);
116  gtk_tree_view_set_rules_hint (tview, TRUE);
117 
118  GtkCellRenderer *renderer;
119  GtkTreeViewColumn *column;
120 
121  renderer = gtk_cell_renderer_toggle_new();
122  column = gtk_tree_view_column_new_with_attributes (
123  NULL, renderer, "active", INSTALLED_COLUMN, NULL);
124  gtk_cell_renderer_set_sensitive(renderer, FALSE);
125  gtk_tree_view_append_column (tview, column);
126 
127  renderer = gtk_cell_renderer_text_new();
128  column = gtk_tree_view_column_new_with_attributes (
129  _("Name"), renderer, "markup", TEXT_COLUMN, NULL);
130  g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
131  gtk_tree_view_column_set_resizable (column, TRUE);
132  gtk_tree_view_column_set_expand (column, TRUE);
133  gtk_tree_view_append_column (tview, column);
134 
135  renderer = gtk_cell_renderer_text_new();
136  column = gtk_tree_view_column_new_with_attributes (
137  _("Version"), renderer, "markup", VERSION_COLUMN, NULL);
138  gtk_tree_view_column_set_resizable (column, TRUE);
139  gtk_tree_view_append_column (tview, column);
140 
141  renderer = gtk_cell_renderer_text_new();
142  column = gtk_tree_view_column_new_with_attributes (
143  _("Vendor"), renderer, "text", VENDOR_COLUMN, NULL);
144  gtk_tree_view_column_set_resizable (column, TRUE);
145  gtk_tree_view_append_column (tview, column);
146 
147  GtkTreeSelection *selection = gtk_tree_view_get_selection (tview);
148  gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
149  g_signal_connect (G_OBJECT (selection), "changed",
150  G_CALLBACK (selection_changed_cb), description);
151 
152  GtkWidget *scroll = gtk_scrolled_window_new (NULL, NULL);
153  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
154  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
155  gtk_scrolled_window_set_shadow_type (
156  GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_IN);
157  gtk_container_add (GTK_CONTAINER (scroll), view);
158 
159  GtkWidget *dialog = gtk_message_dialog_new (YGDialog::currentWindow(),
160  // Translators: same as "Listing of Products"
161  GtkDialogFlags (0), GTK_MESSAGE_OTHER, GTK_BUTTONS_CLOSE, _("Products Listing"));
162  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
163  gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
164  gtk_window_set_default_size (GTK_WINDOW (dialog), 600, 500);
165 
166  GtkWidget *vpaned = gtk_vpaned_new();
167  gtk_paned_pack1 (GTK_PANED (vpaned), scroll, TRUE, FALSE);
168  gtk_paned_pack2 (GTK_PANED (vpaned), description_scroll, FALSE, TRUE);
169  YGUtils::setPaneRelPosition (vpaned, .70);
170  gtk_widget_set_vexpand (vpaned, TRUE);
171  GtkContainer *content = GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog)));
172  gtk_container_add (content, vpaned);
173 
174  gtk_widget_show_all (dialog);
175  m_dialog = dialog;
176 }
177 
178 YGtkPkgProductDialog::~YGtkPkgProductDialog()
179 { gtk_widget_destroy (m_dialog); }
180 
181 void YGtkPkgProductDialog::popup()
182 { gtk_dialog_run (GTK_DIALOG (m_dialog)); }
183