~jibel/synaptic/bugs.309906.385739.403165

« back to all changes in this revision

Viewing changes to gtk/rgutils.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 <stdio.h>
 
28
#include <cstdlib>
 
29
#include <cstring>
 
30
 
 
31
#include "i18n.h"
 
32
#include "rgutils.h"
 
33
 
 
34
 
 
35
void RGFlushInterface()
 
36
{
 
37
   XSync(gdk_display, False);
 
38
 
 
39
   while (gtk_events_pending()) {
 
40
      gtk_main_iteration();
 
41
   }
 
42
}
 
43
 
 
44
/*
 
45
 * SizeToStr: Converts a size long into a human-readable SI string
 
46
 * ----------------------------------------------------
 
47
 * A maximum of four digits are shown before conversion to the next highest
 
48
 * unit. The maximum length of the string will be five characters unless the
 
49
 * size is more than ten yottabytes.
 
50
 *
 
51
 * mvo: we use out own SizeToStr function as the SI spec says we need a 
 
52
 *      space between the number and the unit (this isn't the case in stock apt
 
53
 */
 
54
string SizeToStr(double Size)
 
55
{
 
56
   char S[300];
 
57
   double ASize;
 
58
   if (Size >= 0) {
 
59
      ASize = Size;
 
60
   } else {
 
61
      ASize = -1 * Size;
 
62
   }
 
63
 
 
64
   /* Bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes,
 
65
    * zettabytes, yottabytes.
 
66
    */
 
67
   char Ext[] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
 
68
   int I = 0;
 
69
   while (I <= 8) {
 
70
      if (ASize < 100 && I != 0) {
 
71
         snprintf(S, 300, "%.1f %cB", ASize, Ext[I]);
 
72
         break;
 
73
      }
 
74
 
 
75
      if (ASize < 10000) {
 
76
         snprintf(S, 300, "%.0f %cB", ASize, Ext[I]);
 
77
         break;
 
78
      }
 
79
      ASize /= 1000.0;
 
80
      I++;
 
81
   }
 
82
   return S;
 
83
}
 
84
 
 
85
 
 
86
bool is_binary_in_path(const char *program)
 
87
{
 
88
   gchar **path = g_strsplit(getenv("PATH"), ":", 0);
 
89
 
 
90
   for (int i = 0; path[i] != NULL; i++) {
 
91
      char *s = g_strdup_printf("%s/%s", path[i], program);
 
92
      if (FileExists(s)) {
 
93
         g_free(s);
 
94
         g_strfreev(path);
 
95
         return true;
 
96
      }
 
97
      g_free(s);
 
98
   }
 
99
   g_strfreev(path);
 
100
   return false;
 
101
}
 
102
 
 
103
char *gtk_get_string_from_color(GdkColor * colp)
 
104
{
 
105
   static char *_str = NULL;
 
106
 
 
107
   g_free(_str);
 
108
   if (colp == NULL) {
 
109
      _str = g_strdup("");
 
110
      return _str;
 
111
   }
 
112
   _str = g_strdup_printf("#%4X%4X%4X", colp->red, colp->green, colp->blue);
 
113
   for (char *ptr = _str; *ptr; ptr++)
 
114
      if (*ptr == ' ')
 
115
         *ptr = '0';
 
116
 
 
117
   return _str;
 
118
}
 
119
 
 
120
void gtk_get_color_from_string(const char *cpp, GdkColor **colp)
 
121
{
 
122
   GdkColor *new_color;
 
123
   int result;
 
124
 
 
125
   // "" means no color
 
126
   if (strlen(cpp) == 0) {
 
127
      *colp = NULL;
 
128
      return;
 
129
   }
 
130
 
 
131
   GdkColormap *colormap = gdk_colormap_get_system();
 
132
 
 
133
   new_color = g_new(GdkColor, 1);
 
134
   result = gdk_color_parse(cpp, new_color);
 
135
   gdk_colormap_alloc_color(colormap, new_color, FALSE, TRUE);
 
136
   *colp = new_color;
 
137
}
 
138
 
 
139
const char *utf8_to_locale(const char *str)
 
140
{
 
141
   static char *_str = NULL;
 
142
   if (str == NULL)
 
143
      return NULL;
 
144
   if (g_utf8_validate(str, -1, NULL) == false)
 
145
      return NULL;
 
146
   g_free(_str);
 
147
   _str = NULL;
 
148
   _str = g_locale_from_utf8(str, -1, NULL, NULL, NULL);
 
149
   return _str;
 
150
}
 
151
 
 
152
const char *utf8(const char *str)
 
153
{
 
154
   static char *_str = NULL;
 
155
   if (str == NULL)
 
156
      return NULL;
 
157
   if (g_utf8_validate(str, -1, NULL) == true)
 
158
      return str;
 
159
   g_free(_str);
 
160
   _str = NULL;
 
161
   _str = g_locale_to_utf8(str, -1, NULL, NULL, NULL);
 
162
   return _str;
 
163
}
 
164
 
 
165
 
 
166
 
 
167
 
 
168
// vim:ts=3:sw=3:et