[280] | 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 | |
---|
| 31 | |
---|
| 32 | void RGFlushInterface() |
---|
| 33 | { |
---|
| 34 | XSync(gdk_display, False); |
---|
| 35 | |
---|
| 36 | while (gtk_events_pending()) { |
---|
| 37 | gtk_main_iteration(); |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /* |
---|
| 42 | * SizeToStr: Converts a size long into a human-readable SI string |
---|
| 43 | * ---------------------------------------------------- |
---|
| 44 | * A maximum of four digits are shown before conversion to the next highest |
---|
| 45 | * unit. The maximum length of the string will be five characters unless the |
---|
| 46 | * size is more than ten yottabytes. |
---|
| 47 | * |
---|
| 48 | * mvo: we use out own SizeToStr function as the SI spec says we need a |
---|
| 49 | * space between the number and the unit (this isn't the case in stock apt |
---|
| 50 | */ |
---|
| 51 | string SizeToStr(double Size) |
---|
| 52 | { |
---|
| 53 | char S[300]; |
---|
| 54 | double ASize; |
---|
| 55 | if (Size >= 0) { |
---|
| 56 | ASize = Size; |
---|
| 57 | } else { |
---|
| 58 | ASize = -1 * Size; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /* Bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, |
---|
| 62 | * zettabytes, yottabytes. |
---|
| 63 | */ |
---|
| 64 | char Ext[] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; |
---|
| 65 | int I = 0; |
---|
| 66 | while (I <= 8) { |
---|
| 67 | if (ASize < 100 && I != 0) { |
---|
| 68 | snprintf(S, 300, "%.1f %cB", ASize, Ext[I]); |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | if (ASize < 10000) { |
---|
| 73 | snprintf(S, 300, "%.0f %cB", ASize, Ext[I]); |
---|
| 74 | break; |
---|
| 75 | } |
---|
| 76 | ASize /= 1000.0; |
---|
| 77 | I++; |
---|
| 78 | } |
---|
| 79 | return S; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | bool is_binary_in_path(const char *program) |
---|
| 84 | { |
---|
| 85 | gchar **path = g_strsplit(getenv("PATH"), ":", 0); |
---|
| 86 | |
---|
| 87 | for (int i = 0; path[i] != NULL; i++) { |
---|
| 88 | char *s = g_strdup_printf("%s/%s", path[i], program); |
---|
| 89 | if (FileExists(s)) { |
---|
| 90 | g_free(s); |
---|
| 91 | g_strfreev(path); |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | g_free(s); |
---|
| 95 | } |
---|
| 96 | g_strfreev(path); |
---|
| 97 | return false; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | char *gtk_get_string_from_color(GdkColor * colp) |
---|
| 101 | { |
---|
| 102 | static char *_str = NULL; |
---|
| 103 | |
---|
| 104 | g_free(_str); |
---|
| 105 | if (colp == NULL) { |
---|
| 106 | _str = g_strdup(""); |
---|
| 107 | return _str; |
---|
| 108 | } |
---|
| 109 | _str = g_strdup_printf("#%4X%4X%4X", colp->red, colp->green, colp->blue); |
---|
| 110 | for (char *ptr = _str; *ptr; ptr++) |
---|
| 111 | if (*ptr == ' ') |
---|
| 112 | *ptr = '0'; |
---|
| 113 | |
---|
| 114 | return _str; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void gtk_get_color_from_string(const char *cpp, GdkColor **colp) |
---|
| 118 | { |
---|
| 119 | GdkColor *new_color; |
---|
| 120 | int result; |
---|
| 121 | |
---|
| 122 | // "" means no color |
---|
| 123 | if (strlen(cpp) == 0) { |
---|
| 124 | *colp = NULL; |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | GdkColormap *colormap = gdk_colormap_get_system(); |
---|
| 129 | |
---|
| 130 | new_color = g_new(GdkColor, 1); |
---|
| 131 | result = gdk_color_parse(cpp, new_color); |
---|
| 132 | gdk_colormap_alloc_color(colormap, new_color, FALSE, TRUE); |
---|
| 133 | *colp = new_color; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | const char *utf8_to_locale(const char *str) |
---|
| 137 | { |
---|
| 138 | static char *_str = NULL; |
---|
| 139 | if (str == NULL) |
---|
| 140 | return NULL; |
---|
| 141 | if (g_utf8_validate(str, -1, NULL) == false) |
---|
| 142 | return NULL; |
---|
| 143 | g_free(_str); |
---|
| 144 | _str = NULL; |
---|
| 145 | _str = g_locale_from_utf8(str, -1, NULL, NULL, NULL); |
---|
| 146 | return _str; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | const char *utf8(const char *str) |
---|
| 150 | { |
---|
| 151 | static char *_str = NULL; |
---|
| 152 | if (str == NULL) |
---|
| 153 | return NULL; |
---|
| 154 | if (g_utf8_validate(str, -1, NULL) == true) |
---|
| 155 | return str; |
---|
| 156 | g_free(_str); |
---|
| 157 | _str = NULL; |
---|
| 158 | _str = g_convert(str, -1, "UTF-8", "EUC-JP", NULL, NULL, NULL); |
---|
| 159 | return _str; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | GtkWidget *get_gtk_image(const char *name) |
---|
| 163 | { |
---|
| 164 | gchar *filename; |
---|
| 165 | GtkWidget *img; |
---|
| 166 | filename = g_strdup_printf("../pixmaps/%s.png", name); |
---|
| 167 | if (!FileExists(filename)) { |
---|
| 168 | g_free(filename); |
---|
| 169 | filename = g_strdup_printf(SYNAPTIC_PIXMAPDIR "%s.png", name); |
---|
| 170 | } |
---|
| 171 | img = gtk_image_new_from_file(filename); |
---|
| 172 | if (img == NULL) |
---|
| 173 | std::cerr << "Warning, failed to load: " << filename << std::endl; |
---|
| 174 | g_free(filename); |
---|
| 175 | return img; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | // ------------------------------------------------------------------- |
---|
| 180 | // RPackageStatus stuff |
---|
| 181 | RGPackageStatus RGPackageStatus::pkgStatus; |
---|
| 182 | |
---|
| 183 | void RGPackageStatus::initColors() |
---|
| 184 | { |
---|
| 185 | char *default_status_colors[N_STATUS_COUNT] = { |
---|
| 186 | "#83a67f", // install |
---|
| 187 | "#83a67f", // re-install |
---|
| 188 | "#eed680", // upgrade |
---|
| 189 | "#ada7c8", // downgrade |
---|
| 190 | "#c1665a", // remove |
---|
| 191 | "#df421e", // purge |
---|
| 192 | NULL, // available |
---|
| 193 | "#bab5ab", // available-locked |
---|
| 194 | NULL, // installed-updated |
---|
| 195 | NULL, // installed-outdated |
---|
| 196 | "#bab5ab", // installed-locked |
---|
| 197 | NULL, // broken |
---|
| 198 | NULL // new |
---|
| 199 | }; |
---|
| 200 | |
---|
| 201 | gchar *config_string; |
---|
| 202 | for (int i = 0; i < N_STATUS_COUNT; i++) { |
---|
| 203 | config_string = g_strdup_printf("Synaptic::color-%s", |
---|
| 204 | PackageStatusShortString[i]); |
---|
| 205 | gtk_get_color_from_string(_config-> |
---|
| 206 | Find(config_string, |
---|
| 207 | default_status_colors[i]).c_str(), |
---|
| 208 | &StatusColors[i]); |
---|
| 209 | g_free(config_string); |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void RGPackageStatus::initPixbufs() |
---|
| 214 | { |
---|
| 215 | gchar *filename = NULL; |
---|
| 216 | |
---|
| 217 | for (int i = 0; i < N_STATUS_COUNT; i++) { |
---|
| 218 | filename = g_strdup_printf("../pixmaps/package-%s.png", |
---|
| 219 | PackageStatusShortString[i]); |
---|
| 220 | if (!FileExists(filename)) { |
---|
| 221 | g_free(filename); |
---|
| 222 | filename = g_strdup_printf(SYNAPTIC_PIXMAPDIR "package-%s.png", |
---|
| 223 | PackageStatusShortString[i]); |
---|
| 224 | } |
---|
| 225 | StatusPixbuf[i] = gdk_pixbuf_new_from_file(filename, NULL); |
---|
| 226 | if (StatusPixbuf[i] == NULL) |
---|
| 227 | std::cerr << "Warning, failed to load: " << filename << std::endl; |
---|
| 228 | g_free(filename); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | filename = "../pixmaps/package-supported.png"; |
---|
| 232 | if (!FileExists(filename)) |
---|
| 233 | filename = SYNAPTIC_PIXMAPDIR "package-supported.png"; |
---|
| 234 | |
---|
| 235 | supportedPix = gdk_pixbuf_new_from_file(filename, NULL); |
---|
| 236 | if (supportedPix == NULL) |
---|
| 237 | std::cerr << "Warning, failed to load: " << filename << std::endl; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | // class that finds out what do display to get user |
---|
| 241 | void RGPackageStatus::init() |
---|
| 242 | { |
---|
| 243 | RPackageStatus::init(); |
---|
| 244 | |
---|
| 245 | initColors(); |
---|
| 246 | initPixbufs(); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | GdkColor *RGPackageStatus::getBgColor(RPackage *pkg) |
---|
| 251 | { |
---|
| 252 | return StatusColors[getStatus(pkg)]; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | GdkPixbuf *RGPackageStatus::getSupportedPix(RPackage *pkg) |
---|
| 256 | { |
---|
| 257 | if(isSupported(pkg)) |
---|
| 258 | return supportedPix; |
---|
| 259 | else |
---|
| 260 | return NULL; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | GdkPixbuf *RGPackageStatus::getPixbuf(RPackage *pkg) |
---|
| 264 | { |
---|
| 265 | return StatusPixbuf[getStatus(pkg)]; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | void RGPackageStatus::setColor(int i, GdkColor * new_color) |
---|
| 269 | { |
---|
| 270 | StatusColors[i] = new_color; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | void RGPackageStatus::saveColors() |
---|
| 274 | { |
---|
| 275 | gchar *color_string, *config_string; |
---|
| 276 | for (int i = 0; i < N_STATUS_COUNT; i++) { |
---|
| 277 | color_string = gtk_get_string_from_color(StatusColors[i]); |
---|
| 278 | config_string = g_strdup_printf("Synaptic::color-%s", |
---|
| 279 | PackageStatusShortString[i]); |
---|
| 280 | |
---|
| 281 | _config->Set(config_string, color_string); |
---|
| 282 | g_free(config_string); |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | // vim:ts=3:sw=3:et |
---|