~jibel/synaptic/bugs.290524.502582

« back to all changes in this revision

Viewing changes to gtk/rgmisc.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-08-22 12:40:10 UTC
  • Revision ID: james.westby@ubuntu.com-20080822124010-5z3kuzjp2jozl13a
Tags: 0.62.1ubuntu8
* common/rpackageview.cc:
  - add new "Missing Recommends" default filter
* common/rpackage.cc:
  - fix code to get candidate origin
  - support getting the candidate release file name
* common/rpackagestatus.cc:
  - support maintenanceEndTime() (if the distro supports that)
* 10_ubuntu_maintenance_gui.dpatch:
  - add support for displaying when the maintaince ends

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* rgmisc.cc 
2
 
 * 
3
 
 * Copyright (c) 2003 Michael Vogt
4
 
 * 
5
 
 * Author: Michael Vogt <mvo@debian.org>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or 
8
 
 * modify it under the terms of the GNU General Public License as 
9
 
 * published by the Free Software Foundation; either version 2 of the
10
 
 * License, or (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
 
 * USA
21
 
 */
22
 
 
23
 
#include <X11/Xlib.h>
24
 
#include <gdk/gdkx.h>
25
 
#include <gtk/gtk.h>
26
 
#include <string>
27
 
#include "i18n.h"
28
 
#include "rgmisc.h"
29
 
#include <stdio.h>
30
 
#include <cstdlib>
31
 
#include <cstring>
32
 
 
33
 
 
34
 
void RGFlushInterface()
35
 
{
36
 
   XSync(gdk_display, False);
37
 
 
38
 
   while (gtk_events_pending()) {
39
 
      gtk_main_iteration();
40
 
   }
41
 
}
42
 
 
43
 
/*
44
 
 * SizeToStr: Converts a size long into a human-readable SI string
45
 
 * ----------------------------------------------------
46
 
 * A maximum of four digits are shown before conversion to the next highest
47
 
 * unit. The maximum length of the string will be five characters unless the
48
 
 * size is more than ten yottabytes.
49
 
 *
50
 
 * mvo: we use out own SizeToStr function as the SI spec says we need a 
51
 
 *      space between the number and the unit (this isn't the case in stock apt
52
 
 */
53
 
string SizeToStr(double Size)
54
 
{
55
 
   char S[300];
56
 
   double ASize;
57
 
   if (Size >= 0) {
58
 
      ASize = Size;
59
 
   } else {
60
 
      ASize = -1 * Size;
61
 
   }
62
 
 
63
 
   /* Bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes,
64
 
    * zettabytes, yottabytes.
65
 
    */
66
 
   char Ext[] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
67
 
   int I = 0;
68
 
   while (I <= 8) {
69
 
      if (ASize < 100 && I != 0) {
70
 
         snprintf(S, 300, "%.1f %cB", ASize, Ext[I]);
71
 
         break;
72
 
      }
73
 
 
74
 
      if (ASize < 10000) {
75
 
         snprintf(S, 300, "%.0f %cB", ASize, Ext[I]);
76
 
         break;
77
 
      }
78
 
      ASize /= 1000.0;
79
 
      I++;
80
 
   }
81
 
   return S;
82
 
}
83
 
 
84
 
 
85
 
bool is_binary_in_path(const char *program)
86
 
{
87
 
   gchar **path = g_strsplit(getenv("PATH"), ":", 0);
88
 
 
89
 
   for (int i = 0; path[i] != NULL; i++) {
90
 
      char *s = g_strdup_printf("%s/%s", path[i], program);
91
 
      if (FileExists(s)) {
92
 
         g_free(s);
93
 
         g_strfreev(path);
94
 
         return true;
95
 
      }
96
 
      g_free(s);
97
 
   }
98
 
   g_strfreev(path);
99
 
   return false;
100
 
}
101
 
 
102
 
char *gtk_get_string_from_color(GdkColor * colp)
103
 
{
104
 
   static char *_str = NULL;
105
 
 
106
 
   g_free(_str);
107
 
   if (colp == NULL) {
108
 
      _str = g_strdup("");
109
 
      return _str;
110
 
   }
111
 
   _str = g_strdup_printf("#%4X%4X%4X", colp->red, colp->green, colp->blue);
112
 
   for (char *ptr = _str; *ptr; ptr++)
113
 
      if (*ptr == ' ')
114
 
         *ptr = '0';
115
 
 
116
 
   return _str;
117
 
}
118
 
 
119
 
void gtk_get_color_from_string(const char *cpp, GdkColor **colp)
120
 
{
121
 
   GdkColor *new_color;
122
 
   int result;
123
 
 
124
 
   // "" means no color
125
 
   if (strlen(cpp) == 0) {
126
 
      *colp = NULL;
127
 
      return;
128
 
   }
129
 
 
130
 
   GdkColormap *colormap = gdk_colormap_get_system();
131
 
 
132
 
   new_color = g_new(GdkColor, 1);
133
 
   result = gdk_color_parse(cpp, new_color);
134
 
   gdk_colormap_alloc_color(colormap, new_color, FALSE, TRUE);
135
 
   *colp = new_color;
136
 
}
137
 
 
138
 
const char *utf8_to_locale(const char *str)
139
 
{
140
 
   static char *_str = NULL;
141
 
   if (str == NULL)
142
 
      return NULL;
143
 
   if (g_utf8_validate(str, -1, NULL) == false)
144
 
      return NULL;
145
 
   g_free(_str);
146
 
   _str = NULL;
147
 
   _str = g_locale_from_utf8(str, -1, NULL, NULL, NULL);
148
 
   return _str;
149
 
}
150
 
 
151
 
const char *utf8(const char *str)
152
 
{
153
 
   static char *_str = NULL;
154
 
   if (str == NULL)
155
 
      return NULL;
156
 
   if (g_utf8_validate(str, -1, NULL) == true)
157
 
      return str;
158
 
   g_free(_str);
159
 
   _str = NULL;
160
 
   _str = g_locale_to_utf8(str, -1, NULL, NULL, NULL);
161
 
   return _str;
162
 
}
163
 
 
164
 
static GdkPixbuf *
165
 
get_gdk_pixbuf(const gchar *name, int size=16)
166
 
{
167
 
   GtkIconTheme *theme;
168
 
   GdkPixbuf *pixbuf;
169
 
   GError *error = NULL;
170
 
 
171
 
   theme = gtk_icon_theme_get_default();
172
 
   pixbuf = gtk_icon_theme_load_icon(theme, name, size, 
173
 
                                     (GtkIconLookupFlags)0, &error);
174
 
   if (pixbuf == NULL) 
175
 
      std::cerr << "Warning, failed to load: " << name 
176
 
                << error->message << std::endl;
177
 
 
178
 
   return pixbuf;
179
 
}
180
 
 
181
 
GtkWidget *get_gtk_image(const gchar *name, int size)
182
 
{
183
 
   GdkPixbuf *buf;
184
 
   buf = get_gdk_pixbuf(name, size);
185
 
   if(!buf)
186
 
      return NULL;
187
 
   return gtk_image_new_from_pixbuf(buf);
188
 
}
189
 
 
190
 
 
191
 
// -------------------------------------------------------------------
192
 
// RPackageStatus stuff
193
 
RGPackageStatus RGPackageStatus::pkgStatus;
194
 
 
195
 
void RGPackageStatus::initColors()
196
 
{
197
 
   char *default_status_colors[N_STATUS_COUNT] = {
198
 
      "#8ae234",  // install
199
 
      "#4e9a06",  // re-install
200
 
      "#fce94f",  // upgrade
201
 
      "#ad7fa8",  // downgrade
202
 
      "#ef2929",  // remove
203
 
      "#a40000",  // purge
204
 
      NULL,       // available
205
 
      "#a40000",  // available-locked
206
 
      NULL,       // installed-updated
207
 
      NULL,       // installed-outdated
208
 
      "#a40000",  // installed-locked 
209
 
      NULL,       // broken
210
 
      NULL        // new
211
 
   };
212
 
 
213
 
   gchar *config_string;
214
 
   for (int i = 0; i < N_STATUS_COUNT; i++) {
215
 
      config_string = g_strdup_printf("Synaptic::color-%s",
216
 
                                      PackageStatusShortString[i]);
217
 
      gtk_get_color_from_string(_config->
218
 
                                Find(config_string,
219
 
                                     default_status_colors[i]).c_str(),
220
 
                                &StatusColors[i]);
221
 
      g_free(config_string);
222
 
   }
223
 
}
224
 
 
225
 
void RGPackageStatus::initPixbufs()
226
 
{
227
 
   gchar *s;
228
 
   for (int i = 0; i < N_STATUS_COUNT; i++) {
229
 
      s = g_strdup_printf("package-%s", PackageStatusShortString[i]);
230
 
      StatusPixbuf[i] = get_gdk_pixbuf(s);
231
 
   }
232
 
   supportedPix = get_gdk_pixbuf("package-supported");
233
 
}
234
 
 
235
 
// class that finds out what do display to get user
236
 
void RGPackageStatus::init()
237
 
{
238
 
   RPackageStatus::init();
239
 
 
240
 
   initColors();
241
 
   initPixbufs();
242
 
}
243
 
 
244
 
 
245
 
GdkColor *RGPackageStatus::getBgColor(RPackage *pkg)
246
 
{
247
 
   return StatusColors[getStatus(pkg)];
248
 
}
249
 
 
250
 
GdkPixbuf *RGPackageStatus::getSupportedPix(RPackage *pkg)
251
 
{
252
 
   if(isSupported(pkg))
253
 
      return supportedPix;
254
 
   else
255
 
      return NULL;
256
 
}
257
 
 
258
 
GdkPixbuf *RGPackageStatus::getPixbuf(RPackage *pkg)
259
 
{
260
 
   return StatusPixbuf[getStatus(pkg)];
261
 
}
262
 
 
263
 
void RGPackageStatus::setColor(int i, GdkColor * new_color)
264
 
{
265
 
   StatusColors[i] = new_color;
266
 
}
267
 
 
268
 
void RGPackageStatus::saveColors()
269
 
{
270
 
   gchar *color_string, *config_string;
271
 
   for (int i = 0; i < N_STATUS_COUNT; i++) {
272
 
      color_string = gtk_get_string_from_color(StatusColors[i]);
273
 
      config_string = g_strdup_printf("Synaptic::color-%s",
274
 
                                      PackageStatusShortString[i]);
275
 
 
276
 
      _config->Set(config_string, color_string);
277
 
      g_free(config_string);
278
 
   }
279
 
}
280
 
 
281
 
// vim:ts=3:sw=3:et